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

1.26    ! jjbg        1: /*     $OpenBSD: main.c,v 1.25 2001/05/18 02:41:38 provos 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.26    ! jjbg       47: static char *rcsid = "$OpenBSD: main.c,v 1.25 2001/05/18 02:41:38 provos 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.26    ! jjbg      178: #define N_IPCOMPSTAT    53
        !           179:        { "_ipcompstat" },
1.11      millert   180:        { ""},
1.1       deraadt   181: };
                    182:
                    183: struct protox {
                    184:        u_char  pr_index;               /* index into nlist of cb head */
                    185:        u_char  pr_sindex;              /* index into nlist of stat block */
                    186:        u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
                    187:        void    (*pr_cblocks)();        /* control blocks printing routine */
                    188:        void    (*pr_stats)();          /* statistics printing routine */
                    189:        char    *pr_name;               /* well-known name */
                    190: } protox[] = {
                    191:        { N_TCBTABLE,   N_TCPSTAT,      1,      protopr,
                    192:          tcp_stats,    "tcp" },
                    193:        { N_UDBTABLE,   N_UDPSTAT,      1,      protopr,
                    194:          udp_stats,    "udp" },
                    195:        { -1,           N_IPSTAT,       1,      0,
                    196:          ip_stats,     "ip" },
                    197:        { -1,           N_ICMPSTAT,     1,      0,
                    198:          icmp_stats,   "icmp" },
                    199:        { -1,           N_IGMPSTAT,     1,      0,
                    200:          igmp_stats,   "igmp" },
1.8       angelos   201:        { -1,           N_AHSTAT,       1,      0,
1.15      angelos   202:          ah_stats,     "ah" },
1.8       angelos   203:        { -1,           N_ESPSTAT,      1,      0,
1.15      angelos   204:          esp_stats,    "esp" },
1.8       angelos   205:        { -1,           N_IP4STAT,      1,      0,
1.21      angelos   206:          ipip_stats,   "ipencap" },
1.18      angelos   207:        { -1,           N_ETHERIPSTAT,  1,      0,
                    208:          etherip_stats,"etherip" },
1.26    ! jjbg      209:        { -1,           N_IPCOMPSTAT,   1,      0,
        !           210:          ipcomp_stats, "ipcomp" },
1.1       deraadt   211:        { -1,           -1,             0,      0,
                    212:          0,            0 }
                    213: };
                    214:
1.19      itojun    215: #ifdef INET6
                    216: struct protox ip6protox[] = {
1.22      itojun    217:        { N_TCBTABLE,   N_TCPSTAT,      1,      ip6protopr,
1.23      itojun    218:          0,            "tcp" },
1.22      itojun    219:        { N_UDBTABLE,   N_UDPSTAT,      1,      ip6protopr,
1.23      itojun    220:          0,            "udp" },
1.19      itojun    221:        { -1,           N_IP6STAT,      1,      0,
                    222:          ip6_stats,    "ip6" },
                    223:        { -1,           N_ICMP6STAT,    1,      0,
                    224:          icmp6_stats,  "icmp6" },
                    225:        { -1,           N_PIM6STAT,     1,      0,
                    226:          pim6_stats,   "pim6" },
                    227:        { -1,           -1,             0,      0,
                    228:          0,            0 }
                    229: };
                    230: #endif
                    231:
1.4       mickey    232: struct protox ipxprotox[] = {
                    233:        { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
                    234:          ipx_stats,    "ipx" },
                    235:        { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
                    236:          spx_stats,    "spx" },
                    237:        { -1,           -1,             0,      0,
                    238:          0,            0 }
                    239: };
                    240:
