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

1.70    ! deraadt     1: /*     $OpenBSD: main.c,v 1.69 2007/10/20 18:08:57 sobrado 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.70    ! deraadt    43: static char *rcsid = "$OpenBSD: main.c,v 1.69 2007/10/20 18:08:57 sobrado 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:
1.68      claudio    52: #include <net/route.h>
1.1       deraadt    53: #include <netinet/in.h>
                     54:
                     55: #include <ctype.h>
1.61      djm        56: #include <err.h>
1.1       deraadt    57: #include <errno.h>
                     58: #include <kvm.h>
                     59: #include <limits.h>
                     60: #include <netdb.h>
                     61: #include <nlist.h>
                     62: #include <paths.h>
                     63: #include <stdio.h>
                     64: #include <stdlib.h>
                     65: #include <string.h>
                     66: #include <unistd.h>
                     67: #include "netstat.h"
                     68:
                     69: struct nlist nl[] = {
1.70    ! deraadt    70: #define N_IPSTAT       0
1.1       deraadt    71:        { "_ipstat" },
1.70    ! deraadt    72: #define N_TCBTABLE     1
1.1       deraadt    73:        { "_tcbtable" },
1.70    ! deraadt    74: #define N_TCPSTAT      2
1.1       deraadt    75:        { "_tcpstat" },
1.70    ! deraadt    76: #define N_UDBTABLE     3
1.1       deraadt    77:        { "_udbtable" },
1.70    ! deraadt    78: #define N_UDPSTAT      4
1.1       deraadt    79:        { "_udpstat" },
1.70    ! deraadt    80: #define N_IFNET                5
1.1       deraadt    81:        { "_ifnet" },
1.70    ! deraadt    82: #define N_ICMPSTAT     6
1.1       deraadt    83:        { "_icmpstat" },
1.70    ! deraadt    84: #define N_RTSTAT       7
1.1       deraadt    85:        { "_rtstat" },
1.70    ! deraadt    86: #define N_UNIXSW       8
1.1       deraadt    87:        { "_unixsw" },
1.70    ! deraadt    88: #define N_RTREE                9
1.1       deraadt    89:        { "_rt_tables"},
1.70    ! deraadt    90: #define N_FILE         10
1.1       deraadt    91:        { "_file" },
1.70    ! deraadt    92: #define N_IGMPSTAT     11
1.1       deraadt    93:        { "_igmpstat" },
1.70    ! deraadt    94: #define N_MRTPROTO     12
1.1       deraadt    95:        { "_ip_mrtproto" },
1.70    ! deraadt    96: #define N_MRTSTAT      13
1.1       deraadt    97:        { "_mrtstat" },
1.70    ! deraadt    98: #define N_MFCHASHTBL   14
1.1       deraadt    99:        { "_mfchashtbl" },
1.70    ! deraadt   100: #define N_MFCHASH      15
1.1       deraadt   101:        { "_mfchash" },
1.70    ! deraadt   102: #define N_VIFTABLE     16
1.1       deraadt   103:        { "_viftable" },
1.70    ! deraadt   104: #define N_AHSTAT       17
1.8       angelos   105:        { "_ahstat"},
1.70    ! deraadt   106: #define N_ESPSTAT      18
1.8       angelos   107:        { "_espstat"},
1.70    ! deraadt   108: #define N_IP4STAT      19
1.21      angelos   109:        { "_ipipstat"},
1.70    ! deraadt   110: #define N_DDPSTAT      20
1.12      denny     111:        { "_ddpstat"},
1.70    ! deraadt   112: #define N_DDPCB                21
1.12      denny     113:        { "_ddpcb"},
1.70    ! deraadt   114: #define N_ETHERIPSTAT  22
1.18      angelos   115:        { "_etheripstat"},
1.70    ! deraadt   116: #define N_IP6STAT      23
1.19      itojun    117:        { "_ip6stat" },
1.70    ! deraadt   118: #define N_ICMP6STAT    24
1.19      itojun    119:        { "_icmp6stat" },
1.70    ! deraadt   120: #define N_PIM6STAT     25
1.19      itojun    121:        { "_pim6stat" },
1.70    ! deraadt   122: #define N_MRT6PROTO    26
1.19      itojun    123:        { "_ip6_mrtproto" },
1.70    ! deraadt   124: #define N_MRT6STAT     27
1.19      itojun    125:        { "_mrt6stat" },
1.70    ! deraadt   126: #define N_MF6CTABLE    28
1.19      itojun    127:        { "_mf6ctable" },
1.70    ! deraadt   128: #define N_MIF6TABLE    29
1.19      itojun    129:        { "_mif6table" },
1.70    ! deraadt   130: #define N_IPCOMPSTAT   30
1.26      jjbg      131:        { "_ipcompstat" },
1.70    ! deraadt   132: #define N_RIP6STAT     31
1.32      itojun    133:        { "_rip6stat" },
1.70    ! deraadt   134: #define N_CARPSTAT     32
1.39      mcbride   135:        { "_carpstats" },
1.70    ! deraadt   136: #define N_RAWIPTABLE   33
1.40      markus    137:        { "_rawcbtable" },
1.70    ! deraadt   138: #define N_RAWIP6TABLE  34
1.40      markus    139:        { "_rawin6pcbtable" },
1.70    ! deraadt   140: #define N_PFSYNCSTAT   35
1.43      mcbride   141:        { "_pfsyncstats" },
1.70    ! deraadt   142: #define N_PIMSTAT      36
1.50      mcbride   143:        { "_pimstat" },
1.70    ! deraadt   144: #define N_AF2RTAFIDX   37
1.63      claudio   145:        { "_af2rtafidx" },
1.70    ! deraadt   146: #define N_RTBLIDMAX    38
1.63      claudio   147:        { "_rtbl_id_max" },
1.70    ! deraadt   148: #define N_RTMASK       39
1.63      claudio   149:        { "_mask_rnhead" },
1.55      deraadt   150:        { ""}
1.1       deraadt   151: };
                    152:
                    153: struct protox {
1.38      deraadt   154:        u_char  pr_index;                       /* index into nlist of cb head */
                    155:        u_char  pr_sindex;                      /* index into nlist of stat block */
                    156:        u_char  pr_wanted;                      /* 1 if wanted, 0 otherwise */
                    157:        void    (*pr_cblocks)(u_long, char *);  /* control blocks printing routine */
                    158:        void    (*pr_stats)(u_long, char *);    /* statistics printing routine */
