@@ -22,19 +22,10 @@ # include #endif #ifdef HAVE_NET_IF_H # include #endif -#ifdef HAVE_NET_IF_TYPES_H -# include -#endif -#ifdef HAVE_NET_IF_DL_H -# include -#endif -#ifdef HAVE_NETPACKET_PACKET_H -# include -#endif #import "OFSystemInfo.h" #import "OFSystemInfo+NetworkInterfaces.h" #import "OFArray.h" #import "OFData.h" @@ -299,10 +290,51 @@ # else return false; # endif } #endif + +static bool +queryNetworkInterfaceHardwareAddress(OFMutableDictionary *ret) +{ +#if defined(HAVE_IOCTL) && defined(HAVE_NET_IF_H) && defined(SIOCGIFHWADDR) + OFStringEncoding encoding = [OFLocale encoding]; + int sock = socket(AF_INET, SOCK_DGRAM, 0); + + if (sock < 0) + return false; + + for (OFString *name in ret) { + size_t nameLength = [name cStringLengthWithEncoding: encoding]; + struct ifreq ifr; + OFData *hardwareAddress; + + if (nameLength > IFNAMSIZ) + continue; + + memset(&ifr, 0, sizeof(ifr)); + memcpy(&ifr.ifr_name, [name cStringWithEncoding: encoding], + nameLength); + + if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0) + continue; + + if (ifr.ifr_hwaddr.sa_family != 1) + continue; + + hardwareAddress = [OFData dataWithItems: ifr.ifr_hwaddr.sa_data + count: 6]; + [[ret objectForKey: name] + setObject: hardwareAddress + forKey: OFNetworkInterfaceHardwareAddress]; + } + + return true; +#else + return false; +#endif +} + (OFDictionary OF_GENERIC(OFString *, OFNetworkInterface) *)networkInterfaces { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *ret = [OFMutableDictionary dictionary]; @@ -319,10 +351,11 @@ success |= queryNetworkInterfaceIPXAddresses(ret); #endif #ifdef OF_HAVE_APPLETALK success |= queryNetworkInterfaceAppleTalkAddresses(ret); #endif + success |= queryNetworkInterfaceHardwareAddress(ret); if (!success) { objc_autoreleasePoolPop(pool); return nil; }