Networkinterface, Linux portion (with some additions and Windows changes, still WIP)

This commit is contained in:
Aleksandar Fabijanic 2012-08-12 21:14:48 +00:00
Родитель b5d704bfeb
Коммит 20d5b2d2fb
7 изменённых файлов: 375 добавлений и 369 удалений

Просмотреть файл

@ -24,6 +24,8 @@ Release 1.5.0 (2012-08-??)
- fixed SF#3538785: SMTPClientSession::sendMessage() should take recipient list - fixed SF#3538785: SMTPClientSession::sendMessage() should take recipient list
- added IPAddress::prefixLength() - added IPAddress::prefixLength()
- UTF portability improvements - 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-??) Release 1.4.4 (2012-08-??)
========================== ==========================

Просмотреть файл

@ -68,11 +68,12 @@ class Net_API NetworkInterface
public: public:
typedef std::vector<NetworkInterface> List; typedef std::vector<NetworkInterface> List;
typedef List NetworkInterfaceList;//@deprecated 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 Poco::Tuple<IPAddress, IPAddress, IPAddress> AddressTuple;
typedef std::vector<AddressTuple> AddressList; typedef std::vector<AddressTuple> AddressList;
typedef AddressList::iterator AddressIterator; typedef AddressList::iterator AddressIterator;
typedef AddressList::const_iterator ConstAddressIterator; typedef AddressList::const_iterator ConstAddressIterator;
typedef std::vector<unsigned char> MacAddress;
enum AddressType enum AddressType
{ {
@ -88,7 +89,7 @@ public:
IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address IPv4_OR_IPv6 /// Return interfaces with IPv4 or IPv6 address
}; };
NetworkInterface(unsigned index = ~0); NetworkInterface(unsigned index = 0);
/// Creates a NetworkInterface representing the /// Creates a NetworkInterface representing the
/// default interface. /// default interface.
/// ///
@ -128,7 +129,7 @@ public:
/// ///
/// On other platforms this is the same as name(). /// 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. /// Returns the IP address bound to the interface.
void addAddress(const IPAddress& address); void addAddress(const IPAddress& address);
@ -140,18 +141,23 @@ 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(std::size_t 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(std::size_t 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.
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. /// Returns the IPv4 point-to-point destiation address for this network interface.
const MacAddress& macAddress() const;
unsigned mtu() const; unsigned mtu() const;
/// Returns the MTU for this interface. /// Returns the MTU for this interface.
unsigned hwType() const;
/// returns the MIB IfType of the interface.
bool supportsIP() const; bool supportsIP() const;
/// Returns true if the interface supports IP. /// Returns true if the interface supports IP.
@ -201,7 +207,7 @@ public:
/// Throws an InterfaceNotFoundException if an interface /// Throws an InterfaceNotFoundException if an interface
/// with the give address does not exist. /// 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. /// Returns the NetworkInterface for the given interface index.
/// If an index of 0 is specified, a NetworkInterface instance /// If an index of 0 is specified, a NetworkInterface instance
/// representing the default interface (empty name and /// representing the default interface (empty name and
@ -211,14 +217,6 @@ public:
/// with the given index does not exist (or IPv6 is not /// with the given index does not exist (or IPv6 is not
/// available). /// 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(); static Map map();
/// Returns a map with all network interfaces /// Returns a map with all network interfaces
/// on the system. Map is keyed by interface system /// on the system. Map is keyed by interface system
@ -229,22 +227,22 @@ public:
/// member of the pair. /// member of the pair.
protected: 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. /// 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. /// 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. /// 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. /// 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.
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. /// Determines the interface index of the interface with the given name.
NetworkInterfaceImpl& impl() { return *_pImpl; }; NetworkInterfaceImpl& impl() { return *_pImpl; };

Просмотреть файл

@ -162,7 +162,20 @@
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netdb.h> #include <netdb.h>
#if defined(POCO_OS_FAMILY_UNIX) #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> #include <net/if.h>
#endif // (POCO_OS != POCO_OS_LINUX)
#endif #endif
#if defined(sun) || defined(__APPLE__) #if defined(sun) || defined(__APPLE__)
#include <sys/sockio.h> #include <sys/sockio.h>

Просмотреть файл

@ -70,28 +70,34 @@ public:
typedef NetworkInterface::AddressTuple AddressTuple; typedef NetworkInterface::AddressTuple AddressTuple;
typedef NetworkInterface::AddressList AddressList; typedef NetworkInterface::AddressList AddressList;
NetworkInterfaceImpl(unsigned index = ~0); 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, unsigned index = 0);
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, unsigned index = 0);
unsigned 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);
const IPAddress& address(std::size_t index = 0) const; const IPAddress& address(unsigned index = 0) const;
const NetworkInterface::AddressList& addressList() const; const NetworkInterface::AddressList& addressList() const;
bool hasAddress(const IPAddress& address) const; bool hasAddress(const IPAddress& address) const;
const IPAddress& subnetMask(std::size_t index = 0) const; const IPAddress& subnetMask(unsigned index = 0) const;
const IPAddress& broadcastAddress(std::size_t index = 0) const; const IPAddress& broadcastAddress(unsigned index = 0) const;
const IPAddress& destAddress(std::size_t index = 0) const; const IPAddress& destAddress(unsigned index = 0) const;
const NetworkInterface::MacAddress& macAddress() const;
bool supportsIPv4() const; bool supportsIPv4() const;
bool supportsIPv6() const; bool supportsIPv6() const;
void setName(const std::string& name); void setName(const std::string& name);
void setDisplayName(const std::string& name); void setDisplayName(const std::string& name);
void addAddress(const IPAddress& addr); void addAddress(const IPAddress& addr);
void setMacAddress(const NetworkInterface::MacAddress& addr);
void setMacAddress(const void *addr, std::size_t len);
unsigned mtu() const; unsigned mtu() const;
unsigned ifindex() const;
unsigned hwType() const;
bool broadcast() const; bool broadcast() const;
bool loopback() const; bool loopback() const;
bool multicast() const; bool multicast() const;
@ -109,10 +115,10 @@ protected:
#endif #endif
void setUp(bool up); void setUp(bool up);
void setMtu(int mtu); void setMtu(unsigned mtu);
void setIndex(std::size_t index); void setHWType(unsigned hwType);
void setIndex(unsigned index);
void getPhyParams(); void getPhyParams();
void getIPv4Params();
void getPeerAddress(); void getPeerAddress();
private: private:
@ -127,23 +133,22 @@ private:
bool _up; bool _up;
bool _running; bool _running;
unsigned _mtu; unsigned _mtu;
unsigned _hwType;
NetworkInterface::MacAddress _macAddress;
#if defined(POCO_OS_FAMILY_WINDOWS)
friend NetworkInterface::Map NetworkInterface::map(); friend NetworkInterface::Map NetworkInterface::map();
#endif
}; };
NetworkInterfaceImpl::NetworkInterfaceImpl(unsigned index): NetworkInterfaceImpl::NetworkInterfaceImpl(unsigned index):
_index(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), _name(name),
_displayName(displayName), _displayName(displayName),
_index(index), _index(index),
@ -153,17 +158,15 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
_pointToPoint(false), _pointToPoint(false),
_up(false), _up(false),
_running(false), _running(false),
_mtu(-1) _mtu(0)
{ {
_addressList.push_back(AddressTuple(address, IPAddress(), IPAddress())); _addressList.push_back(AddressTuple(address, IPAddress(), IPAddress()));
getPhyParams(); getPhyParams();
// get remaining IPv4 params from kernel
if (address.family() == IPAddress::IPv4) getIPv4Params();
if (_pointToPoint) getPeerAddress(); 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), _name(name),
_displayName(displayName), _displayName(displayName),
_index(index), _index(index),
@ -173,7 +176,7 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
_pointToPoint(false), _pointToPoint(false),
_up(false), _up(false),
_running(false), _running(false),
_mtu(-1) _mtu(0)
{ {
_addressList.push_back(AddressTuple(address, subnetMask, broadcastAddress)); _addressList.push_back(AddressTuple(address, subnetMask, broadcastAddress));
getPhyParams(); getPhyParams();
@ -184,53 +187,15 @@ NetworkInterfaceImpl::NetworkInterfaceImpl(const std::string& name, const std::s
void NetworkInterfaceImpl::getPhyParams() void NetworkInterfaceImpl::getPhyParams()
{ {
#if !defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_VXWORKS) #if !defined(POCO_OS_FAMILY_WINDOWS) && !defined(POCO_VXWORKS)
const IPAddress::Family family = _addressList.family();
struct ifreq ifr; struct ifreq ifr;
std::strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ); std::strncpy(ifr.ifr_name, _name.c_str(), IFNAMSIZ);
DatagramSocket ds(family); DatagramSocket ds;
ds.impl()->ioctl(SIOCGIFFLAGS, &ifr); ds.impl()->ioctl(SIOCGIFFLAGS, &ifr);
setFlags(ifr.ifr_flags); setFlags(ifr.ifr_flags);
ds.impl()->ioctl(SIOCGIFMTU, &ifr); ds.impl()->ioctl(SIOCGIFMTU, &ifr);
setMtu(ifr.ifr_mtu); 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 #endif
} }
@ -251,7 +216,7 @@ void NetworkInterfaceImpl::getPeerAddress()
if (ifr.ifr_dstaddr.sa_family == AF_INET) if (ifr.ifr_dstaddr.sa_family == AF_INET)
it->set<NetworkInterface::BROADCAST_ADDRESS>(IPAddress(ifr.ifr_dstaddr)); it->set<NetworkInterface::BROADCAST_ADDRESS>(IPAddress(ifr.ifr_dstaddr));
else 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 #else
//TODO //TODO
#endif #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>(); 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()) if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::SUBNET_MASK>(); 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()) if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>(); 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()) if (!pointToPoint())
throw InvalidAccessException("Only PPP addresses have destination address."); throw InvalidAccessException("Only PPP addresses have destination address.");
else if (index < _addressList.size()) else if (index < _addressList.size())
return _addressList[index].get<NetworkInterface::BROADCAST_ADDRESS>(); 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 inline bool NetworkInterfaceImpl::broadcast() const
{ {
return _broadcast; 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; _mtu = mtu;
} }
inline void NetworkInterfaceImpl::setHWType(unsigned hwType)
{
_hwType = hwType;
}
inline void NetworkInterfaceImpl::setIndex(unsigned index) inline void NetworkInterfaceImpl::setIndex(unsigned index)
{ {
_index = index; _index = index;
@ -490,6 +473,19 @@ inline void NetworkInterfaceImpl::addAddress(const IPAddress& addr)
_addressList.push_back(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 // NetworkInterface
@ -499,7 +495,7 @@ inline void NetworkInterfaceImpl::addAddress(const IPAddress& addr)
FastMutex NetworkInterface::_mutex; FastMutex NetworkInterface::_mutex;
NetworkInterface::NetworkInterface(std::size_t index): NetworkInterface::NetworkInterface(unsigned index):
_pImpl(new NetworkInterfaceImpl(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)) _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)) _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)) _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)) _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); 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); return _pImpl->subnetMask(index);
} }
const IPAddress& NetworkInterface::broadcastAddress(std::size_t index) const const IPAddress& NetworkInterface::broadcastAddress(unsigned index) const
{ {
return _pImpl->broadcastAddress(index); 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); return _pImpl->destAddress(index);
} }
@ -622,6 +624,10 @@ unsigned NetworkInterface::mtu() const
return _pImpl->mtu(); return _pImpl->mtu();
} }
unsigned NetworkInterface::hwType() const
{
return _pImpl->hwType();
}
bool NetworkInterface::supportsIP() const bool NetworkInterface::supportsIP() const
{ {
@ -679,11 +685,14 @@ bool NetworkInterface::isUp() const
NetworkInterface NetworkInterface::forName(const std::string& name, bool requireIPv6) NetworkInterface NetworkInterface::forName(const std::string& name, bool requireIPv6)
{ {
NetworkInterfaceList ifs = list(); Map map = NetworkInterface::map();
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it) Map::const_iterator it = map.begin();
Map::const_iterator end = map.end();
for (; it != end; ++it)
{ {
if (it->name() == name && ((requireIPv6 && it->supportsIPv6()) || !requireIPv6)) if (it->second.name() == name && ((requireIPv6 && it->second.supportsIPv6()) || !requireIPv6))
return *it; return it->second;
} }
throw InterfaceNotFoundException(name); 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) NetworkInterface NetworkInterface::forName(const std::string& name, IPVersion ipVersion)
{ {
NetworkInterfaceList ifs = list(); Map map = NetworkInterface::map();
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it) 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()) if (ipVersion == IPv4_ONLY && it->second.supportsIPv4())
return *it; return it->second;
else if (ipVersion == IPv6_ONLY && it->supportsIPv6()) else if (ipVersion == IPv6_ONLY && it->second.supportsIPv6())
return *it; return it->second;
else if (ipVersion == IPv4_OR_IPv6) else if (ipVersion == IPv4_OR_IPv6)
return *it; return it->second;
} }
} }
throw InterfaceNotFoundException(name); throw InterfaceNotFoundException(name);
@ -710,66 +722,67 @@ NetworkInterface NetworkInterface::forName(const std::string& name, IPVersion ip
NetworkInterface NetworkInterface::forAddress(const IPAddress& addr) NetworkInterface NetworkInterface::forAddress(const IPAddress& addr)
{ {
NetworkInterfaceList ifs = list(); Map map = NetworkInterface::map();
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it) Map::const_iterator it = map.begin();
Map::const_iterator end = map.end();
for (; it != end; ++it)
{ {
if (it->address() == addr) const unsigned count = it->second.addressList().size();
return *it; for (unsigned i = 0; i < count; ++i)
{
if (it->second.address(i) == addr)
return it->second;
}
} }
throw InterfaceNotFoundException(addr.toString()); throw InterfaceNotFoundException(addr.toString());
} }
NetworkInterface NetworkInterface::forIndex(int i) NetworkInterface NetworkInterface::forIndex(unsigned i)
{ {
if (i != 0) if (i != 0)
{ {
NetworkInterfaceList ifs = list(); Map map = NetworkInterface::map();
for (NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it)
{ Map::const_iterator it = map.find(i);
if (it->index() == i) if (it != map.end())
return *it; return it->second;
}
throw InterfaceNotFoundException("#" + NumberFormatter::format(i));
} }
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; switch (arphrd) {
Map m = map(); case ARPHRD_ETHER:
return 6; // IF_TYPE_ETHERNET_CSMACD
for (NetworkInterface::Map::const_iterator it = m.begin(); it != m.end(); ++it) case ARPHRD_IEEE802:
{ return 9; // IF_TYPE_ISO88025_TOKENRING
int index = it->second.index(); case ARPHRD_DLCI:
std::string name = it->second.name(); return 32; // IF_TYPE_FRAMERELAY
std::string displayName = it->second.displayName(); case ARPHRD_PPP:
return 512; // IF_TYPE_PPP
typedef NetworkInterface::AddressList List; case ARPHRD_LOOPBACK:
const List& ipList = it->second.addressList(); return 24; // IF_TYPE_SOFTWARE_LOOPBACK
List::const_iterator ipIt = ipList.begin(); case ARPHRD_ATM:
List::const_iterator ipEnd = ipList.end(); return 37; // IF_TYPE_ATM
for (int counter = 0; ipIt != ipEnd; ++ipIt, ++counter) case ARPHRD_IEEE80211:
{ return 71; // IF_TYPE_IEEE80211
IPAddress addr = ipIt->get<NetworkInterface::IP_ADDRESS>(); case ARPHRD_TUNNEL:
IPAddress mask = ipIt->get<NetworkInterface::SUBNET_MASK>(); case ARPHRD_TUNNEL6:
NetworkInterface ni; return 131; // IF_TYPE_TUNNEL
if (mask.isWildcard()) case ARPHRD_IEEE1394:
ni = NetworkInterface(name, displayName, addr, index); return 144; // IF_TYPE_IEEE1394
else default:
{ return 1; // IF_TYPE_OTHER
IPAddress broadcast = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
ni = NetworkInterface(name, displayName, addr, mask, broadcast, index);
}
list.push_back(ni);
}
} }
return list;
} }
#endif
} } // namespace Poco::Net } } // namespace Poco::Net
@ -854,6 +867,9 @@ NetworkInterface::Map NetworkInterface::map()
ULONG outBufLen = 16384; ULONG outBufLen = 16384;
Poco::Buffer<UCHAR> memory(outBufLen); Poco::Buffer<UCHAR> memory(outBufLen);
ULONG flags = (GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_INCLUDE_PREFIX); 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) #if defined(POCO_HAVE_IPv6)
const unsigned family = AF_UNSPEC; //IPv4 and IPv6 const unsigned family = AF_UNSPEC; //IPv4 and IPv6
#else #else
@ -880,8 +896,22 @@ NetworkInterface::Map NetworkInterface::map()
IPAddress address; IPAddress address;
IPAddress subnetMask; IPAddress subnetMask;
IPAddress broadcastAddress; 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) #if defined(POCO_HAVE_IPv6)
poco_assert (pAddress->Ipv6IfIndex == pAddress->IfIndex); 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;
@ -905,7 +935,6 @@ NetworkInterface::Map NetworkInterface::map()
UINT8 prefixLength = pUniAddr->OnLinkPrefixLength; 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);
switch (family) switch (family)
{ {
case AF_INET: 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) // 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); broadcastAddress = getBroadcastAddress(pAddress->FirstPrefix, address);
subnetMask = prefixLength ? IPAddress(prefixLength, IPAddress::IPv4) : IPAddress(); subnetMask = prefixLength ? IPAddress(prefixLength, IPAddress::IPv4) : IPAddress();
if (ifIt == result.end()) // network interface not cached yet, insert if (hasBroadcast)
{ ifIt->second.addAddress(address, subnetMask, broadcastAddress);
if (hasBroadcast) else
ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, subnetMask, broadcastAddress, ifIndex))).first; ifIt->second.addAddress(address);
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);
}
} break; } break;
#if defined(POCO_HAVE_IPv6) #if defined(POCO_HAVE_IPv6)
case AF_INET6: case AF_INET6:
{ {
if (ifIt == result.end()) ifIt = result.insert(Map::value_type(ifIndex, NetworkInterface(name, displayName, address, ifIndex))).first; ifIt->second.addAddress(address);
else
ifIt->second.addAddress(address);
} break; } break;
#endif #endif
} // switch family } // 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 addresses
} // for adapters } // for adapters
return result; return result;
@ -1012,81 +1026,22 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <ifaddrs.h> #include <ifaddrs.h>
#include <net/if.h>
#include <net/if_dl.h> #include <net/if_dl.h>
#include <net/if_types.h>
namespace Poco { namespace Poco {
namespace Net { namespace Net {
NetworkInterface::NetworkInterfaceList NetworkInterface::list() NetworkInterface::Map NetworkInterface::map()
{ {
FastMutex::ScopedLock lock(_mutex); FastMutex::ScopedLock lock(_mutex);
NetworkInterfaceList result; Map result;
unsigned ifIndex = 0;
struct ifaddrs* ifaphead; NetworkInterface intf;
int rc = getifaddrs(&ifaphead); Map::iterator ifIt;
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;
struct ifaddrs* ifaces = 0; struct ifaddrs* ifaces = 0;
struct ifaddrs* currIface = 0; struct ifaddrs* currIface = 0;
@ -1098,30 +1053,62 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
{ {
for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next) for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next)
{ {
IPAddress addr; IPAddress address, subnetMask, broadcastAddress;
bool haveAddr = false; struct sockaddr_dl* sdl;
int ifIndex(-1);
if (currIface->ifa_addr) switch (currIface->ifa_addr->sa_family)
{ {
switch (currIface->ifa_addr->sa_family) case AF_LINK:
{ sdl = (struct sockaddr_dl*)currIface->ifa_addr;
case AF_INET6: ifIndex = sdl->sdl_index;
ifIndex = if_nametoindex(currIface->ifa_name); intf = NetworkInterface(ifIndex);
addr = IPAddress(&reinterpret_cast<const struct sockaddr_in6*>(currIface->ifa_addr)->sin6_addr, sizeof(struct in6_addr), ifIndex); intf.impl().setName(currIface->ifa_name);
haveAddr = true; intf.impl().setDisplayName(currIface->ifa_name);
break; intf.impl().getPhyParams();
case AF_INET:
addr = IPAddress(*(currIface->ifa_addr)); intf.impl().setMacAddress(LLADDR(sdl), sdl->sdl_alen);
haveAddr = true; intf.impl().setHWType(sdl->sdl_type);
break;
default: ifIt = result.insert(Map::value_type(ifIndex, intf)).first;
break; break;
} case AF_INET:
} // need to use Map to search by name, i.e. result.find(name)
if (haveAddr) ifIndex = if_nametoindex(currIface->ifa_name);
{ ifIt = result.find(ifIndex);
std::string name(currIface->ifa_name);
result.push_back(NetworkInterface(name, name, addr, 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 } } // 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 Poco {
namespace Net { namespace Net {
NetworkInterface::NetworkInterfaceList NetworkInterface::list() NetworkInterface::Map NetworkInterface::map()
{ {
FastMutex::ScopedLock lock(_mutex); FastMutex::ScopedLock lock(_mutex);
NetworkInterfaceList result; Map result;
DatagramSocket socket; unsigned ifIndex = 0;
// the following code is loosely based NetworkInterface intf;
// on W. Richard Stevens, UNIX Network Programming, pp 434ff. Map::iterator ifIt;
int lastlen = 0;
int len = 100*sizeof(struct ifreq); struct ifaddrs* ifaces = 0;
char* buf = 0; struct ifaddrs* currIface = 0;
try
if (getifaddrs(&ifaces) < 0)
throw NetException("cannot get network adapter list");
try
{ {
struct ifconf ifc; for (currIface = ifaces; currIface != 0; currIface = currIface->ifa_next)
for (;;)
{ {
buf = new char[len]; IPAddress address, subnetMask, broadcastAddress;
ifc.ifc_len = len; struct sockaddr_ll* sll;
ifc.ifc_buf = buf;
if (::ioctl(socket.impl()->sockfd(), SIOCGIFCONF, &ifc) < 0) switch (currIface->ifa_addr->sa_family)
{
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)
{ {
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: case AF_INET:
addr = IPAddress(ifr->ifr_addr); // need to use Map to search by name, i.e. result.find(name)
haveAddr = true; 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; 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: 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; return result;
} }
@ -1212,9 +1229,6 @@ NetworkInterface::NetworkInterfaceList NetworkInterface::list()
} } // namespace Poco::Net } } // namespace Poco::Net
#endif // POCO_HAVE_IPv6
#else #else
// //
// Non-BSD Unix variants // Non-BSD Unix variants

Просмотреть файл

@ -107,11 +107,11 @@ const NetworkInterface& MulticastEchoServer::interfc() const
Poco::Net::NetworkInterface MulticastEchoServer::findInterface() Poco::Net::NetworkInterface MulticastEchoServer::findInterface()
{ {
NetworkInterface::NetworkInterfaceList ifs = NetworkInterface::list(); NetworkInterface::Map map = NetworkInterface::map();
for (NetworkInterface::NetworkInterfaceList::const_iterator it = ifs.begin(); it != ifs.end(); ++it) for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
{ {
if (it->supportsIPv4() && it->address().isUnicast() && !it->address().isLoopback()) if (it->second.supportsIPv4() && it->second.address().isUnicast() && !it->second.isLoopback() && it->second.isPointToPoint())
return *it; return it->second;
} }
return NetworkInterface(); return NetworkInterface();
} }

Просмотреть файл

@ -36,6 +36,7 @@
#include "Poco/Net/NetworkInterface.h" #include "Poco/Net/NetworkInterface.h"
#include "Poco/Net/IPAddress.h" #include "Poco/Net/IPAddress.h"
#include <iostream> #include <iostream>
#include <iomanip>
using Poco::Net::NetworkInterface; using Poco::Net::NetworkInterface;
@ -65,6 +66,14 @@ void NetworkInterfaceTest::testMap()
std::cout << "DisplayName: " << it->second.displayName() << std::endl; std::cout << "DisplayName: " << it->second.displayName() << std::endl;
std::cout << "Status: " << (it->second.isUp() ? "Up" : "Down") << 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; typedef NetworkInterface::AddressList List;
const List& ipList = it->second.addressList(); const List& ipList = it->second.addressList();
List::const_iterator ipIt = ipList.begin(); List::const_iterator ipIt = ipList.begin();
@ -76,7 +85,7 @@ void NetworkInterfaceTest::testMap()
std::cout << "----------" << std::endl; std::cout << "----------" << std::endl;
std::cout << "Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString() << std::endl; std::cout << "Address: " << ipIt->get<NetworkInterface::IP_ADDRESS>().toString() << std::endl;
IPAddress addr = ipIt->get<NetworkInterface::SUBNET_MASK>(); 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>(); addr = ipIt->get<NetworkInterface::BROADCAST_ADDRESS>();
if (!addr.isWildcard()) std::cout << "Broadcast: " << addr.toString() << std::endl; 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() void NetworkInterfaceTest::testForName()
{ {
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list(); NetworkInterface::Map map = NetworkInterface::map();
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it) for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
{ {
NetworkInterface ifc = NetworkInterface::forName(it->name()); NetworkInterface ifc = NetworkInterface::forName(it->second.name());
assert (ifc.name() == it->name()); assert (ifc.name() == it->second.name());
} }
} }
void NetworkInterfaceTest::testForAddress() void NetworkInterfaceTest::testForAddress()
{ {
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list(); NetworkInterface::Map map = NetworkInterface::map();
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it) for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
{ {
NetworkInterface ifc = NetworkInterface::forAddress(it->address()); // not all interfaces have IP configured
assert (ifc.address() == it->address()); if (it->second.addressList().empty())
continue;
NetworkInterface ifc = NetworkInterface::forAddress(it->second.address());
assert (ifc.address() == it->second.address());
} }
} }
void NetworkInterfaceTest::testForIndex() void NetworkInterfaceTest::testForIndex()
{ {
#if defined(POCO_HAVE_IPv6) NetworkInterface::Map map = NetworkInterface::map();
NetworkInterface::NetworkInterfaceList list = NetworkInterface::list(); for (NetworkInterface::Map::const_iterator it = map.begin(); it != map.end(); ++it)
for (NetworkInterface::NetworkInterfaceList::const_iterator it = list.begin(); it != list.end(); ++it)
{ {
NetworkInterface ifc = NetworkInterface::forIndex(it->index()); NetworkInterface ifc = NetworkInterface::forIndex(it->second.index());
assert (ifc.index() == it->index()); assert (ifc.index() == it->second.index());
} }
#endif
} }
@ -165,7 +146,6 @@ CppUnit::Test* NetworkInterfaceTest::suite()
{ {
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NetworkInterfaceTest"); CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NetworkInterfaceTest");
CppUnit_addTest(pSuite, NetworkInterfaceTest, testList);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testMap); CppUnit_addTest(pSuite, NetworkInterfaceTest, testMap);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForName); CppUnit_addTest(pSuite, NetworkInterfaceTest, testForName);
CppUnit_addTest(pSuite, NetworkInterfaceTest, testForAddress); CppUnit_addTest(pSuite, NetworkInterfaceTest, testForAddress);

Просмотреть файл

@ -46,7 +46,6 @@ public:
NetworkInterfaceTest(const std::string& name); NetworkInterfaceTest(const std::string& name);
~NetworkInterfaceTest(); ~NetworkInterfaceTest();
void testList();
void testMap(); void testMap();
void testForName(); void testForName();
void testForAddress(); void testForAddress();