BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / netstat / main.c
index 9e08913..61a5dd8 100644 (file)
@@ -2,7 +2,33 @@
  * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983, 1988 Regents of the University of California.
  * All rights reserved.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -12,22 +38,22 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     5.20 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     5.23 (Berkeley) 7/1/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
-#include <sys/vmmac.h>
 #include <sys/socket.h>
 #include <sys/file.h>
 #include <sys/socket.h>
 #include <sys/file.h>
-#include <machine/pte.h>
-#include <ctype.h>
 #include <errno.h>
 #include <netdb.h>
 #include <nlist.h>
 #include <errno.h>
 #include <netdb.h>
 #include <nlist.h>
+#include <kvm.h>
 #include <stdio.h>
 #include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
 #include <paths.h>
 
 #include <paths.h>
 
-#define nl netstatnl
 struct nlist nl[] = {
 #define        N_MBSTAT        0
        { "_mbstat" },
 struct nlist nl[] = {
 #define        N_MBSTAT        0
        { "_mbstat" },
@@ -41,89 +67,65 @@ struct nlist nl[] = {
        { "_udb" },
 #define        N_UDPSTAT       5
        { "_udpstat" },
        { "_udb" },
 #define        N_UDPSTAT       5
        { "_udpstat" },
-#define        N_RAWCB         6
-       { "_rawcb" },
-#define        N_SYSMAP        7
-       { "_Sysmap" },
-#define        N_SYSSIZE       8
-       { "_Syssize" },
-#define        N_IFNET         9
+#define        N_IFNET         6
        { "_ifnet" },
        { "_ifnet" },
-#define        N_IMP           10
+#define        N_IMP           7
        { "_imp_softc" },
        { "_imp_softc" },
-#define        N_RTHOST        11
+#define        N_RTHOST        8
        { "_rthost" },
        { "_rthost" },
-#define        N_RTNET         12
+#define        N_RTNET         9
        { "_rtnet" },
        { "_rtnet" },
-#define        N_ICMPSTAT      13
+#define        N_ICMPSTAT      10
        { "_icmpstat" },
        { "_icmpstat" },
-#define        N_RTSTAT        14
+#define        N_RTSTAT        11
        { "_rtstat" },
        { "_rtstat" },
-#define        N_NFILE         15
+#define        N_NFILE         12
        { "_nfile" },
        { "_nfile" },
-#define        N_FILE          16
+#define        N_FILE          13
        { "_file" },
        { "_file" },
-#define        N_UNIXSW        17
+#define        N_UNIXSW        14
        { "_unixsw" },
        { "_unixsw" },
-#define N_RTHASHSIZE   18
+#define N_RTHASHSIZE   15
        { "_rthashsize" },
        { "_rthashsize" },
-#define N_IDP          19
+#define N_IDP          16
        { "_nspcb"},
        { "_nspcb"},
-#define N_IDPSTAT      20
+#define N_IDPSTAT      17
        { "_idpstat"},
        { "_idpstat"},
-#define N_SPPSTAT      21
+#define N_SPPSTAT      18
        { "_spp_istat"},
        { "_spp_istat"},
-#define N_NSERR                22
+#define N_NSERR                19
        { "_ns_errstat"},
        { "_ns_errstat"},
-#define        N_CLNPSTAT      23
+#define        N_CLNPSTAT      20
        { "_clnp_stat"},
        { "_clnp_stat"},
-#define        IN_TP           24
+#define        IN_TP           21
        { "_tp_inpcb" },
        { "_tp_inpcb" },
-#define        ISO_TP          25
+#define        ISO_TP          22
        { "_tp_isopcb" },
        { "_tp_isopcb" },
-#define        ISO_X25         26
-       { /*"_x25_isopcb"*/ "_file"}, /* fast gross hack to speed up */
-#define        N_TPSTAT        27
+#define        N_TPSTAT        23
        { "_tp_stat" },
        { "_tp_stat" },
-#define        N_X25STAT       28
-       { /*"_x25_stat"*/ "_file"},
-#define        N_ESISSTAT      29
+#define        N_ESISSTAT      24
        { "_esis_stat"},
        { "_esis_stat"},
-#define N_NIMP         30
+#define N_NIMP         25
        { "_nimp"},
        { "_nimp"},
-#define N_RTREE                31
+#define N_RTREE                26
        { "_radix_node_head"},
        { "_radix_node_head"},
-#define N_CLTP         32
+#define N_CLTP         27
        { "_cltb"},
        { "_cltb"},
-#define N_CLTPSTAT     33
+#define N_CLTPSTAT     28
        { "_cltpstat"},
        { "_cltpstat"},
-
-
-    /* BBN Internet protocol implementation */
-#define        N_TCP           23
-       { "_tcp" },
-#define        N_UDP           24
-       { "_udp" },
-#define N_RDP          25
-       { "_rdp" },
-#define        N_RDPSTAT       26
-       { "_rdpstat" },
-
        "",
 };
 
 /* internet protocols */
        "",
 };
 
 /* internet protocols */
-extern int protopr(), bbnprotopr();
+extern int protopr();
 extern int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
 /* ns protocols */
 extern int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
 /* ns protocols */
-extern int tcpstats(), udpstats(), ipstats(), icmpstats(), rdpstats();
 extern int nsprotopr();
 extern int spp_stats(), idp_stats(), nserr_stats();
 /* iso protocols */
 extern int iso_protopr();
 extern int tp_stats(), esis_stats(), clnp_stats(), cltp_stats();
 
 extern int nsprotopr();
 extern int spp_stats(), idp_stats(), nserr_stats();
 /* iso protocols */
 extern int iso_protopr();
 extern int tp_stats(), esis_stats(), clnp_stats(), cltp_stats();
 
-#define NULLPROTOX     ((struct protox *) 0)
 struct protox {
        u_char  pr_index;               /* index into nlist of cb head */
        u_char  pr_sindex;              /* index into nlist of stat block */
 struct protox {
        u_char  pr_index;               /* index into nlist of cb head */
        u_char  pr_sindex;              /* index into nlist of stat block */
@@ -131,9 +133,7 @@ struct protox {
        int     (*pr_cblocks)();        /* control blocks printing routine */
        int     (*pr_stats)();          /* statistics printing routine */
        char    *pr_name;               /* well-known name */
        int     (*pr_cblocks)();        /* control blocks printing routine */
        int     (*pr_stats)();          /* statistics printing routine */
        char    *pr_name;               /* well-known name */
-};
-
-struct  protox berkprotox[] = {
+} protox[] = {
        { N_TCB,        N_TCPSTAT,      1,      protopr,
          tcp_stats,    "tcp" },
        { N_UDB,        N_UDPSTAT,      1,      protopr,
        { N_TCB,        N_TCPSTAT,      1,      protopr,
          tcp_stats,    "tcp" },
        { N_UDB,        N_UDPSTAT,      1,      protopr,
@@ -148,23 +148,6 @@ struct  protox berkprotox[] = {
          0,            0 }
 };
 
          0,            0 }
 };
 
-struct protox bbnprotox[] = {
-       { N_TCP,        N_TCPSTAT,      1,      bbnprotopr,
-         tcpstats,     "tcp" },
-       { N_UDP,        N_UDPSTAT,      1,      bbnprotopr,
-         udpstats,     "udp" },
-       { N_RDP,        N_RDPSTAT,      1,      bbnprotopr,
-         rdpstats,     "rdp" },
-       { N_RAWCB,      0,              1,      bbnprotopr,
-         0,            "raw" },
-       { -1,           N_IPSTAT,       1,      0,
-         ipstats,      "ip" },
-       { -1,           N_ICMPSTAT,     1,      0,
-         icmpstats,    "icmp" },
-       { -1,           -1,             0,      0,
-         0,            0 }
-};
-
 struct protox nsprotox[] = {
        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
          idp_stats,    "idp" },
 struct protox nsprotox[] = {
        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
          idp_stats,    "idp" },
@@ -181,10 +164,6 @@ struct protox isoprotox[] = {
          tp_stats,     "tp" },
        { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
          cltp_stats,   "cltp" },
          tp_stats,     "tp" },
        { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
          cltp_stats,   "cltp" },
-#ifdef notdef
-       { ISO_X25,      N_X25STAT,      1,      x25_protopr,
-         x25_stats,    "x25" },
-#endif
        { -1,           N_CLNPSTAT,     1,       0,
          clnp_stats,   "clnp"},
        { -1,           N_ESISSTAT,     1,       0,
        { -1,           N_CLNPSTAT,     1,       0,
          clnp_stats,   "clnp"},
        { -1,           N_ESISSTAT,     1,       0,
@@ -193,11 +172,9 @@ struct protox isoprotox[] = {
          0,            0 }
 };
 
          0,            0 }
 };
 
-struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULLPROTOX };
+struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
 
 
-struct pte *Sysmap;
-
-char   *system = _PATH_UNIX;
+char   *vmunix = _PATH_UNIX;
 char   *kmemf;
 int    kmem;
 int    kflag;
 char   *kmemf;
 int    kmem;
 int    kflag;
