config_parser.cc 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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 <stdint.h>
  15. #include <iostream>
  16. #include <vector>
  17. #include <map>
  18. #include <boost/foreach.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/scoped_ptr.hpp>
  21. #include <boost/lexical_cast.hpp>
  22. #include <boost/algorithm/string.hpp>
  23. #include <util/encode/hex.h>
  24. #include <asiolink/io_address.h>
  25. #include <cc/data.h>
  26. #include <config/ccsession.h>
  27. #include <log/logger_support.h>
  28. #include <dhcp/libdhcp++.h>
  29. #include <dhcp/triplet.h>
  30. #include <dhcp/pool.h>
  31. #include <dhcp/subnet.h>
  32. #include <dhcp/cfgmgr.h>
  33. #include <dhcp6/config_parser.h>
  34. #include <dhcp6/dhcp6_log.h>
  35. using namespace std;
  36. using namespace isc::data;
  37. using namespace isc::asiolink;
  38. namespace isc {
  39. namespace dhcp {
  40. /// @brief auxiliary type used for storing 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 creates 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. /// That 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 to search for
  56. /// options using 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. /// It 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 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 new element.
  174. (*storage_)[param_name_] = value_;
  175. }
  176. /// @brief does nothing
  177. ///
  178. /// This method is required for all parser. 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 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
  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 stored 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 new element.
  236. (*storage_)[param_name_] = value_;
  237. }
  238. /// @brief does nothing
  239. ///
  240. /// This method is required for all parser. 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 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 is 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 lexical 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 parser. 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. /// That 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
  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 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 values.
  498. ///
  499. /// This function iterates over all options configured for a particular
  500. /// subnet or globally and moves them to the storage. If there are any options
  501. /// already present in the storage they will be replaced if options with
  502. /// the same code are present in the intermediate storage.
  503. ///
  504. /// @throw isc::InvalidOperation if failed to set pointer to storage failed
  505. /// to call build() prior to commit. If that happens the data in the storage
  506. /// remain not modified.
  507. virtual void commit() {
  508. if (options_ == NULL) {
  509. isc_throw(isc::InvalidOperation, "Parser logic error: storage must be set before "
  510. "commiting option data.");
  511. } else if (!option_descriptor_.option) {
  512. // Before we can commit the new option should be configured. If it is not
  513. // than somebody must have called commit() before build().
  514. isc_throw(isc::InvalidOperation, "Parser logic error: no option has been configured and"
  515. " thus there is nothing to commit. Has build() been called?");
  516. }
  517. uint16_t opt_type = option_descriptor_.option->getType();
  518. Subnet::OptionContainerTypeIndex& idx = options_->get<1>();
  519. // Try to find options with the particular option code in the main
  520. // storage. If found, remove these options because they will be
  521. // replaced with new one.
  522. Subnet::OptionContainerTypeRange range =
  523. idx.equal_range(opt_type);
  524. if (std::distance(range.first, range.second) > 0) {
  525. idx.erase(range.first, range.second);
  526. }
  527. // Append new option to the main storage.
  528. options_->push_back(option_descriptor_);
  529. }
  530. /// @brief Set storage for the parser.
  531. ///
  532. /// Sets storage for the parser. This storage points to the
  533. /// vector of options and is used by multiple instances of
  534. /// OptionDataParser. Each instance creates exactly one object
  535. /// of dhcp::Option or derived type and appends it to this
  536. /// storage.
  537. ///
  538. /// @param storage pointer to the options storage
  539. void setStorage(OptionStorage* storage) {
  540. options_ = storage;
  541. }
  542. private:
  543. /// @brief Create option instance.
  544. ///
  545. /// Creates an instance of an option and adds it to the provided
  546. /// options storage. If the option data parsed by \ref build function
  547. /// are invalid or insufficient it emits exception.
  548. ///
  549. /// @warning this function does not check if options_ storage pointer
  550. /// is intitialized but this is not needed here because it is checked in
  551. /// \ref build function.
  552. ///
  553. /// @throw Dhcp6ConfigError if parameters provided in the configuration
  554. /// are invalid.
  555. void createOption() {
  556. // Option code is held in the uint32_t storage but is supposed to
  557. // be uint16_t value. We need to check that value in the configuration
  558. // does not exceed range of uint16_t and is not zero.
  559. uint32_t option_code = getUint32Param("code");
  560. if (option_code == 0) {
  561. isc_throw(Dhcp6ConfigError, "Parser error: value of 'code' must not"
  562. << " be equal to zero. Option code '0' is reserved in"
  563. << " DHCPv6.");
  564. } else if (option_code > std::numeric_limits<uint16_t>::max()) {
  565. isc_throw(Dhcp6ConfigError, "Parser error: value of 'code' must not"
  566. << " exceed " << std::numeric_limits<uint16_t>::max());
  567. }
  568. // Check the option name has been specified, is non-empty and does not
  569. // contain spaces.
  570. // @todo possibly some more restrictions apply here?
  571. std::string option_name = getStringParam("name");
  572. if (option_name.empty()) {
  573. isc_throw(Dhcp6ConfigError, "Parser error: option name must not be"
  574. << " empty");
  575. } else if (option_name.find(" ") != std::string::npos) {
  576. isc_throw(Dhcp6ConfigError, "Parser error: option name must not contain"
  577. << " spaces");
  578. }
  579. // Get option data from the configuration database ('data' field).
  580. // Option data is specified by the user as case insensitive string
  581. // of hexadecimal digits for each option.
  582. std::string option_data = getStringParam("data");
  583. // Transform string of hexadecimal digits into binary format.
  584. std::vector<uint8_t> binary;
  585. try {
  586. util::encode::decodeHex(option_data, binary);
  587. } catch (...) {
  588. isc_throw(Dhcp6ConfigError, "Parser error: option data is not a valid"
  589. << " string of hexadecimal digits: " << option_data);
  590. }
  591. // Get all existing DHCPv6 option definitions. The one that matches
  592. // our option will be picked and used to create it.
  593. OptionDefContainer option_defs = LibDHCP::getOptionDefs(Option::V6);
  594. // Get search index #1. It allows searching for options definitions
  595. // using option type value.
  596. const OptionDefContainerTypeIndex& idx = option_defs.get<1>();
  597. // Get all option definitions matching option code we want to create.
  598. const OptionDefContainerTypeRange& range = idx.equal_range(option_code);
  599. size_t num_defs = std::distance(range.first, range.second);
  600. OptionPtr option;
  601. // Currently we do not allow duplicated definitions and if there are
  602. // any duplicates we issue internal server error.
  603. if (num_defs > 1) {
  604. isc_throw(Dhcp6ConfigError, "Internal error: currently it is not"
  605. << " supported to initialize multiple option definitions"
  606. << " for the same option code. This will be supported once"
  607. << " there option spaces are implemented.");
  608. } else if (num_defs == 0) {
  609. // @todo We have a limited set of option definitions intiialized at the moment.
  610. // In the future we want to initialize option definitions for all options.
  611. // Consequently error will be issued if option definition does not exist
  612. // for a particular option code. For now it is ok to create generic option
  613. // if definition does not exist.
  614. OptionPtr option(new Option(Option::V6, static_cast<uint16_t>(option_code),
  615. binary));
  616. // Created option is stored in option_descriptor_ class member until commit
  617. // stage when it inserted into the main storage. If option with the same
  618. // code exists in main storage already it replaces old option.
  619. option_descriptor_.option = option;
  620. option_descriptor_.persistent = false;
  621. } else {
  622. // We have exactly one option definition for the particular option code.
  623. // use it to create option instance.
  624. const OptionDefinitionPtr& def = *(range.first);
  625. // getFactory should never return NULL pointer.
  626. Option::Factory* factory = def->getFactory();
  627. assert(factory != NULL);
  628. try {
  629. OptionPtr option = factory(Option::V6, option_code, binary);
  630. Subnet::OptionDescriptor desc(option, false);
  631. option_descriptor_.option = option;
  632. option_descriptor_.persistent = false;
  633. } catch (const isc::Exception& ex) {
  634. isc_throw(Dhcp6ConfigError, "Parser error: option data does not match"
  635. << " option definition (code " << option_code << "): "
  636. << ex.what());
  637. }
  638. }
  639. }
  640. /// @brief Get a parameter from the strings storage.
  641. ///
  642. /// @param param_id parameter identifier.
  643. /// @throw Dhcp6ConfigError if parameter has not been found.
  644. std::string getStringParam(const std::string& param_id) const {
  645. StringStorage::const_iterator param = string_values_.find(param_id);
  646. if (param == string_values_.end()) {
  647. isc_throw(Dhcp6ConfigError, "Parser error: option-data parameter"
  648. << " '" << param_id << "' not specified");
  649. }
  650. return (param->second);
  651. }
  652. /// @brief Get a parameter from the uint32 values storage.
  653. ///
  654. /// @param param_id parameter identifier.
  655. /// @throw Dhcp6ConfigError if parameter has not been found.
  656. uint32_t getUint32Param(const std::string& param_id) const {
  657. Uint32Storage::const_iterator param = uint32_values_.find(param_id);
  658. if (param == uint32_values_.end()) {
  659. isc_throw(Dhcp6ConfigError, "Parser error: option-data parameter"
  660. << " '" << param_id << "' not specified");
  661. }
  662. return (param->second);
  663. }
  664. /// Storage for uint32 values (e.g. option code).
  665. Uint32Storage uint32_values_;
  666. /// Storage for string values (e.g. option name or data).
  667. StringStorage string_values_;
  668. /// Pointer to options storage. This storage is provided by
  669. /// the calling class and is shared by all OptionDataParser objects.
  670. OptionStorage* options_;
  671. /// Option descriptor holds newly configured option.
  672. Subnet::OptionDescriptor option_descriptor_;
  673. };
  674. /// @brief Parser for option data values with ina subnet.
  675. ///
  676. /// This parser iterates over all entries that define options
  677. /// data for a particular subnet and creates a collection of options.
  678. /// If parsing is successful, all these options are added to the Subnet
  679. /// object.
  680. class OptionDataListParser : public DhcpConfigParser {
  681. public:
  682. /// @brief Constructor.
  683. ///
  684. /// Unless otherwise specified, parsed options will be stored in
  685. /// a global option containers (option_default). That storage location
  686. /// is overriden on a subnet basis.
  687. OptionDataListParser(const std::string&)
  688. : options_(&option_defaults), local_options_() { }
  689. /// @brief Parses entries that define options' data for a subnet.
  690. ///
  691. /// This method iterates over all entries that define option data
  692. /// for options within a single subnet and creates options' instances.
  693. ///
  694. /// @param option_data_list pointer to a list of options' data sets.
  695. /// @throw Dhcp6ConfigError if option parsing failed.
  696. void build(ConstElementPtr option_data_list) {
  697. BOOST_FOREACH(ConstElementPtr option_value, option_data_list->listValue()) {
  698. boost::shared_ptr<OptionDataParser> parser(new OptionDataParser("option-data"));
  699. // options_ member will hold instances of all options thus
  700. // each OptionDataParser takes it as a storage.
  701. parser->setStorage(&local_options_);
  702. // Build the instance of a single option.
  703. parser->build(option_value);
  704. // Store a parser as it will be used to commit.
  705. parsers_.push_back(parser);
  706. }
  707. }
  708. /// @brief Set storage for option instances.
  709. ///
  710. /// @param storage pointer to options storage.
  711. void setStorage(OptionStorage* storage) {
  712. options_ = storage;
  713. }
  714. /// @brief Commit all option values.
  715. ///
  716. /// This function invokes commit for all option values.
  717. void commit() {
  718. BOOST_FOREACH(ParserPtr parser, parsers_) {
  719. parser->commit();
  720. }
  721. std::swap(local_options_, *options_);
  722. }
  723. /// @brief Create DhcpDataListParser object
  724. ///
  725. /// @param param_name param name.
  726. ///
  727. /// @return DhcpConfigParser object.
  728. static DhcpConfigParser* Factory(const std::string& param_name) {
  729. return (new OptionDataListParser(param_name));
  730. }
  731. /// Intermediate option storage
  732. OptionStorage local_options_;
  733. /// Pointer to options instances storage.
  734. OptionStorage* options_;
  735. /// Collection of parsers;
  736. ParserCollection parsers_;
  737. };
  738. /// @brief this class parses a single subnet
  739. ///
  740. /// This class parses the whole subnet definition. It creates parsers
  741. /// for received configuration parameters as needed.
  742. class Subnet6ConfigParser : public DhcpConfigParser {
  743. public:
  744. /// @brief constructor
  745. Subnet6ConfigParser(const std::string& ) {
  746. // The parameter should always be "subnet", but we don't check here
  747. // against it in case some wants to reuse this parser somewhere.
  748. }
  749. /// @brief parses parameter value
  750. ///
  751. /// @param subnet pointer to the content of subnet definition
  752. void build(ConstElementPtr subnet) {
  753. BOOST_FOREACH(ConfigPair param, subnet->mapValue()) {
  754. ParserPtr parser(createSubnet6ConfigParser(param.first));
  755. // The actual type of the parser is unknown here. We have to discover
  756. // parser type here to invoke corresponding setStorage function on it.
  757. // We discover parser type by trying to cast the parser to various
  758. // parser types and checking which one was successful. For this one
  759. // a setStorage and build methods are invoked.
  760. // Try uint32 type parser.
  761. if (buildParser<Uint32Parser, Uint32Storage >(parser, uint32_values_,
  762. param.second)) {
  763. // Storage set, build invoked on the parser, proceed with
  764. // next configuration element.
  765. continue;
  766. }
  767. // Try string type parser.
  768. if (buildParser<StringParser, StringStorage >(parser, string_values_,
  769. param.second)) {
  770. continue;
  771. }
  772. // Try pools parser.
  773. if (buildParser<PoolParser, PoolStorage >(parser, pools_,
  774. param.second)) {
  775. continue;
  776. }
  777. // Try option data parser.
  778. if (buildParser<OptionDataListParser, OptionStorage >(parser, options_,
  779. param.second)) {
  780. continue;
  781. }
  782. }
  783. // Ok, we now have subnet parsed
  784. }
  785. /// @brief commits received configuration.
  786. ///
  787. /// This method does most of the configuration. Many other parsers are just
  788. /// storing the values that are actually consumed here. Pool definitions
  789. /// created in other parsers are used here and added to newly created Subnet6
  790. /// objects. Subnet6 are then added to DHCP CfgMgr.
  791. void commit() {
  792. // Invoke commit on all sub-data parsers.
  793. BOOST_FOREACH(ParserPtr parser, parsers_) {
  794. parser->commit();
  795. }
  796. StringStorage::const_iterator it = string_values_.find("subnet");
  797. if (it == string_values_.end()) {
  798. isc_throw(Dhcp6ConfigError,
  799. "Mandatory subnet definition in subnet missing");
  800. }
  801. string subnet_txt = it->second;
  802. boost::erase_all(subnet_txt, " ");
  803. boost::erase_all(subnet_txt, "\t");
  804. size_t pos = subnet_txt.find("/");
  805. if (pos == string::npos) {
  806. isc_throw(Dhcp6ConfigError,
  807. "Invalid subnet syntax (prefix/len expected):" << it->second);
  808. }
  809. IOAddress addr(subnet_txt.substr(0, pos));
  810. uint8_t len = boost::lexical_cast<unsigned int>(subnet_txt.substr(pos + 1));
  811. Triplet<uint32_t> t1 = getParam("renew-timer");
  812. Triplet<uint32_t> t2 = getParam("rebind-timer");
  813. Triplet<uint32_t> pref = getParam("preferred-lifetime");
  814. Triplet<uint32_t> valid = getParam("valid-lifetime");
  815. /// @todo: Convert this to logger once the parser is working reliably
  816. stringstream tmp;
  817. tmp << addr.toText() << "/" << (int)len
  818. << " with params t1=" << t1 << ", t2=" << t2 << ", pref="
  819. << pref << ", valid=" << valid;
  820. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_NEW_SUBNET).arg(tmp.str());
  821. Subnet6Ptr subnet(new Subnet6(addr, len, t1, t2, pref, valid));
  822. for (PoolStorage::iterator it = pools_.begin(); it != pools_.end(); ++it) {
  823. subnet->addPool6(*it);
  824. }
  825. const Subnet::OptionContainer& options = subnet->getOptions();
  826. const Subnet::OptionContainerTypeIndex& idx = options.get<1>();
  827. // Add subnet specific options.
  828. BOOST_FOREACH(Subnet::OptionDescriptor desc, options_) {
  829. Subnet::OptionContainerTypeRange range = idx.equal_range(desc.option->getType());
  830. if (std::distance(range.first, range.second) > 0) {
  831. LOG_WARN(dhcp6_logger, DHCP6_CONFIG_OPTION_DUPLICATE)
  832. .arg(desc.option->getType()).arg(addr.toText());
  833. }
  834. subnet->addOption(desc.option);
  835. }
  836. // Check all global options and add them to the subnet object if
  837. // they have been configured in the global scope. If they have been
  838. // configured in the subnet scope we don't add global option because
  839. // the one configured in the subnet scope always takes precedence.
  840. BOOST_FOREACH(Subnet::OptionDescriptor desc, option_defaults) {
  841. // Get all options specified locally in the subnet and having
  842. // code equal to global option's code.
  843. Subnet::OptionContainerTypeRange range = idx.equal_range(desc.option->getType());
  844. // @todo: In the future we will be searching for options using either
  845. // option code or namespace. Currently we have only the option
  846. // code available so if there is at least one option found with the
  847. // specific code we don't add globally configured option.
  848. // @todo with this code the first globally configured option
  849. // with the given code will be added to a subnet. We may
  850. // want to issue warning about dropping configuration of
  851. // global option if one already exsist.
  852. if (std::distance(range.first, range.second) == 0) {
  853. subnet->addOption(desc.option);
  854. }
  855. }
  856. CfgMgr::instance().addSubnet6(subnet);
  857. }
  858. private:
  859. /// @brief Set storage for a parser and invoke build.
  860. ///
  861. /// This helper method casts the provided parser pointer to specified
  862. /// type. If cast is successful it sets the corresponding storage for
  863. /// this parser, invokes build on it and save the parser.
  864. ///
  865. /// @tparam T parser type to which parser argument should be cast.
  866. /// @tparam Y storage type for the specified parser type.
  867. /// @param parser parser on which build must be invoked.
  868. /// @param storage reference to a storage that will be set for a parser.
  869. /// @param subnet subnet element read from the configuration and being parsed.
  870. /// @return true if parser pointer was successfully cast to specialized
  871. /// parser type provided as Y.
  872. template<typename T, typename Y>
  873. bool buildParser(const ParserPtr& parser, Y& storage, const ConstElementPtr& subnet) {
  874. // We need to cast to T in order to set storage for the parser.
  875. boost::shared_ptr<T> cast_parser = boost::dynamic_pointer_cast<T>(parser);
  876. // It is common that this cast is not successful because we try to cast to all
  877. // supported parser types as we don't know the type of a parser in advance.
  878. if (cast_parser) {
  879. // Cast, successful so we go ahead with setting storage and actual parse.
  880. cast_parser->setStorage(&storage);
  881. parser->build(subnet);
  882. parsers_.push_back(parser);
  883. // We indicate that cast was successful so as the calling function
  884. // may skip attempts to cast to other parser types and proceed to
  885. // next element.
  886. return (true);
  887. }
  888. // It was not successful. Indicate that another parser type
  889. // should be tried.
  890. return (false);
  891. }
  892. /// @brief creates parsers for entries in subnet definition
  893. ///
  894. /// @todo Add subnet-specific things here (e.g. subnet-specific options)
  895. ///
  896. /// @param config_id name od the entry
  897. /// @return parser object for specified entry name
  898. DhcpConfigParser* createSubnet6ConfigParser(const std::string& config_id) {
  899. FactoryMap factories;
  900. factories.insert(pair<string, ParserFactory*>(
  901. "preferred-lifetime", Uint32Parser::Factory));
  902. factories.insert(pair<string, ParserFactory*>(
  903. "valid-lifetime", Uint32Parser::Factory));
  904. factories.insert(pair<string, ParserFactory*>(
  905. "renew-timer", Uint32Parser::Factory));
  906. factories.insert(pair<string, ParserFactory*>(
  907. "rebind-timer", Uint32Parser::Factory));
  908. factories.insert(pair<string, ParserFactory*>(
  909. "subnet", StringParser::Factory));
  910. factories.insert(pair<string, ParserFactory*>(
  911. "pool", PoolParser::Factory));
  912. factories.insert(pair<string, ParserFactory*>(
  913. "option-data", OptionDataListParser::Factory));
  914. FactoryMap::iterator f = factories.find(config_id);
  915. if (f == factories.end()) {
  916. // Used for debugging only.
  917. // return new DebugParser(config_id);
  918. isc_throw(NotImplemented,
  919. "Parser error: Subnet6 parameter not supported: "
  920. << config_id);
  921. }
  922. return (f->second(config_id));
  923. }
  924. /// @brief returns value for a given parameter (after using inheritance)
  925. ///
  926. /// This method implements inheritance. For a given parameter name, it first
  927. /// checks if there is a global value for it and overwrites it with specific
  928. /// value if such value was defined in subnet.
  929. ///
  930. /// @param name name of the parameter
  931. /// @return triplet with the parameter name
  932. Triplet<uint32_t> getParam(const std::string& name) {
  933. uint32_t value = 0;
  934. bool found = false;
  935. Uint32Storage::iterator global = uint32_defaults.find(name);
  936. if (global != uint32_defaults.end()) {
  937. value = global->second;
  938. found = true;
  939. }
  940. Uint32Storage::iterator local = uint32_values_.find(name);
  941. if (local != uint32_values_.end()) {
  942. value = local->second;
  943. found = true;
  944. }
  945. if (found) {
  946. return (Triplet<uint32_t>(value));
  947. } else {
  948. isc_throw(Dhcp6ConfigError, "Mandatory parameter " << name
  949. << " missing (no global default and no subnet-"
  950. << "specific value)");
  951. }
  952. }
  953. /// storage for subnet-specific uint32 values
  954. Uint32Storage uint32_values_;
  955. /// storage for subnet-specific integer values
  956. StringStorage string_values_;
  957. /// storage for pools belonging to this subnet
  958. PoolStorage pools_;
  959. /// storage for options belonging to this subnet
  960. OptionStorage options_;
  961. /// parsers are stored here
  962. ParserCollection parsers_;
  963. };
  964. /// @brief this class parses list of subnets
  965. ///
  966. /// This is a wrapper parser that handles the whole list of Subnet6
  967. /// definitions. It iterates over all entries and creates Subnet6ConfigParser
  968. /// for each entry.
  969. class Subnets6ListConfigParser : public DhcpConfigParser {
  970. public:
  971. /// @brief constructor
  972. ///
  973. Subnets6ListConfigParser(const std::string&) {
  974. /// parameter name is ignored
  975. }
  976. /// @brief parses contents of the list
  977. ///
  978. /// Iterates over all entries on the list and creates Subnet6ConfigParser
  979. /// for each entry.
  980. ///
  981. /// @param subnets_list pointer to a list of IPv6 subnets
  982. void build(ConstElementPtr subnets_list) {
  983. // No need to define FactoryMap here. There's only one type
  984. // used: Subnet6ConfigParser
  985. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  986. ParserPtr parser(new Subnet6ConfigParser("subnet"));
  987. parser->build(subnet);
  988. subnets_.push_back(parser);
  989. }
  990. }
  991. /// @brief commits subnets definitions.
  992. ///
  993. /// Iterates over all Subnet6 parsers. Each parser contains definitions
  994. /// of a single subnet and its parameters and commits each subnet separately.
  995. void commit() {
  996. // @todo: Implement more subtle reconfiguration than toss
  997. // the old one and replace with the new one.
  998. // remove old subnets
  999. CfgMgr::instance().deleteSubnets6();
  1000. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  1001. subnet->commit();
  1002. }
  1003. }
  1004. /// @brief Returns Subnet6ListConfigParser object
  1005. /// @param param_name name of the parameter
  1006. /// @return Subnets6ListConfigParser object
  1007. static DhcpConfigParser* Factory(const std::string& param_name) {
  1008. return (new Subnets6ListConfigParser(param_name));
  1009. }
  1010. /// @brief collection of subnet parsers.
  1011. ParserCollection subnets_;
  1012. };
  1013. /// @brief creates global parsers
  1014. ///
  1015. /// This method creates global parsers that parse global parameters, i.e.
  1016. /// those that take format of Dhcp6/param1, Dhcp6/param2 and so forth.
  1017. ///
  1018. /// @param config_id pointer to received global configuration entry
  1019. /// @return parser for specified global DHCPv6 parameter
  1020. DhcpConfigParser* createGlobalDhcpConfigParser(const std::string& config_id) {
  1021. FactoryMap factories;
  1022. factories.insert(pair<string, ParserFactory*>(
  1023. "preferred-lifetime", Uint32Parser::Factory));
  1024. factories.insert(pair<string, ParserFactory*>(
  1025. "valid-lifetime", Uint32Parser::Factory));
  1026. factories.insert(pair<string, ParserFactory*>(
  1027. "renew-timer", Uint32Parser::Factory));
  1028. factories.insert(pair<string, ParserFactory*>(
  1029. "rebind-timer", Uint32Parser::Factory));
  1030. factories.insert(pair<string, ParserFactory*>(
  1031. "interface", InterfaceListConfigParser::Factory));
  1032. factories.insert(pair<string, ParserFactory*>(
  1033. "subnet6", Subnets6ListConfigParser::Factory));
  1034. factories.insert(pair<string, ParserFactory*>(
  1035. "option-data", OptionDataListParser::Factory));
  1036. factories.insert(pair<string, ParserFactory*>(
  1037. "version", StringParser::Factory));
  1038. FactoryMap::iterator f = factories.find(config_id);
  1039. if (f == factories.end()) {
  1040. // Used for debugging only.
  1041. // return new DebugParser(config_id);
  1042. isc_throw(NotImplemented,
  1043. "Parser error: Global configuration parameter not supported: "
  1044. << config_id);
  1045. }
  1046. return (f->second(config_id));
  1047. }
  1048. /// @brief configures DHCPv6 server
  1049. ///
  1050. /// This function is called every time a new configuration is received. The extra
  1051. /// parameter is a reference to DHCPv6 server component. It is currently not used
  1052. /// and CfgMgr::instance() is accessed instead.
  1053. ///
  1054. /// This method does not throw. It catches all exceptions and returns them as
  1055. /// reconfiguration statuses. It may return the following response codes:
  1056. /// 0 - configuration successful
  1057. /// 1 - malformed configuration (parsing failed)
  1058. /// 2 - logical error (parsing was successful, but the values are invalid)
  1059. ///
  1060. /// @param config_set a new configuration for DHCPv6 server
  1061. /// @return answer that contains result of reconfiguration
  1062. ConstElementPtr
  1063. configureDhcp6Server(Dhcpv6Srv& , ConstElementPtr config_set) {
  1064. if (!config_set) {
  1065. isc_throw(Dhcp6ConfigError,
  1066. "Null pointer is passed to configuration parser");
  1067. }
  1068. /// @todo: append most essential info here (like "2 new subnets configured")
  1069. string config_details;
  1070. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_COMMAND, DHCP6_CONFIG_START).arg(config_set->str());
  1071. ParserCollection parsers;
  1072. try {
  1073. BOOST_FOREACH(ConfigPair config_pair, config_set->mapValue()) {
  1074. ParserPtr parser(createGlobalDhcpConfigParser(config_pair.first));
  1075. parser->build(config_pair.second);
  1076. parsers.push_back(parser);
  1077. }
  1078. } catch (const isc::Exception& ex) {
  1079. ConstElementPtr answer = isc::config::createAnswer(1,
  1080. string("Configuration parsing failed:") + ex.what());
  1081. return (answer);
  1082. } catch (...) {
  1083. // for things like bad_cast in boost::lexical_cast
  1084. ConstElementPtr answer = isc::config::createAnswer(1,
  1085. string("Configuration parsing failed"));
  1086. }
  1087. try {
  1088. BOOST_FOREACH(ParserPtr parser, parsers) {
  1089. parser->commit();
  1090. }
  1091. }
  1092. catch (const isc::Exception& ex) {
  1093. ConstElementPtr answer = isc::config::createAnswer(2,
  1094. string("Configuration commit failed:") + ex.what());
  1095. return (answer);
  1096. } catch (...) {
  1097. // for things like bad_cast in boost::lexical_cast
  1098. ConstElementPtr answer = isc::config::createAnswer(2,
  1099. string("Configuration commit failed"));
  1100. }
  1101. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_COMPLETE).arg(config_details);
  1102. ConstElementPtr answer = isc::config::createAnswer(0, "Configuration commited.");
  1103. return (answer);
  1104. }
  1105. }; // end of isc::dhcp namespace
  1106. }; // end of isc namespace