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

1.86    ! claudio     1: /*     $OpenBSD: main.c,v 1.85 2010/07/14 23:44:41 dhill 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: #include <sys/param.h>
                     34: #include <sys/file.h>
                     35: #include <sys/protosw.h>
                     36: #include <sys/socket.h>
1.86    ! claudio    37: #include <sys/sysctl.h>
1.1       deraadt    38:
1.68      claudio    39: #include <net/route.h>
1.1       deraadt    40: #include <netinet/in.h>
                     41:
                     42: #include <ctype.h>
1.61      djm        43: #include <err.h>
1.1       deraadt    44: #include <errno.h>
                     45: #include <kvm.h>
                     46: #include <limits.h>
                     47: #include <netdb.h>
                     48: #include <nlist.h>
                     49: #include <paths.h>
                     50: #include <stdio.h>
                     51: #include <stdlib.h>
                     52: #include <string.h>
                     53: #include <unistd.h>
                     54: #include "netstat.h"
                     55:
                     56: struct nlist nl[] = {
1.71      deraadt    57: #define N_TCBTABLE     0
1.1       deraadt    58:        { "_tcbtable" },
1.71      deraadt    59: #define N_UDBTABLE     1
1.1       deraadt    60:        { "_udbtable" },
1.71      deraadt    61: #define N_DDPCB                2
                     62:        { "_ddpcb"},
1.74      claudio    63: #define N_UNIXSW       3
1.1       deraadt    64:        { "_unixsw" },
1.71      deraadt    65:
1.74      claudio    66: #define N_MFCHASHTBL   4
1.1       deraadt    67:        { "_mfchashtbl" },
1.74      claudio    68: #define N_MFCHASH      5
1.1       deraadt    69:        { "_mfchash" },
1.74      claudio    70: #define N_VIFTABLE     6
1.1       deraadt    71:        { "_viftable" },
1.71      deraadt    72:
1.74      claudio    73: #define N_MF6CTABLE    7
1.19      itojun     74:        { "_mf6ctable" },
1.74      claudio    75: #define N_MIF6TABLE    8
1.19      itojun     76:        { "_mif6table" },
1.71      deraadt    77:
1.74      claudio    78: #define N_RTREE                9
1.71      deraadt    79:        { "_rt_tables"},
1.74      claudio    80: #define N_RTMASK       10
1.71      deraadt    81:        { "_mask_rnhead" },
1.74      claudio    82: #define N_AF2RTAFIDX   11
1.71      deraadt    83:        { "_af2rtafidx" },
1.74      claudio    84: #define N_RTBLIDMAX    12
1.71      deraadt    85:        { "_rtbl_id_max" },
                     86:
1.74      claudio    87: #define N_RAWIPTABLE   13
1.40      markus     88:        { "_rawcbtable" },
1.74      claudio    89: #define N_RAWIP6TABLE  14
1.40      markus     90:        { "_rawin6pcbtable" },
1.80      michele    91: #define N_DIVBTABLE    15
                     92:        { "_divbtable" },
1.81      michele    93: #define N_DIVB6TABLE   16
                     94:        { "_divb6table" },
1.71      deraadt    95:
1.55      deraadt    96:        { ""}
1.1       deraadt    97: };
                     98:
                     99: struct protox {
1.38      deraadt   100:        u_char  pr_index;                       /* index into nlist of cb head */
1.83      blambert  101:        void    (*pr_cblocks)(u_long, char *, int); /* control blocks printing routine */
1.71      deraadt   102:        void    (*pr_stats)(char *);            /* statistics printing routine */
1.59      markus    103:        void    (*pr_dump)(u_long);             /* pcb printing routine */
1.38      deraadt   104:        char    *pr_name;                       /* well-known name */
1.1       deraadt   105: } protox[] = {
1.71      deraadt   106:        { N_TCBTABLE,   protopr,        tcp_stats,      tcp_dump,       "tcp" },
                    107:        { N_UDBTABLE,   protopr,        udp_stats,      NULL,           "udp" },
                    108:        { N_RAWIPTABLE, protopr,        ip_stats,       NULL,           "ip" },
1.80      michele   109:        { N_DIVBTABLE,  protopr,        div_stats,      NULL,           "divert" },
1.71      deraadt   110:        { -1,           NULL,           icmp_stats,     NULL,           "icmp" },
                    111:        { -1,           NULL,           igmp_stats,     NULL,           "igmp" },
                    112:        { -1,           NULL,           ah_stats,       NULL,           "ah" },
                    113:        { -1,           NULL,           esp_stats,      NULL,           "esp" },
                    114:        { -1,           NULL,           ipip_stats,     NULL,           "ipencap" },
                    115:        { -1,           NULL,           etherip_stats,  NULL,           "etherip" },
                    116:        { -1,           NULL,           ipcomp_stats,   NULL,           "ipcomp" },
                    117:        { -1,           NULL,           carp_stats,     NULL,           "carp" },
                    118:        { -1,           NULL,           pfsync_stats,   NULL,           "pfsync" },
                    119:        { -1,           NULL,           pim_stats,      NULL,           "pim" },
1.76      gollo     120:        { -1,           NULL,           pflow_stats,    NULL,           "pflow" },
1.71      deraadt   121:        { -1,           NULL,           NULL,           NULL,           NULL }
1.1       deraadt   122: };
                    123:
