if not given an interface to monitor by default, pick the one

with the most bytes in/out the first time and keep using it.

in addition to picking the most likely intersting interface,
this actually fixes a bug where interfaces coming/going in
the middle of eg, "netstat -b -w 1", may end up showing the
data for another interface on any output line (including the
the header declaring the interface!)
This commit is contained in:
mrg 2022-12-28 18:34:33 +00:00
parent 0946466ff9
commit 968fb181e3
1 changed files with 14 additions and 16 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: if.c,v 1.108 2022/12/12 05:09:33 msaitoh Exp $ */
/* $NetBSD: if.c,v 1.109 2022/12/28 18:34:33 mrg Exp $ */
/*
* Copyright (c) 1983, 1988, 1993
@ -34,7 +34,7 @@
#if 0
static char sccsid[] = "from: @(#)if.c 8.2 (Berkeley) 2/21/94";
#else
__RCSID("$NetBSD: if.c,v 1.108 2022/12/12 05:09:33 msaitoh Exp $");
__RCSID("$NetBSD: if.c,v 1.109 2022/12/28 18:34:33 mrg Exp $");
#endif
#endif /* not lint */
@ -1171,7 +1171,11 @@ fetchifs(void)
if_data_ext_get(name, &dext);
if (interface != NULL && !strcmp(name, interface)) {
if ((interface != NULL && !strcmp(name, interface)) ||
(interface == NULL &&
((ip_cur.ift_ib + ip_cur.ift_ob) == 0 ||
(ip_cur.ift_ib + ip_cur.ift_ob <
ifd->ifi_ibytes + ifd->ifi_obytes)))) {
strlcpy(ip_cur.ift_name, name,
sizeof(ip_cur.ift_name));
ip_cur.ift_ip = ifd->ifi_ipackets;
@ -1197,17 +1201,11 @@ fetchifs(void)
break;
}
}
if (interface == NULL) {
strlcpy(ip_cur.ift_name, name,
sizeof(ip_cur.ift_name));
ip_cur.ift_ip = ifd->ifi_ipackets;
ip_cur.ift_ib = ifd->ifi_ibytes;
ip_cur.ift_ie = ifd->ifi_ierrors;
ip_cur.ift_op = ifd->ifi_opackets;
ip_cur.ift_ob = ifd->ifi_obytes;
ip_cur.ift_oe = ifd->ifi_oerrors;
ip_cur.ift_co = ifd->ifi_collisions;
ip_cur.ift_iq = ifd->ifi_iqdrops;
ip_cur.ift_oq = dext.ifi_oqdrops;
}
/*
* If we picked an interface, be sure to keep using it for the rest
* of this instance.
*/
if (interface == NULL)
interface = ip_cur.ift_name;
}