From 18324630105a37dfa66076fac8d2d1de0a5d32d2 Mon Sep 17 00:00:00 2001 From: Alex Dupre Date: Sat, 29 Jun 2013 10:04:27 +0200 Subject: [PATCH] Implement MACAddress::findAllAddresses as in MacOSX. --- .../native/beast_BasicNativeHeaders.h | 2 ++ .../beast_core/native/beast_bsd_Network.cpp | 36 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/modules/beast_core/native/beast_BasicNativeHeaders.h b/modules/beast_core/native/beast_BasicNativeHeaders.h index 21249d82f..7a39aac76 100644 --- a/modules/beast_core/native/beast_BasicNativeHeaders.h +++ b/modules/beast_core/native/beast_BasicNativeHeaders.h @@ -188,6 +188,8 @@ #if BEAST_BSD #include + #include + #include #include #include #include diff --git a/modules/beast_core/native/beast_bsd_Network.cpp b/modules/beast_core/native/beast_bsd_Network.cpp index 9cc852ca9..18620f679 100644 --- a/modules/beast_core/native/beast_bsd_Network.cpp +++ b/modules/beast_core/native/beast_bsd_Network.cpp @@ -23,34 +23,28 @@ void MACAddress::findAllAddresses (Array& result) { -#if 1 - bassertfalse; // VFALCO TODO Implement for FreeBSD -#else - const int s = socket (AF_INET, SOCK_DGRAM, 0); - if (s != -1) + ifaddrs* addrs = nullptr; + + if (getifaddrs (&addrs) == 0) { - 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) + for (const ifaddrs* cursor = addrs; cursor != nullptr; cursor = cursor->ifa_next) { - struct ifreq ifr; - strcpy (ifr.ifr_name, ifc.ifc_req[i].ifr_name); - - if (ioctl (s, SIOCGIFFLAGS, &ifr) == 0 - && (ifr.ifr_flags & IFF_LOOPBACK) == 0 - && ioctl (s, SIOCGIFHWADDR, &ifr) == 0) + sockaddr_storage* sto = (sockaddr_storage*) cursor->ifa_addr; + if (sto->ss_family == AF_LINK) { - 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 }