1.19      itojun    124: struct protox ip6protox[] = {
1.83      blambert  125:        { N_TCBTABLE,   protopr,        NULL,           tcp_dump,       "tcp" },
                    126:        { N_UDBTABLE,   protopr,        NULL,           NULL,           "udp" },
                    127:        { N_RAWIP6TABLE,protopr,        ip6_stats,      NULL,           "ip6" },
                    128:        { N_DIVB6TABLE, protopr,        div6_stats,     NULL,           "divert6" },
1.71      deraadt   129:        { -1,           NULL,           icmp6_stats,    NULL,           "icmp6" },
                    130:        { -1,           NULL,           pim6_stats,     NULL,           "pim6" },
                    131:        { -1,           NULL,           rip6_stats,     NULL,           "rip6" },
                    132:        { -1,           NULL,           NULL,           NULL,           NULL }
1.19      itojun    133: };
                    134:
1.12      denny     135: struct protox atalkprotox[] = {
1.71      deraadt   136:        { N_DDPCB,      atalkprotopr,   ddp_stats,      NULL,           "ddp" },
                    137:        { -1,           NULL,           NULL,           NULL,           NULL }
1.12      denny     138: };
                    139:
1.34      deraadt   140: struct protox *protoprotox[] = {
1.66      henning   141:        protox, ip6protox, atalkprotox, NULL
1.34      deraadt   142: };
1.1       deraadt   143:
1.83      blambert  144: static void printproto(struct protox *, char *, int);
1.30      millert   145: static void usage(void);
                    146: static struct protox *name2protox(char *);
                    147: static struct protox *knownname(char *);
1.86    ! claudio   148: u_int gettable(const char *);
        !           149:
1.1       deraadt   150:
                    151: kvm_t *kvmd;
                    152:
                    153: int