1.1       deraadt   241: struct protox nsprotox[] = {
                    242:        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
                    243:          idp_stats,    "idp" },
                    244:        { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
                    245:          spp_stats,    "spp" },
                    246:        { -1,           N_NSERR,        1,      0,
                    247:          nserr_stats,  "ns_err" },
                    248:        { -1,           -1,             0,      0,
                    249:          0,            0 }
                    250: };
                    251:
                    252: struct protox isoprotox[] = {
                    253:        { ISO_TP,       N_TPSTAT,       1,      iso_protopr,
                    254:          tp_stats,     "tp" },
                    255:        { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
                    256:          cltp_stats,   "cltp" },
                    257:        { -1,           N_CLNPSTAT,     1,       0,
                    258:          clnp_stats,   "clnp"},
                    259:        { -1,           N_ESISSTAT,     1,       0,
                    260:          esis_stats,   "esis"},
                    261:        { -1,           -1,             0,      0,
                    262:          0,            0 }
                    263: };
                    264:
1.12      denny     265: struct protox atalkprotox[] = {
                    266:        { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
                    267:          ddp_stats,    "ddp" },
                    268:        { -1,           -1,             0,      0,
                    269:          0,            0 }
                    270: };
                    271:
1.19      itojun    272: #ifndef INET6
1.12      denny     273: struct protox *protoprotox[] = { protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL };
1.19      itojun    274: #else
                    275: struct protox *protoprotox[] = { protox, ip6protox, ipxprotox, nsprotox, isoprotox, atalkprotox, NULL };
                    276: #endif
