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

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