[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.35

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