Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/netstat if not given an interface to monitor by defa...



details:   https://anonhg.NetBSD.org/src/rev/cbab311f187f
branches:  trunk
changeset: 372826:cbab311f187f
user:      mrg <mrg%NetBSD.org@localhost>
date:      Wed Dec 28 18:34:33 2022 +0000

description:
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!)

diffstat:

 usr.bin/netstat/if.c |  30 ++++++++++++++----------------
 1 files changed, 14 insertions(+), 16 deletions(-)

diffs (56 lines):

diff -r 3360d2f48956 -r cbab311f187f usr.bin/netstat/if.c
--- a/usr.bin/netstat/if.c      Wed Dec 28 18:19:44 2022 +0000
+++ b/usr.bin/netstat/if.c      Wed Dec 28 18:34:33 2022 +0000
@@ -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 @@
 
                        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 @@
                        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;
 }



Home | Main Index | Thread Index | Old Index