[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / netstat

Annotation of src/usr.bin/netstat/main.c, Revision 1.55

1.55    ! deraadt     1: /*     $OpenBSD: main.c,v 1.54 2005/03/30 08:23:47 jaredy Exp $        */
1.2       deraadt     2: /*     $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1983, 1988, 1993
                      6:  *     Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.36      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: #ifndef lint
                     34: char copyright[] =
                     35: "@(#) Copyright (c) 1983, 1988, 1993\n\
                     36:        Regents of the University of California.  All rights reserved.\n";
                     37: #endif /* not lint */
                     38:
                     39: #ifndef lint
                     40: #if 0
                     41: static char sccsid[] = "from: @(#)main.c       8.4 (Berkeley) 3/1/94";
                     42: #else
1.55    ! deraadt    43: static char *rcsid = "$OpenBSD: main.c,v 1.54 2005/03/30 08:23:47 jaredy Exp $";
1.1       deraadt    44: #endif
                     45: #endif /* not lint */
                     46:
                     47: #include <sys/param.h>
                     48: #include <sys/file.h>
                     49: #include <sys/protosw.h>
                     50: #include <sys/socket.h>
                     51:
                     52: #include <netinet/in.h>
                     53:
                     54: #include <ctype.h>
                     55: #include <errno.h>
                     56: #include <kvm.h>
                     57: #include <limits.h>
                     58: #include <netdb.h>
                     59: #include <nlist.h>
                     60: #include <paths.h>
                     61: #include <stdio.h>
                     62: #include <stdlib.h>
                     63: #include <string.h>
                     64: #include <unistd.h>
                     65: #include "netstat.h"
                     66:
                     67: struct nlist nl[] = {
1.55    ! deraadt    68: #define N_MBSTAT       0
1.1       deraadt    69:        { "_mbstat" },
1.55    ! deraadt    70: #define N_IPSTAT       1
1.1       deraadt    71:        { "_ipstat" },
1.55    ! deraadt    72: #define N_TCBTABLE     2
1.1       deraadt    73:        { "_tcbtable" },
1.55    ! deraadt    74: #define N_TCPSTAT      3
1.1       deraadt    75:        { "_tcpstat" },
1.55    ! deraadt    76: #define N_UDBTABLE     4
1.1       deraadt    77:        { "_udbtable" },
1.55    ! deraadt    78: #define N_UDPSTAT      5
1.1       deraadt    79:        { "_udpstat" },
1.55    ! deraadt    80: #define N_IFNET                6
1.1       deraadt    81:        { "_ifnet" },
1.55    ! deraadt    82: #define N_ICMPSTAT     7
1.1       deraadt    83:        { "_icmpstat" },
1.55    ! deraadt    84: #define N_RTSTAT       8
1.1       deraadt    85:        { "_rtstat" },
1.55    ! deraadt    86: #define N_UNIXSW       9
1.1       deraadt    87:        { "_unixsw" },
1.55    ! deraadt    88: #define N_IDP          10
1.1       deraadt    89:        { "_nspcb"},
1.55    ! deraadt    90: #define N_IDPSTAT      11
1.1       deraadt    91:        { "_idpstat"},
1.55    ! deraadt    92: #define N_SPPSTAT      12
1.1       deraadt    93:        { "_spp_istat"},
1.55    ! deraadt    94: #define N_NSERR                13
1.1       deraadt    95:        { "_ns_errstat"},
1.55    ! deraadt    96: #define N_RTREE                14
1.1       deraadt    97:        { "_rt_tables"},
1.55    ! deraadt    98: #define N_FILE         15
1.1       deraadt    99:        { "_file" },
1.55    ! deraadt   100: #define N_IGMPSTAT     16
1.1       deraadt   101:        { "_igmpstat" },
1.55    ! deraadt   102: #define N_MRTPROTO     17
1.1       deraadt   103:        { "_ip_mrtproto" },
1.55    ! deraadt   104: #define N_MRTSTAT      18
1.1       deraadt   105:        { "_mrtstat" },
1.55    ! deraadt   106: #define N_MFCHASHTBL   19
1.1       deraadt   107:        { "_mfchashtbl" },
1.55    ! deraadt   108: #define N_MFCHASH      20
1.1       deraadt   109:        { "_mfchash" },
1.55    ! deraadt   110: #define N_VIFTABLE     21
1.1       deraadt   111:        { "_viftable" },
1.55    ! deraadt   112: #define N_IPX          22
1.4       mickey    113:        { "_ipxcbtable"},
1.55    ! deraadt   114: #define N_IPXSTAT      23
1.4       mickey    115:        { "_ipxstat"},
1.55    ! deraadt   116: #define N_SPXSTAT      24
1.4       mickey    117:        { "_spx_istat"},
1.55    ! deraadt   118: #define N_AHSTAT       25
1.8       angelos   119:        { "_ahstat"},
1.55    ! deraadt   120: #define N_ESPSTAT      26
1.8       angelos   121:        { "_espstat"},
1.55    ! deraadt   122: #define N_IP4STAT      27
1.21      angelos   123:        { "_ipipstat"},
1.55    ! deraadt   124: #define N_DDPSTAT      28
1.12      denny     125:        { "_ddpstat"},
1.55    ! deraadt   126: #define N_DDPCB                29
1.12      denny     127:        { "_ddpcb"},
1.55    ! deraadt   128: #define N_ETHERIPSTAT  30
1.18      angelos   129:        { "_etheripstat"},
1.55    ! deraadt   130: #define N_IP6STAT      31
1.19      itojun    131:        { "_ip6stat" },
1.55    ! deraadt   132: #define N_ICMP6STAT    32
1.19      itojun    133:        { "_icmp6stat" },
1.55    ! deraadt   134: #define N_PIM6STAT     33
1.19      itojun    135:        { "_pim6stat" },
1.55    ! deraadt   136: #define N_MRT6PROTO    34
1.19      itojun    137:        { "_ip6_mrtproto" },
1.55    ! deraadt   138: #define N_MRT6STAT     35
1.19      itojun    139:        { "_mrt6stat" },
1.55    ! deraadt   140: #define N_MF6CTABLE    36
1.19      itojun    141:        { "_mf6ctable" },
1.55    ! deraadt   142: #define N_MIF6TABLE    37
1.19      itojun    143:        { "_mif6table" },
1.55    ! deraadt   144: #define N_MBPOOL       38
1.25      provos    145:        { "_mbpool" },
1.55    ! deraadt   146: #define N_MCLPOOL      39
1.25      provos    147:        { "_mclpool" },
1.55    ! deraadt   148: #define N_IPCOMPSTAT   40
1.26      jjbg      149:        { "_ipcompstat" },
1.55    ! deraadt   150: #define N_RIP6STAT     41
1.32      itojun    151:        { "_rip6stat" },
1.55    ! deraadt   152: #define N_CARPSTAT     42
1.39      mcbride   153:        { "_carpstats" },
1.55    ! deraadt   154: #define N_RAWIPTABLE   43
1.40      markus    155:        { "_rawcbtable" },
1.55    ! deraadt   156: #define N_RAWIP6TABLE  44
1.40      markus    157:        { "_rawin6pcbtable" },
1.55    ! deraadt   158: #define N_PFSYNCSTAT   45
1.43      mcbride   159:        { "_pfsyncstats" },
1.55    ! deraadt   160: #define N_PIMSTAT      46
1.50      mcbride   161:        { "_pimstat" },
1.55    ! deraadt   162:        { ""}
1.1       deraadt   163: };
                    164:
                    165: struct protox {
1.38      deraadt   166:        u_char  pr_index;                       /* index into nlist of cb head */
                    167:        u_char  pr_sindex;                      /* index into nlist of stat block */
                    168:        u_char  pr_wanted;                      /* 1 if wanted, 0 otherwise */
                    169:        void    (*pr_cblocks)(u_long, char *);  /* control blocks printing routine */
                    170:        void    (*pr_stats)(u_long, char *);    /* statistics printing routine */
                    171:        char    *pr_name;                       /* well-known name */
1.1       deraadt   172: } protox[] = {
                    173:        { N_TCBTABLE,   N_TCPSTAT,      1,      protopr,
                    174:          tcp_stats,    "tcp" },
                    175:        { N_UDBTABLE,   N_UDPSTAT,      1,      protopr,
                    176:          udp_stats,    "udp" },
1.40      markus    177:        { N_RAWIPTABLE, N_IPSTAT,       1,      protopr,
1.1       deraadt   178:          ip_stats,     "ip" },
                    179:        { -1,           N_ICMPSTAT,     1,      0,
                    180:          icmp_stats,   "icmp" },
                    181:        { -1,           N_IGMPSTAT,     1,      0,
                    182:          igmp_stats,   "igmp" },
1.8       angelos   183:        { -1,           N_AHSTAT,       1,      0,
1.15      angelos   184:          ah_stats,     "ah" },
1.8       angelos   185:        { -1,           N_ESPSTAT,      1,      0,
1.15      angelos   186:          esp_stats,    "esp" },
1.8       angelos   187:        { -1,           N_IP4STAT,      1,      0,
1.21      angelos   188:          ipip_stats,   "ipencap" },
1.18      angelos   189:        { -1,           N_ETHERIPSTAT,  1,      0,
                    190:          etherip_stats,"etherip" },
1.29      mickey    191:        { -1,           N_IPCOMPSTAT,   1,      0,
                    192:          ipcomp_stats, "ipcomp" },
1.44      deraadt   193:        { -1,           N_CARPSTAT,     1,      0,
                    194:          carp_stats,   "carp" },
                    195:        { -1,           N_PFSYNCSTAT,   1,      0,
                    196:          pfsync_stats, "pfsync" },
1.50      mcbride   197:        { -1,           N_PIMSTAT,      1,      0,
                    198:          pim_stats,    "pim" },
1.1       deraadt   199:        { -1,           -1,             0,      0,
                    200:          0,            0 }
                    201: };
                    202:
1.19      itojun    203: #ifdef INET6
                    204: struct protox ip6protox[] = {
1.22      itojun    205:        { N_TCBTABLE,   N_TCPSTAT,      1,      ip6protopr,
1.23      itojun    206:          0,            "tcp" },
1.22      itojun    207:        { N_UDBTABLE,   N_UDPSTAT,      1,      ip6protopr,
1.23      itojun    208:          0,            "udp" },
1.40      markus    209:        { N_RAWIP6TABLE,N_IP6STAT,      1,      ip6protopr,
1.19      itojun    210:          ip6_stats,    "ip6" },
                    211:        { -1,           N_ICMP6STAT,    1,      0,
                    212:          icmp6_stats,  "icmp6" },
                    213:        { -1,           N_PIM6STAT,     1,      0,
                    214:          pim6_stats,   "pim6" },
1.32      itojun    215:        { -1,           N_RIP6STAT,     1,      0,
                    216:          rip6_stats,   "rip6" },
1.19      itojun    217:        { -1,           -1,             0,      0,
                    218:          0,            0 }
                    219: };
                    220: #endif
                    221:
1.4       mickey    222: struct protox ipxprotox[] = {
                    223:        { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
                    224:          ipx_stats,    "ipx" },
                    225:        { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
                    226:          spx_stats,    "spx" },
                    227:        { -1,           -1,             0,      0,
                    228:          0,            0 }
                    229: };
                    230:
1.1       deraadt   231: struct protox nsprotox[] = {
                    232:        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
                    233:          idp_stats,    "idp" },
                    234:        { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
                    235:          spp_stats,    "spp" },
                    236:        { -1,           N_NSERR,        1,      0,
                    237:          nserr_stats,  "ns_err" },
                    238:        { -1,           -1,             0,      0,
                    239:          0,            0 }
                    240: };
                    241:
