From 412c954afacf0e5f62ff66ae870df18444b4a799 Mon Sep 17 00:00:00 2001 From: Ulrich Drepper Date: Sun, 26 Sep 2004 11:11:28 +0000 Subject: [BZ #381] Update. * sunrpc/clnt_udp.c (is_network_up): Use getifaddrs instead of ioctl. * sunrpc/get_myaddr.c (get_myaddress): Likewise. * sunrpc/pmap_clnt.c (__get_myaddress): Likewise. * sunrpc/pmap_rmt.c (getbroadcastnets): Likewise. Change interface to avoid buffer overrun and remove now useless parameters. (clnt_broadcast): Adjust caller. [BZ #381]. --- sunrpc/clnt_udp.c | 35 +++++++++++++---------------- sunrpc/get_myaddr.c | 53 +++++++++++++++++++------------------------ sunrpc/pmap_clnt.c | 56 +++++++++++++++++++-------------------------- sunrpc/pmap_rmt.c | 65 +++++++++++++++++------------------------------------ 4 files changed, 83 insertions(+), 126 deletions(-) (limited to 'sunrpc') diff --git a/sunrpc/clnt_udp.c b/sunrpc/clnt_udp.c index f906173363..1836ff3433 100644 --- a/sunrpc/clnt_udp.c +++ b/sunrpc/clnt_udp.c @@ -50,6 +50,7 @@ static char sccsid[] = "@(#)clnt_udp.c 1.39 87/08/11 Copyr 1984 Sun Micro"; #include #include #include +#include #ifdef USE_IN_LIBIO # include #endif @@ -234,28 +235,24 @@ INTDEF (clntudp_create) static int is_network_up (int sock) { - struct ifconf ifc; - char buf[UDPMSGSIZE]; - struct ifreq ifreq, *ifr; - int n; - - ifc.ifc_len = sizeof (buf); - ifc.ifc_buf = buf; - if (__ioctl(sock, SIOCGIFCONF, (char *) &ifc) == 0) + struct ifaddrs *ifa; + + if (getifaddrs (&ifa) != 0) + return 0; + + struct ifaddrs *run = ifa; + while (run != NULL) { - ifr = ifc.ifc_req; - for (n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) - { - ifreq = *ifr; - if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) - break; + if ((run->ifa_flags & IFF_UP) != 0 + && run->ifa_addr->sa_family == AF_INET) + break; - if ((ifreq.ifr_flags & IFF_UP) - && ifr->ifr_addr.sa_family == AF_INET) - return 1; - } + run = run->ifa_next; } - return 0; + + freeifaddrs (ifa); + + return run != NULL; } static enum clnt_stat diff --git a/sunrpc/get_myaddr.c b/sunrpc/get_myaddr.c index 7a4bb8cb6f..ee822970f9 100644 --- a/sunrpc/get_myaddr.c +++ b/sunrpc/get_myaddr.c @@ -46,6 +46,7 @@ static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro"; #include #include #include +#include #include /* Order of following two #includes reversed by roland@gnu */ #include @@ -60,50 +61,42 @@ static char sccsid[] = "@(#)get_myaddress.c 1.4 87/08/11 Copyr 1984 Sun Micro"; void get_myaddress (struct sockaddr_in *addr) { - int s; - char buf[BUFSIZ]; - struct ifconf ifc; - struct ifreq ifreq, *ifr; - int len, loopback = 0; + struct ifaddrs *ifa; - if ((s = __socket (AF_INET, SOCK_DGRAM, 0)) < 0) + if (getifaddrs (&ifa) == 0) { - perror ("get_myaddress: socket"); - exit (1); - } - ifc.ifc_len = sizeof (buf); - ifc.ifc_buf = buf; - if (__ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0) - { - perror (_("get_myaddress: ioctl (get interface configuration)")); + perror ("get_myaddress: getifaddrs"); exit (1); } + int loopback = 0; + struct ifaddrs *run; + again: - ifr = ifc.ifc_req; - for (len = ifc.ifc_len; len; len -= sizeof ifreq) + run = ifa; + while (run != NULL) { - ifreq = *ifr; - if (__ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0) + if ((run->ifa_flags & IFF_UP) && run->ifa_addr->sa_family == AF_INET + && (!(run->ifa_flags & IFF_LOOPBACK) + || (loopback == 1 && (run->ifa_flags & IFF_LOOPBACK)))) { - perror ("get_myaddress: ioctl"); - exit (1); - } - if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET) - && (!(ifreq.ifr_flags & IFF_LOOPBACK) || - (loopback == 1 && (ifreq.ifr_flags & IFF_LOOPBACK)))) - { - *addr = *((struct sockaddr_in *) &ifr->ifr_addr); + *addr = *((struct sockaddr_in *) run->ifa_addr); addr->sin_port = htons (PMAPPORT); - __close (s); - return; + goto out; } - ifr++; + + run = run->ifa_next; } + if (loopback == 0) { loopback = 1; goto again; } - __close (s); + out: + freeifaddrs (ifa); + + /* The function is horribly specified. It does not return any error + if no interface is up. Probably this won't happen (at least + loopback is there) but still... */ } diff --git a/sunrpc/pmap_clnt.c b/sunrpc/pmap_clnt.c index d88487d8f4..c968511e96 100644 --- a/sunrpc/pmap_clnt.c +++ b/sunrpc/pmap_clnt.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -54,52 +55,41 @@ static bool_t __get_myaddress (struct sockaddr_in *addr) { - int s; - char buf[BUFSIZ]; - struct ifconf ifc; - struct ifreq ifreq, *ifr; - int len, loopback = 1; + struct ifaddrs *ifa; - if ((s = __socket (AF_INET, SOCK_DGRAM, 0)) < 0) + if (getifaddrs (&ifa) == 0) { - perror ("__get_myaddress: socket"); - exit (1); - } - ifc.ifc_len = sizeof (buf); - ifc.ifc_buf = buf; - if (__ioctl (s, SIOCGIFCONF, (char *) &ifc) < 0) - { - perror (_("__get_myaddress: ioctl (get interface configuration)")); + perror ("get_myaddress: getifaddrs"); exit (1); } + int loopback = 1; + struct ifaddrs *run; + again: - ifr = ifc.ifc_req; - for (len = ifc.ifc_len; len; len -= sizeof ifreq) + run = ifa; + while (run != NULL) { - ifreq = *ifr; - if (__ioctl (s, SIOCGIFFLAGS, (char *) &ifreq) < 0) - { - perror ("__get_myaddress: ioctl"); - exit (1); - } - if ((ifreq.ifr_flags & IFF_UP) && (ifr->ifr_addr.sa_family == AF_INET) - && ((ifreq.ifr_flags & IFF_LOOPBACK) || (loopback == 0))) - { - *addr = *((struct sockaddr_in *) &ifr->ifr_addr); - addr->sin_port = htons (PMAPPORT); - __close (s); - return TRUE; - } - ifr++; + if ((run->ifa_flags & IFF_UP) && run->ifa_addr->sa_family == AF_INET + && ((run->ifa_flags & IFF_LOOPBACK) || loopback == 0)) + { + *addr = *((struct sockaddr_in *) run->ifa_addr); + addr->sin_port = htons (PMAPPORT); + goto out; + } + + run = run->ifa_next; } + if (loopback == 1) { loopback = 0; goto again; } - __close (s); - return FALSE; + out: + freeifaddrs (ifa); + + return run == NULL ? FALSE : TRUE; } diff --git a/sunrpc/pmap_rmt.c b/sunrpc/pmap_rmt.c index c02f546b36..e446f5e608 100644 --- a/sunrpc/pmap_rmt.c +++ b/sunrpc/pmap_rmt.c @@ -53,6 +53,7 @@ static char sccsid[] = "@(#)pmap_rmt.c 1.21 87/08/27 Copyr 1984 Sun Micro"; #undef _POSIX_SOURCE /* Ultrix needs --roland@gnu */ #include /* Ultrix needs before net/if --roland@gnu */ #include +#include #include #include #define MAX_BROADCAST_SIZE 1400 @@ -174,55 +175,31 @@ INTDEF(xdr_rmtcallres) static int internal_function -getbroadcastnets (struct in_addr *addrs, int sock, char *buf) - /* int sock: any valid socket will do */ - /* char *buf: why allocate more when we can use existing... */ +getbroadcastnets (struct in_addr *addrs, int naddrs) { - struct ifconf ifc; - struct ifreq ifreq, *ifr; - struct sockaddr_in *sin; - int n, i; + struct ifaddrs *ifa; - ifc.ifc_len = UDPMSGSIZE; - ifc.ifc_buf = buf; - if (__ioctl (sock, SIOCGIFCONF, (char *) &ifc) < 0) + if (getifaddrs (&ifa) == 0) { - perror (_("broadcast: ioctl (get interface configuration)")); - return (0); + perror ("broadcast: getifaddrs"); + return 0; } - ifr = ifc.ifc_req; - for (i = 0, n = ifc.ifc_len / sizeof (struct ifreq); n > 0; n--, ifr++) + + int i = 0; + struct ifaddrs *run = ifa; + while (run != NULL && i < naddrs) { - ifreq = *ifr; - if (__ioctl (sock, SIOCGIFFLAGS, (char *) &ifreq) < 0) - { - perror (_("broadcast: ioctl (get interface flags)")); - continue; - } - if ((ifreq.ifr_flags & IFF_BROADCAST) && - (ifreq.ifr_flags & IFF_UP) && - ifr->ifr_addr.sa_family == AF_INET) - { - sin = (struct sockaddr_in *) &ifr->ifr_addr; -#ifdef SIOCGIFBRDADDR /* 4.3BSD */ - if (__ioctl (sock, SIOCGIFBRDADDR, (char *) &ifreq) < 0) - { - addrs[i++] = inet_makeaddr (inet_netof - /* Changed to pass struct instead of s_addr member - by roland@gnu. */ - (sin->sin_addr), INADDR_ANY); - } - else - { - addrs[i++] = ((struct sockaddr_in *) - &ifreq.ifr_addr)->sin_addr; - } -#else /* 4.2 BSD */ - addrs[i++] = inet_makeaddr (inet_netof - (sin->sin_addr.s_addr), INADDR_ANY); -#endif - } + if ((run->ifa_flags & IFF_BROADCAST) != 0 + && (run->ifa_flags & IFF_UP) != 0 + && run->ifa_addr->sa_family == AF_INET) + /* Copy the broadcast address. */ + addrs[i++] = ((struct sockaddr_in *) run->ifa_broadaddr)->sin_addr; + + run = run->ifa_next; } + + freeifaddrs (ifa); + return i; } @@ -280,7 +257,7 @@ clnt_broadcast (prog, vers, proc, xargs, argsp, xresults, resultsp, eachresult) #endif /* def SO_BROADCAST */ fd.fd = sock; fd.events = POLLIN; - nets = getbroadcastnets (addrs, sock, inbuf); + nets = getbroadcastnets (addrs, sizeof (addrs) / sizeof (addrs[0])); __bzero ((char *) &baddr, sizeof (baddr)); baddr.sin_family = AF_INET; baddr.sin_port = htons (PMAPPORT); -- cgit v1.2.3