option_custom.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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_CUSTOM_H
  15. #define OPTION_CUSTOM_H
  16. #include <dhcp/option.h>
  17. #include <dhcp/option_definition.h>
  18. #include <util/io_utilities.h>
  19. namespace isc {
  20. namespace dhcp {
  21. /// @brief Option with defined data fields represented as buffers that can
  22. /// be accessed using data field index.
  23. ///
  24. /// This class represents an option which has defined structure: data fields
  25. /// of specific types and order. Those fields can be accessed using indexes,
  26. /// where index 0 represents first data field within an option. The last
  27. /// field can be accessed using index equal to 'number of fields' - 1.
  28. /// Internally, the option data is stored as a collection of OptionBuffer
  29. /// objects, each representing data for a particular data field. This data
  30. /// can be converted to the actual data type using methods implemented
  31. /// within this class. This class is used to represent those options that
  32. /// can't be represented by any other specialized class (this excludes the
  33. /// Option class which is generic and can be used to represent any option).
  34. class OptionCustom : public Option {
  35. public:
  36. /// @brief Constructor, used for options to be sent.
  37. ///
  38. /// This constructor creates an instance of an option with default
  39. /// data set for all data fields. The option buffers are allocated
  40. /// according to data size being stored in particular data fields.
  41. /// For variable size data empty buffers are created.
  42. ///
  43. /// @param def option definition.
  44. /// @param u specifies universe (V4 or V6)
  45. OptionCustom(const OptionDefinition& def, Universe u);
  46. /// @brief Constructor, used for options to be sent.
  47. ///
  48. /// This constructor creates an instance of an option from the whole
  49. /// supplied buffer. This constructor is mainly used to create an
  50. /// instances of options to be stored in outgoing DHCP packets.
  51. /// The buffer used to create the instance of an option can be
  52. /// created from the option data specified in server's configuration.
  53. ///
  54. /// @param def option definition.
  55. /// @param u specifies universe (V4 or V6).
  56. /// @param data content of the option.
  57. ///
  58. /// @throw OutOfRange if option buffer is truncated.
  59. ///
  60. /// @todo list all exceptions thrown by ctor.
  61. OptionCustom(const OptionDefinition& def, Universe u, const OptionBuffer& data);
  62. /// @brief Constructor, used for received options.
  63. ///
  64. /// This constructor creates an instance an option from the portion
  65. /// of the buffer specified by iterators. This is mainly useful when
  66. /// parsing received packets. Such packets are represented by a single
  67. /// buffer holding option data and all sub options. Methods that are
  68. /// parsing a packet, supply relevant portions of the packet buffer
  69. /// to this constructor to create option instances out of it.
  70. ///
  71. /// @param def option definition.
  72. /// @param u specifies universe (V4 or V6).
  73. /// @param first iterator to the first element that should be copied.
  74. /// @param last iterator to the next element after the last one
  75. /// to be copied.
  76. ///
  77. /// @throw OutOfRange if option buffer is truncated.
  78. ///
  79. /// @todo list all exceptions thrown by ctor.
  80. OptionCustom(const OptionDefinition& def, Universe u,
  81. OptionBufferConstIter first, OptionBufferConstIter last);
  82. /// @brief Create new buffer and set its value as an IP address.
  83. ///
  84. /// @param address IPv4 or IPv6 address to be written to
  85. /// a buffer being created.
  86. void addArrayDataField(const asiolink::IOAddress& address);
  87. /// @brief Create new buffer and store boolean value in it.
  88. ///
  89. /// @param value value to be stored in the created buffer.
  90. void addArrayDataField(const bool value);
  91. /// @brief Create new buffer and store integer value in it.
  92. ///
  93. /// @param value value to be stored in the created buffer.
  94. /// @tparam T integer type of the value being stored.
  95. template<typename T>
  96. void addArrayDataField(const T value) {
  97. checkArrayType();
  98. OptionDataType data_type = definition_.getType();
  99. if (OptionDataTypeTraits<T>::type != data_type) {
  100. isc_throw(isc::dhcp::InvalidDataType,
  101. "specified data type " << data_type << " does not"
  102. " match the data type in an option definition");
  103. }
  104. OptionBuffer buf;
  105. OptionDataTypeUtil::writeInt<T>(value, buf);
  106. buffers_.push_back(buf);
  107. }
  108. /// @brief Return a number of the data fields.
  109. ///
  110. /// @return number of data fields held by the option.
  111. uint32_t getDataFieldsNum() const { return (buffers_.size()); }
  112. /// @brief Read a buffer as IP address.
  113. ///
  114. /// @param index buffer index.
  115. ///
  116. /// @return IP address read from a buffer.
  117. /// @throw isc::OutOfRange if index is out of range.
  118. asiolink::IOAddress readAddress(const uint32_t index = 0) const;
  119. /// @brief Write an IP address into a buffer.
  120. ///
  121. /// @param address IP address being written.
  122. /// @param index buffer index.
  123. ///
  124. /// @throw isc::OutOfRange if index is out of range.
  125. /// @throw isc::dhcp::BadDataTypeCast if IP address is invalid.
  126. void writeAddress(const asiolink::IOAddress& address,
  127. const uint32_t index = 0);
  128. /// @brief Read a buffer as binary data.
  129. ///
  130. /// @param index buffer index.
  131. ///
  132. /// @throw isc::OutOfRange if index is out of range.
  133. /// @return read buffer holding binary data.
  134. const OptionBuffer& readBinary(const uint32_t index = 0) const;
  135. /// @brief Write binary data into a buffer.
  136. ///
  137. /// @param buf buffer holding binary data to be written.
  138. /// @param index buffer index.
  139. void writeBinary(const OptionBuffer& buf, const uint32_t index = 0);
  140. /// @brief Read a buffer as boolean value.
  141. ///
  142. /// @param index buffer index.
  143. ///
  144. /// @throw isc::OutOfRange if index is out of range.
  145. /// @return read boolean value.
  146. bool readBoolean(const uint32_t index = 0) const;
  147. /// @brief Write a boolean value into a buffer.
  148. ///
  149. /// @param value boolean value to be written.
  150. /// @param index buffer index.
  151. ///
  152. /// @throw isc::OutOfRange if index is out of range.
  153. void writeBoolean(const bool value, const uint32_t index = 0);
  154. /// @brief Read a buffer as FQDN.
  155. ///
  156. /// @param index buffer index.
  157. /// @param [out] len number of bytes read from a buffer.
  158. ///
  159. /// @throw isc::OutOfRange if buffer index is out of range.
  160. /// @throw isc::dhcp::BadDataTypeCast if a buffer being read
  161. /// does not hold a valid FQDN.
  162. /// @return string representation if FQDN.
  163. std::string readFqdn(const uint32_t index = 0) const;
  164. /// @brief Write an FQDN into a buffer.
  165. ///
  166. /// @param fqdn text representation of FQDN.
  167. /// @param index buffer index.
  168. ///
  169. /// @throw isc::OutOfRange if index is out of range.
  170. void writeFqdn(const std::string& fqdn, const uint32_t index = 0);
  171. /// @brief Read a buffer as integer value.
  172. ///
  173. /// @param index buffer index.
  174. /// @tparam integer type of a value being returned.
  175. ///
  176. /// @throw isc::OutOfRange if index is out of range.
  177. /// @throw isc::dhcp::InvalidDataType if T is invalid.
  178. /// @return read integer value.
  179. template<typename T>
  180. T readInteger(const uint32_t index = 0) const {
  181. // Check that the index is not out of range.
  182. checkIndex(index);
  183. // Check that T points to a valid integer type and this type
  184. // is consistent with an option definition.
  185. checkDataType<T>(index);
  186. // When we created the buffer we have checked that it has a
  187. // valid size so this condition here should be always fulfiled.
  188. assert(buffers_[index].size() == OptionDataTypeTraits<T>::len);
  189. // Read an integer value.
  190. return (OptionDataTypeUtil::readInt<T>(buffers_[index]));
  191. }
  192. /// @brief Write an integer value into a buffer.
  193. ///
  194. /// @param value integer value to be written.
  195. /// @param index buffer index.
  196. /// @tparam T integer type of a value being written.
  197. ///
  198. /// @throw isc::OutOfRange if index is out of range.
  199. /// @throw isc::dhcp::InvalidDataType if T is invalid.
  200. template<typename T>
  201. void writeInteger(const T value, const uint32_t index = 0) {
  202. // Check that the index is not out of range.
  203. checkIndex(index);
  204. // Check that T points to a valid integer type and this type
  205. // is consistent with an option definition.
  206. checkDataType<T>(index);
  207. // Get some temporary buffer.
  208. OptionBuffer buf;
  209. // Try to write to the buffer.
  210. OptionDataTypeUtil::writeInt<T>(value, buf);
  211. // If successful, replace the old buffer with new one.
  212. std::swap(buffers_[index], buf);
  213. }
  214. /// @brief Read a buffer as string value.
  215. ///
  216. /// @param index buffer index.
  217. ///
  218. /// @return string value read from buffer.
  219. /// @throw isc::OutOfRange if index is out of range.
  220. std::string readString(const uint32_t index = 0) const;
  221. /// @brief Write a string value into a buffer.
  222. ///
  223. /// @param text the string value to be written.
  224. /// @param buffer index.
  225. void writeString(const std::string& text,
  226. const uint32_t index = 0);
  227. /// @brief Parses received buffer.
  228. ///
  229. /// @param begin iterator to first byte of option data
  230. /// @param end iterator to end of option data (first byte after option end)
  231. virtual void unpack(OptionBufferConstIter begin,
  232. OptionBufferConstIter end);
  233. /// @brief Returns string representation of the option.
  234. ///
  235. /// @param indent number of spaces before printed text.
  236. ///
  237. /// @return string with text representation.
  238. virtual std::string toText(int indent = 0);
  239. /// @brief Returns length of the complete option (data length +
  240. /// DHCPv4/DHCPv6 option header)
  241. ///
  242. /// @return length of the option
  243. virtual uint16_t len();
  244. /// @brief Sets content of this option from buffer.
  245. ///
  246. /// Option will be resized to length of buffer.
  247. ///
  248. /// @param first iterator pointing begining of buffer to copy.
  249. /// @param last iterator pointing to end of buffer to copy.
  250. void setData(const OptionBufferConstIter first,
  251. const OptionBufferConstIter last);
  252. protected:
  253. /// @brief Writes DHCPv4 option in a wire format to a buffer.
  254. ///
  255. /// @param buf output buffer (option will be stored there).
  256. virtual void pack4(isc::util::OutputBuffer& buf);
  257. /// @brief Writes DHCPv6 option in a wire format to a buffer.
  258. ///
  259. /// @param buf output buffer (built options will be stored here)
  260. virtual void pack6(isc::util::OutputBuffer& buf);
  261. private:
  262. /// @brief Verify that the option comprises an array of values.
  263. ///
  264. /// This helper function is used by createArrayEntry functions
  265. /// and throws an exception if the particular option is not
  266. /// an array.
  267. ///
  268. /// @throw isc::InvalidOperation if option is not an array.
  269. inline void checkArrayType() const {
  270. if (!definition_.getArrayType()) {
  271. isc_throw(InvalidOperation, "failed to add new array entry to an"
  272. << " option. The option is not an array.");
  273. }
  274. }
  275. /// @brief Verify that the integer type is consistent with option
  276. /// field type.
  277. ///
  278. /// This convenience function checks that the data type specified as T
  279. /// is consistent with a type of a data field identified by index.
  280. ///
  281. /// @param index data field index.
  282. /// @tparam data type to be validated.
  283. ///
  284. /// @throw isc::dhcp::InvalidDataType if the type is invalid.
  285. template<typename T>
  286. void checkDataType(const uint32_t index) const;
  287. /// @brief Check if data field index is valid.
  288. ///
  289. /// @param index Data field index to check.
  290. ///
  291. /// @throw isc::OutOfRange if index is out of range.
  292. void checkIndex(const uint32_t index) const;
  293. /// @brief Create a collection of non initialized buffers.
  294. void createBuffers();
  295. /// @brief Create collection of buffers representing data field values.
  296. ///
  297. /// @param data_buf a buffer to be parsed.
  298. void createBuffers(const OptionBuffer& data_buf);
  299. /// @brief Return a text representation of a data field.
  300. ///
  301. /// @param data_type data type of a field.
  302. /// @param index data field buffer index within a custom option.
  303. ///
  304. /// @return text representation of a data field.
  305. std::string dataFieldToText(const OptionDataType data_type,
  306. const uint32_t index) const;
  307. /// Option definition used to create an option.
  308. OptionDefinition definition_;
  309. /// The collection of buffers holding data for option fields.
  310. /// The order of buffers corresponds to the order of option
  311. /// fields.
  312. std::vector<OptionBuffer> buffers_;
  313. };
  314. } // namespace isc::dhcp
  315. } // namespace isc
  316. #endif // OPTION_CUSTOM_H