1.12      denny     242: struct protox atalkprotox[] = {
                    243:        { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
                    244:          ddp_stats,    "ddp" },
                    245:        { -1,           -1,             0,      0,
                    246:          0,            0 }
                    247: };
                    248:
1.19      itojun    249: #ifndef INET6
1.34      deraadt   250: struct protox *protoprotox[] = {
1.48      henning   251:        protox, ipxprotox, nsprotox, atalkprotox, NULL
1.34      deraadt   252: };
1.19      itojun    253: #else
1.34      deraadt   254: struct protox *protoprotox[] = {
1.48      henning   255:        protox, ip6protox, ipxprotox, nsprotox, atalkprotox, NULL
1.34      deraadt   256: };
1.19      itojun    257: #endif
1.1       deraadt   258:
1.30      millert   259: static void printproto(struct protox *, char *);
                    260: static void usage(void);
                    261: static struct protox *name2protox(char *);
                    262: static struct protox *knownname(char *);
1.1       deraadt   263:
                    264: kvm_t *kvmd;
                    265:
                    266: int
1.34      deraadt   267: main(int argc, char *argv[])
1.1       deraadt   268: {
                    269:        extern char *optarg;
                    270:        extern int optind;
1.28      mpech     271:        struct protoent *p;
                    272:        struct protox *tp = NULL; /* for printing cblocks & stats */
1.1       deraadt   273:        int ch;
                    274:        char *nlistf = NULL, *memf = NULL;
                    275:        char buf[_POSIX2_LINE_MAX];
                    276:
                    277:        af = AF_UNSPEC;
                    278:
1.54      jaredy    279:        while ((ch = getopt(argc, argv, "Aabdf:gI:ilM:mN:np:qrstuvw:")) != -1)
1.31      deraadt   280:                switch (ch) {
1.1       deraadt   281:                case 'A':
                    282:                        Aflag = 1;
                    283:                        break;
                    284:                case 'a':
                    285:                        aflag = 1;
1.24      camield   286:                        break;
                    287:                case 'b':
                    288:                        bflag = 1;
1.1       deraadt   289:                        break;
                    290:                case 'd':
                    291:                        dflag = 1;
                    292:                        break;
                    293:                case 'f':
1.4       mickey    294:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   295:                                af = AF_INET;
1.19      itojun    296:                        else if (strcmp(optarg, "inet6") == 0)
                    297:                                af = AF_INET6;
1.7       kstailey  298:                        else if (strcmp(optarg, "local") == 0)
                    299:                                af = AF_LOCAL;
1.1       deraadt   300:                        else if (strcmp(optarg, "unix") == 0)
                    301:                                af = AF_UNIX;
1.4       mickey    302:                        else if (strcmp(optarg, "ipx") == 0)
                    303:                                af = AF_IPX;
                    304:                        else if (strcmp(optarg, "ns") == 0)
                    305:                                af = AF_NS;
1.10      angelos   306:                        else if (strcmp(optarg, "encap") == 0)
1.16      angelos   307:                                af = PF_KEY;
1.12      denny     308:                        else if (strcmp(optarg, "atalk") == 0)
                    309:                                af = AF_APPLETALK;
1.1       deraadt   310:                        else {
                    311:                                (void)fprintf(stderr,
                    312:                                    "%s: %s: unknown address family\n",
1.2       deraadt   313:                                    __progname, optarg);
1.1       deraadt   314:                                exit(1);
                    315:                        }
                    316:                        break;
                    317:                case 'g':
                    318:                        gflag = 1;
                    319:                        break;
1.2       deraadt   320:                case 'I':
1.1       deraadt   321:                        iflag = 1;
1.2       deraadt   322:                        interface = optarg;
1.1       deraadt   323:                        break;
                    324:                case 'i':
                    325:                        iflag = 1;
                    326:                        break;
1.19      itojun    327:                case 'l':
                    328:                        lflag = 1;
                    329:                        break;
1.1       deraadt   330:                case 'M':
                    331:                        memf = optarg;
                    332:                        break;
                    333:                case 'm':
                    334:                        mflag = 1;
                    335:                        break;
                    336:                case 'N':
                    337:                        nlistf = optarg;
                    338:                        break;
                    339:                case 'n':
                    340:                        nflag = 1;
                    341:                        break;
                    342:                case 'p':
                    343:                        if ((tp = name2protox(optarg)) == NULL) {
                    344:                                (void)fprintf(stderr,
1.42      jmc       345:                                    "%s: %s: unknown protocol\n",
1.2       deraadt   346:                                    __progname, optarg);
1.1       deraadt   347:                                exit(1);
                    348:                        }
                    349:                        pflag = 1;
1.27      brian     350:                        break;
                    351:                case 'q':
                    352:                        qflag = 1;
1.1       deraadt   353:                        break;
                    354:                case 'r':
                    355:                        rflag = 1;
1.46      cedric    356:                        break;
1.1       deraadt   357:                case 's':
                    358:                        ++sflag;
                    359:                        break;
                    360:                case 't':
                    361:                        tflag = 1;
                    362:                        break;
                    363:                case 'u':
                    364:                        af = AF_UNIX;
1.13      peter     365:                        break;
                    366:                case 'v':
                    367:                        vflag = 1;
1.1       deraadt   368:                        break;
                    369:                case 'w':
                    370:                        interval = atoi(optarg);
                    371:                        iflag = 1;
                    372:                        break;
                    373:                case '?':
                    374:                default:
                    375:                        usage();
                    376:                }
                    377:        argv += optind;
                    378:        argc -= optind;
                    379:
1.33      deraadt   380:        /*
                    381:         * Discard setgid privileges if not the running kernel so that bad
                    382:         * guys can't print interesting stuff from kernel memory.
                    383:         */
                    384:        if (nlistf != NULL || memf != NULL) {
                    385:                setegid(getgid());
                    386:                setgid(getgid());
                    387:        }
                    388:
                    389:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    390:            buf)) == NULL) {
                    391:                fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
                    392:                exit(1);
                    393:        }
                    394:        setegid(getgid());
                    395:        setgid(getgid());
                    396:
