option_definition.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. // Copyright (C) 2012-2014 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 Copy constructor.
  170. ///
  171. /// @param def Option definition to be used to create a new instance.
  172. explicit OptionDefinition(const OptionDefinition& def);
  173. /// @name Comparison functions and operators.
  174. ///
  175. //@{
  176. /// @brief Check if option definition is equal to other.
  177. ///
  178. /// @param other Option definition to compare to.
  179. ///
  180. /// @return true if two option definitions are equal, false otherwise.
  181. bool equals(const OptionDefinition& other) const;
  182. /// @brief Equality operator.
  183. ///
  184. /// @param other Option definition to compare to.
  185. ///
  186. /// @return true if two option definitions are equal, false otherwise.
  187. bool operator==(const OptionDefinition& other) const {
  188. return (equals(other));
  189. }
  190. /// @brief Inequality operator.
  191. ///
  192. /// @param other Option definition to compare to.
  193. ///
  194. /// @return true if option definitions are not equal, false otherwise.
  195. bool operator!=(const OptionDefinition& other) const {
  196. return (!equals(other));
  197. }
  198. //@}
  199. /// @brief Constructor.
  200. ///
  201. /// This constructor sets the name of the option space that is
  202. /// encapsulated by this option. The encapsulated option space
  203. /// identifies sub-options that are carried within this option.
  204. /// This constructor does not allow to set array indicator
  205. /// because options comprising an array of data fields must
  206. /// not be used with sub-options.
  207. ///
  208. /// @param name option name.
  209. /// @param code option code.
  210. /// @param type option data type.
  211. /// @param encapsulated_space name of the option space being
  212. /// encapsulated by this option.
  213. explicit OptionDefinition(const std::string& name,
  214. const uint16_t code,
  215. const OptionDataType type,
  216. const char* encapsulated_space);
  217. /// @brief Adds data field to the record.
  218. ///
  219. /// @param data_type_name name of the data type for the field.
  220. ///
  221. /// @throw isc::InvalidOperation if option type is not set to RECORD_TYPE.
  222. /// @throw isc::BadValue if specified invalid data type.
  223. void addRecordField(const std::string& data_type_name);
  224. /// @brief Adds data field to the record.
  225. ///
  226. /// @param data_type data type for the field.
  227. ///
  228. /// @throw isc::InvalidOperation if option type is not set to RECORD_TYPE.
  229. /// @throw isc::BadValue if specified invalid data type.
  230. void addRecordField(const OptionDataType data_type);
  231. /// @brief Return array type indicator.
  232. ///
  233. /// The method returns the bool value to indicate whether the option is a
  234. /// a single value or an array of values.
  235. ///
  236. /// @return true if option comprises an array of values.
  237. bool getArrayType() const { return (array_type_); }
  238. /// @brief Return option code.
  239. ///
  240. /// @return option code.
  241. uint16_t getCode() const { return (code_); }
  242. /// @brief Return name of the encapsulated option space.
  243. ///
  244. /// @return name of the encapsulated option space.
  245. std::string getEncapsulatedSpace() const {
  246. return (encapsulated_space_);
  247. }
  248. /// @brief Return option name.
  249. ///
  250. /// @return option name.
  251. std::string getName() const { return (name_); }
  252. /// @brief Return list of record fields.
  253. ///
  254. /// @return list of record fields.
  255. const RecordFieldsCollection& getRecordFields() const {
  256. return (record_fields_);
  257. }
  258. /// @brief Return option data type.
  259. ///
  260. /// @return option data type.
  261. OptionDataType getType() const { return (type_); };
  262. /// @brief Check if the option definition is valid.
  263. ///
  264. /// Note that it is a responsibility of the code that created
  265. /// the OptionDefinition object to validate that it is valid.
  266. /// This function will not be called internally anywhere in this
  267. /// class to verify that the option definition is valid. Using
  268. /// invalid option definition to create an instance of the
  269. /// DHCP option leads to undefined behavior.
  270. ///
  271. /// @throw MalformedOptionDefinition option definition is invalid.
  272. void validate() const;
  273. /// @brief Check if specified format is IA_NA option format.
  274. ///
  275. /// @return true if specified format is IA_NA option format.
  276. bool haveIA6Format() const;
  277. /// @brief Check if specified format is IAADDR option format.
  278. ///
  279. /// @return true if specified format is IAADDR option format.
  280. bool haveIAAddr6Format() const;
  281. /// @brief Check if specified format is IAPREFIX option format.
  282. ///
  283. /// @return true if specified format is IAPREFIX option format.
  284. bool haveIAPrefix6Format() const;
  285. /// @brief Check if specified format is OPTION_CLIENT_FQDN option format.
  286. ///
  287. /// @return true of specified format is OPTION_CLIENT_FQDN option format,
  288. /// false otherwise.
  289. bool haveClientFqdnFormat() const;
  290. /// @brief Check if option has format of the DHCPv4 Client FQDN
  291. /// %Option.
  292. ///
  293. /// The encoding of the domain-name carried by the FQDN option is
  294. /// conditional and is specified in the flags field of the option.
  295. /// The domain-name can be encoded in the ASCII format or canonical
  296. /// wire format. The ASCII format is deprecated, therefore canonical
  297. /// format is selected for the FQDN option definition and this function
  298. /// returns true if the option definition comprises the domain-name
  299. /// field encoded in canonical format.
  300. ///
  301. /// @return true if option has the format of DHCPv4 Client FQDN
  302. /// %Option.
  303. bool haveFqdn4Format() const;
  304. /// @brief Check if the option has format of Vendor-Identifying Vendor
  305. /// Specific Options.
  306. ///
  307. /// @return Always true.
  308. /// @todo The Vendor-Identifying Vendor-Specific Option has a complex format
  309. /// which we do not support here. Therefore it is not really possible to
  310. /// check that the current definition is valid. We may need to add support
  311. /// for such option format or simply do not check the format for certain
  312. /// options, e.g. vendor options, IA_NA, IAADDR and always return objects
  313. /// of the certain type.
  314. bool haveVendor4Format() const;
  315. /// @brief Check if option has a format of the Vendor-Specific Information
  316. /// %Option.
  317. ///
  318. /// The Vendor-Specific Information %Option comprises 32-bit enterprise id
  319. /// and the suboptions.
  320. ///
  321. /// @return true if option definition conforms to the format of the
  322. /// Vendor-Specific Information %Option.
  323. bool haveVendor6Format() const;
  324. /// @brief Check if the option has format of DHCPv4 V-I Vendor Class option.
  325. ///
  326. /// @return true if the option has the format of DHCPv4 Vendor Class option.
  327. bool haveVendorClass4Format() const;
  328. /// @brief Check if the option has format of DHCPv6 Vendor Class option.
  329. ///
  330. /// @return true if option has the format of DHCPv6 Vendor Class option.
  331. bool haveVendorClass6Format() const;
  332. /// @brief Option factory.
  333. ///
  334. /// This function creates an instance of DHCP option using
  335. /// provided chunk of buffer. This function may be used to
  336. /// create option which is to be sent in the outgoing packet.
  337. ///
  338. /// @warning calling this function on invalid option definition
  339. /// yields undefined behavior. Use \ref validate to test that
  340. /// the option definition is valid.
  341. ///
  342. /// @param u option universe (V4 or V6).
  343. /// @param type option type.
  344. /// @param begin beginning of the option buffer.
  345. /// @param end end of the option buffer.
  346. /// @param callback An instance of the function which parses packet options.
  347. /// If this is set to non NULL value this function will be used instead of
  348. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  349. /// isc::dhcp::LibDHCP::unpackOptions4.
  350. ///
  351. /// @return instance of the DHCP option.
  352. /// @throw InvalidOptionValue if data for the option is invalid.
  353. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  354. OptionBufferConstIter begin,
  355. OptionBufferConstIter end,
  356. UnpackOptionsCallback callback = NULL) const;
  357. /// @brief Option factory.
  358. ///
  359. /// This function creates an instance of DHCP option using
  360. /// whole provided buffer. This function may be used to
  361. /// create option which is to be sent in the outgoing packet.
  362. ///
  363. /// @warning calling this function on invalid option definition
  364. /// yields undefined behavior. Use \ref validate to test that
  365. /// the option definition is valid.
  366. ///
  367. /// @param u option universe (V4 or V6).
  368. /// @param type option type.
  369. /// @param buf option buffer.
  370. /// @param callback An instance of the function which parses packet options.
  371. /// If this is set to non NULL value this function will be used instead of
  372. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  373. /// isc::dhcp::LibDHCP::unpackOptions4.
  374. ///
  375. /// @return instance of the DHCP option.
  376. /// @throw InvalidOptionValue if data for the option is invalid.
  377. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  378. const OptionBuffer& buf = OptionBuffer(),
  379. UnpackOptionsCallback callback = NULL) const;
  380. /// @brief Option factory.
  381. ///
  382. /// This function creates an instance of DHCP option using the vector
  383. /// of strings which carry data values for option data fields.
  384. /// The order of values in the vector corresponds to the order of data
  385. /// fields in the option. The supplied string values are cast to
  386. /// their actual data types which are determined based on the
  387. /// option definition. If cast fails due to type mismatch, an exception
  388. /// is thrown. This factory function can be used to create option
  389. /// instance when user specified option value in the <b>comma separated
  390. /// values</b> format in the configuration database. Provided string
  391. /// must be tokenized into the vector of string values and this vector
  392. /// can be supplied to this function.
  393. ///
  394. /// @warning calling this function on invalid option definition
  395. /// yields undefined behavior. Use \ref validate to test that
  396. /// the option definition is valid.
  397. ///
  398. /// @param u option universe (V4 or V6).
  399. /// @param type option type.
  400. /// @param values a vector of values to be used to set data for an option.
  401. ///
  402. /// @return instance of the DHCP option.
  403. /// @throw InvalidOptionValue if data for the option is invalid.
  404. OptionPtr optionFactory(Option::Universe u, uint16_t type,
  405. const std::vector<std::string>& values) const;
  406. /// @brief Factory to create option with address list.
  407. ///
  408. /// @param type option type.
  409. /// @param begin iterator pointing to the beginning of the buffer
  410. /// with a list of IPv4 addresses.
  411. /// @param end iterator pointing to the end of the buffer with
  412. /// a list of IPv4 addresses.
  413. ///
  414. /// @throw isc::OutOfRange if length of the provided option buffer
  415. /// is not multiple of IPV4 address length.
  416. static OptionPtr factoryAddrList4(uint16_t type,
  417. OptionBufferConstIter begin,
  418. OptionBufferConstIter end);
  419. /// @brief Factory to create option with address list.
  420. ///
  421. /// @param type option type.
  422. /// @param begin iterator pointing to the beginning of the buffer
  423. /// with a list of IPv6 addresses.
  424. /// @param end iterator pointing to the end of the buffer with
  425. /// a list of IPv6 addresses.
  426. ///
  427. /// @throw isc::OutOfaRange if length of provided option buffer
  428. /// is not multiple of IPV6 address length.
  429. static OptionPtr factoryAddrList6(uint16_t type,
  430. OptionBufferConstIter begin,
  431. OptionBufferConstIter end);
  432. /// @brief Empty option factory.
  433. ///
  434. /// @param u universe (V6 or V4).
  435. /// @param type option type.
  436. static OptionPtr factoryEmpty(Option::Universe u, uint16_t type);
  437. /// @brief Factory to create generic option.
  438. ///
  439. /// @param u universe (V6 or V4).
  440. /// @param type option type.
  441. /// @param begin iterator pointing to the beginning of the buffer.
  442. /// @param end iterator pointing to the end of the buffer.
  443. static OptionPtr factoryGeneric(Option::Universe u, uint16_t type,
  444. OptionBufferConstIter begin,
  445. OptionBufferConstIter end);
  446. /// @brief Factory for IA-type of option.
  447. ///
  448. /// @param type option type.
  449. /// @param begin iterator pointing to the beginning of the buffer.
  450. /// @param end iterator pointing to the end of the buffer.
  451. ///
  452. /// @throw isc::OutOfRange if provided option buffer is too short or
  453. /// too long. Expected size is 12 bytes.
  454. static OptionPtr factoryIA6(uint16_t type,
  455. OptionBufferConstIter begin,
  456. OptionBufferConstIter end);
  457. /// @brief Factory for IAADDR-type of option.
  458. ///
  459. /// @param type option type.
  460. /// @param begin iterator pointing to the beginning of the buffer.
  461. /// @param end iterator pointing to the end of the buffer.
  462. ///
  463. /// @throw isc::OutOfRange if provided option buffer is too short or
  464. /// too long. Expected size is 24 bytes.
  465. static OptionPtr factoryIAAddr6(uint16_t type,
  466. OptionBufferConstIter begin,
  467. OptionBufferConstIter end);
  468. /// @brief Factory for IAPREFIX-type of option.
  469. ///
  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. ///
  474. /// @throw isc::OutOfRange if provided option buffer is too short.
  475. /// Expected minimum size is 25 bytes.
  476. static OptionPtr factoryIAPrefix6(uint16_t type,
  477. OptionBufferConstIter begin,
  478. OptionBufferConstIter end);
  479. /// @brief Factory function to create option with integer value.
  480. ///
  481. /// @param u universe (V4 or V6).
  482. /// @param type option type.
  483. /// @param encapsulated_space An option space being encapsulated by the
  484. /// options created by this factory function. The options which belong to
  485. /// encapsulated option space are sub options of this option.
  486. /// @param begin iterator pointing to the beginning of the buffer.
  487. /// @param end iterator pointing to the end of the buffer.
  488. /// @param callback An instance of the function which parses packet options.
  489. /// If this is set to non NULL value this function will be used instead of
  490. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  491. /// isc::dhcp::LibDHCP::unpackOptions4.
  492. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  493. ///
  494. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  495. template<typename T>
  496. static OptionPtr factoryInteger(Option::Universe u, uint16_t type,
  497. const std::string& encapsulated_space,
  498. OptionBufferConstIter begin,
  499. OptionBufferConstIter end,
  500. UnpackOptionsCallback callback) {
  501. OptionPtr option(new OptionInt<T>(u, type, 0));
  502. option->setEncapsulatedSpace(encapsulated_space);
  503. option->setCallback(callback);
  504. option->unpack(begin, end);
  505. return (option);
  506. }
  507. /// @brief Factory function to create option with array of integer values.
  508. ///
  509. /// @param u universe (V4 or V6).
  510. /// @param type option type.
  511. /// @param begin iterator pointing to the beginning of the buffer.
  512. /// @param end iterator pointing to the end of the buffer.
  513. /// @tparam T type of the data field (must be one of the uintX_t or intX_t).
  514. ///
  515. /// @throw isc::OutOfRange if provided option buffer length is invalid.
  516. template<typename T>
  517. static OptionPtr factoryIntegerArray(Option::Universe u,
  518. uint16_t type,
  519. OptionBufferConstIter begin,
  520. OptionBufferConstIter end) {
  521. OptionPtr option(new OptionIntArray<T>(u, type, begin, end));
  522. return (option);
  523. }
  524. private:
  525. /// @brief Creates an instance of an option having special format.
  526. ///
  527. /// The option with special formats are encapsulated by the dedicated
  528. /// classes derived from @c Option class. In particular these are:
  529. /// - IA_NA
  530. /// - IAADDR
  531. /// - FQDN
  532. /// - VIVSO.
  533. ///
  534. /// @param u A universe (V4 or V6).
  535. /// @param begin beginning of the option buffer.
  536. /// @param end end of the option buffer.
  537. /// @param callback An instance of the function which parses packet options.
  538. /// If this is set to non NULL value this function will be used instead of
  539. /// @c isc::dhcp::LibDHCP::unpackOptions6 and
  540. /// isc::dhcp::LibDHCP::unpackOptions4.
  541. ///
  542. /// @return An instance of the option having special format or NULL if
  543. /// such an option can't be created because an option with the given
  544. /// option code hasn't got the special format.
  545. OptionPtr factorySpecialFormatOption(Option::Universe u,
  546. OptionBufferConstIter begin,
  547. OptionBufferConstIter end,
  548. UnpackOptionsCallback callback) const;
  549. /// @brief Check if specified option format is a record with 3 fields
  550. /// where first one is custom, and two others are uint32.
  551. ///
  552. /// This is a helper function for functions that detect IA_NA and IAAddr
  553. /// option formats.
  554. ///
  555. /// @param first_type type of the first data field.
  556. ///
  557. /// @return true if actual option format matches expected format.
  558. bool haveIAx6Format(const OptionDataType first_type) const;
  559. /// @brief Check if specified type matches option definition type.
  560. ///
  561. /// @return true if specified type matches option definition type.
  562. inline bool haveType(const OptionDataType type) const {
  563. return (type == type_);
  564. }
  565. /// @brief Converts a string value to a boolean value.
  566. ///
  567. /// This function converts the value represented as string to a boolean
  568. /// value. The following conversions are acceptable:
  569. /// - "true" => true
  570. /// - "false" => false
  571. /// - "1" => true
  572. /// - "0" => false
  573. /// The first two conversions are case insensitive, so as conversions from
  574. /// strings such as "TRUE", "trUE" etc. will be accepted. Note that the
  575. /// only acceptable integer values, carried as strings are: "0" and "1".
  576. /// For other values, e.g. "2", "3" etc. an exception will be thrown
  577. /// during conversion.
  578. ///
  579. /// @param value_str Input value.
  580. ///
  581. /// @return boolean representation of the string specified as the parameter.
  582. /// @throw isc::dhcp::BadDataTypeCast if failed to perform the conversion.
  583. bool convertToBool(const std::string& value_str) const;
  584. /// @brief Perform lexical cast of the value and validate its range.
  585. ///
  586. /// This function performs lexical cast of a string value to integer
  587. /// value and checks if the resulting value is within a range of a
  588. /// target type. The target type should be one of the supported
  589. /// integer types.
  590. ///
  591. /// @param value_str input value given as string.
  592. /// @tparam T target integer type for lexical cast.
  593. ///
  594. /// @return Integer value after conversion from the string.
  595. /// @throw isc::dhcp::BadDataTypeCast if conversion was not successful.
  596. template<typename T>
  597. T lexicalCastWithRangeCheck(const std::string& value_str) const;
  598. /// @brief Write the string value into the provided buffer.
  599. ///
  600. /// This method writes the given value to the specified buffer.
  601. /// The provided string value may represent data of different types.
  602. /// The actual data type is specified with the second argument.
  603. /// Based on a value of this argument, this function will first
  604. /// try to cast the string value to the particular data type and
  605. /// if it is successful it will store the data in the buffer
  606. /// in a binary format.
  607. ///
  608. /// @param value string representation of the value to be written.
  609. /// @param type the actual data type to be stored.
  610. /// @param [in, out] buf buffer where the value is to be stored.
  611. ///
  612. /// @throw BadDataTypeCast if data write was unsuccessful.
  613. void writeToBuffer(const std::string& value, const OptionDataType type,
  614. OptionBuffer& buf) const;
  615. /// Option name.
  616. std::string name_;
  617. /// Option code.
  618. uint16_t code_;
  619. /// Option data type.
  620. OptionDataType type_;
  621. /// Indicates wheter option is a single value or array.
  622. bool array_type_;
  623. /// Name of the space being encapsulated by this option.
  624. std::string encapsulated_space_;
  625. /// Collection of data fields within the record.
  626. RecordFieldsCollection record_fields_;
  627. };
  628. /// @brief Multi index container for DHCP option definitions.
  629. ///
  630. /// This container allows to search for DHCP option definition
  631. /// using two indexes:
  632. /// - sequenced: used to access elements in the order they have
  633. /// been added to the container
  634. /// - option code: used to search definitions of options
  635. /// with a specified option code (aka option type).
  636. /// Note that this container can hold multiple options with the
  637. /// same code. For this reason, the latter index can be used to
  638. /// obtain a range of options for a particular option code.
  639. ///
  640. /// @todo: need an index to search options using option space name
  641. /// once option spaces are implemented.
  642. typedef boost::multi_index_container<
  643. // Container comprises elements of OptionDefinition type.
  644. OptionDefinitionPtr,
  645. // Here we start enumerating various indexes.
  646. boost::multi_index::indexed_by<
  647. // Sequenced index allows accessing elements in the same way
  648. // as elements in std::list. Sequenced is an index #0.
  649. boost::multi_index::sequenced<>,
  650. // Start definition of index #1.
  651. boost::multi_index::hashed_non_unique<
  652. // Use option type as the index key. The type is held
  653. // in OptionDefinition object so we have to call
  654. // OptionDefinition::getCode to retrieve this key
  655. // for each element. The option code is non-unique so
  656. // multiple elements with the same option code can
  657. // be returned by this index.
  658. boost::multi_index::const_mem_fun<
  659. OptionDefinition,
  660. uint16_t,
  661. &OptionDefinition::getCode
  662. >
  663. >
  664. >
  665. > OptionDefContainer;
  666. /// Pointer to an option definition container.
  667. typedef boost::shared_ptr<OptionDefContainer> OptionDefContainerPtr;
  668. /// Container that holds various vendor option containers
  669. typedef std::map<uint32_t, OptionDefContainer> VendorOptionDefContainers;
  670. /// Type of the index #1 - option type.
  671. typedef OptionDefContainer::nth_index<1>::type OptionDefContainerTypeIndex;
  672. /// Pair of iterators to represent the range of options definitions
  673. /// having the same option type value. The first element in this pair
  674. /// represents the beginning of the range, the second element
  675. /// represents the end.
  676. typedef std::pair<OptionDefContainerTypeIndex::const_iterator,
  677. OptionDefContainerTypeIndex::const_iterator> OptionDefContainerTypeRange;
  678. } // namespace isc::dhcp
  679. } // namespace isc
  680. #endif // OPTION_DEFINITION_H