1.34      deraadt   154: main(int argc, char *argv[])
1.1       deraadt   155: {
                    156:        extern char *optarg;
                    157:        extern int optind;
1.68      claudio   158:        const char *errstr;
1.28      mpech     159:        struct protoent *p;
                    160:        struct protox *tp = NULL; /* for printing cblocks & stats */
1.1       deraadt   161:        int ch;
1.59      markus    162:        char *nlistf = NULL, *memf = NULL, *ep;
1.1       deraadt   163:        char buf[_POSIX2_LINE_MAX];
1.61      djm       164:        gid_t gid;
1.59      markus    165:        u_long pcbaddr = 0;
1.68      claudio   166:        u_int tableid = 0;
1.86    ! claudio   167:        int Tflag = 0;
1.82      tedu      168:        int repeatcount = 0;
1.1       deraadt   169:
                    170:        af = AF_UNSPEC;
                    171:
1.82      tedu      172:        while ((ch = getopt(argc, argv, "Aabc:dFf:gI:ilM:mN:np:P:qrsT:tuvW:w:")) != -1)
1.31      deraadt   173:                switch (ch) {
1.1       deraadt   174:                case 'A':
                    175:                        Aflag = 1;
                    176:                        break;
                    177:                case 'a':
                    178:                        aflag = 1;
1.24      camield   179:                        break;
                    180:                case 'b':
                    181:                        bflag = 1;
1.1       deraadt   182:                        break;
1.82      tedu      183:                case 'c':
                    184:                        repeatcount = strtonum(optarg, 1, INT_MAX, &errstr);
                    185:                        break;
1.1       deraadt   186:                case 'd':
                    187:                        dflag = 1;
1.64      pyr       188:                        break;
                    189:                case 'F':
                    190:                        Fflag = 1;
1.1       deraadt   191:                        break;
                    192:                case 'f':
1.4       mickey    193:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   194:                                af = AF_INET;
1.19      itojun    195:                        else if (strcmp(optarg, "inet6") == 0)
                    196:                                af = AF_INET6;
1.7       kstailey  197:                        else if (strcmp(optarg, "local") == 0)
                    198:                                af = AF_LOCAL;
1.1       deraadt   199:                        else if (strcmp(optarg, "unix") == 0)
                    200:                                af = AF_UNIX;
1.10      angelos   201:                        else if (strcmp(optarg, "encap") == 0)
1.16      angelos   202:                                af = PF_KEY;
1.12      denny     203:                        else if (strcmp(optarg, "atalk") == 0)
                    204:                                af = AF_APPLETALK;
1.75      claudio   205:                        else if (strcmp(optarg, "mpls") == 0)
                    206:                                af = AF_MPLS;
1.76      gollo     207:                        else if (strcmp(optarg, "pflow") == 0)
                    208:                                af = PF_PFLOW;
1.63      claudio   209:                        else if (strcmp(optarg, "mask") == 0)
                    210:                                af = 0xff;
1.1       deraadt   211:                        else {
                    212:                                (void)fprintf(stderr,
                    213:                                    "%s: %s: unknown address family\n",
1.2       deraadt   214:                                    __progname, optarg);
1.1       deraadt   215:                                exit(1);
                    216:                        }
                    217:                        break;
                    218:                case 'g':
                    219:                        gflag = 1;
                    220:                        break;
1.2       deraadt   221:                case 'I':
1.1       deraadt   222:                        iflag = 1;
1.2       deraadt   223:                        interface = optarg;
1.1       deraadt   224:                        break;
                    225:                case 'i':
                    226:                        iflag = 1;
                    227:                        break;
1.19      itojun    228:                case 'l':
                    229:                        lflag = 1;
                    230:                        break;
1.1       deraadt   231:                case 'M':
                    232:                        memf = optarg;
                    233:                        break;
                    234:                case 'm':
                    235:                        mflag = 1;
                    236:                        break;
                    237:                case 'N':
                    238:                        nlistf = optarg;
                    239:                        break;
                    240:                case 'n':
                    241:                        nflag = 1;
                    242:                        break;
                    243:                case 'p':
                    244:                        if ((tp = name2protox(optarg)) == NULL) {
                    245:                                (void)fprintf(stderr,
1.42      jmc       246:                                    "%s: %s: unknown protocol\n",
1.2       deraadt   247:                                    __progname, optarg);
1.1       deraadt   248:                                exit(1);
                    249:                        }
                    250:                        pflag = 1;
1.27      brian     251:                        break;
1.59      markus    252:                case 'P':
                    253:                        errno = 0;
                    254:                        pcbaddr = strtoul(optarg, &ep, 16);
                    255:                        if (optarg[0] == '\0' || *ep != '\0' ||
                    256:                            errno == ERANGE) {
                    257:                                (void)fprintf(stderr,
                    258:                                    "%s: %s: invalid PCB address\n",
                    259:                                    __progname, optarg);
                    260:                                exit(1);
                    261:                        }
                    262:                        Pflag = 1;
                    263:                        break;
1.27      brian     264:                case 'q':
                    265:                        qflag = 1;
1.1       deraadt   266:                        break;
                    267:                case 'r':
                    268:                        rflag = 1;
1.46      cedric    269:                        break;
1.1       deraadt   270:                case 's':
                    271:                        ++sflag;
                    272:                        break;
1.68      claudio   273:                case 'T':
1.86    ! claudio   274:                        Tflag = 1;
        !           275:                        tableid = gettable(optarg);
1.68      claudio   276:                        break;
1.1       deraadt   277:                case 't':
                    278:                        tflag = 1;
                    279:                        break;
                    280:                case 'u':
                    281:                        af = AF_UNIX;
1.13      peter     282:                        break;
                    283:                case 'v':
                    284:                        vflag = 1;
1.1       deraadt   285:                        break;
1.56      reyk      286:                case 'W':
                    287:                        Wflag = 1;
                    288:                        interface = optarg;
                    289:                        break;
1.1       deraadt   290:                case 'w':
                    291:                        interval = atoi(optarg);
                    292:                        iflag = 1;
                    293:                        break;
                    294:                case '?':
                    295:                default:
                    296:                        usage();
                    297:                }
                    298:        argv += optind;
                    299:        argc -= optind;
                    300:
1.33      deraadt   301:        /*
1.56      reyk      302:         * Show per-interface statistics which don't need access to
                    303:         * kernel memory (they're using IOCTLs)
                    304:         */
                    305:        if (Wflag) {
                    306:                if (interface == NULL)
                    307:                        usage();
                    308:                net80211_ifstats(interface);
                    309:                exit(0);
                    310:        }
                    311:
1.84      lum       312: #define        BACKWARD_COMPATIBILITY
                    313: #ifdef BACKWARD_COMPATIBILITY
                    314:        if (*argv) {
                    315:                if (isdigit(**argv)) {
                    316:                        interval = atoi(*argv);
                    317:                        if (interval <= 0)
                    318:                                usage();
                    319:                        ++argv;
                    320:                        iflag = 1;
                    321:                }
                    322:                if (*argv) {
                    323:                        nlistf = *argv;
                    324:                        if (*++argv)
                    325:                                memf = *argv;
                    326:                }
                    327:        }
                    328: #endif
                    329:
1.56      reyk      330:        /*
1.33      deraadt   331:         * Discard setgid privileges if not the running kernel so that bad
                    332:         * guys can't print interesting stuff from kernel memory.
1.59      markus    333:         * Dumping PCB info is also restricted.
1.33      deraadt   334:         */
1.61      djm       335:        gid = getgid();
                    336:        if (nlistf != NULL || memf != NULL || Pflag)
                    337:                if (setresgid(gid, gid, gid) == -1)
                    338:                        err(1, "setresgid");
1.73      claudio   339:
1.33      deraadt   340:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    341:            buf)) == NULL) {
1.78      deraadt   342:                fprintf(stderr, "%s: kvm_openfiles: %s\n", __progname, buf);
1.33      deraadt   343:                exit(1);
                    344:        }
