Browse Source

[3715] Use foreach for iterating over the IfaceMgr sockets.

Marcin Siodelski 10 years ago
parent
commit
0ee8be13f0

+ 3 - 8
src/bin/perfdhcp/test_control.cc

@@ -67,15 +67,10 @@ TestControl::TestControlSocket::~TestControlSocket() {
 void
 void
 TestControl::TestControlSocket::initSocketData() {
 TestControl::TestControlSocket::initSocketData() {
     BOOST_FOREACH(IfacePtr iface, IfaceMgr::instance().getIfaces()) {
     BOOST_FOREACH(IfacePtr iface, IfaceMgr::instance().getIfaces()) {
-        const Iface::SocketCollection& socket_collection =
-            iface->getSockets();
-        for (Iface::SocketCollection::const_iterator s =
-                 socket_collection.begin();
-             s != socket_collection.end();
-             ++s) {
-            if (s->sockfd_ == sockfd_) {
+        BOOST_FOREACH(SocketInfo s, iface->getSockets()) {
+            if (s.sockfd_ == sockfd_) {
                 ifindex_ = iface->getIndex();
                 ifindex_ = iface->getIndex();
-                addr_ = s->addr_;
+                addr_ = s.addr_;
                 return;
                 return;
             }
             }
         }
         }

+ 22 - 33
src/lib/dhcp/iface_mgr.cc

@@ -367,11 +367,9 @@ bool
 IfaceMgr::hasOpenSocket(const uint16_t family) const {
 IfaceMgr::hasOpenSocket(const uint16_t family) const {
     // Iterate over all interfaces and search for open sockets.
     // Iterate over all interfaces and search for open sockets.
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
-        const Iface::SocketCollection& sockets = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator sock = sockets.begin();
-             sock != sockets.end(); ++sock) {
+        BOOST_FOREACH(SocketInfo sock, iface->getSockets()) {
             // Check if the socket matches specified family.
             // Check if the socket matches specified family.
-            if (sock->family_ == family) {
+            if (sock.family_ == family) {
                 // There is at least one socket open, so return.
                 // There is at least one socket open, so return.
                 return (true);
                 return (true);
             }
             }
@@ -385,14 +383,13 @@ bool
 IfaceMgr::hasOpenSocket(const IOAddress& addr) const {
 IfaceMgr::hasOpenSocket(const IOAddress& addr) const {
     // Iterate over all interfaces and search for open sockets.
     // Iterate over all interfaces and search for open sockets.
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
-        const Iface::SocketCollection& sockets = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator sock = sockets.begin();
-             sock != sockets.end(); ++sock) {
+        BOOST_FOREACH(SocketInfo sock, iface->getSockets()) {
             // Check if the socket address matches the specified address or
             // Check if the socket address matches the specified address or
             // if address is unspecified (in6addr_any).
             // if address is unspecified (in6addr_any).
-            if (sock->addr_ == addr) {
+            if (sock.addr_ == addr) {
                 return (true);
                 return (true);
-            } else if (sock->addr_ == IOAddress("::")) {
+
+            } else if (sock.addr_ == IOAddress("::")) {
                 // Handle the case that the address is unspecified (any).
                 // Handle the case that the address is unspecified (any).
                 // In this case, we should check if the specified address
                 // In this case, we should check if the specified address
                 // belongs to any of the interfaces.
                 // belongs to any of the interfaces.
@@ -896,17 +893,15 @@ IfaceMgr::receive4(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */) {
     /// and then use its copy for select(). Please note that select() modifies
     /// and then use its copy for select(). Please note that select() modifies
     /// provided set to indicated which sockets have something to read.
     /// provided set to indicated which sockets have something to read.
     BOOST_FOREACH(iface, ifaces_) {
     BOOST_FOREACH(iface, ifaces_) {
-        const Iface::SocketCollection& socket_collection = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator s = socket_collection.begin();
-             s != socket_collection.end(); ++s) {
+        BOOST_FOREACH(SocketInfo s, iface->getSockets()) {
 
 
             // Only deal with IPv4 addresses.
             // Only deal with IPv4 addresses.
-            if (s->addr_.isV4()) {
+            if (s.addr_.isV4()) {
 
 
                 // Add this socket to listening set
                 // Add this socket to listening set
-                FD_SET(s->sockfd_, &sockets);
-                if (maxfd < s->sockfd_) {
-                    maxfd = s->sockfd_;
+                FD_SET(s.sockfd_, &sockets);
+                if (maxfd < s.sockfd_) {
+                    maxfd = s.sockfd_;
                 }
                 }
             }
             }
         }
         }
@@ -969,11 +964,9 @@ IfaceMgr::receive4(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */) {
 
 
     // Let's find out which interface/socket has the data
     // Let's find out which interface/socket has the data
     BOOST_FOREACH(iface, ifaces_) {
     BOOST_FOREACH(iface, ifaces_) {
-        const Iface::SocketCollection& socket_collection = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator s = socket_collection.begin();
-             s != socket_collection.end(); ++s) {
-            if (FD_ISSET(s->sockfd_, &sockets)) {
-                candidate = &(*s);
+        BOOST_FOREACH(SocketInfo s, iface->getSockets()) {
+            if (FD_ISSET(s.sockfd_, &sockets)) {
+                candidate = &(s);
                 break;
                 break;
             }
             }
         }
         }
@@ -1008,17 +1001,15 @@ Pkt6Ptr IfaceMgr::receive6(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */
     /// and then use its copy for select(). Please note that select() modifies
     /// and then use its copy for select(). Please note that select() modifies
     /// provided set to indicated which sockets have something to read.
     /// provided set to indicated which sockets have something to read.
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
-        const Iface::SocketCollection& socket_collection = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator s = socket_collection.begin();
-             s != socket_collection.end(); ++s) {
 
 
+        BOOST_FOREACH(SocketInfo s, iface->getSockets()) {
             // Only deal with IPv6 addresses.
             // Only deal with IPv6 addresses.
-            if (s->addr_.isV6()) {
+            if (s.addr_.isV6()) {
 
 
                 // Add this socket to listening set
                 // Add this socket to listening set
-                FD_SET(s->sockfd_, &sockets);
-                if (maxfd < s->sockfd_) {
-                    maxfd = s->sockfd_;
+                FD_SET(s.sockfd_, &sockets);
+                if (maxfd < s.sockfd_) {
+                    maxfd = s.sockfd_;
                 }
                 }
             }
             }
         }
         }
@@ -1083,11 +1074,9 @@ Pkt6Ptr IfaceMgr::receive6(uint32_t timeout_sec, uint32_t timeout_usec /* = 0 */
 
 
     // Let's find out which interface/socket has the data
     // Let's find out which interface/socket has the data
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
     BOOST_FOREACH(IfacePtr iface, ifaces_) {
-        const Iface::SocketCollection& socket_collection = iface->getSockets();
-        for (Iface::SocketCollection::const_iterator s = socket_collection.begin();
-             s != socket_collection.end(); ++s) {
-            if (FD_ISSET(s->sockfd_, &sockets)) {
-                candidate = &(*s);
+        BOOST_FOREACH(SocketInfo s, iface->getSockets()) {
+            if (FD_ISSET(s.sockfd_, &sockets)) {
+                candidate = &(s);
                 break;
                 break;
             }
             }
         }
         }

+ 11 - 15
src/lib/dhcp/tests/iface_mgr_test_config.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -19,6 +19,8 @@
 #include <dhcp/tests/pkt_filter_test_stub.h>
 #include <dhcp/tests/pkt_filter_test_stub.h>
 #include <dhcp/tests/pkt_filter6_test_stub.h>
 #include <dhcp/tests/pkt_filter6_test_stub.h>
 
 
+#include <boost/foreach.hpp>
+
 using namespace isc::asiolink;
 using namespace isc::asiolink;
 
 
 namespace isc {
 namespace isc {
@@ -151,10 +153,8 @@ IfaceMgrTestConfig::socketOpen(const std::string& iface_name,
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
     }
     }
 
 
-    const Iface::SocketCollection& sockets = iface->getSockets();
-    for (Iface::SocketCollection::const_iterator sock = sockets.begin();
-         sock != sockets.end(); ++sock) {
-        if (sock->family_ == family) {
+    BOOST_FOREACH(SocketInfo sock, iface->getSockets()) {
+        if (sock.family_ == family) {
             return (true);
             return (true);
         }
         }
     }
     }
@@ -169,11 +169,9 @@ IfaceMgrTestConfig::socketOpen(const std::string& iface_name,
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
     }
     }
 
 
-    const Iface::SocketCollection& sockets = iface->getSockets();
-    for (Iface::SocketCollection::const_iterator sock = sockets.begin();
-         sock != sockets.end(); ++sock) {
-        if ((sock->family_ == AF_INET) &&
-            (sock->addr_ == IOAddress(address))) {
+    BOOST_FOREACH(SocketInfo sock, iface->getSockets()) {
+        if ((sock.family_ == AF_INET) &&
+            (sock.addr_ == IOAddress(address))) {
             return (true);
             return (true);
         }
         }
     }
     }
@@ -187,11 +185,9 @@ IfaceMgrTestConfig::unicastOpen(const std::string& iface_name) const {
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
         isc_throw(Unexpected, "No such interface '" << iface_name << "'");
     }
     }
 
 
-    const Iface::SocketCollection& sockets = iface->getSockets();
-    for (Iface::SocketCollection::const_iterator sock = sockets.begin();
-         sock != sockets.end(); ++sock) {
-        if ((!sock->addr_.isV6LinkLocal()) &&
-            (!sock->addr_.isV6Multicast())) {
+    BOOST_FOREACH(SocketInfo sock, iface->getSockets()) {
+        if ((!sock.addr_.isV6LinkLocal()) &&
+            (!sock.addr_.isV6Multicast())) {
             return (true);
             return (true);
         }
         }
     }
     }

+ 5 - 5
src/lib/dhcp/tests/pkt_filter6_test_utils.cc

@@ -1,4 +1,4 @@
-// Copyright (C) 2013-2014 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2013-2015 Internet Systems Consortium, Inc. ("ISC")
 //
 //
 // Permission to use, copy, modify, and/or distribute this software for any
 // Permission to use, copy, modify, and/or distribute this software for any
 // purpose with or without fee is hereby granted, provided that the above
 // purpose with or without fee is hereby granted, provided that the above
@@ -16,6 +16,8 @@
 #include <dhcp/pkt6.h>
 #include <dhcp/pkt6.h>
 #include <dhcp/tests/pkt_filter6_test_utils.h>
 #include <dhcp/tests/pkt_filter6_test_utils.h>
 
 
+#include <boost/foreach.hpp>
+
 #include <arpa/inet.h>
 #include <arpa/inet.h>
 #include <netinet/in.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
 #include <sys/socket.h>
@@ -185,10 +187,8 @@ PktFilter6Stub::openSocket(const Iface& iface, const isc::asiolink::IOAddress& a
                            const uint16_t port, const bool) {
                            const uint16_t port, const bool) {
     // Check if there is any other socket bound to the specified address
     // Check if there is any other socket bound to the specified address
     // and port on this interface.
     // and port on this interface.
-    const Iface::SocketCollection& sockets = iface.getSockets();
-    for (Iface::SocketCollection::const_iterator socket = sockets.begin();
-         socket != sockets.end(); ++socket) {
-        if ((socket->addr_ == addr) && (socket->port_ == port)) {
+    BOOST_FOREACH(SocketInfo socket, iface.getSockets()) {
+        if ((socket.addr_ == addr) && (socket.port_ == port)) {
             isc_throw(SocketConfigError, "test socket bind error");
             isc_throw(SocketConfigError, "test socket bind error");
         }
         }
     }
     }