From a911efe6e5015859806e9be810ac7fb3d87334dc Mon Sep 17 00:00:00 2001 From: Francis Dupont Date: Thu, 26 Jan 2017 21:45:23 +0100 Subject: [PATCH] [5123] Updated dhcpsrv and dhcp[46]/json... codes --- src/bin/dhcp4/json_config_parser.cc | 42 +++--- src/bin/dhcp6/json_config_parser.cc | 128 +++++++------------ src/lib/dhcpsrv/parsers/dhcp_parsers.cc | 163 +++++++++--------------- src/lib/dhcpsrv/parsers/dhcp_parsers.h | 39 +++--- 4 files changed, 141 insertions(+), 231 deletions(-) diff --git a/src/bin/dhcp4/json_config_parser.cc b/src/bin/dhcp4/json_config_parser.cc index 5b60f5be23..a7afc1a18c 100644 --- a/src/bin/dhcp4/json_config_parser.cc +++ b/src/bin/dhcp4/json_config_parser.cc @@ -371,7 +371,7 @@ public: } }; -/// @brief Parser that takes care of global DHCPv6 parameters. +/// @brief Parser that takes care of global DHCPv4 parameters. /// /// See @ref parse method for a list of supported parameters. class Dhcp4ConfigParser : public isc::data::SimpleParser { @@ -397,39 +397,29 @@ public: bool echo_client_id = getBoolean(global, "echo-client-id"); CfgMgr::instance().echoClientId(echo_client_id); - std::string name; - ConstElementPtr value; - try { - // Set the probation period for decline handling. - name = "decline-probation-period"; - value = global->get(name); - uint32_t probation_period = getUint32(name, value); - cfg->setDeclinePeriod(probation_period); - - // Set the DHCPv4-over-DHCPv6 interserver port. - name = "dhcp4o6-port"; - value = global->get(name); - // @todo Change for uint16_t - uint32_t dhcp4o6_port = getUint32(name, value); - cfg->setDhcp4o6Port(dhcp4o6_port); - } catch (const isc::data::TypeError& ex) { - isc_throw(DhcpConfigError, - "invalid value type specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } + // Set the probation period for decline handling. + uint32_t probation_period = + getUint32(global, "decline-probation-period"); + cfg->setDeclinePeriod(probation_period); + + // Set the DHCPv4-over-DHCPv6 interserver port. + // @todo Change for uint16_t + uint32_t dhcp4o6_port = getUint32(global, "dhcp4o6-port"); + cfg->setDhcp4o6Port(dhcp4o6_port); } private: /// @brief Returns a value converted to uint32_t /// - /// Instantiation of extractInt() to uint32_t + /// Instantiation of getIntType() to uint32_t /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return an uint32_t value - uint32_t getUint32(const std::string& name, - isc::data::ConstElementPtr value) { - return (extractInt(name, value)); + uint32_t getUint32(isc::data::ConstElementPtr scope, + const std::string& name) { + return (getIntType(scope, name)); } }; diff --git a/src/bin/dhcp6/json_config_parser.cc b/src/bin/dhcp6/json_config_parser.cc index 406325e20d..58649c8ed2 100644 --- a/src/bin/dhcp6/json_config_parser.cc +++ b/src/bin/dhcp6/json_config_parser.cc @@ -155,52 +155,34 @@ public: /// that define a prefix delegation pool. /// /// @throw DhcpConfigError if configuration parsing fails. - void parse(PoolStoragePtr pools, - ConstElementPtr pd_pool_) { - std::string addr_str; + void parse(PoolStoragePtr pools, ConstElementPtr pd_pool_) { + std::string addr_str = getString(pd_pool_, "prefix"); + + uint8_t prefix_len = getUint8(pd_pool_, "prefix-len"); + + uint8_t delegated_len = getUint8(pd_pool_, "delegated-len"); + std::string excluded_prefix_str = "::"; - uint8_t prefix_len = 0; - uint8_t delegated_len = 0; + if (pd_pool_->contains("excluded-prefix")) { + excluded_prefix_str = getString(pd_pool_, "excluded-prefix"); + } + uint8_t excluded_prefix_len = 0; + if (pd_pool_->contains("excluded-prefix-len")) { + excluded_prefix_len = getUint8(pd_pool_, "excluded-prefix-len"); + } - // Parse the elements that make up the option definition. - BOOST_FOREACH(ConfigPair param, pd_pool_->mapValue()) { - std::string entry(param.first); - ConstElementPtr value(param.second); - try { - if (entry == "prefix") { - addr_str = value->stringValue(); - } else if (entry == "excluded-prefix") { - excluded_prefix_str = value->stringValue(); - } else if (entry == "prefix-len") { - prefix_len = getUint8(entry, value); - } else if (entry == "delegated-len") { - delegated_len = getUint8(entry, value); - } else if (entry == "excluded-prefix-len") { - excluded_prefix_len = getUint8(entry, value); - } else if (entry == "option-data") { - OptionDataListParser opts_parser(AF_INET6); - opts_parser.parse(options_, value); - } else if (entry == "user-context") { - user_context_ = value; - } else { - isc_throw(isc::dhcp::DhcpConfigError, - "unsupported parameter: " << entry - << " (" << value->getPosition() << ")"); - } - } catch (const isc::data::TypeError&) { - isc_throw(isc::dhcp::DhcpConfigError, - "invalid value type specified for parameter '" - << entry << "' (" - << value->getPosition() << ")"); - } + if (pd_pool_->contains("option-data")) { + OptionDataListParser opts_parser(AF_INET6); + opts_parser.parse(options_, pd_pool_->get("option-data")); + } + + if (pd_pool_->contains("user-context")) { + user_context_ = pd_pool_->get("user-context"); } // Check the pool parameters. It will throw an exception if any - // of the required parameters are not present or invalid. - requireParam("prefix", pd_pool_); - requireParam("prefix-len", pd_pool_); - requireParam("delegated-len", pd_pool_); + // of the required parameters are invalid. try { // Attempt to construct the local pool. pool_.reset(new Pool6(IOAddress(addr_str), @@ -228,28 +210,16 @@ public: private: - /// @brief Require a mandatory element - /// - /// @param name Entry name - /// @param config Pools configuration - /// @throw isc::dhcp::DhcpConfigError if not present - void requireParam(const std::string& name, ConstElementPtr config) const { - if (!config->contains(name)) { - isc_throw(isc::dhcp::DhcpConfigError, - "Missing parameter '" << name << "' (" - << config->getPosition() << ")"); - } - } - /// @brief Get an uint8_t value /// - /// @param name Entry name - /// @param value Integer element value + /// Instantiation of getIntType() to uint8_t + /// + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return uint8_t value - /// @throw isc::data::TypeError when it is not an integer - /// isc::dhcp::DhcpConfigError when it does not fit in an uint8_t - uint8_t getUint8(const std::string& name, ConstElementPtr value) const { - return (extractInt(name, value)); + /// @throw isc::dhcp::DhcpConfigError when it is not an uint8_t + uint8_t getUint8(ConstElementPtr scope, const std::string& name) { + return (getIntType(scope, name)); } /// Pointer to the created pool object. @@ -620,39 +590,29 @@ public: /// or having incorrect values. void parse(SrvConfigPtr srv_config, ConstElementPtr global) { - std::string name; - ConstElementPtr value; - try { - // Set the probation period for decline handling. - name = "decline-probation-period"; - value = global->get(name); - uint32_t probation_period = getUint32(name, value); - srv_config->setDeclinePeriod(probation_period); - - // Set the DHCPv4-over-DHCPv6 interserver port. - name = "dhcp4o6-port"; - value = global->get(name); - // @todo Change for uint16_t - uint32_t dhcp4o6_port = getUint32(name, value); - srv_config->setDhcp4o6Port(dhcp4o6_port); - } catch (const isc::data::TypeError& ex) { - isc_throw(DhcpConfigError, - "invalid value type specified for parameter '" << name - << "' (" << value->getPosition() << ")"); - } + // Set the probation period for decline handling. + uint32_t probation_period = + getUint32(global, "decline-probation-period"); + srv_config->setDeclinePeriod(probation_period); + + // Set the DHCPv4-over-DHCPv6 interserver port. + // @todo Change for uint16_t + uint32_t dhcp4o6_port = getUint32(global, "dhcp4o6-port"); + srv_config->setDhcp4o6Port(dhcp4o6_port); } private: /// @brief Returns a value converted to uint32_t /// - /// Instantiation of extractInt() to uint32_t + /// Instantiation of getIntType() to uint32_t /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return an uint32_t value - uint32_t getUint32(const std::string& name, - isc::data::ConstElementPtr value) { - return (extractInt(name, value)); + uint32_t getUint32(isc::data::ConstElementPtr scope, + const std::string& name) { + return (getIntType(scope, name)); } }; diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc index e68af3ff21..550fc92193 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.cc +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.cc @@ -1204,51 +1204,45 @@ SubnetConfigParser::getOptionalParam(const std::string& name) { //**************************** D2ClientConfigParser ********************** uint32_t -D2ClientConfigParser::getUint32(const std::string& name, - ConstElementPtr value) const { - return (extractInt(name, value)); +D2ClientConfigParser::getUint32(ConstElementPtr scope, + const std::string& name) { + return (getIntType(scope, name)); } +// Can't use a constructor as a function namespace { IOAddress buildIOAddress(const std::string& str) { return (IOAddress(str)); } }; IOAddress -D2ClientConfigParser::getIOAddress(const std::string& name, - ConstElementPtr value) const { - return (extractConvert(name, "address", value)); +D2ClientConfigParser::getIOAddress(ConstElementPtr scope, + const std::string& name) { + return (getAndConvert(scope, name, "address")); } dhcp_ddns::NameChangeProtocol -D2ClientConfigParser::getProtocol(const std::string& name, - ConstElementPtr value) const { - return (extractConvert(name, - "NameChangeRequest protocol", - value)); +D2ClientConfigParser::getProtocol(ConstElementPtr scope, + const std::string& name) { + return (getAndConvert + (scope, name, "NameChangeRequest protocol")); } dhcp_ddns::NameChangeFormat -D2ClientConfigParser::getFormat(const std::string& name, - ConstElementPtr value) const { - return (extractConvert(name, - "NameChangeRequest format", - value)); +D2ClientConfigParser::getFormat(ConstElementPtr scope, + const std::string& name) { + return (getAndConvert + (scope, name, "NameChangeRequest format")); } D2ClientConfig::ReplaceClientNameMode -D2ClientConfigParser::getMode(const std::string& name, - ConstElementPtr value) const { - return (extractConvert(name, - "ReplaceClientName mode", - value)); +D2ClientConfigParser::getMode(ConstElementPtr scope, + const std::string& name) { + return (getAndConvert + (scope, name, "ReplaceClientName mode")); } D2ClientConfigPtr @@ -1270,69 +1264,44 @@ D2ClientConfigParser::parse(isc::data::ConstElementPtr client_config) { // Get all parameters that are needed to create the D2ClientConfig. std::string qualifying_suffix; bool found_qualifying_suffix = false; - IOAddress server_ip(0); - uint32_t server_port = 0; - std::string sender_ip_str; - uint32_t sender_port = 0; - uint32_t max_queue_size = 1024; - dhcp_ddns::NameChangeProtocol ncr_protocol; - dhcp_ddns::NameChangeFormat ncr_format; - bool always_include_fqdn = false; - bool allow_client_update; - bool override_no_update = false; - bool override_client_update = false; + if (client_config->contains("qualifying-suffix")) { + qualifying_suffix = getString(client_config, "qualifying-suffix"); + found_qualifying_suffix = true; + } + + IOAddress server_ip = getIOAddress(client_config, "server-ip"); + + uint32_t server_port = getUint32(client_config, "server-port"); + + std::string sender_ip_str = getString(client_config, "sender-ip"); + + uint32_t sender_port = getUint32(client_config, "sender-port"); + + uint32_t max_queue_size = getUint32(client_config, "max-queue-size"); + + dhcp_ddns::NameChangeProtocol ncr_protocol = + getProtocol(client_config, "ncr-protocol"); + + dhcp_ddns::NameChangeFormat ncr_format = + getFormat(client_config, "ncr-format"); + + bool always_include_fqdn = + getBoolean(client_config, "always-include-fqdn"); + + // bool allow_client_update; (unused) + + bool override_no_update = + getBoolean(client_config, "override-no-update"); + + bool override_client_update = + getBoolean(client_config, "override-client-update"); + D2ClientConfig::ReplaceClientNameMode replace_client_name_mode = - D2ClientConfig::ReplaceClientNameMode::RCM_NEVER; - std::string generated_prefix; + getMode(client_config, "replace-client-name"); + + std::string generated_prefix = + getString(client_config, "generated-prefix"); - BOOST_FOREACH(ConfigPair param, client_config->mapValue()) { - std::string entry(param.first); - ConstElementPtr value(param.second); - try { - if (entry == "enable-updates") { - // already done. - } else if (entry == "qualifying-suffix") { - qualifying_suffix = value->stringValue(); - found_qualifying_suffix = true; - } else if (entry == "server-ip") { - server_ip = getIOAddress("server-ip", value); - } else if (entry == "server-port") { - server_port = getUint32("server-port", value); - } else if (entry == "sender-ip") { - sender_ip_str = value->stringValue(); - } else if (entry == "sender-port") { - sender_port = getUint32("sender-port", value); - } else if (entry == "max-queue-size") { - max_queue_size = getUint32("max-queue-size", value); - } else if (entry == "ncr-protocol") { - ncr_protocol = getProtocol("ncr-protocol", value); - } else if (entry == "ncr-format") { - ncr_format = getFormat("ncr-format", value); - } else if (entry == "always-include-fqdn") { - always_include_fqdn = value->boolValue(); - } else if (entry == "allow-client-update") { - allow_client_update = value->boolValue(); - // currently unused - (void)allow_client_update; - } else if (entry == "override-no-update") { - override_no_update = value->boolValue(); - } else if (entry == "override-client-update") { - override_client_update = value->boolValue(); - } else if (entry == "replace-client-name") { - replace_client_name_mode = getMode("replace-client-name", value); - } else if (entry == "generated-prefix") { - generated_prefix = value->stringValue(); - } else { - isc_throw(DhcpConfigError, - "unsupported parameter '" << entry - << " (" << value->getPosition() << ")"); - } - } catch (const isc::data::TypeError&) { - isc_throw(DhcpConfigError, - "invalid value type specified for parameter '" << entry - << " (" << value->getPosition() << ")"); - } - } // Qualifying-suffix is required when updates are enabled if (enable_updates && !found_qualifying_suffix) { @@ -1419,18 +1388,8 @@ D2ClientConfigParser::parse(isc::data::ConstElementPtr client_config) { bool D2ClientConfigParser::isShortCutDisabled(isc::data::ConstElementPtr d2_config) { - if (!d2_config->contains("enable-updates")) { - isc_throw(DhcpConfigError, - "Mandatory parameter 'enable-updates' missing (" - << d2_config->getPosition() << ")"); - } - ConstElementPtr enable = d2_config->get("enable-updates"); - if (enable->getType() != Element::boolean) { - isc_throw(DhcpConfigError, - "invalid value type specified for parameter" - " 'enable-updates' (" << enable->getPosition() << ")"); - } - return (!enable->boolValue() && (d2_config->mapValue().size() == 1)); + bool value = getBoolean(d2_config, "enable-updates"); + return (!value && (d2_config->mapValue().size() == 1)); } /// @brief This table defines default values for D2 client configuration diff --git a/src/lib/dhcpsrv/parsers/dhcp_parsers.h b/src/lib/dhcpsrv/parsers/dhcp_parsers.h index af11df151e..7b2ec6c5b4 100644 --- a/src/lib/dhcpsrv/parsers/dhcp_parsers.h +++ b/src/lib/dhcpsrv/parsers/dhcp_parsers.h @@ -988,52 +988,53 @@ private: /// @brief Returns a value converted to uint32_t /// - /// Instantiation of extractInt() to uint32_t + /// Instantiation of getIntType() to uint32_t /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return an uint32_t value uint32_t - getUint32(const std::string& name, isc::data::ConstElementPtr value) const; + getUint32(isc::data::ConstElementPtr scope, const std::string& name); /// @brief Returns a value converted to IOAddress /// - /// Instantiation of extractConvert() to IOAddress + /// Instantiation of getAndConvert() to IOAddress /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return an IOAddress value isc::asiolink::IOAddress - getIOAddress(const std::string& name, - isc::data::ConstElementPtr value) const; + getIOAddress(isc::data::ConstElementPtr scope, const std::string& name); /// @brief Returns a value converted to NameChangeProtocol /// - /// Instantiation of extractInt() to NameChangeProtocol + /// Instantiation of getAndConvert() to NameChangeProtocol /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return a NameChangeProtocol value dhcp_ddns::NameChangeProtocol - getProtocol(const std::string& name, - isc::data::ConstElementPtr value) const; + getProtocol(isc::data::ConstElementPtr scope, const std::string& name); /// @brief Returns a value converted to NameChangeFormat /// - /// Instantiation of extractConvert() to NameChangeFormat + /// Instantiation of getAndConvert() to NameChangeFormat /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return a NameChangeFormat value dhcp_ddns::NameChangeFormat - getFormat(const std::string& name, - isc::data::ConstElementPtr value) const; + getFormat(isc::data::ConstElementPtr scope, const std::string& name); /// @brief Returns a value converted to ReplaceClientNameMode /// - /// Instantiation of extractConvert() to ReplaceClientNameMode + /// Instantiation of getAndConvert() to ReplaceClientNameMode /// - /// @param value value of the parameter + /// @param scope specified parameter will be extracted from this scope + /// @param name name of the parameter /// @return a NameChangeFormat value D2ClientConfig::ReplaceClientNameMode - getMode(const std::string& name, - isc::data::ConstElementPtr value) const; + getMode(isc::data::ConstElementPtr scope, const std::string& name); }; // Pointers to various parser objects. -- 2.47.2