Release 6
[unix-history] / usr / src / usr.bin / netstat / ns.c
index 1048b98..1883535 100644 (file)
@@ -1,19 +1,15 @@
 /*
 /*
- * Copyright (c) 1983 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1983, 1988 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
  */
 
-
 #ifndef lint
 #ifndef lint
-static char sccsid[] = "@(#)ns.c       5.1 (Berkeley) %G%";
-#endif not lint
-
-#include <stdio.h>
-#include <errno.h>
-#include <nlist.h>
+static char sccsid[] = "@(#)ns.c       5.16 (Berkeley) %G%";
+#endif /* not lint */
 
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/mbuf.h>
 #include <sys/socket.h>
 #include <sys/socketvar.h>
 #include <sys/mbuf.h>
@@ -23,7 +19,6 @@ static char sccsid[] = "@(#)ns.c      5.1 (Berkeley) %G%";
 #include <net/if.h>
 
 #include <netinet/tcp_fsm.h>
 #include <net/if.h>
 
 #include <netinet/tcp_fsm.h>
-#include <netinet/tcp_timer.h>
 
 #include <netns/ns.h>
 #include <netns/ns_pcb.h>
 
 #include <netns/ns.h>
 #include <netns/ns_pcb.h>
@@ -32,20 +27,23 @@ static char sccsid[] = "@(#)ns.c    5.1 (Berkeley) %G%";
 #include <netns/ns_error.h>
 #include <netns/sp.h>
 #include <netns/spidp.h>
 #include <netns/ns_error.h>
 #include <netns/sp.h>
 #include <netns/spidp.h>
+#include <netns/spp_timer.h>
 #include <netns/spp_var.h>
 #define SANAMES
 #include <netns/spp_debug.h>
 
 #include <netns/spp_var.h>
 #define SANAMES
 #include <netns/spp_debug.h>
 
+#include <nlist.h>
+#include <errno.h>
+#include <stdio.h>
+#include <string.h>
+#include "netstat.h"
 
 struct nspcb nspcb;
 struct sppcb sppcb;
 struct socket sockb;
 
 struct nspcb nspcb;
 struct sppcb sppcb;
 struct socket sockb;
-struct protosw proto;
-extern int kmem;
-extern int Aflag;
-extern int aflag;
-extern int nflag;
-char *ns_prpr();
+
+static char *ns_prpr __P((struct ns_addr *));
+static void ns_erputil __P((int, int));
 
 static int first = 1;
 
 
 static int first = 1;
 
@@ -56,43 +54,28 @@ static      int first = 1;
  * -a (all) flag is specified.
  */
 
  * -a (all) flag is specified.
  */
 
+void
 nsprotopr(off, name)
 nsprotopr(off, name)
-       off_t off;
+       u_long off;
        char *name;
 {
        struct nspcb cb;
        register struct nspcb *prev, *next;
        int isspp;
 
        char *name;
 {
        struct nspcb cb;
        register struct nspcb *prev, *next;
        int isspp;
 
-       if (off == 0) {
-               printf("%s control block: symbol not in namelist\n", name);
+       if (off == 0)
                return;
                return;
-       }
        isspp = strcmp(name, "spp") == 0;
        isspp = strcmp(name, "spp") == 0;
-       klseek(kmem, off, 0);
-       read(kmem, &cb, sizeof (struct nspcb));
+       kread(off, (char *)&cb, sizeof (struct nspcb));
        nspcb = cb;
        prev = (struct nspcb *)off;
        nspcb = cb;
        prev = (struct nspcb *)off;
-       if (first) {
-               printf("Active connections");
-               if (aflag)
-                       printf(" (including servers)");
-               putchar('\n');
-               if (Aflag)
-                       printf("%-8.8s ", "PCB");
-               printf(Aflag ? "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
-                       "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
-                       "Proto", "Recv-Q", "Send-Q",
-                       "Local Address", "Foreign Address", "(state)");
-               first = 0;
-       }
+       if (nspcb.nsp_next == (struct nspcb *)off)
+               return;
        for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
        for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
-               char *cp;
-               off_t ppcb;
+               u_long ppcb;
 
                next = nspcb.nsp_next;
 
                next = nspcb.nsp_next;
-               klseek(kmem, (off_t)next, 0);
-               read(kmem, &nspcb, sizeof (nspcb));
+               kread((u_long)next, (char *)&nspcb, sizeof (nspcb));
                if (nspcb.nsp_prev != prev) {
                        printf("???\n");
                        break;
                if (nspcb.nsp_prev != prev) {
                        printf("???\n");
                        break;
@@ -100,16 +83,29 @@ nsprotopr(off, name)
                if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
                        continue;
                }
                if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
                        continue;
                }
-               klseek(kmem, (off_t)nspcb.nsp_socket, 0);
-               read(kmem, &sockb, sizeof (sockb));
-               ppcb = (off_t) nspcb.nsp_pcb;
+               kread((u_long)nspcb.nsp_socket,
+                               (char *)&sockb, sizeof (sockb));
+               ppcb = (u_long) nspcb.nsp_pcb;
                if (ppcb) {
                        if (isspp) {
                if (ppcb) {
                        if (isspp) {
-                               klseek(kmem, ppcb, 0);
-                               read(kmem, &sppcb, sizeof (sppcb));
+                               kread(ppcb, (char *)&sppcb, sizeof (sppcb));
                        } else continue;
                } else
                        if (isspp) continue;
                        } else continue;
                } else
                        if (isspp) continue;
+               if (first) {
+                       printf("Active NS connections");
+                       if (aflag)
+                               printf(" (including servers)");
+                       putchar('\n');
+                       if (Aflag)
+                               printf("%-8.8s ", "PCB");
+                       printf(Aflag ?
+                               "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
+                               "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
+                               "Proto", "Recv-Q", "Send-Q",
+                               "Local Address", "Foreign Address", "(state)");
+                       first = 0;
+               }
                if (Aflag)
                        printf("%8x ", ppcb);
                printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
                if (Aflag)
                        printf("%8x ", ppcb);
                printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
@@ -118,7 +114,7 @@ nsprotopr(off, name)
                printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
                if (isspp) {
                        extern char *tcpstates[];
                printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
                if (isspp) {
                        extern char *tcpstates[];
-                       if (sppcb.s_state < 0 || sppcb.s_state >= TCP_NSTATES)
+                       if (sppcb.s_state >= TCP_NSTATES)
                                printf(" %d", sppcb.s_state);
                        else
                                printf(" %s", tcpstates[sppcb.s_state]);
                                printf(" %d", sppcb.s_state);
                        else
                                printf(" %s", tcpstates[sppcb.s_state]);
@@ -127,71 +123,129 @@ nsprotopr(off, name)
                prev = next;
        }
 }
                prev = next;
        }
 }
