Implement MACAddress::findAllAddresses as in MacOSX.

This commit is contained in:
Alex Dupre
2013-06-29 10:04:27 +02:00
parent a0baaeafd1
commit 1832463010
2 changed files with 17 additions and 21 deletions

View File

@@ -188,6 +188,8 @@
#if BEAST_BSD #if BEAST_BSD
#include <dirent.h> #include <dirent.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
#include <kvm.h> #include <kvm.h>
#include <langinfo.h> #include <langinfo.h>
#include <sys/cdefs.h> #include <sys/cdefs.h>

View File

@@ -23,34 +23,28 @@
void MACAddress::findAllAddresses (Array<MACAddress>& result) void MACAddress::findAllAddresses (Array<MACAddress>& result)
{ {
#if 1 ifaddrs* addrs = nullptr;
bassertfalse; // VFALCO TODO Implement for FreeBSD
#else if (getifaddrs (&addrs) == 0)
const int s = socket (AF_INET, SOCK_DGRAM, 0);
if (s != -1)
{ {
char buf [1024]; for (const ifaddrs* cursor = addrs; cursor != nullptr; cursor = cursor->ifa_next)
struct ifconf ifc;
ifc.ifc_len = sizeof (buf);
ifc.ifc_buf = buf;
ioctl (s, SIOCGIFCONF, &ifc);
for (unsigned int i = 0; i < ifc.ifc_len / sizeof (struct ifreq); ++i)
{ {
struct ifreq ifr; sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr;
strcpy (ifr.ifr_name, ifc.ifc_req[i].ifr_name); if (sto->ss_family == AF_LINK)
if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0
&& (ifr.ifr_flags & IFF_LOOPBACK) == 0
&& ioctl (s, SIOCGIFHWADDR, &ifr) == 0)
{ {
result.addIfNotAlreadyThere (MACAddress ((const uint8*) ifr.ifr_hwaddr.sa_data)); const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr;
#ifndef IFT_ETHER
#define IFT_ETHER 6
#endif
if (sadd->sdl_type == IFT_ETHER)
result.addIfNotAlreadyThere (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen));
} }
} }
close (s); freeifaddrs (addrs);
} }
#endif
} }