1.1       deraadt   397: #define        BACKWARD_COMPATIBILITY
                    398: #ifdef BACKWARD_COMPATIBILITY
                    399:        if (*argv) {
                    400:                if (isdigit(**argv)) {
                    401:                        interval = atoi(*argv);
                    402:                        if (interval <= 0)
                    403:                                usage();
                    404:                        ++argv;
                    405:                        iflag = 1;
                    406:                }
                    407:                if (*argv) {
                    408:                        nlistf = *argv;
                    409:                        if (*++argv)
                    410:                                memf = *argv;
                    411:                }
                    412:        }
                    413: #endif
1.14      deraadt   414:
1.1       deraadt   415:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    416:                if (nlistf)
1.2       deraadt   417:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    418:                            nlistf);
1.1       deraadt   419:                else
1.2       deraadt   420:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   421:                exit(1);
                    422:        }
                    423:        if (mflag) {
1.25      provos    424:                mbpr(nl[N_MBSTAT].n_value, nl[N_MBPOOL].n_value,
                    425:                    nl[N_MCLPOOL].n_value);
1.1       deraadt   426:                exit(0);
                    427:        }
                    428:        if (pflag) {
1.45      markus    429:                printproto(tp, tp->pr_name);
1.1       deraadt   430:                exit(0);
                    431:        }
                    432:        /*
                    433:         * Keep file descriptors open to avoid overhead
                    434:         * of open/close on each call to get* routines.
                    435:         */
                    436:        sethostent(1);
                    437:        setnetent(1);
                    438:        if (iflag) {
                    439:                intpr(interval, nl[N_IFNET].n_value);
                    440:                exit(0);
                    441:        }
                    442:        if (rflag) {
                    443:                if (sflag)
                    444:                        rt_stats(nl[N_RTSTAT].n_value);
                    445:                else
                    446:                        routepr(nl[N_RTREE].n_value);
                    447:                exit(0);
                    448:        }
                    449:        if (gflag) {
1.19      itojun    450:                if (sflag) {
                    451:                        if (af == AF_INET || af == AF_UNSPEC)
                    452:                                mrt_stats(nl[N_MRTPROTO].n_value,
                    453:                                    nl[N_MRTSTAT].n_value);
                    454: #ifdef INET6
                    455:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    456:                                mrt6_stats(nl[N_MRT6PROTO].n_value,
                    457:                                    nl[N_MRT6STAT].n_value);
                    458: #endif
                    459:                }
                    460:                else {
                    461:                        if (af == AF_INET || af == AF_UNSPEC)
                    462:                                mroutepr(nl[N_MRTPROTO].n_value,
                    463:                                    nl[N_MFCHASHTBL].n_value,
                    464:                                    nl[N_MFCHASH].n_value,
                    465:                                    nl[N_VIFTABLE].n_value);
                    466: #ifdef INET6
                    467:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    468:                                mroute6pr(nl[N_MRT6PROTO].n_value,
                    469:                                    nl[N_MF6CTABLE].n_value,
                    470:                                    nl[N_MIF6TABLE].n_value);
                    471: #endif
                    472:                }
1.1       deraadt   473:                exit(0);
                    474:        }
                    475:        if (af == AF_INET || af == AF_UNSPEC) {
                    476:                setprotoent(1);
                    477:                setservent(1);
                    478:                /* ugh, this is O(MN) ... why do we do this? */
1.11      millert   479:                while ((p = getprotoent())) {
1.1       deraadt   480:                        for (tp = protox; tp->pr_name; tp++)
                    481:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    482:                                        break;
                    483:                        if (tp->pr_name == 0 || tp->pr_wanted == 0)
                    484:                                continue;
                    485:                        printproto(tp, p->p_name);
                    486:                }
                    487:                endprotoent();
                    488:        }
