option_definition.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. // Copyright (C) 2012-2013 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_DEFINITION_H
  15. #define OPTION_DEFINITION_H
  16. #include <dhcp/option.h>
  17. #include <dhcp/option_data_types.h>
  18. #include <boost/multi_index/hashed_index.hpp>
  19. #include <boost/multi_index/mem_fun.hpp>
  20. #include <boost/multi_index/sequenced_index.hpp>
  21. #include <boost/multi_index_container.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <map>
  24. namespace isc {
  25. namespace dhcp {
  26. /// @brief Exception to be thrown when invalid option value has been
  27. /// specified for a particular option definition.
  28. class InvalidOptionValue : public Exception {
  29. public:
  30. InvalidOptionValue(const char* file, size_t line, const char* what) :
  31. isc::Exception(file, line, what) { };
  32. };
  33. /// @brief Exception to be thrown when option definition is invalid.
  34. class MalformedOptionDefinition : public Exception {
  35. public:
  36. MalformedOptionDefinition(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) { };
  38. };
  39. /// @brief Exception to be thrown when the particular option definition
  40. /// duplicates existing option definition.
  41. class DuplicateOptionDefinition : public Exception {
  42. public:
  43. DuplicateOptionDefinition(const char* file, size_t line, const char* what) :
  44. isc::Exception(file, line, what) { };
  45. };
  46. /// @brief Forward declaration to OptionDefinition.
  47. class OptionDefinition;
  48. /// @brief Pointer to option definition object.
  49. typedef boost::shared_ptr<OptionDefinition> OptionDefinitionPtr;
  50. /// @brief Forward declaration to OptionInt.
  51. ///
  52. /// This forward declaration is needed to access the OptionInt class without
  53. /// having to include the option_int.h header file. It is required because
  54. /// this header includes libdhcp++.h, and including option_int.h would cause
  55. /// circular inclusion between libdhcp++.h, option_definition.h and
  56. /// option6_int.h.
  57. template<typename T>
  58. class OptionInt;
  59. /// @brief Forward declaration to OptionIntArray.
  60. ///
  61. /// This forward declaration is needed to access the OptionIntArray class
  62. /// without having to include the option_int_array.h header file. It is
  63. /// required because this header includes libdhcp++.h, and including
  64. /// option_int_array.h would cause circular inclusion between libdhcp++.h,
  65. /// option_definition.h and option_int_array.h.
  66. template<typename T>
  67. class OptionIntArray;
  68. /// @brief Base class representing a DHCP option definition.
  69. ///
  70. /// This is a base class representing a DHCP option definition, which describes
  71. /// the format of the option. In particular, it defines:
  72. /// - option name,
  73. /// - option code,
  74. /// - data fields order and their types,
  75. /// - sub options space that the particular option encapsulates.
  76. ///
  77. /// The option type specifies the data type(s) which an option conveys. If
  78. /// this is a single value the option type points to the data type of the
  79. /// value. For example, DHCPv6 option 8 comprises a two-byte option code, a
  80. /// two-byte option length and two-byte field that carries a uint16 value
  81. /// (RFC 3315 - http://ietf.org/rfc/rfc3315.txt). In such a case, the option
  82. /// type is defined as "uint16".
  83. ///
  84. /// When the option has a more complex structure, the option type may be
  85. /// defined as "array", "record" or even "array of records".
  86. ///
  87. /// Array types should be used when the option contains multiple contiguous
  88. /// data values of the same type laid. For example, DHCPv6 option 6 includes
  89. /// multiple fields holding uint16 codes of requested DHCPv6 options (RFC 3315).
  90. /// Such an option can be represented with this class by setting the option
  91. /// type to "uint16" and the array indicator (array_type) to true. The number
  92. /// of elements in the array is effectively unlimited (although it is actually
  93. /// limited by the maximal DHCPv6 option length).
  94. ///
  95. /// Should the option comprise data fields of different types, the "record"
  96. /// option type is used. In such cases the data field types within the record
  97. /// are specified using \ref OptionDefinition::addRecordField.
  98. ///
  99. /// When the OptionDefinition object has been sucessfully created, it can be
  100. /// queried to return the appropriate option factory function for the specified
  101. /// specified option format. There are a number of "standard" factory functions
  102. /// that cover well known (common) formats. If the particular format does not
  103. /// match any common format the generic factory function is returned.
  104. ///
  105. /// The following data type strings are supported:
  106. /// - "empty" (option does not contain data fields)
  107. /// - "boolean"
  108. /// - "int8"
  109. /// - "int16"
  110. /// - "int32"
  111. /// - "uint8"
  112. /// - "uint16"
  113. /// - "uint32"
  114. /// - "ipv4-address" (IPv4 Address)
  115. /// - "ipv6-address" (IPV6 Address)
  116. /// - "string"
  117. /// - "fqdn" (fully qualified name)
  118. /// - "record" (set of data fields of different types)
  119. ///
  120. /// @todo Extend the comment to describe "generic factories".
  121. /// @todo Extend this class to use custom namespaces.
  122. /// @todo Extend this class with more factory functions.
  123. class OptionDefinition {
  124. public:
  125. /// List of fields within the record.
  126. typedef std::vector<OptionDataType> RecordFieldsCollection;
  127. /// Const iterator for record data fields.
  128. typedef std::vector<OptionDataType>::const_iterator RecordFieldsConstIter;
  129. /// @brief Constructor.
  130. ///
  131. /// @param name option name.
  132. /// @param code option code.
  133. /// @param type option data type as string.
  134. /// @param array_type array indicator, if true it indicates that the
  135. /// option fields are the array.
  136. explicit OptionDefinition(const std::string& name,
  137. const uint16_t code,
  138. const std::string& type,
  139. const bool array_type = false);
  140. /// @brief Constructor.
  141. ///
  142. /// @param name option name.
  143. /// @param code option code.
  144. /// @param type option data type.
  145. /// @param array_type array indicator, if true it indicates that the
  146. /// option fields are the array.
  147. explicit OptionDefinition(const std::string& name,
  148. const uint16_t code,
  149. const OptionDataType type,
  150. const bool array_type = false);
  151. /// @brief Constructor.
  152. ///
  153. /// This constructor sets the name of the option space that is
  154. /// encapsulated by this option. The encapsulated option space
  155. /// identifies sub-options that are carried within this option.
  156. /// This constructor does not allow to set array indicator
  157. /// because options comprising an array of data fields must
  158. /// not be used with sub-options.
  159. ///
  160. /// @param name option name.
  161. /// @param code option code.
  162. /// @param type option data type given as string.
  163. /// @param encapsulated_space name of the option space being
  164. /// encapsulated by this option.
  165. explicit OptionDefinition(const std::string& name,
  166. const uint16_t code,
  167. const std::string& type,
  168. const char* encapsulated_space);
  169. /// @brief Constructor.
  170. ///
  171. /// This constructor sets the name of the option space that is
  172. /// encapsulated by this option. The encapsulated option space
  173. /// identifies sub-options that are carried within this option.
  174. /// This constructor does not allow to set array indicator
  175. /// because options comprising an array of data fields must
  176. /// not be used with sub-options.
  177. ///
  178. /// @param name option name.
  179. /// @param code option code.
  180. /// @param type option data type.
  181. /// @param encapsulated_space name of the option space being
  182. /// encapsulated by this option.
  183. explicit OptionDefinition(const std::string& name,
  184. const uint16_t code,
  185. const OptionDataType type,
  186. const char* encapsulated_space);
  187. /// @brief Adds data field to the record.
  188. ///
  189. /// @param data_type_name name of the data type for the field.
  190. ///
  191. /// @throw isc::InvalidOperation if option type is not set to RECORD_TYPE.
  192. /// @throw isc::BadValue if specified invalid data type.
  193. void addRecordField(const std::string& data_type_name);
  194. /// @brief Adds data field to the record.
  195. ///
  196. /// @param data_type data type for the field.
  197. ///
  198. /// @throw isc::InvalidOperation if option type is not set to RECORD_TYPE.
  199. /// @throw isc::BadValue if specified invalid data type.
  200. void addRecordField(const OptionDataType data_type);
  201. /// @brief Return array type indicator.
  202. ///
  203. /// The method returns the bool value to indicate whether the option is a
  204. /// a single value or an array of values.
  205. ///
  206. /// @return true if option comprises an array of values.
  207. bool getArrayType() const { return (array_type_); }
  208. /// @brief Return option code.
  209. ///
  210. /// @return option code.
  211. uint16_t getCode() const { return (code_); }
  212. /// @brief Return name of the encapsulated option space.
  213. ///
  214. /// @return name of the encapsulated option space.
  215. std::string getEncapsulatedSpace() const {
  216. return (encapsulated_space_);
  217. }
  218. /// @brief Return option name.
  219. ///
  220. /// @return option name.
  221. std::string getName() const { return (name_); }
  222. /// @brief Return list of record fields.
  223. ///
  224. /// @return list of record fields.
  225. const RecordFieldsCollection& getRecordFields() const { return (record_fields_); }
  226. /// @brief Return option data type.
  227. ///
  228. /// @return option data type.
  229. OptionDataType getType() const { return (type_); };
  230. /// @brief Check if the option definition is valid.
  231. ///
  232. /// Note that it is a responsibility of the code that created
  233. /// the OptionDefinition object to validate that it is valid.
  234. /// This function will not be called internally anywhere in this
  235. /// class to verify that the option definition is valid. Using
  236. /// invalid option definition to create an instance of the
  237. /// DHCP option leads to undefined behavior.
  238. ///
  239. /// @throw MalformedOptionDefinition option definition is invalid.
  240. void validate() const;
  241. /// @brief Check if specified format is IA_NA option format.
  242. ///
  243. /// @return true if specified format is IA_NA option format.
  244. bool haveIA6Format() const;
  245. /// @brief Check if specified format is IAADDR option format.
  246. ///
  247. /// @return true if specified format is IAADDR option format.
  248. bool haveIAAddr6Format() const;
  249. /// @brief Check if specified format is IAPREFIX option format.
  250. ///
  251. /// @return true if specified format is IAPREFIX option format.
  252. bool haveIAPrefix6Format() const;
  253. /// @brief Check if specified format is OPTION_CLIENT_FQDN option format.
  254. ///
  255. /// @return true of specified format is OPTION_CLIENT_FQDN option format,
  256. /// false otherwise.
  257. bool haveClientFqdnFormat() const;
  258. /// @brief Check if option has format of the DHCPv4 Client FQDN
  259. /// %Option.
  260. ///
  261. /// The encoding of the domain-name carried by the FQDN option is
  262. /// conditional and is specified in the flags field of the option.
  263. /// The domain-name can be encoded in the ASCII format or canonical
  264. /// wire format. The ASCII format is deprecated, therefore canonical
  265. /// format is selected for the FQDN option definition and this function
  266. /// returns true if the option definition comprises the domain-name
  267. /// field encoded in canonical format.
  268. ///
  269. /// @return true if option has the format of DHCPv4 Client FQDN
  270. /// %Option.
  271. bool haveFqdn4Format() const;
  272. /// @brief Check if the option has format of Vendor-Identifying Vendor
  273. /// Specific Options.
  274. ///
  275. /// @return Always true.
  276. /// @todo The Vendor-Identifying Vendor-Specific Option has a complex format
  277. /// which we do not support here. Therefore it is not really possible to
  278. /// check that the current definition is valid. We may need to add support
  279. /// for such option format or simply do not check the format for certain
  280. /// options, e.g. vendor options, IA_NA, IAADDR and always return objects
  281. /// of the certain type.
  282. bool haveVendor4Format() const;
  283. /// @brief Check if option has a format of the Vendor-Specific Information
  284. /// %Option.
  285. ///
  286. /// The Vendor-Specific Information %Option comprises 32-bit enterprise id
  287. /// and the suboptions.
  288. ///
  289. /// @return true if option definition conforms to the format of the
  290. /// Vendor-Specific Information %Option.
  291. bool haveVendor6Format() const;
  292. /// @brief Option factory.
  293. ///
  294. /// This function creates an instance of DHCP option using
  295. /// provided chunk of buffer. This function may be used to
  296. /// create option which is to be sent in the outgoing packet.
  297. ///
  298. /// @warning calling this function on invalid option definition
  299. /// yields undefined behavior. Use \ref validate to test that
  300. /// the option definition is valid.
  301. ///
  302. /// @param u option universe (V4 or V6).
  303. /// @param type option type.
  304. /// @param begin beginning of the option buffer.
  305. /// @param end end of the option buffer.
  306. /// @param callback An instance of the function which parses packet options.
  307. /// If this is set to non NULL value this function will be used instead of
  308. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  309. /// isc::dhcp::LibDHCP::unpackOptions4.
  310. ///
  311. /// @return instance of the DHCP option.
  312. /// @throw InvalidOptionValue if data for the option is invalid.
  313. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  314. OptionBufferConstIter begin,
  315. OptionBufferConstIter end,
  316. UnpackOptionsCallback callback = NULL) const;
  317. /// @brief Option factory.
  318. ///
  319. /// This function creates an instance of DHCP option using
  320. /// whole provided buffer. This function may be used to
  321. /// create option which is to be sent in the outgoing packet.
  322. ///
  323. /// @warning calling this function on invalid option definition
  324. /// yields undefined behavior. Use \ref validate to test that
  325. /// the option definition is valid.
  326. ///
  327. /// @param u option universe (V4 or V6).
  328. /// @param type option type.
  329. /// @param buf option buffer.
  330. /// @param callback An instance of the function which parses packet options.
  331. /// If this is set to non NULL value this function will be used instead of
  332. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  333. /// isc::dhcp::LibDHCP::unpackOptions4.
  334. ///
  335. /// @return instance of the DHCP option.
  336. /// @throw InvalidOptionValue if data for the option is invalid.
  337. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  338. const OptionBuffer& buf = OptionBuffer(),
  339. UnpackOptionsCallback callback = NULL) const;
  340. /// @brief Option factory.
  341. ///
  342. /// This function creates an instance of DHCP option using the vector
  343. /// of strings which carry data values for option data fields.
  344. /// The order of values in the vector corresponds to the order of data
  345. /// fields in the option. The supplied string values are cast to
  346. /// their actual data types which are determined based on the
  347. /// option definition. If cast fails due to type mismatch, an exception
  348. /// is thrown. This factory function can be used to create option
  349. /// instance when user specified option value in the <b>comma separated
  350. /// values</b> format in the configuration database. Provided string
  351. /// must be tokenized into the vector of string values and this vector
  352. /// can be supplied to this function.
  353. ///
  354. /// @warning calling this function on invalid option definition
  355. /// yields undefined behavior. Use \ref validate to test that
  356. /// the option definition is valid.
  357. ///
  358. /// @param u option universe (V4 or V6).
  359. /// @param type option type.
  360. /// @param values a vector of values to be used to set data for an option.
  361. ///
  362. /// @return instance of the DHCP option.
  363. /// @throw InvalidOptionValue if data for the option is invalid.
  364. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  365. const std::vector<std::string>& values) const;
  366. /// @brief Factory to create option with address list.
  367. ///
  368. /// @param type option type.
  369. /// @param begin iterator pointing to the beginning of the buffer
  370. /// with a list of IPv4 addresses.
  371. /// @param end iterator pointing to the end of the buffer with
  372. /// a list of IPv4 addresses.
  373. ///
  374. /// @throw isc::OutOfRange if length of the provided option buffer
  375. /// is not multiple of IPV4 address length.
  376. static OptionPtr factoryAddrList4(uint16_t type,
  377. OptionBufferConstIter begin,
  378. OptionBufferConstIter end);
  379. /// @brief Factory to create option with address list.
  380. ///
  381. /// @param type option type.
  382. /// @param begin iterator pointing to the beginning of the buffer
  383. /// with a list of IPv6 addresses.
  384. /// @param end iterator pointing to the end of the buffer with
  385. /// a list of IPv6 addresses.
  386. ///
  387. /// @throw isc::OutOfaRange if length of provided option buffer
  388. /// is not multiple of IPV6 address length.
  389. static OptionPtr factoryAddrList6(uint16_t type,
  390. OptionBufferConstIter begin,
  391. OptionBufferConstIter end);
  392. /// @brief Empty option factory.
  393. ///
  394. /// @param u universe (V6 or V4).
  395. /// @param type option type.
  396. static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
  397. /// @brief Factory to create generic option.
  398. ///
  399. /// @param u universe (V6 or V4).
  400. /// @param type option type.
  401. /// @param begin iterator pointing to the beginning of the buffer.
  402. /// @param end iterator pointing to the end of the buffer.
  403. static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
  404. OptionBufferConstIter begin,
  405. OptionBufferConstIter end);
  406. /// @brief Factory for IA-type of option.
  407. ///
  408. /// @param type option type.
  409. /// @param begin iterator pointing to the beginning of the buffer.
  410. /// @param end iterator pointing to the end of the buffer.
  411. ///
  412. /// @throw isc::OutOfRange if provided option buffer is too short or
  413. /// too long. Expected size is 12 bytes.
  414. static OptionPtr factoryIA6(uint16_t type,
  415. OptionBufferConstIter begin,
  416. OptionBufferConstIter end);
  417. /// @brief Factory for IAADDR-type of option.
  418. ///
  419. /// @param type option type.
  420. /// @param begin iterator pointing to the beginning of the buffer.
  421. /// @param end iterator pointing to the end of the buffer.
  422. ///
  423. /// @throw isc::OutOfRange if provided option buffer is too short or
  424. /// too long. Expected size is 24 bytes.
  425. static OptionPtr factoryIAAddr6(uint16_t type,
  426. OptionBufferConstIter begin,
  427. OptionBufferConstIter end);
  428. /// @brief Factory for IAPREFIX-type of option.
  429. ///
  430. /// @param type option type.
  431. /// @param begin iterator pointing to the beginning of the buffer.
  432. /// @param end iterator pointing to the end of the buffer.
  433. ///
  434. /// @throw isc::OutOfRange if provided option buffer is too short.
  435. /// Expected minimum size is 25 bytes.
  436. static OptionPtr factoryIAPrefix6(uint16_t type,
  437. OptionBufferConstIter begin,
  438. OptionBufferConstIter end);
  439. /// @brief Factory function to create option with integer value.
  440. ///
  441. /// @param u universe (V4 or V6).
  442. /// @param type option type.
  443. /// @param encapsulated_space An option space being encapsulated by the
  444. /// options created by this factory function. The options which belong to
  445. /// encapsulated option space are sub options of this option.
  446. /// @param begin iterator pointing to the beginning of the buffer.
  447. /// @param end iterator pointing to the end of the buffer.
  448. /// @param callback An instance of the function which parses packet options.
  449. /// If this is set to non NULL value this function will be used instead of
  450. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  451. /// isc::dhcp::LibDHCP::unpackOptions4.
  452. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  453. ///
  454. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  455. template<typename T>
  456. static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
  457. const std::string& encapsulated_space,
  458. OptionBufferConstIter begin,
  459. OptionBufferConstIter end,
  460. UnpackOptionsCallback callback) {
  461. OptionPtr option(new OptionInt<T>(u, type, 0));
  462. option->setEncapsulatedSpace(encapsulated_space);
  463. option->setCallback(callback);
  464. option->unpack(begin, end);
  465. return (option);
  466. }
  467. /// @brief Factory function to create option with array of integer values.
  468. ///
  469. /// @param u universe (V4 or V6).
  470. /// @param type option type.
  471. /// @param begin iterator pointing to the beginning of the buffer.
  472. /// @param end iterator pointing to the end of the buffer.
  473. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  474. ///
  475. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  476. template<typename T>
  477. static OptionPtr factoryIntegerArray(Option::Universe u,
  478. uint16_t type,
  479. OptionBufferConstIter begin,
  480. OptionBufferConstIter end) {
  481. OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
  482. return (option);
  483. }
  484. private:
  485. /// @brief Creates an instance of an option having special format.
  486. ///
  487. /// The option with special formats are encapsulated by the dedicated
  488. /// classes derived from @c Option class. In particular these are:
  489. /// - IA_NA
  490. /// - IAADDR
  491. /// - FQDN
  492. /// - VIVSO.
  493. ///
  494. /// @param u A universe (V4 or V6).
  495. /// @param begin beginning of the option buffer.
  496. /// @param end end of the option buffer.
  497. /// @param callback An instance of the function which parses packet options.
  498. /// If this is set to non NULL value this function will be used instead of
  499. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  500. /// isc::dhcp::LibDHCP::unpackOptions4.
  501. ///
  502. /// @return An instance of the option having special format or NULL if
  503. /// such an option can't be created because an option with the given
  504. /// option code hasn't got the special format.
  505. OptionPtr factorySpecialFormatOption(Option::Universe u,
  506. OptionBufferConstIter begin,
  507. OptionBufferConstIter end,
  508. UnpackOptionsCallback callback) const;
  509. /// @brief Check if specified option format is a record with 3 fields
  510. /// where first one is custom, and two others are uint32.
  511. ///
  512. /// This is a helper function for functions that detect IA_NA and IAAddr
  513. /// option formats.
  514. ///
  515. /// @param first_type type of the first data field.
  516. ///
  517. /// @return true if actual option format matches expected format.
  518. bool haveIAx6Format(const OptionDataType first_type) const;
  519. /// @brief Check if specified type matches option definition type.
  520. ///
  521. /// @return true if specified type matches option definition type.
  522. inline bool haveType(const OptionDataType type) const {
  523. return (type == type_);
  524. }
  525. /// @brief Perform lexical cast of the value and validate its range.
  526. ///
  527. /// This function performs lexical cast of a string value to integer
  528. /// or boolean value and checks if the resulting value is within a
  529. /// range of a target type. Note that range checks are not performed
  530. /// on boolean values. The target type should be one of the supported
  531. /// integer types or bool.
  532. ///
  533. /// @param value_str input value given as string.
  534. /// @tparam T target type for lexical cast.
  535. ///
  536. /// @return cast value.
  537. /// @throw BadDataTypeCast if cast was not successful.
  538. template<typename T>
  539. T lexicalCastWithRangeCheck(const std::string& value_str) const;
  540. /// @brief Write the string value into the provided buffer.
  541. ///
  542. /// This method writes the given value to the specified buffer.
  543. /// The provided string value may represent data of different types.
  544. /// The actual data type is specified with the second argument.
  545. /// Based on a value of this argument, this function will first
  546. /// try to cast the string value to the particular data type and
  547. /// if it is successful it will store the data in the buffer
  548. /// in a binary format.
  549. ///
  550. /// @param value string representation of the value to be written.
  551. /// @param type the actual data type to be stored.
  552. /// @param [in, out] buf buffer where the value is to be stored.
  553. ///
  554. /// @throw BadDataTypeCast if data write was unsuccessful.
  555. void writeToBuffer(const std::string& value, const OptionDataType type,
  556. OptionBuffer& buf) const;
  557. /// Option name.
  558. std::string name_;
  559. /// Option code.
  560. uint16_t code_;
  561. /// Option data type.
  562. OptionDataType type_;
  563. /// Indicates wheter option is a single value or array.
  564. bool array_type_;
  565. /// Name of the space being encapsulated by this option.
  566. std::string encapsulated_space_;
  567. /// Collection of data fields within the record.
  568. RecordFieldsCollection record_fields_;
  569. };
  570. /// @brief Multi index container for DHCP option definitions.
  571. ///
  572. /// This container allows to search for DHCP option definition
  573. /// using two indexes:
  574. /// - sequenced: used to access elements in the order they have
  575. /// been added to the container
  576. /// - option code: used to search definitions of options
  577. /// with a specified option code (aka option type).
  578. /// Note that this container can hold multiple options with the
  579. /// same code. For this reason, the latter index can be used to
  580. /// obtain a range of options for a particular option code.
  581. ///
  582. /// @todo: need an index to search options using option space name
  583. /// once option spaces are implemented.
  584. typedef boost::multi_index_container<
  585. // Container comprises elements of OptionDefinition type.
  586. OptionDefinitionPtr,
  587. // Here we start enumerating various indexes.
  588. boost::multi_index::indexed_by<
  589. // Sequenced index allows accessing elements in the same way
  590. // as elements in std::list. Sequenced is an index #0.
  591. boost::multi_index::sequenced<>,
  592. // Start definition of index #1.
  593. boost::multi_index::hashed_non_unique<
  594. // Use option type as the index key. The type is held
  595. // in OptionDefinition object so we have to call
  596. // OptionDefinition::getCode to retrieve this key
  597. // for each element. The option code is non-unique so
  598. // multiple elements with the same option code can
  599. // be returned by this index.
  600. boost::multi_index::const_mem_fun<
  601. OptionDefinition,
  602. uint16_t,
  603. &OptionDefinition::getCode
  604. >
  605. >
  606. >
  607. > OptionDefContainer;
  608. /// Pointer to an option definition container.
  609. typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
  610. /// Container that holds various vendor option containers
  611. typedef std::map<uint32_t, OptionDefContainer> VendorOptionDefContainers;
  612. /// Type of the index #1 - option type.
  613. typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
  614. /// Pair of iterators to represent the range of options definitions
  615. /// having the same option type value. The first element in this pair
  616. /// represents the beginning of the range, the second element
  617. /// represents the end.
  618. typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
  619. OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
  620. } // namespace isc::dhcp
  621. } // namespace isc
  622. #endif // OPTION_DEFINITION_H