--- portmap_4.orig/from_local.c 2005-02-21 19:45:02.584530600 +0100 +++ portmap_4/from_local.c 2005-02-21 19:52:32.067198848 +0100 @@ -54,6 +54,7 @@ static char sccsid[] = "@(#) from_local. #include #include #include +#include #ifndef TRUE #define TRUE 1 @@ -101,57 +102,30 @@ static int grow_addrs() int find_local() { - struct ifconf ifc; - struct ifreq ifreq; - struct ifreq *ifr; - struct ifreq *the_end; - int sock; - char buf[BUFSIZ]; + struct ifaddrs *if_list, *ifr; - /* - * Get list of network interfaces. We use a huge buffer to allow for the - * presence of non-IP interfaces. - */ + /* Get list of network interfaces. */ - if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) < 0) { - perror("socket"); - return (0); - } - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if (ioctl(sock, SIOCGIFCONF, (char *) &ifc) < 0) { - perror("SIOCGIFCONF"); - (void) close(sock); + if (getifaddrs(&if_list) < 0) { + perror("getifaddrs"); return (0); } + /* Get IP address of each active IP network interface. */ - the_end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); num_local = 0; - for (ifr = ifc.ifc_req; ifr < the_end; ifr++) { - if (ifr->ifr_addr.sa_family == AF_INET) { /* IP net interface */ - ifreq = *ifr; - if (ioctl(sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) { - perror("SIOCGIFFLAGS"); - } else if (ifreq.ifr_flags & IFF_UP) { /* active interface */ - if (ioctl(sock, SIOCGIFADDR, (char *) &ifreq) < 0) { - perror("SIOCGIFADDR"); - } else { - if (num_local >= num_addrs) - if (grow_addrs() == 0) - break; - addrs[num_local++] = ((struct sockaddr_in *) - & ifreq.ifr_addr)->sin_addr; - } + for (ifr = if_list; ifr != NULL; ifr = ifr->ifa_next) { + if (ifr->ifa_addr->sa_family == AF_INET) { /* IP net interface */ + if (ifr->ifa_flags & IFF_UP) { /* active interface */ + if (num_local >= num_addrs) + if (grow_addrs() == 0) + break; + addrs[num_local++] = ((struct sockaddr_in *) + ifr->ifa_addr)->sin_addr; } } - /* Support for variable-length addresses. */ -#ifdef HAS_SA_LEN - ifr = (struct ifreq *) ((caddr_t) ifr - + ifr->ifr_addr.sa_len - sizeof(struct sockaddr)); -#endif } - (void) close(sock); + freeifaddrs(if_list); return (num_local); }