From 85237d203bc9dfe549218c72686a611cff6368b4 Mon Sep 17 00:00:00 2001 From: maxim Date: Mon, 24 Jan 2005 13:27:24 +0000 Subject: [PATCH] o Try hard to guess a buffer size for a fast growing routing table. An approach taken from killall/killall.c. PR: bin/76075 Submitted by: Dmitrij Tejblum MFC after: 3 weeks --- usr.sbin/arp/arp.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/usr.sbin/arp/arp.c b/usr.sbin/arp/arp.c index 9c794aeae493..32c0bc258501 100644 --- a/usr.sbin/arp/arp.c +++ b/usr.sbin/arp/arp.c @@ -456,12 +456,12 @@ search(u_long addr, action_fn *action) { int mib[6]; size_t needed; - char *lim, *buf, *next; + char *lim, *buf, *newbuf, *next; struct rt_msghdr *rtm; struct sockaddr_inarp *sin2; struct sockaddr_dl *sdl; char ifname[IF_NAMESIZE]; - int found_entry = 0; + int st, found_entry = 0; mib[0] = CTL_NET; mib[1] = PF_ROUTE; @@ -473,9 +473,19 @@ search(u_long addr, action_fn *action) err(1, "route-sysctl-estimate"); if (needed == 0) /* empty table */ return 0; - if ((buf = malloc(needed)) == NULL) - err(1, "malloc"); - if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) + buf = NULL; + do { + needed += needed / 2; + newbuf = realloc(buf, needed); + if (newbuf == NULL) { + if (buf != NULL) + free(buf); + errx(1, "could not reallocate memory"); + } + buf = newbuf; + st = sysctl(mib, 6, buf, &needed, NULL, 0); + } while (st == -1 && errno == ENOMEM); + if (st == -1) err(1, "actual retrieval of routing table"); lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) {