option_definition.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 OPTION_CLIENT_FQDN option format.
  250. ///
  251. /// @return true of specified format is OPTION_CLIENT_FQDN option format,
  252. /// false otherwise.
  253. bool haveClientFqdnFormat() const;
  254. /// @brief Check if option has format of the DHCPv4 Client FQDN
  255. /// %Option.
  256. ///
  257. /// The encoding of the domain-name carried by the FQDN option is
  258. /// conditional and is specified in the flags field of the option.
  259. /// The domain-name can be encoded in the ASCII format or canonical
  260. /// wire format. The ASCII format is deprecated, therefore canonical
  261. /// format is selected for the FQDN option definition and this function
  262. /// returns true if the option definition comprises the domain-name
  263. /// field encoded in canonical format.
  264. ///
  265. /// @return true if option has the format of DHCPv4 Client FQDN
  266. /// %Option.
  267. bool haveFqdn4Format() const;
  268. /// @brief Check if the option has format of Vendor-Identifying Vendor
  269. /// Specific Options.
  270. ///
  271. /// @return Always true.
  272. /// @todo The Vendor-Identifying Vendor-Specific Option has a complex format
  273. /// which we do not support here. Therefore it is not really possible to
  274. /// check that the current definition is valid. We may need to add support
  275. /// for such option format or simply do not check the format for certain
  276. /// options, e.g. vendor options, IA_NA, IAADDR and always return objects
  277. /// of the certain type.
  278. bool haveVendor4Format() const;
  279. /// @brief Check if option has a format of the Vendor-Specific Information
  280. /// %Option.
  281. ///
  282. /// The Vendor-Specific Information %Option comprises 32-bit enterprise id
  283. /// and the suboptions.
  284. ///
  285. /// @return true if option definition conforms to the format of the
  286. /// Vendor-Specific Information %Option.
  287. bool haveVendor6Format() const;
  288. /// @brief Option factory.
  289. ///
  290. /// This function creates an instance of DHCP option using
  291. /// provided chunk of buffer. This function may be used to
  292. /// create option which is to be sent in the outgoing packet.
  293. ///
  294. /// @warning calling this function on invalid option definition
  295. /// yields undefined behavior. Use \ref validate to test that
  296. /// the option definition is valid.
  297. ///
  298. /// @param u option universe (V4 or V6).
  299. /// @param type option type.
  300. /// @param begin beginning of the option buffer.
  301. /// @param end end of the option buffer.
  302. /// @param callback An instance of the function which parses packet options.
  303. /// If this is set to non NULL value this function will be used instead of
  304. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  305. /// isc::dhcp::LibDHCP::unpackOptions4.
  306. ///
  307. /// @return instance of the DHCP option.
  308. /// @throw InvalidOptionValue if data for the option is invalid.
  309. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  310. OptionBufferConstIter begin,
  311. OptionBufferConstIter end,
  312. UnpackOptionsCallback callback = NULL) const;
  313. /// @brief Option factory.
  314. ///
  315. /// This function creates an instance of DHCP option using
  316. /// whole provided buffer. This function may be used to
  317. /// create option which is to be sent in the outgoing packet.
  318. ///
  319. /// @warning calling this function on invalid option definition
  320. /// yields undefined behavior. Use \ref validate to test that
  321. /// the option definition is valid.
  322. ///
  323. /// @param u option universe (V4 or V6).
  324. /// @param type option type.
  325. /// @param buf option buffer.
  326. /// @param callback An instance of the function which parses packet options.
  327. /// If this is set to non NULL value this function will be used instead of
  328. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  329. /// isc::dhcp::LibDHCP::unpackOptions4.
  330. ///
  331. /// @return instance of the DHCP option.
  332. /// @throw InvalidOptionValue if data for the option is invalid.
  333. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  334. const OptionBuffer& buf = OptionBuffer(),
  335. UnpackOptionsCallback callback = NULL) const;
  336. /// @brief Option factory.
  337. ///
  338. /// This function creates an instance of DHCP option using the vector
  339. /// of strings which carry data values for option data fields.
  340. /// The order of values in the vector corresponds to the order of data
  341. /// fields in the option. The supplied string values are cast to
  342. /// their actual data types which are determined based on the
  343. /// option definition. If cast fails due to type mismatch, an exception
  344. /// is thrown. This factory function can be used to create option
  345. /// instance when user specified option value in the <b>comma separated
  346. /// values</b> format in the configuration database. Provided string
  347. /// must be tokenized into the vector of string values and this vector
  348. /// can be supplied to this function.
  349. ///
  350. /// @warning calling this function on invalid option definition
  351. /// yields undefined behavior. Use \ref validate to test that
  352. /// the option definition is valid.
  353. ///
  354. /// @param u option universe (V4 or V6).
  355. /// @param type option type.
  356. /// @param values a vector of values to be used to set data for an option.
  357. ///
  358. /// @return instance of the DHCP option.
  359. /// @throw InvalidOptionValue if data for the option is invalid.
  360. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  361. const std::vector<std::string>& values) const;
  362. /// @brief Factory to create option with address list.
  363. ///
  364. /// @param type option type.
  365. /// @param begin iterator pointing to the beginning of the buffer
  366. /// with a list of IPv4 addresses.
  367. /// @param end iterator pointing to the end of the buffer with
  368. /// a list of IPv4 addresses.
  369. ///
  370. /// @throw isc::OutOfRange if length of the provided option buffer
  371. /// is not multiple of IPV4 address length.
  372. static OptionPtr factoryAddrList4(uint16_t type,
  373. OptionBufferConstIter begin,
  374. OptionBufferConstIter end);
  375. /// @brief Factory to create option with address list.
  376. ///
  377. /// @param type option type.
  378. /// @param begin iterator pointing to the beginning of the buffer
  379. /// with a list of IPv6 addresses.
  380. /// @param end iterator pointing to the end of the buffer with
  381. /// a list of IPv6 addresses.
  382. ///
  383. /// @throw isc::OutOfaRange if length of provided option buffer
  384. /// is not multiple of IPV6 address length.
  385. static OptionPtr factoryAddrList6(uint16_t type,
  386. OptionBufferConstIter begin,
  387. OptionBufferConstIter end);
  388. /// @brief Empty option factory.
  389. ///
  390. /// @param u universe (V6 or V4).
  391. /// @param type option type.
  392. static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
  393. /// @brief Factory to create generic option.
  394. ///
  395. /// @param u universe (V6 or V4).
  396. /// @param type option type.
  397. /// @param begin iterator pointing to the beginning of the buffer.
  398. /// @param end iterator pointing to the end of the buffer.
  399. static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
  400. OptionBufferConstIter begin,
  401. OptionBufferConstIter end);
  402. /// @brief Factory for IA-type of option.
  403. ///
  404. /// @param type option type.
  405. /// @param begin iterator pointing to the beginning of the buffer.
  406. /// @param end iterator pointing to the end of the buffer.
  407. ///
  408. /// @throw isc::OutOfRange if provided option buffer is too short or
  409. /// too long. Expected size is 12 bytes.
  410. /// @throw isc::BadValue if specified universe value is not V6.
  411. static OptionPtr factoryIA6(uint16_t type,
  412. OptionBufferConstIter begin,
  413. OptionBufferConstIter end);
  414. /// @brief Factory for IAADDR-type of option.
  415. ///
  416. /// @param type option type.
  417. /// @param begin iterator pointing to the beginning of the buffer.
  418. /// @param end iterator pointing to the end of the buffer.
  419. ///
  420. /// @throw isc::OutOfRange if provided option buffer is too short or
  421. /// too long. Expected size is 24 bytes.
  422. /// @throw isc::BadValue if specified universe value is not V6.
  423. static OptionPtr factoryIAAddr6(uint16_t type,
  424. OptionBufferConstIter begin,
  425. OptionBufferConstIter end);
  426. /// @brief Factory function to create option with integer value.
  427. ///
  428. /// @param u universe (V4 or V6).
  429. /// @param type option type.
  430. /// @param encapsulated_space An option space being encapsulated by the
  431. /// options created by this factory function. The options which belong to
  432. /// encapsulated option space are sub options of this option.
  433. /// @param begin iterator pointing to the beginning of the buffer.
  434. /// @param end iterator pointing to the end of the buffer.
  435. /// @param callback An instance of the function which parses packet options.
  436. /// If this is set to non NULL value this function will be used instead of
  437. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  438. /// isc::dhcp::LibDHCP::unpackOptions4.
  439. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  440. ///
  441. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  442. template<typename T>
  443. static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
  444. const std::string& encapsulated_space,
  445. OptionBufferConstIter begin,
  446. OptionBufferConstIter end,
  447. UnpackOptionsCallback callback) {
  448. OptionPtr option(new OptionInt<T>(u, type, 0));
  449. option->setEncapsulatedSpace(encapsulated_space);
  450. option->setCallback(callback);
  451. option->unpack(begin, end);
  452. return (option);
  453. }
  454. /// @brief Factory function to create option with array of integer values.
  455. ///
  456. /// @param u universe (V4 or V6).
  457. /// @param type option type.
  458. /// @param begin iterator pointing to the beginning of the buffer.
  459. /// @param end iterator pointing to the end of the buffer.
  460. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  461. ///
  462. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  463. template<typename T>
  464. static OptionPtr factoryIntegerArray(Option::Universe u,
  465. uint16_t type,
  466. OptionBufferConstIter begin,
  467. OptionBufferConstIter end) {
  468. OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
  469. return (option);
  470. }
  471. private:
  472. /// @brief Creates an instance of an option having special format.
  473. ///
  474. /// The option with special formats are encapsulated by the dedicated
  475. /// classes derived from @c Option class. In particular these are:
  476. /// - IA_NA
  477. /// - IAADDR
  478. /// - FQDN
  479. /// - VIVSO.
  480. ///
  481. /// @param u A universe (V4 or V6).
  482. /// @param begin beginning of the option buffer.
  483. /// @param end end of the option buffer.
  484. /// @param callback An instance of the function which parses packet options.
  485. /// If this is set to non NULL value this function will be used instead of
  486. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  487. /// isc::dhcp::LibDHCP::unpackOptions4.
  488. ///
  489. /// @return An instance of the option having special format or NULL if
  490. /// such an option can't be created because an option with the given
  491. /// option code hasn't got the special format.
  492. OptionPtr factorySpecialFormatOption(Option::Universe u,
  493. OptionBufferConstIter begin,
  494. OptionBufferConstIter end,
  495. UnpackOptionsCallback callback) const;
  496. /// @brief Check if specified option format is a record with 3 fields
  497. /// where first one is custom, and two others are uint32.
  498. ///
  499. /// This is a helper function for functions that detect IA_NA and IAAddr
  500. /// option formats.
  501. ///
  502. /// @param first_type type of the first data field.
  503. ///
  504. /// @return true if actual option format matches expected format.
  505. bool haveIAx6Format(const OptionDataType first_type) const;
  506. /// @brief Check if specified type matches option definition type.
  507. ///
  508. /// @return true if specified type matches option definition type.
  509. inline bool haveType(const OptionDataType type) const {
  510. return (type == type_);
  511. }
  512. /// @brief Perform lexical cast of the value and validate its range.
  513. ///
  514. /// This function performs lexical cast of a string value to integer
  515. /// or boolean value and checks if the resulting value is within a
  516. /// range of a target type. Note that range checks are not performed
  517. /// on boolean values. The target type should be one of the supported
  518. /// integer types or bool.
  519. ///
  520. /// @param value_str input value given as string.
  521. /// @tparam T target type for lexical cast.
  522. ///
  523. /// @return cast value.
  524. /// @throw BadDataTypeCast if cast was not successful.
  525. template<typename T>
  526. T lexicalCastWithRangeCheck(const std::string& value_str) const;
  527. /// @brief Write the string value into the provided buffer.
  528. ///
  529. /// This method writes the given value to the specified buffer.
  530. /// The provided string value may represent data of different types.
  531. /// The actual data type is specified with the second argument.
  532. /// Based on a value of this argument, this function will first
  533. /// try to cast the string value to the particular data type and
  534. /// if it is successful it will store the data in the buffer
  535. /// in a binary format.
  536. ///
  537. /// @param value string representation of the value to be written.
  538. /// @param type the actual data type to be stored.
  539. /// @param [in, out] buf buffer where the value is to be stored.
  540. ///
  541. /// @throw BadDataTypeCast if data write was unsuccessful.
  542. void writeToBuffer(const std::string& value, const OptionDataType type,
  543. OptionBuffer& buf) const;
  544. /// Option name.
  545. std::string name_;
  546. /// Option code.
  547. uint16_t code_;
  548. /// Option data type.
  549. OptionDataType type_;
  550. /// Indicates wheter option is a single value or array.
  551. bool array_type_;
  552. /// Name of the space being encapsulated by this option.
  553. std::string encapsulated_space_;
  554. /// Collection of data fields within the record.
  555. RecordFieldsCollection record_fields_;
  556. };
  557. /// @brief Multi index container for DHCP option definitions.
  558. ///
  559. /// This container allows to search for DHCP option definition
  560. /// using two indexes:
  561. /// - sequenced: used to access elements in the order they have
  562. /// been added to the container
  563. /// - option code: used to search definitions of options
  564. /// with a specified option code (aka option type).
  565. /// Note that this container can hold multiple options with the
  566. /// same code. For this reason, the latter index can be used to
  567. /// obtain a range of options for a particular option code.
  568. ///
  569. /// @todo: need an index to search options using option space name
  570. /// once option spaces are implemented.
  571. typedef boost::multi_index_container<
  572. // Container comprises elements of OptionDefinition type.
  573. OptionDefinitionPtr,
  574. // Here we start enumerating various indexes.
  575. boost::multi_index::indexed_by<
  576. // Sequenced index allows accessing elements in the same way
  577. // as elements in std::list. Sequenced is an index #0.
  578. boost::multi_index::sequenced<>,
  579. // Start definition of index #1.
  580. boost::multi_index::hashed_non_unique<
  581. // Use option type as the index key. The type is held
  582. // in OptionDefinition object so we have to call
  583. // OptionDefinition::getCode to retrieve this key
  584. // for each element. The option code is non-unique so
  585. // multiple elements with the same option code can
  586. // be returned by this index.
  587. boost::multi_index::const_mem_fun<
  588. OptionDefinition,
  589. uint16_t,
  590. &OptionDefinition::getCode
  591. >
  592. >
  593. >
  594. > OptionDefContainer;
  595. /// Pointer to an option definition container.
  596. typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
  597. /// Container that holds various vendor option containers
  598. typedef std::map<uint32_t, OptionDefContainer> VendorOptionDefContainers;
  599. /// Type of the index #1 - option type.
  600. typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
  601. /// Pair of iterators to represent the range of options definitions
  602. /// having the same option type value. The first element in this pair
  603. /// represents the beginning of the range, the second element
  604. /// represents the end.
  605. typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
  606. OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
  607. } // namespace isc::dhcp
  608. } // namespace isc
  609. #endif // OPTION_DEFINITION_H