dhcp_parsers.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765
  1. // Copyright (C) 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 DHCP_PARSERS_H
  15. #define DHCP_PARSERS_H
  16. #include <asiolink/io_address.h>
  17. #include <cc/data.h>
  18. #include <dhcp/option_definition.h>
  19. #include <dhcpsrv/dhcp_config_parser.h>
  20. #include <dhcpsrv/option_space_container.h>
  21. #include <dhcpsrv/subnet.h>
  22. #include <exceptions/exceptions.h>
  23. #include <stdint.h>
  24. #include <string>
  25. #include <vector>
  26. namespace isc {
  27. namespace dhcp {
  28. /// @brief Storage for option definitions.
  29. typedef OptionSpaceContainer<OptionDefContainer,
  30. OptionDefinitionPtr> OptionDefStorage;
  31. /// @brief Shared pointer to option definitions storage.
  32. typedef boost::shared_ptr<OptionDefStorage> OptionDefStoragePtr;
  33. /// Collection of containers holding option spaces. Each container within
  34. /// a particular option space holds so-called option descriptors.
  35. typedef OptionSpaceContainer<Subnet::OptionContainer,
  36. Subnet::OptionDescriptor> OptionStorage;
  37. /// @brief Shared pointer to option storage.
  38. typedef boost::shared_ptr<OptionStorage> OptionStoragePtr;
  39. /// @brief A template class that stores named elements of a given data type.
  40. ///
  41. /// This template class is provides data value storage for configuration parameters
  42. /// of a given data type. The values are stored by parameter name and as instances
  43. /// of type "ValueType".
  44. ///
  45. /// @param ValueType is the data type of the elements to store.
  46. template<typename ValueType>
  47. class ValueStorage {
  48. public:
  49. /// @brief Stores the the parameter and its value in the store.
  50. ///
  51. /// If the parameter does not exist in the store, then it will be added,
  52. /// otherwise its data value will be updated with the given value.
  53. ///
  54. /// @param name is the name of the paramater to store.
  55. /// @param value is the data value to store.
  56. void setParam(const std::string& name, const ValueType& value) {
  57. values_[name] = value;
  58. }
  59. /// @brief Returns the data value for the given parameter.
  60. ///
  61. /// Finds and returns the data value for the given parameter.
  62. /// @param name is the name of the parameter for which the data
  63. /// value is desired.
  64. ///
  65. /// @return The paramater's data value of type <ValueType>.
  66. /// @throw DhcpConfigError if the parameter is not found.
  67. ValueType getParam(const std::string& name) const {
  68. typename std::map<std::string, ValueType>::const_iterator param
  69. = values_.find(name);
  70. if (param == values_.end()) {
  71. isc_throw(DhcpConfigError, "Missing parameter '"
  72. << name << "'");
  73. }
  74. return (param->second);
  75. }
  76. /// @brief Remove the parameter from the store.
  77. ///
  78. /// Deletes the entry for the given parameter from the store if it
  79. /// exists.
  80. ///
  81. /// @param name is the name of the paramater to delete.
  82. void delParam(const std::string& name) {
  83. values_.erase(name);
  84. }
  85. /// @brief Deletes all of the entries from the store.
  86. ///
  87. void clear() {
  88. values_.clear();
  89. }
  90. private:
  91. /// @brief An std::map of the data values, keyed by parameter names.
  92. std::map<std::string, ValueType> values_;
  93. };
  94. /// @brief a collection of elements that store uint32 values
  95. typedef ValueStorage<uint32_t> Uint32Storage;
  96. typedef boost::shared_ptr<Uint32Storage> Uint32StoragePtr;
  97. /// @brief a collection of elements that store string values
  98. typedef ValueStorage<std::string> StringStorage;
  99. typedef boost::shared_ptr<StringStorage> StringStoragePtr;
  100. /// @brief Storage for parsed boolean values.
  101. typedef ValueStorage<bool> BooleanStorage;
  102. typedef boost::shared_ptr<BooleanStorage> BooleanStoragePtr;
  103. /// @brief Container for the current parsing context. It provides a
  104. /// single enclosure for the storage of configuration parameters,
  105. /// options, option definitions, and other context specific information
  106. /// that needs to be accessible throughout the parsing and parsing
  107. /// constructs.
  108. class ParserContext {
  109. public:
  110. /// @brief Constructor
  111. ///
  112. /// @param universe is the Option::Universe value of this
  113. /// context.
  114. ParserContext(Option::Universe universe);
  115. /// @brief Copy constructor
  116. ParserContext(const ParserContext& rhs);
  117. /// @brief Storage for boolean parameters.
  118. BooleanStoragePtr boolean_values_;
  119. /// @brief Storage for uint32 parameters.
  120. Uint32StoragePtr uint32_values_;
  121. /// @brief Storage for string parameters.
  122. StringStoragePtr string_values_;
  123. /// @brief Storage for options.
  124. OptionStoragePtr options_;
  125. /// @brief Storage for option definitions.
  126. OptionDefStoragePtr option_defs_;
  127. /// @brief The parsing universe of this context.
  128. Option::Universe universe_;
  129. /// @brief Assignment operator
  130. ParserContext& operator=(const ParserContext& rhs);
  131. };
  132. /// @brief Pointer to various parser context.
  133. typedef boost::shared_ptr<ParserContext> ParserContextPtr;
  134. /// @brief Simple data-type parser template class
  135. ///
  136. /// This is the template class for simple data-type parsers. It supports
  137. /// parsing a configuration parameter with specific data-type for its
  138. /// possible values. It provides a common constructor, commit, and templated
  139. /// data storage. The "build" method implementation must be provided by a
  140. /// declaring type.
  141. /// @param ValueType is the data type of the configuration paramater value
  142. /// the parser should handle.
  143. template<typename ValueType>
  144. class ValueParser : public DhcpConfigParser {
  145. public:
  146. /// @brief Constructor.
  147. ///
  148. /// @param param_name name of the parameter.
  149. /// @param storage is a pointer to the storage container where the parsed
  150. /// value be stored upon commit.
  151. /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
  152. /// name is empty.
  153. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  154. ValueParser(const std::string& param_name,
  155. boost::shared_ptr<ValueStorage<ValueType> > storage)
  156. : storage_(storage), param_name_(param_name), value_() {
  157. // Empty parameter name is invalid.
  158. if (param_name_.empty()) {
  159. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  160. << "empty parameter name provided");
  161. }
  162. // NUll storage is invalid.
  163. if (!storage_) {
  164. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  165. << "storage may not be NULL");
  166. }
  167. }
  168. /// @brief Parse a given element into a value of type <ValueType>
  169. ///
  170. /// @param value a value to be parsed.
  171. ///
  172. /// @throw isc::BadValue Typically the implementing type will throw
  173. /// a BadValue exception when given an invalid Element to parse.
  174. void build(isc::data::ConstElementPtr value);
  175. /// @brief Put a parsed value to the storage.
  176. void commit() {
  177. // If a given parameter already exists in the storage we override
  178. // its value. If it doesn't we insert a new element.
  179. storage_->setParam(param_name_, value_);
  180. }
  181. private:
  182. /// Pointer to the storage where committed value is stored.
  183. boost::shared_ptr<ValueStorage<ValueType> > storage_;
  184. /// Name of the parameter which value is parsed with this parser.
  185. std::string param_name_;
  186. /// Parsed value.
  187. ValueType value_;
  188. };
  189. /// @brief typedefs for simple data type parsers
  190. typedef ValueParser<bool> BooleanParser;
  191. typedef ValueParser<uint32_t> Uint32Parser;
  192. typedef ValueParser<std::string> StringParser;
  193. /// @brief a dummy configuration parser
  194. ///
  195. /// It is a debugging parser. It does not configure anything,
  196. /// will accept any configuration and will just print it out
  197. /// on commit. Useful for debugging existing configurations and
  198. /// adding new ones.
  199. class DebugParser : public DhcpConfigParser {
  200. public:
  201. /// @brief Constructor
  202. ///
  203. /// See @ref DhcpConfigParser class for details.
  204. ///
  205. /// @param param_name name of the parsed parameter
  206. DebugParser(const std::string& param_name);
  207. /// @brief builds parameter value
  208. ///
  209. /// See @ref DhcpConfigParser class for details.
  210. ///
  211. /// @param new_config pointer to the new configuration
  212. virtual void build(isc::data::ConstElementPtr new_config);
  213. /// @brief pretends to apply the configuration
  214. ///
  215. /// This is a method required by base class. It pretends to apply the
  216. /// configuration, but in fact it only prints the parameter out.
  217. ///
  218. /// See @ref DhcpConfigParser class for details.
  219. virtual void commit();
  220. private:
  221. /// name of the parsed parameter
  222. std::string param_name_;
  223. /// pointer to the actual value of the parameter
  224. isc::data::ConstElementPtr value_;
  225. };
  226. /// @brief parser for interface list definition
  227. ///
  228. /// This parser handles Dhcp4/interface entry.
  229. /// It contains a list of network interfaces that the server listens on.
  230. /// In particular, it can contain an entry called "all" or "any" that
  231. /// designates all interfaces.
  232. ///
  233. /// It is useful for parsing Dhcp4/interface parameter.
  234. class InterfaceListConfigParser : public DhcpConfigParser {
  235. public:
  236. /// @brief constructor
  237. ///
  238. /// As this is a dedicated parser, it must be used to parse
  239. /// "interface" parameter only. All other types will throw exception.
  240. ///
  241. /// @param param_name name of the configuration parameter being parsed
  242. /// @throw BadValue if supplied parameter name is not "interface"
  243. InterfaceListConfigParser(const std::string& param_name);
  244. /// @brief parses parameters value
  245. ///
  246. /// Parses configuration entry (list of parameters) and adds each element
  247. /// to the interfaces list.
  248. ///
  249. /// @param value pointer to the content of parsed values
  250. virtual void build(isc::data::ConstElementPtr value);
  251. /// @brief commits interfaces list configuration
  252. virtual void commit();
  253. private:
  254. /// contains list of network interfaces
  255. std::vector<std::string> interfaces_;
  256. };
  257. /// @brief Parser for option data value.
  258. ///
  259. /// This parser parses configuration entries that specify value of
  260. /// a single option. These entries include option name, option code
  261. /// and data carried by the option. The option data can be specified
  262. /// in one of the two available formats: binary value represented as
  263. /// a string of hexadecimal digits or a list of comma separated values.
  264. /// The format being used is controlled by csv-format configuration
  265. /// parameter. When setting this value to True, the latter format is
  266. /// used. The subsequent values in the CSV format apply to relevant
  267. /// option data fields in the configured option. For example the
  268. /// configuration: "data" : "192.168.2.0, 56, hello world" can be
  269. /// used to set values for the option comprising IPv4 address,
  270. /// integer and string data field. Note that order matters. If the
  271. /// order of values does not match the order of data fields within
  272. /// an option the configuration will not be accepted. If parsing
  273. /// is successful then an instance of an option is created and
  274. /// added to the storage provided by the calling class.
  275. class OptionDataParser : public DhcpConfigParser {
  276. public:
  277. /// @brief Constructor.
  278. ///
  279. /// @param dummy first argument is ignored, all Parser constructors
  280. /// accept string as first argument.
  281. /// @param options is the option storage in which to store the parsed option
  282. /// upon "commit".
  283. /// @param global_context is a pointer to the global context which
  284. /// stores global scope parameters, options, option defintions.
  285. /// @throw isc::dhcp::DhcpConfigError if options or global_context are null.
  286. OptionDataParser(const std::string&, OptionStoragePtr options,
  287. ParserContextPtr global_context);
  288. /// @brief Parses the single option data.
  289. ///
  290. /// This method parses the data of a single option from the configuration.
  291. /// The option data includes option name, option code and data being
  292. /// carried by this option. Eventually it creates the instance of the
  293. /// option.
  294. ///
  295. /// @param option_data_entries collection of entries that define value
  296. /// for a particular option.
  297. /// @throw DhcpConfigError if invalid parameter specified in
  298. /// the configuration.
  299. /// @throw isc::InvalidOperation if failed to set storage prior to
  300. /// calling build.
  301. virtual void build(isc::data::ConstElementPtr option_data_entries);
  302. /// @brief Commits option value.
  303. ///
  304. /// This function adds a new option to the storage or replaces an existing
  305. /// option with the same code.
  306. ///
  307. /// @throw isc::InvalidOperation if failed to set pointer to storage or
  308. /// failed
  309. /// to call build() prior to commit. If that happens data in the storage
  310. /// remain un-modified.
  311. virtual void commit();
  312. /// @brief virtual destructor to ensure orderly destruction of derivations.
  313. virtual ~OptionDataParser(){};
  314. protected:
  315. /// @brief Finds an option definition within the server's option space
  316. ///
  317. /// Given an option space and an option code, find the correpsonding
  318. /// option defintion within the server's option defintion storage. This
  319. /// method is pure virtual requiring derivations to manage which option
  320. /// space(s) is valid for search.
  321. ///
  322. /// @param option_space name of the parameter option space
  323. /// @param option_code numeric value of the parameter to find
  324. /// @return OptionDefintionPtr of the option defintion or an
  325. /// empty OptionDefinitionPtr if not found.
  326. /// @throw DhcpConfigError if the option space requested is not valid
  327. /// for this server.
  328. virtual OptionDefinitionPtr findServerSpaceOptionDefinition (
  329. std::string& option_space, uint32_t option_code) = 0;
  330. private:
  331. /// @brief Create option instance.
  332. ///
  333. /// Creates an instance of an option and adds it to the provided
  334. /// options storage. If the option data parsed by \ref build function
  335. /// are invalid or insufficient this function emits an exception.
  336. ///
  337. /// @warning this function does not check if options_ storage pointer
  338. /// is intitialized but this check is not needed here because it is done
  339. /// in the \ref build function.
  340. ///
  341. /// @throw DhcpConfigError if parameters provided in the configuration
  342. /// are invalid.
  343. void createOption();
  344. /// Storage for boolean values.
  345. BooleanStoragePtr boolean_values_;
  346. /// Storage for string values (e.g. option name or data).
  347. StringStoragePtr string_values_;
  348. /// Storage for uint32 values (e.g. option code).
  349. Uint32StoragePtr uint32_values_;
  350. /// Pointer to options storage. This storage is provided by
  351. /// the calling class and is shared by all OptionDataParser objects.
  352. OptionStoragePtr options_;
  353. /// Option descriptor holds newly configured option.
  354. Subnet::OptionDescriptor option_descriptor_;
  355. /// Option space name where the option belongs to.
  356. std::string option_space_;
  357. /// Parsing context which contains global values, options and option
  358. /// definitions.
  359. ParserContextPtr global_context_;
  360. };
  361. ///@brief Function pointer for OptionDataParser factory methods
  362. typedef OptionDataParser *OptionDataParserFactory(const std::string&,
  363. OptionStoragePtr options, ParserContextPtr global_context);
  364. /// @brief Parser for option data values within a subnet.
  365. ///
  366. /// This parser iterates over all entries that define options
  367. /// data for a particular subnet and creates a collection of options.
  368. /// If parsing is successful, all these options are added to the Subnet
  369. /// object.
  370. class OptionDataListParser : public DhcpConfigParser {
  371. public:
  372. /// @brief Constructor.
  373. ///
  374. /// @param string& nominally would be param name, this is always ignored.
  375. /// @param options parsed option storage for options in this list
  376. /// @param global_context is a pointer to the global context which
  377. /// stores global scope parameters, options, option defintions.
  378. /// @param optionDataParserFactory factory method for creating individual
  379. /// option parsers
  380. /// @throw isc::dhcp::DhcpConfigError if options or global_context are null.
  381. OptionDataListParser(const std::string&, OptionStoragePtr options,
  382. ParserContextPtr global_context,
  383. OptionDataParserFactory *optionDataParserFactory);
  384. /// @brief Parses entries that define options' data for a subnet.
  385. ///
  386. /// This method iterates over all entries that define option data
  387. /// for options within a single subnet and creates options' instances.
  388. ///
  389. /// @param option_data_list pointer to a list of options' data sets.
  390. /// @throw DhcpConfigError if option parsing failed.
  391. void build(isc::data::ConstElementPtr option_data_list);
  392. /// @brief Commit all option values.
  393. ///
  394. /// This function invokes commit for all option values.
  395. void commit();
  396. private:
  397. /// Pointer to options instances storage.
  398. OptionStoragePtr options_;
  399. /// Intermediate option storage. This storage is used by
  400. /// lower level parsers to add new options. Values held
  401. /// in this storage are assigned to main storage (options_)
  402. /// if overall parsing was successful.
  403. OptionStoragePtr local_options_;
  404. /// Collection of parsers;
  405. ParserCollection parsers_;
  406. /// Parsing context which contains global values, options and option
  407. /// definitions.
  408. ParserContextPtr global_context_;
  409. /// Factory to create server-specific option data parsers
  410. OptionDataParserFactory *optionDataParserFactory_;
  411. };
  412. /// @brief Parser for a single option definition.
  413. ///
  414. /// This parser creates an instance of a single option definition.
  415. class OptionDefParser : public DhcpConfigParser {
  416. public:
  417. /// @brief Constructor.
  418. ///
  419. /// @param dummy first argument is ignored, all Parser constructors
  420. /// accept string as first argument.
  421. /// @param storage is the definition storage in which to store the parsed
  422. /// definition upon "commit".
  423. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  424. OptionDefParser(const std::string&, OptionDefStoragePtr storage);
  425. /// @brief Parses an entry that describes single option definition.
  426. ///
  427. /// @param option_def a configuration entry to be parsed.
  428. ///
  429. /// @throw DhcpConfigError if parsing was unsuccessful.
  430. void build(isc::data::ConstElementPtr option_def);
  431. /// @brief Stores the parsed option definition in a storage.
  432. void commit();
  433. private:
  434. /// @brief Create option definition from the parsed parameters.
  435. void createOptionDef();
  436. /// Instance of option definition being created by this parser.
  437. OptionDefinitionPtr option_definition_;
  438. /// Name of the space the option definition belongs to.
  439. std::string option_space_name_;
  440. /// Pointer to a storage where the option definition will be
  441. /// added when \ref commit is called.
  442. OptionDefStoragePtr storage_;
  443. /// Storage for boolean values.
  444. BooleanStoragePtr boolean_values_;
  445. /// Storage for string values.
  446. StringStoragePtr string_values_;
  447. /// Storage for uint32 values.
  448. Uint32StoragePtr uint32_values_;
  449. };
  450. /// @brief Parser for a list of option definitions.
  451. ///
  452. /// This parser iterates over all configuration entries that define
  453. /// option definitions and creates instances of these definitions.
  454. /// If the parsing is successful, the collection of created definitions
  455. /// is put into the provided storage.
  456. class OptionDefListParser : public DhcpConfigParser {
  457. public:
  458. /// @brief Constructor.
  459. ///
  460. /// @param dummy first argument is ignored, all Parser constructors
  461. /// accept string as first argument.
  462. /// @param storage is the definition storage in which to store the parsed
  463. /// definitions in this list
  464. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  465. OptionDefListParser(const std::string&, OptionDefStoragePtr storage);
  466. /// @brief Parse configuration entries.
  467. ///
  468. /// This function parses configuration entries and creates instances
  469. /// of option definitions.
  470. ///
  471. /// @param option_def_list pointer to an element that holds entries
  472. /// that define option definitions.
  473. /// @throw DhcpConfigError if configuration parsing fails.
  474. void build(isc::data::ConstElementPtr option_def_list);
  475. /// @brief Stores option definitions in the CfgMgr.
  476. void commit();
  477. private:
  478. /// @brief storage for option definitions.
  479. OptionDefStoragePtr storage_;
  480. };
  481. /// @brief a collection of pools
  482. ///
  483. /// That type is used as intermediate storage, when pools are parsed, but there is
  484. /// no subnet object created yet to store them.
  485. typedef std::vector<PoolPtr> PoolStorage;
  486. typedef boost::shared_ptr<PoolStorage> PoolStoragePtr;
  487. /// @brief parser for pool definition
  488. ///
  489. /// This abstract parser handles pool definitions, i.e. a list of entries of one
  490. /// of two syntaxes: min-max and prefix/len. Pool objects are created
  491. /// and stored in chosen PoolStorage container.
  492. ///
  493. /// It is useful for parsing Dhcp<4/6>/subnet<4/6>[X]/pool parameters.
  494. class PoolParser : public DhcpConfigParser {
  495. public:
  496. /// @brief constructor.
  497. /// @param dummy first argument is ignored, all Parser constructors
  498. /// accept string as first argument.
  499. /// @param pools is the storage in which to store the parsed pool
  500. /// upon "commit".
  501. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  502. PoolParser(const std::string&, PoolStoragePtr pools);
  503. /// @brief parses the actual list
  504. ///
  505. /// This method parses the actual list of interfaces.
  506. /// No validation is done at this stage, everything is interpreted as
  507. /// interface name.
  508. /// @param pools_list list of pools defined for a subnet
  509. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  510. virtual void build(isc::data::ConstElementPtr pools_list);
  511. /// @brief Stores the parsed values in a storage provided
  512. /// by an upper level parser.
  513. virtual void commit();
  514. protected:
  515. /// @brief Creates a Pool object given a IPv4 prefix and the prefix length.
  516. ///
  517. /// @param addr is the IP prefix of the pool.
  518. /// @param len is the prefix length.
  519. /// @param ignored dummy parameter to provide symmetry between
  520. /// @return returns a PoolPtr to the new Pool object.
  521. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &addr, uint32_t len,
  522. int32_t ptype=0) = 0;
  523. /// @brief Creates a Pool object given starting and ending IP addresses.
  524. ///
  525. /// @param min is the first IP address in the pool.
  526. /// @param max is the last IP address in the pool.
  527. /// @param ptype is the type of pool to create (not used by all derivations)
  528. /// @return returns a PoolPtr to the new Pool object.
  529. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &min,
  530. isc::asiolink::IOAddress &max, int32_t ptype=0) = 0;
  531. /// @brief pointer to the actual Pools storage
  532. ///
  533. /// That is typically a storage somewhere in Subnet parser
  534. /// (an upper level parser).
  535. PoolStoragePtr pools_;
  536. /// A temporary storage for pools configuration. It is a
  537. /// storage where pools are stored by build function.
  538. PoolStorage local_pools_;
  539. };
  540. /// @brief this class parses a single subnet
  541. ///
  542. /// This class parses the whole subnet definition. It creates parsers
  543. /// for received configuration parameters as needed.
  544. class SubnetConfigParser : public DhcpConfigParser {
  545. public:
  546. /// @brief constructor
  547. SubnetConfigParser(const std::string&, ParserContextPtr global_context);
  548. /// @brief parses parameter value
  549. ///
  550. /// @param subnet pointer to the content of subnet definition
  551. ///
  552. /// @throw isc::DhcpConfigError if subnet configuration parsing failed.
  553. virtual void build(isc::data::ConstElementPtr subnet);
  554. /// @brief Adds the created subnet to a server's configuration.
  555. virtual void commit() = 0;
  556. protected:
  557. /// @brief creates parsers for entries in subnet definition
  558. ///
  559. /// @param config_id name od the entry
  560. ///
  561. /// @return parser object for specified entry name
  562. /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
  563. /// for unknown config element
  564. virtual DhcpConfigParser* createSubnetConfigParser(
  565. const std::string& config_id) = 0;
  566. /// @brief Determines if the given option space name and code describe
  567. /// a standard option for the server.
  568. ///
  569. /// @param option_space is the name of the option space to consider
  570. /// @param code is the numeric option code to consider
  571. /// @return returns true if the space and code are part of the server's
  572. /// standard options.
  573. virtual bool isServerStdOption(std::string option_space, uint32_t code) = 0;
  574. /// @brief Returns the option definition for a given option code from
  575. /// the server's standard set of options.
  576. /// @param code is the numeric option code of the desired option definition.
  577. /// @return returns a pointer the option definition
  578. virtual OptionDefinitionPtr getServerStdOptionDefinition (
  579. uint32_t code) = 0;
  580. /// @brief Issues a server specific warning regarding duplicate subnet
  581. /// options.
  582. ///
  583. /// @param code is the numeric option code of the duplicate option
  584. /// @param addr is the subnet address
  585. /// @todo a means to know the correct logger and perhaps a common
  586. /// message would allow this method to be emitted by the base class.
  587. virtual void duplicate_option_warning(uint32_t code,
  588. isc::asiolink::IOAddress& addr) = 0;
  589. /// @brief Instantiates the subnet based on a given IP prefix and prefix
  590. /// length.
  591. ///
  592. /// @param addr is the IP prefix of the subnet.
  593. /// @param len is the prefix length
  594. virtual void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) = 0;
  595. /// @brief Returns value for a given parameter (after using inheritance)
  596. ///
  597. /// This method implements inheritance. For a given parameter name, it first
  598. /// checks if there is a global value for it and overwrites it with specific
  599. /// value if such value was defined in subnet.
  600. ///
  601. /// @param name name of the parameter
  602. /// @return triplet with the parameter name
  603. /// @throw DhcpConfigError when requested parameter is not present
  604. isc::dhcp::Triplet<uint32_t> getParam(const std::string& name);
  605. private:
  606. /// @brief Append sub-options to an option.
  607. ///
  608. /// @param option_space a name of the encapsulated option space.
  609. /// @param option option instance to append sub-options to.
  610. void appendSubOptions(const std::string& option_space, OptionPtr& option);
  611. /// @brief Create a new subnet using a data from child parsers.
  612. ///
  613. /// @throw isc::dhcp::DhcpConfigError if subnet configuration parsing
  614. /// failed.
  615. void createSubnet();
  616. protected:
  617. /// Storage for subnet-specific integer values.
  618. Uint32StoragePtr uint32_values_;
  619. /// Storage for subnet-specific string values.
  620. StringStoragePtr string_values_;
  621. /// Storage for pools belonging to this subnet.
  622. PoolStoragePtr pools_;
  623. /// Storage for options belonging to this subnet.
  624. OptionStoragePtr options_;
  625. /// Parsers are stored here.
  626. ParserCollection parsers_;
  627. /// Pointer to the created subnet object.
  628. isc::dhcp::SubnetPtr subnet_;
  629. /// Parsing context which contains global values, options and option
  630. /// definitions.
  631. ParserContextPtr global_context_;
  632. };
  633. // Pointers to various parser objects.
  634. typedef boost::shared_ptr<BooleanParser> BooleanParserPtr;
  635. typedef boost::shared_ptr<StringParser> StringParserPtr;
  636. typedef boost::shared_ptr<Uint32Parser> Uint32ParserPtr;
  637. }; // end of isc::dhcp namespace
  638. }; // end of isc namespace
  639. #endif // DHCP_PARSERS_H