Source-Changes-HG archive

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

[src/trunk]: src/usr.bin/netstat Use sysctl to fetch IP, ICMP, TCP, and UDP s...



details:   https://anonhg.NetBSD.org/src/rev/7ebb1ef33f1c
branches:  trunk
changeset: 583433:7ebb1ef33f1c
user:      elad <elad%NetBSD.org@localhost>
date:      Sat Aug 06 17:58:13 2005 +0000

description:
Use sysctl to fetch IP, ICMP, TCP, and UDP statistics.

diffstat:

 usr.bin/netstat/inet.c |  68 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 53 insertions(+), 15 deletions(-)

diffs (125 lines):

diff -r 07cab38f9a0d -r 7ebb1ef33f1c usr.bin/netstat/inet.c
--- a/usr.bin/netstat/inet.c    Sat Aug 06 14:34:12 2005 +0000
+++ b/usr.bin/netstat/inet.c    Sat Aug 06 17:58:13 2005 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: inet.c,v 1.64 2005/08/04 19:41:28 rpaulo Exp $ */
+/*     $NetBSD: inet.c,v 1.65 2005/08/06 17:58:13 elad Exp $   */
 
 /*
  * Copyright (c) 1983, 1988, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "from: @(#)inet.c       8.4 (Berkeley) 4/20/94";
 #else
-__RCSID("$NetBSD: inet.c,v 1.64 2005/08/04 19:41:28 rpaulo Exp $");
+__RCSID("$NetBSD: inet.c,v 1.65 2005/08/06 17:58:13 elad Exp $");
 #endif
 #endif /* not lint */
 
@@ -44,6 +44,7 @@
 #include <sys/socketvar.h>
 #include <sys/mbuf.h>
 #include <sys/protosw.h>
+#include <sys/sysctl.h>
 
 #include <net/if_arp.h>
 #include <net/route.h>
@@ -79,6 +80,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <err.h>
 #include "netstat.h"
 
 struct inpcb inpcb;
@@ -204,10 +206,19 @@
 {
        struct tcpstat tcpstat;
 
-       if (off == 0)
-               return;
+       if (use_sysctl) {
+               size_t size = sizeof(tcpstat);
+
+               if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &size,
+                                NULL, 0) == -1)
+                       err(1, "net.inet.tcp.stats");
+       } else {
+               if (off == 0)
+                       return;
+               kread(off, (char *)&tcpstat, sizeof (tcpstat));
+       }
+
        printf ("%s:\n", name);
-       kread(off, (char *)&tcpstat, sizeof (tcpstat));
 
 #define        ps(f, m) if (tcpstat.f || sflag <= 1) \
     printf(m, (unsigned long long)tcpstat.f)
@@ -321,10 +332,19 @@
        struct udpstat udpstat;
        u_quad_t delivered;
 
-       if (off == 0)
-               return;
-       printf("%s:\n", name);
-       kread(off, (char *)&udpstat, sizeof (udpstat));
+       if (use_sysctl) {
+               size_t size = sizeof(udpstat);
+
+               if (sysctlbyname("net.inet.udp.stats", &udpstat, &size,
+                                NULL, 0) == -1)
+                       err(1, "net.inet.udp.stats");
+       } else {
+               if (off == 0)
+                       return;
+               kread(off, (char *)&udpstat, sizeof (udpstat));
+       }
+
+       printf ("%s:\n", name);
 
 #define        ps(f, m) if (udpstat.f || sflag <= 1) \
     printf(m, (unsigned long long)udpstat.f)
@@ -367,9 +387,18 @@
 {
        struct ipstat ipstat;
 
-       if (off == 0)
-               return;
-       kread(off, (char *)&ipstat, sizeof (ipstat));
+       if (use_sysctl) {
+               size_t size = sizeof(ipstat);
+
+               if (sysctlbyname("net.inet.ip.stats", &ipstat, &size,
+                                NULL, 0) == -1)
+                       err(1, "net.inet.ip.stats");
+       } else {
+               if (off == 0)
+                       return;
+               kread(off, (char *)&ipstat, sizeof (ipstat));
+       }
+
        printf("%s:\n", name);
 
 #define        ps(f, m) if (ipstat.f || sflag <= 1) \
@@ -446,9 +475,18 @@
        struct icmpstat icmpstat;
        int i, first;
 
-       if (off == 0)
-               return;
-       kread(off, (char *)&icmpstat, sizeof (icmpstat));
+       if (use_sysctl) {
+               size_t size = sizeof(icmpstat);
+
+               if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &size,
+                                NULL, 0) == -1)
+                       err(1, "net.inet.icmp.stats");
+       } else {
+               if (off == 0)
+                       return;
+               kread(off, (char *)&icmpstat, sizeof (icmpstat));
+       }
+
        printf("%s:\n", name);
 
 #define        p(f, m) if (icmpstat.f || sflag <= 1) \



Home | Main Index | Thread Index | Old Index