dhcp_parsers.h 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 <hooks/libinfo.h>
  22. #include <exceptions/exceptions.h>
  23. #include <util/optional_value.h>
  24. #include <boost/shared_ptr.hpp>
  25. #include <stdint.h>
  26. #include <string>
  27. #include <vector>
  28. namespace isc {
  29. namespace dhcp {
  30. /// Collection of containers holding option spaces. Each container within
  31. /// a particular option space holds so-called option descriptors.
  32. typedef OptionSpaceContainer<OptionContainer, OptionDescriptor,
  33. std::string> OptionStorage;
  34. /// @brief Shared pointer to option storage.
  35. typedef boost::shared_ptr<OptionStorage> OptionStoragePtr;
  36. /// @brief A template class that stores named elements of a given data type.
  37. ///
  38. /// This template class is provides data value storage for configuration
  39. /// parameters of a given data type. The values are stored by parameter name
  40. /// and as instances of type "ValueType". Each value held in the storage has
  41. /// a corresponding position within a configuration string (file) specified
  42. /// as a: file name, line number and position within the line. The position
  43. /// information is used for logging when the particular configuration value
  44. /// causes a configuration error.
  45. ///
  46. /// @tparam ValueType is the data type of the elements to store.
  47. template<typename ValueType>
  48. class ValueStorage {
  49. public:
  50. /// @brief Stores the the parameter, its value and the position in the
  51. /// store.
  52. ///
  53. /// If the parameter does not exist in the store, then it will be added,
  54. /// otherwise its data value and the position will be updated with the
  55. /// given values.
  56. ///
  57. /// @param name is the name of the parameter to store.
  58. /// @param value is the data value to store.
  59. /// @param position is the position of the data element within a
  60. /// configuration string (file).
  61. void setParam(const std::string& name, const ValueType& value,
  62. const data::Element::Position& position) {
  63. values_[name] = value;
  64. positions_[name] = position;
  65. }
  66. /// @brief Returns the data value for the given parameter.
  67. ///
  68. /// Finds and returns the data value for the given parameter.
  69. /// @param name is the name of the parameter for which the data
  70. /// value is desired.
  71. ///
  72. /// @return The parameter's data value of type @c ValueType.
  73. /// @throw DhcpConfigError if the parameter is not found.
  74. ValueType getParam(const std::string& name) const {
  75. typename std::map<std::string, ValueType>::const_iterator param
  76. = values_.find(name);
  77. if (param == values_.end()) {
  78. isc_throw(DhcpConfigError, "Missing parameter '"
  79. << name << "'");
  80. }
  81. return (param->second);
  82. }
  83. /// @brief Returns position of the data element in the configuration string.
  84. ///
  85. /// The returned object comprises file name, line number and the position
  86. /// within the particular line of the configuration string where the data
  87. /// element holding a particular value is located.
  88. ///
  89. /// @param name is the name of the parameter which position is desired.
  90. /// @param parent Pointer to a data element which position should be
  91. /// returned when position of the specified parameter is not found.
  92. ///
  93. /// @return Position of the data element or the position holding empty
  94. /// file name and two zeros if the position hasn't been specified for the
  95. /// particular value.
  96. const data::Element::Position&
  97. getPosition(const std::string& name, const data::ConstElementPtr parent =
  98. data::ConstElementPtr()) const {
  99. typename std::map<std::string, data::Element::Position>::const_iterator
  100. pos = positions_.find(name);
  101. if (pos == positions_.end()) {
  102. return (parent ? parent->getPosition() :
  103. data::Element::ZERO_POSITION());
  104. }
  105. return (pos->second);
  106. }
  107. /// @brief Returns the data value for an optional parameter.
  108. ///
  109. /// Finds and returns the data value for the given parameter or
  110. /// a supplied default value if it is not found.
  111. ///
  112. /// @param name is the name of the parameter for which the data
  113. /// value is desired.
  114. /// @param default_value value to use the default
  115. ///
  116. /// @return The parameter's data value of type @c ValueType.
  117. ValueType getOptionalParam(const std::string& name,
  118. const ValueType& default_value) const {
  119. typename std::map<std::string, ValueType>::const_iterator param
  120. = values_.find(name);
  121. if (param == values_.end()) {
  122. return (default_value);
  123. }
  124. return (param->second);
  125. }
  126. /// @brief Remove the parameter from the store.
  127. ///
  128. /// Deletes the entry for the given parameter from the store if it
  129. /// exists.
  130. ///
  131. /// @param name is the name of the parameter to delete.
  132. void delParam(const std::string& name) {
  133. values_.erase(name);
  134. positions_.erase(name);
  135. }
  136. /// @brief Deletes all of the entries from the store.
  137. ///
  138. void clear() {
  139. values_.clear();
  140. positions_.clear();
  141. }
  142. private:
  143. /// @brief An std::map of the data values, keyed by parameter names.
  144. std::map<std::string, ValueType> values_;
  145. /// @brief An std::map holding positions of the data elements in the
  146. /// configuration, which values are held in @c values_.
  147. ///
  148. /// The position is used for logging, when the particular value
  149. /// causes a configuration error.
  150. std::map<std::string, data::Element::Position> positions_;
  151. };
  152. /// @brief a collection of elements that store uint32 values
  153. typedef ValueStorage<uint32_t> Uint32Storage;
  154. typedef boost::shared_ptr<Uint32Storage> Uint32StoragePtr;
  155. /// @brief a collection of elements that store string values
  156. typedef ValueStorage<std::string> StringStorage;
  157. typedef boost::shared_ptr<StringStorage> StringStoragePtr;
  158. /// @brief Storage for parsed boolean values.
  159. typedef ValueStorage<bool> BooleanStorage;
  160. typedef boost::shared_ptr<BooleanStorage> BooleanStoragePtr;
  161. /// @brief Container for the current parsing context. It provides a
  162. /// single enclosure for the storage of configuration parameters,
  163. /// options, option definitions, and other context specific information
  164. /// that needs to be accessible throughout the parsing and parsing
  165. /// constructs.
  166. class ParserContext {
  167. public:
  168. /// @brief Constructor
  169. ///
  170. /// @param universe is the Option::Universe value of this
  171. /// context.
  172. ParserContext(Option::Universe universe);
  173. /// @brief Copy constructor
  174. ParserContext(const ParserContext& rhs);
  175. /// @brief Storage for boolean parameters.
  176. BooleanStoragePtr boolean_values_;
  177. /// @brief Storage for uint32 parameters.
  178. Uint32StoragePtr uint32_values_;
  179. /// @brief Storage for string parameters.
  180. StringStoragePtr string_values_;
  181. /// @brief Hooks libraries pointer.
  182. ///
  183. /// The hooks libraries information is a vector of strings, each containing
  184. /// the name of a library. Hooks libraries should only be reloaded if the
  185. /// list of names has changed, so the list of current DHCP parameters
  186. /// (in isc::dhcp::CfgMgr) contains an indication as to whether the list has
  187. /// altered. This indication is implemented by storing a pointer to the
  188. /// list of library names which is cleared when the libraries are loaded.
  189. /// So either the pointer is null (meaning don't reload the libraries and
  190. /// the list of current names can be obtained from the HooksManager) or it
  191. /// is non-null (this is the new list of names, reload the libraries when
  192. /// possible).
  193. isc::hooks::HookLibsCollectionPtr hooks_libraries_;
  194. /// @brief The parsing universe of this context.
  195. Option::Universe universe_;
  196. /// @brief Assignment operator
  197. ParserContext& operator=(const ParserContext& rhs);
  198. /// @brief Copy the context fields.
  199. ///
  200. /// This class method initializes the context data by copying the data
  201. /// stored in the context instance provided as an argument. Note that
  202. /// this function will also handle copying the NULL pointers.
  203. ///
  204. /// @param ctx context to be copied.
  205. void copyContext(const ParserContext& ctx);
  206. template<typename T>
  207. void copyContextPointer(const boost::shared_ptr<T>& source_ptr,
  208. boost::shared_ptr<T>& dest_ptr);
  209. };
  210. /// @brief Pointer to various parser context.
  211. typedef boost::shared_ptr<ParserContext> ParserContextPtr;
  212. /// @brief Simple data-type parser template class
  213. ///
  214. /// This is the template class for simple data-type parsers. It supports
  215. /// parsing a configuration parameter with specific data-type for its
  216. /// possible values. It provides a common constructor, commit, and templated
  217. /// data storage. The "build" method implementation must be provided by a
  218. /// declaring type.
  219. /// @param ValueType is the data type of the configuration parameter value
  220. /// the parser should handle.
  221. template<typename ValueType>
  222. class ValueParser : public DhcpConfigParser {
  223. public:
  224. /// @brief Constructor.
  225. ///
  226. /// @param param_name name of the parameter.
  227. /// @param storage is a pointer to the storage container where the parsed
  228. /// value be stored upon commit.
  229. /// @throw isc::dhcp::DhcpConfigError if a provided parameter's
  230. /// name is empty.
  231. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  232. ValueParser(const std::string& param_name,
  233. boost::shared_ptr<ValueStorage<ValueType> > storage)
  234. : storage_(storage), param_name_(param_name), value_(), pos_() {
  235. // Empty parameter name is invalid.
  236. if (param_name_.empty()) {
  237. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  238. << "empty parameter name provided");
  239. }
  240. // Null storage is invalid.
  241. if (!storage_) {
  242. isc_throw(isc::dhcp::DhcpConfigError, "parser logic error:"
  243. << "storage may not be NULL");
  244. }
  245. }
  246. /// @brief Parse a given element into a value of type @c ValueType
  247. ///
  248. /// @param value a value to be parsed.
  249. ///
  250. /// @throw isc::BadValue Typically the implementing type will throw
  251. /// a BadValue exception when given an invalid Element to parse.
  252. void build(isc::data::ConstElementPtr value);
  253. /// @brief Put a parsed value to the storage.
  254. void commit() {
  255. // If a given parameter already exists in the storage we override
  256. // its value. If it doesn't we insert a new element.
  257. storage_->setParam(param_name_, value_, pos_);
  258. }
  259. private:
  260. /// @brief Performs operations common for all specializations of the
  261. /// @c build function.
  262. ///
  263. /// This method should be called by all specializations of the @c build
  264. /// method.
  265. ///
  266. /// @param value a value being parsed.
  267. void buildCommon(isc::data::ConstElementPtr value) {
  268. // Remember position of the data element.
  269. pos_ = value->getPosition();
  270. }
  271. /// Pointer to the storage where committed value is stored.
  272. boost::shared_ptr<ValueStorage<ValueType> > storage_;
  273. /// Name of the parameter which value is parsed with this parser.
  274. std::string param_name_;
  275. /// Parsed value.
  276. ValueType value_;
  277. data::Element::Position pos_;
  278. };
  279. /// @brief typedefs for simple data type parsers
  280. typedef ValueParser<bool> BooleanParser;
  281. typedef ValueParser<uint32_t> Uint32Parser;
  282. typedef ValueParser<std::string> StringParser;
  283. /// @brief a dummy configuration parser
  284. ///
  285. /// It is a debugging parser. It does not configure anything,
  286. /// will accept any configuration and will just print it out
  287. /// on commit. Useful for debugging existing configurations and
  288. /// adding new ones.
  289. class DebugParser : public DhcpConfigParser {
  290. public:
  291. /// @brief Constructor
  292. ///
  293. /// See @ref DhcpConfigParser class for details.
  294. ///
  295. /// @param param_name name of the parsed parameter
  296. DebugParser(const std::string& param_name);
  297. /// @brief builds parameter value
  298. ///
  299. /// See @ref DhcpConfigParser class for details.
  300. ///
  301. /// @param new_config pointer to the new configuration
  302. virtual void build(isc::data::ConstElementPtr new_config);
  303. /// @brief pretends to apply the configuration
  304. ///
  305. /// This is a method required by base class. It pretends to apply the
  306. /// configuration, but in fact it only prints the parameter out.
  307. ///
  308. /// See @ref DhcpConfigParser class for details.
  309. virtual void commit();
  310. private:
  311. /// name of the parsed parameter
  312. std::string param_name_;
  313. /// pointer to the actual value of the parameter
  314. isc::data::ConstElementPtr value_;
  315. };
  316. /// @brief parser for MAC/hardware acquisition sources
  317. ///
  318. /// This parser handles Dhcp6/mac-sources entry.
  319. /// It contains a list of MAC/hardware acquisition source, i.e. methods how
  320. /// MAC address can possibly by obtained in DHCPv6. For a currently supported
  321. /// methods, see @ref isc::dhcp::Pkt::getMAC.
  322. class MACSourcesListConfigParser : public isc::data::SimpleParser {
  323. public:
  324. /// @brief parses parameters value
  325. ///
  326. /// Parses configuration entry (list of sources) and adds each element
  327. /// to the sources list.
  328. ///
  329. /// @param value pointer to the content of parsed values
  330. void parse(CfgMACSource& mac_sources, isc::data::ConstElementPtr value);
  331. };
  332. /// @brief Parser for the control-socket structure
  333. ///
  334. /// It does not parse anything, simply stores the element in
  335. /// the staging config.
  336. class ControlSocketParser : public isc::data::SimpleParser {
  337. public:
  338. /// @brief "Parses" control-socket structure
  339. ///
  340. /// Since the SrvConfig structure takes the socket definition
  341. /// as ConstElementPtr, there's really nothing to parse here.
  342. /// It only does basic sanity checks and throws DhcpConfigError
  343. /// if the value is null or is not a map.
  344. ///
  345. /// @param srv_cfg parsed values will be stored here
  346. /// @param value pointer to the content of parsed values
  347. void parse(SrvConfig& srv_cfg, isc::data::ConstElementPtr value);
  348. };
  349. /// @brief Parser for hooks library list
  350. ///
  351. /// This parser handles the list of hooks libraries. This is an optional list,
  352. /// which may be empty.
  353. ///
  354. /// However, the parser does more than just check the list of library names.
  355. /// It does two other things:
  356. ///
  357. /// -# The problem faced with the hooks libraries is that we wish to avoid
  358. /// reloading the libraries if they have not changed. (This would cause the
  359. /// "unload" and "load" methods to run. Although libraries should be written
  360. /// to cope with this, it is feasible that such an action may be costly in
  361. /// terms of time and resources, or may cause side effects such as clearing
  362. /// an internal cache.) To this end, the parser also checks the list against
  363. /// the list of libraries current loaded and notes if there are changes.
  364. /// -# If there are, the parser validates the libraries; it opens them and
  365. /// checks that the "version" function exists and returns the correct value.
  366. ///
  367. /// Only if the library list has changed and the libraries are valid will the
  368. /// change be applied.
  369. class HooksLibrariesParser : public DhcpConfigParser {
  370. public:
  371. /// @brief Constructor
  372. ///
  373. /// As this is a dedicated parser, it must be used to parse
  374. /// "hooks-libraries" parameter only. All other types will throw exception.
  375. ///
  376. /// @param param_name name of the configuration parameter being parsed.
  377. ///
  378. /// @throw BadValue if supplied parameter name is not "hooks-libraries"
  379. HooksLibrariesParser(const std::string& param_name);
  380. /// @brief Parses parameters value
  381. ///
  382. /// Parses configuration entry (list of parameters) and adds each element
  383. /// to the hooks libraries list. The method also checks whether the
  384. /// list of libraries is the same as that already loaded. If not, it
  385. /// checks each of the libraries in the list for validity (they exist and
  386. /// have a "version" function that returns the correct value).
  387. ///
  388. /// The syntax for specifying hooks libraries allow for library-specific
  389. /// parameters to be specified along with the library, e.g.
  390. ///
  391. /// @code
  392. /// "hooks-libraries": [
  393. /// {
  394. /// "library": "hook-lib-1.so",
  395. /// "parameters": {
  396. /// "alpha": "a string",
  397. /// "beta": 42
  398. /// }
  399. /// },
  400. /// :
  401. /// ]
  402. /// @endcode
  403. ///
  404. /// As Kea has not yet implemented parameters, the parsing code only checks
  405. /// that:
  406. ///
  407. /// -# Each element in the hooks-libraries list is a map
  408. /// -# The map contains an element "library" whose value is a string: all
  409. /// other elements in the map are ignored.
  410. ///
  411. /// @param value pointer to the content of parsed values
  412. virtual void build(isc::data::ConstElementPtr value);
  413. /// @brief Commits hooks libraries data
  414. ///
  415. /// Providing that the specified libraries are valid and are different
  416. /// to those already loaded, this method loads the new set of libraries
  417. /// (and unloads the existing set).
  418. virtual void commit();
  419. /// @brief Returns list of parsed libraries
  420. ///
  421. /// Principally for testing, this returns the list of libraries as well as
  422. /// an indication as to whether the list is different from the list of
  423. /// libraries already loaded.
  424. ///
  425. /// @param [out] libraries List of libraries that were specified in the
  426. /// new configuration.
  427. /// @param [out] changed true if the list is different from that currently
  428. /// loaded.
  429. void getLibraries(isc::hooks::HookLibsCollection& libraries, bool& changed);
  430. private:
  431. /// List of hooks libraries with their configuration parameters
  432. isc::hooks::HookLibsCollection libraries_;
  433. /// Indicator flagging that the list of libraries has changed.
  434. bool changed_;
  435. };
  436. /// @brief Parser for option data value.
  437. ///
  438. /// This parser parses configuration entries that specify value of
  439. /// a single option. These entries include option name, option code
  440. /// and data carried by the option. The option data can be specified
  441. /// in one of the two available formats: binary value represented as
  442. /// a string of hexadecimal digits or a list of comma separated values.
  443. /// The format being used is controlled by csv-format configuration
  444. /// parameter. When setting this value to True, the latter format is
  445. /// used. The subsequent values in the CSV format apply to relevant
  446. /// option data fields in the configured option. For example the
  447. /// configuration: "data" : "192.168.2.0, 56, hello world" can be
  448. /// used to set values for the option comprising IPv4 address,
  449. /// integer and string data field. Note that order matters. If the
  450. /// order of values does not match the order of data fields within
  451. /// an option the configuration will not be accepted. If parsing
  452. /// is successful then an instance of an option is created and
  453. /// added to the storage provided by the calling class.
  454. class OptionDataParser : public isc::data::SimpleParser {
  455. public:
  456. /// @brief Constructor.
  457. ///
  458. /// @param address_family Address family: @c AF_INET or @c AF_INET6.
  459. OptionDataParser(const uint16_t address_family);
  460. /// @brief Parses ElementPtr containing option definition
  461. ///
  462. /// This method parses ElementPtr containing the option definition,
  463. /// instantiates the option for it and then returns a pair
  464. /// of option descriptor (that holds that new option) and
  465. /// a string that specifies the option space.
  466. ///
  467. /// Note: ElementPtr is expected to contain all fields. If your
  468. /// ElementPtr does not have them, please use
  469. /// @ref isc::data::SimpleParser::setDefaults to fill the missing fields
  470. /// with default values.
  471. ///
  472. /// @param single_option ElementPtr containing option definition
  473. /// @return Option object wrapped in option description and an option
  474. /// space
  475. std::pair<OptionDescriptor, std::string>
  476. parse(isc::data::ConstElementPtr single_option);
  477. private:
  478. /// @brief Finds an option definition within an option space
  479. ///
  480. /// Given an option space and an option code, find the corresponding
  481. /// option definition within the option definition storage.
  482. ///
  483. /// @param option_space name of the parameter option space
  484. /// @param search_key an option code or name to be used to lookup the
  485. /// option definition.
  486. /// @tparam A numeric type for searching using an option code or the
  487. /// string for searching using the option name.
  488. ///
  489. /// @return OptionDefinitionPtr of the option definition or an
  490. /// empty OptionDefinitionPtr if not found.
  491. /// @throw DhcpConfigError if the option space requested is not valid
  492. /// for this server.
  493. template<typename SearchKey>
  494. OptionDefinitionPtr findOptionDefinition(const std::string& option_space,
  495. const SearchKey& search_key) const;
  496. /// @brief Create option instance.
  497. ///
  498. /// Creates an instance of an option and adds it to the provided
  499. /// options storage. If the option data parsed by \ref build function
  500. /// are invalid or insufficient this function emits an exception.
  501. ///
  502. /// @param option_data An element holding data for a single option being
  503. /// created.
  504. ///
  505. /// @return created option descriptor
  506. ///
  507. /// @throw DhcpConfigError if parameters provided in the configuration
  508. /// are invalid.
  509. std::pair<OptionDescriptor, std::string>
  510. createOption(isc::data::ConstElementPtr option_data);
  511. /// @brief Retrieves parsed option code as an optional value.
  512. ///
  513. /// @param parent A data element holding full option data configuration.
  514. ///
  515. /// @return Option code, possibly unspecified.
  516. /// @throw DhcpConfigError if option code is invalid.
  517. util::OptionalValue<uint32_t>
  518. extractCode(data::ConstElementPtr parent) const;
  519. /// @brief Retrieves parsed option name as an optional value.
  520. ///
  521. /// @param parent A data element holding full option data configuration.
  522. ///
  523. /// @return Option name, possibly unspecified.
  524. /// @throw DhcpConfigError if option name is invalid.
  525. util::OptionalValue<std::string>
  526. extractName(data::ConstElementPtr parent) const;
  527. /// @brief Retrieves csv-format parameter as an optional value.
  528. ///
  529. /// @return Value of the csv-format parameter, possibly unspecified.
  530. util::OptionalValue<bool> extractCSVFormat(data::ConstElementPtr parent) const;
  531. /// @brief Retrieves option data as a string.
  532. ///
  533. /// @param parent A data element holding full option data configuration.
  534. /// @return Option data as a string. It will return empty string if
  535. /// option data is unspecified.
  536. std::string extractData(data::ConstElementPtr parent) const;
  537. /// @brief Retrieves option space name.
  538. ///
  539. /// If option space name is not specified in the configuration the
  540. /// 'dhcp4' or 'dhcp6' option space name is returned, depending on
  541. /// the universe specified in the parser context.
  542. ///
  543. /// @param parent A data element holding full option data configuration.
  544. ///
  545. /// @return Option space name.
  546. std::string extractSpace(data::ConstElementPtr parent) const;
  547. /// @brief Address family: @c AF_INET or @c AF_INET6.
  548. uint16_t address_family_;
  549. };
  550. ///@brief Function pointer for OptionDataParser factory methods
  551. typedef OptionDataParser *OptionDataParserFactory(const std::string&,
  552. OptionStoragePtr options, ParserContextPtr global_context);
  553. /// @brief Parser for option data values within a subnet.
  554. ///
  555. /// This parser iterates over all entries that define options
  556. /// data for a particular subnet and creates a collection of options.
  557. /// If parsing is successful, all these options are added to the Subnet
  558. /// object.
  559. class OptionDataListParser : public isc::data::SimpleParser {
  560. public:
  561. /// @brief Constructor.
  562. ///
  563. /// @param address_family Address family: @c AF_INET or AF_INET6
  564. OptionDataListParser(const uint16_t address_family);
  565. /// @brief Parses a list of options, instantiates them and stores in cfg
  566. ///
  567. /// This method expects to get a list of options in option_data_list,
  568. /// iterates over them, creates option objects, wraps them with
  569. /// option descriptor and stores in specified cfg.
  570. ///
  571. /// @param cfg created options will be stored here
  572. /// @param option_data_list configuration that describes the options
  573. void parse(const CfgOptionPtr& cfg,
  574. isc::data::ConstElementPtr option_data_list);
  575. private:
  576. /// @brief Address family: @c AF_INET or @c AF_INET6
  577. uint16_t address_family_;
  578. };
  579. typedef std::pair<isc::dhcp::OptionDefinitionPtr, std::string> OptionDefinitionTuple;
  580. /// @brief Parser for a single option definition.
  581. ///
  582. /// This parser creates an instance of a single option definition.
  583. class OptionDefParser : public isc::data::SimpleParser {
  584. public:
  585. /// @brief Parses an entry that describes single option definition.
  586. ///
  587. /// @param option_def a configuration entry to be parsed.
  588. /// @return tuple (option definition, option space) of the parsed structure
  589. ///
  590. /// @throw DhcpConfigError if parsing was unsuccessful.
  591. OptionDefinitionTuple
  592. parse(isc::data::ConstElementPtr option_def);
  593. };
  594. /// @brief Parser for a list of option definitions.
  595. ///
  596. /// This parser iterates over all configuration entries that define
  597. /// option definitions and creates instances of these definitions.
  598. /// If the parsing is successful, the collection of created definitions
  599. /// is put into the provided storage.
  600. class OptionDefListParser : public isc::data::SimpleParser {
  601. public:
  602. /// @brief Parses a list of option definitions, create them and store in cfg
  603. ///
  604. /// This method iterates over def_list, which is a JSON list of option definitions,
  605. /// then creates corresponding option definitions and store them in the
  606. /// configuration structure.
  607. ///
  608. /// @param def_list JSON list describing option definitions
  609. /// @param cfg parsed option definitions will be stored here
  610. void parse(CfgOptionDefPtr cfg, isc::data::ConstElementPtr def_list);
  611. };
  612. /// @brief a collection of pools
  613. ///
  614. /// That type is used as intermediate storage, when pools are parsed, but there is
  615. /// no subnet object created yet to store them.
  616. typedef std::vector<PoolPtr> PoolStorage;
  617. typedef boost::shared_ptr<PoolStorage> PoolStoragePtr;
  618. /// @brief parser for a single pool definition
  619. ///
  620. /// This abstract parser handles pool definitions, i.e. a list of entries of one
  621. /// of two syntaxes: min-max and prefix/len. Pool objects are created
  622. /// and stored in chosen PoolStorage container.
  623. ///
  624. /// It is useful for parsing Dhcp<4/6>/subnet<4/6>[X]/pools[X] structure.
  625. class PoolParser : public DhcpConfigParser {
  626. public:
  627. /// @brief constructor.
  628. ///
  629. /// @param dummy first argument is ignored, all Parser constructors
  630. /// accept string as first argument.
  631. /// @param pools is the storage in which to store the parsed pool
  632. /// upon "commit".
  633. /// @param address_family AF_INET (for DHCPv4) or AF_INET6 (for DHCPv6).
  634. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  635. PoolParser(const std::string& dummy, PoolStoragePtr pools,
  636. const uint16_t address_family);
  637. /// @brief parses the actual structure
  638. ///
  639. /// This method parses the actual list of interfaces.
  640. /// No validation is done at this stage, everything is interpreted as
  641. /// interface name.
  642. /// @param pool_structure a single entry on a list of pools
  643. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  644. virtual void build(isc::data::ConstElementPtr pool_structure);
  645. /// @brief Stores the parsed values in a storage provided
  646. /// by an upper level parser.
  647. virtual void commit();
  648. protected:
  649. /// @brief Creates a Pool object given a IPv4 prefix and the prefix length.
  650. ///
  651. /// @param addr is the IP prefix of the pool.
  652. /// @param len is the prefix length.
  653. /// @param ptype is the type of pool to create.
  654. /// @return returns a PoolPtr to the new Pool object.
  655. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &addr, uint32_t len,
  656. int32_t ptype=0) = 0;
  657. /// @brief Creates a Pool object given starting and ending IP addresses.
  658. ///
  659. /// @param min is the first IP address in the pool.
  660. /// @param max is the last IP address in the pool.
  661. /// @param ptype is the type of pool to create (not used by all derivations)
  662. /// @return returns a PoolPtr to the new Pool object.
  663. virtual PoolPtr poolMaker(isc::asiolink::IOAddress &min,
  664. isc::asiolink::IOAddress &max, int32_t ptype=0) = 0;
  665. /// @brief pointer to the actual Pools storage
  666. ///
  667. /// That is typically a storage somewhere in Subnet parser
  668. /// (an upper level parser).
  669. PoolStoragePtr pools_;
  670. /// A temporary storage for pools configuration. It is a
  671. /// storage where pools are stored by build function.
  672. PoolStorage local_pools_;
  673. /// A storage for pool specific option values.
  674. CfgOptionPtr options_;
  675. /// @brief Address family: AF_INET (for DHCPv4) or AF_INET6 for DHCPv6.
  676. uint16_t address_family_;
  677. };
  678. /// @brief Parser for a list of pools
  679. ///
  680. /// This parser parses a list pools. Each element on that list gets its own
  681. /// parser, created with poolParserMaker() method. That method must be specified
  682. /// for each protocol family (v4 or v6) separately.
  683. ///
  684. /// This class is not intended to be used directly. Instead, derived classes
  685. /// should implement poolParserMaker() method.
  686. class PoolsListParser : public DhcpConfigParser {
  687. public:
  688. /// @brief constructor.
  689. ///
  690. /// @param dummy first argument is ignored, all Parser constructors
  691. /// accept a string as the first argument.
  692. /// @param pools is the storage in which to store the parsed pool
  693. /// upon "commit".
  694. /// @throw isc::dhcp::DhcpConfigError if storage is null.
  695. PoolsListParser(const std::string& dummy, PoolStoragePtr pools);
  696. /// @brief parses the actual structure
  697. ///
  698. /// This method parses the actual list of pools. It creates a parser
  699. /// for each structure using poolParserMaker().
  700. ///
  701. /// @param pools_list a list of pool structures
  702. /// @throw isc::dhcp::DhcpConfigError when pool parsing fails
  703. virtual void build(isc::data::ConstElementPtr pools_list);
  704. /// @brief Stores the parsed values in storage provided
  705. /// by an upper level parser.
  706. virtual void commit();
  707. protected:
  708. /// @brief Creates a PoolParser object
  709. ///
  710. /// Instantiates appropriate (v4 or v6) PoolParser object.
  711. /// @param storage parameter that is passed to ParserMaker() constructor.
  712. virtual ParserPtr poolParserMaker(PoolStoragePtr storage) = 0;
  713. /// @brief pointer to the actual Pools storage
  714. ///
  715. /// That is typically a storage somewhere in Subnet parser
  716. /// (an upper level parser).
  717. PoolStoragePtr pools_;
  718. /// A temporary storage for pools configuration. It is the
  719. /// storage where pools are stored by the build function.
  720. PoolStoragePtr local_pools_;
  721. /// Collection of parsers;
  722. ParserCollection parsers_;
  723. };
  724. /// @brief parser for additional relay information
  725. ///
  726. /// This concrete parser handles RelayInfo structure definitions.
  727. /// So far that structure holds only relay IP (v4 or v6) address, but it
  728. /// is expected that the number of parameters will increase over time.
  729. ///
  730. /// It is useful for parsing Dhcp<4/6>/subnet<4/6>[x]/relay parameters.
  731. class RelayInfoParser : public isc::data::SimpleParser {
  732. public:
  733. /// @brief constructor
  734. /// @param family specifies protocol family (IPv4 or IPv6)
  735. RelayInfoParser(const isc::dhcp::Option::Universe& family);
  736. /// @brief parses the actual relay parameters
  737. ///
  738. /// The elements currently supported are:
  739. /// -# ip-address
  740. ///
  741. /// @param cfg configuration will be stored here
  742. /// @param relay_info JSON structure holding relay parameters to parse
  743. void parse(const isc::dhcp::Subnet::RelayInfoPtr& cfg,
  744. isc::data::ConstElementPtr relay_info);
  745. protected:
  746. /// Protocol family (IPv4 or IPv6)
  747. Option::Universe family_;
  748. };
  749. /// @brief this class parses a single subnet
  750. ///
  751. /// This class parses the whole subnet definition. It creates parsers
  752. /// for received configuration parameters as needed.
  753. class SubnetConfigParser : public DhcpConfigParser {
  754. public:
  755. /// @brief constructor
  756. ///
  757. /// @param global_context
  758. /// @param default_addr default IP address (0.0.0.0 for IPv4, :: for IPv6)
  759. SubnetConfigParser(const std::string&, ParserContextPtr global_context,
  760. const isc::asiolink::IOAddress& default_addr);
  761. /// @brief parses parameter value
  762. ///
  763. /// @param subnet pointer to the content of subnet definition
  764. ///
  765. /// @throw isc::DhcpConfigError if subnet configuration parsing failed.
  766. virtual void build(isc::data::ConstElementPtr subnet);
  767. /// @brief Adds the created subnet to a server's configuration.
  768. virtual void commit() = 0;
  769. protected:
  770. /// @brief creates parsers for entries in subnet definition
  771. ///
  772. /// @param config_id name of the entry
  773. ///
  774. /// @return parser object for specified entry name
  775. /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
  776. /// for unknown config element
  777. virtual DhcpConfigParser* createSubnetConfigParser(
  778. const std::string& config_id) = 0;
  779. /// @brief Issues a server specific warning regarding duplicate subnet
  780. /// options.
  781. ///
  782. /// @param code is the numeric option code of the duplicate option
  783. /// @param addr is the subnet address
  784. /// @todo a means to know the correct logger and perhaps a common
  785. /// message would allow this method to be emitted by the base class.
  786. virtual void duplicate_option_warning(uint32_t code,
  787. isc::asiolink::IOAddress& addr) = 0;
  788. /// @brief Instantiates the subnet based on a given IP prefix and prefix
  789. /// length.
  790. ///
  791. /// @param addr is the IP prefix of the subnet.
  792. /// @param len is the prefix length
  793. virtual void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) = 0;
  794. /// @brief Returns value for a given parameter (after using inheritance)
  795. ///
  796. /// This method implements inheritance. For a given parameter name, it first
  797. /// checks if there is a global value for it and overwrites it with specific
  798. /// value if such value was defined in subnet.
  799. ///
  800. /// @param name name of the parameter
  801. /// @return triplet with the parameter name
  802. /// @throw DhcpConfigError when requested parameter is not present
  803. isc::dhcp::Triplet<uint32_t> getParam(const std::string& name);
  804. /// @brief Returns optional value for a given parameter.
  805. ///
  806. /// This method checks if an optional parameter has been specified for
  807. /// a subnet. If not, it will try to use a global value. If the global
  808. /// value is not specified it will return an object representing an
  809. /// unspecified value.
  810. ///
  811. /// @param name name of the configuration parameter.
  812. /// @return An optional value or a @c Triplet object representing
  813. /// unspecified value.
  814. isc::dhcp::Triplet<uint32_t> getOptionalParam(const std::string& name);
  815. /// @brief Attempts to convert text representation to HRMode enum.
  816. ///
  817. /// Allowed values are "disabled", "off" (alias for disabled),
  818. /// "out-of-pool" and "all". See Subnet::HRMode for their exact meaning.
  819. ///
  820. /// @param txt Host Reservation mode in the textual form.
  821. ///
  822. /// @throw BadValue if the text cannot be converted.
  823. ///
  824. /// @return one of allowed HRMode values
  825. static Subnet::HRMode hrModeFromText(const std::string& txt);
  826. private:
  827. /// @brief Create a new subnet using a data from child parsers.
  828. ///
  829. /// @throw isc::dhcp::DhcpConfigError if subnet configuration parsing
  830. /// failed.
  831. void createSubnet();
  832. protected:
  833. /// Storage for subnet-specific integer values.
  834. Uint32StoragePtr uint32_values_;
  835. /// Storage for subnet-specific string values.
  836. StringStoragePtr string_values_;
  837. /// Storage for subnet-specific boolean values.
  838. BooleanStoragePtr boolean_values_;
  839. /// Storage for pools belonging to this subnet.
  840. PoolStoragePtr pools_;
  841. /// Parsers are stored here.
  842. ParserCollection parsers_;
  843. /// Pointer to the created subnet object.
  844. isc::dhcp::SubnetPtr subnet_;
  845. /// Parsing context which contains global values, options and option
  846. /// definitions.
  847. ParserContextPtr global_context_;
  848. /// Pointer to relay information
  849. isc::dhcp::Subnet::RelayInfoPtr relay_info_;
  850. /// Pointer to the options configuration.
  851. CfgOptionPtr options_;
  852. };
  853. /// @brief Parser for D2ClientConfig
  854. ///
  855. /// This class parses the configuration element "dhcp-ddns" common to the
  856. /// spec files for both dhcp4 and dhcp6. It creates an instance of a
  857. /// D2ClientConfig.
  858. class D2ClientConfigParser : public isc::dhcp::DhcpConfigParser {
  859. public:
  860. /// @brief Constructor
  861. ///
  862. /// @param entry_name is an arbitrary label assigned to this configuration
  863. /// definition.
  864. D2ClientConfigParser(const std::string& entry_name);
  865. /// @brief Destructor
  866. virtual ~D2ClientConfigParser();
  867. /// @brief Performs the parsing of the given dhcp-ddns element.
  868. ///
  869. /// The results of the parsing are retained internally for use during
  870. /// commit.
  871. /// @todo This parser supplies hard-coded default values for all
  872. /// optional parameters. This should be changed once a new plan
  873. /// for configuration is determined.
  874. ///
  875. /// @param client_config is the "dhcp-ddns" configuration to parse
  876. virtual void build(isc::data::ConstElementPtr client_config);
  877. /// @brief Creates a parser for the given "dhcp-ddns" member element id.
  878. ///
  879. /// The elements currently supported are (see isc::dhcp::D2ClientConfig
  880. /// for details on each):
  881. /// -# enable-updates
  882. /// -# qualifying-suffix
  883. /// -# server-ip
  884. /// -# server-port
  885. /// -# ncr-protocol
  886. /// -# ncr-format
  887. /// -# remove-on-renew
  888. /// -# always-include-fqdn
  889. /// -# allow-client-update
  890. /// -# override-no-update
  891. /// -# override-client-update
  892. /// -# replace-client-name
  893. /// -# generated-prefix
  894. ///
  895. /// @param config_id is the "item_name" for a specific member element of
  896. /// the "dns_server" specification.
  897. ///
  898. /// @return returns a pointer to newly created parser.
  899. virtual isc::dhcp::ParserPtr createConfigParser(const std::string&
  900. config_id);
  901. /// @brief Instantiates a D2ClientConfig from internal data values
  902. /// passes to CfgMgr singleton.
  903. virtual void commit();
  904. private:
  905. /// @brief Arbitrary label assigned to this parser instance.
  906. /// Primarily used for diagnostics.
  907. std::string entry_name_;
  908. /// Storage for subnet-specific boolean values.
  909. BooleanStoragePtr boolean_values_;
  910. /// Storage for subnet-specific integer values.
  911. Uint32StoragePtr uint32_values_;
  912. /// Storage for subnet-specific string values.
  913. StringStoragePtr string_values_;
  914. /// @brief Pointer to temporary local instance created during build.
  915. D2ClientConfigPtr local_client_config_ ;
  916. };
  917. // Pointers to various parser objects.
  918. typedef boost::shared_ptr<BooleanParser> BooleanParserPtr;
  919. typedef boost::shared_ptr<StringParser> StringParserPtr;
  920. typedef boost::shared_ptr<Uint32Parser> Uint32ParserPtr;
  921. }; // end of isc::dhcp namespace
  922. }; // end of isc namespace
  923. #endif // DHCP_PARSERS_H