From: Marcin Siodelski Date: Fri, 13 Mar 2015 10:16:45 +0000 (+0100) Subject: [3715] Use foreach for iterating over the IfaceMgr sockets. X-Git-Tag: trac3764_base~10^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0ee8be13f0a3aadfb2ff8802c1b54433e837aea1;p=thirdparty%2Fkea.git [3715] Use foreach for iterating over the IfaceMgr sockets. --- diff --git a/src/bin/perfdhcp/test_control.cc b/src/bin/perfdhcp/test_control.cc index 9575df7e91..1f9f9d3ff4 100644 --- a/src/bin/perfdhcp/test_control.cc +++ b/src/bin/perfdhcp/test_control.cc @@ -67,15 +67,10 @@ TestControl::TestControlSocket::~TestControlSocket() { void TestControl::TestControlSocket::initSocketData() { 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(); - addr_ = s->addr_; + addr_ = s.addr_; return; } } diff --git a/src/lib/dhcp/iface_mgr.cc b/src/lib/dhcp/iface_mgr.cc index b74789ee30..f904cfce1a 100644 --- a/src/lib/dhcp/iface_mgr.cc +++ b/src/lib/dhcp/iface_mgr.cc @@ -367,11 +367,9 @@ bool IfaceMgr::hasOpenSocket(const uint16_t family) const { // Iterate over all interfaces and search for open sockets. 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. - if (sock->family_ == family) { + if (sock.family_ == family) { // There is at least one socket open, so return. return (true); } @@ -385,14 +383,13 @@ bool IfaceMgr::hasOpenSocket(const IOAddress& addr) const { // Iterate over all interfaces and search for open sockets. 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 // if address is unspecified (in6addr_any). - if (sock->addr_ == addr) { + if (sock.addr_ == addr) { return (true); - } else if (sock->addr_ == IOAddress("::")) { + + } else if (sock.addr_ == IOAddress("::")) { // Handle the case that the address is unspecified (any). // In this case, we should check if the specified address // 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 /// provided set to indicated which sockets have something to read. 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. - if (s->addr_.isV4()) { + if (s.addr_.isV4()) { // 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 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; } } @@ -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 /// provided set to indicated which sockets have something to read. 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. - if (s->addr_.isV6()) { + if (s.addr_.isV6()) { // 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 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; } } diff --git a/src/lib/dhcp/tests/iface_mgr_test_config.cc b/src/lib/dhcp/tests/iface_mgr_test_config.cc index a021b83336..52fc5d2c0f 100644 --- a/src/lib/dhcp/tests/iface_mgr_test_config.cc +++ b/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 // purpose with or without fee is hereby granted, provided that the above @@ -19,6 +19,8 @@ #include #include +#include + using namespace isc::asiolink; namespace isc { @@ -151,10 +153,8 @@ IfaceMgrTestConfig::socketOpen(const std::string& 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); } } @@ -169,11 +169,9 @@ IfaceMgrTestConfig::socketOpen(const std::string& 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); } } @@ -187,11 +185,9 @@ IfaceMgrTestConfig::unicastOpen(const std::string& iface_name) const { 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); } } diff --git a/src/lib/dhcp/tests/pkt_filter6_test_utils.cc b/src/lib/dhcp/tests/pkt_filter6_test_utils.cc index 339a6e64b1..59dd4b5fca 100644 --- a/src/lib/dhcp/tests/pkt_filter6_test_utils.cc +++ b/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 // purpose with or without fee is hereby granted, provided that the above @@ -16,6 +16,8 @@ #include #include +#include + #include #include #include @@ -185,10 +187,8 @@ PktFilter6Stub::openSocket(const Iface& iface, const isc::asiolink::IOAddress& a const uint16_t port, const bool) { // Check if there is any other socket bound to the specified address // 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"); } }