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

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