SF#3560762: NetworkInterface::map ignores iponly flag

This commit is contained in:
Aleksandar Fabijanic 2012-08-26 04:47:44 +00:00
Родитель d384a499d5
Коммит 8a84ef0e86
3 изменённых файлов: 64 добавлений и 29 удалений

Просмотреть файл

@ -1161,8 +1161,8 @@ void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
}
} // namespace
NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
{
FastMutex::ScopedLock lock(_mutex);
@ -1176,7 +1176,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
if (getifaddrs(&ifaces) < 0)
throw NetException("cannot get network adapter list");
try
{
for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next)
@ -1192,7 +1192,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
{
struct sockaddr_dl* sdl = (struct sockaddr_dl*) currIface->ifa_addr;
ifIndex = sdl->sdl_index;
if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly))
if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly) && !ipOnly)
{
intf = NetworkInterface(ifIndex);
setInterfaceParams(currIface, intf.impl());
@ -1306,6 +1306,17 @@ static NetworkInterface::Type fromNative(unsigned arphrd)
}
}
void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
{
struct sockaddr_dl* sdl = (struct sockaddr_dl*) iface->ifa_addr;
impl.setName(iface->ifa_name);
impl.setDisplayName(iface->ifa_name);
impl.setPhyParams();
impl.setMACAddress(sdl->sll_addr, sdl->sll_halen);
impl.setType(fromNative(sdl->sll_hatype));
}
}
@ -1337,21 +1348,24 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
{
struct sockaddr_ll* sll = (struct sockaddr_ll*)iface->ifa_addr;
ifIndex = sll->sll_ifindex;
intf = NetworkInterface(ifIndex);
intf.impl().setName(iface->ifa_name);
intf.impl().setDisplayName(iface->ifa_name);
intf.impl().setPhyParams();
intf.impl().setMACAddress(sll->sll_addr, sll->sll_halen);
intf.impl().setType(fromNative(sll->sll_hatype));
if ((upOnly && intf.isUp()) || !upOnly)
if ((result.find(ifIndex) == result.end()) && ((upOnly && intf.isUp()) || !upOnly) && !ipOnly)
{
intf = NetworkInterface(ifIndex);
setInterfaceParams(iface, intf.impl());
ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
}
break;
}
case AF_INET:
{
ifIndex = if_nametoindex(iface->ifa_name);
if ((ifIt == result.end()) && ((upOnly && intf.isUp()) || !upOnly))
{
intf = NetworkInterface(ifIndex);
setInterfaceParams(iface, intf.impl());
ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
}
address = IPAddress(*(iface->ifa_addr));
subnetMask = IPAddress(*(iface->ifa_netmask));
@ -1360,13 +1374,18 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
else if (iface->ifa_flags & IFF_POINTOPOINT && iface->ifa_dstaddr)
broadcastAddress = IPAddress(*(iface->ifa_dstaddr));
else
broadcastAddress = IPAddress();
broadcastAddress = IPAddress();
break;
}
#if defined(POCO_HAVE_IPv6)
case AF_INET6:
ifIndex = if_nametoindex(iface->ifa_name);
if ((ifIt == result.end()) && ((upOnly && intf.isUp()) || !upOnly))
{
intf = NetworkInterface(ifIndex);
setInterfaceParams(iface, intf.impl());
ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
}
address = IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(iface->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex);
subnetMask = IPAddress(*(iface->ifa_netmask));
broadcastAddress = IPAddress();
@ -1375,16 +1394,6 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
default:
continue;
}
if (family == AF_INET || family == AF_INET6)
{
intf = NetworkInterface(std::string(iface->ifa_name), address, subnetMask, broadcastAddress, ifIndex);
if ((upOnly && intf.isUp()) || !upOnly)
{
if ((ifIt = result.find(ifIndex)) != result.end())
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
}
}
} // for interface
}
catch (...)

Просмотреть файл

@ -69,7 +69,7 @@ void NetworkInterfaceTest::testMap()
NetworkInterface::MACAddress mac(it->second.macAddress());
if (!mac.empty() && (it->second.type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK))
std::cout << "MAC Address: " << mac << std::endl;
std::cout << "MAC Address: (" << it->second.type() << ") " << mac << std::endl;
typedef NetworkInterface::AddressList List;
const List& ipList = it->second.addressList();
@ -107,7 +107,7 @@ void NetworkInterfaceTest::testList()
NetworkInterface::MACAddress mac(it->macAddress());
if (!mac.empty() && (it->type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK))
std::cout << "MAC Address: " << mac << std::endl;
std::cout << "MAC Address: (" << it->type() << ") " << mac << std::endl;
typedef NetworkInterface::AddressList List;
const List& ipList = it->addressList();
@ -117,7 +117,7 @@ void NetworkInterfaceTest::testList()
{
std::cout << "IP Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString() << std::endl;
IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>();
if (!addr.isWildcard()) std::cout << "Subnet: " << ipIt->get<NetworkInterface::SUBNET_MASK>().toString() << std::endl;
if (!addr.isWildcard()) std::cout << "Subnet: " << ipIt->get<NetworkInterface::SUBNET_MASK>().toString() << " (/" << ipIt->get<NetworkInterface::SUBNET_MASK>().prefixLength() << ")" << std::endl;
addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
if (!addr.isWildcard()) std::cout << "Broadcast: " << ipIt->get<NetworkInterface::BROADCAST_ADDRESS>().toString() << std::endl;
}
@ -175,6 +175,28 @@ void NetworkInterfaceTest::testForIndex()
}
void NetworkInterfaceTest::testMapIpOnly()
{
NetworkInterface::Map m = NetworkInterface::map(true, false);
assert (!m.empty());
for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it)
{
assert(it->second.supportsIPv4() || it->second.supportsIPv6());
}
}
void NetworkInterfaceTest::testMapUpOnly()
{
NetworkInterface::Map m = NetworkInterface::map(false, true);
assert (!m.empty());
for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it)
{
assert(it->second.isUp());
}
}
void NetworkInterfaceTest::setUp()
{
}
@ -194,6 +216,8 @@ CppUnit::Test* NetworkInterfaceTest::suite()
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForName);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForAddress);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForIndex);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testMapIpOnly);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testMapUpOnly);
return pSuite;
}

Просмотреть файл

@ -51,6 +51,8 @@ public:
void testForName();
void testForAddress();
void testForIndex();
void testMapIpOnly();
void testMapUpOnly();
void setUp();
void tearDown();