From 665d6cf79127c45545fd18e1ba85b3f086ed42c0 Mon Sep 17 00:00:00 2001 From: deraadt Date: Fri, 24 Jan 1997 19:17:25 +0000 Subject: [PATCH] SIOCGIFCONF nicely --- lib/libpcap/inet.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/libpcap/inet.c b/lib/libpcap/inet.c index fd066d1463d..d32a4f890e2 100644 --- a/lib/libpcap/inet.c +++ b/lib/libpcap/inet.c @@ -1,4 +1,4 @@ -/* $OpenBSD: inet.c,v 1.5 1996/07/19 07:52:16 deraadt Exp $ */ +/* $OpenBSD: inet.c,v 1.6 1997/01/24 19:17:25 deraadt Exp $ */ /* * Copyright (c) 1994, 1995, 1996 @@ -86,27 +86,36 @@ pcap_lookupdev(errbuf) register char *errbuf; { register int fd, minunit, n; - register char *cp; + register char *cp, *ibuf = NULL; register struct ifreq *ifrp, *ifend, *ifnext, *mp; struct ifconf ifc; - struct ifreq ibuf[16], ifr; + struct ifreq ifr; static char device[sizeof(ifrp->ifr_name) + 1]; + int len = 8192; fd = socket(AF_INET, SOCK_DGRAM, 0); if (fd < 0) { (void)sprintf(errbuf, "socket: %s", pcap_strerror(errno)); return (NULL); } - ifc.ifc_len = sizeof ibuf; - ifc.ifc_buf = (caddr_t)ibuf; - - if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 || - ifc.ifc_len < sizeof(struct ifreq)) { - (void)sprintf(errbuf, "SIOCGIFCONF: %s", pcap_strerror(errno)); - (void)close(fd); - return (NULL); + while (1) { + ifc.ifc_len = len; + ifc.ifc_buf = ibuf = realloc(ibuf, len); + if (ibuf == NULL) { + close(fd); + return (NULL); + } + if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0) { + (void)close(fd); + free(ibuf); + return (NULL); + } + if (ifc.ifc_len + sizeof(ifr) < len) + break; + len *= 2; } - ifrp = ibuf; + + ifrp = (struct ifreq *)ibuf; ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len); mp = NULL; @@ -134,6 +143,7 @@ pcap_lookupdev(errbuf) (void)sprintf(errbuf, "SIOCGIFFLAGS: %s", pcap_strerror(errno)); (void)close(fd); + free(ibuf); return (NULL); } @@ -149,6 +159,7 @@ pcap_lookupdev(errbuf) mp = ifrp; } } + free(ibuf); (void)close(fd); if (mp == NULL) { (void)strcpy(errbuf, "no suitable device found"); -- 2.20.1