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

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