diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-01-22 09:22:02 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 1997-01-22 09:22:02 +0000 |
commit | e6a3e3cc1a85d6c3ee9a44f4849407289af6c3a4 (patch) | |
tree | d79680838641a45cfbb95bf39f66587958f18f37 | |
parent | e1a789935c56dfb377000358a26c607d28e448b9 (diff) |
SIOCGIFCONF nicely
-rw-r--r-- | usr.sbin/rarpd/rarpd.c | 33 | ||||
-rw-r--r-- | usr.sbin/timed/timed/timed.c | 34 |
2 files changed, 47 insertions, 20 deletions
diff --git a/usr.sbin/rarpd/rarpd.c b/usr.sbin/rarpd/rarpd.c index 9847ffde373..16771c32655 100644 --- a/usr.sbin/rarpd/rarpd.c +++ b/usr.sbin/rarpd/rarpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rarpd.c,v 1.6 1997/01/15 23:44:15 millert Exp $ */ +/* $OpenBSD: rarpd.c,v 1.7 1997/01/22 09:22:01 deraadt Exp $ */ /* $NetBSD: rarpd.c,v 1.12 1996/03/21 18:28:23 jtc Exp $ */ /* @@ -28,7 +28,7 @@ char copyright[] = #endif /* not lint */ #ifndef lint -static char rcsid[] = "$OpenBSD: rarpd.c,v 1.6 1997/01/15 23:44:15 millert Exp $"; +static char rcsid[] = "$OpenBSD: rarpd.c,v 1.7 1997/01/22 09:22:01 deraadt Exp $"; #endif @@ -224,10 +224,10 @@ init_one(ifname) void init_all() { - char inbuf[8192]; + char *inbuf = NULL; struct ifconf ifc; struct ifreq *ifr; - int fd; + int fd, inlen = 8192; int i, len; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { @@ -235,18 +235,30 @@ init_all() /* NOTREACHED */ } - ifc.ifc_len = sizeof(inbuf); - ifc.ifc_buf = inbuf; - if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) { - err(FATAL, "init_all: SIOCGIFCONF: %s", strerror(errno)); - /* NOTREACHED */ + while (1) { + ifc.ifc_len = inlen; + ifc.ifc_buf = inbuf = realloc(inbuf, inlen); + if (inbuf == NULL) { + close(fd); + err(FATAL, "init_all: malloc: %s", strerror(errno)); + } + if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) { + (void) close(fd); + free(inbuf); + err(FATAL, "init_all: SIOCGIFCONF: %s", strerror(errno)); + /* NOTREACHED */ + } + if (ifc.ifc_len + sizeof(*ifr) < inlen) + break; + inlen *= 2; } + ifr = ifc.ifc_req; for (i = 0; i < ifc.ifc_len; i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) { len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len; if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) { + free(inbuf); err(FATAL, "init_all: SIOCGIFFLAGS: %s", strerror(errno)); /* NOTREACHED */ @@ -256,6 +268,7 @@ init_all() continue; init_one(ifr->ifr_name); } + free(inbuf); (void) close(fd); } diff --git a/usr.sbin/timed/timed/timed.c b/usr.sbin/timed/timed/timed.c index c6d22698eb0..e17ffe62741 100644 --- a/usr.sbin/timed/timed/timed.c +++ b/usr.sbin/timed/timed/timed.c @@ -42,7 +42,7 @@ static char sccsid[] = "@(#)timed.c 5.1 (Berkeley) 5/11/93"; #endif /* not lint */ #ifdef sgi -#ident "$Revision: 1.4 $" +#ident "$Revision: 1.5 $" #endif /* sgi */ #define TSPTYPES @@ -137,7 +137,7 @@ main(int argc, char **argv) int nflag, iflag; struct timeval ntime; struct servent *srvp; - char buf[BUFSIZ], *cp, *cplim; + char *inbuf = NULL, *cp, *cplim; struct ifconf ifc; struct ifreq ifreq, ifreqf, *ifr; register struct netinfo *ntp; @@ -147,6 +147,7 @@ main(int argc, char **argv) struct nets *nt; struct sockaddr_in server; u_short port; + int inlen = 8192; int c; extern char *optarg; extern int optind, opterr; @@ -384,20 +385,32 @@ main(int argc, char **argv) if (0 == (nt->net & 0xff000000)) nt->net <<= 8; } - ifc.ifc_len = sizeof(buf); - ifc.ifc_buf = buf; - if (ioctl(sock, SIOCGIFCONF, (char *)&ifc) < 0) { - perror("timed: get interface configuration"); - exit(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("timed: get interface configuration"); + exit(1); + } + if (ifc.ifc_len + sizeof(ifreq) < inlen) + break; + inlen *= 2; } + ntp = NULL; #ifdef sgi #define size(p) (sizeof(*ifr) - sizeof(ifr->ifr_name)) /* XXX hack. kludge */ #else #define size(p) max((p).sa_len, sizeof(p)) #endif - cplim = buf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ - for (cp = buf; cp < cplim; + cplim = inbuf + ifc.ifc_len; /*skip over if's with big ifr_addr's */ + for (cp = inbuf; cp < cplim; cp += sizeof (ifr->ifr_name) + size(ifr->ifr_addr)) { ifr = (struct ifreq *)cp; if (ifr->ifr_addr.sa_family != AF_INET) @@ -467,13 +480,14 @@ main(int argc, char **argv) ntip = ntp; ntp = NULL; } + if (ntp) (void) free((char *)ntp); if (nettab == NULL) { fprintf(stderr, "timed: no network usable\n"); exit(1); } - + free(inbuf); #ifdef sgi (void)schedctl(RENICE,0,10); /* run fast to get good time */ |