mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-23 16:48:06 +02:00
fixed NetworkInterface for Windows
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// NetworkInterface.h
|
// NetworkInterface.h
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Net/include/Poco/Net/NetworkInterface.h#2 $
|
// $Id: //poco/1.3/Net/include/Poco/Net/NetworkInterface.h#5 $
|
||||||
//
|
//
|
||||||
// Library: Net
|
// Library: Net
|
||||||
// Package: Sockets
|
// Package: Sockets
|
||||||
@@ -91,6 +91,15 @@ public:
|
|||||||
const std::string& name() const;
|
const std::string& name() const;
|
||||||
/// Returns the interface name.
|
/// Returns the interface name.
|
||||||
|
|
||||||
|
const std::string& displayName() const;
|
||||||
|
/// Returns the interface display name.
|
||||||
|
///
|
||||||
|
/// On Windows platforms, this is currently the network adapter
|
||||||
|
/// name. This may change to the "friendly name" of the network
|
||||||
|
/// connection in a future version, however.
|
||||||
|
///
|
||||||
|
/// On other platforms this is the same as name().
|
||||||
|
|
||||||
const IPAddress& address() const;
|
const IPAddress& address() const;
|
||||||
/// Returns the IP address bound to the interface.
|
/// Returns the IP address bound to the interface.
|
||||||
|
|
||||||
@@ -140,10 +149,10 @@ public:
|
|||||||
/// the same interface.
|
/// the same interface.
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
NetworkInterface(const std::string& name, const IPAddress& address, int index = -1);
|
NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, int index = -1);
|
||||||
/// Creates the NetworkInterface.
|
/// Creates the NetworkInterface.
|
||||||
|
|
||||||
NetworkInterface(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
|
NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
|
||||||
/// Creates the NetworkInterface.
|
/// Creates the NetworkInterface.
|
||||||
|
|
||||||
IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
|
IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// NetworkInterface.cpp
|
// NetworkInterface.cpp
|
||||||
//
|
//
|
||||||
// $Id: //poco/svn/Net/src/NetworkInterface.cpp#2 $
|
// $Id: //poco/1.3/Net/src/NetworkInterface.cpp#7 $
|
||||||
//
|
//
|
||||||
// Library: Net
|
// Library: Net
|
||||||
// Package: Sockets
|
// Package: Sockets
|
||||||
@@ -39,6 +39,9 @@
|
|||||||
#include "Poco/Net/NetException.h"
|
#include "Poco/Net/NetException.h"
|
||||||
#include "Poco/NumberFormatter.h"
|
#include "Poco/NumberFormatter.h"
|
||||||
#include "Poco/RefCountedObject.h"
|
#include "Poco/RefCountedObject.h"
|
||||||
|
#if defined(_WIN32) && defined(POCO_WIN32_UTF8)
|
||||||
|
#include "Poco/UnicodeConverter.h"
|
||||||
|
#endif
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|
||||||
@@ -59,11 +62,12 @@ class NetworkInterfaceImpl: public Poco::RefCountedObject
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
NetworkInterfaceImpl();
|
NetworkInterfaceImpl();
|
||||||
NetworkInterfaceImpl(const std::string& name, 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 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;
|
int index() const;
|
||||||
const std::string& name() const;
|
const std::string& name() const;
|
||||||
|
const std::string& displayName() const;
|
||||||
const IPAddress& address() const;
|
const IPAddress& address() const;
|
||||||
const IPAddress& subnetMask() const;
|
const IPAddress& subnetMask() const;
|
||||||
const IPAddress& broadcastAddress() const;
|
const IPAddress& broadcastAddress() const;
|
||||||
@@ -73,6 +77,7 @@ protected:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
std::string _name;
|
std::string _name;
|
||||||
|
std::string _displayName;
|
||||||
IPAddress _address;
|
IPAddress _address;
|
||||||
IPAddress _subnetMask;
|
IPAddress _subnetMask;
|
||||||
IPAddress _broadcastAddress;
|
IPAddress _broadcastAddress;
|
||||||
@@ -86,8 +91,9 @@ NetworkInterfaceImpl::NetworkInterfaceImpl():
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const IPAddress& address, int index):
|
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
|
||||||
_name(name),
|
_name(name),
|
||||||
|
_displayName(displayName),
|
||||||
_address(address),
|
_address(address),
|
||||||
_index(index)
|
_index(index)
|
||||||
{
|
{
|
||||||
@@ -118,8 +124,9 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const IPAddr
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
|
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
|
||||||
_name(name),
|
_name(name),
|
||||||
|
_displayName(displayName),
|
||||||
_address(address),
|
_address(address),
|
||||||
_subnetMask(subnetMask),
|
_subnetMask(subnetMask),
|
||||||
_broadcastAddress(broadcastAddress),
|
_broadcastAddress(broadcastAddress),
|
||||||
@@ -145,6 +152,12 @@ inline const std::string& NetworkInterfaceImpl::name() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
inline const std::string& NetworkInterfaceImpl::displayName() const
|
||||||
|
{
|
||||||
|
return _displayName;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
inline const IPAddress& NetworkInterfaceImpl::address() const
|
inline const IPAddress& NetworkInterfaceImpl::address() const
|
||||||
{
|
{
|
||||||
return _address;
|
return _address;
|
||||||
@@ -184,14 +197,14 @@ NetworkInterface::NetworkInterface(const NetworkInterface& interface):
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, int index):
|
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
|
||||||
_pImpl(new NetworkInterfaceImpl(name, address, index))
|
_pImpl(new NetworkInterfaceImpl(name, displayName, address, index))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
|
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index):
|
||||||
_pImpl(new NetworkInterfaceImpl(name, address, subnetMask, broadcastAddress, index))
|
_pImpl(new NetworkInterfaceImpl(name, displayName, address, subnetMask, broadcastAddress, index))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,6 +242,12 @@ const std::string& NetworkInterface::name() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
const std::string& NetworkInterface::displayName() const
|
||||||
|
{
|
||||||
|
return _pImpl->displayName();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
const IPAddress& NetworkInterface::address() const
|
const IPAddress& NetworkInterface::address() const
|
||||||
{
|
{
|
||||||
return _pImpl->address();
|
return _pImpl->address();
|
||||||
@@ -292,7 +311,7 @@ NetworkInterface NetworkInterface::forName(const std::string& name, bool require
|
|||||||
throw InterfaceNotFoundException(addr.toString(), "interface has no IP address");
|
throw InterfaceNotFoundException(addr.toString(), "interface has no IP address");
|
||||||
int index = 0;
|
int index = 0;
|
||||||
#endif
|
#endif
|
||||||
return NetworkInterface(name, addr, index);
|
return NetworkInterface(name, name, addr, index);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,7 +397,16 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
if (pAddress->FirstUnicastAddress)
|
if (pAddress->FirstUnicastAddress)
|
||||||
{
|
{
|
||||||
IPAddress addr(pAddress->FirstUnicastAddress->Address.lpSockaddr, pAddress->FirstUnicastAddress->Address.iSockaddrLength);
|
IPAddress addr(pAddress->FirstUnicastAddress->Address.lpSockaddr, pAddress->FirstUnicastAddress->Address.iSockaddrLength);
|
||||||
result.push_back(NetworkInterface(std::string(pAddress->AdapterName), addr, pAddress->Ipv6IfIndex));
|
std::string name(pAddress->AdapterName);
|
||||||
|
std::string displayName;
|
||||||
|
#ifdef POCO_WIN32_UTF8
|
||||||
|
Poco::UnicodeConverter::toUTF8(pAddress->Description, displayName);
|
||||||
|
#else
|
||||||
|
char displayNameBuffer[1024];
|
||||||
|
int rc = WideCharToMultiByte(CP_ACP, WC_DEFAULTCHAR, pAddress->Description, -1, displayNameBuffer, sizeof(displayNameBuffer), NULL, NULL);
|
||||||
|
if (rc) displayName = displayNameBuffer;
|
||||||
|
#endif
|
||||||
|
result.push_back(NetworkInterface(name, displayName, addr, pAddress->Ipv6IfIndex));
|
||||||
pAddress = pAddress->Next;
|
pAddress = pAddress->Next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -394,7 +422,7 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Add IPv4 loopback interface (not returned by GetAdaptersInfo)
|
// Add IPv4 loopback interface (not returned by GetAdaptersInfo)
|
||||||
result.push_back(NetworkInterface("Loopback", IPAddress("127.0.0.1"), IPAddress("255.0.0.0"), IPAddress(), -1));
|
result.push_back(NetworkInterface("Loopback", "Loopback Interface", IPAddress("127.0.0.1"), IPAddress("255.0.0.0"), IPAddress(), -1));
|
||||||
// On Windows 2000 we use GetAdaptersInfo.
|
// On Windows 2000 we use GetAdaptersInfo.
|
||||||
PIP_ADAPTER_INFO pAdapterInfo;
|
PIP_ADAPTER_INFO pAdapterInfo;
|
||||||
PIP_ADAPTER_INFO pInfo = 0;
|
PIP_ADAPTER_INFO pInfo = 0;
|
||||||
@@ -425,7 +453,9 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
IPAddress subnetMask(std::string(pInfo->IpAddressList.IpMask.String));
|
IPAddress subnetMask(std::string(pInfo->IpAddressList.IpMask.String));
|
||||||
IPAddress broadcastAddress(address);
|
IPAddress broadcastAddress(address);
|
||||||
broadcastAddress.mask(subnetMask, IPAddress("255.255.255.255"));
|
broadcastAddress.mask(subnetMask, IPAddress("255.255.255.255"));
|
||||||
result.push_back(NetworkInterface(std::string(pInfo->AdapterName), address, subnetMask, broadcastAddress));
|
std::string name(pInfo->AdapterName);
|
||||||
|
std::string displayName(pInfo->Description);
|
||||||
|
result.push_back(NetworkInterface(name, displayName, address, subnetMask, broadcastAddress));
|
||||||
}
|
}
|
||||||
pInfo = pInfo->Next;
|
pInfo = pInfo->Next;
|
||||||
}
|
}
|
||||||
@@ -475,14 +505,20 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
{
|
{
|
||||||
if (ifap->ifa_addr->sa_family == AF_INET)
|
if (ifap->ifa_addr->sa_family == AF_INET)
|
||||||
{
|
{
|
||||||
|
std::string name(ifap->ifa_name);
|
||||||
IPAddress addr(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_addr)->sin_addr, sizeof(struct in_addr));
|
IPAddress addr(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_addr)->sin_addr, sizeof(struct in_addr));
|
||||||
result.push_back(NetworkInterface(std::string(ifap->ifa_name), addr));
|
IPAddress subnetMask(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_netmask)->sin_addr, sizeof(struct in_addr));
|
||||||
|
IPAddress broadcastAddr;
|
||||||
|
if (ifap->ifa_flags & IFF_BROADCAST)
|
||||||
|
broadcastAddr = IPAddress(&reinterpret_cast<struct sockaddr_in*>(ifap->ifa_dstaddr)->sin_addr, sizeof(struct in_addr));
|
||||||
|
result.push_back(NetworkInterface(name, name, addr, subnetMask, broadcastAddr));
|
||||||
}
|
}
|
||||||
#if defined(POCO_HAVE_IPv6)
|
#if defined(POCO_HAVE_IPv6)
|
||||||
else if (ifap->ifa_addr->sa_family == AF_INET6)
|
else if (ifap->ifa_addr->sa_family == AF_INET6)
|
||||||
{
|
{
|
||||||
IPAddress addr(&reinterpret_cast<struct sockaddr_in6*>(ifap->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
|
IPAddress addr(&reinterpret_cast<struct sockaddr_in6*>(ifap->ifa_addr)->sin6_addr, sizeof(struct in6_addr));
|
||||||
result.push_back(NetworkInterface(std::string(ifap->ifa_name), addr, if_nametoindex(ifap->ifa_name)));
|
std::string name(ifap->ifa_name);
|
||||||
|
result.push_back(NetworkInterface(name, name, addr, if_nametoindex(ifap->ifa_name)));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -564,7 +600,8 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
#else
|
#else
|
||||||
int index = -1;
|
int index = -1;
|
||||||
#endif
|
#endif
|
||||||
result.push_back(NetworkInterface(std::string(ifr->ifr_name), addr, index));
|
std::string name(ifr->ifr_name);
|
||||||
|
result.push_back(NetworkInterface(name, name, addr, index));
|
||||||
}
|
}
|
||||||
ptr += sizeof(struct ifreq);
|
ptr += sizeof(struct ifreq);
|
||||||
}
|
}
|
||||||
@@ -659,7 +696,8 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
|||||||
#else
|
#else
|
||||||
int index = -1;
|
int index = -1;
|
||||||
#endif
|
#endif
|
||||||
result.push_back(NetworkInterface(std::string(ifr->ifr_name), addr, index));
|
std::string name(ifr->ifr_name);
|
||||||
|
result.push_back(NetworkInterface(name, name, addr, index));
|
||||||
}
|
}
|
||||||
len += sizeof(ifr->ifr_name);
|
len += sizeof(ifr->ifr_name);
|
||||||
ptr += len;
|
ptr += len;
|
||||||
|
Reference in New Issue
Block a user