1.1       deraadt   277:
                    278: static void printproto __P((struct protox *, char *));
                    279: static void usage __P((void));
                    280: static struct protox *name2protox __P((char *));
                    281: static struct protox *knownname __P((char *));
                    282:
                    283: kvm_t *kvmd;
                    284:
                    285: int
                    286: main(argc, argv)
                    287:        int argc;
                    288:        char *argv[];
                    289: {
                    290:        extern char *optarg;
                    291:        extern int optind;
                    292:        register struct protoent *p;
1.19      itojun    293:        register struct protox *tp = NULL; /* for printing cblocks & stats */
1.1       deraadt   294:        int ch;
                    295:        char *nlistf = NULL, *memf = NULL;
                    296:        char buf[_POSIX2_LINE_MAX];
                    297:
                    298:        af = AF_UNSPEC;
                    299:
1.24      camield   300:        while ((ch = getopt(argc, argv, "Aabdf:gI:ilM:mN:np:rstuvw:")) != -1)
1.1       deraadt   301:                switch(ch) {
                    302:                case 'A':
                    303:                        Aflag = 1;
                    304:                        break;
                    305:                case 'a':
                    306:                        aflag = 1;
1.24      camield   307:                        break;
                    308:                case 'b':
                    309:                        bflag = 1;
1.1       deraadt   310:                        break;
                    311:                case 'd':
                    312:                        dflag = 1;
                    313:                        break;
                    314:                case 'f':
1.4       mickey    315:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   316:                                af = AF_INET;
1.19      itojun    317:                        else if (strcmp(optarg, "inet6") == 0)
                    318:                                af = AF_INET6;
1.7       kstailey  319:                        else if (strcmp(optarg, "local") == 0)
                    320:                                af = AF_LOCAL;
1.1       deraadt   321:                        else if (strcmp(optarg, "unix") == 0)
                    322:                                af = AF_UNIX;
1.4       mickey    323:                        else if (strcmp(optarg, "ipx") == 0)
                    324:                                af = AF_IPX;
                    325:                        else if (strcmp(optarg, "ns") == 0)
                    326:                                af = AF_NS;
1.1       deraadt   327:                        else if (strcmp(optarg, "iso") == 0)
                    328:                                af = AF_ISO;
1.10      angelos   329:                        else if (strcmp(optarg, "encap") == 0)
1.16      angelos   330:                                af = PF_KEY;
1.12      denny     331:                        else if (strcmp(optarg, "atalk") == 0)
                    332:                                af = AF_APPLETALK;
1.1       deraadt   333:                        else {
                    334:                                (void)fprintf(stderr,
                    335:                                    "%s: %s: unknown address family\n",
1.2       deraadt   336:                                    __progname, optarg);
1.1       deraadt   337:                                exit(1);
                    338:                        }
                    339:                        break;
                    340:                case 'g':
                    341:                        gflag = 1;
                    342:                        break;
1.2       deraadt   343:                case 'I':
1.1       deraadt   344:                        iflag = 1;
1.2       deraadt   345:                        interface = optarg;
1.1       deraadt   346:                        break;
                    347:                case 'i':
                    348:                        iflag = 1;
                    349:                        break;
1.19      itojun    350:                case 'l':
                    351:                        lflag = 1;
                    352:                        break;
1.1       deraadt   353:                case 'M':
                    354:                        memf = optarg;
                    355:                        break;
                    356:                case 'm':
                    357:                        mflag = 1;
                    358:                        break;
                    359:                case 'N':
                    360:                        nlistf = optarg;
                    361:                        break;
                    362:                case 'n':
                    363:                        nflag = 1;
                    364:                        break;
                    365:                case 'p':
                    366:                        if ((tp = name2protox(optarg)) == NULL) {
                    367:                                (void)fprintf(stderr,
                    368:                                    "%s: %s: unknown or uninstrumented protocol\n",
1.2       deraadt   369:                                    __progname, optarg);
1.1       deraadt   370:                                exit(1);
                    371:                        }
                    372:                        pflag = 1;
                    373:                        break;
                    374:                case 'r':
                    375:                        rflag = 1;
                    376:                        break;
                    377:                case 's':
                    378:                        ++sflag;
                    379:                        break;
                    380:                case 't':
                    381:                        tflag = 1;
                    382:                        break;
                    383:                case 'u':
                    384:                        af = AF_UNIX;
1.13      peter     385:                        break;
                    386:                case 'v':
                    387:                        vflag = 1;
1.1       deraadt   388:                        break;
                    389:                case 'w':
                    390:                        interval = atoi(optarg);
                    391:                        iflag = 1;
                    392:                        break;
                    393:                case '?':
                    394:                default:
                    395:                        usage();
                    396:                }
                    397:        argv += optind;
                    398:        argc -= optind;
                    399:
                    400: #define        BACKWARD_COMPATIBILITY
                    401: #ifdef BACKWARD_COMPATIBILITY
                    402:        if (*argv) {
                    403:                if (isdigit(**argv)) {
                    404:                        interval = atoi(*argv);
                    405:                        if (interval <= 0)
                    406:                                usage();
                    407:                        ++argv;
                    408:                        iflag = 1;
                    409:                }
                    410:                if (*argv) {
                    411:                        nlistf = *argv;
                    412:                        if (*++argv)
                    413:                                memf = *argv;
                    414:                }
                    415:        }
                    416: #endif
                    417:
                    418:        /*
                    419:         * Discard setgid privileges if not the running kernel so that bad
                    420:         * guys can't print interesting stuff from kernel memory.
                    421:         */
1.5       tholo     422:        if (nlistf != NULL || memf != NULL) {
                    423:                setegid(getgid());
1.1       deraadt   424:                setgid(getgid());
1.5       tholo     425:        }
1.1       deraadt   426:
1.2       deraadt   427:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    428:            buf)) == NULL) {
                    429:                fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
1.1       deraadt   430:                exit(1);
                    431:        }
1.14      deraadt   432:        setegid(getgid());
                    433:        setgid(getgid());
                    434:
