mirror of
https://github.com/pocoproject/poco.git
synced 2025-01-18 00:15:27 +01:00
Networkinterface, Linux portion (with some additions and Windows changes, still WIP)
This commit is contained in:
parent
b5d704bfeb
commit
20d5b2d2fb
@ -24,6 +24,8 @@ Release 1.5.0 (2012-08-??)
|
||||
- fixed SF#3538785: SMTPClientSession::sendMessage() should take recipient list
|
||||
- added IPAddress::prefixLength()
|
||||
- UTF portability improvements
|
||||
- fixed SF#3556186: Linux shouldn't use <net/if.h> in Net/SocketDefs.h
|
||||
- added IPAddress RFC 4291 compatible site-local prefix support
|
||||
|
||||
Release 1.4.4 (2012-08-??)
|
||||
==========================
|
||||
|
@ -68,11 +68,12 @@ class Net_API NetworkInterface
|
||||
public:
|
||||
typedef std::vector<NetworkInterface> List;
|
||||
typedef List NetworkInterfaceList;//@deprecated
|
||||
typedef std::map<std::size_t, NetworkInterface> Map;
|
||||
typedef std::map<unsigned, NetworkInterface> Map;
|
||||
typedef Poco::Tuple<IPAddress, IPAddress, IPAddress> AddressTuple;
|
||||
typedef std::vector<AddressTuple> AddressList;
|
||||
typedef AddressList::iterator AddressIterator;
|
||||
typedef AddressList::const_iterator ConstAddressIterator;
|
||||
typedef std::vector<unsigned char> MacAddress;
|
||||
|
||||
enum AddressType
|
||||
{
|
||||
@ -88,7 +89,7 @@ public:
|
||||
IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address
|
||||
};
|
||||
|
||||
NetworkInterface(unsigned index = ~0);
|
||||
NetworkInterface(unsigned index = 0);
|
||||
/// Creates a NetworkInterface representing the
|
||||
/// default interface.
|
||||
///
|
||||
@ -128,7 +129,7 @@ public:
|
||||
///
|
||||
/// On other platforms this is the same as name().
|
||||
|
||||
const IPAddress& address(std::size_t index = 0) const;
|
||||
const IPAddress& address(unsigned index = 0) const;
|
||||
/// Returns the IP address bound to the interface.
|
||||
|
||||
void addAddress(const IPAddress& address);
|
||||
@ -140,18 +141,23 @@ public:
|
||||
const AddressList& addressList() const;
|
||||
/// Returns the list of IP addresses bound to the interface.
|
||||
|
||||
const IPAddress& subnetMask(std::size_t index = 0) const;
|
||||
const IPAddress& subnetMask(unsigned index = 0) const;
|
||||
/// Returns the subnet mask for this network interface.
|
||||
|
||||
const IPAddress& broadcastAddress(std::size_t index = 0) const;
|
||||
const IPAddress& broadcastAddress(unsigned index = 0) const;
|
||||
/// Returns the broadcast address for this network interface.
|
||||
|
||||
const IPAddress& destAddress(std::size_t index = 0) const;
|
||||
const IPAddress& destAddress(unsigned index = 0) const;
|
||||
/// Returns the IPv4 point-to-point destiation address for this network interface.
|
||||
|
||||
const MacAddress& macAddress() const;
|
||||
|
||||
unsigned mtu() const;
|
||||
/// Returns the MTU for this interface.
|
||||
|
||||
unsigned hwType() const;
|
||||
/// returns the MIB IfType of the interface.
|
||||
|
||||
bool supportsIP() const;
|
||||
/// Returns true if the interface supports IP.
|
||||
|
||||
@ -201,7 +207,7 @@ public:
|
||||
/// Throws an InterfaceNotFoundException if an interface
|
||||
/// with the give address does not exist.
|
||||
|
||||
static NetworkInterface forIndex(int index);
|
||||
static NetworkInterface forIndex(unsigned index);
|
||||
/// Returns the NetworkInterface for the given interface index.
|
||||
/// If an index of 0 is specified, a NetworkInterface instance
|
||||
/// representing the default interface (empty name and
|
||||
@ -211,14 +217,6 @@ public:
|
||||
/// with the given index does not exist (or IPv6 is not
|
||||
/// available).
|
||||
|
||||
static List list();
|
||||
/// Returns a list with all network interfaces
|
||||
/// on the system.
|
||||
///
|
||||
/// If there are multiple addresses bound to one interface,
|
||||
/// multiple NetworkInterface entries are listed for
|
||||
/// the same interface.
|
||||
|
||||
static Map map();
|
||||
/// Returns a map with all network interfaces
|
||||
/// on the system. Map is keyed by interface system
|
||||
@ -229,22 +227,22 @@ public:
|
||||
/// member of the pair.
|
||||
|
||||
protected:
|
||||
NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, int index = -1);
|
||||
NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, unsigned index = 0);
|
||||
/// Creates the NetworkInterface.
|
||||
|
||||
NetworkInterface(const std::string& name, const std::string& displayName, 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, unsigned index = 0);
|
||||
/// Creates the NetworkInterface.
|
||||
|
||||
NetworkInterface(const std::string& name, const IPAddress& address, int index = -1);
|
||||
NetworkInterface(const std::string& name, const IPAddress& address, unsigned index = 0);
|
||||
/// 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 IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, unsigned index = 0);
|
||||
/// Creates the NetworkInterface.
|
||||
|
||||
IPAddress interfaceNameToAddress(const std::string& interfaceName) const;
|
||||
/// Determines the IPAddress bound to the interface with the given name.
|
||||
|
||||
int interfaceNameToIndex(const std::string& interfaceName) const;
|
||||
unsigned interfaceNameToIndex(const std::string& interfaceName) const;
|
||||
/// Determines the interface index of the interface with the given name.
|
||||
|
||||
NetworkInterfaceImpl& impl() { return *_pImpl; };
|
||||
|
@ -162,7 +162,20 @@
|
||||
#include <netinet/tcp.h>
|
||||
#include <netdb.h>
|
||||
#if defined(POCO_OS_FAMILY_UNIX)
|
||||
#if (POCO_OS == POCO_OS_LINUX)
|
||||
// <linux/if.h> is needed by NetworkInterface for ifmap, ifreq and ifconf definitions
|
||||
#include <linux/if.h>
|
||||
// if_nametoindex and if_indextoname are needed by IPAddress and NetworkInterface
|
||||
// we can't get them from <net/if.h> because of a conflict with <linux/if.h>, so
|
||||
// we declare them here
|
||||
extern "C"
|
||||
{
|
||||
extern unsigned int if_nametoindex (__const char *__ifname) __THROW;
|
||||
extern char *if_indextoname (unsigned int __ifindex, char *__ifname) __THROW;
|
||||
}
|
||||
#else // (POCO_OS != POCO_OS_LINUX)
|
||||
#include <net/if.h>
|
||||
#endif // (POCO_OS != POCO_OS_LINUX)
|
||||
#endif
|
||||
#if defined(sun) || defined(__APPLE__)
|
||||
#include <sys/sockio.h>
|
||||
|
@ -70,28 +70,34 @@ public:
|
||||
typedef NetworkInterface::AddressTuple AddressTuple;
|
||||
typedef NetworkInterface::AddressList AddressList;
|
||||
|
||||
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, const IPAddress& subnetMask, const IPAddress& broadcastAddress, int index = -1);
|
||||
NetworkInterfaceImpl(unsigned index = 0);
|
||||
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, unsigned index = 0);
|
||||
NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, unsigned index = 0);
|
||||
|
||||
unsigned index() const;
|
||||
const std::string& name() const;
|
||||
const std::string& displayName() const;
|
||||
void addAddress(const AddressTuple& address);
|
||||
const IPAddress& address(std::size_t index = 0) const;
|
||||
const IPAddress& address(unsigned index = 0) const;
|
||||
const NetworkInterface::AddressList& addressList() const;
|
||||
bool hasAddress(const IPAddress& address) const;
|
||||
const IPAddress& subnetMask(std::size_t index = 0) const;
|
||||
const IPAddress& broadcastAddress(std::size_t index = 0) const;
|
||||
const IPAddress& destAddress(std::size_t index = 0) const;
|
||||
const IPAddress& subnetMask(unsigned index = 0) const;
|
||||
const IPAddress& broadcastAddress(unsigned index = 0) const;
|
||||
const IPAddress& destAddress(unsigned index = 0) const;
|
||||
const NetworkInterface::MacAddress& macAddress() const;
|
||||
bool supportsIPv4() const;
|
||||
bool supportsIPv6() const;
|
||||
|
||||
void setName(const std::string& name);
|
||||
void setDisplayName(const std::string& name);
|
||||
void addAddress(const IPAddress& addr);
|
||||
void setMacAddress(const NetworkInterface::MacAddress& addr);
|
||||
void setMacAddress(const void *addr, std::size_t len);
|
||||
|
||||
unsigned mtu() const;
|
||||
unsigned ifindex() const;
|
||||
unsigned hwType() const;
|
||||
|
||||
bool broadcast() const;
|
||||
bool loopback() const;
|
||||
bool multicast() const;
|
||||
@ -109,10 +115,10 @@ protected:
|
||||
#endif
|
||||
|
||||
void setUp(bool up);
|
||||
void setMtu(int mtu);
|
||||
void setIndex(std::size_t index);
|
||||
void setMtu(unsigned mtu);
|
||||
void setHWType(unsigned hwType);
|
||||
void setIndex(unsigned index);
|
||||
void getPhyParams();
|
||||
void getIPv4Params();
|
||||
void getPeerAddress();
|
||||
|
||||
private:
|
||||
@ -127,23 +133,22 @@ private:
|
||||
bool _up;
|
||||
bool _running;
|
||||
unsigned _mtu;
|
||||
unsigned _hwType;
|
||||
|
||||
NetworkInterface::MacAddress _macAddress;
|
||||
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS)
|
||||
friend NetworkInterface::Map NetworkInterface::map();
|
||||
#endif
|
||||
|
||||
};
|
||||
|
||||
|
||||
NetworkInterfaceImpl::NetworkInterfaceImpl(unsigned index):
|
||||
_index(index),
|
||||
_mtu(~0)
|
||||
_mtu(0)
|
||||
{
|
||||
_addressList.resize(1);
|
||||
}
|
||||
|
||||
|
||||
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
|
||||
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, const IPAddress& address, unsigned index):
|
||||
_name(name),
|
||||
_displayName(displayName),
|
||||
_index(index),
|
||||
@ -153,17 +158,15 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
|
||||
_pointToPoint(false),
|
||||
_up(false),
|
||||
_running(false),
|
||||
_mtu(-1)
|
||||
_mtu(0)
|
||||
{
|
||||
_addressList.push_back(AddressTuple(address, IPAddress(), IPAddress()));
|
||||
getPhyParams();
|
||||
// get remaining IPv4 params from kernel
|
||||
if (address.family() == IPAddress::IPv4) getIPv4Params();
|
||||
if (_pointToPoint) getPeerAddress();
|
||||
}
|
||||
|
||||
|
||||
NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::string& displayName, 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, unsigned index):
|
||||
_name(name),
|
||||
_displayName(displayName),
|
||||
_index(index),
|
||||
@ -173,7 +176,7 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
|
||||
_pointToPoint(false),
|
||||
_up(false),
|
||||
_running(false),
|
||||
_mtu(-1)
|
||||
_mtu(0)
|
||||
{
|
||||
_addressList.push_back(AddressTuple(address, subnetMask, broadcastAddress));
|
||||
getPhyParams();
|
||||
@ -184,53 +187,15 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
|
||||
void NetworkInterfaceImpl::getPhyParams()
|
||||
{
|
||||
#if !defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_VXWORKS)
|
||||
const IPAddress::Family family = _addressList.family();
|
||||
struct ifreq ifr;
|
||||
std::strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ);
|
||||
DatagramSocket ds(family);
|
||||
DatagramSocket ds;
|
||||
|
||||
ds.impl()->ioctl(SIOCGIFFLAGS, &ifr);
|
||||
setFlags(ifr.ifr_flags);
|
||||
|
||||
ds.impl()->ioctl(SIOCGIFMTU, &ifr);
|
||||
setMtu(ifr.ifr_mtu);
|
||||
|
||||
#if POCO_OS == POCO_OS_MAC_OS_X
|
||||
setIfIndex(if_nametoindex(ifr.ifr_name));
|
||||
#else
|
||||
ds.impl()->ioctl(SIOCGIFINDEX, &ifr);
|
||||
setIfIndex(ifr.ifr_ifindex);
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void NetworkInterfaceImpl::getIPv4Params()
|
||||
{
|
||||
#if !defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_VXWORKS)
|
||||
struct ifreq ifr;
|
||||
std::strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ);
|
||||
DatagramSocket ds(IPAddress::IPv4);
|
||||
|
||||
if (!_loopback) {
|
||||
ds.impl()->ioctl(SIOCGIFNETMASK, &ifr);
|
||||
if (ifr.ifr_addr.sa_family == AF_INET)
|
||||
_subnetMask = IPAddress(ifr.ifr_addr);
|
||||
}
|
||||
|
||||
if (!_loopback && !_pointToPoint)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Not every interface (e.g. loopback) has a broadcast address
|
||||
ds.impl()->ioctl(SIOCGIFBRDADDR, &ifr);
|
||||
if (ifr.ifr_addr.sa_family == AF_INET)
|
||||
_broadcastAddress = IPAddress(ifr.ifr_addr);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -251,7 +216,7 @@ void NetworkInterfaceImpl::getPeerAddress()
|
||||
if (ifr.ifr_dstaddr.sa_family == AF_INET)
|
||||
it->set<NetworkInterface::BROADCAST_ADDRESS>(IPAddress(ifr.ifr_dstaddr));
|
||||
else
|
||||
it->set<NetworkInterface::BROADCAST_ADDRESS>(IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(&ifr.ifr_dstaddr)->sin6_addr, sizeof(struct in6_addr), _index));
|
||||
it->set<NetworkInterface::BROADCAST_ADDRESS>(IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(&ifr.ifr_dstaddr)->sin6_addr, sizeof(struct in6_addr), _index));
|
||||
#else
|
||||
//TODO
|
||||
#endif
|
||||
@ -329,10 +294,10 @@ bool NetworkInterfaceImpl::hasAddress(const IPAddress& address) const
|
||||
}
|
||||
|
||||
|
||||
inline const IPAddress& NetworkInterfaceImpl::address(std::size_t index) const
|
||||
inline const IPAddress& NetworkInterfaceImpl::address(unsigned index) const
|
||||
{
|
||||
if (index < _addressList.size()) return _addressList[index].get<NetworkInterface::IP_ADDRESS>();
|
||||
else throw NotFoundException(Poco::format("No address with index %z.", index));
|
||||
else throw NotFoundException(Poco::format("No address with index %u.", index));
|
||||
}
|
||||
|
||||
|
||||
@ -342,32 +307,38 @@ inline const NetworkInterface::AddressList& NetworkInterfaceImpl::addressList()
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterfaceImpl::subnetMask(std::size_t index) const
|
||||
const IPAddress& NetworkInterfaceImpl::subnetMask(unsigned index) const
|
||||
{
|
||||
if (index < _addressList.size())
|
||||
return _addressList[index].get<NetworkInterface::SUBNET_MASK>();
|
||||
|
||||
throw NotFoundException(Poco::format("No subnet mask with index %z.", index));
|
||||
throw NotFoundException(Poco::format("No subnet mask with index %u.", index));
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterfaceImpl::broadcastAddress(std::size_t index) const
|
||||
const IPAddress& NetworkInterfaceImpl::broadcastAddress(unsigned index) const
|
||||
{
|
||||
if (index < _addressList.size())
|
||||
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>();
|
||||
|
||||
throw NotFoundException(Poco::format("No subnet mask with index %z.", index));
|
||||
throw NotFoundException(Poco::format("No subnet mask with index %u.", index));
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterfaceImpl::destAddress(std::size_t index) const
|
||||
const IPAddress& NetworkInterfaceImpl::destAddress(unsigned index) const
|
||||
{
|
||||
if (!pointToPoint())
|
||||
throw InvalidAccessException("Only PPP addresses have destination address.");
|
||||
else if (index < _addressList.size())
|
||||
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>();
|
||||
|
||||
throw NotFoundException(Poco::format("No address with index %z.", index));
|
||||
throw NotFoundException(Poco::format("No address with index %u.", index));
|
||||
}
|
||||
|
||||
|
||||
const NetworkInterface::MacAddress& NetworkInterfaceImpl::macAddress() const
|
||||
{
|
||||
return _macAddress;
|
||||
}
|
||||
|
||||
|
||||
@ -377,6 +348,12 @@ inline unsigned NetworkInterfaceImpl::mtu() const
|
||||
}
|
||||
|
||||
|
||||
inline unsigned NetworkInterfaceImpl::hwType() const
|
||||
{
|
||||
return _hwType;
|
||||
}
|
||||
|
||||
|
||||
inline bool NetworkInterfaceImpl::broadcast() const
|
||||
{
|
||||
return _broadcast;
|
||||
@ -461,12 +438,18 @@ inline void NetworkInterfaceImpl::setUp(bool up)
|
||||
}
|
||||
|
||||
|
||||
inline void NetworkInterfaceImpl::setMtu(int mtu)
|
||||
inline void NetworkInterfaceImpl::setMtu(unsigned mtu)
|
||||
{
|
||||
_mtu = mtu;
|
||||
}
|
||||
|
||||
|
||||
inline void NetworkInterfaceImpl::setHWType(unsigned hwType)
|
||||
{
|
||||
_hwType = hwType;
|
||||
}
|
||||
|
||||
|
||||
inline void NetworkInterfaceImpl::setIndex(unsigned index)
|
||||
{
|
||||
_index = index;
|
||||
@ -490,6 +473,19 @@ inline void NetworkInterfaceImpl::addAddress(const IPAddress& addr)
|
||||
_addressList.push_back(addr);
|
||||
}
|
||||
|
||||
inline void NetworkInterfaceImpl::setMacAddress(const NetworkInterface::MacAddress& addr)
|
||||
{
|
||||
_macAddress = addr;
|
||||
}
|
||||
|
||||
|
||||
inline void NetworkInterfaceImpl::setMacAddress(const void *addr, std::size_t len)
|
||||
{
|
||||
_macAddress.clear();
|
||||
for (unsigned i = 0; i < len; ++i)
|
||||
_macAddress.push_back(((unsigned char *)addr)[i]);
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// NetworkInterface
|
||||
@ -499,7 +495,7 @@ inline void NetworkInterfaceImpl::addAddress(const IPAddress& addr)
|
||||
FastMutex NetworkInterface::_mutex;
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterface(std::size_t index):
|
||||
NetworkInterface::NetworkInterface(unsigned index):
|
||||
_pImpl(new NetworkInterfaceImpl(index))
|
||||
{
|
||||
}
|
||||
@ -512,25 +508,25 @@ NetworkInterface::NetworkInterface(const NetworkInterface& interfc):
|
||||
}
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, int index):
|
||||
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, const IPAddress& address, unsigned index):
|
||||
_pImpl(new NetworkInterfaceImpl(name, displayName, address, index))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterface(const std::string& name, const std::string& displayName, 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, unsigned index):
|
||||
_pImpl(new NetworkInterfaceImpl(name, displayName, address, subnetMask, broadcastAddress, index))
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, int index):
|
||||
NetworkInterface::NetworkInterface(const std::string& name, const IPAddress& address, unsigned index):
|
||||
_pImpl(new NetworkInterfaceImpl(name, name, 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 IPAddress& address, const IPAddress& subnetMask, const IPAddress& broadcastAddress, unsigned index):
|
||||
_pImpl(new NetworkInterfaceImpl(name, name, address, subnetMask, broadcastAddress, index))
|
||||
{
|
||||
}
|
||||
@ -587,7 +583,7 @@ void NetworkInterface::addAddress(const IPAddress& address, const IPAddress& sub
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterface::address(std::size_t index) const
|
||||
const IPAddress& NetworkInterface::address(unsigned index) const
|
||||
{
|
||||
return _pImpl->address(index);
|
||||
}
|
||||
@ -599,19 +595,25 @@ const NetworkInterface::AddressList& NetworkInterface::addressList() const
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterface::subnetMask(std::size_t index) const
|
||||
const IPAddress& NetworkInterface::subnetMask(unsigned index) const
|
||||
{
|
||||
return _pImpl->subnetMask(index);
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterface::broadcastAddress(std::size_t index) const
|
||||
const IPAddress& NetworkInterface::broadcastAddress(unsigned index) const
|
||||
{
|
||||
return _pImpl->broadcastAddress(index);
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterface::destAddress(std::size_t index) const
|
||||
const NetworkInterface::MacAddress& NetworkInterface::macAddress() const
|
||||
{
|
||||
return _pImpl->macAddress();
|
||||
}
|
||||
|
||||
|
||||
const IPAddress& NetworkInterface::destAddress(unsigned index) const
|
||||
{
|
||||
return _pImpl->destAddress(index);
|
||||
}
|
||||
@ -622,6 +624,10 @@ unsigned NetworkInterface::mtu() const
|
||||
return _pImpl->mtu();
|
||||
}
|
||||
|
||||
unsigned NetworkInterface::hwType() const
|
||||
{
|
||||
return _pImpl->hwType();
|
||||
}
|
||||
|
||||
bool NetworkInterface::supportsIP() const
|
||||
{
|
||||
@ -679,11 +685,14 @@ bool NetworkInterface::isUp() const
|
||||
|
||||
NetworkInterface NetworkInterface::forName(const std::string& name, bool requireIPv6)
|
||||
{
|
||||
NetworkInterfaceList ifs = list();
|
||||
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
|
||||
Map map = NetworkInterface::map();
|
||||
Map::const_iterator it = map.begin();
|
||||
Map::const_iterator end = map.end();
|
||||
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if (it->name() == name && ((requireIPv6 && it->supportsIPv6()) || !requireIPv6))
|
||||
return *it;
|
||||
if (it->second.name() == name && ((requireIPv6 && it->second.supportsIPv6()) || !requireIPv6))
|
||||
return it->second;
|
||||
}
|
||||
throw InterfaceNotFoundException(name);
|
||||
}
|
||||
@ -691,17 +700,20 @@ NetworkInterface NetworkInterface::forName(const std::string& name, bool require
|
||||
|
||||
NetworkInterface NetworkInterface::forName(const std::string& name, IPVersion ipVersion)
|
||||
{
|
||||
NetworkInterfaceList ifs = list();
|
||||
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
|
||||
Map map = NetworkInterface::map();
|
||||
Map::const_iterator it = map.begin();
|
||||
Map::const_iterator end = map.end();
|
||||
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if (it->name() == name)
|
||||
if (it->second.name() == name)
|
||||
{
|
||||
if (ipVersion == IPv4_ONLY && it->supportsIPv4())
|
||||
return *it;
|
||||
else if (ipVersion == IPv6_ONLY && it->supportsIPv6())
|
||||
return *it;
|
||||
if (ipVersion == IPv4_ONLY && it->second.supportsIPv4())
|
||||
return it->second;
|
||||
else if (ipVersion == IPv6_ONLY && it->second.supportsIPv6())
|
||||
return it->second;
|
||||
else if (ipVersion == IPv4_OR_IPv6)
|
||||
return *it;
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
throw InterfaceNotFoundException(name);
|
||||
@ -710,66 +722,67 @@ NetworkInterface NetworkInterface::forName(const std::string& name, IPVersion ip
|
||||
|
||||
NetworkInterface NetworkInterface::forAddress(const IPAddress& addr)
|
||||
{
|
||||
NetworkInterfaceList ifs = list();
|
||||
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
|
||||
Map map = NetworkInterface::map();
|
||||
Map::const_iterator it = map.begin();
|
||||
Map::const_iterator end = map.end();
|
||||
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
if (it->address() == addr)
|
||||
return *it;
|
||||
const unsigned count = it->second.addressList().size();
|
||||
for (unsigned i = 0; i < count; ++i)
|
||||
{
|
||||
if (it->second.address(i) == addr)
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
throw InterfaceNotFoundException(addr.toString());
|
||||
}
|
||||
|
||||
|
||||
NetworkInterface NetworkInterface::forIndex(int i)
|
||||
NetworkInterface NetworkInterface::forIndex(unsigned i)
|
||||
{
|
||||
if (i != 0)
|
||||
{
|
||||
NetworkInterfaceList ifs = list();
|
||||
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
|
||||
{
|
||||
if (it->index() == i)
|
||||
return *it;
|
||||
}
|
||||
throw InterfaceNotFoundException("#" + NumberFormatter::format(i));
|
||||
Map map = NetworkInterface::map();
|
||||
|
||||
Map::const_iterator it = map.find(i);
|
||||
if (it != map.end())
|
||||
return it->second;
|
||||
}
|
||||
else return NetworkInterface();
|
||||
throw InterfaceNotFoundException("#" + NumberFormatter::format(i));
|
||||
}
|
||||
|
||||
#if (POCO_OS == POCO_OS_LINUX)
|
||||
|
||||
NetworkInterface::List NetworkInterface::list()
|
||||
#include <net/if_arp.h>
|
||||
|
||||
static unsigned arphrdToIfType(unsigned arphrd)
|
||||
{
|
||||
List list;
|
||||
Map m = map();
|
||||
|
||||
for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it)
|
||||
{
|
||||
int index = it->second.index();
|
||||
std::string name = it->second.name();
|
||||
std::string displayName = it->second.displayName();
|
||||
|
||||
typedef NetworkInterface::AddressList List;
|
||||
const List& ipList = it->second.addressList();
|
||||
List::const_iterator ipIt = ipList.begin();
|
||||
List::const_iterator ipEnd = ipList.end();
|
||||
for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter)
|
||||
{
|
||||
IPAddress addr = ipIt->get<NetworkInterface::IP_ADDRESS>();
|
||||
IPAddress mask = ipIt->get<NetworkInterface::SUBNET_MASK>();
|
||||
NetworkInterface ni;
|
||||
if (mask.isWildcard())
|
||||
ni = NetworkInterface(name, displayName, addr, index);
|
||||
else
|
||||
{
|
||||
IPAddress broadcast = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
|
||||
ni = NetworkInterface(name, displayName, addr, mask, broadcast, index);
|
||||
}
|
||||
|
||||
list.push_back(ni);
|
||||
}
|
||||
switch (arphrd) {
|
||||
case ARPHRD_ETHER:
|
||||
return 6; // IF_TYPE_ETHERNET_CSMACD
|
||||
case ARPHRD_IEEE802:
|
||||
return 9; // IF_TYPE_ISO88025_TOKENRING
|
||||
case ARPHRD_DLCI:
|
||||
return 32; // IF_TYPE_FRAMERELAY
|
||||
case ARPHRD_PPP:
|
||||
return 512; // IF_TYPE_PPP
|
||||
case ARPHRD_LOOPBACK:
|
||||
return 24; // IF_TYPE_SOFTWARE_LOOPBACK
|
||||
case ARPHRD_ATM:
|
||||
return 37; // IF_TYPE_ATM
|
||||
case ARPHRD_IEEE80211:
|
||||
return 71; // IF_TYPE_IEEE80211
|
||||
case ARPHRD_TUNNEL:
|
||||
case ARPHRD_TUNNEL6:
|
||||
return 131; // IF_TYPE_TUNNEL
|
||||
case ARPHRD_IEEE1394:
|
||||
return 144; // IF_TYPE_IEEE1394
|
||||
default:
|
||||
return 1; // IF_TYPE_OTHER
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
@ -854,6 +867,9 @@ NetworkInterface::Map NetworkInterface::map()
|
||||
ULONG outBufLen = 16384;
|
||||
Poco::Buffer<UCHAR> memory(outBufLen);
|
||||
ULONG flags = (GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX);
|
||||
#if (POCO_OS != POCO_OS_WINDOWS_CE)
|
||||
flags |= GAA_FLAG_INCLUDE_ALL_INTERFACES;
|
||||
#endif
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
const unsigned family = AF_UNSPEC; //IPv4 and IPv6
|
||||
#else
|
||||
@ -880,8 +896,22 @@ NetworkInterface::Map NetworkInterface::map()
|
||||
IPAddress address;
|
||||
IPAddress subnetMask;
|
||||
IPAddress broadcastAddress;
|
||||
unsigned ifIndex = ~0;
|
||||
unsigned ifIndex = 0;
|
||||
|
||||
//
|
||||
// Create interface even if it has an empty list of addresses; also, set
|
||||
// physical attributes which are protocol independent (name, media type,
|
||||
// MAC address, MTU, operational status, etc).
|
||||
//
|
||||
Map::iterator ifIt = result.insert(Map::value_type(ifInex, NetworkInterface(name, displayName, ifIndex);
|
||||
|
||||
ifIt->second.impl().setFlags(pAddress->Flags, pAddress->IfType);
|
||||
ifIt->second.impl().setMtu(pAddress->Mtu);
|
||||
ifIt->second.impl().setUp(pAddress->OperStatus == IfOperStatusUp);
|
||||
|
||||
ifIt->second.impl().setMacAddress(pAddress->PhysicalAddress, pAddress->PhysicalAddressLength);
|
||||
ifIt->second.impl().setHWType(pAddress->IfType);
|
||||
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
poco_assert (pAddress->Ipv6IfIndex == pAddress->IfIndex);
|
||||
if (pAddress->Flags & IP_ADAPTER_IPV6_ENABLED) ifIndex = pAddress->Ipv6IfIndex;
|
||||
@ -905,7 +935,6 @@ NetworkInterface::Map NetworkInterface::map()
|
||||
UINT8 prefixLength = pUniAddr->OnLinkPrefixLength;
|
||||
address = IPAddress(pUniAddr->Address);
|
||||
ADDRESS_FAMILY family = pUniAddr->Address.lpSockaddr->sa_family;
|
||||
Map::iterator ifIt = result.find(ifIndex);
|
||||
switch (family)
|
||||
{
|
||||
case AF_INET:
|
||||
@ -916,33 +945,18 @@ NetworkInterface::Map NetworkInterface::map()
|
||||
// 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)
|
||||
ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, subnetMask, broadcastAddress, ifIndex))).first;
|
||||
else
|
||||
ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, ifIndex))).first;
|
||||
}
|
||||
else // network interface is cached, add address to it
|
||||
{
|
||||
if (hasBroadcast)
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
else
|
||||
ifIt->second.addAddress(address);
|
||||
}
|
||||
if (hasBroadcast)
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
else
|
||||
ifIt->second.addAddress(address);
|
||||
} break;
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
case AF_INET6:
|
||||
{
|
||||
if (ifIt == result.end()) ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, ifIndex))).first;
|
||||
else
|
||||
ifIt->second.addAddress(address);
|
||||
ifIt->second.addAddress(address);
|
||||
} break;
|
||||
#endif
|
||||
} // switch family
|
||||
ifIt->second.impl().setFlags(pAddress->Flags, pAddress->IfType);
|
||||
ifIt->second.impl().setMtu(pAddress->Mtu);
|
||||
ifIt->second.impl().setUp(pAddress->OperStatus == IfOperStatusUp);
|
||||
} // for addresses
|
||||
} // for adapters
|
||||
return result;
|
||||
@ -1012,81 +1026,22 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_dl.h>
|
||||
#include <net/if_types.h>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
NetworkInterface::Map NetworkInterface::map()
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
NetworkInterfaceList result;
|
||||
|
||||
struct ifaddrs* ifaphead;
|
||||
int rc = getifaddrs(&ifaphead);
|
||||
if (rc) throw NetException("cannot get network adapter list");
|
||||
|
||||
for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
|
||||
{
|
||||
if (ifap->ifa_addr)
|
||||
{
|
||||
if (ifap->ifa_addr->sa_family == AF_INET)
|
||||
{
|
||||
std::string name(ifap->ifa_name);
|
||||
IPAddress addr(*(ifap->ifa_addr));
|
||||
IPAddress subnetMask, broadcastAddr;
|
||||
if (ifap->ifa_netmask)
|
||||
subnetMask = IPAddress(*(ifap->ifa_netmask));
|
||||
if ((ifap->ifa_flags & IFF_BROADCAST)
|
||||
&& ifap->ifa_broadaddr)
|
||||
broadcastAddr = IPAddress(*(ifap->ifa_broadaddr));
|
||||
result.push_back(NetworkInterface(name, name, addr, subnetMask, broadcastAddr));
|
||||
}
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
else if (ifap->ifa_addr->sa_family == AF_INET6)
|
||||
{
|
||||
std::string name(ifap->ifa_name);
|
||||
Poco::UInt32 ifIndex = if_nametoindex(ifap->ifa_name);
|
||||
IPAddress addr(&reinterpret_cast<struct sockaddr_in6*>(ifap->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex);
|
||||
IPAddress subnetMask, broadcastAddr;
|
||||
if (ifap->ifa_netmask)
|
||||
subnetMask = IPAddress(*(ifap->ifa_netmask));
|
||||
if ((ifap->ifa_flags & IFF_BROADCAST)
|
||||
&& ifap->ifa_broadaddr)
|
||||
broadcastAddr = IPAddress(*(ifap->ifa_broadaddr));
|
||||
result.push_back(NetworkInterface(name, name, addr, subnetMask, broadcastAddr, ifIndex));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
freeifaddrs(ifaphead);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
} } // namespace Poco::Net
|
||||
|
||||
|
||||
#elif POCO_OS == POCO_OS_LINUX
|
||||
//
|
||||
// Linux
|
||||
//
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
{
|
||||
NetworkInterfaceList result;
|
||||
Map result;
|
||||
unsigned ifIndex = 0;
|
||||
NetworkInterface intf;
|
||||
Map::iterator ifIt;
|
||||
|
||||
struct ifaddrs* ifaces = 0;
|
||||
struct ifaddrs* currIface = 0;
|
||||
@ -1098,30 +1053,62 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
{
|
||||
for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next)
|
||||
{
|
||||
IPAddress addr;
|
||||
bool haveAddr = false;
|
||||
int ifIndex(-1);
|
||||
if (currIface->ifa_addr)
|
||||
IPAddress address, subnetMask, broadcastAddress;
|
||||
struct sockaddr_dl* sdl;
|
||||
|
||||
switch (currIface->ifa_addr->sa_family)
|
||||
{
|
||||
switch (currIface->ifa_addr->sa_family)
|
||||
{
|
||||
case AF_INET6:
|
||||
ifIndex = if_nametoindex(currIface->ifa_name);
|
||||
addr = IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(currIface->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex);
|
||||
haveAddr = true;
|
||||
break;
|
||||
case AF_INET:
|
||||
addr = IPAddress(*(currIface->ifa_addr));
|
||||
haveAddr = true;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (haveAddr)
|
||||
{
|
||||
std::string name(currIface->ifa_name);
|
||||
result.push_back(NetworkInterface(name, name, addr, ifIndex));
|
||||
case AF_LINK:
|
||||
sdl = (struct sockaddr_dl*)currIface->ifa_addr;
|
||||
ifIndex = sdl->sdl_index;
|
||||
intf = NetworkInterface(ifIndex);
|
||||
intf.impl().setName(currIface->ifa_name);
|
||||
intf.impl().setDisplayName(currIface->ifa_name);
|
||||
intf.impl().getPhyParams();
|
||||
|
||||
intf.impl().setMacAddress(LLADDR(sdl), sdl->sdl_alen);
|
||||
intf.impl().setHWType(sdl->sdl_type);
|
||||
|
||||
ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
|
||||
break;
|
||||
case AF_INET:
|
||||
// need to use Map to search by name, i.e. result.find(name)
|
||||
ifIndex = if_nametoindex(currIface->ifa_name);
|
||||
ifIt = result.find(ifIndex);
|
||||
|
||||
address = IPAddress(*(currIface->ifa_addr));
|
||||
subnetMask = IPAddress(*(currIface->ifa_netmask));
|
||||
|
||||
if (currIface->ifa_flags & IFF_BROADCAST && currIface->ifa_broadaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_broadaddr));
|
||||
else if (currIface->ifa_flags & IFF_POINTOPOINT && currIface->ifa_dstaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_dstaddr));
|
||||
else
|
||||
broadcastAddress = IPAddress();
|
||||
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
break;
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
case AF_INET6:
|
||||
// need to use Map to search by name, i.e. result.find(name)
|
||||
ifIndex = if_nametoindex(currIface->ifa_name);
|
||||
ifIt = result.find(ifIndex);
|
||||
|
||||
address = IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(currIface->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex);
|
||||
subnetMask = IPAddress(*(currIface->ifa_netmask));
|
||||
|
||||
if (currIface->ifa_flags & IFF_BROADCAST && currIface->ifa_broadaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_broadaddr));
|
||||
else if (currIface->ifa_flags & IFF_POINTOPOINT && currIface->ifa_dstaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_dstaddr));
|
||||
else
|
||||
broadcastAddress = IPAddress();
|
||||
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1137,74 +1124,104 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
} } // namespace Poco::Net
|
||||
|
||||
|
||||
#else // !POCO_HAVE_IPv6
|
||||
#elif POCO_OS == POCO_OS_LINUX
|
||||
//
|
||||
// Linux
|
||||
//
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <linux/if.h>
|
||||
#include <linux/if_packet.h>
|
||||
|
||||
|
||||
namespace Poco {
|
||||
namespace Net {
|
||||
|
||||
|
||||
NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
NetworkInterface::Map NetworkInterface::map()
|
||||
{
|
||||
FastMutex::ScopedLock lock(_mutex);
|
||||
NetworkInterfaceList result;
|
||||
DatagramSocket socket;
|
||||
// the following code is loosely based
|
||||
// on W. Richard Stevens, UNIX Network Programming, pp 434ff.
|
||||
int lastlen = 0;
|
||||
int len = 100*sizeof(struct ifreq);
|
||||
char* buf = 0;
|
||||
try
|
||||
Map result;
|
||||
unsigned ifIndex = 0;
|
||||
NetworkInterface intf;
|
||||
Map::iterator ifIt;
|
||||
|
||||
struct ifaddrs* ifaces = 0;
|
||||
struct ifaddrs* currIface = 0;
|
||||
|
||||
if (getifaddrs(&ifaces) < 0)
|
||||
throw NetException("cannot get network adapter list");
|
||||
|
||||
try
|
||||
{
|
||||
struct ifconf ifc;
|
||||
for (;;)
|
||||
for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next)
|
||||
{
|
||||
buf = new char[len];
|
||||
ifc.ifc_len = len;
|
||||
ifc.ifc_buf = buf;
|
||||
if (::ioctl(socket.impl()->sockfd(), SIOCGIFCONF, &ifc) < 0)
|
||||
{
|
||||
if (errno != EINVAL || lastlen != 0)
|
||||
throw NetException("cannot get network adapter list");
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ifc.ifc_len == lastlen)
|
||||
break;
|
||||
lastlen = ifc.ifc_len;
|
||||
}
|
||||
len += 10*sizeof(struct ifreq);
|
||||
delete [] buf;
|
||||
}
|
||||
for (const char* ptr = buf; ptr < buf + ifc.ifc_len;)
|
||||
{
|
||||
const struct ifreq* ifr = reinterpret_cast<const struct ifreq*>(ptr);
|
||||
IPAddress addr;
|
||||
bool haveAddr = false;
|
||||
switch (ifr->ifr_addr.sa_family)
|
||||
IPAddress address, subnetMask, broadcastAddress;
|
||||
struct sockaddr_ll* sll;
|
||||
|
||||
switch (currIface->ifa_addr->sa_family)
|
||||
{
|
||||
case AF_PACKET:
|
||||
sll = (struct sockaddr_ll*)currIface->ifa_addr;
|
||||
ifIndex = sll->sll_ifindex;
|
||||
intf = NetworkInterface(ifIndex);
|
||||
intf.impl().setName(currIface->ifa_name);
|
||||
intf.impl().setDisplayName(currIface->ifa_name);
|
||||
intf.impl().getPhyParams();
|
||||
|
||||
intf.impl().setMacAddress(sll->sll_addr, sll->sll_halen);
|
||||
intf.impl().setHWType(arphrdToIfType(sll->sll_hatype));
|
||||
|
||||
ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
|
||||
break;
|
||||
case AF_INET:
|
||||
addr = IPAddress(ifr->ifr_addr);
|
||||
haveAddr = true;
|
||||
// need to use Map to search by name, i.e. result.find(name)
|
||||
ifIndex = if_nametoindex(currIface->ifa_name);
|
||||
ifIt = result.find(ifIndex);
|
||||
|
||||
address = IPAddress(*(currIface->ifa_addr));
|
||||
subnetMask = IPAddress(*(currIface->ifa_netmask));
|
||||
|
||||
if (currIface->ifa_flags & IFF_BROADCAST && currIface->ifa_broadaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_broadaddr));
|
||||
else if (currIface->ifa_flags & IFF_POINTOPOINT && currIface->ifa_dstaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_dstaddr));
|
||||
else
|
||||
broadcastAddress = IPAddress();
|
||||
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
break;
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
case AF_INET6:
|
||||
// need to use Map to search by name, i.e. result.find(name)
|
||||
ifIndex = if_nametoindex(currIface->ifa_name);
|
||||
ifIt = result.find(ifIndex);
|
||||
|
||||
address = IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(currIface->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex);
|
||||
subnetMask = IPAddress(*(currIface->ifa_netmask));
|
||||
|
||||
if (currIface->ifa_flags & IFF_BROADCAST && currIface->ifa_broadaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_broadaddr));
|
||||
else if (currIface->ifa_flags & IFF_POINTOPOINT && currIface->ifa_dstaddr)
|
||||
broadcastAddress = IPAddress(*(currIface->ifa_dstaddr));
|
||||
else
|
||||
broadcastAddress = IPAddress();
|
||||
|
||||
ifIt->second.addAddress(address, subnetMask, broadcastAddress);
|
||||
break;
|
||||
#endif
|
||||
default:
|
||||
break;
|
||||
continue;
|
||||
}
|
||||
if (haveAddr)
|
||||
{
|
||||
int index = -1;
|
||||
std::string name(ifr->ifr_name);
|
||||
result.push_back(NetworkInterface(name, name, addr, index));
|
||||
}
|
||||
ptr += sizeof(struct ifreq);
|
||||
}
|
||||
}
|
||||
catch (...)
|
||||
catch (...)
|
||||
{
|
||||
delete [] buf;
|
||||
throw;
|
||||
}
|
||||
delete [] buf;
|
||||
if (ifaces) freeifaddrs(ifaces);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -1212,9 +1229,6 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
|
||||
} } // namespace Poco::Net
|
||||
|
||||
|
||||
#endif // POCO_HAVE_IPv6
|
||||
|
||||
|
||||
#else
|
||||
//
|
||||
// Non-BSD Unix variants
|
||||
|
@ -107,11 +107,11 @@ const NetworkInterface& MulticastEchoServer::interfc() const
|
||||
|
||||
Poco::Net::NetworkInterface MulticastEchoServer::findInterface()
|
||||
{
|
||||
NetworkInterface::NetworkInterfaceList ifs = NetworkInterface::list();
|
||||
for (NetworkInterface::NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
|
||||
NetworkInterface::Map map = NetworkInterface::map();
|
||||
for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
if (it->supportsIPv4() && it->address().isUnicast() && !it->address().isLoopback())
|
||||
return *it;
|
||||
if (it->second.supportsIPv4() && it->second.address().isUnicast() && !it->second.isLoopback() && it->second.isPointToPoint())
|
||||
return it->second;
|
||||
}
|
||||
return NetworkInterface();
|
||||
}
|
||||
|
@ -36,6 +36,7 @@
|
||||
#include "Poco/Net/NetworkInterface.h"
|
||||
#include "Poco/Net/IPAddress.h"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
|
||||
using Poco::Net::NetworkInterface;
|
||||
@ -65,6 +66,14 @@ void NetworkInterfaceTest::testMap()
|
||||
std::cout << "DisplayName: " << it->second.displayName() << std::endl;
|
||||
std::cout << "Status: " << (it->second.isUp() ? "Up" : "Down") << std::endl;
|
||||
|
||||
NetworkInterface::MacAddress mac(it->second.macAddress());
|
||||
if (!mac.empty()) {
|
||||
std::cout << "Mac Address: (" << it->second.hwType() << ")";
|
||||
for (unsigned i = 0; i < mac.size(); ++i)
|
||||
std::cout << ((i == 0) ? ' ' : ':') << std::hex << std::setw(2) << std::setfill('0') << (unsigned)mac[i];
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
typedef NetworkInterface::AddressList List;
|
||||
const List& ipList = it->second.addressList();
|
||||
List::const_iterator ipIt = ipList.begin();
|
||||
@ -76,7 +85,7 @@ void NetworkInterfaceTest::testMap()
|
||||
std::cout << "----------" << std::endl;
|
||||
std::cout << "Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString() << std::endl;
|
||||
IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>();
|
||||
if (!addr.isWildcard()) std::cout << "Subnet: " << addr.toString() << std::endl;
|
||||
if (!addr.isWildcard()) std::cout << "Subnet: " << addr.toString() << " (/" << addr.prefixLength() << ")" << std::endl;
|
||||
addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
|
||||
if (!addr.isWildcard()) std::cout << "Broadcast: " << addr.toString() << std::endl;
|
||||
}
|
||||
@ -86,68 +95,40 @@ void NetworkInterfaceTest::testMap()
|
||||
}
|
||||
|
||||
|
||||
void NetworkInterfaceTest::testList()
|
||||
{
|
||||
NetworkInterface::List list = NetworkInterface::list();
|
||||
assert (!list.empty());
|
||||
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
{
|
||||
std::cout << "==============" << std::endl;
|
||||
|
||||
std::cout << "Index: " << it->index() << std::endl;
|
||||
std::cout << "Name: " << it->name() << std::endl;
|
||||
std::cout << "DisplayName: " << it->displayName() << std::endl;
|
||||
|
||||
typedef NetworkInterface::AddressList List;
|
||||
const List& ipList = it->addressList();
|
||||
List::const_iterator ipIt = ipList.begin();
|
||||
List::const_iterator ipEnd = ipList.end();
|
||||
for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter)
|
||||
{
|
||||
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;
|
||||
addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
|
||||
if (!addr.isWildcard()) std::cout << "Broadcast: " << ipIt->get<NetworkInterface::BROADCAST_ADDRESS>().toString() << std::endl;
|
||||
}
|
||||
|
||||
std::cout << "==============" << std::endl << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void NetworkInterfaceTest::testForName()
|
||||
{
|
||||
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list();
|
||||
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
NetworkInterface::Map map = NetworkInterface::map();
|
||||
for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
NetworkInterface ifc = NetworkInterface::forName(it->name());
|
||||
assert (ifc.name() == it->name());
|
||||
NetworkInterface ifc = NetworkInterface::forName(it->second.name());
|
||||
assert (ifc.name() == it->second.name());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void NetworkInterfaceTest::testForAddress()
|
||||
{
|
||||
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list();
|
||||
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
NetworkInterface::Map map = NetworkInterface::map();
|
||||
for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
NetworkInterface ifc = NetworkInterface::forAddress(it->address());
|
||||
assert (ifc.address() == it->address());
|
||||
// not all interfaces have IP configured
|
||||
if (it->second.addressList().empty())
|
||||
continue;
|
||||
|
||||
NetworkInterface ifc = NetworkInterface::forAddress(it->second.address());
|
||||
assert (ifc.address() == it->second.address());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void NetworkInterfaceTest::testForIndex()
|
||||
{
|
||||
#if defined(POCO_HAVE_IPv6)
|
||||
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list();
|
||||
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
NetworkInterface::Map map = NetworkInterface::map();
|
||||
for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
|
||||
{
|
||||
NetworkInterface ifc = NetworkInterface::forIndex(it->index());
|
||||
assert (ifc.index() == it->index());
|
||||
NetworkInterface ifc = NetworkInterface::forIndex(it->second.index());
|
||||
assert (ifc.index() == it->second.index());
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@ -165,7 +146,6 @@ CppUnit::Test* NetworkInterfaceTest::suite()
|
||||
{
|
||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NetworkInterfaceTest");
|
||||
|
||||
CppUnit_addTest(pSuite, NetworkInterfaceTest, testList);
|
||||
CppUnit_addTest(pSuite, NetworkInterfaceTest, testMap);
|
||||
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForName);
|
||||
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForAddress);
|
||||
|
@ -46,7 +46,6 @@ public:
|
||||
NetworkInterfaceTest(const std::string& name);
|
||||
~NetworkInterfaceTest();
|
||||
|
||||
void testList();
|
||||
void testMap();
|
||||
void testForName();
|
||||
void testForAddress();
|
||||
|
Loading…
x
Reference in New Issue
Block a user