From: Marcin Siodelski Date: Fri, 13 Mar 2015 09:53:42 +0000 (+0100) Subject: [3715] Use boost_foreach for iterating over the iface addresses. X-Git-Tag: trac3764_base~10^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53ee5f29c2a62b2430d591105ebfd27c3351c5d2;p=thirdparty%2Fkea.git [3715] Use boost_foreach for iterating over the iface addresses. --- diff --git a/src/bin/perfdhcp/test_control.cc b/src/bin/perfdhcp/test_control.cc index a9e1a09b22..9575df7e91 100644 --- a/src/bin/perfdhcp/test_control.cc +++ b/src/bin/perfdhcp/test_control.cc @@ -25,6 +25,9 @@ #include "perf_pkt4.h" #include "perf_pkt6.h" +#include +#include + #include #include #include @@ -33,8 +36,6 @@ #include #include -#include - using namespace std; using namespace boost::posix_time; using namespace isc; diff --git a/src/bin/perfdhcp/tests/command_options_unittest.cc b/src/bin/perfdhcp/tests/command_options_unittest.cc index 5431018ece..d4aecc6632 100644 --- a/src/bin/perfdhcp/tests/command_options_unittest.cc +++ b/src/bin/perfdhcp/tests/command_options_unittest.cc @@ -786,7 +786,7 @@ TEST_F(CommandOptionsTest, Interface) { // not fail this test. if (ifaces.size() > 0) { // Get the name of the interface we detected. - iface_name = ifaces.begin()->getName(); + iface_name = (*ifaces.begin())->getName(); // Use the name in the command parser. ASSERT_NO_THROW(process("perfdhcp -4 -l " + iface_name + " abc")); // We expect that command parser will detect that argument diff --git a/src/bin/perfdhcp/tests/test_control_unittest.cc b/src/bin/perfdhcp/tests/test_control_unittest.cc index c0d8ed90d8..7bdf7a5256 100644 --- a/src/bin/perfdhcp/tests/test_control_unittest.cc +++ b/src/bin/perfdhcp/tests/test_control_unittest.cc @@ -21,6 +21,7 @@ #include #include +#include #include #include diff --git a/src/lib/dhcp/iface_mgr.cc b/src/lib/dhcp/iface_mgr.cc index 10972d25dc..b74789ee30 100644 --- a/src/lib/dhcp/iface_mgr.cc +++ b/src/lib/dhcp/iface_mgr.cc @@ -193,9 +193,8 @@ IfaceMgr::IfaceMgr() } void Iface::addUnicast(const isc::asiolink::IOAddress& addr) { - for (Iface::AddressCollection::const_iterator i = unicasts_.begin(); - i != unicasts_.end(); ++i) { - if (i->get() == addr) { + BOOST_FOREACH(Address a, unicasts_) { + if (a.get() == addr) { isc_throw(BadValue, "Address " << addr << " already defined on the " << name_ << " interface."); } @@ -207,13 +206,11 @@ bool Iface::getAddress4(isc::asiolink::IOAddress& address) const { // Iterate over existing addresses assigned to the interface. // Try to find the one that is IPv4. - const AddressCollection& addrs = getAddresses(); - for (AddressCollection::const_iterator addr = addrs.begin(); - addr != addrs.end(); ++addr) { + BOOST_FOREACH(Address addr, getAddresses()) { // If address is IPv4, we assign it to the function argument // and return true. - if (addr->get().isV4()) { - address = addr->get(); + if (addr.get().isV4()) { + address = addr.get(); return (true); } } @@ -223,10 +220,8 @@ Iface::getAddress4(isc::asiolink::IOAddress& address) const { bool Iface::hasAddress(const isc::asiolink::IOAddress& address) const { - const AddressCollection& addrs = getAddresses(); - for (AddressCollection::const_iterator addr = addrs.begin(); - addr != addrs.end(); ++addr) { - if (address == addr->get()) { + BOOST_FOREACH(Address addr, getAddresses()) { + if (address == addr.get()) { return (true); } } @@ -235,7 +230,7 @@ Iface::hasAddress(const isc::asiolink::IOAddress& address) const { void Iface::addAddress(const isc::asiolink::IOAddress& addr) { - addrs_.push_back(OptionalValue(addr, OptionalValueState(true))); + addrs_.push_back(Address(addr, OptionalValueState(true))); } void @@ -262,9 +257,8 @@ Iface::setActive(const bool active) { unsigned int Iface::countActive4() const { uint16_t count = 0; - for (AddressCollection::const_iterator addr_it = addrs_.begin(); - addr_it != addrs_.end(); ++addr_it) { - if (addr_it->get().isV4() && addr_it->isSpecified()) { + BOOST_FOREACH(Address addr, addrs_) { + if (addr.get().isV4() && addr.isSpecified()) { ++count; } } @@ -402,11 +396,8 @@ IfaceMgr::hasOpenSocket(const IOAddress& addr) const { // 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. - for (Iface::AddressCollection::const_iterator addr_it = - iface->getAddresses().begin(); - addr_it != iface->getAddresses().end(); - ++addr_it) { - if (addr == addr_it->get()) { + BOOST_FOREACH(Iface::Address a, iface->getAddresses()) { + if (addr == a.get()) { return (true); } } @@ -493,12 +484,9 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast, } Iface::AddressCollection addrs = iface->getAddresses(); - for (Iface::AddressCollection::iterator addr = addrs.begin(); - addr != addrs.end(); - ++addr) { - + BOOST_FOREACH(Iface::Address addr, iface->getAddresses()) { // Skip non-IPv4 addresses and those that weren't selected.. - if (!addr->get().isV4() || !addr->isSpecified()) { + if (!addr.get().isV4() || !addr.isSpecified()) { continue; } @@ -532,7 +520,7 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast, try { // We haven't open any broadcast sockets yet, so we can // open at least one more. - openSocket(iface->getName(), *addr, port, true, true); + openSocket(iface->getName(), addr.get(), port, true, true); } catch (const Exception& ex) { IFACEMGR_ERROR(SocketConfigError, error_handler, "failed to open socket on interface " @@ -550,7 +538,7 @@ IfaceMgr::openSockets4(const uint16_t port, const bool use_bcast, } else { try { // Not broadcast capable, do not set broadcast flags. - openSocket(iface->getName(), *addr, port, false, false); + openSocket(iface->getName(), addr.get(), port, false, false); } catch (const Exception& ex) { IFACEMGR_ERROR(SocketConfigError, error_handler, "failed to open socket on interface " @@ -599,12 +587,10 @@ IfaceMgr::openSockets6(const uint16_t port, } // Open unicast sockets if there are any unicast addresses defined - Iface::AddressCollection unicasts = iface->getUnicasts(); - for (Iface::AddressCollection::iterator addr = unicasts.begin(); - addr != unicasts.end(); ++addr) { + BOOST_FOREACH(Iface::Address addr, iface->getUnicasts()) { try { - openSocket(iface->getName(), *addr, port); + openSocket(iface->getName(), addr, port); } catch (const Exception& ex) { IFACEMGR_ERROR(SocketConfigError, error_handler, "Failed to open unicast socket on interface " @@ -617,13 +603,10 @@ IfaceMgr::openSockets6(const uint16_t port, } - Iface::AddressCollection addrs = iface->getAddresses(); - for (Iface::AddressCollection::iterator addr = addrs.begin(); - addr != addrs.end(); - ++addr) { + BOOST_FOREACH(Iface::Address addr, iface->getAddresses()) { // Skip all but V6 addresses. - if (!addr->get().isV6()) { + if (!addr.get().isV6()) { continue; } @@ -632,14 +615,14 @@ IfaceMgr::openSockets6(const uint16_t port, // with interface with 2 global addresses, we would bind 3 sockets // (one for link-local and two for global). That would result in // getting each message 3 times. - if (!addr->get().isV6LinkLocal()){ + if (!addr.get().isV6LinkLocal()){ continue; } // Run OS-specific function to open a socket capable of receiving // packets sent to All_DHCP_Relay_Agents_and_Servers multicast // address. - if (openMulticastSocket(*iface, *addr, port, error_handler)) { + if (openMulticastSocket(*iface, addr, port, error_handler)) { ++count; } @@ -665,9 +648,8 @@ IfaceMgr::printIfaces(std::ostream& out /*= std::cout*/) { << ")" << endl; out << " " << addrs.size() << " addr(s):"; - for (Iface::AddressCollection::const_iterator addr = addrs.begin(); - addr != addrs.end(); ++addr) { - out << " " << addr->get().toText(); + BOOST_FOREACH(Iface::Address addr, addrs) { + out << " " << addr.get().toText(); } out << endl; } @@ -770,20 +752,16 @@ int IfaceMgr::openSocketFromAddress(const IOAddress& addr, // Search through detected interfaces and addresses to match // local address we got. BOOST_FOREACH(IfacePtr iface, ifaces_) { - Iface::AddressCollection addrs = iface->getAddresses(); - - for (Iface::AddressCollection::iterator addr_it = addrs.begin(); - addr_it != addrs.end(); - ++addr_it) { + BOOST_FOREACH(Iface::Address a, iface->getAddresses()) { // Local address must match one of the addresses // on detected interfaces. If it does, we have // address and interface detected so we can open // socket. - if (addr_it->get() == addr) { + if (a.get() == addr) { // Open socket using local interface, address and port. // This may cause isc::Unexpected exception. - return (openSocket(iface->getName(), *addr_it, port, false)); + return (openSocket(iface->getName(), a, port, false)); } } } diff --git a/src/lib/dhcp/iface_mgr.h b/src/lib/dhcp/iface_mgr.h index d0dd68b3b9..0b4eb824c8 100644 --- a/src/lib/dhcp/iface_mgr.h +++ b/src/lib/dhcp/iface_mgr.h @@ -165,9 +165,11 @@ public: /// Maximum MAC address length (Infiniband uses 20 bytes) static const unsigned int MAX_MAC_LEN = 20; + /// @brief Address type. + typedef util::OptionalValue Address; + /// Type that defines list of addresses - typedef - std::list > AddressCollection; + typedef std::list
AddressCollection; /// @brief Type that holds a list of socket information. /// diff --git a/src/lib/dhcp/tests/iface_mgr_unittest.cc b/src/lib/dhcp/tests/iface_mgr_unittest.cc index 357cfc823a..014bb321e9 100644 --- a/src/lib/dhcp/tests/iface_mgr_unittest.cc +++ b/src/lib/dhcp/tests/iface_mgr_unittest.cc @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -304,13 +305,11 @@ public: sock != sockets.end(); ++sock) { if (sock->addr_ == IOAddress(addr)) { return (true); + } else if ((sock->addr_ == IOAddress("::")) && (IOAddress(addr).isV6LinkLocal())) { - for (Iface::AddressCollection::const_iterator addr_it = - iface->getAddresses().begin(); - addr_it != iface->getAddresses().end(); - ++addr_it) { - if (addr_it->get() == IOAddress(addr)) { + BOOST_FOREACH(Iface::Address a, iface->getAddresses()) { + if (a.get() == IOAddress(addr)) { return (true); } } @@ -2360,10 +2359,8 @@ checkIfAddrs(const Iface & iface, struct ifaddrs *& ifptr) { IOAddress addrv4 = IOAddress::fromBytes(AF_INET, p); - for (Iface::AddressCollection::const_iterator a = - iface.getAddresses().begin(); - a != iface.getAddresses().end(); ++ a) { - if(a->get().isV4() && (a->get()) == addrv4) { + BOOST_FOREACH(Iface::Address a, iface.getAddresses()) { + if(a.get().isV4() && (a.get()) == addrv4) { return (true); } } @@ -2377,10 +2374,8 @@ checkIfAddrs(const Iface & iface, struct ifaddrs *& ifptr) { IOAddress addrv6 = IOAddress::fromBytes(AF_INET6, p); - for(Iface::AddressCollection::const_iterator a = - iface.getAddresses().begin(); - a != iface.getAddresses().end(); ++ a) { - if (a->get().isV6() && (a->get() == addrv6)) { + BOOST_FOREACH(Iface::Address a, iface.getAddresses()) { + if (a.get().isV6() && (a.get() == addrv6)) { return (true); } } diff --git a/src/lib/dhcpsrv/cfg_iface.cc b/src/lib/dhcpsrv/cfg_iface.cc index 8cc1191af9..e26b25327f 100644 --- a/src/lib/dhcpsrv/cfg_iface.cc +++ b/src/lib/dhcpsrv/cfg_iface.cc @@ -190,11 +190,9 @@ void CfgIface::setIfaceAddrsState(const uint16_t family, const bool active, Iface& iface) const { // Activate/deactivate all addresses. - const Iface::AddressCollection addresses = iface.getAddresses(); - for (Iface::AddressCollection::const_iterator addr_it = - addresses.begin(); addr_it != addresses.end(); ++addr_it) { - if (addr_it->get().getFamily() == family) { - iface.setActive(addr_it->get(), active); + BOOST_FOREACH(Iface::Address addr, iface.getAddresses()) { + if (addr.get().getFamily() == family) { + iface.setActive(addr.get(), active); } } }