1.59      markus    159:        void    (*pr_dump)(u_long);             /* pcb printing routine */
1.38      deraadt   160:        char    *pr_name;                       /* well-known name */
1.1       deraadt   161: } protox[] = {
                    162:        { N_TCBTABLE,   N_TCPSTAT,      1,      protopr,
1.59      markus    163:          tcp_stats,    tcp_dump,       "tcp" },
1.1       deraadt   164:        { N_UDBTABLE,   N_UDPSTAT,      1,      protopr,
1.59      markus    165:          udp_stats,    0,              "udp" },
1.40      markus    166:        { N_RAWIPTABLE, N_IPSTAT,       1,      protopr,
1.59      markus    167:          ip_stats,     0,              "ip" },
1.1       deraadt   168:        { -1,           N_ICMPSTAT,     1,      0,
1.59      markus    169:          icmp_stats,   0,              "icmp" },
1.1       deraadt   170:        { -1,           N_IGMPSTAT,     1,      0,
1.59      markus    171:          igmp_stats,   0,              "igmp" },
1.8       angelos   172:        { -1,           N_AHSTAT,       1,      0,
1.59      markus    173:          ah_stats,     0,              "ah" },
1.8       angelos   174:        { -1,           N_ESPSTAT,      1,      0,
1.59      markus    175:          esp_stats,    0,              "esp" },
1.8       angelos   176:        { -1,           N_IP4STAT,      1,      0,
1.59      markus    177:          ipip_stats,   0,              "ipencap" },
1.18      angelos   178:        { -1,           N_ETHERIPSTAT,  1,      0,
1.59      markus    179:          etherip_stats,0,              "etherip" },
1.29      mickey    180:        { -1,           N_IPCOMPSTAT,   1,      0,
1.59      markus    181:          ipcomp_stats, 0,              "ipcomp" },
1.44      deraadt   182:        { -1,           N_CARPSTAT,     1,      0,
1.59      markus    183:          carp_stats,   0,              "carp" },
1.44      deraadt   184:        { -1,           N_PFSYNCSTAT,   1,      0,
1.59      markus    185:          pfsync_stats, 0,              "pfsync" },
1.50      mcbride   186:        { -1,           N_PIMSTAT,      1,      0,
1.59      markus    187:          pim_stats,    0,              "pim" },
1.1       deraadt   188:        { -1,           -1,             0,      0,
1.59      markus    189:          0,            0,              0 }
1.1       deraadt   190: };
                    191:
