get network interface type from /sys/class/net as well

This commit is contained in:
Guenter Obiltschnig
2017-02-13 18:51:07 +01:00
parent 91211c7cd1
commit 84feb7f35a

View File

@@ -23,6 +23,8 @@
#include "Poco/Net/DatagramSocket.h" #include "Poco/Net/DatagramSocket.h"
#include "Poco/Net/NetException.h" #include "Poco/Net/NetException.h"
#include "Poco/NumberFormatter.h" #include "Poco/NumberFormatter.h"
#include "Poco/NumberParser.h"
#include "Poco/StringTokenizer.h"
#include "Poco/RefCountedObject.h" #include "Poco/RefCountedObject.h"
#include "Poco/Format.h" #include "Poco/Format.h"
#if defined(POCO_OS_FAMILY_WINDOWS) #if defined(POCO_OS_FAMILY_WINDOWS)
@@ -35,6 +37,7 @@
#include <ipifcons.h> #include <ipifcons.h>
#endif #endif
#include <cstring> #include <cstring>
#include <fstream>
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
@@ -1560,26 +1563,37 @@ void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
impl.setMACAddress(sdl->sll_addr, sdl->sll_halen); impl.setMACAddress(sdl->sll_addr, sdl->sll_halen);
impl.setType(fromNative(sdl->sll_hatype)); impl.setType(fromNative(sdl->sll_hatype));
#else #else
// TODO: fix for non-Ethernet interfaces std::string ifPath("/sys/class/net/");
std::string ifpath("/sys/class/net/"); ifPath += iface->ifa_name;
ifpath += iface->ifa_name;
ifpath += "/address"; std::string addrPath(ifPath);
int fd = open(ifpath.c_str(), O_RDONLY); addrPath += "/address";
if (fd >= 0)
std::ifstream addrStream(addrPath.c_str());
if (addrStream.good())
{ {
char buffer[18]; std::string addr;
int n = read(fd, buffer, 17); std::getline(addrStream, addr);
close(fd); Poco::StringTokenizer tok(addr, ":");
if (n == 17) std::vector<unsigned char> mac;
for (Poco::StringTokenizer::Iterator it = tok.begin(); it != tok.end(); ++it)
{ {
unsigned char mac[6]; mac.push_back(static_cast<unsigned char>(Poco::NumberParser::parseHex(*it)));
buffer[n] = 0;
if (std::sscanf(buffer, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) == 6)
{
impl.setMACAddress(mac, 6);
}
} }
} impl.setMACAddress(&mac[0], mac.size());
addrStream.close();
}
std::string typePath(ifPath);
typePath += "/type";
std::ifstream typeStream(typePath.c_str());
if (typeStream.good())
{
int type;
typeStream >> type;
impl.setType(fromNative(type));
typeStream.close();
}
#endif // POCO_NO_LINUX_IF_PACKET_H #endif // POCO_NO_LINUX_IF_PACKET_H
} }