From: Thomas Markwalder Date: Fri, 25 Oct 2019 18:24:42 +0000 (-0400) Subject: [#964,!577] Added socket descriptor parameter to IfaceMgr external callback handler X-Git-Tag: Kea-1.7.2~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14742a09cfbe75aeefa49d27931bfb82221d03bb;p=thirdparty%2Fkea.git [#964,!577] Added socket descriptor parameter to IfaceMgr external callback handler src/lib/dhcp/iface_mgr.* typedef SocketCallback - added socket descriptpor parameter Added socket fd to invocations of socket ready callback src/bin/dhcp4/dhcp4to6_ipc.* void Dhcp4to6Ipc::handler(int /* fd */) src/bin/dhcp6/dhcp6to4_ipc.* void Dhcp6to4Ipc::handler(int /* fd */) Dhcp6to4Ipc& ipc = Dhcp6to4Ipc::instance(); Pkt6Ptr pkt; src/hooks/dhcp/high_availability/ha_service.* socketReadyHandler() - initial stub implementation of socket ready handler --- diff --git a/src/bin/dhcp4/dhcp4to6_ipc.cc b/src/bin/dhcp4/dhcp4to6_ipc.cc index 1c698abffd..b78f41e957 100644 --- a/src/bin/dhcp4/dhcp4to6_ipc.cc +++ b/src/bin/dhcp4/dhcp4to6_ipc.cc @@ -52,7 +52,7 @@ void Dhcp4to6Ipc::open() { } } -void Dhcp4to6Ipc::handler() { +void Dhcp4to6Ipc::handler(int /* fd */) { Dhcp4to6Ipc& ipc = Dhcp4to6Ipc::instance(); Pkt6Ptr pkt; diff --git a/src/bin/dhcp4/dhcp4to6_ipc.h b/src/bin/dhcp4/dhcp4to6_ipc.h index 6b098585c5..ba2e1a6a93 100644 --- a/src/bin/dhcp4/dhcp4to6_ipc.h +++ b/src/bin/dhcp4/dhcp4to6_ipc.h @@ -47,7 +47,7 @@ public: /// /// The handler processes the DHCPv4-query DHCPv6 packet and /// sends the DHCPv4-response DHCPv6 packet back to the DHCPv6 server - static void handler(); + static void handler(int /* fd */); }; } // namespace isc diff --git a/src/bin/dhcp6/dhcp6to4_ipc.cc b/src/bin/dhcp6/dhcp6to4_ipc.cc index 02e275db80..0022aced20 100644 --- a/src/bin/dhcp6/dhcp6to4_ipc.cc +++ b/src/bin/dhcp6/dhcp6to4_ipc.cc @@ -54,7 +54,7 @@ void Dhcp6to4Ipc::open() { } } -void Dhcp6to4Ipc::handler() { +void Dhcp6to4Ipc::handler(int /* fd */) { Dhcp6to4Ipc& ipc = Dhcp6to4Ipc::instance(); Pkt6Ptr pkt; diff --git a/src/bin/dhcp6/dhcp6to4_ipc.h b/src/bin/dhcp6/dhcp6to4_ipc.h index d039e8f1b4..1d393e9884 100644 --- a/src/bin/dhcp6/dhcp6to4_ipc.h +++ b/src/bin/dhcp6/dhcp6to4_ipc.h @@ -44,7 +44,7 @@ public: /// @brief On receive handler /// /// The handler sends the DHCPv6 packet back to the remote address - static void handler(); + static void handler(int /* fd */); /// @param client_port UDP port where all responses are sent to. /// Not zero is mostly useful for testing purposes. diff --git a/src/hooks/dhcp/high_availability/ha_service.cc b/src/hooks/dhcp/high_availability/ha_service.cc index 4589a8ad57..0cb08bf658 100644 --- a/src/hooks/dhcp/high_availability/ha_service.cc +++ b/src/hooks/dhcp/high_availability/ha_service.cc @@ -1632,7 +1632,9 @@ HAService::clientConnectHandler(const boost::system::error_code& ec, int tcp_nat // run by an explicit call IOService ready in kea-dhcp code. // We are registerin the socket only to interrupt main-thread // select(). - IfaceMgr::instance().addExternalSocket(tcp_native_fd, 0); + IfaceMgr::instance().addExternalSocket(tcp_native_fd, + boost::bind(&HAService::socketReadyHandler, this, _1) + ); } // If ec.value() == boost::asio::error::already_connected, we should already @@ -1642,6 +1644,16 @@ HAService::clientConnectHandler(const boost::system::error_code& ec, int tcp_nat return (true); } +void +HAService::socketReadyHandler(int tcp_native_fd) { + std::cout << "HAService::socketReadyHandler - ready socket:" << tcp_native_fd << std::endl; + // If the socket is not usable/or in a transaction + // we'll unregister it + // if (socket bad) /{ + // IfaceMgr::instance().deleteExternalSocket(tcp_native_fd, 0); + // } +} + void HAService::clientCloseHandler(int tcp_native_fd) { if (tcp_native_fd >= 0) { diff --git a/src/hooks/dhcp/high_availability/ha_service.h b/src/hooks/dhcp/high_availability/ha_service.h index 6765f7b0ad..48efda89fe 100644 --- a/src/hooks/dhcp/high_availability/ha_service.h +++ b/src/hooks/dhcp/high_availability/ha_service.h @@ -738,6 +738,19 @@ protected: /// error we want Connection logic to process it. bool clientConnectHandler(const boost::system::error_code& ec, int tcp_native_fd); + /// @brief IfaceMgr external socket ready callback handler + /// + /// IfaceMgr invokes this call back when a registered socket has been + /// flagged as ready to read. It is installed by the invocation to + /// register the socket with IfaceMgr made in @ref clientConnectHandler. + /// + /// @todo add logic to determine if the + /// socket is involved in an ongoing transcation or not. If not, it + /// will be unregistered from IfaceMgr. + /// + /// @param tcp_native_fd socket descriptor of the ready socket + void socketReadyHandler(int tcp_native_fd); + /// @brief HttpClient close callback handler /// /// Passed into HttpClient calls to allow unregistration of client's diff --git a/src/lib/dhcp/iface_mgr.cc b/src/lib/dhcp/iface_mgr.cc index 8420ead396..afcdb03863 100644 --- a/src/lib/dhcp/iface_mgr.cc +++ b/src/lib/dhcp/iface_mgr.cc @@ -1089,7 +1089,7 @@ Pkt4Ptr IfaceMgr::receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec / // layer access without integrating any specific features // in IfaceMgr if (s.callback_) { - s.callback_(); + s.callback_(s.socket_); } return (Pkt4Ptr()); @@ -1184,7 +1184,7 @@ Pkt4Ptr IfaceMgr::receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec /* // layer access without integrating any specific features // in IfaceMgr if (s.callback_) { - s.callback_(); + s.callback_(s.socket_); } return (Pkt4Ptr()); @@ -1313,7 +1313,7 @@ IfaceMgr::receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ ) // layer access without integrating any specific features // in IfaceMgr if (s.callback_) { - s.callback_(); + s.callback_(s.socket_); } return (Pkt6Ptr()); @@ -1430,7 +1430,7 @@ IfaceMgr::receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ // layer access without integrating any specific features // in IfaceMgr if (s.callback_) { - s.callback_(); + s.callback_(s.socket_); } return (Pkt6Ptr()); diff --git a/src/lib/dhcp/iface_mgr.h b/src/lib/dhcp/iface_mgr.h index ef17f92745..fc0d044e6d 100644 --- a/src/lib/dhcp/iface_mgr.h +++ b/src/lib/dhcp/iface_mgr.h @@ -478,7 +478,8 @@ boost::function IfaceMgrErrorMsgCallback; class IfaceMgr : public boost::noncopyable { public: /// Defines callback used when data is received over external sockets. - typedef boost::function SocketCallback; + /// @param fd socket descriptor of the ready socket + typedef boost::function SocketCallback; /// Keeps callback information for external sockets. struct SocketCallbackInfo { diff --git a/src/lib/dhcp/tests/iface_mgr_unittest.cc b/src/lib/dhcp/tests/iface_mgr_unittest.cc index c206e5e1e0..bcb3552248 100644 --- a/src/lib/dhcp/tests/iface_mgr_unittest.cc +++ b/src/lib/dhcp/tests/iface_mgr_unittest.cc @@ -695,14 +695,14 @@ public: int pipefd[2]; EXPECT_TRUE(pipe(pipefd) == 0); EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], - [&callback_ok](){ callback_ok = true; })); + [&callback_ok](int /* fd */){ callback_ok = true; })); // Let's create a second pipe and register it as well int secondpipe[2]; EXPECT_TRUE(pipe(secondpipe) == 0); EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], - [&callback2_ok](){ callback2_ok = true; })); + [&callback2_ok](int /* fd */){ callback2_ok = true; })); // Verify a call with no data and normal external sockets works ok. Pkt4Ptr pkt4; @@ -778,14 +778,14 @@ public: int pipefd[2]; EXPECT_TRUE(pipe(pipefd) == 0); EXPECT_NO_THROW(ifacemgr->addExternalSocket(pipefd[0], - [&callback_ok](){ callback_ok = true; })); + [&callback_ok](int /* fd*/){ callback_ok = true; })); // Let's create a second pipe and register it as well int secondpipe[2]; EXPECT_TRUE(pipe(secondpipe) == 0); EXPECT_NO_THROW(ifacemgr->addExternalSocket(secondpipe[0], - [&callback2_ok](){ callback2_ok = true; })); + [&callback2_ok](int /*fd */){ callback2_ok = true; })); // Verify a call with no data and normal external sockets works ok. Pkt6Ptr pkt6; @@ -2749,11 +2749,11 @@ TEST_F(IfaceMgrTest, detectIfaces) { volatile bool callback_ok; volatile bool callback2_ok; -void my_callback(void) { +void my_callback(int /* fd */) { callback_ok = true; } -void my_callback2(void) { +void my_callback2(int /* fd */) { callback2_ok = true; }