]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5123] Updated dhcpsrv and dhcp[46]/json... codes
authorFrancis Dupont <fdupont@isc.org>
Thu, 26 Jan 2017 20:45:23 +0000 (21:45 +0100)
committerFrancis Dupont <fdupont@isc.org>
Thu, 26 Jan 2017 20:45:23 +0000 (21:45 +0100)
src/bin/dhcp4/json_config_parser.cc
src/bin/dhcp6/json_config_parser.cc
src/lib/dhcpsrv/parsers/dhcp_parsers.cc
src/lib/dhcpsrv/parsers/dhcp_parsers.h

index 5b60f5be23b8cdb1af5948c7e45b1c3a6160766a..a7afc1a18c5d8d7ccaedf9c78b579797da08c56f 100644 (file)
@@ -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<uint32_t, DhcpConfigError>(name, value));
+    uint32_t getUint32(isc::data::ConstElementPtr scope,
+                       const std::string& name) {
+        return (getIntType<uint32_t>(scope, name));
     }
 };
 
index 406325e20d465ee87fef11890c75de7b45b64fad..58649c8ed2512a1b1f4415d262282e3db085645f 100644 (file)
@@ -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<uint8_t, DhcpConfigError>(name, value));
+    /// @throw isc::dhcp::DhcpConfigError when it is not an uint8_t
+    uint8_t getUint8(ConstElementPtr scope, const std::string& name) {
+        return (getIntType<uint8_t>(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<uint32_t, DhcpConfigError>(name, value));
+    uint32_t getUint32(isc::data::ConstElementPtr scope,
+                       const std::string& name) {
+        return (getIntType<uint32_t>(scope, name));
     }
 };
 
index e68af3ff215b448d2cf656dc23fa117f8c46ccb2..550fc9219315eca65f4aa5c660c4e436b2054ab2 100644 (file)
@@ -1204,51 +1204,45 @@ SubnetConfigParser::getOptionalParam(const std::string& name) {
 //**************************** D2ClientConfigParser **********************
 
 uint32_t
-D2ClientConfigParser::getUint32(const std::string& name,
-                                ConstElementPtr value) const {
-    return (extractInt<uint32_t, DhcpConfigError>(name, value));
+D2ClientConfigParser::getUint32(ConstElementPtr scope,
+                                const std::string& name) {
+    return (getIntType<uint32_t>(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<IOAddress,
-                           buildIOAddress,
-                           DhcpConfigError>(name, "address", value));
+D2ClientConfigParser::getIOAddress(ConstElementPtr scope,
+                                   const std::string& name) {
+    return (getAndConvert<IOAddress,
+            buildIOAddress>(scope, name, "address"));
 }
 
 dhcp_ddns::NameChangeProtocol
-D2ClientConfigParser::getProtocol(const std::string& name,
-                                  ConstElementPtr value) const {
-    return (extractConvert<dhcp_ddns::NameChangeProtocol,
-                           dhcp_ddns::stringToNcrProtocol,
-                            DhcpConfigError>(name,
-                                             "NameChangeRequest protocol",
-                                             value));
+D2ClientConfigParser::getProtocol(ConstElementPtr scope,
+                                  const std::string& name) {
+    return (getAndConvert<dhcp_ddns::NameChangeProtocol,
+            dhcp_ddns::stringToNcrProtocol>
+            (scope, name, "NameChangeRequest protocol"));
 }
 
 dhcp_ddns::NameChangeFormat
-D2ClientConfigParser::getFormat(const std::string& name,
-                                ConstElementPtr value) const {
-    return (extractConvert<dhcp_ddns::NameChangeFormat,
-                           dhcp_ddns::stringToNcrFormat,
-                           DhcpConfigError>(name,
-                                            "NameChangeRequest format",
-                                            value));
+D2ClientConfigParser::getFormat(ConstElementPtr scope,
+                                const std::string& name) {
+    return (getAndConvert<dhcp_ddns::NameChangeFormat,
+            dhcp_ddns::stringToNcrFormat>
+            (scope, name, "NameChangeRequest format"));
 }
 
 D2ClientConfig::ReplaceClientNameMode
-D2ClientConfigParser::getMode(const std::string& name,
-                              ConstElementPtr value) const {
-    return (extractConvert<D2ClientConfig::ReplaceClientNameMode,
-                           D2ClientConfig::stringToReplaceClientNameMode,
-                           DhcpConfigError>(name,
-                                            "ReplaceClientName mode",
-                                            value));
+D2ClientConfigParser::getMode(ConstElementPtr scope,
+                              const std::string& name) {
+    return (getAndConvert<D2ClientConfig::ReplaceClientNameMode,
+            D2ClientConfig::stringToReplaceClientNameMode>
+            (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
index af11df151e4611f70b71a1d0350b6f4f3ca0621d..7b2ec6c5b46d8e082f74233310e425a87497ee20 100644 (file)
@@ -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.