1.19      itojun    192: #ifdef INET6
                    193: struct protox ip6protox[] = {
1.22      itojun    194:        { N_TCBTABLE,   N_TCPSTAT,      1,      ip6protopr,
1.59      markus    195:          0,            tcp_dump,       "tcp" },
1.22      itojun    196:        { N_UDBTABLE,   N_UDPSTAT,      1,      ip6protopr,
1.59      markus    197:          0,            0,              "udp" },
1.40      markus    198:        { N_RAWIP6TABLE,N_IP6STAT,      1,      ip6protopr,
1.59      markus    199:          ip6_stats,    0,              "ip6" },
1.19      itojun    200:        { -1,           N_ICMP6STAT,    1,      0,
1.59      markus    201:          icmp6_stats,  0,              "icmp6" },
1.19      itojun    202:        { -1,           N_PIM6STAT,     1,      0,
1.59      markus    203:          pim6_stats,   0,              "pim6" },
1.32      itojun    204:        { -1,           N_RIP6STAT,     1,      0,
1.59      markus    205:          rip6_stats,   0,              "rip6" },
1.19      itojun    206:        { -1,           -1,             0,      0,
1.59      markus    207:          0,            0,              0 }
1.19      itojun    208: };
                    209: #endif
                    210:
1.12      denny     211: struct protox atalkprotox[] = {
                    212:        { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
1.59      markus    213:          ddp_stats,    0,              "ddp" },
1.12      denny     214:        { -1,           -1,             0,      0,
1.59      markus    215:          0,            0,              0 }
1.12      denny     216: };
                    217:
1.19      itojun    218: #ifndef INET6
1.34      deraadt   219: struct protox *protoprotox[] = {
1.66      henning   220:        protox, atalkprotox, NULL
1.34      deraadt   221: };
1.19      itojun    222: #else
1.34      deraadt   223: struct protox *protoprotox[] = {
1.66      henning   224:        protox, ip6protox, atalkprotox, NULL
1.34      deraadt   225: };
1.19      itojun    226: #endif
1.1       deraadt   227:
1.30      millert   228: static void printproto(struct protox *, char *);
                    229: static void usage(void);
                    230: static struct protox *name2protox(char *);
                    231: static struct protox *knownname(char *);
1.1       deraadt   232:
                    233: kvm_t *kvmd;
                    234:
                    235: int