1.61      djm       345:
                    346:        if (nlistf == NULL && memf == NULL && !Pflag)
                    347:                if (setresgid(gid, gid, gid) == -1)
                    348:                        err(1, "setresgid");
1.14      deraadt   349:
1.1       deraadt   350:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    351:                if (nlistf)
1.2       deraadt   352:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    353:                            nlistf);
1.1       deraadt   354:                else
1.2       deraadt   355:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   356:                exit(1);
                    357:        }
                    358:        if (mflag) {
1.70      deraadt   359:                mbpr();
1.1       deraadt   360:                exit(0);
                    361:        }
                    362:        if (pflag) {
1.83      blambert  363:                printproto(tp, tp->pr_name, af);
1.59      markus    364:                exit(0);
                    365:        }
                    366:        if (Pflag) {
                    367:                if (tp == NULL && (tp = name2protox("tcp")) == NULL) {
                    368:                        (void)fprintf(stderr,
                    369:                            "%s: %s: unknown protocol\n",
                    370:                            __progname, "tcp");
                    371:                        exit(1);
                    372:                }
                    373:                if (tp->pr_dump)
                    374:                        (tp->pr_dump)(pcbaddr);
1.1       deraadt   375:                exit(0);
                    376:        }
                    377:        /*
                    378:         * Keep file descriptors open to avoid overhead
                    379:         * of open/close on each call to get* routines.
                    380:         */
                    381:        sethostent(1);
                    382:        setnetent(1);
