dhcp_parsers.h 34 KB

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