1.34      deraadt   236: main(int argc, char *argv[])
1.1       deraadt   237: {
                    238:        extern char *optarg;
                    239:        extern int optind;
1.68      claudio   240:        const char *errstr;
1.28      mpech     241:        struct protoent *p;
                    242:        struct protox *tp = NULL; /* for printing cblocks & stats */
1.1       deraadt   243:        int ch;
1.59      markus    244:        char *nlistf = NULL, *memf = NULL, *ep;
1.1       deraadt   245:        char buf[_POSIX2_LINE_MAX];
1.61      djm       246:        gid_t gid;
1.59      markus    247:        u_long pcbaddr = 0;
1.68      claudio   248:        u_int tableid = 0;
1.1       deraadt   249:
                    250:        af = AF_UNSPEC;
                    251:
1.68      claudio   252:        while ((ch = getopt(argc, argv, "AabdFf:gI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
1.31      deraadt   253:                switch (ch) {
1.1       deraadt   254:                case 'A':
                    255:                        Aflag = 1;
                    256:                        break;
                    257:                case 'a':
                    258:                        aflag = 1;
1.24      camield   259:                        break;
                    260:                case 'b':
                    261:                        bflag = 1;
1.1       deraadt   262:                        break;
                    263:                case 'd':
                    264:                        dflag = 1;
1.64      pyr       265:                        break;
                    266:                case 'F':
                    267:                        Fflag = 1;
1.1       deraadt   268:                        break;
                    269:                case 'f':
1.4       mickey    270:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   271:                                af = AF_INET;
1.19      itojun    272:                        else if (strcmp(optarg, "inet6") == 0)
                    273:                                af = AF_INET6;
1.7       kstailey  274:                        else if (strcmp(optarg, "local") == 0)
                    275:                                af = AF_LOCAL;
1.1       deraadt   276:                        else if (strcmp(optarg, "unix") == 0)
                    277:                                af = AF_UNIX;
1.10      angelos   278:                        else if (strcmp(optarg, "encap") == 0)
1.16      angelos   279:                                af = PF_KEY;
1.12      denny     280:                        else if (strcmp(optarg, "atalk") == 0)
                    281:                                af = AF_APPLETALK;
1.63      claudio   282:                        else if (strcmp(optarg, "mask") == 0)
                    283:                                af = 0xff;
1.1       deraadt   284:                        else {
                    285:                                (void)fprintf(stderr,
                    286:                                    "%s: %s: unknown address family\n",
1.2       deraadt   287:                                    __progname, optarg);
1.1       deraadt   288:                                exit(1);
                    289:                        }
                    290:                        break;
                    291:                case 'g':
                    292:                        gflag = 1;
                    293:                        break;
1.2       deraadt   294:                case 'I':
1.1       deraadt   295:                        iflag = 1;
1.2       deraadt   296:                        interface = optarg;
1.1       deraadt   297:                        break;
                    298:                case 'i':
                    299:                        iflag = 1;
                    300:                        break;
1.19      itojun    301:                case 'l':
                    302:                        lflag = 1;
                    303:                        break;
1.1       deraadt   304:                case 'M':
                    305:                        memf = optarg;
                    306:                        break;
                    307:                case 'm':
                    308:                        mflag = 1;
                    309:                        break;
                    310:                case 'N':
                    311:                        nlistf = optarg;
                    312:                        break;
                    313:                case 'n':
                    314:                        nflag = 1;
                    315:                        break;
                    316:                case 'p':
                    317:                        if ((tp = name2protox(optarg)) == NULL) {
                    318:                                (void)fprintf(stderr,
1.42      jmc       319:                                    "%s: %s: unknown protocol\n",
1.2       deraadt   320:                                    __progname, optarg);
1.1       deraadt   321:                                exit(1);
                    322:                        }
                    323:                        pflag = 1;
1.27      brian     324:                        break;
1.59      markus    325:                case 'P':
                    326:                        errno = 0;
                    327:                        pcbaddr = strtoul(optarg, &ep, 16);
                    328:                        if (optarg[0] == '\0' || *ep != '\0' ||
                    329:                            errno == ERANGE) {
                    330:                                (void)fprintf(stderr,
                    331:                                    "%s: %s: invalid PCB address\n",
                    332:                                    __progname, optarg);
                    333:                                exit(1);
                    334:                        }
                    335:                        Pflag = 1;
                    336:                        break;
1.27      brian     337:                case 'q':
                    338:                        qflag = 1;
1.1       deraadt   339:                        break;
                    340:                case 'r':
                    341:                        rflag = 1;
1.46      cedric    342:                        break;
1.1       deraadt   343:                case 's':
                    344:                        ++sflag;
                    345:                        break;
1.68      claudio   346:                case 'T':
                    347:                        tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr);
                    348:                        if (errstr)
                    349:                                errx(1, "invalid table id: %s", errstr);
                    350:                        break;
1.1       deraadt   351:                case 't':
                    352:                        tflag = 1;
                    353:                        break;
                    354:                case 'u':
                    355:                        af = AF_UNIX;
1.13      peter     356:                        break;
                    357:                case 'v':
                    358:                        vflag = 1;
1.1       deraadt   359:                        break;
1.56      reyk      360:                case 'W':
                    361:                        Wflag = 1;
                    362:                        interface = optarg;
                    363:                        break;
1.1       deraadt   364:                case 'w':
                    365:                        interval = atoi(optarg);
                    366:                        iflag = 1;
                    367:                        break;
                    368:                case '?':
                    369:                default:
                    370:                        usage();
                    371:                }
                    372:        argv += optind;
                    373:        argc -= optind;
                    374:
1.33      deraadt   375:        /*
1.56      reyk      376:         * Show per-interface statistics which don't need access to
                    377:         * kernel memory (they're using IOCTLs)
                    378:         */
                    379:        if (Wflag) {
                    380:                if (interface == NULL)
                    381:                        usage();
                    382:                net80211_ifstats(interface);
                    383:                exit(0);
                    384:        }
                    385:
                    386:        /*
1.33      deraadt   387:         * Discard setgid privileges if not the running kernel so that bad
                    388:         * guys can't print interesting stuff from kernel memory.
1.59      markus    389:         * Dumping PCB info is also restricted.
1.33      deraadt   390:         */
1.61      djm       391:        gid = getgid();
                    392:        if (nlistf != NULL || memf != NULL || Pflag)
                    393:                if (setresgid(gid, gid, gid) == -1)
                    394:                        err(1, "setresgid");
1.62      claudio   395:        if (nlistf == NULL && memf == NULL && rflag && !Aflag) {
                    396:                /* printing the routing table no longer needs kvm */
                    397:                if (setresgid(gid, gid, gid) == -1)
                    398:                        err(1, "setresgid");
                    399:                if (sflag)
                    400:                        rt_stats(1, 0);
                    401:                else
1.68      claudio   402:                        p_rttables(af, tableid);
1.62      claudio   403:                exit(0);
                    404:        }
1.33      deraadt   405:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    406:            buf)) == NULL) {
                    407:                fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
                    408:                exit(1);
                    409:        }