1.1       deraadt   435:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    436:                if (nlistf)
1.2       deraadt   437:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    438:                            nlistf);
1.1       deraadt   439:                else
1.2       deraadt   440:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   441:                exit(1);
                    442:        }
                    443:        if (mflag) {
1.25      provos    444:                mbpr(nl[N_MBSTAT].n_value, nl[N_MBPOOL].n_value,
                    445:                    nl[N_MCLPOOL].n_value);
1.1       deraadt   446:                exit(0);
                    447:        }
                    448:        if (pflag) {
                    449:                if (tp->pr_stats)
                    450:                        (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
                    451:                                tp->pr_name);
                    452:                else
                    453:                        printf("%s: no stats routine\n", tp->pr_name);
                    454:                exit(0);
                    455:        }
                    456:        /*
                    457:         * Keep file descriptors open to avoid overhead
                    458:         * of open/close on each call to get* routines.
                    459:         */
                    460:        sethostent(1);
                    461:        setnetent(1);
                    462:        if (iflag) {
                    463:                intpr(interval, nl[N_IFNET].n_value);
                    464:                exit(0);
                    465:        }
                    466:        if (rflag) {
                    467:                if (sflag)
                    468:                        rt_stats(nl[N_RTSTAT].n_value);
                    469:                else
                    470:                        routepr(nl[N_RTREE].n_value);
                    471:                exit(0);
                    472:        }
                    473:        if (gflag) {
1.19      itojun    474:                if (sflag) {
                    475:                        if (af == AF_INET || af == AF_UNSPEC)
                    476:                                mrt_stats(nl[N_MRTPROTO].n_value,
                    477:                                    nl[N_MRTSTAT].n_value);
                    478: #ifdef INET6
                    479:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    480:                                mrt6_stats(nl[N_MRT6PROTO].n_value,
                    481:                                    nl[N_MRT6STAT].n_value);
                    482: #endif
                    483:                }
                    484:                else {
                    485:                        if (af == AF_INET || af == AF_UNSPEC)
                    486:                                mroutepr(nl[N_MRTPROTO].n_value,
                    487:                                    nl[N_MFCHASHTBL].n_value,
                    488:                                    nl[N_MFCHASH].n_value,
                    489:                                    nl[N_VIFTABLE].n_value);
                    490: #ifdef INET6
                    491:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    492:                                mroute6pr(nl[N_MRT6PROTO].n_value,
                    493:                                    nl[N_MF6CTABLE].n_value,
                    494:                                    nl[N_MIF6TABLE].n_value);
                    495: #endif
                    496:                }
1.1       deraadt   497:                exit(0);
                    498:        }
                    499:        if (af == AF_INET || af == AF_UNSPEC) {
                    500:                setprotoent(1);
                    501:                setservent(1);
                    502:                /* ugh, this is O(MN) ... why do we do this? */
1.11      millert   503:                while ((p = getprotoent())) {
1.1       deraadt   504:                        for (tp = protox; tp->pr_name; tp++)
                    505:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    506:                                        break;
                    507:                        if (tp->pr_name == 0 || tp->pr_wanted == 0)
                    508:                                continue;
                    509:                        printproto(tp, p->p_name);
                    510:                }
                    511:                endprotoent();
                    512:        }
1.19      itojun    513: #ifdef INET6
                    514:        if (af == AF_INET6 || af == AF_UNSPEC)
                    515:                for (tp = ip6protox; tp->pr_name; tp++)
                    516:                        printproto(tp, tp->pr_name);
                    517: #endif
1.4       mickey    518:        if (af == AF_IPX || af == AF_UNSPEC)
                    519:                for (tp = ipxprotox; tp->pr_name; tp++)
                    520:                        printproto(tp, tp->pr_name);
1.1       deraadt   521:        if (af == AF_NS || af == AF_UNSPEC)
                    522:                for (tp = nsprotox; tp->pr_name; tp++)
                    523:                        printproto(tp, tp->pr_name);
                    524:        if (af == AF_ISO || af == AF_UNSPEC)
                    525:                for (tp = isoprotox; tp->pr_name; tp++)
                    526:                        printproto(tp, tp->pr_name);
                    527:        if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
                    528:                unixpr(nl[N_UNIXSW].n_value);
1.12      denny     529:        if (af == AF_APPLETALK || af == AF_UNSPEC)
                    530:                for (tp = atalkprotox; tp->pr_name; tp++)
                    531:                        printproto(tp, tp->pr_name);
