config_parser.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. // Copyright (C) 2010 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 <asiolink/io_address.h>
  24. #include <cc/data.h>
  25. #include <config/ccsession.h>
  26. #include <log/logger_support.h>
  27. #include <dhcp/triplet.h>
  28. #include <dhcp/pool.h>
  29. #include <dhcp/subnet.h>
  30. #include <dhcp/cfgmgr.h>
  31. #include <dhcp6/config_parser.h>
  32. #include <dhcp6/dhcp6_log.h>
  33. using namespace std;
  34. using namespace isc::data;
  35. using namespace isc::asiolink;
  36. namespace isc {
  37. namespace dhcp {
  38. /// @brief auxiliary type used for storing element name and its parser
  39. typedef pair<string, ConstElementPtr> ConfigPair;
  40. /// @brief a factory method that will create a parser for a given element name
  41. typedef DhcpConfigParser* ParserFactory(const std::string& config_id);
  42. /// @brief a collection of factories that creates parsers for specified element names
  43. typedef std::map<std::string, ParserFactory*> FactoryMap;
  44. /// @brief a collection of elements that store uint32 values (e.g. renew-timer = 900)
  45. typedef std::map<string, uint32_t> Uint32Storage;
  46. /// @brief a collection of elements that store string values
  47. typedef std::map<string, string> StringStorage;
  48. /// @brief a collection of pools
  49. ///
  50. /// That type is used as intermediate storage, when pools are parsed, but there is
  51. /// no subnet object created yet to store them.
  52. typedef std::vector<Pool6Ptr> PoolStorage;
  53. /// @brief Global uint32 parameters that will be used as defaults.
  54. Uint32Storage uint32_defaults;
  55. /// @brief global string parameters that will be used as defaults.
  56. StringStorage string_defaults;
  57. /// @brief a dummy configuration parser
  58. ///
  59. /// It is a debugging parser. It does not configure anything,
  60. /// will accept any configuration and will just print it out
  61. /// on commit. Useful for debugging existing configurations and
  62. /// adding new ones.
  63. class DebugParser : public DhcpConfigParser {
  64. public:
  65. /// @brief Constructor
  66. ///
  67. /// See \ref DhcpConfigParser class for details.
  68. ///
  69. /// @param param_name name of the parsed parameter
  70. DebugParser(const std::string& param_name)
  71. :param_name_(param_name) {
  72. }
  73. /// @brief builds parameter value
  74. ///
  75. /// See \ref DhcpConfigParser class for details.
  76. ///
  77. /// @param new_config pointer to the new configuration
  78. virtual void build(ConstElementPtr new_config) {
  79. std::cout << "Build for token: [" << param_name_ << "] = ["
  80. << value_->str() << "]" << std::endl;
  81. value_ = new_config;
  82. }
  83. /// @brief pretends to apply the configuration
  84. ///
  85. /// This is a method required by base class. It pretends to apply the
  86. /// configuration, but in fact it only prints the parameter out.
  87. ///
  88. /// See \ref DhcpConfigParser class for details.
  89. virtual void commit() {
  90. // Debug message. The whole DebugParser class is used only for parser
  91. // debugging, and is not used in production code. It is very convenient
  92. // to keep it around. Please do not turn this cout into logger calls.
  93. std::cout << "Commit for token: [" << param_name_ << "] = ["
  94. << value_->str() << "]" << std::endl;
  95. }
  96. /// @brief factory that constructs DebugParser objects
  97. ///
  98. /// @param param_name name of the parameter to be parsed
  99. static DhcpConfigParser* Factory(const std::string& param_name) {
  100. return (new DebugParser(param_name));
  101. }
  102. protected:
  103. /// name of the parsed parameter
  104. std::string param_name_;
  105. /// pointer to the actual value of the parameter
  106. ConstElementPtr value_;
  107. };
  108. /// @brief Configuration parser for uint32 parameters
  109. ///
  110. /// This class is a generic parser that is able to handle any uint32 integer
  111. /// type. By default it stores the value in external global container
  112. /// (uint32_defaults). If used in smaller scopes (e.g. to parse parameters
  113. /// in subnet config), it can be pointed to a different storage, using
  114. /// setStorage() method. This class follows the parser interface, laid out
  115. /// in its base class, \ref DhcpConfigParser.
  116. ///
  117. /// For overview of usability of this generic purpose parser, see
  118. /// \ref dhcpv6-config-inherit page.
  119. class Uint32Parser : public DhcpConfigParser {
  120. public:
  121. /// @brief constructor for Uint32Parser
  122. /// @param param_name name of the configuration parameter being parsed
  123. Uint32Parser(const std::string& param_name)
  124. :storage_(&uint32_defaults), param_name_(param_name) {
  125. }
  126. /// @brief builds parameter value
  127. ///
  128. /// Parses configuration entry and stores it in a storage. See
  129. /// \ref setStorage() for details.
  130. ///
  131. /// @param value pointer to the content of parsed values
  132. virtual void build(ConstElementPtr value) {
  133. try {
  134. value_ = boost::lexical_cast<uint32_t>(value->str());
  135. } catch (const boost::bad_lexical_cast &) {
  136. isc_throw(BadValue, "Failed to parse value " << value->str()
  137. << " as unsigned 32-bit integer.");
  138. }
  139. storage_->insert(pair<string, uint32_t>(param_name_, value_));
  140. }
  141. /// @brief does nothing
  142. ///
  143. /// This method is required for all parser. The value itself
  144. /// is not commited anywhere. Higher level parsers are expected to
  145. /// use values stored in the storage, e.g. renew-timer for a given
  146. /// subnet is stored in subnet-specific storage. It is not commited
  147. /// here, but is rather used by \ref Subnet6Parser when constructing
  148. /// the subnet.
  149. virtual void commit() {
  150. }
  151. /// @brief factory that constructs Uint32Parser objects
  152. ///
  153. /// @param param_name name of the parameter to be parsed
  154. static DhcpConfigParser* Factory(const std::string& param_name) {
  155. return (new Uint32Parser(param_name));
  156. }
  157. /// @brief sets storage for value of this parameter
  158. ///
  159. /// See \ref dhcpv6-config-inherit for details.
  160. ///
  161. /// @param storage pointer to the storage container
  162. void setStorage(Uint32Storage* storage) {
  163. storage_ = storage;
  164. }
  165. protected:
  166. /// pointer to the storage, where parsed value will be stored
  167. Uint32Storage* storage_;
  168. /// name of the parameter to be parsed
  169. std::string param_name_;
  170. /// the actual parsed value
  171. uint32_t value_;
  172. };
  173. /// @brief Configuration parser for string parameters
  174. ///
  175. /// This class is a generic parser that is able to handle any string
  176. /// parameter. By default it stores the value in external global container
  177. /// (string_defaults). If used in smaller scopes (e.g. to parse parameters
  178. /// in subnet config), it can be pointed to a different storage, using
  179. /// setStorage() method. This class follows the parser interface, laid out
  180. /// in its base class, \ref DhcpConfigParser.
  181. ///
  182. /// For overview of usability of this generic purpose parser, see
  183. /// \ref dhcpv6-config-inherit page.
  184. class StringParser : public DhcpConfigParser {
  185. public:
  186. /// @brief constructor for StringParser
  187. /// @param param_name name of the configuration parameter being parsed
  188. StringParser(const std::string& param_name)
  189. :storage_(&string_defaults), param_name_(param_name) {
  190. }
  191. /// @brief parses parameter value
  192. ///
  193. /// Parses configuration entry and stored it in storage. See
  194. /// \ref setStorage() for details.
  195. ///
  196. /// @param value pointer to the content of parsed values
  197. virtual void build(ConstElementPtr value) {
  198. value_ = value->str();
  199. boost::erase_all(value_, "\"");
  200. storage_->insert(pair<string, string>(param_name_, value_));
  201. }
  202. /// @brief does nothing
  203. ///
  204. /// This method is required for all parser. The value itself
  205. /// is not commited anywhere. Higher level parsers are expected to
  206. /// use values stored in the storage, e.g. renew-timer for a given
  207. /// subnet is stored in subnet-specific storage. It is not commited
  208. /// here, but is rather used by its parent parser when constructing
  209. /// an object, e.g. the subnet.
  210. virtual void commit() {
  211. }
  212. /// @brief factory that constructs StringParser objects
  213. ///
  214. /// @param param_name name of the parameter to be parsed
  215. static DhcpConfigParser* Factory(const std::string& param_name) {
  216. return (new StringParser(param_name));
  217. }
  218. /// @brief sets storage for value of this parameter
  219. ///
  220. /// See \ref dhcpv6-config-inherit for details.
  221. ///
  222. /// @param storage pointer to the storage container
  223. void setStorage(StringStorage* storage) {
  224. storage_ = storage;
  225. }
  226. protected:
  227. /// pointer to the storage, where parsed value will be stored
  228. StringStorage* storage_;
  229. /// name of the parameter to be parsed
  230. std::string param_name_;
  231. /// the actual parsed value
  232. std::string value_;
  233. };
  234. /// @brief parser for interface list definition
  235. ///
  236. /// This parser handles Dhcp6/interface entry.
  237. /// It contains a list of network interfaces that the server listens on.
  238. /// In particular, it can contain an entry called "all" or "any" that
  239. /// designates all interfaces.
  240. ///
  241. /// It is useful for parsing Dhcp6/interface parameter.
  242. class InterfaceListConfigParser : public DhcpConfigParser {
  243. public:
  244. /// @brief constructor
  245. ///
  246. /// As this is a dedicated parser, it must be used to parse
  247. /// "interface" parameter only. All other types will throw exception.
  248. ///
  249. /// @param param_name name of the configuration parameter being parsed
  250. InterfaceListConfigParser(const std::string& param_name) {
  251. if (param_name != "interface") {
  252. isc_throw(NotImplemented, "Internal error. Interface configuration "
  253. "parser called for the wrong parameter: " << param_name);
  254. }
  255. }
  256. /// @brief parses parameters value
  257. ///
  258. /// Parses configuration entry (list of parameters) and stores it in
  259. /// storage. See \ref setStorage() for details.
  260. ///
  261. /// @param value pointer to the content of parsed values
  262. virtual void build(ConstElementPtr value) {
  263. BOOST_FOREACH(ConstElementPtr iface, value->listValue()) {
  264. interfaces_.push_back(iface->str());
  265. }
  266. }
  267. /// @brief commits interfaces list configuration
  268. virtual void commit() {
  269. /// @todo: Implement per interface listening. Currently always listening
  270. /// on all interfaces.
  271. }
  272. /// @brief factory that constructs InterfaceListConfigParser objects
  273. ///
  274. /// @param param_name name of the parameter to be parsed
  275. static DhcpConfigParser* Factory(const std::string& param_name) {
  276. return (new InterfaceListConfigParser(param_name));
  277. }
  278. protected:
  279. /// contains list of network interfaces
  280. vector<string> interfaces_;
  281. };
  282. /// @brief parser for pool definition
  283. ///
  284. /// This parser handles pool definitions, i.e. a list of entries of one
  285. /// of two syntaxes: min-max and prefix/len. Pool6 objects are created
  286. /// and stored in chosen PoolStorage container.
  287. ///
  288. /// As there are no default values for pool, setStorage() must be called
  289. /// before build(). Otherwise exception will be thrown.
  290. ///
  291. /// It is useful for parsing Dhcp6/subnet6[X]/pool parameters.
  292. class PoolParser : public DhcpConfigParser {
  293. public:
  294. /// @brief constructor.
  295. PoolParser(const std::string& /*param_name*/)
  296. :pools_(NULL) {
  297. // ignore parameter name, it is always Dhcp6/subnet6[X]/pool
  298. }
  299. /// @brief parses the actual list
  300. ///
  301. /// This method parses the actual list of interfaces.
  302. /// No validation is done at this stage, everything is interpreted as
  303. /// interface name.
  304. void build(ConstElementPtr pools_list) {
  305. // setStorage() should have been called before build
  306. if (!pools_) {
  307. isc_throw(NotImplemented, "Parser logic error. No pool storage set,"
  308. " but pool parser asked to parse pools");
  309. }
  310. BOOST_FOREACH(ConstElementPtr text_pool, pools_list->listValue()) {
  311. // That should be a single pool representation. It should contain
  312. // text is form prefix/len or first - last. Note that spaces
  313. // are allowed
  314. string txt = text_pool->stringValue();
  315. // first let's remove any whitespaces
  316. boost::erase_all(txt, " "); // space
  317. boost::erase_all(txt, "\t"); // tabulation
  318. // Is this prefix/len notation?
  319. size_t pos = txt.find("/");
  320. if (pos != string::npos) {
  321. IOAddress addr("::");
  322. uint8_t len = 0;
  323. try {
  324. addr = IOAddress(txt.substr(0, pos));
  325. // start with the first character after /
  326. string prefix_len = txt.substr(pos + 1);
  327. // It is lexical cast to int and then downcast to uint8_t.
  328. // Direct cast to uint8_t (which is really an unsigned char)
  329. // will result in interpreting the first digit as output
  330. // value and throwing exception if length is written on two
  331. // digits (because there are extra characters left over).
  332. // No checks for values over 128. Range correctness will
  333. // be checked in Pool6 constructor.
  334. len = boost::lexical_cast<int>(prefix_len);
  335. } catch (...) {
  336. isc_throw(Dhcp6ConfigError, "Failed to parse pool "
  337. "definition: " << text_pool->stringValue());
  338. }
  339. Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, addr, len));
  340. pools_->push_back(pool);
  341. continue;
  342. }
  343. // Is this min-max notation?
  344. pos = txt.find("-");
  345. if (pos != string::npos) {
  346. // using min-max notation
  347. IOAddress min(txt.substr(0,pos - 1));
  348. IOAddress max(txt.substr(pos + 1));
  349. Pool6Ptr pool(new Pool6(Pool6::TYPE_IA, min, max));
  350. pools_->push_back(pool);
  351. continue;
  352. }
  353. isc_throw(Dhcp6ConfigError, "Failed to parse pool definition:"
  354. << text_pool->stringValue() <<
  355. ". Does not contain - (for min-max) nor / (prefix/len)");
  356. }
  357. }
  358. /// @brief sets storage for value of this parameter
  359. ///
  360. /// See \ref dhcpv6-config-inherit for details.
  361. ///
  362. /// @param storage pointer to the storage container
  363. void setStorage(PoolStorage* storage) {
  364. pools_ = storage;
  365. }
  366. /// @brief does nothing.
  367. ///
  368. /// This method is required for all parser. The value itself
  369. /// is not commited anywhere. Higher level parsers (for subnet) are expected
  370. /// to use values stored in the storage.
  371. virtual void commit() {}
  372. /// @brief factory that constructs PoolParser objects
  373. ///
  374. /// @param param_name name of the parameter to be parsed
  375. static DhcpConfigParser* Factory(const std::string& param_name) {
  376. return (new PoolParser(param_name));
  377. }
  378. protected:
  379. /// @brief pointer to the actual Pools storage
  380. ///
  381. /// That is typically a storage somewhere in Subnet parser
  382. /// (an upper level parser).
  383. PoolStorage* pools_;
  384. };
  385. /// @brief this class parses a single subnet
  386. ///
  387. /// This class parses the whole subnet definition. It creates parsers
  388. /// for received configuration parameters as needed.
  389. class Subnet6ConfigParser : public DhcpConfigParser {
  390. public:
  391. /// @brief constructor
  392. Subnet6ConfigParser(const std::string& ) {
  393. // The parameter should always be "subnet", but we don't check here
  394. // against it in case some wants to reuse this parser somewhere.
  395. }
  396. /// @brief parses parameter value
  397. ///
  398. /// @param subnet pointer to the content of subnet definition
  399. void build(ConstElementPtr subnet) {
  400. BOOST_FOREACH(ConfigPair param, subnet->mapValue()) {
  401. ParserPtr parser(createSubnet6ConfigParser(param.first));
  402. // if this is an Uint32 parser, tell it to store the values
  403. // in values_, rather than in global storage
  404. boost::shared_ptr<Uint32Parser> uintParser =
  405. boost::dynamic_pointer_cast<Uint32Parser>(parser);
  406. if (uintParser) {
  407. uintParser->setStorage(&uint32_values_);
  408. } else {
  409. boost::shared_ptr<StringParser> stringParser =
  410. boost::dynamic_pointer_cast<StringParser>(parser);
  411. if (stringParser) {
  412. stringParser->setStorage(&string_values_);
  413. } else {
  414. boost::shared_ptr<PoolParser> poolParser =
  415. boost::dynamic_pointer_cast<PoolParser>(parser);
  416. if (poolParser) {
  417. poolParser->setStorage(&pools_);
  418. }
  419. }
  420. }
  421. parser->build(param.second);
  422. parsers_.push_back(parser);
  423. }
  424. // Ok, we now have subnet parsed
  425. }
  426. /// @brief commits received configuration.
  427. ///
  428. /// This method does most of the configuration. Many other parsers are just
  429. /// storing the values that are actually consumed here. Pool definitions
  430. /// created in other parsers are used here and added to newly created Subnet6
  431. /// objects. Subnet6 are then added to DHCP CfgMgr.
  432. void commit() {
  433. StringStorage::const_iterator it = string_values_.find("subnet");
  434. if (it == string_values_.end()) {
  435. isc_throw(Dhcp6ConfigError,
  436. "Mandatory subnet definition in subnet missing");
  437. }
  438. string subnet_txt = it->second;
  439. boost::erase_all(subnet_txt, " ");
  440. boost::erase_all(subnet_txt, "\t");
  441. size_t pos = subnet_txt.find("/");
  442. if (pos == string::npos) {
  443. isc_throw(Dhcp6ConfigError,
  444. "Invalid subnet syntax (prefix/len expected):" << it->second);
  445. }
  446. IOAddress addr(subnet_txt.substr(0, pos));
  447. uint8_t len = boost::lexical_cast<unsigned int>(subnet_txt.substr(pos + 1));
  448. Triplet<uint32_t> t1 = getParam("renew-timer");
  449. Triplet<uint32_t> t2 = getParam("rebind-timer");
  450. Triplet<uint32_t> pref = getParam("preferred-lifetime");
  451. Triplet<uint32_t> valid = getParam("valid-lifetime");
  452. /// @todo: Convert this to logger once the parser is working reliably
  453. stringstream tmp;
  454. tmp << addr.toText() << "/" << (int)len
  455. << " with params t1=" << t1 << ", t2=" << t2 << ", pref="
  456. << pref << ", valid=" << valid;
  457. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_NEW_SUBNET).arg(tmp.str());
  458. Subnet6Ptr subnet(new Subnet6(addr, len, t1, t2, pref, valid));
  459. for (PoolStorage::iterator it = pools_.begin(); it != pools_.end(); ++it) {
  460. subnet->addPool6(*it);
  461. }
  462. CfgMgr::instance().addSubnet6(subnet);
  463. }
  464. protected:
  465. /// @brief creates parsers for entries in subnet definition
  466. ///
  467. /// @todo Add subnet-specific things here (e.g. subnet-specific options)
  468. ///
  469. /// @param config_id name od the entry
  470. /// @return parser object for specified entry name
  471. DhcpConfigParser* createSubnet6ConfigParser(const std::string& config_id) {
  472. FactoryMap factories;
  473. factories.insert(pair<string, ParserFactory*>(
  474. "preferred-lifetime", Uint32Parser::Factory));
  475. factories.insert(pair<string, ParserFactory*>(
  476. "valid-lifetime", Uint32Parser::Factory));
  477. factories.insert(pair<string, ParserFactory*>(
  478. "renew-timer", Uint32Parser::Factory));
  479. factories.insert(pair<string, ParserFactory*>(
  480. "rebind-timer", Uint32Parser::Factory));
  481. factories.insert(pair<string, ParserFactory*>(
  482. "subnet", StringParser::Factory));
  483. factories.insert(pair<string, ParserFactory*>(
  484. "pool", PoolParser::Factory));
  485. FactoryMap::iterator f = factories.find(config_id);
  486. if (f == factories.end()) {
  487. // Used for debugging only.
  488. // return new DebugParser(config_id);
  489. isc_throw(NotImplemented,
  490. "Parser error: Subnet6 parameter not supported: "
  491. << config_id);
  492. }
  493. return (f->second(config_id));
  494. }
  495. /// @brief returns value for a given parameter (after using inheritance)
  496. ///
  497. /// This method implements inheritance. For a given parameter name, it first
  498. /// checks if there is a global value for it and overwrites it with specific
  499. /// value if such value was defined in subnet.
  500. ///
  501. /// @param name name of the parameter
  502. /// @return triplet with the parameter name
  503. Triplet<uint32_t> getParam(const std::string& name) {
  504. uint32_t value = 0;
  505. bool found = false;
  506. Uint32Storage::iterator global = uint32_defaults.find(name);
  507. if (global != uint32_defaults.end()) {
  508. value = global->second;
  509. found = true;
  510. }
  511. Uint32Storage::iterator local = uint32_values_.find(name);
  512. if (local != uint32_values_.end()) {
  513. value = local->second;
  514. found = true;
  515. }
  516. if (found) {
  517. return (Triplet<uint32_t>(value));
  518. } else {
  519. isc_throw(Dhcp6ConfigError, "Mandatory parameter " << name
  520. << " missing (no global default and no subnet-"
  521. << "specific value)");
  522. }
  523. }
  524. /// storage for subnet-specific uint32 values
  525. Uint32Storage uint32_values_;
  526. /// storage for subnet-specific integer values
  527. StringStorage string_values_;
  528. /// storage for pools belonging to this subnet
  529. PoolStorage pools_;
  530. /// parsers are stored here
  531. ParserCollection parsers_;
  532. };
  533. /// @brief this class parses list of subnets
  534. ///
  535. /// This is a wrapper parser that handles the whole list of Subnet6
  536. /// definitions. It iterates over all entries and creates Subnet6ConfigParser
  537. /// for each entry.
  538. class Subnets6ListConfigParser : public DhcpConfigParser {
  539. public:
  540. /// @brief constructor
  541. ///
  542. Subnets6ListConfigParser(const std::string&) {
  543. /// parameter name is ignored
  544. }
  545. /// @brief parses contents of the list
  546. ///
  547. /// Iterates over all entries on the list and creates Subnet6ConfigParser
  548. /// for each entry.
  549. ///
  550. /// @param subnets_list pointer to a list of IPv6 subnets
  551. void build(ConstElementPtr subnets_list) {
  552. // No need to define FactoryMap here. There's only one type
  553. // used: Subnet6ConfigParser
  554. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  555. ParserPtr parser(new Subnet6ConfigParser("subnet"));
  556. parser->build(subnet);
  557. subnets_.push_back(parser);
  558. }
  559. }
  560. /// @brief commits subnets definitions.
  561. ///
  562. /// Iterates over all Subnet6 parsers. Each parser contains definitions
  563. /// of a single subnet and its parameters and commits each subnet separately.
  564. void commit() {
  565. // @todo: Implement more subtle reconfiguration than toss
  566. // the old one and replace with the new one.
  567. // remove old subnets
  568. CfgMgr::instance().deleteSubnets6();
  569. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  570. subnet->commit();
  571. }
  572. }
  573. /// @brief Returns Subnet6ListConfigParser object
  574. /// @param param_name name of the parameter
  575. /// @return Subnets6ListConfigParser object
  576. static DhcpConfigParser* Factory(const std::string& param_name) {
  577. return (new Subnets6ListConfigParser(param_name));
  578. }
  579. /// @brief collection of subnet parsers.
  580. ParserCollection subnets_;
  581. };
  582. /// @brief creates global parsers
  583. ///
  584. /// This method creates global parsers that parse global parameters, i.e.
  585. /// those that take format of Dhcp6/param1, Dhcp6/param2 and so forth.
  586. ///
  587. /// @param config_id pointer to received global configuration entry
  588. /// @return parser for specified global DHCPv6 parameter
  589. DhcpConfigParser* createGlobalDhcpConfigParser(const std::string& config_id) {
  590. FactoryMap factories;
  591. //
  592. factories.insert(pair<string, ParserFactory*>(
  593. "preferred-lifetime", Uint32Parser::Factory));
  594. factories.insert(pair<string, ParserFactory*>(
  595. "valid-lifetime", Uint32Parser::Factory));
  596. factories.insert(pair<string, ParserFactory*>(
  597. "renew-timer", Uint32Parser::Factory));
  598. factories.insert(pair<string, ParserFactory*>(
  599. "rebind-timer", Uint32Parser::Factory));
  600. factories.insert(pair<string, ParserFactory*>(
  601. "interface", InterfaceListConfigParser::Factory));
  602. factories.insert(pair<string, ParserFactory*>(
  603. "subnet6", Subnets6ListConfigParser::Factory));
  604. factories.insert(pair<string, ParserFactory*>(
  605. "version", StringParser::Factory));
  606. FactoryMap::iterator f = factories.find(config_id);
  607. if (f == factories.end()) {
  608. // Used for debugging only.
  609. // return new DebugParser(config_id);
  610. isc_throw(NotImplemented,
  611. "Parser error: Global configuration parameter not supported: "
  612. << config_id);
  613. }
  614. return (f->second(config_id));
  615. }
  616. /// @brief configures DHCPv6 server
  617. ///
  618. /// This function is called every time a new configuration is received. The extra
  619. /// parameter is a reference to DHCPv6 server component. It is currently not used
  620. /// and CfgMgr::instance() is accessed instead.
  621. ///
  622. /// This method does not throw. It catches all exceptions and returns them as
  623. /// reconfiguration statuses. It may return the following response codes:
  624. /// 0 - configuration successful
  625. /// 1 - malformed configuration (parsing failed)
  626. /// 2 - logical error (parsing was successful, but the values are invalid)
  627. ///
  628. /// @param config_set a new configuration for DHCPv6 server
  629. /// @return answer that contains result of reconfiguration
  630. ConstElementPtr
  631. configureDhcp6Server(Dhcpv6Srv& , ConstElementPtr config_set) {
  632. if (!config_set) {
  633. isc_throw(Dhcp6ConfigError,
  634. "Null pointer is passed to configuration parser");
  635. }
  636. /// @todo: append most essential info here (like "2 new subnets configured")
  637. string config_details;
  638. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_COMMAND, DHCP6_CONFIG_START).arg(config_set->str());
  639. ParserCollection parsers;
  640. try {
  641. BOOST_FOREACH(ConfigPair config_pair, config_set->mapValue()) {
  642. ParserPtr parser(createGlobalDhcpConfigParser(config_pair.first));
  643. parser->build(config_pair.second);
  644. parsers.push_back(parser);
  645. }
  646. } catch (const isc::Exception& ex) {
  647. ConstElementPtr answer = isc::config::createAnswer(1,
  648. string("Configuration parsing failed:") + ex.what());
  649. return (answer);
  650. } catch (...) {
  651. // for things like bad_cast in boost::lexical_cast
  652. ConstElementPtr answer = isc::config::createAnswer(1,
  653. string("Configuration parsing failed"));
  654. }
  655. try {
  656. BOOST_FOREACH(ParserPtr parser, parsers) {
  657. parser->commit();
  658. }
  659. }
  660. catch (const isc::Exception& ex) {
  661. ConstElementPtr answer = isc::config::createAnswer(2,
  662. string("Configuration commit failed:") + ex.what());
  663. return (answer);
  664. } catch (...) {
  665. // for things like bad_cast in boost::lexical_cast
  666. ConstElementPtr answer = isc::config::createAnswer(2,
  667. string("Configuration commit failed"));
  668. }
  669. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_COMPLETE).arg(config_details);
  670. ConstElementPtr answer = isc::config::createAnswer(0, "Configuration commited.");
  671. return (answer);
  672. }
  673. }; // end of isc::dhcp namespace
  674. }; // end of isc namespace