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

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