1.19      itojun    489: #ifdef INET6
                    490:        if (af == AF_INET6 || af == AF_UNSPEC)
                    491:                for (tp = ip6protox; tp->pr_name; tp++)
                    492:                        printproto(tp, tp->pr_name);
                    493: #endif
1.4       mickey    494:        if (af == AF_IPX || af == AF_UNSPEC)
                    495:                for (tp = ipxprotox; tp->pr_name; tp++)
                    496:                        printproto(tp, tp->pr_name);
1.1       deraadt   497:        if (af == AF_NS || af == AF_UNSPEC)
                    498:                for (tp = nsprotox; tp->pr_name; tp++)
                    499:                        printproto(tp, tp->pr_name);
                    500:        if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
                    501:                unixpr(nl[N_UNIXSW].n_value);
1.12      denny     502:        if (af == AF_APPLETALK || af == AF_UNSPEC)
                    503:                for (tp = atalkprotox; tp->pr_name; tp++)
                    504:                        printproto(tp, tp->pr_name);
1.1       deraadt   505:        exit(0);
                    506: }
                    507:
                    508: /*
                    509:  * Print out protocol statistics or control blocks (per sflag).
                    510:  * If the interface was not specifically requested, and the symbol
                    511:  * is not in the namelist, ignore this one.
                    512:  */
                    513: static void
