- NetworkInterface Windows improvements (detect broadcast address rather than calculate it)

- site-local IPv6 prefix (RFC4291)
NOTE: other platforms compilation still broken (WIP)
This commit is contained in:
Aleksandar Fabijanic
2012-08-11 16:45:48 +00:00
parent 2371cae637
commit b5d704bfeb
6 changed files with 188 additions and 126 deletions

View File

@@ -236,8 +236,10 @@ public:
/// IPv4 site local addresses are in on of the 10.0.0.0/24, /// IPv4 site local addresses are in on of the 10.0.0.0/24,
/// 192.168.0.0/16 or 172.16.0.0 to 172.31.255.255 ranges. /// 192.168.0.0/16 or 172.16.0.0 to 172.31.255.255 ranges.
/// ///
/// IPv6 site local addresses have 1111 1110 11 as the first /// Originally, IPv6 site-local addresses had FEC0/10 (1111 1110 11)
/// 10 bits, followed by 38 zeros. /// prefix (RFC 4291), followed by 38 zeros. Interfaces using
/// this mask are supported, but obsolete; RFC 4193 prescribes
/// fc00::/7 (1111 110) as local unicast prefix.
bool isIPv4Compatible() const; bool isIPv4Compatible() const;
/// Returns true iff the address is IPv4 compatible. /// Returns true iff the address is IPv4 compatible.

View File

@@ -88,12 +88,12 @@ public:
IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address
}; };
NetworkInterface(std::size_t index = 0); NetworkInterface(unsigned index = ~0);
/// Creates a NetworkInterface representing the /// Creates a NetworkInterface representing the
/// default interface. /// default interface.
/// ///
/// The name is empty, the IP address is the wildcard /// The name is empty, the IP address is the wildcard
/// address and the index is zero. /// address and the index is max value of unsigned integer.
NetworkInterface(const NetworkInterface& interfc); NetworkInterface(const NetworkInterface& interfc);
/// Creates the NetworkInterface by copying another one. /// Creates the NetworkInterface by copying another one.
@@ -113,8 +113,8 @@ public:
void swap(NetworkInterface& other); void swap(NetworkInterface& other);
/// Swaps the NetworkInterface with another one. /// Swaps the NetworkInterface with another one.
std::size_t index() const; unsigned index() const;
/// Returns the interface index. /// Returns the interface OS index.
const std::string& name() const; const std::string& name() const;
/// Returns the interface name. /// Returns the interface name.
@@ -149,11 +149,11 @@ public:
const IPAddress& destAddress(std::size_t index = 0) const; const IPAddress& destAddress(std::size_t index = 0) const;
/// Returns the IPv4 point-to-point destiation address for this network interface. /// Returns the IPv4 point-to-point destiation address for this network interface.
int mtu() const; unsigned mtu() const;
/// Returns the MTU for this interface. /// Returns the MTU for this interface.
int ifindex() const; bool supportsIP() const;
/// Returns the OS's index for this interface. /// Returns true if the interface supports IP.
bool supportsIPv4() const; bool supportsIPv4() const;
/// Returns true if the interface supports IPv4. /// Returns true if the interface supports IPv4.

View File

@@ -549,7 +549,7 @@ public:
bool isSiteLocal() const bool isSiteLocal() const
{ {
const UInt16* words = reinterpret_cast<const UInt16*>(&_addr); const UInt16* words = reinterpret_cast<const UInt16*>(&_addr);
return (ntohs(words[0]) & 0xFFE0) == 0xFEC0; return ((ntohs(words[0]) & 0xFFE0) == 0xFEC0) || ((ntohs(words[0]) & 0xFF00) == 0xFC00);
} }
bool isIPv4Compatible() const bool isIPv4Compatible() const

View File

