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

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