1.71      deraadt   383:
1.1       deraadt   384:        if (iflag) {
1.82      tedu      385:                intpr(interval, repeatcount);
1.1       deraadt   386:                exit(0);
                    387:        }
                    388:        if (rflag) {
                    389:                if (sflag)
1.73      claudio   390:                        rt_stats();
                    391:                else if (Aflag || nlistf != NULL || memf != NULL)
1.63      claudio   392:                        routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
1.79      claudio   393:                            nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value,
                    394:                            tableid);
1.73      claudio   395:                else
1.86    ! claudio   396:                        p_rttables(af, tableid, Tflag);
1.1       deraadt   397:                exit(0);
                    398:        }
                    399:        if (gflag) {
1.19      itojun    400:                if (sflag) {
                    401:                        if (af == AF_INET || af == AF_UNSPEC)
1.71      deraadt   402:                                mrt_stats();
1.19      itojun    403:                        if (af == AF_INET6 || af == AF_UNSPEC)
1.71      deraadt   404:                                mrt6_stats();
1.70      deraadt   405:                } else {
1.19      itojun    406:                        if (af == AF_INET || af == AF_UNSPEC)
1.71      deraadt   407:                                mroutepr(nl[N_MFCHASHTBL].n_value,
1.19      itojun    408:                                    nl[N_MFCHASH].n_value,
                    409:                                    nl[N_VIFTABLE].n_value);
                    410:                        if (af == AF_INET6 || af == AF_UNSPEC)
1.71      deraadt   411:                                mroute6pr(nl[N_MF6CTABLE].n_value,
1.19      itojun    412:                                    nl[N_MIF6TABLE].n_value);
                    413:                }
1.1       deraadt   414:                exit(0);
                    415:        }
                    416:        if (af == AF_INET || af == AF_UNSPEC) {
                    417:                setprotoent(1);
                    418:                setservent(1);
                    419:                /* ugh, this is O(MN) ... why do we do this? */
1.11      millert   420:                while ((p = getprotoent())) {
1.1       deraadt   421:                        for (tp = protox; tp->pr_name; tp++)
                    422:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    423:                                        break;
1.71      deraadt   424:                        if (tp->pr_name == 0)
1.1       deraadt   425:                                continue;
1.85      dhill     426:                        printproto(tp, p->p_name, AF_INET);
1.1       deraadt   427:                }
                    428:                endprotoent();
1.76      gollo     429:        }
                    430:        if (af == PF_PFLOW || af == AF_UNSPEC) {
                    431:                tp = name2protox("pflow");
1.83      blambert  432:                printproto(tp, tp->pr_name, af);
1.1       deraadt   433:        }
1.19      itojun    434:        if (af == AF_INET6 || af == AF_UNSPEC)
                    435:                for (tp = ip6protox; tp->pr_name; tp++)
1.85      dhill     436:                        printproto(tp, tp->pr_name, AF_INET6);
1.1       deraadt   437:        if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
                    438:                unixpr(nl[N_UNIXSW].n_value);
1.12      denny     439:        if (af == AF_APPLETALK || af == AF_UNSPEC)
                    440:                for (tp = atalkprotox; tp->pr_name; tp++)
1.83      blambert  441:                        printproto(tp, tp->pr_name, af);
1.1       deraadt   442:        exit(0);
                    443: }
                    444:
                    445: /*
                    446:  * Print out protocol statistics or control blocks (per sflag).
                    447:  * If the interface was not specifically requested, and the symbol
                    448:  * is not in the namelist, ignore this one.
                    449:  */
                    450: static void
1.83      blambert  451: printproto(struct protox *tp, char *name, int af)
1.1       deraadt   452: {
                    453:        if (sflag) {
1.71      deraadt   454:                if (tp->pr_stats != NULL)
                    455:                        (*tp->pr_stats)(name);
1.1       deraadt   456:        } else {
1.71      deraadt   457:                u_char i = tp->pr_index;
                    458:                if (tp->pr_cblocks != NULL &&
                    459:                    i < sizeof(nl) / sizeof(nl[0]) &&
                    460:                    (nl[i].n_value || af != AF_UNSPEC))
1.83      blambert  461:                        (*tp->pr_cblocks)(nl[i].n_value, name, af);
1.1       deraadt   462:        }
                    463: }
                    464:
                    465: /*
                    466:  * Read kernel memory, return 0 on success.
                    467:  */
                    468: int
