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

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