Permalink
Browse files

NetworkInterface::firstAddress() should not throw on unconfigured int…

…erfaces.
  • Loading branch information...
1 parent 0472e95 commit 046bbdcc4151ef2e44b919eb65cf6509ac3ae91f @pprindeville committed Nov 15, 2012
@@ -151,7 +151,8 @@ class Net_API NetworkInterface
/// On other platforms this is the same as name().
const IPAddress& firstAddress(IPAddress::Family family) const;
- /// Returns the first IP address bound to the interface.
+ /// Returns the first IP address bound to the interface,
+ /// or wildcard if there is none.
const IPAddress& address(unsigned index = 0) const;
/// Returns the IP address bound to the interface at index position.
@@ -240,30 +240,21 @@ NetworkInterfaceImpl::~NetworkInterfaceImpl()
bool NetworkInterfaceImpl::supportsIPv4() const
{
- AddressList::const_iterator it = _addressList.begin();
- AddressList::const_iterator end = _addressList.end();
- for (; it != end; ++it)
- {
- if (IPAddress::IPv4 == it->get<NetworkInterface::IP_ADDRESS>().family())
- return true;
- }
+ IPAddress addr = firstAddress(IPAddress::IPv4);
- return false;
+ return !addr.isWildcard();
}
bool NetworkInterfaceImpl::supportsIPv6() const
{
#ifdef POCO_HAVE_IPv6
- AddressList::const_iterator it = _addressList.begin();
- AddressList::const_iterator end = _addressList.end();
- for (; it != end; ++it)
- {
- if (IPAddress::IPv6 == it->get<NetworkInterface::IP_ADDRESS>().family())
- return true;
- }
-#endif
+ IPAddress addr = firstAddress(IPAddress::IPv6);
+
+ return !addr.isWildcard();
+#else
return false;
+#endif
}
@@ -295,7 +286,10 @@ const IPAddress& NetworkInterfaceImpl::firstAddress(IPAddress::Family family) co
if (addr.family() == family) return addr;
}
- throw NotFoundException(format("%s family address not found.", (family == IPAddress::IPv4) ? std::string("IPv4") : std::string("IPv6")));
+ // because testing isWildcard() is a lot faster than catching an
+ // exception... and having an unconfigured interface is hardly
+ // an 'exceptional' state.
+ return IPAddress(family);
}
@@ -110,8 +110,9 @@ Poco::Net::NetworkInterface MulticastEchoServer::findInterface()
NetworkInterface::Map m = NetworkInterface::map();
for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it)
{
- if (it->second.supportsIPv4() &&
- it->second.firstAddress(IPAddress::IPv4).isUnicast() &&
+ IPAddress addr = it->second.firstAddress(IPAddress::IPv4);
+ if (!addr.isWildcard() &&
+ addr.isUnicast() &&
!it->second.isLoopback() &&
!it->second.isPointToPoint())
{
@@ -146,19 +146,15 @@ void NetworkInterfaceTest::testForAddress()
// not all interfaces have IP configured
if (it->second.addressList().empty()) continue;
+ IPAddress first = it->second.firstAddress(IPAddress::IPv4);
if (it->second.supportsIPv4())
{
- NetworkInterface ifc = NetworkInterface::forAddress(it->second.firstAddress(IPAddress::IPv4));
- assert (ifc.firstAddress(IPAddress::IPv4) == it->second.firstAddress(IPAddress::IPv4));
+ NetworkInterface ifc = NetworkInterface::forAddress(first);
+ assert (ifc.firstAddress(IPAddress::IPv4) == first);
}
else
{
- try
- {
- it->second.firstAddress(IPAddress::IPv4);
- fail ("must throw");
- }
- catch (NotFoundException&) { }
+ assert(first.isWildcard());
}
}
}

0 comments on commit 046bbdc

Please sign in to comment.