1.1       deraadt   532:        exit(0);
                    533: }
                    534:
                    535: /*
                    536:  * Print out protocol statistics or control blocks (per sflag).
                    537:  * If the interface was not specifically requested, and the symbol
                    538:  * is not in the namelist, ignore this one.
                    539:  */
                    540: static void
                    541: printproto(tp, name)
                    542:        register struct protox *tp;
                    543:        char *name;
                    544: {
                    545:        void (*pr)();
                    546:        u_long off;
                    547:
                    548:        if (sflag) {
                    549:                pr = tp->pr_stats;
                    550:                off = nl[tp->pr_sindex].n_value;
                    551:        } else {
                    552:                pr = tp->pr_cblocks;
                    553:                off = nl[tp->pr_index].n_value;
                    554:        }
                    555:        if (pr != NULL && (off || af != AF_UNSPEC))
                    556:                (*pr)(off, name);
                    557: }
                    558:
                    559: /*
                    560:  * Read kernel memory, return 0 on success.
                    561:  */
                    562: int
                    563: kread(addr, buf, size)
                    564:        u_long addr;
                    565:        char *buf;
                    566:        int size;
                    567: {
                    568:
                    569:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   570:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   571:                    kvm_geterr(kvmd));
                    572:                return (-1);
                    573:        }
                    574:        return (0);
                    575: }
                    576:
                    577: char *
                    578: plural(n)
                    579:        int n;
                    580: {
                    581:        return (n != 1 ? "s" : "");
                    582: }
                    583:
                    584: char *
                    585: plurales(n)
                    586:        int n;
                    587: {
                    588:        return (n != 1 ? "es" : "");
                    589: }
                    590:
                    591: /*
                    592:  * Find the protox for the given "well-known" name.
                    593:  */
                    594: static struct protox *
                    595: knownname(name)
                    596:        char *name;
                    597: {
                    598:        struct protox **tpp, *tp;
                    599:
                    600:        for (tpp = protoprotox; *tpp; tpp++)
                    601:                for (tp = *tpp; tp->pr_name; tp++)
                    602:                        if (strcmp(tp->pr_name, name) == 0)
                    603:                                return (tp);
                    604:        return (NULL);
                    605: }
                    606:
                    607: /*
                    608:  * Find the protox corresponding to name.
                    609:  */
                    610: static struct protox *
                    611: name2protox(name)
                    612:        char *name;
                    613: {
                    614:        struct protox *tp;
                    615:        char **alias;                   /* alias from p->aliases */
                    616:        struct protoent *p;
                    617:
                    618:        /*
                    619:         * Try to find the name in the list of "well-known" names. If that
                    620:         * fails, check if name is an alias for an Internet protocol.
                    621:         */
1.11      millert   622:        if ((tp = knownname(name)))
1.1       deraadt   623:                return (tp);
                    624:
                    625:        setprotoent(1);                 /* make protocol lookup cheaper */
1.11      millert   626:        while ((p = getprotoent())) {
1.1       deraadt   627:                /* assert: name not same as p->name */
                    628:                for (alias = p->p_aliases; *alias; alias++)
                    629:                        if (strcmp(name, *alias) == 0) {
                    630:                                endprotoent();
                    631:                                return (knownname(p->p_name));
                    632:                        }
                    633:        }
                    634:        endprotoent();
                    635:        return (NULL);
                    636: }
                    637:
                    638: static void
                    639: usage()
                    640: {
                    641:        (void)fprintf(stderr,
1.2       deraadt   642: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   643:        (void)fprintf(stderr,
1.17      d         644: "       %s [-gimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   645:        (void)fprintf(stderr,
1.2       deraadt   646: "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
1.1       deraadt   647:        (void)fprintf(stderr,
1.2       deraadt   648: "       %s [-M core] [-N system] [-p protocol]\n", __progname);
1.1       deraadt   649:        exit(1);
                    650: }