-#define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
+#define ANY(x,y,z) \
+       ((x) ? printf("\t%d %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
 
 /*
  * Dump SPP statistics structure.
  */
 
 /*
  * Dump SPP statistics structure.
  */
+void
 spp_stats(off, name)
 spp_stats(off, name)
-       off_t off;
+       u_long off;
        char *name;
 {
        struct spp_istat spp_istat;
        char *name;
 {
        struct spp_istat spp_istat;
+#define sppstat spp_istat.newstats
 
 
-       if (off == 0) {
-               printf("%sstat: symbol not in namelist\n", name);
+       if (off == 0)
                return;
                return;
-       }
-       klseek(kmem, off, 0);
-       read(kmem, (char *)&spp_istat, sizeof (spp_istat));
+       kread(off, (char *)&spp_istat, sizeof (spp_istat));
        printf("%s:\n", name);
        ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
        ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
        printf("%s:\n", name);
        ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
        ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
-       ANY(spp_istat.nonucn, "connection", " dropped due to inability to connect");
-       ANY(spp_istat.noconn, "connection", " dropped due to inability to connect");
-       ANY(spp_istat.notme, "connection", " incompleted due to mismatched id's");
+       ANY(spp_istat.nonucn, "connection",
+           " dropped due to inability to connect");
+       ANY(spp_istat.noconn, "connection",
+           " dropped due to inability to connect");
+       ANY(spp_istat.notme, "connection",
+           " incompleted due to mismatched id's");
        ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
        ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
        ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
        ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
        ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
        ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
        ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
        ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
+       ANY(sppstat.spps_connattempt, "connection", " initiated");
+       ANY(sppstat.spps_accepts, "connection", " accepted");
+       ANY(sppstat.spps_connects, "connection", " established");
+       ANY(sppstat.spps_drops, "connection", " dropped");
+       ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
+       ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
+       ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
+       ANY(sppstat.spps_rttupdated, "time", " we got rtt");
+       ANY(sppstat.spps_delack, "delayed ack", " sent");
+       ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
+       ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
+       ANY(sppstat.spps_persisttimeo, "persist timeout", "");
+       ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
+       ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
+       ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
+       ANY(sppstat.spps_sndtotal, "total packet", " sent");
+       ANY(sppstat.spps_sndpack, "data packet", " sent");
+       ANY(sppstat.spps_sndbyte, "data byte", " sent");
+       ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
+       ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
+       ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
+       ANY(sppstat.spps_sndprobe, "window probe", " sent");
+       ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
+       ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
+       ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
+       ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
+       ANY(sppstat.spps_rcvtotal, "total packet", " received");
+       ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
+       ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
+       ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
+       ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
+       ANY(sppstat.spps_rcvshort, "packet", " received too short");
+       ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
+       ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
+       ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
+       ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
+       ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
+       ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
+       ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
+       ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
+       ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
+       ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
+       ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
+       ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
+       ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
+       ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
+       ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
 }
 }
+#undef ANY
+#define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
 
 /*
  * Dump IDP statistics structure.
  */
 
 /*
  * Dump IDP statistics structure.
  */
+void
 idp_stats(off, name)
 idp_stats(off, name)
-       off_t off;
+       u_long off;
        char *name;
 {
        struct idpstat idpstat;
 
        char *name;
 {
        struct idpstat idpstat;
 
-       if (off == 0) {
-               printf("%sstat: symbol not in namelist\n", name);
+       if (off == 0)
                return;
                return;
-       }
-       klseek(kmem, off, 0);
-       read(kmem, (char *)&idpstat, sizeof (idpstat));
+       kread(off, (char *)&idpstat, sizeof (idpstat));
+       printf("%s:\n", name);
        ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
        ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
        ANY(idpstat.idps_badsum, "packet", " with bad checksums");
 }
 
        ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
        ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
        ANY(idpstat.idps_badsum, "packet", " with bad checksums");
 }
 
-static char *((ns_errnames[])[2]) = {
-       {"Unspecified Error", " at Destination"},
-       {"Bad Checksum", " at Destination"},
-       {"No Listener", " at Socket"},
-       {"Packet", " Refused due to lack of space at Destination"},
-       {"Unspecified Error", " while gatewayed"},
-       {"Bad Checksum", " while gatewayed"},
-       {"Packet", " forwarded too many times"},
-       {"Packet", " too large to be forwarded"},
+static struct {
+       u_short code;
+       char *name;
+       char *where;
+} ns_errnames[] = {
+       {0, "Unspecified Error", " at Destination"},
+       {1, "Bad Checksum", " at Destination"},
+       {2, "No Listener", " at Socket"},
+       {3, "Packet", " Refused due to lack of space at Destination"},
+       {01000, "Unspecified Error", " while gatewayed"},
+       {01001, "Bad Checksum", " while gatewayed"},
+       {01002, "Packet", " forwarded too many times"},
+       {01003, "Packet", " too large to be forwarded"},
+       {-1, 0, 0},
 };
 
 /*
  * Dump NS Error statistics structure.
  */
 };
 
 /*
  * Dump NS Error statistics structure.
  */
+/*ARGSUSED*/
+void
 nserr_stats(off, name)
 nserr_stats(off, name)
-       off_t off;
+       u_long off;
        char *name;
 {
        struct ns_errstat ns_errstat;
        char *name;
 {
        struct ns_errstat ns_errstat;
@@ -199,12 +253,9 @@ nserr_stats(off, name)
        register int histoprint = 1;
        int z;
 
        register int histoprint = 1;
        int z;
 
-       if (off == 0) {
-               printf("%sstat: symbol not in namelist\n", name);
+       if (off == 0)
                return;
                return;
-       }
-       klseek(kmem, off, 0);
-       read(kmem, (char *)&ns_errstat, sizeof (ns_errstat));
+       kread(off, (char *)&ns_errstat, sizeof (ns_errstat));
        printf("NS error statistics:\n");
        ANY(ns_errstat.ns_es_error, "call", " to ns_error");
        ANY(ns_errstat.ns_es_oldshort, "error",
        printf("NS error statistics:\n");
        ANY(ns_errstat.ns_es_error, "call", " to ns_error");
        ANY(ns_errstat.ns_es_oldshort, "error",
@@ -221,7 +272,8 @@ nserr_stats(off, name)
                        printf("Output Error Histogram:\n");
                        histoprint = 0;
                }
                        printf("Output Error Histogram:\n");
                        histoprint = 0;
                }
-               ANY(z, ns_errnames[j][0], ns_errnames[j][1]);
+               ns_erputil(z, ns_errstat.ns_es_codes[j]);
+
        }
        histoprint = 1;
        for(j = 0; j < NS_ERR_MAX; j ++) {
        }
        histoprint = 1;
        for(j = 0; j < NS_ERR_MAX; j ++) {
@@ -230,16 +282,44 @@ nserr_stats(off, name)
                        printf("Input Error Histogram:\n");
                        histoprint = 0;
                }
                        printf("Input Error Histogram:\n");
                        histoprint = 0;
                }
-               ANY(z, ns_errnames[j][0], ns_errnames[j][1]);
+               ns_erputil(z, ns_errstat.ns_es_codes[j]);
+       }
+}
+
+static void
+ns_erputil(z, c)
+       int z, c;
+{
+       int j;
+       char codebuf[30];
+       char *name, *where;
+
+       for(j = 0;; j ++) {
+               if ((name = ns_errnames[j].name) == 0)
+                       break;
+               if (ns_errnames[j].code == c)
+                       break;
        }
        }
+       if (name == 0)  {
+               if (c > 01000)
+                       where = "in transit";
+               else
+                       where = "at destination";
+               sprintf(codebuf, "Unknown XNS error code 0%o", c);
+               name = codebuf;
+       } else
+               where =  ns_errnames[j].where;
+       ANY(z, name, where);
 }
 }
+
 static struct sockaddr_ns ssns = {AF_NS};
 
 static struct sockaddr_ns ssns = {AF_NS};
 
+static
 char *ns_prpr(x)
 char *ns_prpr(x)
-struct ns_addr *x;
+       struct ns_addr *x;
 {
 {
-       extern char *ns_print();
        struct sockaddr_ns *sns = &ssns;
        struct sockaddr_ns *sns = &ssns;
+
        sns->sns_addr = *x;
        sns->sns_addr = *x;
-       return(ns_print(sns));
+       return(ns_print((struct sockaddr *)sns));
 }
 }