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

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