option6_int.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 OPTION6_INT_H
  15. #define OPTION6_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 DHCPv6 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 Option6Int: public Option {
  35. public:
  36. /// @brief Constructor.
  37. ///
  38. /// @param type option type.
  39. /// @param value option value.
  40. ///
  41. /// @throw isc::dhcp::InvalidDataType if data field type provided
  42. /// as template parameter is not a supported integer type.
  43. Option6Int(uint16_t type, T value)
  44. : Option(Option::V6, type), value_(value) {
  45. if (!OptionDataTypeTraits<T>::integer_type) {
  46. isc_throw(dhcp::InvalidDataType, "non-integer type");
  47. }
  48. }
  49. /// @brief Constructor.
  50. ///
  51. /// This constructor creates option from a buffer. This construtor
  52. /// may throw exception if \ref unpack function throws during buffer
  53. /// parsing.
  54. ///
  55. /// @param type option type.
  56. /// @param begin iterator to first byte of option data.
  57. /// @param end iterator to end of option data (first byte after option end).
  58. ///
  59. /// @throw isc::OutOfRange if provided buffer is shorter than data size.
  60. /// @throw isc::dhcp::InvalidDataType if data field type provided
  61. /// as template parameter is not a supported integer type.
  62. Option6Int(uint16_t type, OptionBufferConstIter begin,
  63. OptionBufferConstIter end)
  64. : Option(Option::V6, type) {
  65. if (!OptionDataTypeTraits<T>::integer_type) {
  66. isc_throw(dhcp::InvalidDataType, "non-integer type");
  67. }
  68. unpack(begin, end);
  69. }
  70. /// Writes option in wire-format to buf, returns pointer to first unused
  71. /// byte after stored option.
  72. ///
  73. /// @param [out] buf buffer (option will be stored here)
  74. ///
  75. /// @throw isc::dhcp::InvalidDataType if size of a data field type is not
  76. /// equal to 1, 2 or 4 bytes. The data type is not checked in this function
  77. /// because it is checked in a constructor.
  78. void pack(isc::util::OutputBuffer& buf) {
  79. buf.writeUint16(type_);
  80. buf.writeUint16(len() - OPTION6_HDR_LEN);
  81. // Depending on the data type length we use different utility functions
  82. // writeUint16 or writeUint32 which write the data in the network byte
  83. // order to the provided buffer. The same functions can be safely used
  84. // for either unsiged or signed integers so there is not need to create
  85. // special cases for intX_t types.
  86. switch (OptionDataTypeTraits<T>::len) {
  87. case 1:
  88. buf.writeUint8(value_);
  89. break;
  90. case 2:
  91. buf.writeUint16(value_);
  92. break;
  93. case 4:
  94. buf.writeUint32(value_);
  95. break;
  96. default:
  97. isc_throw(dhcp::InvalidDataType, "non-integer type");
  98. }
  99. LibDHCP::packOptions6(buf, options_);
  100. }
  101. /// @brief Parses received buffer
  102. ///
  103. /// Parses received buffer and returns offset to the first unused byte after
  104. /// parsed option.
  105. ///
  106. /// @param begin iterator to first byte of option data
  107. /// @param end iterator to end of option data (first byte after option end)
  108. ///
  109. /// @throw isc::OutOfRange if provided buffer is shorter than data size.
  110. /// @throw isc::dhcp::InvalidDataType if size of a data field type is not
  111. /// equal to 1, 2 or 4 bytes. The data type is not checked in this function
  112. /// because it is checked in a constructor.
  113. virtual void unpack(OptionBufferConstIter begin, OptionBufferConstIter end) {
  114. if (distance(begin, end) < sizeof(T)) {
  115. isc_throw(OutOfRange, "Option " << getType() << " truncated");
  116. }
  117. // @todo consider what to do if buffer is longer than data type.
  118. // Depending on the data type length we use different utility functions
  119. // readUint16 or readUint32 which read the data laid in the network byte
  120. // order from the provided buffer. The same functions can be safely used
  121. // for either unsiged or signed integers so there is not need to create
  122. // special cases for intX_t types.
  123. int data_size_len = OptionDataTypeTraits<T>::len;
  124. switch (data_size_len) {
  125. case 1:
  126. value_ = *begin;
  127. break;
  128. case 2:
  129. value_ = isc::util::readUint16(&(*begin));
  130. break;
  131. case 4:
  132. value_ = isc::util::readUint32(&(*begin));
  133. break;
  134. default:
  135. isc_throw(dhcp::InvalidDataType, "non-integer type");
  136. }
  137. // Use local variable to set a new value for this iterator.
  138. // When using OptionDataTypeTraits<T>::len directly some versions
  139. // of clang complain about unresolved reference to
  140. // OptionDataTypeTraits structure during linking.
  141. begin += data_size_len;
  142. LibDHCP::unpackOptions6(OptionBuffer(begin, end), options_);
  143. }
  144. /// @brief Set option value.
  145. ///
  146. /// @param value new option value.
  147. void setValue(T value) { value_ = value; }
  148. /// @brief Return option value.
  149. ///
  150. /// @return option value.
  151. T getValue() const { return value_; }
  152. /// @brief returns complete length of option
  153. ///
  154. /// Returns length of this option, including option header and suboptions
  155. ///
  156. /// @return length of this option
  157. virtual uint16_t len() {
  158. uint16_t length = OPTION6_HDR_LEN + sizeof(T);
  159. // length of all suboptions
  160. for (Option::OptionCollection::iterator it = options_.begin();
  161. it != options_.end();
  162. ++it) {
  163. length += (*it).second->len();
  164. }
  165. return (length);
  166. }
  167. private:
  168. T value_; ///< Value conveyed by the option.
  169. };
  170. } // isc::dhcp namespace
  171. } // isc namespace
  172. #endif // OPTION6_INT_H