config_parser.cc 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. // Copyright (C) 2012 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. #include <asiolink/io_address.h>
  15. #include <cc/data.h>
  16. #include <config/ccsession.h>
  17. #include <dhcp/libdhcp++.h>
  18. #include <dhcp6/config_parser.h>
  19. #include <dhcp6/dhcp6_log.h>
  20. #include <dhcpsrv/cfgmgr.h>
  21. #include <dhcpsrv/pool.h>
  22. #include <dhcpsrv/subnet.h>
  23. #include <dhcpsrv/triplet.h>
  24. #include <log/logger_support.h>
  25. #include <util/encode/hex.h>
  26. #include <boost/algorithm/string.hpp>
  27. #include <boost/foreach.hpp>
  28. #include <boost/lexical_cast.hpp>
  29. #include <boost/scoped_ptr.hpp>
  30. #include <boost/shared_ptr.hpp>
  31. #include <iostream>
  32. #include <map>
  33. #include <vector>
  34. #include <stdint.h>
  35. using namespace std;
  36. using namespace isc::data;
  37. using namespace isc::asiolink;
  38. namespace isc {
  39. namespace dhcp {
  40. /// @brief an auxiliary type used for storing an element name and its parser
  41. typedef pair<string, ConstElementPtr> ConfigPair;
  42. /// @brief a factory method that will create a parser for a given element name
  43. typedef DhcpConfigParser* ParserFactory(const std::string& config_id);
  44. /// @brief a collection of factories that create parsers for specified element names
  45. typedef std::map<std::string, ParserFactory*> FactoryMap;
  46. /// @brief a collection of elements that store uint32 values (e.g. renew-timer = 900)
  47. typedef std::map<string, uint32_t> Uint32Storage;
  48. /// @brief a collection of elements that store string values
  49. typedef std::map<string, string> StringStorage;
  50. /// @brief a collection of pools
  51. ///
  52. /// This type is used as intermediate storage, when pools are parsed, but there is
  53. /// no subnet object created yet to store them.
  54. typedef std::vector<Pool6Ptr> PoolStorage;
  55. /// @brief Collection of option descriptors. This container allows searching for
  56. /// options using the option code or persistency flag. This is useful when merging
  57. /// existing options with newly configured options.
  58. typedef Subnet::OptionContainer OptionStorage;
  59. /// @brief Global uint32 parameters that will be used as defaults.
  60. Uint32Storage uint32_defaults;
  61. /// @brief global string parameters that will be used as defaults.
  62. StringStorage string_defaults;
  63. /// @brief Global storage for options that will be used as defaults.
  64. OptionStorage option_defaults;
  65. /// @brief a dummy configuration parser
  66. ///
  67. /// This is a debugging parser. It does not configure anything,
  68. /// will accept any configuration and will just print it out
  69. /// on commit. Useful for debugging existing configurations and
  70. /// adding new ones.
  71. class DebugParser : public DhcpConfigParser {
  72. public:
  73. /// @brief Constructor
  74. ///
  75. /// See \ref DhcpConfigParser class for details.
  76. ///
  77. /// @param param_name name of the parsed parameter
  78. DebugParser(const std::string& param_name)
  79. :param_name_(param_name) {
  80. }
  81. /// @brief builds parameter value
  82. ///
  83. /// See \ref DhcpConfigParser class for details.
  84. ///
  85. /// @param new_config pointer to the new configuration
  86. virtual void build(ConstElementPtr new_config) {
  87. std::cout << "Build for token: [" << param_name_ << "] = ["
  88. << value_->str() << "]" << std::endl;
  89. value_ = new_config;
  90. }
  91. /// @brief pretends to apply the configuration
  92. ///
  93. /// This is a method required by the base class. It pretends to apply the
  94. /// configuration, but in fact it only prints the parameter out.
  95. ///
  96. /// See \ref DhcpConfigParser class for details.
  97. virtual void commit() {
  98. // Debug message. The whole DebugParser class is used only for parser
  99. // debugging, and is not used in production code. It is very convenient
  100. // to keep it around. Please do not turn this cout into logger calls.
  101. std::cout << "Commit for token: [" << param_name_ << "] = ["
  102. << value_->str() << "]" << std::endl;
  103. }
  104. /// @brief factory that constructs DebugParser objects
  105. ///
  106. /// @param param_name name of the parameter to be parsed
  107. static DhcpConfigParser* Factory(const std::string& param_name) {
  108. return (new DebugParser(param_name));
  109. }
  110. protected:
  111. /// name of the parsed parameter
  112. std::string param_name_;
  113. /// pointer to the actual value of the parameter
  114. ConstElementPtr value_;
  115. };
  116. /// @brief Configuration parser for uint32 parameters
  117. ///
  118. /// This class is a generic parser that is able to handle any uint32 integer
  119. /// type. By default it stores the value in external global container
  120. /// (uint32_defaults). If used in smaller scopes (e.g. to parse parameters
  121. /// in subnet config), it can be pointed to a different storage, using
  122. /// setStorage() method. This class follows the parser interface, laid out
  123. /// in its base class, \ref DhcpConfigParser.
  124. ///
  125. /// For overview of usability of this generic purpose parser, see
  126. /// \ref dhcpv6-config-inherit page.
  127. ///
  128. /// @todo this class should be turned into the template class which
  129. /// will handle all uintX_types of data (see ticket #2415).
  130. class Uint32Parser : public DhcpConfigParser {
  131. public:
  132. /// @brief constructor for Uint32Parser
  133. /// @param param_name name of the configuration parameter being parsed
  134. Uint32Parser(const std::string& param_name)
  135. :storage_(&uint32_defaults), param_name_(param_name) {
  136. }
  137. /// @brief builds parameter value
  138. ///
  139. /// Parses configuration entry and stores it in a storage. See
  140. /// \ref setStorage() for details.
  141. ///
  142. /// @param value pointer to the content of parsed values
  143. virtual void build(ConstElementPtr value) {
  144. bool parse_error = false;
  145. // Cast the provided value to int64 value to check.
  146. int64_t int64value = 0;
  147. try {
  148. // Parsing the value as a int64 value allows to
  149. // check if the provided value is within the range
  150. // of uint32_t (is not negative or greater than
  151. // maximal uint32_t value).
  152. int64value = boost::lexical_cast<int64_t>(value->str());
  153. } catch (const boost::bad_lexical_cast&) {
  154. parse_error = true;
  155. }
  156. if (!parse_error) {
  157. if ((int64value < 0) ||
  158. (int64value > std::numeric_limits<uint32_t>::max())) {
  159. parse_error = true;
  160. } else {
  161. try {
  162. value_ = boost::lexical_cast<uint32_t>(value->str());
  163. } catch (const boost::bad_lexical_cast &) {
  164. parse_error = true;
  165. }
  166. }
  167. }
  168. if (parse_error) {
  169. isc_throw(BadValue, "Failed to parse value " << value->str()
  170. << " as unsigned 32-bit integer.");
  171. }
  172. // If a given parameter already exists in the storage we override
  173. // its value. If it doesn't we insert a new element.
  174. (*storage_)[param_name_] = value_;
  175. }
  176. /// @brief does nothing
  177. ///
  178. /// This method is required for all parsers. The value itself
  179. /// is not commited anywhere. Higher level parsers are expected to
  180. /// use values stored in the storage, e.g. renew-timer for a given
  181. /// subnet is stored in subnet-specific storage. It is not commited
  182. /// here, but is rather used by \ref Subnet6Parser when constructing
  183. /// the subnet.
  184. virtual void commit() { }
  185. /// @brief factory that constructs Uint32Parser objects
  186. ///
  187. /// @param param_name name of the parameter to be parsed
  188. static DhcpConfigParser* Factory(const std::string& param_name) {
  189. return (new Uint32Parser(param_name));
  190. }
  191. /// @brief sets storage for value of this parameter
  192. ///
  193. /// See \ref dhcpv6-config-inherit for details.
  194. ///
  195. /// @param storage pointer to the storage container
  196. void setStorage(Uint32Storage* storage) {
  197. storage_ = storage;
  198. }
  199. protected:
  200. /// pointer to the storage, where parsed value will be stored
  201. Uint32Storage* storage_;
  202. /// name of the parameter to be parsed
  203. std::string param_name_;
  204. /// the actual parsed value
  205. uint32_t value_;
  206. };
  207. /// @brief Configuration parser for string parameters
  208. ///
  209. /// This class is a generic parser that is able to handle any string
  210. /// parameter. By default it stores the value in an external global container
  211. /// (string_defaults). If used in smaller scopes (e.g. to parse parameters
  212. /// in subnet config), it can be pointed to a different storage, using the
  213. /// setStorage() method. This class follows the parser interface, laid out
  214. /// in its base class, \ref DhcpConfigParser.
  215. ///
  216. /// For overview of usability of this generic purpose parser, see
  217. /// \ref dhcpv6-config-inherit page.
  218. class StringParser : public DhcpConfigParser {
  219. public:
  220. /// @brief constructor for StringParser
  221. /// @param param_name name of the configuration parameter being parsed
  222. StringParser(const std::string& param_name)
  223. :storage_(&string_defaults), param_name_(param_name) {
  224. }
  225. /// @brief parses parameter value
  226. ///
  227. /// Parses configuration entry and stores it in storage. See
  228. /// \ref setStorage() for details.
  229. ///
  230. /// @param value pointer to the content of parsed values
  231. virtual void build(ConstElementPtr value) {
  232. value_ = value->str();
  233. boost::erase_all(value_, "\"");
  234. // If a given parameter already exists in the storage we override
  235. // its value. If it doesn't we insert a new element.
  236. (*storage_)[param_name_] = value_;
  237. }
  238. /// @brief does nothing
  239. ///
  240. /// This method is required for all parsers. The value itself
  241. /// is not commited anywhere. Higher level parsers are expected to
  242. /// use values stored in the storage, e.g. renew-timer for a given
  243. /// subnet is stored in subnet-specific storage. It is not commited
  244. /// here, but is rather used by its parent parser when constructing
  245. /// an object, e.g. the subnet.
  246. virtual void commit() { }
  247. /// @brief factory that constructs StringParser objects
  248. ///
  249. /// @param param_name name of the parameter to be parsed
  250. static DhcpConfigParser* Factory(const std::string& param_name) {
  251. return (new StringParser(param_name));
  252. }
  253. /// @brief sets storage for value of this parameter
  254. ///
  255. /// See \ref dhcpv6-config-inherit for details.
  256. ///
  257. /// @param storage pointer to the storage container
  258. void setStorage(StringStorage* storage) {
  259. storage_ = storage;
  260. }
  261. protected:
  262. /// pointer to the storage, where parsed value will be stored
  263. StringStorage* storage_;
  264. /// name of the parameter to be parsed
  265. std::string param_name_;
  266. /// the actual parsed value
  267. std::string value_;
  268. };
  269. /// @brief parser for interface list definition
  270. ///
  271. /// This parser handles Dhcp6/interface entry.
  272. /// It contains a list of network interfaces that the server listens on.
  273. /// In particular, it can contain an entry called "all" or "any" that
  274. /// designates all interfaces.
  275. ///
  276. /// It is useful for parsing Dhcp6/interface parameter.
  277. class InterfaceListConfigParser : public DhcpConfigParser {
  278. public:
  279. /// @brief constructor
  280. ///
  281. /// As this is a dedicated parser, it must be used to parse
  282. /// "interface" parameter only. All other types will throw exception.
  283. ///
  284. /// @param param_name name of the configuration parameter being parsed
  285. InterfaceListConfigParser(const std::string& param_name) {
  286. if (param_name != "interface") {
  287. isc_throw(NotImplemented, "Internal error. Interface configuration "
  288. "parser called for the wrong parameter: " << param_name);
  289. }
  290. }
  291. /// @brief parses parameters value
  292. ///
  293. /// Parses configuration entry (list of parameters) and stores it in
  294. /// storage. See \ref setStorage() for details.
  295. ///
  296. /// @param value pointer to the content of parsed values
  297. virtual void build(ConstElementPtr value) {
  298. BOOST_FOREACH(ConstElementPtr iface, value->listValue()) {
  299. interfaces_.push_back(iface->str());
  300. }
  301. }
  302. /// @brief commits interfaces list configuration
  303. virtual void commit() {
  304. /// @todo: Implement per interface listening. Currently always listening
  305. /// on all interfaces.
  306. }
  307. /// @brief factory that constructs InterfaceListConfigParser objects
  308. ///
  309. /// @param param_name name of the parameter to be parsed
  310. static DhcpConfigParser* Factory(const std::string& param_name) {
  311. return (new InterfaceListConfigParser(param_name));
  312. }
  313. protected:
  314. /// contains list of network interfaces
  315. vector<string> interfaces_;
  316. };
  317. /// @brief parser for pool definition
  318. ///
  319. /// This parser handles pool definitions, i.e. a list of entries of one
  320. /// of two syntaxes: min-max and prefix/len. Pool6 objects are created
  321. /// and stored in chosen PoolStorage container.
  322. ///
  323. /// As there are no default values for pool, setStorage() must be called
  324. /// before build(). Otherwise an exception will be thrown.
  325. ///
  326. /// It is useful for parsing Dhcp6/subnet6[X]/pool parameters.
  327. class PoolParser : public DhcpConfigParser {
  328. public:
  329. /// @brief constructor.
  330. PoolParser(const std::string& /*param_name*/)
  331. :pools_(NULL) {
  332. // ignore parameter name, it is always Dhcp6/subnet6[X]/pool
  333. }
  334. /// @brief parses the actual list
  335. ///
  336. /// This method parses the actual list of interfaces.
  337. /// No validation is done at this stage, everything is interpreted as
  338. /// interface name.
  339. void build(ConstElementPtr pools_list) {
  340. // setStorage() should have been called before build
  341. if (!pools_) {
  342. isc_throw(NotImplemented, "Parser logic error. No pool storage set,"
  343. " but pool parser asked to parse pools");
  344. }
  345. BOOST_FOREACH(ConstElementPtr text_pool, pools_list->listValue()) {
  346. // That should be a single pool representation. It should contain
  347. // text in the form prefix/len or first - last. Note that spaces
  348. // are allowed
  349. string txt = text_pool->stringValue();
  350. // first let's remove any whitespaces
  351. boost::erase_all(txt, " "); // space
  352. boost::erase_all(txt, "\t"); // tabulation
  353. // Is this prefix/len notation?
  354. size_t pos = txt.find("/");
  355. if (pos != string::npos) {
  356. IOAddress addr("::");
  357. uint8_t len = 0;
  358. try {
  359. addr = IOAddress(txt.substr(0, pos));
  360. // start with the first character after /
  361. string prefix_len = txt.substr(pos + 1);
  362. // It is lexically cast to int and then downcast to uint8_t.
  363. // Direct cast to uint8_t (which is really an unsigned char)
  364. // will result in interpreting the first digit as output
  365. // value and throwing exception if length is written on two
  366. // digits (because there are extra characters left over).
  367. // No checks for values over 128. Range correctness will
  368. // be checked in Pool6 constructor.
  369. len = boost::lexical_cast<int>(prefix_len);
  370. } catch (...) {
  371. isc_throw(Dhcp6ConfigError, "Failed to parse pool "
  372. "definition: " << text_pool->stringValue());
  373. }
  374. Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, addr, len));
  375. pools_->push_back(pool);
  376. continue;
  377. }
  378. // Is this min-max notation?
  379. pos = txt.find("-");
  380. if (pos != string::npos) {
  381. // using min-max notation
  382. IOAddress min(txt.substr(0,pos - 1));
  383. IOAddress max(txt.substr(pos + 1));
  384. Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, min, max));
  385. pools_->push_back(pool);
  386. continue;
  387. }
  388. isc_throw(Dhcp6ConfigError, "Failed to parse pool definition:"
  389. << text_pool->stringValue() <<
  390. ". Does not contain - (for min-max) nor / (prefix/len)");
  391. }
  392. }
  393. /// @brief sets storage for value of this parameter
  394. ///
  395. /// See \ref dhcpv6-config-inherit for details.
  396. ///
  397. /// @param storage pointer to the storage container
  398. void setStorage(PoolStorage* storage) {
  399. pools_ = storage;
  400. }
  401. /// @brief does nothing.
  402. ///
  403. /// This method is required for all parsers. The value itself
  404. /// is not commited anywhere. Higher level parsers (for subnet) are expected
  405. /// to use values stored in the storage.
  406. virtual void commit() {}
  407. /// @brief factory that constructs PoolParser objects
  408. ///
  409. /// @param param_name name of the parameter to be parsed
  410. static DhcpConfigParser* Factory(const std::string& param_name) {
  411. return (new PoolParser(param_name));
  412. }
  413. protected:
  414. /// @brief pointer to the actual Pools storage
  415. ///
  416. /// This is typically a storage somewhere in Subnet parser
  417. /// (an upper level parser).
  418. PoolStorage* pools_;
  419. };
  420. /// @brief Parser for option data value.
  421. ///
  422. /// This parser parses configuration entries that specify value of
  423. /// a single option. These entries include option name, option code
  424. /// and data carried by the option. If parsing is successful than an
  425. /// instance of an option is created and added to the storage provided
  426. /// by the calling class.
  427. ///
  428. /// @todo This class parses and validates the option name. However it is
  429. /// not used anywhere util support for option spaces is implemented
  430. /// (see tickets #2319, #2314). When option spaces are implemented
  431. /// there will be a way to reference the particular option using
  432. /// its type (code) or option name.
  433. class OptionDataParser : public DhcpConfigParser {
  434. public:
  435. /// @brief Constructor.
  436. ///
  437. /// Class constructor.
  438. OptionDataParser(const std::string&)
  439. : options_(NULL),
  440. // initialize option to NULL ptr
  441. option_descriptor_(false) { }
  442. /// @brief Parses the single option data.
  443. ///
  444. /// This method parses the data of a single option from the configuration.
  445. /// The option data includes option name, option code and data being
  446. /// carried by this option. Eventually it creates the instance of the
  447. /// option.
  448. ///
  449. /// @warning setStorage must be called with valid storage pointer prior
  450. /// to calling this method.
  451. ///
  452. /// @param option_data_entries collection of entries that define value
  453. /// for a particular option.
  454. /// @throw Dhcp6ConfigError if invalid parameter specified in
  455. /// the configuration.
  456. /// @throw isc::InvalidOperation if failed to set storage prior to
  457. /// calling build.
  458. /// @throw isc::BadValue if option data storage is invalid.
  459. virtual void build(ConstElementPtr option_data_entries) {
  460. if (options_ == NULL) {
  461. isc_throw(isc::InvalidOperation, "Parser logic error: storage must be set before "
  462. "parsing option data.");
  463. }
  464. BOOST_FOREACH(ConfigPair param, option_data_entries->mapValue()) {
  465. ParserPtr parser;
  466. if (param.first == "name") {
  467. boost::shared_ptr<StringParser>
  468. name_parser(dynamic_cast<StringParser*>(StringParser::Factory(param.first)));
  469. if (name_parser) {
  470. name_parser->setStorage(&string_values_);
  471. parser = name_parser;
  472. }
  473. } else if (param.first == "code") {
  474. boost::shared_ptr<Uint32Parser>
  475. code_parser(dynamic_cast<Uint32Parser*>(Uint32Parser::Factory(param.first)));
  476. if (code_parser) {
  477. code_parser->setStorage(&uint32_values_);
  478. parser = code_parser;
  479. }
  480. } else if (param.first == "data") {
  481. boost::shared_ptr<StringParser>
  482. value_parser(dynamic_cast<StringParser*>(StringParser::Factory(param.first)));
  483. if (value_parser) {
  484. value_parser->setStorage(&string_values_);
  485. parser = value_parser;
  486. }
  487. } else {
  488. isc_throw(Dhcp6ConfigError,
  489. "Parser error: option-data parameter not supported: "
  490. << param.first);
  491. }
  492. parser->build(param.second);
  493. }
  494. // Try to create the option instance.
  495. createOption();
  496. }
  497. /// @brief Commits option value.
  498. ///
  499. /// This function adds a new option to the storage or replaces an existing option
  500. /// with the same code.
  501. ///
  502. /// @throw isc::InvalidOperation if failed to set pointer to storage or failed
  503. /// to call build() prior to commit. If that happens data in the storage
  504. /// remain un-modified.
  505. virtual void commit() {
  506. if (options_ == NULL) {
  507. isc_throw(isc::InvalidOperation, "Parser logic error: storage must be set before "
  508. "commiting option data.");
  509. } else if (!option_descriptor_.option) {
  510. // Before we can commit the new option should be configured. If it is not
  511. // than somebody must have called commit() before build().
  512. isc_throw(isc::InvalidOperation, "Parser logic error: no option has been configured and"
  513. " thus there is nothing to commit. Has build() been called?");
  514. }
  515. uint16_t opt_type = option_descriptor_.option->getType();
  516. Subnet::OptionContainerTypeIndex& idx = options_->get<1>();
  517. // Try to find options with the particular option code in the main
  518. // storage. If found, remove these options because they will be
  519. // replaced with new one.
  520. Subnet::OptionContainerTypeRange range =
  521. idx.equal_range(opt_type);
  522. if (std::distance(range.first, range.second) > 0) {
  523. idx.erase(range.first, range.second);
  524. }
  525. // Append new option to the main storage.
  526. options_->push_back(option_descriptor_);
  527. }
  528. /// @brief Set storage for the parser.
  529. ///
  530. /// Sets storage for the parser. This storage points to the
  531. /// vector of options and is used by multiple instances of
  532. /// OptionDataParser. Each instance creates exactly one object
  533. /// of dhcp::Option or derived type and appends it to this
  534. /// storage.
  535. ///
  536. /// @param storage pointer to the options storage
  537. void setStorage(OptionStorage* storage) {
  538. options_ = storage;
  539. }
  540. private:
  541. /// @brief Create option instance.
  542. ///
  543. /// Creates an instance of an option and adds it to the provided
  544. /// options storage. If the option data parsed by \ref build function
  545. /// are invalid or insufficient this function emits an exception.
  546. ///
  547. /// @warning this function does not check if options_ storage pointer
  548. /// is intitialized but this check is not needed here because it is done
  549. /// in the \ref build function.
  550. ///
  551. /// @throw Dhcp6ConfigError if parameters provided in the configuration
  552. /// are invalid.
  553. void createOption() {
  554. // Option code is held in the uint32_t storage but is supposed to
  555. // be uint16_t value. We need to check that value in the configuration
  556. // does not exceed range of uint16_t and is not zero.
  557. uint32_t option_code = getUint32Param("code");
  558. if (option_code == 0) {
  559. isc_throw(Dhcp6ConfigError, "Parser error: value of 'code' must not"
  560. << " be equal to zero. Option code '0' is reserved in"
  561. << " DHCPv6.");
  562. } else if (option_code > std::numeric_limits<uint16_t>::max()) {
  563. isc_throw(Dhcp6ConfigError, "Parser error: value of 'code' must not"
  564. << " exceed " << std::numeric_limits<uint16_t>::max());
  565. }
  566. // Check that the option name has been specified, is non-empty and does not
  567. // contain spaces.
  568. // @todo possibly some more restrictions apply here?
  569. std::string option_name = getStringParam("name");
  570. if (option_name.empty()) {
  571. isc_throw(Dhcp6ConfigError, "Parser error: option name must not be"
  572. << " empty");
  573. } else if (option_name.find(" ") != std::string::npos) {
  574. isc_throw(Dhcp6ConfigError, "Parser error: option name must not contain"
  575. << " spaces");
  576. }
  577. // Get option data from the configuration database ('data' field).
  578. // Option data is specified by the user as case insensitive string
  579. // of hexadecimal digits for each option.
  580. std::string option_data = getStringParam("data");
  581. // Transform string of hexadecimal digits into binary format.
  582. std::vector<uint8_t> binary;
  583. try {
  584. util::encode::decodeHex(option_data, binary);
  585. } catch (...) {
  586. isc_throw(Dhcp6ConfigError, "Parser error: option data is not a valid"
  587. << " string of hexadecimal digits: " << option_data);
  588. }
  589. // Get all existing DHCPv6 option definitions. The one that matches
  590. // our option will be picked and used to create it.
  591. OptionDefContainer option_defs = LibDHCP::getOptionDefs(Option::V6);
  592. // Get search index #1. It allows searching for options definitions
  593. // using option type value.
  594. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  595. // Get all option definitions matching option code we want to create.
  596. const OptionDefContainerTypeRange& range = idx.equal_range(option_code);
  597. size_t num_defs = std::distance(range.first, range.second);
  598. OptionPtr option;
  599. // Currently we do not allow duplicated definitions and if there are
  600. // any duplicates we issue internal server error.
  601. if (num_defs > 1) {
  602. isc_throw(Dhcp6ConfigError, "Internal error: currently it is not"
  603. << " supported to initialize multiple option definitions"
  604. << " for the same option code. This will be supported once"
  605. << " there option spaces are implemented.");
  606. } else if (num_defs == 0) {
  607. // @todo We have a limited set of option definitions intiialized at the moment.
  608. // In the future we want to initialize option definitions for all options.
  609. // Consequently an error will be issued if an option definition does not exist
  610. // for a particular option code. For now it is ok to create generic option
  611. // if definition does not exist.
  612. OptionPtr option(new Option(Option::V6, static_cast<uint16_t>(option_code),
  613. binary));
  614. // The created option is stored in option_descriptor_ class member until the
  615. // commit stage when it is inserted into the main storage. If an option with the
  616. // same code exists in main storage already the old option is replaced.
  617. option_descriptor_.option = option;
  618. option_descriptor_.persistent = false;
  619. } else {
  620. // We have exactly one option definition for the particular option code
  621. // use it to create the option instance.
  622. const OptionDefinitionPtr& def = *(range.first);
  623. try {
  624. OptionPtr option = def->optionFactory(Option::V6, option_code, binary);
  625. Subnet::OptionDescriptor desc(option, false);
  626. option_descriptor_.option = option;
  627. option_descriptor_.persistent = false;
  628. } catch (const isc::Exception& ex) {
  629. isc_throw(Dhcp6ConfigError, "Parser error: option data does not match"
  630. << " option definition (code " << option_code << "): "
  631. << ex.what());
  632. }
  633. }
  634. }
  635. /// @brief Get a parameter from the strings storage.
  636. ///
  637. /// @param param_id parameter identifier.
  638. /// @throw Dhcp6ConfigError if parameter has not been found.
  639. std::string getStringParam(const std::string& param_id) const {
  640. StringStorage::const_iterator param = string_values_.find(param_id);
  641. if (param == string_values_.end()) {
  642. isc_throw(Dhcp6ConfigError, "Parser error: option-data parameter"
  643. << " '" << param_id << "' not specified");
  644. }
  645. return (param->second);
  646. }
  647. /// @brief Get a parameter from the uint32 values storage.
  648. ///
  649. /// @param param_id parameter identifier.
  650. /// @throw Dhcp6ConfigError if parameter has not been found.
  651. uint32_t getUint32Param(const std::string& param_id) const {
  652. Uint32Storage::const_iterator param = uint32_values_.find(param_id);
  653. if (param == uint32_values_.end()) {
  654. isc_throw(Dhcp6ConfigError, "Parser error: option-data parameter"
  655. << " '" << param_id << "' not specified");
  656. }
  657. return (param->second);
  658. }
  659. /// Storage for uint32 values (e.g. option code).
  660. Uint32Storage uint32_values_;
  661. /// Storage for string values (e.g. option name or data).
  662. StringStorage string_values_;
  663. /// Pointer to options storage. This storage is provided by
  664. /// the calling class and is shared by all OptionDataParser objects.
  665. OptionStorage* options_;
  666. /// Option descriptor holds newly configured option.
  667. Subnet::OptionDescriptor option_descriptor_;
  668. };
  669. /// @brief Parser for option data values within a subnet.
  670. ///
  671. /// This parser iterates over all entries that define options
  672. /// data for a particular subnet and creates a collection of options.
  673. /// If parsing is successful, all these options are added to the Subnet
  674. /// object.
  675. class OptionDataListParser : public DhcpConfigParser {
  676. public:
  677. /// @brief Constructor.
  678. ///
  679. /// Unless otherwise specified, parsed options will be stored in
  680. /// a global option container (option_default). That storage location
  681. /// is overriden on a subnet basis.
  682. OptionDataListParser(const std::string&)
  683. : options_(&option_defaults), local_options_() { }
  684. /// @brief Parses entries that define options' data for a subnet.
  685. ///
  686. /// This method iterates over all entries that define option data
  687. /// for options within a single subnet and creates options' instances.
  688. ///
  689. /// @param option_data_list pointer to a list of options' data sets.
  690. /// @throw Dhcp6ConfigError if option parsing failed.
  691. void build(ConstElementPtr option_data_list) {
  692. BOOST_FOREACH(ConstElementPtr option_value, option_data_list->listValue()) {
  693. boost::shared_ptr<OptionDataParser> parser(new OptionDataParser("option-data"));
  694. // options_ member will hold instances of all options thus
  695. // each OptionDataParser takes it as a storage.
  696. parser->setStorage(&local_options_);
  697. // Build the instance of a single option.
  698. parser->build(option_value);
  699. // Store a parser as it will be used to commit.
  700. parsers_.push_back(parser);
  701. }
  702. }
  703. /// @brief Set storage for option instances.
  704. ///
  705. /// @param storage pointer to options storage.
  706. void setStorage(OptionStorage* storage) {
  707. options_ = storage;
  708. }
  709. /// @brief Commit all option values.
  710. ///
  711. /// This function invokes commit for all option values.
  712. void commit() {
  713. BOOST_FOREACH(ParserPtr parser, parsers_) {
  714. parser->commit();
  715. }
  716. // Parsing was successful and we have all configured
  717. // options in local storage. We can now replace old values
  718. // with new values.
  719. std::swap(local_options_, *options_);
  720. }
  721. /// @brief Create DhcpDataListParser object
  722. ///
  723. /// @param param_name param name.
  724. ///
  725. /// @return DhcpConfigParser object.
  726. static DhcpConfigParser* Factory(const std::string& param_name) {
  727. return (new OptionDataListParser(param_name));
  728. }
  729. /// Intermediate option storage. This storage is used by
  730. /// lower level parsers to add new options. Values held
  731. /// in this storage are assigned to main storage (options_)
  732. /// if overall parsing was successful.
  733. OptionStorage local_options_;
  734. /// Pointer to options instances storage.
  735. OptionStorage* options_;
  736. /// Collection of parsers;
  737. ParserCollection parsers_;
  738. };
  739. /// @brief this class parses a single subnet
  740. ///
  741. /// This class parses the whole subnet definition. It creates parsers
  742. /// for received configuration parameters as needed.
  743. class Subnet6ConfigParser : public DhcpConfigParser {
  744. public:
  745. /// @brief constructor
  746. Subnet6ConfigParser(const std::string& ) {
  747. // The parameter should always be "subnet", but we don't check
  748. // against that here in case some wants to reuse this parser somewhere.
  749. }
  750. /// @brief parses parameter value
  751. ///
  752. /// @param subnet pointer to the content of subnet definition
  753. void build(ConstElementPtr subnet) {
  754. BOOST_FOREACH(ConfigPair param, subnet->mapValue()) {
  755. ParserPtr parser(createSubnet6ConfigParser(param.first));
  756. // The actual type of the parser is unknown here. We have to discover
  757. // the parser type here to invoke the corresponding setStorage function
  758. // on it. We discover parser type by trying to cast the parser to various
  759. // parser types and checking which one was successful. For this one
  760. // a setStorage and build methods are invoked.
  761. // Try uint32 type parser.
  762. if (buildParser<Uint32Parser, Uint32Storage >(parser, uint32_values_,
  763. param.second)) {
  764. // Storage set, build invoked on the parser, proceed with
  765. // next configuration element.
  766. continue;
  767. }
  768. // Try string type parser.
  769. if (buildParser<StringParser, StringStorage >(parser, string_values_,
  770. param.second)) {
  771. continue;
  772. }
  773. // Try pools parser.
  774. if (buildParser<PoolParser, PoolStorage >(parser, pools_,
  775. param.second)) {
  776. continue;
  777. }
  778. // Try option data parser.
  779. if (buildParser<OptionDataListParser, OptionStorage >(parser, options_,
  780. param.second)) {
  781. continue;
  782. }
  783. }
  784. // Ok, we now have subnet parsed
  785. }
  786. /// @brief commits received configuration.
  787. ///
  788. /// This method does most of the configuration. Many other parsers are just
  789. /// storing the values that are actually consumed here. Pool definitions
  790. /// created in other parsers are used here and added to newly created Subnet6
  791. /// objects. Subnet6 are then added to DHCP CfgMgr.
  792. void commit() {
  793. // Invoke commit on all sub-data parsers.
  794. BOOST_FOREACH(ParserPtr parser, parsers_) {
  795. parser->commit();
  796. }
  797. StringStorage::const_iterator it = string_values_.find("subnet");
  798. if (it == string_values_.end()) {
  799. isc_throw(Dhcp6ConfigError,
  800. "Mandatory subnet definition in subnet missing");
  801. }
  802. string subnet_txt = it->second;
  803. boost::erase_all(subnet_txt, " ");
  804. boost::erase_all(subnet_txt, "\t");
  805. size_t pos = subnet_txt.find("/");
  806. if (pos == string::npos) {
  807. isc_throw(Dhcp6ConfigError,
  808. "Invalid subnet syntax (prefix/len expected):" << it->second);
  809. }
  810. IOAddress addr(subnet_txt.substr(0, pos));
  811. uint8_t len = boost::lexical_cast<unsigned int>(subnet_txt.substr(pos + 1));
  812. Triplet<uint32_t> t1 = getParam("renew-timer");
  813. Triplet<uint32_t> t2 = getParam("rebind-timer");
  814. Triplet<uint32_t> pref = getParam("preferred-lifetime");
  815. Triplet<uint32_t> valid = getParam("valid-lifetime");
  816. /// @todo: Convert this to logger once the parser is working reliably
  817. stringstream tmp;
  818. tmp << addr.toText() << "/" << (int)len
  819. << " with params t1=" << t1 << ", t2=" << t2 << ", pref="
  820. << pref << ", valid=" << valid;
  821. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_NEW_SUBNET).arg(tmp.str());
  822. Subnet6Ptr subnet(new Subnet6(addr, len, t1, t2, pref, valid));
  823. for (PoolStorage::iterator it = pools_.begin(); it != pools_.end(); ++it) {
  824. subnet->addPool6(*it);
  825. }
  826. const Subnet::OptionContainer& options = subnet->getOptions();
  827. const Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  828. // Add subnet specific options.
  829. BOOST_FOREACH(Subnet::OptionDescriptor desc, options_) {
  830. Subnet::OptionContainerTypeRange range = idx.equal_range(desc.option->getType());
  831. if (std::distance(range.first, range.second) > 0) {
  832. LOG_WARN(dhcp6_logger, DHCP6_CONFIG_OPTION_DUPLICATE)
  833. .arg(desc.option->getType()).arg(addr.toText());
  834. }
  835. subnet->addOption(desc.option);
  836. }
  837. // Check all global options and add them to the subnet object if
  838. // they have been configured in the global scope. If they have been
  839. // configured in the subnet scope we don't add global option because
  840. // the one configured in the subnet scope always takes precedence.
  841. BOOST_FOREACH(Subnet::OptionDescriptor desc, option_defaults) {
  842. // Get all options specified locally in the subnet and having
  843. // code equal to global option's code.
  844. Subnet::OptionContainerTypeRange range = idx.equal_range(desc.option->getType());
  845. // @todo: In the future we will be searching for options using either
  846. // an option code or namespace. Currently we have only the option
  847. // code available so if there is at least one option found with the
  848. // specific code we don't add the globally configured option.
  849. // @todo with this code the first globally configured option
  850. // with the given code will be added to a subnet. We may
  851. // want to issue a warning about dropping the configuration of
  852. // a global option if one already exsists.
  853. if (std::distance(range.first, range.second) == 0) {
  854. subnet->addOption(desc.option);
  855. }
  856. }
  857. CfgMgr::instance().addSubnet6(subnet);
  858. }
  859. private:
  860. /// @brief Set storage for a parser and invoke build.
  861. ///
  862. /// This helper method casts the provided parser pointer to the specified
  863. /// type. If the cast is successful it sets the corresponding storage for
  864. /// this parser, invokes build on it and saves the parser.
  865. ///
  866. /// @tparam T parser type to which parser argument should be cast.
  867. /// @tparam Y storage type for the specified parser type.
  868. /// @param parser parser on which build must be invoked.
  869. /// @param storage reference to a storage that will be set for a parser.
  870. /// @param subnet subnet element read from the configuration and being parsed.
  871. /// @return true if parser pointer was successfully cast to specialized
  872. /// parser type provided as Y.
  873. template<typename T, typename Y>
  874. bool buildParser(const ParserPtr& parser, Y& storage, const ConstElementPtr& subnet) {
  875. // We need to cast to T in order to set storage for the parser.
  876. boost::shared_ptr<T> cast_parser = boost::dynamic_pointer_cast<T>(parser);
  877. // It is common that this cast is not successful because we try to cast to all
  878. // supported parser types as we don't know the type of a parser in advance.
  879. if (cast_parser) {
  880. // Cast, successful so we go ahead with setting storage and actual parse.
  881. cast_parser->setStorage(&storage);
  882. parser->build(subnet);
  883. parsers_.push_back(parser);
  884. // We indicate that cast was successful so as the calling function
  885. // may skip attempts to cast to other parser types and proceed to
  886. // next element.
  887. return (true);
  888. }
  889. // It was not successful. Indicate that another parser type
  890. // should be tried.
  891. return (false);
  892. }
  893. /// @brief creates parsers for entries in subnet definition
  894. ///
  895. /// @todo Add subnet-specific things here (e.g. subnet-specific options)
  896. ///
  897. /// @param config_id name od the entry
  898. /// @return parser object for specified entry name
  899. DhcpConfigParser* createSubnet6ConfigParser(const std::string& config_id) {
  900. FactoryMap factories;
  901. factories.insert(pair<string, ParserFactory*>(
  902. "preferred-lifetime", Uint32Parser::Factory));
  903. factories.insert(pair<string, ParserFactory*>(
  904. "valid-lifetime", Uint32Parser::Factory));
  905. factories.insert(pair<string, ParserFactory*>(
  906. "renew-timer", Uint32Parser::Factory));
  907. factories.insert(pair<string, ParserFactory*>(
  908. "rebind-timer", Uint32Parser::Factory));
  909. factories.insert(pair<string, ParserFactory*>(
  910. "subnet", StringParser::Factory));
  911. factories.insert(pair<string, ParserFactory*>(
  912. "pool", PoolParser::Factory));
  913. factories.insert(pair<string, ParserFactory*>(
  914. "option-data", OptionDataListParser::Factory));
  915. FactoryMap::iterator f = factories.find(config_id);
  916. if (f == factories.end()) {
  917. // Used for debugging only.
  918. // return new DebugParser(config_id);
  919. isc_throw(NotImplemented,
  920. "Parser error: Subnet6 parameter not supported: "
  921. << config_id);
  922. }
  923. return (f->second(config_id));
  924. }
  925. /// @brief returns value for a given parameter (after using inheritance)
  926. ///
  927. /// This method implements inheritance. For a given parameter name, it first
  928. /// checks if there is a global value for it and overwrites it with specific
  929. /// value if such value was defined in subnet.
  930. ///
  931. /// @param name name of the parameter
  932. /// @return triplet with the parameter name
  933. Triplet<uint32_t> getParam(const std::string& name) {
  934. uint32_t value = 0;
  935. bool found = false;
  936. Uint32Storage::iterator global = uint32_defaults.find(name);
  937. if (global != uint32_defaults.end()) {
  938. value = global->second;
  939. found = true;
  940. }
  941. Uint32Storage::iterator local = uint32_values_.find(name);
  942. if (local != uint32_values_.end()) {
  943. value = local->second;
  944. found = true;
  945. }
  946. if (found) {
  947. return (Triplet<uint32_t>(value));
  948. } else {
  949. isc_throw(Dhcp6ConfigError, "Mandatory parameter " << name
  950. << " missing (no global default and no subnet-"
  951. << "specific value)");
  952. }
  953. }
  954. /// storage for subnet-specific uint32 values
  955. Uint32Storage uint32_values_;
  956. /// storage for subnet-specific integer values
  957. StringStorage string_values_;
  958. /// storage for pools belonging to this subnet
  959. PoolStorage pools_;
  960. /// storage for options belonging to this subnet
  961. OptionStorage options_;
  962. /// parsers are stored here
  963. ParserCollection parsers_;
  964. };
  965. /// @brief this class parses a list of subnets
  966. ///
  967. /// This is a wrapper parser that handles the whole list of Subnet6
  968. /// definitions. It iterates over all entries and creates Subnet6ConfigParser
  969. /// for each entry.
  970. class Subnets6ListConfigParser : public DhcpConfigParser {
  971. public:
  972. /// @brief constructor
  973. ///
  974. Subnets6ListConfigParser(const std::string&) {
  975. /// parameter name is ignored
  976. }
  977. /// @brief parses contents of the list
  978. ///
  979. /// Iterates over all entries on the list and creates a Subnet6ConfigParser
  980. /// for each entry.
  981. ///
  982. /// @param subnets_list pointer to a list of IPv6 subnets
  983. void build(ConstElementPtr subnets_list) {
  984. // No need to define FactoryMap here. There's only one type
  985. // used: Subnet6ConfigParser
  986. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  987. ParserPtr parser(new Subnet6ConfigParser("subnet"));
  988. parser->build(subnet);
  989. subnets_.push_back(parser);
  990. }
  991. }
  992. /// @brief commits subnets definitions.
  993. ///
  994. /// Iterates over all Subnet6 parsers. Each parser contains definitions
  995. /// of a single subnet and its parameters and commits each subnet separately.
  996. void commit() {
  997. // @todo: Implement more subtle reconfiguration than toss
  998. // the old one and replace with the new one.
  999. // remove old subnets
  1000. CfgMgr::instance().deleteSubnets6();
  1001. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  1002. subnet->commit();
  1003. }
  1004. }
  1005. /// @brief Returns Subnet6ListConfigParser object
  1006. /// @param param_name name of the parameter
  1007. /// @return Subnets6ListConfigParser object
  1008. static DhcpConfigParser* Factory(const std::string& param_name) {
  1009. return (new Subnets6ListConfigParser(param_name));
  1010. }
  1011. /// @brief collection of subnet parsers.
  1012. ParserCollection subnets_;
  1013. };
  1014. /// @brief creates global parsers
  1015. ///
  1016. /// This method creates global parsers that parse global parameters, i.e.
  1017. /// those that take format of Dhcp6/param1, Dhcp6/param2 and so forth.
  1018. ///
  1019. /// @param config_id pointer to received global configuration entry
  1020. /// @return parser for specified global DHCPv6 parameter
  1021. DhcpConfigParser* createGlobalDhcpConfigParser(const std::string& config_id) {
  1022. FactoryMap factories;
  1023. factories.insert(pair<string, ParserFactory*>(
  1024. "preferred-lifetime", Uint32Parser::Factory));
  1025. factories.insert(pair<string, ParserFactory*>(
  1026. "valid-lifetime", Uint32Parser::Factory));
  1027. factories.insert(pair<string, ParserFactory*>(
  1028. "renew-timer", Uint32Parser::Factory));
  1029. factories.insert(pair<string, ParserFactory*>(
  1030. "rebind-timer", Uint32Parser::Factory));
  1031. factories.insert(pair<string, ParserFactory*>(
  1032. "interface", InterfaceListConfigParser::Factory));
  1033. factories.insert(pair<string, ParserFactory*>(
  1034. "subnet6", Subnets6ListConfigParser::Factory));
  1035. factories.insert(pair<string, ParserFactory*>(
  1036. "option-data", OptionDataListParser::Factory));
  1037. factories.insert(pair<string, ParserFactory*>(
  1038. "version", StringParser::Factory));
  1039. FactoryMap::iterator f = factories.find(config_id);
  1040. if (f == factories.end()) {
  1041. // Used for debugging only.
  1042. // return new DebugParser(config_id);
  1043. isc_throw(NotImplemented,
  1044. "Parser error: Global configuration parameter not supported: "
  1045. << config_id);
  1046. }
  1047. return (f->second(config_id));
  1048. }
  1049. /// @brief configures DHCPv6 server
  1050. ///
  1051. /// This function is called every time a new configuration is received. The extra
  1052. /// parameter is a reference to DHCPv6 server component. It is currently not used
  1053. /// and CfgMgr::instance() is accessed instead.
  1054. ///
  1055. /// This method does not throw. It catches all exceptions and returns them as
  1056. /// reconfiguration statuses. It may return the following response codes:
  1057. /// 0 - configuration successful
  1058. /// 1 - malformed configuration (parsing failed)
  1059. /// 2 - logical error (parsing was successful, but the values are invalid)
  1060. ///
  1061. /// @param config_set a new configuration for DHCPv6 server
  1062. /// @return answer that contains result of reconfiguration
  1063. ConstElementPtr
  1064. configureDhcp6Server(Dhcpv6Srv& , ConstElementPtr config_set) {
  1065. if (!config_set) {
  1066. isc_throw(Dhcp6ConfigError,
  1067. "Null pointer is passed to configuration parser");
  1068. }
  1069. /// @todo: append most essential info here (like "2 new subnets configured")
  1070. string config_details;
  1071. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_COMMAND, DHCP6_CONFIG_START).arg(config_set->str());
  1072. ParserCollection parsers;
  1073. try {
  1074. BOOST_FOREACH(ConfigPair config_pair, config_set->mapValue()) {
  1075. ParserPtr parser(createGlobalDhcpConfigParser(config_pair.first));
  1076. parser->build(config_pair.second);
  1077. parsers.push_back(parser);
  1078. }
  1079. } catch (const isc::Exception& ex) {
  1080. ConstElementPtr answer = isc::config::createAnswer(1,
  1081. string("Configuration parsing failed:") + ex.what());
  1082. return (answer);
  1083. } catch (...) {
  1084. // for things like bad_cast in boost::lexical_cast
  1085. ConstElementPtr answer = isc::config::createAnswer(1,
  1086. string("Configuration parsing failed"));
  1087. }
  1088. try {
  1089. BOOST_FOREACH(ParserPtr parser, parsers) {
  1090. parser->commit();
  1091. }
  1092. }
  1093. catch (const isc::Exception& ex) {
  1094. ConstElementPtr answer = isc::config::createAnswer(2,
  1095. string("Configuration commit failed:") + ex.what());
  1096. return (answer);
  1097. } catch (...) {
  1098. // for things like bad_cast in boost::lexical_cast
  1099. ConstElementPtr answer = isc::config::createAnswer(2,
  1100. string("Configuration commit failed"));
  1101. }
  1102. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_COMPLETE).arg(config_details);
  1103. ConstElementPtr answer = isc::config::createAnswer(0, "Configuration commited.");
  1104. return (answer);
  1105. }
  1106. }; // end of isc::dhcp namespace
  1107. }; // end of isc namespace