From e185faf1a28f406a13a42b6c7a63b6bb8671998c Mon Sep 17 00:00:00 2001 From: deraadt Date: Wed, 22 Jan 1997 08:52:32 +0000 Subject: [PATCH] SIOCGIFCONF more carefully --- lib/libc/rpc/get_myaddress.c | 29 +++++++++++++++++++++-------- usr.sbin/ypbind/ypbind.c | 32 ++++++++++++++++++++++---------- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/libc/rpc/get_myaddress.c b/lib/libc/rpc/get_myaddress.c index aaa6741c483..25efad7f12c 100644 --- a/lib/libc/rpc/get_myaddress.c +++ b/lib/libc/rpc/get_myaddress.c @@ -28,7 +28,7 @@ */ #if defined(LIBC_SCCS) && !defined(lint) -static char *rcsid = "$OpenBSD: get_myaddress.c,v 1.5 1996/08/19 08:31:31 tholo Exp $"; +static char *rcsid = "$OpenBSD: get_myaddress.c,v 1.6 1997/01/22 08:52:32 deraadt Exp $"; #endif /* LIBC_SCCS and not lint */ /* @@ -43,6 +43,7 @@ static char *rcsid = "$OpenBSD: get_myaddress.c,v 1.5 1996/08/19 08:31:31 tholo #include #include #include +#include #include #include #include @@ -59,20 +60,30 @@ get_myaddress(addr) struct sockaddr_in *addr; { int s; - char buf[BUFSIZ]; + char *inbuf = NULL; struct ifconf ifc; struct ifreq ifreq, *ifr; - int len, slop; + int len = 8192, slop; int loopback = 0, gotit = 0; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { return (-1); } - ifc.ifc_len = sizeof (buf); - ifc.ifc_buf = buf; - if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { - (void) close(s); - return (-1); + while (1) { + ifc.ifc_len = len; + ifc.ifc_buf = inbuf = realloc(inbuf, len); + if (inbuf == NULL) { + close(s); + return (-1); + } + if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) { + (void) close(s); + free(inbuf); + return (-1); + } + if (ifc.ifc_len + sizeof(ifreq) < len) + break; + len *= 2; } again: ifr = ifc.ifc_req; @@ -80,6 +91,7 @@ again: ifreq = *ifr; if (ioctl(s, SIOCGIFFLAGS, (char *)&ifreq) < 0) { (void) close(s); + free(inbuf); return (-1); } if ((ifreq.ifr_flags & IFF_UP) && @@ -105,5 +117,6 @@ again: goto again; } (void) close(s); + free (inbuf); return (0); } diff --git a/usr.sbin/ypbind/ypbind.c b/usr.sbin/ypbind/ypbind.c index 633effe47a5..763f5d72a60 100644 --- a/usr.sbin/ypbind/ypbind.c +++ b/usr.sbin/ypbind/ypbind.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ypbind.c,v 1.13 1996/12/21 05:52:31 deraadt Exp $ */ +/* $OpenBSD: ypbind.c,v 1.14 1997/01/22 08:54:14 deraadt Exp $ */ /* * Copyright (c) 1996 Theo de Raadt @@ -34,7 +34,7 @@ */ #ifndef LINT -static char rcsid[] = "$OpenBSD: ypbind.c,v 1.13 1996/12/21 05:52:31 deraadt Exp $"; +static char rcsid[] = "$OpenBSD: ypbind.c,v 1.14 1997/01/22 08:54:14 deraadt Exp $"; #endif #include @@ -631,10 +631,10 @@ broadcast(ypdb) { domainname dom = ypdb->dom_domain; struct rpc_msg msg; - char buf[1400], inbuf[8192]; + char buf[1400], *inbuf = NULL; char path[MAXPATHLEN]; enum clnt_stat st; - int outlen, i, sock, len; + int outlen, i, sock, len, inlen = 8192; struct sockaddr_in bindsin; struct ifconf ifc; struct ifreq ifreq, *ifr; @@ -712,13 +712,24 @@ broadcast(ypdb) return -1; } - ifc.ifc_len = sizeof inbuf; - ifc.ifc_buf = inbuf; - if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) { - close(sock); - perror("ioctl(SIOCGIFCONF)"); - return -1; + while (1) { + ifc.ifc_len = inlen; + ifc.ifc_buf = inbuf = realloc(inbuf, inlen); + if (inbuf == NULL) { + close(sock); + return (-1); + } + if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { + (void) close(sock); + free(inbuf); + perror("ioctl(SIOCGIFCONF)"); + return (-1); + } + if (ifc.ifc_len + sizeof(ifreq) < inlen) + break; + inlen *= 2; } + ifr = ifc.ifc_req; ifreq.ifr_name[0] = '\0'; for (i = 0; i < ifc.ifc_len; i += len, @@ -759,6 +770,7 @@ broadcast(ypdb) perror("sendto"); } close(sock); + free(inbuf); return 0; } -- 2.20.1