From: Francis Dupont Date: Sat, 13 Dec 2025 13:44:00 +0000 (+0100) Subject: [#4258] Changed ctor X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=74f53cfef33ddadca558a19ac51439d5c00f7d96;p=thirdparty%2Fkea.git [#4258] Changed ctor --- diff --git a/src/lib/dhcp/iface_mgr.cc b/src/lib/dhcp/iface_mgr.cc index 121bbe0262..bd4016e78e 100644 --- a/src/lib/dhcp/iface_mgr.cc +++ b/src/lib/dhcp/iface_mgr.cc @@ -341,8 +341,7 @@ IfaceMgr::addExternalSocket(int socketfd, SocketCallback callback) { .arg(id_); } // New entry. - SocketCallbackInfo x; - x.socket_ = socketfd; + SocketCallbackInfo x(socketfd); x.callback_ = callback; std::lock_guard lock(callbacks_mutex_); auto& idx = callbacks_.get<1>(); @@ -1264,7 +1263,7 @@ Pkt4Ptr IfaceMgr::receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec / } // Let's find out which external socket has the data - SocketCallbackInfo ex_sock; + boost::scoped_ptr ex_sock; bool found = false; { std::lock_guard lock(callbacks_mutex_); @@ -1281,18 +1280,18 @@ Pkt4Ptr IfaceMgr::receive4Indirect(uint32_t timeout_sec, uint32_t timeout_usec / if (it->callback_) { // Note the external socket to call its callback without // the lock taken so it can be deleted. - ex_sock = *it; + ex_sock.reset(new SocketCallbackInfo(*it)); break; } } } } - if (ex_sock.callback_) { + if (ex_sock && ex_sock->callback_) { // Calling the external socket's callback provides its service // layer access without integrating any specific features // in IfaceMgr - ex_sock.callback_(ex_sock.socket_); + ex_sock->callback_(ex_sock->socket_); } if (found) { return (Pkt4Ptr()); @@ -1373,7 +1372,7 @@ Pkt4Ptr IfaceMgr::receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec /* } // Let's find out which socket has the data - SocketCallbackInfo ex_sock; + boost::scoped_ptr ex_sock; bool found = false; { std::lock_guard lock(callbacks_mutex_); @@ -1390,18 +1389,18 @@ Pkt4Ptr IfaceMgr::receive4Direct(uint32_t timeout_sec, uint32_t timeout_usec /* if (it->callback_) { // Note the external socket to call its callback without // the lock taken so it can be deleted. - ex_sock = *it; + ex_sock.reset(new SocketCallbackInfo(*it)); break; } } } } - if (ex_sock.callback_) { + if (ex_sock && ex_sock->callback_) { // Calling the external socket's callback provides its service // layer access without integrating any specific features // in IfaceMgr - ex_sock.callback_(ex_sock.socket_); + ex_sock->callback_(ex_sock->socket_); } if (found) { return (Pkt4Ptr()); @@ -1523,7 +1522,7 @@ IfaceMgr::receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ ) } // Let's find out which socket has the data - SocketCallbackInfo ex_sock; + boost::scoped_ptr ex_sock; bool found = false; { std::lock_guard lock(callbacks_mutex_); @@ -1540,18 +1539,18 @@ IfaceMgr::receive6Direct(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ ) if (it->callback_) { // Note the external socket to call its callback without // the lock taken so it can be deleted. - ex_sock = *it; + ex_sock.reset(new SocketCallbackInfo(*it)); break; } } } } - if (ex_sock.callback_) { + if (ex_sock && ex_sock->callback_) { // Calling the external socket's callback provides its service // layer access without integrating any specific features // in IfaceMgr - ex_sock.callback_(ex_sock.socket_); + ex_sock->callback_(ex_sock->socket_); } if (found) { return (Pkt6Ptr()); @@ -1673,7 +1672,7 @@ IfaceMgr::receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ } // Let's find out which external socket has the data - SocketCallbackInfo ex_sock; + boost::scoped_ptr ex_sock; bool found = false; { std::lock_guard lock(callbacks_mutex_); @@ -1690,18 +1689,18 @@ IfaceMgr::receive6Indirect(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */ if (it->callback_) { // Note the external socket to call its callback without // the lock taken so it can be deleted. - ex_sock = *it; + ex_sock.reset(new SocketCallbackInfo(*it)); break; } } } } - if (ex_sock.callback_) { + if (ex_sock && ex_sock->callback_) { // Calling the external socket's callback provides its service // layer access without integrating any specific features // in IfaceMgr - ex_sock.callback_(ex_sock.socket_); + ex_sock->callback_(ex_sock->socket_); } if (found) { return (Pkt6Ptr()); diff --git a/src/lib/dhcp/iface_mgr.h b/src/lib/dhcp/iface_mgr.h index 5d81016640..052501d8f4 100644 --- a/src/lib/dhcp/iface_mgr.h +++ b/src/lib/dhcp/iface_mgr.h @@ -709,7 +709,10 @@ public: bool unusable_; /// @brief Constructor. - SocketCallbackInfo() : socket_(-1), unusable_(false) { + /// + /// @param socket The socket descriptor. + SocketCallbackInfo(int socket) + : socket_(socket), callback_(0), unusable_(false) { } }; @@ -1680,6 +1683,9 @@ private: /// @brief Handle closed external socket. /// + /// @note: the caller must take the lock when it generates + /// the argument and calls this method. + /// /// @param it The external socket info iterator. void handleClosedExternalSocket(SocketCallbackInfoIterator it); diff --git a/src/lib/dhcp/tests/iface_mgr_unittest.cc b/src/lib/dhcp/tests/iface_mgr_unittest.cc index c1a3d5a2c1..9068ce616e 100644 --- a/src/lib/dhcp/tests/iface_mgr_unittest.cc +++ b/src/lib/dhcp/tests/iface_mgr_unittest.cc @@ -3720,16 +3720,14 @@ TEST(SocketCallbackInfoContainer, replace) { return (seq.str()); }; for (int i = 0; i < 9; ++i) { - IfaceMgr::SocketCallbackInfo s; - s.socket_ = i; + IfaceMgr::SocketCallbackInfo s(i); callbacks.push_back(s); } EXPECT_EQ("0\n1\n2\n3\n4\n5\n6\n7\n8\n", getSequence()); auto& idx = callbacks.get<1>(); auto it = idx.find(5); ASSERT_NE(it, idx.end()); - IfaceMgr::SocketCallbackInfo x; - x.socket_ = 9; + IfaceMgr::SocketCallbackInfo x(9); EXPECT_TRUE(idx.replace(it, x)); EXPECT_EQ("0\n1\n2\n3\n4\n9\n6\n7\n8\n", getSequence()); }