]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[#4258] Changed ctor
authorFrancis Dupont <fdupont@isc.org>
Sat, 13 Dec 2025 13:44:00 +0000 (14:44 +0100)
committerFrancis Dupont <fdupont@isc.org>
Fri, 9 Jan 2026 14:23:27 +0000 (15:23 +0100)
src/lib/dhcp/iface_mgr.cc
src/lib/dhcp/iface_mgr.h
src/lib/dhcp/tests/iface_mgr_unittest.cc

index 121bbe026286d06784423292fcb9295ad2fcec14..bd4016e78ee2e62c07c439df9d7123b5f68e74a1 100644 (file)
@@ -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<std::mutex> 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<SocketCallbackInfo> ex_sock;
         bool found = false;
         {
             std::lock_guard<std::mutex> 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<SocketCallbackInfo> ex_sock;
     bool found = false;
     {
         std::lock_guard<std::mutex> 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<SocketCallbackInfo> ex_sock;
     bool found = false;
     {
         std::lock_guard<std::mutex> 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<SocketCallbackInfo> ex_sock;
         bool found = false;
         {
             std::lock_guard<std::mutex> 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());
index 5d810166409ff1112d7793830ad20826f733e71b..052501d8f402f40fdc3e95c8742bc298bfaa31a4 100644 (file)
@@ -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);
 
index c1a3d5a2c19903e62516c97f72fceb8703c5f437..9068ce616edbc0cdfde1327a0ad73908b0f4ab40 100644 (file)
@@ -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());
 }