@@ -70,11 +70,11 @@ public:
typedef NetworkInterface::AddressTuple AddressTuple; typedef NetworkInterface::AddressTuple AddressTuple;
typedef NetworkInterface::AddressList AddressList; typedef NetworkInterface::AddressList AddressList;
NetworkInterfaceImpl(int index = -1); NetworkInterfaceImpl(unsigned index = ~0);
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index = -1); NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index = -1);
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1); NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
int index() const; unsigned index() const;
const std::string& name() const; const std::string& name() const;
const std::string& displayName() const; const std::string& displayName() const;
void addAddress(const AddressTuple& address); void addAddress(const AddressTuple& address);
@@ -91,8 +91,7 @@ public:
void setDisplayName(const std::string& name); void setDisplayName(const std::string& name);
void addAddress(const IPAddress& addr); void addAddress(const IPAddress& addr);
int mtu() const; unsigned mtu() const;
int ifindex() const;
bool broadcast() const; bool broadcast() const;
bool loopback() const; bool loopback() const;
bool multicast() const; bool multicast() const;
@@ -120,14 +119,14 @@ private:
std::string _name; std::string _name;
std::string _displayName; std::string _displayName;
AddressList _addressList; AddressList _addressList;
std::size_t _index; unsigned _index;
bool _broadcast; bool _broadcast;
bool _loopback; bool _loopback;
bool _multicast; bool _multicast;
bool _pointToPoint; bool _pointToPoint;
bool _up; bool _up;
bool _running; bool _running;
int _mtu; unsigned _mtu;
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
friend NetworkInterface::Map NetworkInterface::map(); friend NetworkInterface::Map NetworkInterface::map();
@@ -136,9 +135,9 @@ private:
}; };
NetworkInterfaceImpl::NetworkInterfaceImpl(int index): NetworkInterfaceImpl::NetworkInterfaceImpl(unsigned index):
_index(index), _index(index),
_mtu(-1) _mtu(~0)
{ {
_addressList.resize(1); _addressList.resize(1);
} }
@@ -293,7 +292,7 @@ bool NetworkInterfaceImpl::supportsIPv6() const
} }
inline int NetworkInterfaceImpl::index() const inline unsigned NetworkInterfaceImpl::index() const
{ {
return _index; return _index;
} }
@@ -311,7 +310,7 @@ inline const std::string& NetworkInterfaceImpl::displayName() const
} }
void NetworkInterfaceImpl::addAddress(const AddressTuple& address) inline void NetworkInterfaceImpl::addAddress(const AddressTuple& address)
{ {
_addressList.push_back(address); _addressList.push_back(address);
} }
@@ -343,7 +342,7 @@ inline const NetworkInterface::AddressList& NetworkInterfaceImpl::addressList()
} }
inline const IPAddress& NetworkInterfaceImpl::subnetMask(std::size_t index) const const IPAddress& NetworkInterfaceImpl::subnetMask(std::size_t index) const
{ {
if (index < _addressList.size()) if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::SUBNET_MASK>(); return _addressList[index].get<NetworkInterface::SUBNET_MASK>();
@@ -352,7 +351,7 @@ inline const IPAddress& NetworkInterfaceImpl::subnetMask(std::size_t index) cons
} }
inline const IPAddress& NetworkInterfaceImpl::broadcastAddress(std::size_t index) const const IPAddress& NetworkInterfaceImpl::broadcastAddress(std::size_t index) const
{ {
if (index < _addressList.size()) if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>(); return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>();
@@ -361,7 +360,7 @@ inline const IPAddress& NetworkInterfaceImpl::broadcastAddress(std::size_t index
} }
inline const IPAddress& NetworkInterfaceImpl::destAddress(std::size_t index) const const IPAddress& NetworkInterfaceImpl::destAddress(std::size_t index) const
{ {
if (!pointToPoint()) if (!pointToPoint())
throw InvalidAccessException("Only PPP addresses have destination address."); throw InvalidAccessException("Only PPP addresses have destination address.");
@@ -372,7 +371,7 @@ inline const IPAddress& NetworkInterfaceImpl::destAddress(std::size_t index) con
} }
inline int NetworkInterfaceImpl::mtu() const inline unsigned NetworkInterfaceImpl::mtu() const
{ {
return _mtu; return _mtu;
} }
@@ -558,7 +557,7 @@ void NetworkInterface::swap(NetworkInterface& other)
} }
std::size_t NetworkInterface::index() const unsigned NetworkInterface::index() const
{ {
return _pImpl->index(); return _pImpl->index();
} }
@@ -618,12 +617,18 @@ const IPAddress& NetworkInterface::destAddress(std::size_t index) const
} }
int NetworkInterface::mtu() const unsigned NetworkInterface::mtu() const
{ {
return _pImpl->mtu(); return _pImpl->mtu();
} }
bool NetworkInterface::supportsIP() const
{
return _pImpl->supportsIPv4() || _pImpl->supportsIPv6();
}
bool NetworkInterface::supportsIPv4() const bool NetworkInterface::supportsIPv4() const
{ {
return _pImpl->supportsIPv4(); return _pImpl->supportsIPv4();
@@ -641,26 +646,31 @@ bool NetworkInterface::supportsBroadcast() const
return _pImpl->broadcast(); return _pImpl->broadcast();
} }
bool NetworkInterface::supportsMulticast() const bool NetworkInterface::supportsMulticast() const
{ {
return _pImpl->multicast(); return _pImpl->multicast();
} }
bool NetworkInterface::isLoopback() const bool NetworkInterface::isLoopback() const
{ {
return _pImpl->loopback(); return _pImpl->loopback();
} }
bool NetworkInterface::isPointToPoint() const bool NetworkInterface::isPointToPoint() const
{ {
return _pImpl->pointToPoint(); return _pImpl->pointToPoint();
} }
bool NetworkInterface::isRunning() const bool NetworkInterface::isRunning() const
{ {
return _pImpl->running(); return _pImpl->running();
} }
bool NetworkInterface::isUp() const bool NetworkInterface::isUp() const
{ {
return _pImpl->up(); return _pImpl->up();
@@ -783,6 +793,60 @@ NetworkInterface::List NetworkInterface::list()
namespace Poco { namespace Poco {
namespace Net { namespace Net {
namespace {
IPAddress getBroadcastAddress(PIP_ADAPTER_PREFIX pPrefix, const IPAddress& addr)
/// This function relies on (1) subnet prefix being at the position
/// immediately preceding and (2) broadcast address being at the position
/// immediately succeeding the IPv4 unicast address.
///
/// Since there is no explicit guarantee on order, to ensure correctness,
/// the above constraints are checked prior to returning the result.
{
PIP_ADAPTER_PREFIX pPrev = 0;
for (int i = 0; pPrefix; pPrefix = pPrefix->Next, ++i)
{
ADDRESS_FAMILY family = pPrefix->Address.lpSockaddr->sa_family;
if ((family == AF_INET) && (addr == IPAddress(pPrefix->Address)))
break;
pPrev = pPrefix;
}
if (pPrefix && pPrefix->Next && pPrev)
{
IPAddress prefix(pPrev->PrefixLength, IPAddress::IPv4);
IPAddress mask(pPrefix->Next->Address);
if ((prefix & mask) == (prefix & addr))
return IPAddress(pPrefix->Next->Address);
}
return IPAddress(IPAddress::IPv4);
}
std::string getErrorMessage(DWORD errorCode)
{
std::string errMsg;
DWORD dwFlg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
LPWSTR lpMsgBuf = 0;
if (FormatMessageW(dwFlg, 0, errorCode, 0, (LPWSTR) & lpMsgBuf, 0, NULL))
UnicodeConverter::toUTF8(lpMsgBuf, errMsg);
#else
LPTSTR lpMsgBuf = 0;
if (FormatMessageA(dwFlg, 0, errorCode, 0, (LPTSTR) & lpMsgBuf, 0, NULL))
errMsg = lpMsgBuf;
#endif
LocalFree(lpMsgBuf);
return errMsg;
}
} /// namespace
NetworkInterface::Map NetworkInterface::map() NetworkInterface::Map NetworkInterface::map()
{ {
FastMutex::ScopedLock lock(_mutex); FastMutex::ScopedLock lock(_mutex);
@@ -797,36 +861,37 @@ NetworkInterface::Map NetworkInterface::map()
#endif #endif
DWORD dwRetVal = 0; DWORD dwRetVal = 0;
ULONG iterations = 0; ULONG iterations = 0;
PIP_ADAPTER_ADDRESSES pAddresses = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(memory.begin()); PIP_ADAPTER_ADDRESSES pAddress = reinterpret_cast<IP_ADAPTER_ADDRESSES*>(memory.begin());
do do
{ {
if ((dwRetVal = GetAdaptersAddresses(family, flags, NULL, pAddresses, &outBufLen)) == ERROR_BUFFER_OVERFLOW) if (ERROR_BUFFER_OVERFLOW == (dwRetVal = GetAdaptersAddresses(family, flags, 0, pAddress, &outBufLen)))
memory.resize(outBufLen); memory.resize(outBufLen); // adjust size and try again
else break; else if (ERROR_NO_DATA == dwRetVal) // no network interfaces found
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (++iterations <= 2)); return result;
else if (NO_ERROR != dwRetVal) // error occurred
throw SystemException(format("An error occurred while trying to obtain list of network interfaces: [%s]", getErrorMessage(dwRetVal)));
else
break; // all good
} while ((ERROR_BUFFER_OVERFLOW == dwRetVal) && (++iterations <= 2));
if (dwRetVal == ERROR_NO_DATA) return result;// no network interfaces found poco_assert (NO_ERROR == dwRetVal);
for (; pAddress; pAddress = pAddress->Next)
if (dwRetVal == NO_ERROR)
{
for (PIP_ADAPTER_ADDRESSES pAddress = pAddresses; pAddress; pAddress = pAddress->Next)
{ {
IPAddress address; IPAddress address;
IPAddress subnetMask; IPAddress subnetMask;
IPAddress broadcastAddress; IPAddress broadcastAddress;
IPAddress destAddress;
unsigned ifIndex = ~0; unsigned ifIndex = ~0;
#if defined(POCO_HAVE_IPv6) #if defined(POCO_HAVE_IPv6)
poco_assert (pAddress->Ipv6IfIndex == pAddress->IfIndex);
if (pAddress->Flags & IP_ADAPTER_IPV6_ENABLED) ifIndex = pAddress->Ipv6IfIndex; if (pAddress->Flags & IP_ADAPTER_IPV6_ENABLED) ifIndex = pAddress->Ipv6IfIndex;
else else
#endif #endif
if (pAddress->Flags & IP_ADAPTER_IPV4_ENABLED) ifIndex = pAddress->IfIndex; if (pAddress->Flags & IP_ADAPTER_IPV4_ENABLED) ifIndex = pAddress->IfIndex;
PIP_ADAPTER_PREFIX pPrefix = pAddress->FirstPrefix;
for (PIP_ADAPTER_UNICAST_ADDRESS pUniAddr = pAddress->FirstUnicastAddress; for (PIP_ADAPTER_UNICAST_ADDRESS pUniAddr = pAddress->FirstUnicastAddress;
pUniAddr; pUniAddr;
pUniAddr = pUniAddr->Next, pPrefix = pPrefix ? pPrefix->Next : 0) pUniAddr = pUniAddr->Next)
{ {
std::string name(pAddress->AdapterName); std::string name(pAddress->AdapterName);
std::string displayName; std::string displayName;
@@ -837,6 +902,7 @@ NetworkInterface::Map NetworkInterface::map()
int rc = WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR, pAddress->FriendlyName, -1, displayNameBuffer, sizeof(displayNameBuffer), NULL, NULL); int rc = WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR, pAddress->FriendlyName, -1, displayNameBuffer, sizeof(displayNameBuffer), NULL, NULL);
if (rc) displayName = displayNameBuffer; if (rc) displayName = displayNameBuffer;
#endif #endif
UINT8 prefixLength = pUniAddr->OnLinkPrefixLength;
address = IPAddress(pUniAddr->Address); address = IPAddress(pUniAddr->Address);
ADDRESS_FAMILY family = pUniAddr->Address.lpSockaddr->sa_family; ADDRESS_FAMILY family = pUniAddr->Address.lpSockaddr->sa_family;
Map::iterator ifIt = result.find(ifIndex); Map::iterator ifIt = result.find(ifIndex);
@@ -844,17 +910,20 @@ NetworkInterface::Map NetworkInterface::map()
{ {
case AF_INET: case AF_INET:
{ {
bool hasBroadcast = (pAddress->IfType == IF_TYPE_ETHERNET_CSMACD); // Windows lists broadcast address on localhost
subnetMask = pPrefix ? IPAddress(pPrefix->Length, IPAddress::IPv4) : IPAddress(); bool hasBroadcast = (pAddress->IfType == IF_TYPE_ETHERNET_CSMACD) || (pAddress->IfType == IF_TYPE_SOFTWARE_LOOPBACK);
broadcastAddress = address | ~subnetMask; // On Windows, a valid broadcast address will be all 1's (== address | ~subnetMask); we go an extra mile here in order to
if (ifIt == result.end()) // make sure we reflect the actual value held by system and protect against misconfiguration (e.g. bad DHCP config entry)
broadcastAddress = getBroadcastAddress(pAddress->FirstPrefix, address);
subnetMask = prefixLength ? IPAddress(prefixLength, IPAddress::IPv4) : IPAddress();
if (ifIt == result.end()) // network interface not cached yet, insert
{ {
if (hasBroadcast) if (hasBroadcast)
ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, subnetMask, broadcastAddress, ifIndex))).first; ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, subnetMask, broadcastAddress, ifIndex))).first;
else else
ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, ifIndex))).first; ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, ifIndex))).first;
} }
else else // network interface is cached, add address to it
{ {
if (hasBroadcast) if (hasBroadcast)
ifIt->second.addAddress(address, subnetMask, broadcastAddress); ifIt->second.addAddress(address, subnetMask, broadcastAddress);
@@ -876,30 +945,6 @@ NetworkInterface::Map NetworkInterface::map()
ifIt->second.impl().setUp(pAddress->OperStatus == IfOperStatusUp); ifIt->second.impl().setUp(pAddress->OperStatus == IfOperStatusUp);
} // for addresses } // for addresses
} // for adapters } // for adapters
} // if no error
else // error occurred
{
std::string errMsg;
DWORD dwFlg = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
#if defined(POCO_WIN32_UTF8) && !defined(POCO_NO_WSTRING)
LPWSTR lpMsgBuf = 0;
if (FormatMessageW(dwFlg, 0, dwRetVal, 0, (LPWSTR) & lpMsgBuf, 0, NULL))
{
UnicodeConverter::toUTF8(lpMsgBuf, errMsg);
LocalFree(lpMsgBuf);
}
#else
LPTSTR lpMsgBuf = 0;
if (FormatMessageA(dwFlg, 0, dwRetVal, 0, (LPTSTR) & lpMsgBuf, 0, NULL))
{
errMsg = lpMsgBuf;
LocalFree(lpMsgBuf);
}
#endif
throw SystemException(format("An error occurred whiel trying to obtain list of network interfaces: [%s]", errMsg));
}
return result; return result;
} }

