summaryrefslogtreecommitdiffstats
path: root/lib/libpcap/inet.c
diff options
context:
space:
mode:
authorderaadt <deraadt@openbsd.org>1997-01-24 19:17:25 +0000
committerderaadt <deraadt@openbsd.org>1997-01-24 19:17:25 +0000
commit665d6cf79127c45545fd18e1ba85b3f086ed42c0 (patch)
tree995110d9cc061ac022e65d538b0c65f0205f48f2 /lib/libpcap/inet.c
parentdo not build fake disklabel partitions for MBR partitions that have out-of-range start/size fields (diff)
downloadwireguard-openbsd-665d6cf79127c45545fd18e1ba85b3f086ed42c0.tar.xz
wireguard-openbsd-665d6cf79127c45545fd18e1ba85b3f086ed42c0.zip
SIOCGIFCONF nicely
Diffstat (limited to 'lib/libpcap/inet.c')
-rw-r--r--lib/libpcap/inet.c35
1 files 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");