Parcourir la source

[3572] DHCPv4 server sends host specific options.

Marcin Siodelski il y a 9 ans
Parent
commit
b1899f97cd

+ 9 - 1
src/bin/dhcp4/dhcp4_srv.cc

@@ -916,13 +916,21 @@ void
 Dhcpv4Srv::buildCfgOptionList(Dhcpv4Exchange& ex) {
     CfgOptionList& co_list = ex.getCfgOptionList();
 
-    // First subnet configured options
+    // Retrieve subnet.
     Subnet4Ptr subnet = ex.getContext()->subnet_;
     if (!subnet) {
         // All methods using the CfgOptionList object return soon when
         // there is no subnet so do the same
         return;
     }
+
+    // Firstly, host specific options.
+    const ConstHostPtr& host = ex.getContext()->host_;
+    if (host) {
+        co_list.push_back(host->getCfgOption4());
+    }
+
+    // Secondly, subnet configured options.
     if (!subnet->getCfgOption()->empty()) {
         co_list.push_back(subnet->getCfgOption());
     }

+ 1 - 0
src/bin/dhcp4/tests/Makefile.am

@@ -89,6 +89,7 @@ dhcp4_unittests_SOURCES += dhcp4_client.cc dhcp4_client.h
 dhcp4_unittests_SOURCES += hooks_unittest.cc
 dhcp4_unittests_SOURCES += inform_unittest.cc
 dhcp4_unittests_SOURCES += dora_unittest.cc
+dhcp4_unittests_SOURCES += host_options_unittest.cc
 dhcp4_unittests_SOURCES += release_unittest.cc
 dhcp4_unittests_SOURCES += out_of_range_unittest.cc
 dhcp4_unittests_SOURCES += decline_unittest.cc

+ 2 - 2
src/bin/dhcp4/tests/dhcp4_client.cc

@@ -163,13 +163,13 @@ Dhcp4Client::applyConfiguration() {
     Option4AddrLstPtr opt_log_servers = boost::dynamic_pointer_cast<
         Option4AddrLst>(resp->getOption(DHO_LOG_SERVERS));
     if (opt_log_servers) {
-        config_.log_servers_ = opt_routers->getAddresses();
+        config_.log_servers_ = opt_log_servers->getAddresses();
     }
     // Quotes Servers
     Option4AddrLstPtr opt_quotes_servers = boost::dynamic_pointer_cast<
         Option4AddrLst>(resp->getOption(DHO_COOKIE_SERVERS));
     if (opt_quotes_servers) {
-        config_.quotes_servers_ = opt_dns_servers->getAddresses();
+        config_.quotes_servers_ = opt_quotes_servers->getAddresses();
     }
     // Server Identifier
     OptionCustomPtr opt_serverid = boost::dynamic_pointer_cast<

+ 232 - 0
src/bin/dhcp4/tests/host_options_unittest.cc

@@ -0,0 +1,232 @@
+// Copyright (C) 2016 Internet Systems Consortium, Inc. ("ISC")
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+#include <config.h>
+#include <dhcp/dhcp4.h>
+#include <dhcp/tests/iface_mgr_test_config.h>
+#include <dhcp4/tests/dhcp4_test_utils.h>
+#include <dhcp4/tests/dhcp4_client.h>
+#include <stats/stats_mgr.h>
+
+using namespace isc;
+using namespace isc::asiolink;
+using namespace isc::dhcp;
+using namespace isc::dhcp::test;
+
+namespace {
+
+/// @brief Set of JSON configurations used throughout the tests.
+///
+/// - Configuration 0:
+///   - Used for testing direct traffic
+///   - 1 subnet: 10.0.0.0/24
+///   - 1 pool: 10.0.0.10-10.0.0.100
+///   - Router option present: 10.0.0.200 and 10.0.0.201
+///   - Domain Name Server option present: 10.0.0.202, 10.0.0.203.
+///   - Log Servers option present: 192.0.2.200 and 192.0.2.201
+///   - Quotes Servers option present: 192.0.2.202, 192.0.2.203.
+///
+const char* HOST_CONFIGS[] = {
+// Configuration 0
+    "{ \"interfaces-config\": {"
+        "      \"interfaces\": [ \"*\" ]"
+        "},"
+        "\"valid-lifetime\": 600,"
+        "\"subnet4\": [ { "
+        "    \"subnet\": \"10.0.0.0/24\", "
+        "    \"id\": 1,"
+        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
+        "    \"option-data\": [ {"
+        "        \"name\": \"routers\","
+        "        \"data\": \"10.0.0.200,10.0.0.201\""
+        "    },"
+        "    {"
+        "        \"name\": \"domain-name-servers\","
+        "        \"data\": \"10.0.0.202,10.0.0.203\""
+        "    },"
+        "    {"
+        "        \"name\": \"log-servers\","
+        "        \"data\": \"10.0.0.200,10.0.0.201\""
+        "    },"
+        "    {"
+        "        \"name\": \"cookie-servers\","
+        "        \"data\": \"10.0.0.202,10.0.0.203\""
+        "    } ],"
+        "    \"reservations\": [ "
+        "    {"
+        "        \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
+        "        \"ip-address\": \"10.0.0.7\","
+        "        \"option-data\": [ {"
+        "            \"name\": \"cookie-servers\","
+        "            \"data\": \"10.1.1.202,10.1.1.203\""
+        "        },"
+        "        {"
+        "            \"name\": \"log-servers\","
+        "            \"data\": \"10.1.1.200,10.1.1.201\""
+        "        } ]"
+        "    } ]"
+        " } ]"
+    "}",
+
+// Configuration 1
+    "{ \"interfaces-config\": {"
+        "      \"interfaces\": [ \"*\" ]"
+        "},"
+        "\"valid-lifetime\": 600,"
+        "\"subnet4\": [ { "
+        "    \"subnet\": \"10.0.0.0/24\", "
+        "    \"id\": 1,"
+        "    \"pools\": [ { \"pool\": \"10.0.0.10-10.0.0.100\" } ],"
+        "    \"option-data\": [ {"
+        "        \"name\": \"routers\","
+        "        \"data\": \"10.0.0.200,10.0.0.201\""
+        "    },"
+        "    {"
+        "        \"name\": \"domain-name-servers\","
+        "        \"data\": \"10.0.0.202,10.0.0.203\""
+        "    },"
+        "    {"
+        "        \"name\": \"log-servers\","
+        "        \"data\": \"10.0.0.200,10.0.0.201\""
+        "    },"
+        "    {"
+        "        \"name\": \"cookie-servers\","
+        "        \"data\": \"10.0.0.202,10.0.0.203\""
+        "    } ],"
+        "    \"reservations\": [ "
+        "    {"
+        "        \"hw-address\": \"aa:bb:cc:dd:ee:ff\","
+        "        \"ip-address\": \"10.0.0.7\","
+        "        \"option-data\": [ {"
+        "            \"name\": \"routers\","
+        "            \"data\": \"10.1.1.200,10.1.1.201\""
+        "        },"
+        "        {"
+        "            \"name\": \"domain-name-servers\","
+        "            \"data\": \"10.1.1.202,10.1.1.203\""
+        "        } ]"
+        "    } ]"
+        " } ]"
+    "}"
+};
+
+/// @brief Test fixture class for testing static reservations of options.
+class HostOptionsTest : public Dhcpv4SrvTest {
+public:
+
+    /// @brief Constructor.
+    ///
+    /// Sets up fake interfaces.
+    HostOptionsTest()
+        : Dhcpv4SrvTest(),
+          iface_mgr_test_config_(true) {
+        IfaceMgr::instance().openSockets4();
+
+        // Let's wipe all existing statistics.
+        isc::stats::StatsMgr::instance().removeAll();
+    }
+
+    /// @brief Destructor.
+    ///
+    /// Cleans up statistics after the test.
+    virtual ~HostOptionsTest() {
+        // Let's wipe all existing statistics.
+        isc::stats::StatsMgr::instance().removeAll();
+    }
+
+    /// @brief Interface Manager's fake configuration control.
+    IfaceMgrTestConfig iface_mgr_test_config_;
+
+};
+
+TEST_F(HostOptionsTest, overrideRequestedOptions) {
+    Dhcp4Client client(Dhcp4Client::SELECTING);
+    client.setHWAddress("aa:bb:cc:dd:ee:ff");
+    client.requestOptions(DHO_DOMAIN_NAME_SERVERS, DHO_LOG_SERVERS,
+                          DHO_COOKIE_SERVERS);
+
+    // Configure DHCP server.
+    configure(HOST_CONFIGS[0], *client.getServer());
+
+    // Perform 4-way exchange with the server but to not request any
+    // specific address in the DHCPDISCOVER message.
+    ASSERT_NO_THROW(client.doDORA());
+
+    // Make sure that the server responded.
+    ASSERT_TRUE(client.getContext().response_);
+    Pkt4Ptr resp = client.getContext().response_;
+    // Make sure that the server has responded with DHCPACK.
+    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
+    // Response must not be relayed.
+    EXPECT_FALSE(resp->isRelayed());
+    // Make sure that the server id is present.
+    EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
+    // Make sure that the client has got the lease for the reserved
+    // address.
+    ASSERT_EQ("10.0.0.7", client.config_.lease_.addr_.toText());
+
+    ASSERT_EQ(2, client.config_.routers_.size());
+    EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
+    EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
+    // Make sure that the DNS Servers option has been received.
+    ASSERT_EQ(2, client.config_.dns_servers_.size());
+    EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
+    EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
+    // Make sure that the Quotes Servers option has been received.
+    ASSERT_EQ(2, client.config_.quotes_servers_.size());
+    EXPECT_EQ("10.1.1.202", client.config_.quotes_servers_[0].toText());
+    EXPECT_EQ("10.1.1.203", client.config_.quotes_servers_[1].toText());
+    // Make sure that the Log Servers option has been received.
+    ASSERT_EQ(2, client.config_.log_servers_.size());
+    EXPECT_EQ("10.1.1.200", client.config_.log_servers_[0].toText());
+    EXPECT_EQ("10.1.1.201", client.config_.log_servers_[1].toText());
+}
+
+TEST_F(HostOptionsTest, overrideDefaultOptions) {
+    Dhcp4Client client(Dhcp4Client::SELECTING);
+    client.setHWAddress("aa:bb:cc:dd:ee:ff");
+
+    client.requestOptions(DHO_LOG_SERVERS, DHO_COOKIE_SERVERS);
+
+    // Configure DHCP server.
+    configure(HOST_CONFIGS[1], *client.getServer());
+
+    // Perform 4-way exchange with the server but to not request any
+    // specific address in the DHCPDISCOVER message.
+    ASSERT_NO_THROW(client.doDORA());
+
+    // Make sure that the server responded.
+    ASSERT_TRUE(client.getContext().response_);
+    Pkt4Ptr resp = client.getContext().response_;
+    // Make sure that the server has responded with DHCPACK.
+    ASSERT_EQ(DHCPACK, static_cast<int>(resp->getType()));
+    // Response must not be relayed.
+    EXPECT_FALSE(resp->isRelayed());
+    // Make sure that the server id is present.
+    EXPECT_EQ("10.0.0.1", client.config_.serverid_.toText());
+    // Make sure that the client has got the lease for the reserved
+    // address.
+    ASSERT_EQ("10.0.0.7", client.config_.lease_.addr_.toText());
+
+    ASSERT_EQ(2, client.config_.routers_.size());
+    EXPECT_EQ("10.1.1.200", client.config_.routers_[0].toText());
+    EXPECT_EQ("10.1.1.201", client.config_.routers_[1].toText());
+    // Make sure that the DNS Servers option has been received.
+    ASSERT_EQ(2, client.config_.dns_servers_.size());
+    EXPECT_EQ("10.1.1.202", client.config_.dns_servers_[0].toText());
+    EXPECT_EQ("10.1.1.203", client.config_.dns_servers_[1].toText());
+    // Make sure that the Quotes Servers option has been received.
+    ASSERT_EQ(2, client.config_.quotes_servers_.size());
+    EXPECT_EQ("10.0.0.202", client.config_.quotes_servers_[0].toText());
+    EXPECT_EQ("10.0.0.203", client.config_.quotes_servers_[1].toText());
+    // Make sure that the Log Servers option has been received.
+    ASSERT_EQ(2, client.config_.log_servers_.size());
+    EXPECT_EQ("10.0.0.200", client.config_.log_servers_[0].toText());
+    EXPECT_EQ("10.0.0.201", client.config_.log_servers_[1].toText());
+}
+
+
+} // end of anonymous namespace

+ 13 - 13
src/bin/dhcp4/tests/inform_unittest.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2016 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -58,11 +58,11 @@ const char* INFORM_CONFIGS[] = {
         "    },"
         "    {"
         "        \"name\": \"log-servers\","
-        "        \"data\": \"10.0.0.200,10.0.0.201\""
+        "        \"data\": \"10.0.0.202,10.0.0.203\""
         "    },"
         "    {"
         "        \"name\": \"cookie-servers\","
-        "        \"data\": \"10.0.0.202,10.0.0.203\""
+        "        \"data\": \"10.0.0.200,10.0.0.201\""
         "    } ]"
         " } ]"
     "}",
@@ -159,12 +159,12 @@ TEST_F(InformTest, directClientBroadcast) {
     EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
     // Make sure that the Log Servers option has been received.
     ASSERT_EQ(2, client.config_.quotes_servers_.size());
-    EXPECT_EQ("10.0.0.200", client.config_.routers_[0].toText());
-    EXPECT_EQ("10.0.0.201", client.config_.routers_[1].toText());
+    EXPECT_EQ("10.0.0.200", client.config_.quotes_servers_[0].toText());
+    EXPECT_EQ("10.0.0.201", client.config_.quotes_servers_[1].toText());
     // Make sure that the Quotes Servers option has been received.
     ASSERT_EQ(2, client.config_.log_servers_.size());
-    EXPECT_EQ("10.0.0.202", client.config_.dns_servers_[0].toText());
-    EXPECT_EQ("10.0.0.203", client.config_.dns_servers_[1].toText());
+    EXPECT_EQ("10.0.0.202", client.config_.log_servers_[0].toText());
+    EXPECT_EQ("10.0.0.203", client.config_.log_servers_[1].toText());
 
     // Check that we can send another DHCPINFORM message using
     // different ciaddr and we will get the configuration.
@@ -313,14 +313,14 @@ TEST_F(InformTest, relayedClient) {
     ASSERT_EQ(2, client.config_.dns_servers_.size());
     EXPECT_EQ("192.0.2.202", client.config_.dns_servers_[0].toText());
     EXPECT_EQ("192.0.2.203", client.config_.dns_servers_[1].toText());
-    // Make sure that the Log Servers option has been received.
-    ASSERT_EQ(2, client.config_.quotes_servers_.size());
-    EXPECT_EQ("192.0.2.200", client.config_.routers_[0].toText());
-    EXPECT_EQ("192.0.2.201", client.config_.routers_[1].toText());
     // Make sure that the Quotes Servers option has been received.
+    ASSERT_EQ(2, client.config_.quotes_servers_.size());
+    EXPECT_EQ("10.0.0.202", client.config_.quotes_servers_[0].toText());
+    EXPECT_EQ("10.0.0.203", client.config_.quotes_servers_[1].toText());
+    // Make sure that the Log Servers option has been received.
     ASSERT_EQ(2, client.config_.log_servers_.size());
-    EXPECT_EQ("192.0.2.202", client.config_.dns_servers_[0].toText());
-    EXPECT_EQ("192.0.2.203", client.config_.dns_servers_[1].toText());
+    EXPECT_EQ("10.0.0.200", client.config_.log_servers_[0].toText());
+    EXPECT_EQ("10.0.0.201", client.config_.log_servers_[1].toText());
 }
 
 // This test checks that the server can respond to the DHCPINFORM message

+ 1 - 1
src/lib/dhcpsrv/host.cc

@@ -81,7 +81,7 @@ Host::Host(const uint8_t* identifier, const size_t identifier_len,
       ipv4_reservation_(asiolink::IOAddress::IPV4_ZERO_ADDRESS()),
       hostname_(hostname), dhcp4_client_classes_(dhcp4_client_classes),
       dhcp6_client_classes_(dhcp6_client_classes), host_id_(0),
-      cfg_option4_(), cfg_option6_() {
+      cfg_option4_(new CfgOption()), cfg_option6_(new CfgOption()) {
 
     // Initialize host identifier.
     setIdentifier(identifier, identifier_len, identifier_type);