View File

@@ -405,7 +405,7 @@ void IPAddressTest::testClassification6()
assert (!ip10.isOrgLocalMC()); assert (!ip10.isOrgLocalMC());
assert (!ip10.isGlobalMC()); assert (!ip10.isGlobalMC());
IPAddress ip6("fec0::21f:5bff:fec6:6707"); // site local unicast IPAddress ip6("fec0::21f:5bff:fec6:6707"); // site local unicast (RFC 4291)
assert (!ip6.isWildcard()); assert (!ip6.isWildcard());
assert (!ip6.isBroadcast()); assert (!ip6.isBroadcast());
assert (!ip6.isLoopback()); assert (!ip6.isLoopback());
@@ -419,6 +419,21 @@ void IPAddressTest::testClassification6()
assert (!ip6.isSiteLocalMC()); assert (!ip6.isSiteLocalMC());
assert (!ip6.isOrgLocalMC()); assert (!ip6.isOrgLocalMC());
assert (!ip6.isGlobalMC()); assert (!ip6.isGlobalMC());
IPAddress ip7("fc00::21f:5bff:fec6:6707"); // site local unicast (RFC 4193)
assert (!ip7.isWildcard());
assert (!ip7.isBroadcast());
assert (!ip7.isLoopback());
assert (!ip7.isMulticast());
assert (ip7.isUnicast());
assert (!ip7.isLinkLocal());
assert (ip7.isSiteLocal());
assert (!ip7.isWellKnownMC());
assert (!ip7.isNodeLocalMC());
assert (!ip7.isLinkLocalMC());
assert (!ip7.isSiteLocalMC());
assert (!ip7.isOrgLocalMC());
assert (!ip7.isGlobalMC());
#endif #endif
} }