- #define MAX_IFACES 400
- int sock;
- struct ifreq iflist[MAX_IFACES];
- struct ifconf ifconf;
- struct ifreq *ifr, *ifr_end;
- struct sockaddr_dl *dl, *sdl = NULL;
-
- /* Get socket */
- if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
- err(1, "socket");
-
- /* Get interface list */
- ifconf.ifc_req = iflist;
- ifconf.ifc_len = sizeof(iflist);
- if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0)
- err(1, "ioctl(SIOCGIFCONF)");
- close(sock);
-
- /* Look for this interface in the list */
- for (ifr = ifconf.ifc_req,
- ifr_end = (struct ifreq *)
- (ifconf.ifc_buf + ifconf.ifc_len);
- ifr < ifr_end;
- ifr = (struct ifreq *) ((char *) &ifr->ifr_addr
- + ifr->ifr_addr.sa_len)) {
- dl = (struct sockaddr_dl *)&ifr->ifr_addr;
- if (ifr->ifr_addr.sa_family == AF_LINK
- && (ifr->ifr_flags & IFF_POINTOPOINT)
- && !strncmp(s, dl->sdl_data, dl->sdl_nlen)
- && s[dl->sdl_nlen] == 0) {
- sdl = dl;
- break;
- }
- }
+ struct ifaddrs *ifap, *ifa;
+ struct sockaddr_dl *sdl = NULL;
+
+ if (getifaddrs(&ifap))
+ err(1, "getifaddrs");
+
+ for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr->sa_family != AF_LINK)
+ continue;
+
+ if (strcmp(s, ifa->ifa_name))
+ continue;