config_parser.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. // Copyright (C) 2012-2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #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 <dhcp/iface_mgr.h>
  21. #include <dhcpsrv/cfgmgr.h>
  22. #include <dhcpsrv/dbaccess_parser.h>
  23. #include <dhcpsrv/dhcp_config_parser.h>
  24. #include <dhcpsrv/dhcp_parsers.h>
  25. #include <dhcpsrv/pool.h>
  26. #include <dhcpsrv/subnet.h>
  27. #include <dhcpsrv/triplet.h>
  28. #include <log/logger_support.h>
  29. #include <util/encode/hex.h>
  30. #include <util/strutil.h>
  31. #include <boost/algorithm/string.hpp>
  32. #include <boost/foreach.hpp>
  33. #include <boost/lexical_cast.hpp>
  34. #include <boost/scoped_ptr.hpp>
  35. #include <boost/shared_ptr.hpp>
  36. #include <iostream>
  37. #include <map>
  38. #include <vector>
  39. #include <stdint.h>
  40. using namespace std;
  41. using namespace isc;
  42. using namespace isc::data;
  43. using namespace isc::dhcp;
  44. using namespace isc::asiolink;
  45. namespace {
  46. // Pointers to various parser objects.
  47. typedef boost::shared_ptr<BooleanParser> BooleanParserPtr;
  48. typedef boost::shared_ptr<StringParser> StringParserPtr;
  49. typedef boost::shared_ptr<Uint32Parser> Uint32ParserPtr;
  50. /// @brief Parser for DHCP6 option data value.
  51. ///
  52. /// This parser parses configuration entries that specify value of
  53. /// a single option specific to DHCP6. It provides the DHCP6-specific
  54. /// implementation of the abstract class OptionDataParser.
  55. class Dhcp6OptionDataParser : public OptionDataParser {
  56. public:
  57. /// @brief Constructor.
  58. ///
  59. /// @param dummy first param, option names are always "Dhcp6/option-data[n]"
  60. /// @param options is the option storage in which to store the parsed option
  61. /// upon "commit".
  62. /// @param global_context is a pointer to the global context which
  63. /// stores global scope parameters, options, option defintions.
  64. Dhcp6OptionDataParser(const std::string&, OptionStoragePtr options,
  65. ParserContextPtr global_context)
  66. :OptionDataParser("", options, global_context) {
  67. }
  68. /// @brief static factory method for instantiating Dhcp4OptionDataParsers
  69. ///
  70. /// @param param_name name of the parameter to be parsed.
  71. /// @param options storage where the parameter value is to be stored.
  72. /// @param global_context is a pointer to the global context which
  73. /// stores global scope parameters, options, option defintions.
  74. /// @return returns a pointer to a new OptionDataParser. Caller is
  75. /// is responsible for deleting it when it is no longer needed.
  76. static OptionDataParser* factory(const std::string& param_name,
  77. OptionStoragePtr options, ParserContextPtr global_context) {
  78. return (new Dhcp6OptionDataParser(param_name, options, global_context));
  79. }
  80. protected:
  81. /// @brief Finds an option definition within the server's option space
  82. ///
  83. /// Given an option space and an option code, find the correpsonding
  84. /// option defintion within the server's option defintion storage.
  85. ///
  86. /// @param option_space name of the parameter option space
  87. /// @param option_code numeric value of the parameter to find
  88. /// @return OptionDefintionPtr of the option defintion or an
  89. /// empty OptionDefinitionPtr if not found.
  90. /// @throw DhcpConfigError if the option space requested is not valid
  91. /// for this server.
  92. virtual OptionDefinitionPtr findServerSpaceOptionDefinition (
  93. std::string& option_space, uint32_t option_code) {
  94. OptionDefinitionPtr def;
  95. if (option_space == "dhcp6" &&
  96. LibDHCP::isStandardOption(Option::V6, option_code)) {
  97. def = LibDHCP::getOptionDef(Option::V6, option_code);
  98. } else if (option_space == "dhcp4") {
  99. isc_throw(DhcpConfigError, "'dhcp4' option space name is reserved"
  100. << " for DHCPv4 server");
  101. }
  102. return def;
  103. }
  104. };
  105. /// @brief Parser for IPv4 pool definitions.
  106. ///
  107. /// This is the IPv6 derivation of the PoolParser class and handles pool
  108. /// definitions, i.e. a list of entries of one of two syntaxes: min-max and
  109. /// prefix/len for IPv6 pools. Pool6 objects are created and stored in chosen
  110. /// PoolStorage container.
  111. ///
  112. /// It is useful for parsing Dhcp6/subnet6[X]/pool parameters.
  113. class Pool6Parser : public PoolParser {
  114. public:
  115. /// @brief Constructor.
  116. ///
  117. /// @param param_name name of the parameter. Note, it is passed through
  118. /// but unused, parameter is currently always "Dhcp6/subnet6[X]/pool"
  119. /// @param pools storage container in which to store the parsed pool
  120. /// upon "commit"
  121. Pool6Parser(const std::string& param_name, PoolStoragePtr pools)
  122. :PoolParser(param_name, pools) {
  123. }
  124. protected:
  125. /// @brief Creates a Pool6 object given a IPv6 prefix and the prefix length.
  126. ///
  127. /// @param addr is the IPv6 prefix of the pool.
  128. /// @param len is the prefix length.
  129. /// @param ptype is the type of IPv6 pool (Pool6::Pool6Type). Note this is
  130. /// passed in as an int32_t and cast to Pool6Type to accommodate a
  131. /// polymorphic interface.
  132. /// @return returns a PoolPtr to the new Pool4 object.
  133. PoolPtr poolMaker (IOAddress &addr, uint32_t len, int32_t ptype)
  134. {
  135. return (PoolPtr(new Pool6(static_cast<isc::dhcp::Pool6::Pool6Type>
  136. (ptype), addr, len)));
  137. }
  138. /// @brief Creates a Pool6 object given starting and ending IPv6 addresses.
  139. ///
  140. /// @param min is the first IPv6 address in the pool.
  141. /// @param max is the last IPv6 address in the pool.
  142. /// @param ptype is the type of IPv6 pool (Pool6::Pool6Type). Note this is
  143. /// passed in as an int32_t and cast to Pool6Type to accommodate a
  144. /// polymorphic interface.
  145. /// @return returns a PoolPtr to the new Pool4 object.
  146. PoolPtr poolMaker (IOAddress &min, IOAddress &max, int32_t ptype)
  147. {
  148. return (PoolPtr(new Pool6(static_cast<isc::dhcp::Pool6::Pool6Type>
  149. (ptype), min, max)));
  150. }
  151. };
  152. /// @brief This class parses a single IPv6 subnet.
  153. ///
  154. /// This is the IPv6 derivation of the SubnetConfigParser class and it parses
  155. /// the whole subnet definition. It creates parsersfor received configuration
  156. /// parameters as needed.
  157. class Subnet6ConfigParser : public SubnetConfigParser {
  158. public:
  159. /// @brief Constructor
  160. ///
  161. /// @param ignored first parameter
  162. /// stores global scope parameters, options, option defintions.
  163. Subnet6ConfigParser(const std::string&)
  164. :SubnetConfigParser("", globalContext()) {
  165. }
  166. /// @brief Adds the created subnet to a server's configuration.
  167. /// @throw throws Unexpected if dynamic cast fails.
  168. void commit() {
  169. if (subnet_) {
  170. Subnet6Ptr sub6ptr = boost::dynamic_pointer_cast<Subnet6>(subnet_);
  171. if (!sub6ptr) {
  172. // If we hit this, it is a programming error.
  173. isc_throw(Unexpected,
  174. "Invalid cast in Subnet4ConfigParser::commit");
  175. }
  176. isc::dhcp::CfgMgr::instance().addSubnet6(sub6ptr);
  177. }
  178. }
  179. protected:
  180. /// @brief creates parsers for entries in subnet definition
  181. ///
  182. /// @param config_id name of the entry
  183. ///
  184. /// @return parser object for specified entry name. Note the caller is
  185. /// responsible for deleting the parser created.
  186. /// @throw isc::dhcp::DhcpConfigError if trying to create a parser
  187. /// for unknown config element
  188. DhcpConfigParser* createSubnetConfigParser(const std::string& config_id) {
  189. DhcpConfigParser* parser = NULL;
  190. if ((config_id.compare("preferred-lifetime") == 0) ||
  191. (config_id.compare("valid-lifetime") == 0) ||
  192. (config_id.compare("renew-timer") == 0) ||
  193. (config_id.compare("rebind-timer") == 0)) {
  194. parser = new Uint32Parser(config_id, uint32_values_);
  195. } else if ((config_id.compare("subnet") == 0) ||
  196. (config_id.compare("interface") == 0) ||
  197. (config_id.compare("interface-id") == 0)) {
  198. parser = new StringParser(config_id, string_values_);
  199. } else if (config_id.compare("pool") == 0) {
  200. parser = new Pool6Parser(config_id, pools_);
  201. } else if (config_id.compare("option-data") == 0) {
  202. parser = new OptionDataListParser(config_id, options_,
  203. global_context_,
  204. Dhcp6OptionDataParser::factory);
  205. } else {
  206. isc_throw(NotImplemented,
  207. "parser error: Subnet6 parameter not supported: " << config_id);
  208. }
  209. return (parser);
  210. }
  211. /// @brief Determines if the given option space name and code describe
  212. /// a standard option for the DHCP6 server.
  213. ///
  214. /// @param option_space is the name of the option space to consider
  215. /// @param code is the numeric option code to consider
  216. /// @return returns true if the space and code are part of the server's
  217. /// standard options.
  218. bool isServerStdOption(std::string option_space, uint32_t code) {
  219. return ((option_space.compare("dhcp6") == 0)
  220. && LibDHCP::isStandardOption(Option::V6, code));
  221. }
  222. /// @brief Returns the option definition for a given option code from
  223. /// the DHCP6 server's standard set of options.
  224. /// @param code is the numeric option code of the desired option definition.
  225. /// @return returns a pointer the option definition
  226. OptionDefinitionPtr getServerStdOptionDefinition (uint32_t code) {
  227. return (LibDHCP::getOptionDef(Option::V6, code));
  228. }
  229. /// @brief Issues a DHCP6 server specific warning regarding duplicate subnet
  230. /// options.
  231. ///
  232. /// @param code is the numeric option code of the duplicate option
  233. /// @param addr is the subnet address
  234. /// @todo A means to know the correct logger and perhaps a common
  235. /// message would allow this message to be emitted by the base class.
  236. virtual void duplicate_option_warning(uint32_t code,
  237. isc::asiolink::IOAddress& addr) {
  238. LOG_WARN(dhcp6_logger, DHCP6_CONFIG_OPTION_DUPLICATE)
  239. .arg(code).arg(addr.toText());
  240. }
  241. /// @brief Instantiates the IPv6 Subnet based on a given IPv6 address
  242. /// and prefix length.
  243. ///
  244. /// @param addr is IPv6 prefix of the subnet.
  245. /// @param len is the prefix length
  246. void initSubnet(isc::asiolink::IOAddress addr, uint8_t len) {
  247. // Get all 'time' parameters using inheritance.
  248. // If the subnet-specific value is defined then use it, else
  249. // use the global value. The global value must always be
  250. // present. If it is not, it is an internal error and exception
  251. // is thrown.
  252. Triplet<uint32_t> t1 = getParam("renew-timer");
  253. Triplet<uint32_t> t2 = getParam("rebind-timer");
  254. Triplet<uint32_t> pref = getParam("preferred-lifetime");
  255. Triplet<uint32_t> valid = getParam("valid-lifetime");
  256. // Get interface-id option content. For now we support string
  257. // represenation only
  258. std::string ifaceid;
  259. try {
  260. ifaceid = string_values_->getParam("interface-id");
  261. } catch (const DhcpConfigError &) {
  262. // interface-id is not mandatory
  263. }
  264. // Specifying both interface for locally reachable subnets and
  265. // interface id for relays is mutually exclusive. Need to test for
  266. // this condition.
  267. if (!ifaceid.empty()) {
  268. std::string iface;
  269. try {
  270. iface = string_values_->getParam("interface");
  271. } catch (const DhcpConfigError &) {
  272. // iface not mandatory
  273. }
  274. if (!iface.empty()) {
  275. isc_throw(isc::dhcp::DhcpConfigError,
  276. "parser error: interface (defined for locally reachable "
  277. "subnets) and interface-id (defined for subnets reachable"
  278. " via relays) cannot be defined at the same time for "
  279. "subnet " << addr.toText() << "/" << (int)len);
  280. }
  281. }
  282. stringstream tmp;
  283. tmp << addr.toText() << "/" << static_cast<int>(len)
  284. << " with params t1=" << t1 << ", t2=" << t2 << ", pref="
  285. << pref << ", valid=" << valid;
  286. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_NEW_SUBNET).arg(tmp.str());
  287. // Create a new subnet.
  288. Subnet6* subnet6 = new Subnet6(addr, len, t1, t2, pref, valid);
  289. // Configure interface-id for remote interfaces, if defined
  290. if (!ifaceid.empty()) {
  291. OptionBuffer tmp(ifaceid.begin(), ifaceid.end());
  292. OptionPtr opt(new Option(Option::V6, D6O_INTERFACE_ID, tmp));
  293. subnet6->setInterfaceId(opt);
  294. }
  295. subnet_.reset(subnet6);
  296. }
  297. };
  298. /// @brief this class parses a list of DHCP6 subnets
  299. ///
  300. /// This is a wrapper parser that handles the whole list of Subnet6
  301. /// definitions. It iterates over all entries and creates Subnet6ConfigParser
  302. /// for each entry.
  303. class Subnets6ListConfigParser : public DhcpConfigParser {
  304. public:
  305. /// @brief constructor
  306. ///
  307. /// @param dummy first argument, always ignored. All parsers accept a
  308. /// string parameter "name" as their first argument.
  309. Subnets6ListConfigParser(const std::string&) {
  310. }
  311. /// @brief parses contents of the list
  312. ///
  313. /// Iterates over all entries on the list and creates a Subnet6ConfigParser
  314. /// for each entry.
  315. ///
  316. /// @param subnets_list pointer to a list of IPv6 subnets
  317. void build(ConstElementPtr subnets_list) {
  318. BOOST_FOREACH(ConstElementPtr subnet, subnets_list->listValue()) {
  319. ParserPtr parser(new Subnet6ConfigParser("subnet"));
  320. parser->build(subnet);
  321. subnets_.push_back(parser);
  322. }
  323. }
  324. /// @brief commits subnets definitions.
  325. ///
  326. /// Iterates over all Subnet6 parsers. Each parser contains definitions of
  327. /// a single subnet and its parameters and commits each subnet separately.
  328. void commit() {
  329. // @todo: Implement more subtle reconfiguration than toss
  330. // the old one and replace with the new one.
  331. // remove old subnets
  332. isc::dhcp::CfgMgr::instance().deleteSubnets6();
  333. BOOST_FOREACH(ParserPtr subnet, subnets_) {
  334. subnet->commit();
  335. }
  336. }
  337. /// @brief Returns Subnet6ListConfigParser object
  338. /// @param param_name name of the parameter
  339. /// @return Subnets6ListConfigParser object
  340. static DhcpConfigParser* factory(const std::string& param_name) {
  341. return (new Subnets6ListConfigParser(param_name));
  342. }
  343. /// @brief collection of subnet parsers.
  344. ParserCollection subnets_;
  345. };
  346. } // anonymous namespace
  347. namespace isc {
  348. namespace dhcp {
  349. /// @brief creates global parsers
  350. ///
  351. /// This method creates global parsers that parse global parameters, i.e.
  352. /// those that take format of Dhcp6/param1, Dhcp6/param2 and so forth.
  353. ///
  354. /// @param config_id pointer to received global configuration entry
  355. /// @return parser for specified global DHCPv6 parameter
  356. /// @throw NotImplemented if trying to create a parser for unknown config
  357. /// element
  358. DhcpConfigParser* createGlobal6DhcpConfigParser(const std::string& config_id) {
  359. DhcpConfigParser* parser = NULL;
  360. if ((config_id.compare("preferred-lifetime") == 0) ||
  361. (config_id.compare("valid-lifetime") == 0) ||
  362. (config_id.compare("renew-timer") == 0) ||
  363. (config_id.compare("rebind-timer") == 0)) {
  364. parser = new Uint32Parser(config_id,
  365. globalContext()->uint32_values_);
  366. } else if (config_id.compare("interfaces") == 0) {
  367. parser = new InterfaceListConfigParser(config_id);
  368. } else if (config_id.compare("subnet6") == 0) {
  369. parser = new Subnets6ListConfigParser(config_id);
  370. } else if (config_id.compare("option-data") == 0) {
  371. parser = new OptionDataListParser(config_id,
  372. globalContext()->options_,
  373. globalContext(),
  374. Dhcp6OptionDataParser::factory);
  375. } else if (config_id.compare("option-def") == 0) {
  376. parser = new OptionDefListParser(config_id,
  377. globalContext()->option_defs_);
  378. } else if (config_id.compare("version") == 0) {
  379. parser = new StringParser(config_id,
  380. globalContext()->string_values_);
  381. } else if (config_id.compare("lease-database") == 0) {
  382. parser = new DbAccessParser(config_id);
  383. } else if (config_id.compare("hooks-libraries") == 0) {
  384. parser = new HooksLibrariesParser(config_id);
  385. } else {
  386. isc_throw(NotImplemented,
  387. "Parser error: Global configuration parameter not supported: "
  388. << config_id);
  389. }
  390. return (parser);
  391. }
  392. isc::data::ConstElementPtr
  393. configureDhcp6Server(Dhcpv6Srv&, isc::data::ConstElementPtr config_set) {
  394. if (!config_set) {
  395. ConstElementPtr answer = isc::config::createAnswer(1,
  396. string("Can't parse NULL config"));
  397. return (answer);
  398. }
  399. /// @todo: Append most essential info here (like "2 new subnets configured")
  400. string config_details;
  401. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_COMMAND,
  402. DHCP6_CONFIG_START).arg(config_set->str());
  403. // Some of the values specified in the configuration depend on
  404. // other values. Typically, the values in the subnet6 structure
  405. // depend on the global values. Also, option values configuration
  406. // must be performed after the option definitions configurations.
  407. // Thus we group parsers and will fire them in the right order:
  408. // all parsers other than subnet6 and option-data parser,
  409. // option-data parser, subnet6 parser.
  410. ParserCollection independent_parsers;
  411. ParserPtr subnet_parser;
  412. ParserPtr option_parser;
  413. ParserPtr iface_parser;
  414. // Some of the parsers alter state of the system that can't easily
  415. // be undone. (Or alter it in a way such that undoing the change
  416. // has the same risk of failure as doing the change.)
  417. ParserPtr hooks_parser;
  418. // The subnet parsers implement data inheritance by directly
  419. // accessing global storage. For this reason the global data
  420. // parsers must store the parsed data into global storages
  421. // immediately. This may cause data inconsistency if the
  422. // parsing operation fails after the global storage has been
  423. // modified. We need to preserve the original global data here
  424. // so as we can rollback changes when an error occurs.
  425. ParserContext original_context(*globalContext());
  426. // answer will hold the result.
  427. ConstElementPtr answer;
  428. // rollback informs whether error occured and original data
  429. // have to be restored to global storages.
  430. bool rollback = false;
  431. // config_pair holds ther details of the current parser when iterating over
  432. // the parsers. It is declared outside the loop so in case of error, the
  433. // name of the failing parser can be retrieved within the "catch" clause.
  434. ConfigPair config_pair;
  435. try {
  436. // Make parsers grouping.
  437. const std::map<std::string, ConstElementPtr>& values_map =
  438. config_set->mapValue();
  439. BOOST_FOREACH(config_pair, values_map) {
  440. ParserPtr parser(createGlobal6DhcpConfigParser(config_pair.first));
  441. LOG_DEBUG(dhcp6_logger, DBG_DHCP6_DETAIL, DHCP6_PARSER_CREATED)
  442. .arg(config_pair.first);
  443. if (config_pair.first == "subnet6") {
  444. subnet_parser = parser;
  445. } else if (config_pair.first == "option-data") {
  446. option_parser = parser;
  447. } else if (config_pair.first == "hooks-libraries") {
  448. // Executing the commit will alter currently loaded hooks
  449. // libraries. Check if the supplied libraries are valid,
  450. // but defer the commit until after everything else has
  451. // committed.
  452. hooks_parser = parser;
  453. hooks_parser->build(config_pair.second);
  454. } else if (config_pair.first == "interfaces") {
  455. // The interface parser is independent from any other parser and
  456. // can be run here before other parsers.
  457. parser->build(config_pair.second);
  458. iface_parser = parser;
  459. } else {
  460. // Those parsers should be started before other
  461. // parsers so we can call build straight away.
  462. independent_parsers.push_back(parser);
  463. parser->build(config_pair.second);
  464. // The commit operation here may modify the global storage
  465. // but we need it so as the subnet6 parser can access the
  466. // parsed data.
  467. parser->commit();
  468. }
  469. }
  470. // The option values parser is the next one to be run.
  471. std::map<std::string, ConstElementPtr>::const_iterator option_config =
  472. values_map.find("option-data");
  473. if (option_config != values_map.end()) {
  474. option_parser->build(option_config->second);
  475. option_parser->commit();
  476. }
  477. // The subnet parser is the last one to be run.
  478. std::map<std::string, ConstElementPtr>::const_iterator subnet_config =
  479. values_map.find("subnet6");
  480. if (subnet_config != values_map.end()) {
  481. subnet_parser->build(subnet_config->second);
  482. }
  483. } catch (const isc::Exception& ex) {
  484. LOG_ERROR(dhcp6_logger, DHCP6_PARSER_FAIL)
  485. .arg(config_pair.first).arg(ex.what());
  486. answer = isc::config::createAnswer(1,
  487. string("Configuration parsing failed: ") + ex.what());
  488. // An error occured, so make sure that we restore original data.
  489. rollback = true;
  490. } catch (...) {
  491. // for things like bad_cast in boost::lexical_cast
  492. LOG_ERROR(dhcp6_logger, DHCP6_PARSER_EXCEPTION).arg(config_pair.first);
  493. answer = isc::config::createAnswer(1,
  494. string("Configuration parsing failed"));
  495. // An error occured, so make sure that we restore original data.
  496. rollback = true;
  497. }
  498. // So far so good, there was no parsing error so let's commit the
  499. // configuration. This will add created subnets and option values into
  500. // the server's configuration.
  501. // This operation should be exception safe but let's make sure.
  502. if (!rollback) {
  503. try {
  504. if (subnet_parser) {
  505. subnet_parser->commit();
  506. }
  507. if (iface_parser) {
  508. iface_parser->commit();
  509. }
  510. // This occurs last as if it succeeds, there is no easy way to
  511. // revert it. As a result, the failure to commit a subsequent
  512. // change causes problems when trying to roll back.
  513. if (hooks_parser) {
  514. hooks_parser->commit();
  515. }
  516. }
  517. catch (const isc::Exception& ex) {
  518. LOG_ERROR(dhcp6_logger, DHCP6_PARSER_COMMIT_FAIL).arg(ex.what());
  519. answer = isc::config::createAnswer(2,
  520. string("Configuration commit failed:") + ex.what());
  521. // An error occured, so make sure to restore the original data.
  522. rollback = true;
  523. } catch (...) {
  524. // for things like bad_cast in boost::lexical_cast
  525. LOG_ERROR(dhcp6_logger, DHCP6_PARSER_COMMIT_EXCEPTION);
  526. answer = isc::config::createAnswer(2,
  527. string("Configuration commit failed"));
  528. // An error occured, so make sure to restore the original data.
  529. rollback = true;
  530. }
  531. }
  532. // Rollback changes as the configuration parsing failed.
  533. if (rollback) {
  534. globalContext().reset(new ParserContext(original_context));
  535. return (answer);
  536. }
  537. LOG_INFO(dhcp6_logger, DHCP6_CONFIG_COMPLETE).arg(config_details);
  538. // Everything was fine. Configuration is successful.
  539. answer = isc::config::createAnswer(0, "Configuration committed.");
  540. return (answer);
  541. }
  542. ParserContextPtr& globalContext() {
  543. static ParserContextPtr global_context_ptr(new ParserContext(Option::V6));
  544. return (global_context_ptr);
  545. }
  546. }; // end of isc::dhcp namespace
  547. }; // end of isc namespace