From ee9a463848b6585566f0c67f01a4ebab33764f89 Mon Sep 17 00:00:00 2001 From: Garrett Wollman Date: Thu, 7 Dec 1995 19:21:53 +0000 Subject: [PATCH] Use a dynamically-sized buffer for SIOCGIFCONF so that `ifconfig -a' actually retrieves all the information no matter how many interfaces there are. (Probably there are other utilities which need similar modification.) Submitted by: Andrew Webster --- sbin/ifconfig/ifconfig.c | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/sbin/ifconfig/ifconfig.c b/sbin/ifconfig/ifconfig.c index c0b2905daccd..9fc8da63af89 100644 --- a/sbin/ifconfig/ifconfig.c +++ b/sbin/ifconfig/ifconfig.c @@ -31,6 +31,11 @@ * SUCH DAMAGE. */ +/* + * 951109 - Andrew@pubnix.net - Changed to iterative buffer growing mechanism + * for ifconfig -a so all interfaces are queried. + * + */ #ifndef lint static char copyright[] = "@(#) Copyright (c) 1983, 1993\n\ @@ -216,22 +221,33 @@ main(argc, argv) exit(1); } if (strstr(name, "-a")) { + char *buffer; struct ifconf ifc; -#define MAX_INTERFACES 50 /* Yeah right. */ - char buffer[MAX_INTERFACES * sizeof(struct ifreq)]; struct ifreq *ifptr, *end; int ifflags, selectflag = -1; + int oldbufsize, bufsize = sizeof(struct ifreq); if (strstr(name, "-au")) selectflag = 1; if (strstr(name, "-ad")) selectflag = 0; - ifc.ifc_len = sizeof(buffer); - ifc.ifc_buf = buffer; - if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) { - perror("ifconfig (SIOCGIFCONF)"); - exit (1); - } + buffer = malloc(bufsize); /* allocate first buffer */ + ifc.ifc_len = bufsize; /* Initial setting */ + /* + * Itterate through here until we don't get any more data + */ + do { + oldbufsize = ifc.ifc_len; + bufsize += 1+sizeof(struct ifreq); + buffer = realloc((void *)buffer, bufsize); + ifc.ifc_len = bufsize; + ifc.ifc_buf = buffer; + if (ioctl(s, SIOCGIFCONF, (char *) &ifc) < 0) { + perror("ifconfig (SIOCGIFCONF)"); + exit (1); + } + } while (ifc.ifc_len > oldbufsize); + ifflags = ifc.ifc_req->ifr_flags; end = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len); ifptr = ifc.ifc_req; @@ -243,7 +259,7 @@ main(argc, argv) perror("ifconfig: socket"); exit(1); } - if (ifptr->ifr_flags == ifflags) + if (ifptr->ifr_flags == ifflags) ifconfig(argc,argv,af,rafp,selectflag); if(ifptr->ifr_addr.sa_len) /* Dohw! */ ifptr = (struct ifreq *) ((caddr_t) ifptr + @@ -251,6 +267,7 @@ main(argc, argv) sizeof(struct sockaddr)); ifptr++; } + free(buffer); } else ifconfig(argc,argv,af,rafp, -1);