added workaround for Linux systems that don't have <linux/if_packet.h>

This commit is contained in:
Guenter Obiltschnig
2017-02-13 18:29:54 +01:00
parent e84788f085
commit 91211c7cd1

View File

@@ -1516,7 +1516,9 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
#include <ifaddrs.h> #include <ifaddrs.h>
#endif #endif
#include <net/if.h> #include <net/if.h>
#ifndef POCO_NO_LINUX_IF_PACKET_H
#include <linux/if_packet.h> #include <linux/if_packet.h>
#endif
#include <net/if_arp.h> #include <net/if_arp.h>
#include <iostream> #include <iostream>
@@ -1548,14 +1550,37 @@ static NetworkInterface::Type fromNative(unsigned arphrd)
void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl) void setInterfaceParams(struct ifaddrs* iface, NetworkInterfaceImpl& impl)
{ {
struct sockaddr_ll* sdl = (struct sockaddr_ll*) iface->ifa_addr;
impl.setName(iface->ifa_name); impl.setName(iface->ifa_name);
impl.setDisplayName(iface->ifa_name); impl.setDisplayName(iface->ifa_name);
impl.setAdapterName(iface->ifa_name); impl.setAdapterName(iface->ifa_name);
impl.setPhyParams(); impl.setPhyParams();
#ifndef POCO_NO_LINUX_IF_PACKET_H
struct sockaddr_ll* sdl = (struct sockaddr_ll*) iface->ifa_addr;
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
// TODO: fix for non-Ethernet interfaces
std::string ifpath("/sys/class/net/");
ifpath += iface->ifa_name;
ifpath += "/address";
int fd = open(ifpath.c_str(), O_RDONLY);
if (fd >= 0)
{
char buffer[18];
int n = read(fd, buffer, 17);
close(fd);
if (n == 17)
{
unsigned char mac[6];
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);
}
}
}
#endif // POCO_NO_LINUX_IF_PACKET_H
} }
#endif #endif
@@ -1588,6 +1613,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
unsigned family = iface->ifa_addr->sa_family; unsigned family = iface->ifa_addr->sa_family;
switch (family) switch (family)
{ {
#ifndef POCO_NO_LINUX_IF_PACKET_H
case AF_PACKET: case AF_PACKET:
{ {
struct sockaddr_ll* sll = (struct sockaddr_ll*)iface->ifa_addr; struct sockaddr_ll* sll = (struct sockaddr_ll*)iface->ifa_addr;
@@ -1600,6 +1626,7 @@ NetworkInterface::Map NetworkInterface::map(bool ipOnly, bool upOnly)
break; break;
} }
#endif // POCO_NO_LINUX_IF_PACKET_H
case AF_INET: case AF_INET:
ifIndex = if_nametoindex(iface->ifa_name); ifIndex = if_nametoindex(iface->ifa_name);
ifIt = result.find(ifIndex); ifIt = result.find(ifIndex);