1.61      djm       410:
                    411:        if (nlistf == NULL && memf == NULL && !Pflag)
                    412:                if (setresgid(gid, gid, gid) == -1)
                    413:                        err(1, "setresgid");
1.33      deraadt   414:
1.1       deraadt   415: #define        BACKWARD_COMPATIBILITY
                    416: #ifdef BACKWARD_COMPATIBILITY
                    417:        if (*argv) {
                    418:                if (isdigit(**argv)) {
                    419:                        interval = atoi(*argv);
                    420:                        if (interval <= 0)
                    421:                                usage();
                    422:                        ++argv;
                    423:                        iflag = 1;
                    424:                }
                    425:                if (*argv) {
                    426:                        nlistf = *argv;
                    427:                        if (*++argv)
                    428:                                memf = *argv;
                    429:                }
                    430:        }
                    431: #endif
1.14      deraadt   432:
1.1       deraadt   433:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    434:                if (nlistf)
1.2       deraadt   435:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    436:                            nlistf);
1.1       deraadt   437:                else
1.2       deraadt   438:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   439:                exit(1);
                    440:        }
                    441:        if (mflag) {
1.70    ! deraadt   442:                mbpr();
1.1       deraadt   443:                exit(0);
                    444:        }
                    445:        if (pflag) {
1.45      markus    446:                printproto(tp, tp->pr_name);
1.59      markus    447:                exit(0);
                    448:        }
                    449:        if (Pflag) {
                    450:                if (tp == NULL && (tp = name2protox("tcp")) == NULL) {
                    451:                        (void)fprintf(stderr,
                    452:                            "%s: %s: unknown protocol\n",
                    453:                            __progname, "tcp");
                    454:                        exit(1);
                    455:                }
                    456:                if (tp->pr_dump)
                    457:                        (tp->pr_dump)(pcbaddr);
1.1       deraadt   458:                exit(0);
                    459:        }
                    460:        /*
                    461:         * Keep file descriptors open to avoid overhead
                    462:         * of open/close on each call to get* routines.
                    463:         */
                    464:        sethostent(1);
                    465:        setnetent(1);