1.34      deraadt   514: printproto(struct protox *tp, char *name)
1.1       deraadt   515: {
1.38      deraadt   516:        void (*pr)(u_long, char *);
1.35      dhartmei  517:        u_char i;
1.1       deraadt   518:
                    519:        if (sflag) {
                    520:                pr = tp->pr_stats;
1.35      dhartmei  521:                i = tp->pr_sindex;
1.1       deraadt   522:        } else {
                    523:                pr = tp->pr_cblocks;
1.35      dhartmei  524:                i = tp->pr_index;
1.1       deraadt   525:        }
1.35      dhartmei  526:        if (pr != NULL && i < sizeof(nl) / sizeof(nl[0]) &&
                    527:            (nl[i].n_value || af != AF_UNSPEC))
                    528:                (*pr)(nl[i].n_value, name);
1.1       deraadt   529: }
                    530:
                    531: /*
                    532:  * Read kernel memory, return 0 on success.
                    533:  */
                    534: int
1.53      jaredy    535: kread(u_long addr, void *buf, int size)
1.1       deraadt   536: {
                    537:
                    538:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   539:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   540:                    kvm_geterr(kvmd));
                    541:                return (-1);
                    542:        }
                    543:        return (0);
                    544: }
                    545:
                    546: char *
1.34      deraadt   547: plural(int n)
1.1       deraadt   548: {
                    549:        return (n != 1 ? "s" : "");
                    550: }
                    551:
                    552: char *