@@ -218,12 +195,9 @@ int        unit;
 
 int    af = AF_UNSPEC;
 
 
 int    af = AF_UNSPEC;
 
-extern char *malloc();
-extern off_t lseek();
-
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char *argv[];
+       char **argv;
 {
        extern char *optarg;
        extern int optind;
 {
        extern char *optarg;
        extern int optind;
@@ -231,26 +205,18 @@ main(argc, argv)
        register struct protox *tp;     /* for printing cblocks & stats */
        struct protox *name2protox();   /* for -p */
        int ch;
        register struct protox *tp;     /* for printing cblocks & stats */
        struct protox *name2protox();   /* for -p */
        int ch;
+       void usage(); 
 
 
-       while ((ch = getopt(argc, argv, "AI:af:himnp:drstu")) != EOF)
+       while ((ch = getopt(argc, argv, "Aadf:hI:iM:mN:np:rstuw")) != EOF)
                switch((char)ch) {
                case 'A':
                switch((char)ch) {
                case 'A':
-                       Aflag++;
-                       break;
-               case 'I': {
-                       char *cp;
-
-                       iflag++;
-                       for (cp = interface = optarg; isalpha(*cp); cp++);
-                       unit = atoi(cp);
-                       *cp = '\0';
+                       Aflag = 1;
                        break;
                        break;
-               }
                case 'a':
                case 'a':
-                       aflag++;
+                       aflag = 1;
                        break;
                case 'd':
                        break;
                case 'd':
-                       dflag++;
+                       dflag = 1;
                        break;
                case 'f':
                        if (strcmp(optarg, "ns") == 0)
                        break;
                case 'f':
                        if (strcmp(optarg, "ns") == 0)
@@ -262,41 +228,63 @@ main(argc, argv)
                        else if (strcmp(optarg, "iso") == 0)
                                af = AF_ISO;
                        else {
                        else if (strcmp(optarg, "iso") == 0)
                                af = AF_ISO;
                        else {
-                               fprintf(stderr, "%s: unknown address family\n", optarg);
-                               exit(10);
+                               (void)fprintf(stderr,
+                                   "%s: unknown address family\n", optarg);
+                               exit(1);
                        }
                        break;
                case 'h':
                        }
                        break;
                case 'h':
-                       hflag++;
+                       hflag = 1;
+                       break;
+               case 'I': {
+                       char *cp;
+
+                       iflag = 1;
+                       for (cp = interface = optarg; isalpha(*cp); cp++);
+                       unit = atoi(cp);
+                       *cp = '\0';
                        break;
                        break;
+               }
                case 'i':
                case 'i':
-                       iflag++;
+                       iflag = 1;
+                       break;
+               case 'M':
+                       kmemf = optarg;
+                       kflag = 1;
                        break;
                case 'm':
                        break;
                case 'm':
-                       mflag++;
+                       mflag = 1;
+                       break;
+               case 'N':
+                       vmunix = optarg;
                        break;
                case 'n':
                        break;
                case 'n':
-                       nflag++;
+                       nflag = 1;
                        break;
                case 'p':
                        break;
                case 'p':
-                       if ((tp = name2protox(optarg)) == NULLPROTOX) {
-                               fprintf(stderr, "%s: unknown or uninstrumented protocol\n", optarg);
-                               exit(10);
+                       if ((tp = name2protox(optarg)) == NULL) {
+                               (void)fprintf(stderr,
+                                   "%s: unknown or uninstrumented protocol\n",
+                                   optarg);
+                               exit(1);
                        }
                        }
-                       pflag++;
+                       pflag = 1;
                        break;
                case 'r':
                        break;
                case 'r':
-                       rflag++;
+                       rflag = 1;
                        break;
                case 's':
                        break;
                case 's':
-                       sflag++;
+                       sflag = 1;
                        break;
                case 't':
                        break;
                case 't':
-                       tflag++;
+                       tflag = 1;
                        break;
                case 'u':
                        af = AF_UNIX;
                        break;
                        break;
                case 'u':
                        af = AF_UNIX;
                        break;
+               case 'w':
+                       interval = atoi(optarg);
+                       break;
                case '?':
                default:
                        usage();
                case '?':
                default:
                        usage();
@@ -304,29 +292,31 @@ main(argc, argv)
        argv += optind;
        argc -= optind;
 
        argv += optind;
        argc -= optind;
 
-       if (argc > 0) {
-               if (isdigit(argv[0][0])) {
-                       interval = atoi(argv[0]);
+#define        BACKWARD_COMPATIBILITY
+#ifdef BACKWARD_COMPATIBILITY
+       if (*argv) {
+               if (isdigit(**argv)) {
+                       interval = atoi(*argv);
                        if (interval <= 0)
                                usage();
                        if (interval <= 0)
                                usage();
-                       argv++, argc--;
-                       iflag++;
+                       ++argv;
+                       iflag = 1;
                }
                }
-               if (argc > 0) {
-                       system = *argv;
-                       argv++, argc--;
-                       if (argc > 0) {
+               if (*argv) {
+                       vmunix = *argv;
+                       if (*++argv) {
                                kmemf = *argv;
                                kmemf = *argv;
-                               kflag++;
+                               kflag = 1;
                        }
                }
        }
                        }
                }
        }
-       if (kvm_openfiles(system, kmemf, (char *)0) == -1) {
-               fprintf(stderr, "netstat(kvm_openfiles): %s\n", kvm_geterr());
+#endif
+       if (kvm_openfiles(vmunix, kmemf, NULL) == -1) {
+               fprintf(stderr, "netstat: kvm_openfiles: %s\n", kvm_geterr());
                exit(1);
        }
        if (kvm_nlist(nl) < 0 || nl[0].n_type == 0) {
                exit(1);
        }
        if (kvm_nlist(nl) < 0 || nl[0].n_type == 0) {
-               fprintf(stderr, "%s: no namelist\n", system);
+               fprintf(stderr, "%s: no namelist\n", vmunix);
                exit(1);
        }
        if (mflag) {
                exit(1);
        }
        if (mflag) {
@@ -366,21 +356,23 @@ main(argc, argv)
                exit(0);
        }
     if (af == AF_INET || af == AF_UNSPEC) {
                exit(0);
        }
     if (af == AF_INET || af == AF_UNSPEC) {
-       struct protox *head;
-
-       head = (nl[N_TCB].n_type == 0) ? bbnprotox : berkprotox;
        setprotoent(1);
        setservent(1);
        setprotoent(1);
        setservent(1);
+       while (p = getprotoent()) {
 
 
-       for (tp = head; tp->pr_name; tp++) {
-               if (tp->pr_wanted == 0)
+               for (tp = protox; tp->pr_name; tp++)
+                       if (strcmp(tp->pr_name, p->p_name) == 0)
+                               break;
+               if (tp->pr_name == 0 || tp->pr_wanted == 0)
                        continue;
                        continue;
-
                if (sflag) {
                        if (tp->pr_stats)
                if (sflag) {
                        if (tp->pr_stats)
-                           (*tp->pr_stats)(nl[tp->pr_sindex].n_value, tp->pr_name);
-               } else if (tp->pr_cblocks)
-                       (*tp->pr_cblocks)(nl[tp->pr_index].n_value, tp->pr_name);
+                               (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
+                                       p->p_name);
+               } else
+                       if (tp->pr_cblocks)
+                               (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
+                                       p->p_name);
        }
        endprotoent();
     }
        }
        endprotoent();
     }
@@ -416,12 +408,10 @@ main(argc, argv)
     exit(0);
 }
 
     exit(0);
 }
 
-
 char *
 plural(n)
        int n;
 {
 char *
 plural(n)
        int n;
 {
-
        return (n != 1 ? "s" : "");
 }
 
        return (n != 1 ? "s" : "");
 }
 
@@ -438,7 +428,7 @@ knownname(name)
            for (tp = *tpp; tp->pr_name; tp++)
                if (strcmp(tp->pr_name, name) == 0)
                        return(tp);
            for (tp = *tpp; tp->pr_name; tp++)
                if (strcmp(tp->pr_name, name) == 0)
                        return(tp);
-       return(NULLPROTOX);
+       return(NULL);
 }
 
 /*
 }
 
 /*
@@ -469,11 +459,19 @@ name2protox(name)
                        }
        }
        endprotoent();
                        }
        }
        endprotoent();
-       return(NULLPROTOX);
+       return(NULL);
 }
 
 }
 
+void
 usage()
 {
 usage()
 {
-       fputs("usage: netstat [-Aan] [-f address_family] [system] [core]\n               [-himnrs] [-f address_family] [system] [core]\n               [-n] [-I interface] interval [system] [core]\n", stderr);
+       (void)fprintf(stderr,
+"usage: netstat [-Aan] [-f address_family] [-M core] [-N system]\n");
+       (void)fprintf(stderr,
+"               [-himnrs] [-f address_family] [-M core] [-N system]\n");
+       (void)fprintf(stderr,
+"               [-n] [-I interface] [-M core] [-N system] [-w wait]\n");
+       (void)fprintf(stderr,
+"               [-M core] [-N system] [-p protocol]\n");
        exit(1);
 }
        exit(1);
 }