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

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