1.56      reyk      466:
1.1       deraadt   467:        if (iflag) {
                    468:                intpr(interval, nl[N_IFNET].n_value);
                    469:                exit(0);
                    470:        }
                    471:        if (rflag) {
                    472:                if (sflag)
1.62      claudio   473:                        rt_stats(0, nl[N_RTSTAT].n_value);
1.1       deraadt   474:                else
1.63      claudio   475:                        routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
                    476:                            nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value);
1.1       deraadt   477:                exit(0);
                    478:        }
                    479:        if (gflag) {
1.19      itojun    480:                if (sflag) {
                    481:                        if (af == AF_INET || af == AF_UNSPEC)
                    482:                                mrt_stats(nl[N_MRTPROTO].n_value,
                    483:                                    nl[N_MRTSTAT].n_value);
                    484: #ifdef INET6
                    485:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    486:                                mrt6_stats(nl[N_MRT6PROTO].n_value,
                    487:                                    nl[N_MRT6STAT].n_value);
                    488: #endif
1.70    ! deraadt   489:                } else {
1.19      itojun    490:                        if (af == AF_INET || af == AF_UNSPEC)
                    491:                                mroutepr(nl[N_MRTPROTO].n_value,
                    492:                                    nl[N_MFCHASHTBL].n_value,
                    493:                                    nl[N_MFCHASH].n_value,
                    494:                                    nl[N_VIFTABLE].n_value);
                    495: #ifdef INET6
                    496:                        if (af == AF_INET6 || af == AF_UNSPEC)
                    497:                                mroute6pr(nl[N_MRT6PROTO].n_value,
                    498:                                    nl[N_MF6CTABLE].n_value,
                    499:                                    nl[N_MIF6TABLE].n_value);
                    500: #endif
                    501:                }
1.1       deraadt   502:                exit(0);
                    503:        }
                    504:        if (af == AF_INET || af == AF_UNSPEC) {
                    505:                setprotoent(1);
                    506:                setservent(1);
                    507:                /* ugh, this is O(MN) ... why do we do this? */
1.11      millert   508:                while ((p = getprotoent())) {
1.1       deraadt   509:                        for (tp = protox; tp->pr_name; tp++)
                    510:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    511:                                        break;
                    512:                        if (tp->pr_name == 0 || tp->pr_wanted == 0)
                    513:                                continue;
                    514:                        printproto(tp, p->p_name);
                    515:                }
                    516:                endprotoent();
                    517:        }
1.19      itojun    518: #ifdef INET6
                    519:        if (af == AF_INET6 || af == AF_UNSPEC)
                    520:                for (tp = ip6protox; tp->pr_name; tp++)
                    521:                        printproto(tp, tp->pr_name);
                    522: #endif
1.1       deraadt   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.53      jaredy    558: kread(u_long addr, void *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.60      jaredy    630:            "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n"
1.69      sobrado   631:            "       %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
                    632:            "               [-T tableid]\n"
1.60      jaredy    633:            "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n"
                    634:            "       %s [-M core] [-N system] -P pcbaddr\n"
                    635:            "       %s [-s] [-M core] [-N system] [-p protocol]\n"
                    636:            "       %s [-a] [-f address_family] [-i | -I interface]\n"
                    637:            "       %s [-W interface]\n",
                    638:            __progname, __progname, __progname, __progname,
                    639:            __progname, __progname, __progname);
1.1       deraadt   640:        exit(1);
                    641: }