1.34      deraadt   553: plurales(int n)
1.1       deraadt   554: {
                    555:        return (n != 1 ? "es" : "");
                    556: }
                    557:
                    558: /*
                    559:  * Find the protox for the given "well-known" name.
                    560:  */
                    561: static struct protox *
1.34      deraadt   562: knownname(char *name)
1.1       deraadt   563: {
                    564:        struct protox **tpp, *tp;
                    565:
                    566:        for (tpp = protoprotox; *tpp; tpp++)
                    567:                for (tp = *tpp; tp->pr_name; tp++)
                    568:                        if (strcmp(tp->pr_name, name) == 0)
                    569:                                return (tp);
                    570:        return (NULL);
                    571: }
                    572:
                    573: /*
                    574:  * Find the protox corresponding to name.
                    575:  */
                    576: static struct protox *
1.34      deraadt   577: name2protox(char *name)
1.1       deraadt   578: {
                    579:        struct protox *tp;
                    580:        char **alias;                   /* alias from p->aliases */
                    581:        struct protoent *p;
                    582:
                    583:        /*
                    584:         * Try to find the name in the list of "well-known" names. If that
                    585:         * fails, check if name is an alias for an Internet protocol.
                    586:         */
1.11      millert   587:        if ((tp = knownname(name)))
1.1       deraadt   588:                return (tp);
                    589:
                    590:        setprotoent(1);                 /* make protocol lookup cheaper */
1.11      millert   591:        while ((p = getprotoent())) {
1.1       deraadt   592:                /* assert: name not same as p->name */
                    593:                for (alias = p->p_aliases; *alias; alias++)
                    594:                        if (strcmp(name, *alias) == 0) {
                    595:                                endprotoent();
                    596:                                return (knownname(p->p_name));
                    597:                        }
                    598:        }
                    599:        endprotoent();
                    600:        return (NULL);
                    601: }
                    602:
                    603: static void
1.34      deraadt   604: usage(void)
1.1       deraadt   605: {
                    606:        (void)fprintf(stderr,
1.2       deraadt   607: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   608:        (void)fprintf(stderr,
1.54      jaredy    609: "       %s [-bdgilmnqrstu] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   610:        (void)fprintf(stderr,
1.37      jmc       611: "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
1.1       deraadt   612:        (void)fprintf(stderr,
1.47      jmc       613: "       %s [-s] [-M core] [-N system] [-p protocol]\n", __progname);
1.37      jmc       614:        (void)fprintf(stderr,
1.41      jmc       615: "       %s [-a] [-f address_family] [-i | -I interface]\n", __progname);
1.1       deraadt   616:        exit(1);
                    617: }