]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[5442] Store network states in DHCPv4 and DHCPv6 servers.
authorMarcin Siodelski <marcin@isc.org>
Fri, 1 Dec 2017 16:13:11 +0000 (17:13 +0100)
committerMarcin Siodelski <marcin@isc.org>
Fri, 1 Dec 2017 16:13:11 +0000 (17:13 +0100)
src/bin/dhcp4/dhcp4_srv.cc
src/bin/dhcp4/dhcp4_srv.h
src/bin/dhcp6/dhcp6_srv.cc
src/bin/dhcp6/dhcp6_srv.h
src/lib/dhcpsrv/network_state.cc
src/lib/dhcpsrv/network_state.h
src/lib/dhcpsrv/tests/network_state_unittest.cc

index 8912441fa41208520fd994510bb1f26a7a4fa8f7..6dda15b7e2ebd48ace0e91d0927533aca7900d59 100644 (file)
@@ -416,7 +416,7 @@ const std::string Dhcpv4Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
 Dhcpv4Srv::Dhcpv4Srv(uint16_t port, const bool use_bcast,
                      const bool direct_response_desired)
     : io_service_(new IOService()), shutdown_(true), alloc_engine_(), port_(port),
-      use_bcast_(use_bcast) {
+      use_bcast_(use_bcast), network_state_(NetworkState::DHCPv4) {
 
     LOG_DEBUG(dhcp4_logger, DBG_DHCP4_START, DHCP4_OPEN_SOCKET).arg(port);
     try {
@@ -804,7 +804,10 @@ Dhcpv4Srv::run_one() {
         return;
     }
 
-    processPacket(query, rsp);
+    // If the DHCP service has been globally disabled, drop the packet.
+    if (network_state_.isServiceEnabled()) {
+        processPacket(query, rsp);
+    }
 
     if (!rsp) {
         return;
index 2fa90f127e3da6782f16cd6a578ab68d80d1fced..a353ce8723486fe0a17503835f06c86a61e94fb3 100644 (file)
 #include <dhcp/option4_client_fqdn.h>
 #include <dhcp/option_custom.h>
 #include <dhcp_ddns/ncr_msg.h>
-#include <dhcpsrv/d2_client_mgr.h>
-#include <dhcpsrv/subnet.h>
 #include <dhcpsrv/alloc_engine.h>
 #include <dhcpsrv/cfg_option.h>
+#include <dhcpsrv/d2_client_mgr.h>
+#include <dhcpsrv/network_state.h>
+#include <dhcpsrv/subnet.h>
 #include <hooks/callout_handle.h>
 #include <dhcpsrv/daemon.h>
 
@@ -840,6 +841,10 @@ private:
     uint16_t port_;  ///< UDP port number on which server listens.
     bool use_bcast_; ///< Should broadcast be enabled on sockets (if true).
 
+    /// @brief Holds information about disabled DHCP service and/or
+    /// disabled subnet/network scopes.
+    NetworkState network_state_;
+
 public:
     /// Class methods for DHCPv4-over-DHCPv6 handler
 
index a0aa2cb111b9edc2d12956970cf91cf15d006240..df488d29c3e7be999c3521af7e20a7db29063953 100644 (file)
@@ -179,7 +179,7 @@ const std::string Dhcpv6Srv::VENDOR_CLASS_PREFIX("VENDOR_CLASS_");
 
 Dhcpv6Srv::Dhcpv6Srv(uint16_t port)
     : io_service_(new IOService()), port_(port), serverid_(), shutdown_(true),
-      alloc_engine_()
+      alloc_engine_(), name_change_reqs_(), network_state_(NetworkState::DHCPv6)
 {
 
     LOG_DEBUG(dhcp6_logger, DBG_DHCP6_START, DHCP6_OPEN_SOCKET).arg(port);
@@ -468,7 +468,10 @@ void Dhcpv6Srv::run_one() {
         return;
     }
 
-    processPacket(query, rsp);
+    // If the DHCP service has been globally disabled, drop the packet.
+    if (network_state_.isServiceEnabled()) {
+        processPacket(query, rsp);
+    }
 
     if (!rsp) {
         return;
index ed7a10300583c614f1661d64eea886ba83d2aefb..57ab326d985402e4f90daf8e2be4e9878dcc0d15 100644 (file)
@@ -19,6 +19,7 @@
 #include <dhcpsrv/alloc_engine.h>
 #include <dhcpsrv/cfg_option.h>
 #include <dhcpsrv/d2_client_mgr.h>
+#include <dhcpsrv/network_state.h>
 #include <dhcpsrv/subnet.h>
 #include <hooks/callout_handle.h>
 #include <dhcpsrv/daemon.h>
@@ -871,6 +872,13 @@ protected:
     /// Holds a list of @c isc::dhcp_ddns::NameChangeRequest objects, which
     /// are waiting for sending to kea-dhcp-ddns module.
     std::queue<isc::dhcp_ddns::NameChangeRequest> name_change_reqs_;
+
+private:
+
+    /// @brief Holds information about disabled DHCP service and/or
+    /// disabled subnet/network scopes.
+    NetworkState network_state_;
+
 };
 
 }; // namespace isc::dhcp
index 3b29f667898f6b9cc96107874536728f90004713..6a0f9ea3198b462455a3a3247e341cc952c5ff8e 100644 (file)
@@ -18,9 +18,14 @@ class NetworkStateImpl : public boost::enable_shared_from_this<NetworkStateImpl>
 public:
 
     /// @brief Constructor.
-    NetworkStateImpl()
-        : globally_disabled_(false), disabled_subnets_(), disabled_networks_(),
-          timer_present_(false), timer_mgr_(TimerMgr::instance()) {
+    NetworkStateImpl(const NetworkState::ServerType& server_type)
+        : server_type_(server_type), globally_disabled_(false), disabled_subnets_(),
+          disabled_networks_(), timer_present_(false), timer_mgr_(TimerMgr::instance()) {
+    }
+
+    /// @brief Destructor.
+    ~NetworkStateImpl() {
+        destroyTimer();
     }
 
     /// @brief Globally disables or enables DHCP service.
@@ -66,6 +71,9 @@ public:
         }
     }
 
+    /// @brief Server type.
+    NetworkState::ServerType server_type_;
+
     /// @brief A flag indicating if DHCP service is globally disabled.
     bool globally_disabled_;
 
@@ -86,8 +94,8 @@ public:
     TimerMgrPtr timer_mgr_;
 };
 
-NetworkState::NetworkState()
-    : impl_(new NetworkStateImpl()) {
+NetworkState::NetworkState(const NetworkState::ServerType& server_type)
+    : impl_(new NetworkStateImpl(server_type)) {
 }
 
 void
index 300d91dff166237c1afb4a676834e2796c6765ab..b5434d6a0590861fa06da0113e89306fcbf43ee5 100644 (file)
@@ -45,6 +45,12 @@ class NetworkStateImpl;
 class NetworkState {
 public:
 
+    /// @brief DHCP server type.
+    enum ServerType {
+        DHCPv4,
+        DHCPv6
+    };
+
     /// @brief Type of the container holding collection of subnet identifiers.
     typedef std::set<SubnetID> Subnets;
 
@@ -52,7 +58,7 @@ public:
     typedef std::set<std::string> Networks;
 
     /// @brief Constructor.
-    NetworkState();
+    NetworkState(const ServerType& server_type);
 
     /// @brief Globally disables DHCP service.
     ///
@@ -134,6 +140,9 @@ private:
     boost::shared_ptr<NetworkStateImpl> impl_;
 };
 
+/// @brief Pointer to the @c NetworkState object.
+typedef boost::shared_ptr<NetworkState> NetworkStatePtr;
+
 } // end of namespace isc::dhcp
 } // end of namespace isc
 
index 13f6396d73afebbe1114ffd83702b72da98a890d..ab2837b350012a61e98ff70a28016374ad9b7315 100644 (file)
@@ -65,7 +65,7 @@ public:
 
 // This test verifies that it is possible to disable and then enable service.
 TEST_F(NetworkStateTest, disableEnableService) {
-    NetworkState state;
+    NetworkState state(NetworkState::DHCPv4);
     state.disableService();
     EXPECT_FALSE(state.isServiceEnabled());
     state.enableService();
@@ -75,7 +75,7 @@ TEST_F(NetworkStateTest, disableEnableService) {
 // This test verifies that enableAll() enables the service. This test will be extended
 // in the future to verify that it also enables disabled scopes.
 TEST_F(NetworkStateTest, enableAll) {
-    NetworkState state;
+    NetworkState state(NetworkState::DHCPv4);
     state.disableService();
     EXPECT_FALSE(state.isServiceEnabled());
     state.enableAll();
@@ -85,7 +85,7 @@ TEST_F(NetworkStateTest, enableAll) {
 // This test verifies that it is possible to setup delayed execution of enableAll
 // function.
 TEST_F(NetworkStateTest, delayedEnableAll) {
-    NetworkState state;
+    NetworkState state(NetworkState::DHCPv4);
     // Disable the service and then schedule enabling it in 1 second.
     state.disableService();
     state.delayedEnableAll(1);
@@ -99,7 +99,7 @@ TEST_F(NetworkStateTest, delayedEnableAll) {
 // This test verifies that explicitly enabling the service cancels the timer
 // scheduled for automatically enabling it.
 TEST_F(NetworkStateTest, earlyEnableAll) {
-    NetworkState state;
+    NetworkState state(NetworkState::DHCPv4);
     // Disable the service.
     state.disableService();
     EXPECT_FALSE(state.isServiceEnabled());
@@ -115,7 +115,7 @@ TEST_F(NetworkStateTest, earlyEnableAll) {
 // This test verifies that it is possible to call delayedEnableAll multiple times
 // and that it results in only one timer being scheduled.
 TEST_F(NetworkStateTest, multipleDelayedEnableAll) {
-    NetworkState state;
+    NetworkState state(NetworkState::DHCPv4);
     // Disable the service and then schedule enabling it in 1 second.
     state.disableService();
     // Schedule the first timer for 5 seconds.