option_int.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef OPTION_INT_H
  15. #define OPTION_INT_H
  16. #include <dhcp/libdhcp++.h>
  17. #include <dhcp/option.h>
  18. #include <dhcp/option_data_types.h>
  19. #include <util/io_utilities.h>
  20. #include <stdint.h>
  21. namespace isc {
  22. namespace dhcp {
  23. /// This template class represents DHCP option with single value.
  24. /// This value is of integer type and can be any of the following:
  25. /// - uint8_t,
  26. /// - uint16_t,
  27. /// - uint32_t,
  28. /// - int8_t,
  29. /// - int16_t,
  30. /// - int32_t.
  31. ///
  32. /// @param T data field type (see above).
  33. template<typename T>
  34. class OptionInt: public Option {
  35. public:
  36. /// @brief Constructor.
  37. ///
  38. /// @param u universe (V4 or V6)
  39. /// @param type option type.
  40. /// @param value option value.
  41. ///
  42. /// @throw isc::dhcp::InvalidDataType if data field type provided
  43. /// as template parameter is not a supported integer type.
  44. OptionInt(Option::Universe u, uint16_t type, T value)
  45. : Option(u, type), value_(value) {
  46. if (!OptionDataTypeTraits<T>::integer_type) {
  47. isc_throw(dhcp::InvalidDataType, "non-integer type");
  48. }
  49. }
  50. /// @brief Constructor.
  51. ///
  52. /// This constructor creates option from a buffer. This construtor
  53. /// may throw exception if \ref unpack function throws during buffer
  54. /// parsing.
  55. ///
  56. /// @param u universe (V4 or V6)
  57. /// @param type option type.
  58. /// @param begin iterator to first byte of option data.
  59. /// @param end iterator to end of option data (first byte after option end).
  60. ///
  61. /// @throw isc::OutOfRange if provided buffer is shorter than data size.
  62. /// @throw isc::dhcp::InvalidDataType if data field type provided
  63. /// as template parameter is not a supported integer type.
  64. OptionInt(Option::Universe u, uint16_t type, OptionBufferConstIter begin,
  65. OptionBufferConstIter end)
  66. : Option(u, type) {
  67. if (!OptionDataTypeTraits<T>::integer_type) {
  68. isc_throw(dhcp::InvalidDataType, "non-integer type");
  69. }
  70. unpack(begin, end);
  71. }
  72. /// Writes option in wire-format to buf, returns pointer to first unused
  73. /// byte after stored option.
  74. ///
  75. /// @param [out] buf buffer (option will be stored here)
  76. ///
  77. /// @throw isc::dhcp::InvalidDataType if size of a data field type is not
  78. /// equal to 1, 2 or 4 bytes. The data type is not checked in this function
  79. /// because it is checked in a constructor.
  80. void pack(isc::util::OutputBuffer& buf) {
  81. // Pack option header.
  82. packHeader(buf);
  83. // Depending on the data type length we use different utility functions
  84. // writeUint16 or writeUint32 which write the data in the network byte
  85. // order to the provided buffer. The same functions can be safely used
  86. // for either unsigned or signed integers so there is not need to create
  87. // special cases for intX_t types.
  88. switch (OptionDataTypeTraits<T>::len) {
  89. case 1:
  90. buf.writeUint8(value_);
  91. break;
  92. case 2:
  93. buf.writeUint16(value_);
  94. break;
  95. case 4:
  96. buf.writeUint32(value_);
  97. break;
  98. default:
  99. isc_throw(dhcp::InvalidDataType, "non-integer type");
  100. }
  101. packOptions(buf);
  102. }
  103. /// @brief Parses received buffer
  104. ///
  105. /// Parses received buffer and returns offset to the first unused byte after
  106. /// parsed option.
  107. ///
  108. /// @param begin iterator to first byte of option data
  109. /// @param end iterator to end of option data (first byte after option end)
  110. ///
  111. /// @throw isc::OutOfRange if provided buffer is shorter than data size.
  112. /// @throw isc::dhcp::InvalidDataType if size of a data field type is not
  113. /// equal to 1, 2 or 4 bytes. The data type is not checked in this function
  114. /// because it is checked in a constructor.
  115. virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
  116. if (distance(begin, end) < sizeof(T)) {
  117. isc_throw(OutOfRange, "Option " << getType() << " truncated");
  118. }
  119. // @todo consider what to do if buffer is longer than data type.
  120. // Depending on the data type length we use different utility functions
  121. // readUint16 or readUint32 which read the data laid in the network byte
  122. // order from the provided buffer. The same functions can be safely used
  123. // for either unsigned or signed integers so there is not need to create
  124. // special cases for intX_t types.
  125. int data_size_len = OptionDataTypeTraits<T>::len;
  126. switch (data_size_len) {
  127. case 1:
  128. value_ = *begin;
  129. break;
  130. case 2:
  131. value_ = isc::util::readUint16(&(*begin));
  132. break;
  133. case 4:
  134. value_ = isc::util::readUint32(&(*begin));
  135. break;
  136. default:
  137. isc_throw(dhcp::InvalidDataType, "non-integer type");
  138. }
  139. // Use local variable to set a new value for this iterator.
  140. // When using OptionDataTypeTraits<T>::len directly some versions
  141. // of clang complain about unresolved reference to
  142. // OptionDataTypeTraits structure during linking.
  143. begin += data_size_len;
  144. unpackOptions(OptionBuffer(begin, end));
  145. }
  146. /// @brief Set option value.
  147. ///
  148. /// @param value new option value.
  149. void setValue(T value) { value_ = value; }
  150. /// @brief Return option value.
  151. ///
  152. /// @return option value.
  153. T getValue() const { return value_; }
  154. /// @brief returns complete length of option
  155. ///
  156. /// Returns length of this option, including option header and suboptions
  157. ///
  158. /// @return length of this option
  159. virtual uint16_t len() {
  160. // Calculate the length of the header.
  161. uint16_t length = (universe_ == Option::V4) ? OPTION4_HDR_LEN : OPTION6_HDR_LEN;
  162. // The data length is equal to size of T.
  163. length += sizeof(T);;
  164. // length of all suboptions
  165. for (Option::OptionCollection::iterator it = options_.begin();
  166. it != options_.end();
  167. ++it) {
  168. length += (*it).second->len();
  169. }
  170. return (length);
  171. }
  172. private:
  173. T value_; ///< Value conveyed by the option.
  174. };
  175. } // isc::dhcp namespace
  176. } // isc namespace
  177. #endif // OPTION_INT_H