From: Marcin Siodelski Date: Fri, 1 Dec 2017 16:13:11 +0000 (+0100) Subject: [5442] Store network states in DHCPv4 and DHCPv6 servers. X-Git-Tag: trac5443_base~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cff0cf5a8a2cbfd4e787b6a092e8f8851a9d0d12;p=thirdparty%2Fkea.git [5442] Store network states in DHCPv4 and DHCPv6 servers. --- diff --git a/src/bin/dhcp4/dhcp4_srv.cc b/src/bin/dhcp4/dhcp4_srv.cc index 8912441fa4..6dda15b7e2 100644 --- a/src/bin/dhcp4/dhcp4_srv.cc +++ b/src/bin/dhcp4/dhcp4_srv.cc @@ -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; diff --git a/src/bin/dhcp4/dhcp4_srv.h b/src/bin/dhcp4/dhcp4_srv.h index 2fa90f127e..a353ce8723 100644 --- a/src/bin/dhcp4/dhcp4_srv.h +++ b/src/bin/dhcp4/dhcp4_srv.h @@ -15,10 +15,11 @@ #include #include #include -#include -#include #include #include +#include +#include +#include #include #include @@ -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 diff --git a/src/bin/dhcp6/dhcp6_srv.cc b/src/bin/dhcp6/dhcp6_srv.cc index a0aa2cb111..df488d29c3 100644 --- a/src/bin/dhcp6/dhcp6_srv.cc +++ b/src/bin/dhcp6/dhcp6_srv.cc @@ -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; diff --git a/src/bin/dhcp6/dhcp6_srv.h b/src/bin/dhcp6/dhcp6_srv.h index ed7a103005..57ab326d98 100644 --- a/src/bin/dhcp6/dhcp6_srv.h +++ b/src/bin/dhcp6/dhcp6_srv.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -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 name_change_reqs_; + +private: + + /// @brief Holds information about disabled DHCP service and/or + /// disabled subnet/network scopes. + NetworkState network_state_; + }; }; // namespace isc::dhcp diff --git a/src/lib/dhcpsrv/network_state.cc b/src/lib/dhcpsrv/network_state.cc index 3b29f66789..6a0f9ea319 100644 --- a/src/lib/dhcpsrv/network_state.cc +++ b/src/lib/dhcpsrv/network_state.cc @@ -18,9 +18,14 @@ class NetworkStateImpl : public boost::enable_shared_from_this 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 diff --git a/src/lib/dhcpsrv/network_state.h b/src/lib/dhcpsrv/network_state.h index 300d91dff1..b5434d6a05 100644 --- a/src/lib/dhcpsrv/network_state.h +++ b/src/lib/dhcpsrv/network_state.h @@ -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 Subnets; @@ -52,7 +58,7 @@ public: typedef std::set Networks; /// @brief Constructor. - NetworkState(); + NetworkState(const ServerType& server_type); /// @brief Globally disables DHCP service. /// @@ -134,6 +140,9 @@ private: boost::shared_ptr impl_; }; +/// @brief Pointer to the @c NetworkState object. +typedef boost::shared_ptr NetworkStatePtr; + } // end of namespace isc::dhcp } // end of namespace isc diff --git a/src/lib/dhcpsrv/tests/network_state_unittest.cc b/src/lib/dhcpsrv/tests/network_state_unittest.cc index 13f6396d73..ab2837b350 100644 --- a/src/lib/dhcpsrv/tests/network_state_unittest.cc +++ b/src/lib/dhcpsrv/tests/network_state_unittest.cc @@ -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.