BSD 4_4 release
[unix-history] / usr / src / usr.bin / netstat / main.c
index 4203064..447c611 100644 (file)
@@ -1,18 +1,44 @@
 /*
 /*
- * Copyright (c) 1983, 1988 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1983, 1988, 1993
+ *     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
 char copyright[] =
  */
 
 #ifndef lint
 char copyright[] =
-"@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
- All rights reserved.\n";
+"@(#) Copyright (c) 1983, 1988, 1993\n\
      Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     5.32 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     8.1 (Berkeley) 6/12/93";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -98,17 +124,6 @@ struct nlist nl[] = {
        { "_mrttable" },
 #define N_VIFTABLE     30
        { "_viftable" },
        { "_mrttable" },
 #define N_VIFTABLE     30
        { "_viftable" },
-
-    /* BBN Internet protocol implementation */
-#define        N_TCP           23
-       { "_tcp" },
-#define        N_UDP           24
-       { "_udp" },
-#define N_RDP          25
-       { "_rdp" },
-#define        N_RDPSTAT       26
-       { "_rdpstat" },
-
        "",
 };
 
        "",
 };
 
@@ -119,9 +134,7 @@ struct protox {
        void    (*pr_cblocks)();        /* control blocks printing routine */
        void    (*pr_stats)();          /* statistics printing routine */
        char    *pr_name;               /* well-known name */
        void    (*pr_cblocks)();        /* control blocks printing routine */
        void    (*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,
@@ -136,23 +149,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" },
@@ -206,7 +202,7 @@ main(argc, argv)
                prog = argv[0];
        af = AF_UNSPEC;
 
                prog = argv[0];
        af = AF_UNSPEC;
 
-       while ((ch = getopt(argc, argv, "AaBdf:hI:iM:mN:np:rstuw:")) != EOF)
+       while ((ch = getopt(argc, argv, "AaBdf:I:iM:mN:np:rstuw:")) != EOF)
                switch(ch) {
                case 'A':
                        Aflag = 1;
                switch(ch) {
                case 'A':
                        Aflag = 1;
@@ -236,9 +232,6 @@ main(argc, argv)
                                exit(1);
                        }
                        break;
                                exit(1);
                        }
                        break;
-               case 'h':
-                       hflag = 1;
-                       break;
                case 'I': {
                        char *cp;
 
                case 'I': {
                        char *cp;
 
@@ -343,10 +336,6 @@ main(argc, argv)
                        printf("%s: no stats routine\n", tp->pr_name);
                exit(0);
        }
                        printf("%s: no stats routine\n", tp->pr_name);
                exit(0);
        }
-       if (hflag) {
-               hostpr(nl[N_IMP].n_value, nl[N_NIMP].n_value);
-               exit(0);
-       }
        /*
         * Keep file descriptors open to avoid overhead
         * of open/close on each call to get* routines.
        /*
         * Keep file descriptors open to avoid overhead
         * of open/close on each call to get* routines.
@@ -374,13 +363,50 @@ main(argc, argv)
                            nl[N_VIFTABLE].n_value);
                exit(0);
        }
                            nl[N_VIFTABLE].n_value);
                exit(0);
        }
+       if (af == AF_INET || af == AF_UNSPEC) {
+               setprotoent(1);
+               setservent(1);
+               /* ugh, this is O(MN) ... why do we do this? */
+               while (p = getprotoent()) {
+                       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;
+                       printproto(tp, p->p_name);
+               }
+               endprotoent();
+       }
+       if (af == AF_NS || af == AF_UNSPEC)
+               for (tp = nsprotox; tp->pr_name; tp++)
+                       printproto(tp, tp->pr_name);
+       if (af == AF_ISO || af == AF_UNSPEC)
+               for (tp = isoprotox; tp->pr_name; tp++)
+                       printproto(tp, tp->pr_name);
+       if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
+               unixpr(nl[N_UNIXSW].n_value);
+       exit(0);
+}
 
 
-
-               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);
+/*
+ * Print out protocol statistics or control blocks (per sflag).
+ * If the interface was not specifically requested, and the symbol
+ * is not in the namelist, ignore this one.
+ */
+static void
+printproto(tp, name)
+       register struct protox *tp;
+       char *name;
+{
+       void (*pr)();
+       u_long off;
+
+       if (sflag) {
+               pr = tp->pr_stats;
+               off = nl[tp->pr_sindex].n_value;
+       } else {
+               pr = tp->pr_cblocks;
+               off = nl[tp->pr_index].n_value;
        }
        if (pr != NULL && (off || af != AF_UNSPEC))
                (*pr)(off, name);
        }
        if (pr != NULL && (off || af != AF_UNSPEC))
                (*pr)(off, name);