Poco::Net::NetworkInterface::list does not list inactive interfaces even when explicitly being asked for it #2044

This commit is contained in:
Alex Fabijanic 2017-12-19 08:47:31 +01:00
parent 56a483bcf9
commit 2b125e1c4e
3 changed files with 56 additions and 42 deletions

View File

@ -89,7 +89,7 @@ public:
NI_TYPE_IEEE1394, NI_TYPE_IEEE1394,
NI_TYPE_OTHER NI_TYPE_OTHER
}; };
enum IPVersion enum IPVersion
{ {
IPv4_ONLY, /// Return interfaces with IPv4 address only IPv4_ONLY, /// Return interfaces with IPv4 address only
@ -173,10 +173,10 @@ public:
const AddressList& addressList() const; const AddressList& addressList() const;
/// Returns the list of IP addresses bound to the interface. /// Returns the list of IP addresses bound to the interface.
const IPAddress& subnetMask(unsigned index = 0) const; const IPAddress& subnetMask(unsigned index = 0) const;
/// Returns the subnet mask for this network interface. /// Returns the subnet mask for this network interface.
const IPAddress& broadcastAddress(unsigned index = 0) const; const IPAddress& broadcastAddress(unsigned index = 0) const;
/// Returns the broadcast address for this network interface. /// Returns the broadcast address for this network interface.
@ -249,7 +249,7 @@ public:
/// ///
/// Throws an InterfaceNotFoundException if an interface /// Throws an InterfaceNotFoundException if an interface
/// with the given index does not exist. /// with the given index does not exist.
static List list(bool ipOnly = true, bool upOnly = true); static List list(bool ipOnly = true, bool upOnly = true);
/// Returns a list with all network interfaces /// Returns a list with all network interfaces
/// on the system. /// on the system.
@ -264,7 +264,7 @@ public:
/// If there are multiple addresses bound to one interface, /// If there are multiple addresses bound to one interface,
/// multiple NetworkInterface entries are listed for /// multiple NetworkInterface entries are listed for
/// the same interface. /// the same interface.
static Map map(bool ipOnly = true, bool upOnly = true); static Map map(bool ipOnly = true, bool upOnly = true);
/// Returns a map containing system network interfaces /// Returns a map containing system network interfaces
/// Map is keyed by interface system indices. /// Map is keyed by interface system indices.
@ -307,10 +307,10 @@ protected:
unsigned index, unsigned index,
MACAddress* pMACAddress = 0); MACAddress* pMACAddress = 0);
/// Creates the NetworkInterface. /// Creates the NetworkInterface.
IPAddress interfaceNameToAddress(const std::string& interfaceName) const; IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
/// Determines the IPAddress bound to the interface with the given name. /// Determines the IPAddress bound to the interface with the given name.
unsigned interfaceNameToIndex(const std::string& interfaceName) const; unsigned interfaceNameToIndex(const std::string& interfaceName) const;
/// Determines the interface index of the interface with the given name. /// Determines the interface index of the interface with the given name.
@ -318,7 +318,7 @@ protected:
private: private:
NetworkInterfaceImpl* _pImpl; NetworkInterfaceImpl* _pImpl;
static Poco::FastMutex _mutex; static Poco::FastMutex _mutex;
}; };

View File

