home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / netstat / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-01  |  10.6 KB  |  478 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1983, 1988 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)main.c    5.23 (Berkeley) 7/1/91";
  42. #endif /* not lint */
  43.  
  44. #include <sys/param.h>
  45. #include <sys/socket.h>
  46. #include <sys/file.h>
  47. #include <errno.h>
  48. #include <netdb.h>
  49. #include <nlist.h>
  50. #include <kvm.h>
  51. #include <stdio.h>
  52. #include <ctype.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <paths.h>
  56.  
  57. struct nlist nl[] = {
  58. #define    N_MBSTAT    0
  59.     { "_mbstat" },
  60. #define    N_IPSTAT    1
  61.     { "_ipstat" },
  62. #define    N_TCB        2
  63.     { "_tcb" },
  64. #define    N_TCPSTAT    3
  65.     { "_tcpstat" },
  66. #define    N_UDB        4
  67.     { "_udb" },
  68. #define    N_UDPSTAT    5
  69.     { "_udpstat" },
  70. #define    N_IFNET        6
  71.     { "_ifnet" },
  72. #define    N_IMP        7
  73.     { "_imp_softc" },
  74. #define    N_RTHOST    8
  75.     { "_rthost" },
  76. #define    N_RTNET        9
  77.     { "_rtnet" },
  78. #define    N_ICMPSTAT    10
  79.     { "_icmpstat" },
  80. #define    N_RTSTAT    11
  81.     { "_rtstat" },
  82. #define    N_NFILE        12
  83.     { "_nfile" },
  84. #define    N_FILE        13
  85.     { "_file" },
  86. #define    N_UNIXSW    14
  87.     { "_unixsw" },
  88. #define N_RTHASHSIZE    15
  89.     { "_rthashsize" },
  90. #define N_IDP        16
  91.     { "_nspcb"},
  92. #define N_IDPSTAT    17
  93.     { "_idpstat"},
  94. #define N_SPPSTAT    18
  95.     { "_spp_istat"},
  96. #define N_NSERR        19
  97.     { "_ns_errstat"},
  98. #define    N_CLNPSTAT    20
  99.     { "_clnp_stat"},
  100. #define    IN_TP        21
  101.     { "_tp_inpcb" },
  102. #define    ISO_TP        22
  103.     { "_tp_isopcb" },
  104. #define    N_TPSTAT    23
  105.     { "_tp_stat" },
  106. #define    N_ESISSTAT    24
  107.     { "_esis_stat"},
  108. #define N_NIMP        25
  109.     { "_nimp"},
  110. #define N_RTREE        26
  111.     { "_radix_node_head"},
  112. #define N_CLTP        27
  113.     { "_cltb"},
  114. #define N_CLTPSTAT    28
  115.     { "_cltpstat"},
  116.     "",
  117. };
  118.  
  119. /* internet protocols */
  120. extern    int protopr();
  121. extern    int tcp_stats(), udp_stats(), ip_stats(), icmp_stats();
  122. /* ns protocols */
  123. extern    int nsprotopr();
  124. extern    int spp_stats(), idp_stats(), nserr_stats();
  125. /* iso protocols */
  126. extern    int iso_protopr();
  127. extern    int tp_stats(), esis_stats(), clnp_stats(), cltp_stats();
  128.  
  129. struct protox {
  130.     u_char    pr_index;        /* index into nlist of cb head */
  131.     u_char    pr_sindex;        /* index into nlist of stat block */
  132.     u_char    pr_wanted;        /* 1 if wanted, 0 otherwise */
  133.     int    (*pr_cblocks)();    /* control blocks printing routine */
  134.     int    (*pr_stats)();        /* statistics printing routine */
  135.     char    *pr_name;        /* well-known name */
  136. } protox[] = {
  137.     { N_TCB,    N_TCPSTAT,    1,    protopr,
  138.       tcp_stats,    "tcp" },
  139.     { N_UDB,    N_UDPSTAT,    1,    protopr,
  140.       udp_stats,    "udp" },
  141.     { IN_TP,    N_TPSTAT,    1,    protopr,
  142.       tp_stats,    "tpip" },
  143.     { -1,        N_IPSTAT,    1,    0,
  144.       ip_stats,    "ip" },
  145.     { -1,        N_ICMPSTAT,    1,    0,
  146.       icmp_stats,    "icmp" },
  147.     { -1,        -1,        0,    0,
  148.       0,        0 }
  149. };
  150.  
  151. struct protox nsprotox[] = {
  152.     { N_IDP,    N_IDPSTAT,    1,    nsprotopr,
  153.       idp_stats,    "idp" },
  154.     { N_IDP,    N_SPPSTAT,    1,    nsprotopr,
  155.       spp_stats,    "spp" },
  156.     { -1,        N_NSERR,    1,    0,
  157.       nserr_stats,    "ns_err" },
  158.     { -1,        -1,        0,    0,
  159.       0,        0 }
  160. };
  161.  
  162. struct protox isoprotox[] = {
  163.     { ISO_TP,    N_TPSTAT,    1,    iso_protopr,
  164.       tp_stats,    "tp" },
  165.     { N_CLTP,    N_CLTPSTAT,    1,    iso_protopr,
  166.       cltp_stats,    "cltp" },
  167.     { -1,        N_CLNPSTAT,    1,     0,
  168.       clnp_stats,    "clnp"},
  169.     { -1,        N_ESISSTAT,    1,     0,
  170.       esis_stats,    "esis"},
  171.     { -1,        -1,        0,    0,
  172.       0,        0 }
  173. };
  174.  
  175. struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
  176.  
  177. char    *vmunix = _PATH_UNIX;
  178. char    *kmemf;
  179. int    kmem;
  180. int    kflag;
  181. int    Aflag;
  182. int    aflag;
  183. int    hflag;
  184. int    iflag;
  185. int    mflag;
  186. int    nflag;
  187. int    pflag;
  188. int    rflag;
  189. int    sflag;
  190. int    tflag;
  191. int    dflag;
  192. int    interval;
  193. char    *interface;
  194. int    unit;
  195.  
  196. int    af = AF_UNSPEC;
  197.  
  198. main(argc, argv)
  199.     int argc;
  200.     char **argv;
  201. {
  202.     extern char *optarg;
  203.     extern int optind;
  204.     register struct protoent *p;
  205.     register struct protox *tp;    /* for printing cblocks & stats */
  206.     struct protox *name2protox();    /* for -p */
  207.     int ch;
  208.     void usage(); 
  209.  
  210.     while ((ch = getopt(argc, argv, "Aadf:hI:iM:mN:np:rstuw")) != EOF)
  211.         switch((char)ch) {
  212.         case 'A':
  213.             Aflag = 1;
  214.             break;
  215.         case 'a':
  216.             aflag = 1;
  217.             break;
  218.         case 'd':
  219.             dflag = 1;
  220.             break;
  221.         case 'f':
  222.             if (strcmp(optarg, "ns") == 0)
  223.                 af = AF_NS;
  224.             else if (strcmp(optarg, "inet") == 0)
  225.                 af = AF_INET;
  226.             else if (strcmp(optarg, "unix") == 0)
  227.                 af = AF_UNIX;
  228.             else if (strcmp(optarg, "iso") == 0)
  229.                 af = AF_ISO;
  230.             else {
  231.                 (void)fprintf(stderr,
  232.                     "%s: unknown address family\n", optarg);
  233.                 exit(1);
  234.             }
  235.             break;
  236.         case 'h':
  237.             hflag = 1;
  238.             break;
  239.         case 'I': {
  240.             char *cp;
  241.  
  242.             iflag = 1;
  243.             for (cp = interface = optarg; isalpha(*cp); cp++);
  244.             unit = atoi(cp);
  245.             *cp = '\0';
  246.             break;
  247.         }
  248.         case 'i':
  249.             iflag = 1;
  250.             break;
  251.         case 'M':
  252.             kmemf = optarg;
  253.             kflag = 1;
  254.             break;
  255.         case 'm':
  256.             mflag = 1;
  257.             break;
  258.         case 'N':
  259.             vmunix = optarg;
  260.             break;
  261.         case 'n':
  262.             nflag = 1;
  263.             break;
  264.         case 'p':
  265.             if ((tp = name2protox(optarg)) == NULL) {
  266.                 (void)fprintf(stderr,
  267.                     "%s: unknown or uninstrumented protocol\n",
  268.                     optarg);
  269.                 exit(1);
  270.             }
  271.             pflag = 1;
  272.             break;
  273.         case 'r':
  274.             rflag = 1;
  275.             break;
  276.         case 's':
  277.             sflag = 1;
  278.             break;
  279.         case 't':
  280.             tflag = 1;
  281.             break;
  282.         case 'u':
  283.             af = AF_UNIX;
  284.             break;
  285.         case 'w':
  286.             interval = atoi(optarg);
  287.             break;
  288.         case '?':
  289.         default:
  290.             usage();
  291.         }
  292.     argv += optind;
  293.     argc -= optind;
  294.  
  295. #define    BACKWARD_COMPATIBILITY
  296. #ifdef    BACKWARD_COMPATIBILITY
  297.     if (*argv) {
  298.         if (isdigit(**argv)) {
  299.             interval = atoi(*argv);
  300.             if (interval <= 0)
  301.                 usage();
  302.             ++argv;
  303.             iflag = 1;
  304.         }
  305.         if (*argv) {
  306.             vmunix = *argv;
  307.             if (*++argv) {
  308.                 kmemf = *argv;
  309.                 kflag = 1;
  310.             }
  311.         }
  312.     }
  313. #endif
  314.     if (kvm_openfiles(vmunix, kmemf, NULL) == -1) {
  315.         fprintf(stderr, "netstat: kvm_openfiles: %s\n", kvm_geterr());
  316.         exit(1);
  317.     }
  318.     if (kvm_nlist(nl) < 0 || nl[0].n_type == 0) {
  319.         fprintf(stderr, "%s: no namelist\n", vmunix);
  320.         exit(1);
  321.     }
  322.     if (mflag) {
  323.         mbpr((off_t)nl[N_MBSTAT].n_value);
  324.         exit(0);
  325.     }
  326.     if (pflag) {
  327.         if (tp->pr_stats)
  328.             (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  329.                 tp->pr_name);
  330.         else
  331.             printf("%s: no stats routine\n", tp->pr_name);
  332.         exit(0);
  333.     }
  334.     if (hflag) {
  335.         hostpr(nl[N_IMP].n_value, nl[N_NIMP].n_value);
  336.         exit(0);
  337.     }
  338.     /*
  339.      * Keep file descriptors open to avoid overhead
  340.      * of open/close on each call to get* routines.
  341.      */
  342.     sethostent(1);
  343.     setnetent(1);
  344.     if (iflag) {
  345.         intpr(interval, nl[N_IFNET].n_value);
  346.         exit(0);
  347.     }
  348.     if (rflag) {
  349.         if (sflag)
  350.             rt_stats((off_t)nl[N_RTSTAT].n_value);
  351.         else
  352.             routepr((off_t)nl[N_RTHOST].n_value, 
  353.                 (off_t)nl[N_RTNET].n_value,
  354.                 (off_t)nl[N_RTHASHSIZE].n_value,
  355.                 (off_t)nl[N_RTREE].n_value);
  356.         exit(0);
  357.     }
  358.     if (af == AF_INET || af == AF_UNSPEC) {
  359.     setprotoent(1);
  360.     setservent(1);
  361.     while (p = getprotoent()) {
  362.  
  363.         for (tp = protox; tp->pr_name; tp++)
  364.             if (strcmp(tp->pr_name, p->p_name) == 0)
  365.                 break;
  366.         if (tp->pr_name == 0 || tp->pr_wanted == 0)
  367.             continue;
  368.         if (sflag) {
  369.             if (tp->pr_stats)
  370.                 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  371.                     p->p_name);
  372.         } else
  373.             if (tp->pr_cblocks)
  374.                 (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
  375.                     p->p_name);
  376.     }
  377.     endprotoent();
  378.     }
  379.     if (af == AF_NS || af == AF_UNSPEC) {
  380.     for (tp = nsprotox; tp->pr_name; tp++) {
  381.         if (sflag) {
  382.             if (tp->pr_stats)
  383.                 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  384.                     tp->pr_name);
  385.         } else
  386.             if (tp->pr_cblocks)
  387.                 (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
  388.                     tp->pr_name);
  389.     }
  390.     }
  391.     if (af == AF_ISO || af == AF_UNSPEC) {
  392.     for (tp = isoprotox; tp->pr_name; tp++) {
  393.         if (sflag) {
  394.             if (tp->pr_stats)
  395.                 (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
  396.                     tp->pr_name);
  397.         } else
  398.             if (tp->pr_cblocks)
  399.                 (*tp->pr_cblocks)(nl[tp->pr_index].n_value,
  400.                     tp->pr_name);
  401.     }
  402.     }
  403.     if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
  404.         unixpr((off_t)nl[N_NFILE].n_value, (off_t)nl[N_FILE].n_value,
  405.         (struct protosw *)nl[N_UNIXSW].n_value);
  406.     if (af == AF_UNSPEC && sflag)
  407.     impstats(nl[N_IMP].n_value, nl[N_NIMP].n_value);
  408.     exit(0);
  409. }
  410.  
  411. char *
  412. plural(n)
  413.     int n;
  414. {
  415.     return (n != 1 ? "s" : "");
  416. }
  417.  
  418. /*
  419.  * Find the protox for the given "well-known" name.
  420.  */
  421. struct protox *
  422. knownname(name)
  423.     char *name;
  424. {
  425.     struct protox **tpp, *tp;
  426.  
  427.     for (tpp = protoprotox; *tpp; tpp++)
  428.         for (tp = *tpp; tp->pr_name; tp++)
  429.         if (strcmp(tp->pr_name, name) == 0)
  430.             return(tp);
  431.     return(NULL);
  432. }
  433.  
  434. /*
  435.  * Find the protox corresponding to name.
  436.  */
  437. struct protox *
  438. name2protox(name)
  439.     char *name;
  440. {
  441.     struct protox *tp;
  442.     char **alias;            /* alias from p->aliases */
  443.     struct protoent *p;
  444.  
  445.     /*
  446.      * Try to find the name in the list of "well-known" names. If that
  447.      * fails, check if name is an alias for an Internet protocol.
  448.      */
  449.     if (tp = knownname(name))
  450.         return(tp);
  451.  
  452.     setprotoent(1);            /* make protocol lookup cheaper */
  453.     while (p = getprotoent()) {
  454.         /* assert: name not same as p->name */
  455.         for (alias = p->p_aliases; *alias; alias++)
  456.             if (strcmp(name, *alias) == 0) {
  457.                 endprotoent();
  458.                 return(knownname(p->p_name));
  459.             }
  460.     }
  461.     endprotoent();
  462.     return(NULL);
  463. }
  464.  
  465. void
  466. usage()
  467. {
  468.     (void)fprintf(stderr,
  469. "usage: netstat [-Aan] [-f address_family] [-M core] [-N system]\n");
  470.     (void)fprintf(stderr,
  471. "               [-himnrs] [-f address_family] [-M core] [-N system]\n");
  472.     (void)fprintf(stderr,
  473. "               [-n] [-I interface] [-M core] [-N system] [-w wait]\n");
  474.     (void)fprintf(stderr,
  475. "               [-M core] [-N system] [-p protocol]\n");
  476.     exit(1);
  477. }
  478.