Browse Source

[4317] Added client-id as supported host identifier.

Marcin Siodelski 9 years ago
parent
commit
2987c3d82f

+ 9 - 0
src/lib/dhcpsrv/host.cc

@@ -136,6 +136,9 @@ Host::getIdentifierType(const std::string& identifier_name) {
     } else if (identifier_name == "circuit-id") {
         return (IDENT_CIRCUIT_ID);
 
+    } else if (identifier_name == "client-id") {
+        return (IDENT_CLIENT_ID);
+
     } else {
         isc_throw(isc::BadValue, "invalid client identifier type '"
                   << identifier_name << "'");
@@ -176,6 +179,9 @@ Host::getIdentifierAsText(const IdentifierType& type, const uint8_t* value,
     case IDENT_CIRCUIT_ID:
         s << "circuit-id";
         break;
+    case IDENT_CLIENT_ID:
+        s << "client-id";
+        break;
     default:
         // This should never happen actually, unless we add new identifier
         // and forget to add a case for it above.
@@ -198,6 +204,9 @@ Host::getIdentifierName(const IdentifierType& type) {
     case Host::IDENT_CIRCUIT_ID:
         return ("circuit-id");
 
+    case Host::IDENT_CLIENT_ID:
+        return ("client-id");
+
     default:
         ;
     }

+ 10 - 6
src/lib/dhcpsrv/host.h

@@ -175,17 +175,21 @@ public:
 
     /// @brief Type of the host identifier.
     ///
-    /// Currently hardware address assigned to an interface and the
-    /// DHCPv6 client's DUID are supported.
+    /// Currently supported identifiers are:
+    /// - hardware address (DHCPv4 and DHCPv6),
+    /// - DUID (DHCPv4 and DHCPv6),
+    /// - circuit identifier (DHCPv4),
+    /// - client identifier (DHCPv4).
     enum IdentifierType {
         IDENT_HWADDR,
         IDENT_DUID,
-        IDENT_CIRCUIT_ID
+        IDENT_CIRCUIT_ID,
+        IDENT_CLIENT_ID
     };
 
     /// @brief Constant pointing to the last identifier of the
     /// @ref IdentifierType enumeration.
-    static const IdentifierType LAST_IDENTIFIER_TYPE = IDENT_CIRCUIT_ID;
+    static const IdentifierType LAST_IDENTIFIER_TYPE = IDENT_CLIENT_ID;
 
     /// @brief Constructor.
     ///
@@ -240,7 +244,7 @@ public:
     ///
     /// @param identifier Identifier in the textual format. The expected formats
     /// for the hardware address and other identifiers are provided above.
-    /// @param identifier_name One of "hw-address", "duid", "circuit-id".
+    /// @param identifier_name One of "hw-address", "duid", "circuit-id", "client-id".
     /// @param ipv4_subnet_id Identifier of the IPv4 subnet to which the host
     /// is connected.
     /// @param ipv6_subnet_id Identifier of the IPv6 subnet to which the host
@@ -282,7 +286,7 @@ public:
     /// This method is called by the @c Host constructor.
     ///
     /// @param identifier Reference to a new identifier in the textual format.
-    /// @param name One of "hw-address", "duid", "circuit-id".
+    /// @param name One of "hw-address", "duid", "circuit-id", "client-id".
     ///
     /// @throw BadValue if the identifier is invalid.
     void setIdentifier(const std::string& identifier, const std::string& name);

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

@@ -41,6 +41,7 @@ getSupportedParams4(const bool identifiers_only = false) {
         identifiers_set.insert("hw-address");
         identifiers_set.insert("duid");
         identifiers_set.insert("circuit-id");
+        identifiers_set.insert("client-id");
     }
     // Copy identifiers and add all other parameters.
     if (params_set.empty()) {

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

@@ -182,6 +182,9 @@ protected:
 
     /// @brief Vector holding circuit id used by tests.
     std::vector<uint8_t> circuit_id_;
+
+    /// @brief Vector holding client id used by tests.
+    std::vector<uint8_t> client_id_;
 };
 
 void
@@ -199,6 +202,11 @@ HostReservationParserTest::SetUp() {
 
     const std::string circuit_id_str = "howdy";
     circuit_id_.assign(circuit_id_str.begin(), circuit_id_str.end());
+
+    client_id_.push_back(0x01); // Client identifier type.
+    // Often client id comprises HW address.
+    client_id_.insert(client_id_.end(), hwaddr_->hwaddr_.begin(),
+                      hwaddr_->hwaddr_.end());
 }
 
 void
@@ -252,6 +260,22 @@ TEST_F(HostReservationParserTest, dhcp4CircuitIdHexWithPrefix) {
                     circuit_id_);
 }
 
+// This test verifies that the parser can parse a reservation entry for
+// which client-id is an identifier. The client-id is specified in
+// hexadecimal format.
+TEST_F(HostReservationParserTest, dhcp4ClientIdHex) {
+    testIdentifier4("client-id", "01010203040506", Host::IDENT_CLIENT_ID,
+                    client_id_);
+}
+
+// This test verifies that the parser can parse a reservation entry for
+// which client-id is an identifier. The client-id is specified in
+// hexadecimal format with a '0x' prefix.
+TEST_F(HostReservationParserTest, dhcp4ClientIdHexWithPrefix) {
+    testIdentifier4("client-id", "0x01010203040506", Host::IDENT_CLIENT_ID,
+                    client_id_);
+}
+
 // This test verifies that the parser can parse the reservation entry
 // when IPv4 address is specified, but hostname is not.
 TEST_F(HostReservationParserTest, dhcp4NoHostname) {
@@ -465,6 +489,17 @@ TEST_F(HostReservationParserTest, dhcp6CircuitId) {
     testInvalidConfig<HostReservationParser6>(config);
 }
 
+// This test verifies that host reservation parser for DHCPv6 rejects
+// "client-id" as a host identifier.
+TEST_F(HostReservationParserTest, dhcp6ClientId) {
+    // Use DHCPv4 specific identifier 'circuit-id' with DHCPv6 parser.
+    std::string config = "{ \"client-id\": \"01010203040506\","
+        "\"ip-addresses\": [ \"2001:db8:1::100\", \"2001:db8:1::200\" ],"
+        "\"prefixes\": [ ],"
+        "\"hostname\": \"foo.example.com\" }";
+    testInvalidConfig<HostReservationParser6>(config);
+}
+
 // This test verfies that the parser can parse the IPv6 reservation entry
 // which lacks hostname parameter.
 TEST_F(HostReservationParserTest, dhcp6NoHostname) {

+ 2 - 1
src/lib/dhcpsrv/tests/host_unittest.cc

@@ -23,7 +23,7 @@ namespace {
 /// @brief Holds a type of the last identifier in @c IdentifierType enum.
 ///
 /// This value must be updated when new identifiers are added to the enum.
-const Host::IdentifierType LAST_IDENTIFIER_TYPE = Host::IDENT_CIRCUIT_ID;
+const Host::IdentifierType LAST_IDENTIFIER_TYPE = Host::IDENT_CLIENT_ID;
 
 // This test verifies that it is possible to create IPv6 address
 // reservation.
@@ -180,6 +180,7 @@ TEST_F(HostTest, getIdentifier) {
     EXPECT_EQ(Host::IDENT_HWADDR, Host::getIdentifierType("hw-address"));
     EXPECT_EQ(Host::IDENT_DUID, Host::getIdentifierType("duid"));
     EXPECT_EQ(Host::IDENT_CIRCUIT_ID, Host::getIdentifierType("circuit-id"));
+    EXPECT_EQ(Host::IDENT_CLIENT_ID, Host::getIdentifierType("client-id"));
 
     EXPECT_THROW(Host::getIdentifierType("unuspported"), isc::BadValue);
 }