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

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