dhcp_parsers.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. // Copyright (C) 2013-2017 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef DHCP_PARSERS_H
  7. #define DHCP_PARSERS_H
  8. #include <asiolink/io_address.h>
  9. #include <cc/data.h>
  10. #include <dhcp/option_definition.h>
  11. #include <dhcp/option_space_container.h>
  12. #include <dhcpsrv/d2_client_cfg.h>
  13. #include <dhcpsrv/cfg_iface.h>
  14. #include <dhcpsrv/cfg_option.h>
  15. #include <dhcpsrv/subnet.h>
  16. #include <dhcpsrv/cfg_option_def.h>
  17. #include <dhcpsrv/cfg_mac_source.h>
  18. #include <dhcpsrv/srv_config.h>
  19. #include <dhcpsrv/parsers/dhcp_config_parser.h>
  20. #include <cc/simple_parser.h>
  21. #include <exceptions/exceptions.h>
  22. #include <util/optional_value.h>
  23. #include <boost/shared_ptr.hpp>
  24. #include <stdint.h>
  25. #include <string>
  26. #include <vector>
  27. namespace isc {
  28. namespace dhcp {
  29. /// Collection of containers holding option spaces. Each container within
  30. /// a particular option space holds so-called option descriptors.
  31. typedef OptionSpaceContainer<OptionContainer, OptionDescriptor,
  32. std::string> OptionStorage;
  33. /// @brief Shared pointer to option storage.
  34. typedef boost::shared_ptr<OptionStorage> OptionStoragePtr;
  35. /// @brief A template class that stores named elements of a given data type.
  36. ///
  37. /// This template class is provides data value storage for configuration
  38. /// parameters of a given data type. The values are stored by parameter name
  39. /// and as instances of type "ValueType". Each value held in the storage has
  40. /// a corresponding position within a configuration string (file) specified
  41. /// as a: file name, line number and position within the line. The position
  42. /// information is used for logging when the particular configuration value
  43. /// causes a configuration error.
  44. ///
  45. /// @tparam 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, its value and the position in the
  50. /// store.
  51. ///
  52. /// If the parameter does not exist in the store, then it will be added,
  53. /// otherwise its data value and the position will be updated with the
  54. /// given values.
  55. ///
  56. /// @param name is the name of the parameter to store.
  57. /// @param value is the data value to store.
  58. /// @param position is the position of the data element within a
  59. /// configuration string (file).
  60. void setParam(const std::string& name, const ValueType& value,
  61. const data::Element::Position& position) {
  62. values_[name] = value;
  63. positions_[name] = position;
  64. }
  65. /// @brief Returns the data value for the given parameter.
  66. ///
  67. /// Finds and returns the data value for the given parameter.
  68. /// @param name is the name of the parameter for which the data
  69. /// value is desired.
  70. ///
  71. /// @return The parameter's data value of type @c ValueType.
  72. /// @throw DhcpConfigError if the parameter is not found.
  73. ValueType getParam(const std::string& name) const {
  74. typename std::map<std::string, ValueType>::const_iterator param
  75. = values_.find(name);
  76. if (param == values_.end()) {
  77. isc_throw(DhcpConfigError, "Missing parameter '"
  78. << name << "'");
  79. }
  80. return (param->second);
  81. }
  82. /// @brief Returns position of the data element in the configuration string.
  83. ///
  84. /// The returned object comprises file name, line number and the position
  85. /// within the particular line of the configuration string where the data
  86. /// element holding a particular value is located.
  87. ///
  88. /// @param name is the name of the parameter which position is desired.
  89. /// @param parent Pointer to a data element which position should be
  90. /// returned when position of the specified parameter is not found.
  91. ///
  92. /// @return Position of the data element or the position holding empty
  93. /// file name and two zeros if the position hasn't been specified for the
  94. /// particular value.
  95. const data::Element::Position&
  96. getPosition(const std::string& name, const data::ConstElementPtr parent =
  97. data::ConstElementPtr()) const {
  98. typename std::map<std::string, data::Element::Position>::const_iterator
  99. pos = positions_.find(name);
  100. if (pos == positions_.end()) {
  101. return (parent ? parent->getPosition() :
  102. data::Element::ZERO_POSITION());
  103. }
  104. return (pos->second);
  105. }
  106. /// @brief Returns the data value for an optional parameter.
  107. ///
  108. /// Finds and returns the data value for the given parameter or
  109. /// a supplied default value if it is not found.
  110. ///
  111. /// @param name is the name of the parameter for which the data
  112. /// value is desired.
  113. /// @param default_value value to use the default
  114. ///
  115. /// @return The parameter's data value of type @c ValueType.
  116. ValueType getOptionalParam(const std::string& name,
  117. const ValueType& default_value) const {
  118. typename std::map<std::string, ValueType>::const_iterator param
  119. = values_.find(name);
  120. if (param == values_.end()) {
  121. return (default_value);
  122. }
  123. return (param->second);
  124. }
  125. /// @brief Remove the parameter from the store.
  126. ///
  127. /// Deletes the entry for the given parameter from the store if it
  128. /// exists.
  129. ///
  130. /// @param name is the name of the parameter to delete.
  131. void delParam(const std::string& name) {
  132. values_.erase(name);
  133. positions_.erase(name);
  134. }
  135. /// @brief Deletes all of the entries from the store.
  136. ///
  137. void clear() {
  138. values_.clear();
  139. positions_.clear();
  140. }
  141. private:
  142. /// @brief An std::map of the data values, keyed by parameter names.
  143. std::map<std::string, ValueType> values_;
  144. /// @brief An std::map holding positions of the data elements in the
  145. /// configuration, which values are held in @c values_.
  146. ///
  147. /// The position is used for logging, when the particular value
  148. /// causes a configuration error.
  149. std::map<std::string, data::Element::Position> positions_;
  150. };
  151. /// @brief a collection of elements that store uint32 values
  152. typedef ValueStorage<uint32_t> Uint32Storage;
  153. typedef boost::shared_ptr<Uint32Storage> Uint32StoragePtr;
  154. /// @brief a collection of elements that store string values
  155. typedef ValueStorage<std::string> StringStorage;
  156. typedef boost::shared_ptr<StringStorage> StringStoragePtr;
  157. /// @brief Storage for parsed boolean values.
  158. typedef ValueStorage<bool> BooleanStorage;
  159. typedef boost::shared_ptr<BooleanStorage> BooleanStoragePtr;
  160. /// @brief Simple data-type parser template class
  161. ///
  162. /// This is the template class for simple data-type parsers. It supports
  163. /// parsing a configuration parameter with specific data-type for its
  164. /// possible values. It provides a common constructor, commit, and templated
  165. /// data storage. The "build" method implementation must be provided by a
  166. /// declaring type.
  167. /// @param ValueType is the data type of the configuration parameter value
  168. /// the parser should handle.
  169. template<typename ValueType>
  170. class ValueParser : public DhcpConfigParser {
  171. public:
  172. /// @brief Constructor.
  173. ///
  174. /// @param param_name name of the parameter.
  175. /// @param storage is a pointer to the storage container where the parsed
  176. /// value be stored upon commit.
  177. /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
  178. /// name is empty.
  179. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  180. ValueParser(const std::string& param_name,
  181. boost::shared_ptr<ValueStorage<ValueType> > storage)
  182. : storage_(storage), param_name_(param_name), value_(), pos_() {
  183. // Empty parameter name is invalid.
  184. if (param_name_.empty()) {
  185. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  186. << "empty parameter name provided");
  187. }
  188. // Null storage is invalid.
  189. if (!storage_) {
  190. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  191. << "storage may not be NULL");
  192. }
  193. }
  194. /// @brief Parse a given element into a value of type @c ValueType
  195. ///
  196. /// @param value a value to be parsed.
  197. ///
  198. /// @throw isc::BadValue Typically the implementing type will throw
  199. /// a BadValue exception when given an invalid Element to parse.
  200. void build(isc::data::ConstElementPtr value);
  201. /// @brief Put a parsed value to the storage.
  202. void commit() {
  203. // If a given parameter already exists in the storage we override
  204. // its value. If it doesn't we insert a new element.
  205. storage_->setParam(param_name_, value_, pos_);
  206. }
  207. private:
  208. /// @brief Performs operations common for all specializations of the
  209. /// @c build function.
  210. ///
  211. /// This method should be called by all specializations of the @c build
  212. /// method.
  213. ///
  214. /// @param value a value being parsed.
  215. void buildCommon(isc::data::ConstElementPtr value) {
  216. // Remember position of the data element.
  217. pos_ = value->getPosition();
  218. }
  219. /// Pointer to the storage where committed value is stored.
  220. boost::shared_ptr<ValueStorage<ValueType> > storage_;
  221. /// Name of the parameter which value is parsed with this parser.
  222. std::string param_name_;
  223. /// Parsed value.
  224. ValueType value_;
  225. data::Element::Position pos_;
  226. };
  227. /// @brief typedefs for simple data type parsers
  228. typedef ValueParser<bool> BooleanParser;
  229. typedef ValueParser<uint32_t> Uint32Parser;
  230. typedef ValueParser<std::string> StringParser;
  231. /// @brief a dummy configuration parser
  232. ///
  233. /// It is a debugging parser. It does not configure anything,
  234. /// will accept any configuration and will just print it out
  235. /// on commit. Useful for debugging existing configurations and
  236. /// adding new ones.
  237. class DebugParser : public DhcpConfigParser {
  238. public:
  239. /// @brief Constructor
  240. ///
  241. /// See @ref DhcpConfigParser class for details.
  242. ///
  243. /// @param param_name name of the parsed parameter
  244. DebugParser(const std::string& param_name);
  245. /// @brief builds parameter value
  246. ///
  247. /// See @ref DhcpConfigParser class for details.
  248. ///
  249. /// @param new_config pointer to the new configuration
  250. virtual void build(isc::data::ConstElementPtr new_config);
  251. /// @brief pretends to apply the configuration
  252. ///
  253. /// This is a method required by base class. It pretends to apply the
  254. /// configuration, but in fact it only prints the parameter out.
  255. ///
  256. /// See @ref DhcpConfigParser class for details.
  257. virtual void commit();
  258. private:
  259. /// name of the parsed parameter
  260. std::string param_name_;
  261. /// pointer to the actual value of the parameter
  262. isc::data::ConstElementPtr value_;
  263. };
  264. /// @brief parser for MAC/hardware acquisition sources
  265. ///
  266. /// This parser handles Dhcp6/mac-sources entry.
  267. /// It contains a list of MAC/hardware acquisition source, i.e. methods how
  268. /// MAC address can possibly by obtained in DHCPv6. For a currently supported
  269. /// methods, see @ref isc::dhcp::Pkt::getMAC.
  270. class MACSourcesListConfigParser : public isc::data::SimpleParser {
  271. public:
  272. /// @brief parses parameters value
  273. ///
  274. /// Parses configuration entry (list of sources) and adds each element
  275. /// to the sources list.
  276. ///
  277. /// @param value pointer to the content of parsed values
  278. /// @param mac_sources parsed sources will be stored here
  279. void parse(CfgMACSource& mac_sources, isc::data::ConstElementPtr value);
  280. };
  281. /// @brief Parser for the control-socket structure
  282. ///
  283. /// It does not parse anything, simply stores the element in
  284. /// the staging config.
  285. class ControlSocketParser : public isc::data::SimpleParser {
  286. public:
  287. /// @brief "Parses" control-socket structure
  288. ///
  289. /// Since the SrvConfig structure takes the socket definition
  290. /// as ConstElementPtr, there's really nothing to parse here.
  291. /// It only does basic sanity checks and throws DhcpConfigError
  292. /// if the value is null or is not a map.
  293. ///
  294. /// @param srv_cfg parsed values will be stored here
  295. /// @param value pointer to the content of parsed values
  296. void parse(SrvConfig& srv_cfg, isc::data::ConstElementPtr value);
  297. };
  298. /// @brief Parser for option data value.
  299. ///
  300. /// This parser parses configuration entries that specify value of
  301. /// a single option. These entries include option name, option code
  302. /// and data carried by the option. The option data can be specified
  303. /// in one of the two available formats: binary value represented as
  304. /// a string of hexadecimal digits or a list of comma separated values.
  305. /// The format being used is controlled by csv-format configuration
  306. /// parameter. When setting this value to True, the latter format is
  307. /// used. The subsequent values in the CSV format apply to relevant
  308. /// option data fields in the configured option. For example the
  309. /// configuration: "data" : "192.168.2.0, 56, hello world" can be
  310. /// used to set values for the option comprising IPv4 address,
  311. /// integer and string data field. Note that order matters. If the
  312. /// order of values does not match the order of data fields within
  313. /// an option the configuration will not be accepted. If parsing
  314. /// is successful then an instance of an option is created and
  315. /// added to the storage provided by the calling class.
  316. class OptionDataParser : public isc::data::SimpleParser {
  317. public:
  318. /// @brief Constructor.
  319. ///
  320. /// @param address_family Address family: @c AF_INET or @c AF_INET6.
  321. explicit OptionDataParser(const uint16_t address_family);
  322. /// @brief Parses ElementPtr containing option definition
  323. ///
  324. /// This method parses ElementPtr containing the option definition,
  325. /// instantiates the option for it and then returns a pair
  326. /// of option descriptor (that holds that new option) and
  327. /// a string that specifies the option space.
  328. ///
  329. /// Note: ElementPtr is expected to contain all fields. If your
  330. /// ElementPtr does not have them, please use
  331. /// @ref isc::data::SimpleParser::setDefaults to fill the missing fields
  332. /// with default values.
  333. ///
  334. /// @param single_option ElementPtr containing option definition
  335. /// @return Option object wrapped in option description and an option
  336. /// space
  337. std::pair<OptionDescriptor, std::string>
  338. parse(isc::data::ConstElementPtr single_option);
  339. private:
  340. /// @brief Finds an option definition within an option space
  341. ///
  342. /// Given an option space and an option code, find the corresponding
  343. /// option definition within the option definition storage.
  344. ///
  345. /// @param option_space name of the parameter option space
  346. /// @param search_key an option code or name to be used to lookup the
  347. /// option definition.
  348. /// @tparam A numeric type for searching using an option code or the
  349. /// string for searching using the option name.
  350. ///
  351. /// @return OptionDefinitionPtr of the option definition or an
  352. /// empty OptionDefinitionPtr if not found.
  353. /// @throw DhcpConfigError if the option space requested is not valid
  354. /// for this server.
  355. template<typename SearchKey>
  356. OptionDefinitionPtr findOptionDefinition(const std::string& option_space,
  357. const SearchKey& search_key) const;
  358. /// @brief Create option instance.
  359. ///
  360. /// Creates an instance of an option and adds it to the provided
  361. /// options storage. If the option data parsed by \ref build function
  362. /// are invalid or insufficient this function emits an exception.
  363. ///
  364. /// @param option_data An element holding data for a single option being
  365. /// created.
  366. ///
  367. /// @return created option descriptor
  368. ///
  369. /// @throw DhcpConfigError if parameters provided in the configuration
  370. /// are invalid.
  371. std::pair<OptionDescriptor, std::string>
  372. createOption(isc::data::ConstElementPtr option_data);
  373. /// @brief Retrieves parsed option code as an optional value.
  374. ///
  375. /// @param parent A data element holding full option data configuration.
  376. ///
  377. /// @return Option code, possibly unspecified.
  378. /// @throw DhcpConfigError if option code is invalid.
  379. util::OptionalValue<uint32_t>
  380. extractCode(data::ConstElementPtr parent) const;
  381. /// @brief Retrieves parsed option name as an optional value.
  382. ///
  383. /// @param parent A data element holding full option data configuration.
  384. ///
  385. /// @return Option name, possibly unspecified.
  386. /// @throw DhcpConfigError if option name is invalid.
  387. util::OptionalValue<std::string>
  388. extractName(data::ConstElementPtr parent) const;
  389. /// @brief Retrieves csv-format parameter as an optional value.
  390. ///
  391. /// @return Value of the csv-format parameter, possibly unspecified.
  392. util::OptionalValue<bool> extractCSVFormat(data::ConstElementPtr parent) const;
  393. /// @brief Retrieves option data as a string.
  394. ///
  395. /// @param parent A data element holding full option data configuration.
  396. /// @return Option data as a string. It will return empty string if
  397. /// option data is unspecified.
  398. std::string extractData(data::ConstElementPtr parent) const;
  399. /// @brief Retrieves option space name.
  400. ///
  401. /// If option space name is not specified in the configuration the
  402. /// 'dhcp4' or 'dhcp6' option space name is returned, depending on
  403. /// the universe specified in the parser context.
  404. ///
  405. /// @param parent A data element holding full option data configuration.
  406. ///
  407. /// @return Option space name.
  408. std::string extractSpace(data::ConstElementPtr parent) const;
  409. /// @brief Address family: @c AF_INET or @c AF_INET6.
  410. uint16_t address_family_;
  411. };
  412. /// @brief Parser for option data values within a subnet.
  413. ///
  414. /// This parser iterates over all entries that define options
  415. /// data for a particular subnet and creates a collection of options.
  416. /// If parsing is successful, all these options are added to the Subnet
  417. /// object.
  418. class OptionDataListParser : public isc::data::SimpleParser {
  419. public:
  420. /// @brief Constructor.
  421. ///
  422. /// @param address_family Address family: @c AF_INET or AF_INET6
  423. explicit OptionDataListParser(const uint16_t address_family);
  424. /// @brief Parses a list of options, instantiates them and stores in cfg
  425. ///
  426. /// This method expects to get a list of options in option_data_list,
  427. /// iterates over them, creates option objects, wraps them with
  428. /// option descriptor and stores in specified cfg.
  429. ///
  430. /// @param cfg created options will be stored here
  431. /// @param option_data_list configuration that describes the options
  432. void parse(const CfgOptionPtr& cfg,
  433. isc::data::ConstElementPtr option_data_list);
  434. private:
  435. /// @brief Address family: @c AF_INET or @c AF_INET6
  436. uint16_t address_family_;
  437. };
  438. typedef std::pair<isc::dhcp::OptionDefinitionPtr, std::string> OptionDefinitionTuple;
  439. /// @brief Parser for a single option definition.
  440. ///
  441. /// This parser creates an instance of a single option definition.
  442. class OptionDefParser : public isc::data::SimpleParser {
  443. public:
  444. /// @brief Parses an entry that describes single option definition.
  445. ///
  446. /// @param option_def a configuration entry to be parsed.
  447. /// @return tuple (option definition, option space) of the parsed structure
  448. ///
  449. /// @throw DhcpConfigError if parsing was unsuccessful.
  450. OptionDefinitionTuple
  451. parse(isc::data::ConstElementPtr option_def);
  452. };
  453. /// @brief Parser for a list of option definitions.
  454. ///
  455. /// This parser iterates over all configuration entries that define
  456. /// option definitions and creates instances of these definitions.
  457. /// If the parsing is successful, the collection of created definitions
  458. /// is put into the provided storage.
  459. class OptionDefListParser : public isc::data::SimpleParser {
  460. public:
  461. /// @brief Parses a list of option definitions, create them and store in cfg
  462. ///
  463. /// This method iterates over def_list, which is a JSON list of option definitions,
  464. /// then creates corresponding option definitions and store them in the
  465. /// configuration structure.
  466. ///
  467. /// @param def_list JSON list describing option definitions
  468. /// @param cfg parsed option definitions will be stored here
  469. void parse(CfgOptionDefPtr cfg, isc::data::ConstElementPtr def_list);
  470. };
  471. /// @brief a collection of pools
  472. ///
  473. /// That type is used as intermediate storage, when pools are parsed, but there is
  474. /// no subnet object created yet to store them.
  475. typedef std::vector<PoolPtr> PoolStorage;
  476. typedef boost::shared_ptr<PoolStorage> PoolStoragePtr;
  477. /// @brief parser for a single pool definition
  478. ///
  479. /// This abstract parser handles pool definitions, i.e. a list of entries of one
  480. /// of two syntaxes: min-max and prefix/len. Pool objects are created
  481. /// and stored in chosen PoolStorage container.
  482. ///
  483. /// It is useful for parsing Dhcp<4/6>/subnet<4/6>[X]/pools[X] structure.
  484. class PoolParser : public isc::data::SimpleParser {
  485. public:
  486. /// @brief destructor.
  487. virtual ~PoolParser() {
  488. }
  489. /// @brief parses the actual structure
  490. ///
  491. /// This method parses the actual list of interfaces.
  492. /// No validation is done at this stage, everything is interpreted as
  493. /// interface name.
  494. /// @param pools is the storage in which to store the parsed pool
  495. /// @param pool_structure a single entry on a list of pools
  496. /// @param address_family AF_INET (for DHCPv4) or AF_INET6 (for DHCPv6).
  497. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  498. virtual void parse(PoolStoragePtr pools,
  499. isc::data::ConstElementPtr pool_structure,
  500. const uint16_t address_family);
  501. protected:
  502. /// @brief Creates a Pool object given a IPv4 prefix and the prefix length.
  503. ///
  504. /// @param addr is the IP prefix of the pool.
  505. /// @param len is the prefix length.
  506. /// @param ptype is the type of pool to create.
  507. /// @return returns a PoolPtr to the new Pool object.
  508. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &addr, uint32_t len,
  509. int32_t ptype = 0) = 0;
  510. /// @brief Creates a Pool object given starting and ending IP addresses.
  511. ///
  512. /// @param min is the first IP address in the pool.
  513. /// @param max is the last IP address in the pool.
  514. /// @param ptype is the type of pool to create (not used by all derivations)
  515. /// @return returns a PoolPtr to the new Pool object.
  516. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &min,
  517. isc::asiolink::IOAddress &max,
  518. int32_t ptype = 0) = 0;
  519. };
  520. /// @brief Parser for a list of pools
  521. ///
  522. /// This parser parses a list pools. Each element on that list gets its own
  523. /// parser, created with poolParserMaker() method. That method must be specified
  524. /// for each protocol family (v4 or v6) separately.
  525. class PoolsListParser : public isc::data::SimpleParser {
  526. public:
  527. /// @brief destructor.
  528. virtual ~PoolsListParser() {
  529. }
  530. /// @brief parses the actual structure
  531. ///
  532. /// This method parses the actual list of pools.
  533. ///
  534. /// @param pools is the storage in which to store the parsed pools.
  535. /// @param pools_list a list of pool structures
  536. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  537. virtual void parse(PoolStoragePtr pools,
  538. isc::data::ConstElementPtr pools_list) = 0;
  539. };
  540. /// @brief parser for additional relay information
  541. ///
  542. /// This concrete parser handles RelayInfo structure definitions.
  543. /// So far that structure holds only relay IP (v4 or v6) address, but it
  544. /// is expected that the number of parameters will increase over time.
  545. ///
  546. /// It is useful for parsing Dhcp<4/6>/subnet<4/6>[x]/relay parameters.
  547. class RelayInfoParser : public isc::data::SimpleParser {
  548. public:
  549. /// @brief constructor
  550. /// @param family specifies protocol family (IPv4 or IPv6)
  551. explicit RelayInfoParser(const isc::dhcp::Option::Universe& family);
  552. /// @brief parses the actual relay parameters
  553. ///
  554. /// The elements currently supported are:
  555. /// -# ip-address
  556. ///
  557. /// @param cfg configuration will be stored here
  558. /// @param relay_info JSON structure holding relay parameters to parse
  559. void parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg,
  560. isc::data::ConstElementPtr relay_info);
  561. private:
  562. /// @brief Returns a value converted to IOAddress
  563. ///
  564. /// Instantiation of getAndConvert() to IOAddress
  565. ///
  566. /// @param scope specified parameter will be extracted from this scope
  567. /// @param name name of the parameter
  568. /// @return an IOAddress value
  569. isc::asiolink::IOAddress
  570. getIOAddress(isc::data::ConstElementPtr scope, const std::string& name);
  571. /// Protocol family (IPv4 or IPv6)
  572. Option::Universe family_;
  573. };
  574. /// @brief this class parses a single subnet
  575. ///
  576. /// There are dedicated @ref Subnet4ConfigParser and @ref Subnet6ConfigParser
  577. /// classes. They provide specialized parse() methods that return Subnet4Ptr
  578. /// or Subnet6Ptr.
  579. ///
  580. /// This class parses the whole subnet definition. This class attempts to
  581. /// unify the code between v4 and v6 as much as possible. As a result, the flow
  582. /// is somewhat complex and it looks as follows:
  583. ///
  584. /// ------- Base class
  585. /// /
  586. /// | /----- Derived class
  587. /// 1. * SubnetXConfigParser::parse() is called.
  588. /// 2. * SubnetConfigParser::parse() is called.
  589. /// 3. * SubnetConfigParser::createSubnet() is called.
  590. /// 4. * SubnetXConfigParser::initSubnet() is called (Subnet4 or Subnet6 is
  591. /// instantiated here and family specific parameters are set)
  592. /// 5. Control returns to createSubnet() (step 3) and common parameters
  593. /// are set.
  594. class SubnetConfigParser : public isc::data::SimpleParser {
  595. public:
  596. /// @brief constructor
  597. ///
  598. /// @param family address family: @c AF_INET or @c AF_INET6
  599. explicit SubnetConfigParser(uint16_t family);
  600. /// @brief virtual destructor (does nothing)
  601. virtual ~SubnetConfigParser() { }
  602. protected:
  603. /// @brief parses a subnet description and returns Subnet{4,6} structure
  604. ///
  605. /// This method is called from specialized (Subnet4ConfigParser or
  606. /// Subnet6ConfigParser) classes.
  607. ///
  608. /// @param subnet pointer to the content of subnet definition
  609. /// @return a pointer to newly created subnet
  610. ///
  611. /// @throw isc::DhcpConfigError if subnet configuration parsing failed.
  612. SubnetPtr parse(isc::data::ConstElementPtr subnet);
  613. /// @brief Instantiates the subnet based on a given IP prefix and prefix
  614. /// length.
  615. ///
  616. /// @param params configuration parameters for that subnet
  617. /// @param addr is the IP prefix of the subnet.
  618. /// @param len is the prefix length
  619. virtual void initSubnet(isc::data::ConstElementPtr params,
  620. isc::asiolink::IOAddress addr, uint8_t len) = 0;
  621. /// @brief Attempts to convert text representation to HRMode enum.
  622. ///
  623. /// Allowed values are "disabled", "off" (alias for disabled),
  624. /// "out-of-pool" and "all". See Subnet::HRMode for their exact meaning.
  625. ///
  626. /// @param txt Host Reservation mode in the textual form.
  627. ///
  628. /// @throw BadValue if the text cannot be converted.
  629. ///
  630. /// @return one of allowed HRMode values
  631. static Subnet::HRMode hrModeFromText(const std::string& txt);
  632. private:
  633. /// @brief Create a new subnet using a data from child parsers.
  634. ///
  635. /// @param data Element map that describes the subnet
  636. /// @throw isc::dhcp::DhcpConfigError if subnet configuration parsing
  637. /// failed.
  638. void createSubnet(isc::data::ConstElementPtr data);
  639. protected:
  640. /// Storage for pools belonging to this subnet.
  641. PoolStoragePtr pools_;
  642. /// Pointer to the created subnet object.
  643. isc::dhcp::SubnetPtr subnet_;
  644. /// @brief Address family: @c AF_INET or @c AF_INET6
  645. uint16_t address_family_;
  646. /// Pointer to relay information
  647. isc::dhcp::Subnet::RelayInfoPtr relay_info_;
  648. /// Pointer to the options configuration.
  649. CfgOptionPtr options_;
  650. };
  651. /// @brief Parser for D2ClientConfig
  652. ///
  653. /// This class parses the configuration element "dhcp-ddns" common to the
  654. /// config files for both dhcp4 and dhcp6. It creates an instance of a
  655. /// D2ClientConfig.
  656. class D2ClientConfigParser : public isc::data::SimpleParser {
  657. public:
  658. /// @brief Parses a given dhcp-ddns element into D2ClientConfig.
  659. ///
  660. /// @param d2_client_cfg is the "dhcp-ddns" configuration to parse
  661. ///
  662. /// The elements currently supported are (see isc::dhcp::D2ClientConfig
  663. /// for details on each):
  664. /// -# enable-updates
  665. /// -# qualifying-suffix
  666. /// -# server-ip
  667. /// -# server-port
  668. /// -# sender-ip
  669. /// -# sender-port
  670. /// -# max-queue-size
  671. /// -# ncr-protocol
  672. /// -# ncr-format
  673. /// -# always-include-fqdn
  674. /// -# override-no-update
  675. /// -# override-client-update
  676. /// -# replace-client-name
  677. /// -# generated-prefix
  678. ///
  679. /// @return returns a pointer to newly created D2ClientConfig.
  680. D2ClientConfigPtr parse(isc::data::ConstElementPtr d2_client_cfg);
  681. /// @brief Defaults for the D2 client configuration.
  682. static const isc::data::SimpleDefaults D2_CLIENT_CONFIG_DEFAULTS;
  683. /// @brief Sets all defaults for D2 client configuration.
  684. ///
  685. /// This method sets defaults value. It must not be called
  686. /// before the short cut disabled updates condition was checked.
  687. ///
  688. /// @param d2_config d2 client configuration (will be const cast
  689. // to ElementPtr)
  690. /// @return number of parameters inserted
  691. static size_t setAllDefaults(isc::data::ConstElementPtr d2_config);
  692. private:
  693. /// @brief Returns a value converted to IOAddress
  694. ///
  695. /// Instantiation of getAndConvert() to IOAddress
  696. ///
  697. /// @param scope specified parameter will be extracted from this scope
  698. /// @param name name of the parameter
  699. /// @return an IOAddress value
  700. isc::asiolink::IOAddress
  701. getIOAddress(isc::data::ConstElementPtr scope, const std::string& name);
  702. /// @brief Returns a value converted to NameChangeProtocol
  703. ///
  704. /// Instantiation of getAndConvert() to NameChangeProtocol
  705. ///
  706. /// @param scope specified parameter will be extracted from this scope
  707. /// @param name name of the parameter
  708. /// @return a NameChangeProtocol value
  709. dhcp_ddns::NameChangeProtocol
  710. getProtocol(isc::data::ConstElementPtr scope, const std::string& name);
  711. /// @brief Returns a value converted to NameChangeFormat
  712. ///
  713. /// Instantiation of getAndConvert() to NameChangeFormat
  714. ///
  715. /// @param scope specified parameter will be extracted from this scope
  716. /// @param name name of the parameter
  717. /// @return a NameChangeFormat value
  718. dhcp_ddns::NameChangeFormat
  719. getFormat(isc::data::ConstElementPtr scope, const std::string& name);
  720. /// @brief Returns a value converted to ReplaceClientNameMode
  721. ///
  722. /// Instantiation of getAndConvert() to ReplaceClientNameMode
  723. ///
  724. /// @param scope specified parameter will be extracted from this scope
  725. /// @param name name of the parameter
  726. /// @return a NameChangeFormat value
  727. D2ClientConfig::ReplaceClientNameMode
  728. getMode(isc::data::ConstElementPtr scope, const std::string& name);
  729. };
  730. }; // end of isc::dhcp namespace
  731. }; // end of isc namespace
  732. #endif // DHCP_PARSERS_H