1.53      jaredy    469: kread(u_long addr, void *buf, int size)
1.1       deraadt   470: {
                    471:
                    472:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   473:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   474:                    kvm_geterr(kvmd));
                    475:                return (-1);
                    476:        }
                    477:        return (0);
                    478: }
                    479:
                    480: char *
1.77      claudio   481: plural(u_int64_t n)
1.1       deraadt   482: {
                    483:        return (n != 1 ? "s" : "");
                    484: }
                    485:
                    486: char *
1.77      claudio   487: plurales(u_int64_t n)
1.1       deraadt   488: {
                    489:        return (n != 1 ? "es" : "");
                    490: }
                    491:
                    492: /*
                    493:  * Find the protox for the given "well-known" name.
                    494:  */
                    495: static struct protox *
1.34      deraadt   496: knownname(char *name)
1.1       deraadt   497: {
                    498:        struct protox **tpp, *tp;
                    499:
                    500:        for (tpp = protoprotox; *tpp; tpp++)
                    501:                for (tp = *tpp; tp->pr_name; tp++)
                    502:                        if (strcmp(tp->pr_name, name) == 0)
                    503:                                return (tp);
                    504:        return (NULL);
                    505: }
                    506:
                    507: /*
                    508:  * Find the protox corresponding to name.
                    509:  */
                    510: static struct protox *
1.34      deraadt   511: name2protox(char *name)
1.1       deraadt   512: {
                    513:        struct protox *tp;
                    514:        char **alias;                   /* alias from p->aliases */
                    515:        struct protoent *p;
                    516:
                    517:        /*
                    518:         * Try to find the name in the list of "well-known" names. If that
                    519:         * fails, check if name is an alias for an Internet protocol.
                    520:         */
1.11      millert   521:        if ((tp = knownname(name)))
1.1       deraadt   522:                return (tp);
                    523:
                    524:        setprotoent(1);                 /* make protocol lookup cheaper */
1.11      millert   525:        while ((p = getprotoent())) {
1.1       deraadt   526:                /* assert: name not same as p->name */
                    527:                for (alias = p->p_aliases; *alias; alias++)
                    528:                        if (strcmp(name, *alias) == 0) {
                    529:                                endprotoent();
                    530:                                return (knownname(p->p_name));
                    531:                        }
                    532:        }
                    533:        endprotoent();
                    534:        return (NULL);
                    535: }
                    536:
                    537: static void
1.34      deraadt   538: usage(void)
1.1       deraadt   539: {
                    540:        (void)fprintf(stderr,
1.60      jaredy    541:            "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n"
1.69      sobrado   542:            "       %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
                    543:            "               [-T tableid]\n"
1.82      tedu      544:            "       %s [-bdn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n"
1.60      jaredy    545:            "       %s [-M core] [-N system] -P pcbaddr\n"
                    546:            "       %s [-s] [-M core] [-N system] [-p protocol]\n"
                    547:            "       %s [-a] [-f address_family] [-i | -I interface]\n"
                    548:            "       %s [-W interface]\n",
                    549:            __progname, __progname, __progname, __progname,
                    550:            __progname, __progname, __progname);
1.1       deraadt   551:        exit(1);
                    552: }
1.86    ! claudio   553:
        !           554: u_int
        !           555: gettable(const char *s)
        !           556: {
        !           557:        const char *errstr;
        !           558:        struct rt_tableinfo info;
        !           559:        int mib[6];
        !           560:        size_t len;
        !           561:        u_int tableid;
        !           562:
        !           563:        tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr);
        !           564:        if (errstr)
        !           565:                errx(1, "invalid table id: %s", errstr);
        !           566:
        !           567:        mib[0] = CTL_NET;
        !           568:        mib[1] = AF_ROUTE;
        !           569:        mib[2] = 0;
        !           570:        mib[3] = 0;
        !           571:        mib[4] = NET_RT_TABLE;
        !           572:        mib[5] = tableid;
        !           573:
        !           574:        len = sizeof(info);
        !           575:        if (sysctl(mib, 6, &info, &len, NULL, 0) == -1)
        !           576:                err(1, "routing table %i", tableid);
        !           577:
        !           578:        return (tableid);
        !           579: }
        !           580: