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
const int s = socket (AF_INET, SOCK_DGRAM, 0);
if (s != -1)
{
char buf [1024];
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) if (getifaddrs (&addrs) == 0)
{ {
struct ifreq ifr; for (const ifaddrs* cursor = addrs; cursor != nullptr; cursor = cursor->ifa_next)
strcpy (ifr.ifr_name, ifc.ifc_req[i].ifr_name); {
sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr;
if (sto->ss_family == AF_LINK)
{
const sockaddr_dl* const sadd = (const sockaddr_dl*) cursor->ifa_addr;
if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0 #ifndef IFT_ETHER
&& (ifr.ifr_flags & IFF_LOOPBACK) == 0 #define IFT_ETHER 6
&& ioctl (s, SIOCGIFHWADDR, &ifr) == 0) #endif
{
result.addIfNotAlreadyThere (MACAddress ((const uint8*) ifr.ifr_hwaddr.sa_data)); if (sadd->sdl_type == IFT_ETHER)
result.addIfNotAlreadyThere (MACAddress (((const uint8*) sadd->sdl_data) + sadd->sdl_nlen));
} }
} }
close (s); freeifaddrs (addrs);
} }
#endif
} }