@ -875,33 +875,40 @@ NetworkInterface::List NetworkInterface::list(bool ipOnly, bool upOnly)
typedef NetworkInterface::AddressList List; typedef NetworkInterface::AddressList List;
const List& ipList = it->second.addressList(); const List& ipList = it->second.addressList();
List::const_iterator ipIt = ipList.begin(); if (ipList.size() > 0)
List::const_iterator ipEnd = ipList.end();
for (; ipIt != ipEnd; ++ipIt)
{ {
IPAddress addr = ipIt->get<NetworkInterface::IP_ADDRESS>(); List::const_iterator ipIt = ipList.begin();
IPAddress mask = ipIt->get<NetworkInterface::SUBNET_MASK>(); List::const_iterator ipEnd = ipList.end();
NetworkInterface ni; for(; ipIt != ipEnd; ++ipIt)
if (mask.isWildcard())
{ {
ni = NetworkInterface(name, displayName, adapterName, addr, index, &mac); IPAddress addr = ipIt->get<NetworkInterface::IP_ADDRESS>();
} IPAddress mask = ipIt->get<NetworkInterface::SUBNET_MASK>();
else NetworkInterface ni;
{ if(mask.isWildcard())
IPAddress broadcast = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>(); {
ni = NetworkInterface(name, displayName, adapterName, addr, mask, broadcast, index, &mac); ni = NetworkInterface(name, displayName, adapterName, addr, index, &mac);
} }
else
{
IPAddress broadcast = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
ni = NetworkInterface(name, displayName, adapterName, addr, mask, broadcast, index, &mac);
}
ni._pImpl->_broadcast = it->second._pImpl->_broadcast; ni._pImpl->_broadcast = it->second._pImpl->_broadcast;
ni._pImpl->_loopback = it->second._pImpl->_loopback; ni._pImpl->_loopback = it->second._pImpl->_loopback;
ni._pImpl->_multicast = it->second._pImpl->_multicast; ni._pImpl->_multicast = it->second._pImpl->_multicast;
ni._pImpl->_pointToPoint = it->second._pImpl->_pointToPoint; ni._pImpl->_pointToPoint = it->second._pImpl->_pointToPoint;
ni._pImpl->_up = it->second._pImpl->_up; ni._pImpl->_up = it->second._pImpl->_up;
ni._pImpl->_running = it->second._pImpl->_running; ni._pImpl->_running = it->second._pImpl->_running;
ni._pImpl->_mtu = it->second._pImpl->_mtu; ni._pImpl->_mtu = it->second._pImpl->_mtu;
ni._pImpl->_type = it->second._pImpl->_type; ni._pImpl->_type = it->second._pImpl->_type;
list.push_back(ni); list.push_back(ni);
}
}
else
{
list.push_back(NetworkInterface(name, displayName, adapterName, index, &mac));
} }
} }

View File

@ -79,7 +79,7 @@ void NetworkInterfaceTest::testList()
{ {
NetworkInterface::List list = NetworkInterface::list(false, false); NetworkInterface::List list = NetworkInterface::list(false, false);
assert (!list.empty()); assert (!list.empty());
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it) for (NetworkInterface::List::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
std::cout << std::endl << "==============" << std::endl; std::cout << std::endl << "==============" << std::endl;
@ -92,10 +92,10 @@ void NetworkInterfaceTest::testList()
if (!mac.empty() && (it->type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK)) if (!mac.empty() && (it->type() != NetworkInterface::NI_TYPE_SOFTWARE_LOOPBACK))
std::cout << "MAC Address: (" << it->type() << ") " << mac << std::endl; std::cout << "MAC Address: (" << it->type() << ") " << mac << std::endl;
typedef NetworkInterface::AddressList List; typedef NetworkInterface::AddressList AddrList;
const List& ipList = it->addressList(); const AddrList& ipList = it->addressList();
List::const_iterator ipIt = ipList.begin(); AddrList::const_iterator ipIt = ipList.begin();
List::const_iterator ipEnd = ipList.end(); AddrList::const_iterator ipEnd = ipList.end();
for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter) for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter)
{ {
std::cout << "IP Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>() << std::endl; std::cout << "IP Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>() << std::endl;
@ -213,13 +213,20 @@ void NetworkInterfaceTest::testListMapConformance()
typedef NetworkInterface::AddressList List; typedef NetworkInterface::AddressList List;
const List& ipList = mapIt->second.addressList(); const List& ipList = mapIt->second.addressList();
List::const_iterator ipIt = ipList.begin(); if (ipList.size() > 0)
List::const_iterator ipEnd = ipList.end();
for (; ipIt != ipEnd; ++ipIt, ++counter, ++listIt)
{ {
NetworkInterface::MACAddress lmac = listIt->macAddress(); List::const_iterator ipIt = ipList.begin();
assert (lmac == mac); List::const_iterator ipEnd = ipList.end();
if (listIt == l.end()) fail ("wrong number of list items"); for(; ipIt != ipEnd; ++ipIt, ++counter, ++listIt)
{
NetworkInterface::MACAddress lmac = listIt->macAddress();
assert (lmac == mac);
if(listIt == l.end()) fail("wrong number of list items");
}
}
else
{
++counter;
} }
} }