Browse Source

[master] Merge branch 'trac3810'

Marcin Siodelski 10 years ago
parent
commit
54ee0f6328

+ 54 - 0
src/lib/dhcpsrv/parsers/host_reservation_parser.cc

@@ -23,6 +23,44 @@
 using namespace isc::asiolink;
 using namespace isc::data;
 
+namespace {
+
+/// @brief Returns set of the supported parameters for DHCPv4.
+///
+/// This function returns the set of supported parameters for
+/// host reservation in DHCPv4.
+const std::set<std::string>& getSupportedParams4() {
+    static std::set<std::string> params_set;
+    if (params_set.empty()) {
+        const char* params[] = {
+            "duid", "hw-address", "hostname", "ip-address", NULL
+        };
+        for (int i = 0; params[i] != NULL; ++i) {
+            params_set.insert(std::string(params[i]));
+        }
+    }
+    return (params_set);
+}
+
+/// @brief Returns set of the supported parameters for DHCPv4.
+///
+/// This function returns the set of supported parameters for
+/// host reservation in DHCPv6.
+const std::set<std::string>& getSupportedParams6() {
+    static std::set<std::string> params_set;
+    if (params_set.empty()) {
+        const char* params[] = {
+            "duid", "hw-address", "hostname", "ip-addresses", "prefixes", NULL
+        };
+        for (int i = 0; params[i] != NULL; ++i) {
+            params_set.insert(std::string(params[i]));
+        }
+    }
+    return (params_set);
+}
+
+}
+
 namespace isc {
 namespace dhcp {
 
@@ -40,6 +78,12 @@ HostReservationParser::build(isc::data::ConstElementPtr reservation_data) {
     // reservations.
     BOOST_FOREACH(ConfigPair element, reservation_data->mapValue()) {
         try {
+            // Check if we support this parameter.
+            if (!isSupportedParameter(element.first)) {
+                isc_throw(DhcpConfigError, "unsupported configuration"
+                          " parameter '" << element.first << "'");
+            }
+
             if (element.first == "hw-address" || element.first == "duid") {
                 if (!identifier_name.empty()) {
                     isc_throw(DhcpConfigError, "the 'hw-address' and 'duid'"
@@ -116,6 +160,11 @@ HostReservationParser4::build(isc::data::ConstElementPtr reservation_data) {
     addHost(reservation_data);
 }
 
+bool
+HostReservationParser4::isSupportedParameter(const std::string& param_name) const {
+    return (getSupportedParams4().count(param_name) > 0);
+}
+
 HostReservationParser6::HostReservationParser6(const SubnetID& subnet_id)
     : HostReservationParser(subnet_id) {
 }
@@ -197,5 +246,10 @@ HostReservationParser6::build(isc::data::ConstElementPtr reservation_data) {
     addHost(reservation_data);
 }
 
+bool
+HostReservationParser6::isSupportedParameter(const std::string& param_name) const {
+    return (getSupportedParams6().count(param_name) > 0);
+}
+
 } // end of namespace isc::dhcp
 } // end of namespace isc

+ 26 - 1
src/lib/dhcpsrv/parsers/host_reservation_parser.h

@@ -1,4 +1,4 @@
-// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
@@ -56,6 +56,13 @@ protected:
     /// @throw DhcpConfigError When operation to add a configured host fails.
     void addHost(isc::data::ConstElementPtr reservation_data);
 
+    /// @brief Checks if the specified parameter is supported by the parser.
+    ///
+    /// @param param_name Parameter name.
+    ///
+    /// @return true if the parameter is supported, false otherwise.
+    virtual bool isSupportedParameter(const std::string& param_name) const = 0;
+
     /// @brief Identifier of the subnet that the host is connected to.
     SubnetID subnet_id_;
 
@@ -82,6 +89,15 @@ public:
     ///
     /// @throw DhcpConfigError If the configuration is invalid.
     virtual void build(isc::data::ConstElementPtr reservation_data);
+
+protected:
+
+    /// @brief Checks if the specified parameter is supported by the parser.
+    ///
+    /// @param param_name Parameter name.
+    ///
+    /// @return true if the parameter is supported, false otherwise.
+    virtual bool isSupportedParameter(const std::string& param_name) const;
 };
 
 /// @brief Parser for a single host reservation for DHCPv6.
@@ -101,6 +117,15 @@ public:
     ///
     /// @throw DhcpConfigError If the configuration is invalid.
     virtual void build(isc::data::ConstElementPtr reservation_data);
+
+protected:
+
+    /// @brief Checks if the specified parameter is supported by the parser.
+    ///
+    /// @param param_name Parameter name.
+    ///
+    /// @return true if the parameter is supported, false otherwise.
+    virtual bool isSupportedParameter(const std::string& param_name) const;
 };
 
 

+ 32 - 0
src/lib/dhcpsrv/tests/host_reservation_parser_unittest.cc

@@ -273,6 +273,22 @@ TEST_F(HostReservationParserTest, bcastAddress) {
     EXPECT_THROW(parser.build(config_element), DhcpConfigError);
 }
 
+// This test verifies that the configuration parser for host reservations
+// throws an exception when unsupported parameter is specified.
+TEST_F(HostReservationParserTest, invalidParameterName) {
+    // The "ip-addresses" parameter name is incorrect for the DHCPv4
+    // case - it is only valid for DHCPv6 case. Trying to set this
+    // parameter should result in error.
+    std::string config = "{ \"hw-address\": \"01:02:03:04:05:06\","
+        "\"hostname\": \"foo.bar.isc.org\","
+        "\"ip-addresses\": \"2001:db8:1::1\" }";
+
+    ElementPtr config_element = Element::fromJSON(config);
+
+    HostReservationParser4 parser(SubnetID(10));
+    EXPECT_THROW(parser.build(config_element), DhcpConfigError);
+}
+
 // This test verfies that the parser can parse the IPv6 reservation entry for
 // which hw-address is a host identifier.
 TEST_F(HostReservationParserTest, dhcp6HWaddr) {
@@ -484,4 +500,20 @@ TEST_F(HostReservationParserTest, dhcp6DuplicatedPrefix) {
 }
 
 
+// This test verifies that the configuration parser for host reservations
+// throws an exception when unsupported parameter is specified.
+TEST_F(HostReservationParserTest, dhcp6invalidParameterName) {
+    // The "ip-address" parameter name is incorrect for the DHCPv6
+    // case - it is only valid for DHCPv4 case. Trying to set this
+    // parameter should result in error.
+    std::string config = "{ \"hw-address\": \"01:02:03:04:05:06\","
+        "\"hostname\": \"foo.bar.isc.org\","
+        "\"ip-address\": \"192.0.2.3\" }";
+
+    ElementPtr config_element = Element::fromJSON(config);
+
+    HostReservationParser6 parser(SubnetID(10));
+    EXPECT_THROW(parser.build(config_element), DhcpConfigError);
+}
+
 } // end of anonymous namespace