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

1.3     ! deraadt     1: /*     $OpenBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej 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.
                     16:  * 3. All advertising materials mentioning features or use of this software
                     17:  *    must display the following acknowledgement:
                     18:  *     This product includes software developed by the University of
                     19:  *     California, Berkeley and its contributors.
                     20:  * 4. Neither the name of the University nor the names of its contributors
                     21:  *    may be used to endorse or promote products derived from this software
                     22:  *    without specific prior written permission.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     25:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     26:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     27:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     28:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     29:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     30:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     31:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     32:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     33:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     34:  * SUCH DAMAGE.
                     35:  */
                     36:
                     37: #ifndef lint
                     38: char copyright[] =
                     39: "@(#) Copyright (c) 1983, 1988, 1993\n\
                     40:        Regents of the University of California.  All rights reserved.\n";
                     41: #endif /* not lint */
                     42:
                     43: #ifndef lint
                     44: #if 0
                     45: static char sccsid[] = "from: @(#)main.c       8.4 (Berkeley) 3/1/94";
                     46: #else
1.3     ! deraadt    47: static char *rcsid = "$OpenBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $";
1.1       deraadt    48: #endif
                     49: #endif /* not lint */
                     50:
                     51: #include <sys/param.h>
                     52: #include <sys/file.h>
                     53: #include <sys/protosw.h>
                     54: #include <sys/socket.h>
                     55:
                     56: #include <netinet/in.h>
                     57:
                     58: #include <ctype.h>
                     59: #include <errno.h>
                     60: #include <kvm.h>
                     61: #include <limits.h>
                     62: #include <netdb.h>
                     63: #include <nlist.h>
                     64: #include <paths.h>
                     65: #include <stdio.h>
                     66: #include <stdlib.h>
                     67: #include <string.h>
                     68: #include <unistd.h>
                     69: #include "netstat.h"
                     70:
                     71: struct nlist nl[] = {
                     72: #define        N_MBSTAT        0
                     73:        { "_mbstat" },
                     74: #define        N_IPSTAT        1
                     75:        { "_ipstat" },
                     76: #define        N_TCBTABLE      2
                     77:        { "_tcbtable" },
                     78: #define        N_TCPSTAT       3
                     79:        { "_tcpstat" },
                     80: #define        N_UDBTABLE      4
                     81:        { "_udbtable" },
                     82: #define        N_UDPSTAT       5
                     83:        { "_udpstat" },
                     84: #define        N_IFNET         6
                     85:        { "_ifnet" },
                     86: #define        N_IMP           7
                     87:        { "_imp_softc" },
                     88: #define        N_ICMPSTAT      8
                     89:        { "_icmpstat" },
                     90: #define        N_RTSTAT        9
                     91:        { "_rtstat" },
                     92: #define        N_UNIXSW        10
                     93:        { "_unixsw" },
                     94: #define N_IDP          11
                     95:        { "_nspcb"},
                     96: #define N_IDPSTAT      12
                     97:        { "_idpstat"},
                     98: #define N_SPPSTAT      13
                     99:        { "_spp_istat"},
                    100: #define N_NSERR                14
                    101:        { "_ns_errstat"},
                    102: #define        N_CLNPSTAT      15
                    103:        { "_clnp_stat"},
                    104: #define        IN_NOTUSED      16
                    105:        { "_tp_inpcb" },
                    106: #define        ISO_TP          17
                    107:        { "_tp_refinfo" },
                    108: #define        N_TPSTAT        18
                    109:        { "_tp_stat" },
                    110: #define        N_ESISSTAT      19
                    111:        { "_esis_stat"},
                    112: #define N_NIMP         20
                    113:        { "_nimp"},
                    114: #define N_RTREE                21
                    115:        { "_rt_tables"},
                    116: #define N_CLTP         22
                    117:        { "_cltb"},
                    118: #define N_CLTPSTAT     23
                    119:        { "_cltpstat"},
                    120: #define        N_NFILE         24
                    121:        { "_nfile" },
                    122: #define        N_FILE          25
                    123:        { "_file" },
                    124: #define N_IGMPSTAT     26
                    125:        { "_igmpstat" },
                    126: #define N_MRTPROTO     27
                    127:        { "_ip_mrtproto" },
                    128: #define N_MRTSTAT      28
                    129:        { "_mrtstat" },
                    130: #define N_MFCHASHTBL   29
                    131:        { "_mfchashtbl" },
                    132: #define        N_MFCHASH       30
                    133:        { "_mfchash" },
                    134: #define N_VIFTABLE     31
                    135:        { "_viftable" },
                    136:        "",
                    137: };
                    138:
                    139: struct protox {
                    140:        u_char  pr_index;               /* index into nlist of cb head */
                    141:        u_char  pr_sindex;              /* index into nlist of stat block */
                    142:        u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
                    143:        void    (*pr_cblocks)();        /* control blocks printing routine */
                    144:        void    (*pr_stats)();          /* statistics printing routine */
                    145:        char    *pr_name;               /* well-known name */
                    146: } protox[] = {
                    147:        { N_TCBTABLE,   N_TCPSTAT,      1,      protopr,
                    148:          tcp_stats,    "tcp" },
                    149:        { N_UDBTABLE,   N_UDPSTAT,      1,      protopr,
                    150:          udp_stats,    "udp" },
                    151:        { -1,           N_IPSTAT,       1,      0,
                    152:          ip_stats,     "ip" },
                    153:        { -1,           N_ICMPSTAT,     1,      0,
                    154:          icmp_stats,   "icmp" },
                    155:        { -1,           N_IGMPSTAT,     1,      0,
                    156:          igmp_stats,   "igmp" },
                    157:        { -1,           -1,             0,      0,
                    158:          0,            0 }
                    159: };
                    160:
                    161: struct protox nsprotox[] = {
                    162:        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
                    163:          idp_stats,    "idp" },
                    164:        { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
                    165:          spp_stats,    "spp" },
                    166:        { -1,           N_NSERR,        1,      0,
                    167:          nserr_stats,  "ns_err" },
                    168:        { -1,           -1,             0,      0,
                    169:          0,            0 }
                    170: };
                    171:
                    172: struct protox isoprotox[] = {
                    173:        { ISO_TP,       N_TPSTAT,       1,      iso_protopr,
                    174:          tp_stats,     "tp" },
                    175:        { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
                    176:          cltp_stats,   "cltp" },
                    177:        { -1,           N_CLNPSTAT,     1,       0,
                    178:          clnp_stats,   "clnp"},
                    179:        { -1,           N_ESISSTAT,     1,       0,
                    180:          esis_stats,   "esis"},
                    181:        { -1,           -1,             0,      0,
                    182:          0,            0 }
                    183: };
                    184:
                    185: struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
                    186:
                    187: static void printproto __P((struct protox *, char *));
                    188: static void usage __P((void));
                    189: static struct protox *name2protox __P((char *));
                    190: static struct protox *knownname __P((char *));
                    191:
                    192: kvm_t *kvmd;
                    193:
                    194: int
                    195: main(argc, argv)
                    196:        int argc;
                    197:        char *argv[];
                    198: {
                    199:        extern char *optarg;
                    200:        extern int optind;
                    201:        register struct protoent *p;
                    202:        register struct protox *tp;     /* for printing cblocks & stats */
                    203:        register char *cp;
                    204:        int ch;
                    205:        char *nlistf = NULL, *memf = NULL;
                    206:        char buf[_POSIX2_LINE_MAX];
                    207:
                    208:        af = AF_UNSPEC;
                    209:
                    210:        while ((ch = getopt(argc, argv, "Aadf:ghI:iM:mN:np:rstuw:")) != EOF)
                    211:                switch(ch) {
                    212:                case 'A':
                    213:                        Aflag = 1;
                    214:                        break;
                    215:                case 'a':
                    216:                        aflag = 1;
                    217:                        break;
                    218:                case 'd':
                    219:                        dflag = 1;
                    220:                        break;
                    221:                case 'f':
                    222:                        if (strcmp(optarg, "ns") == 0)
                    223:                                af = AF_NS;
                    224:                        else if (strcmp(optarg, "inet") == 0)
                    225:                                af = AF_INET;
                    226:                        else if (strcmp(optarg, "unix") == 0)
                    227:                                af = AF_UNIX;
                    228:                        else if (strcmp(optarg, "iso") == 0)
                    229:                                af = AF_ISO;
                    230:                        else {
                    231:                                (void)fprintf(stderr,
                    232:                                    "%s: %s: unknown address family\n",
1.2       deraadt   233:                                    __progname, optarg);
1.1       deraadt   234:                                exit(1);
                    235:                        }
                    236:                        break;
                    237:                case 'g':
                    238:                        gflag = 1;
                    239:                        break;
1.2       deraadt   240:                case 'I':
1.1       deraadt   241:                        iflag = 1;
1.2       deraadt   242:                        interface = optarg;
1.1       deraadt   243:                        break;
                    244:                case 'i':
                    245:                        iflag = 1;
                    246:                        break;
                    247:                case 'M':
                    248:                        memf = optarg;
                    249:                        break;
                    250:                case 'm':
                    251:                        mflag = 1;
                    252:                        break;
                    253:                case 'N':
                    254:                        nlistf = optarg;
                    255:                        break;
                    256:                case 'n':
                    257:                        nflag = 1;
                    258:                        break;
                    259:                case 'p':
                    260:                        if ((tp = name2protox(optarg)) == NULL) {
                    261:                                (void)fprintf(stderr,
                    262:                                    "%s: %s: unknown or uninstrumented protocol\n",
1.2       deraadt   263:                                    __progname, optarg);
1.1       deraadt   264:                                exit(1);
                    265:                        }
                    266:                        pflag = 1;
                    267:                        break;
                    268:                case 'r':
                    269:                        rflag = 1;
                    270:                        break;
                    271:                case 's':
                    272:                        ++sflag;
                    273:                        break;
                    274:                case 't':
                    275:                        tflag = 1;
                    276:                        break;
                    277:                case 'u':
                    278:                        af = AF_UNIX;
                    279:                        break;
                    280:                case 'w':
                    281:                        interval = atoi(optarg);
                    282:                        iflag = 1;
                    283:                        break;
                    284:                case '?':
                    285:                default:
                    286:                        usage();
                    287:                }
                    288:        argv += optind;
                    289:        argc -= optind;
                    290:
                    291: #define        BACKWARD_COMPATIBILITY
                    292: #ifdef BACKWARD_COMPATIBILITY
                    293:        if (*argv) {
                    294:                if (isdigit(**argv)) {
                    295:                        interval = atoi(*argv);
                    296:                        if (interval <= 0)
                    297:                                usage();
                    298:                        ++argv;
                    299:                        iflag = 1;
                    300:                }
                    301:                if (*argv) {
                    302:                        nlistf = *argv;
                    303:                        if (*++argv)
                    304:                                memf = *argv;
                    305:                }
                    306:        }
                    307: #endif
                    308:
                    309:        /*
                    310:         * Discard setgid privileges if not the running kernel so that bad
                    311:         * guys can't print interesting stuff from kernel memory.
                    312:         */
                    313:        if (nlistf != NULL || memf != NULL)
                    314:                setgid(getgid());
                    315:
1.2       deraadt   316:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    317:            buf)) == NULL) {
                    318:                fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
1.1       deraadt   319:                exit(1);
                    320:        }
                    321:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    322:                if (nlistf)
1.2       deraadt   323:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    324:                            nlistf);
1.1       deraadt   325:                else
1.2       deraadt   326:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   327:                exit(1);
                    328:        }
                    329:        if (mflag) {
                    330:                mbpr(nl[N_MBSTAT].n_value);
                    331:                exit(0);
                    332:        }
                    333:        if (pflag) {
                    334:                if (tp->pr_stats)
                    335:                        (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
                    336:                                tp->pr_name);
                    337:                else
                    338:                        printf("%s: no stats routine\n", tp->pr_name);
                    339:                exit(0);
                    340:        }
                    341:        /*
                    342:         * Keep file descriptors open to avoid overhead
                    343:         * of open/close on each call to get* routines.
                    344:         */
                    345:        sethostent(1);
                    346:        setnetent(1);
                    347:        if (iflag) {
                    348:                intpr(interval, nl[N_IFNET].n_value);
                    349:                exit(0);
                    350:        }
                    351:        if (rflag) {
                    352:                if (sflag)
                    353:                        rt_stats(nl[N_RTSTAT].n_value);
                    354:                else
                    355:                        routepr(nl[N_RTREE].n_value);
                    356:                exit(0);
                    357:        }
                    358:        if (gflag) {
                    359:                if (sflag)
                    360:                        mrt_stats(nl[N_MRTPROTO].n_value,
                    361:                            nl[N_MRTSTAT].n_value);
                    362:                else
                    363:                        mroutepr(nl[N_MRTPROTO].n_value,
                    364:                            nl[N_MFCHASHTBL].n_value,
                    365:                            nl[N_MFCHASH].n_value,
                    366:                            nl[N_VIFTABLE].n_value);
                    367:                exit(0);
                    368:        }
                    369:        if (af == AF_INET || af == AF_UNSPEC) {
                    370:                setprotoent(1);
                    371:                setservent(1);
                    372:                /* ugh, this is O(MN) ... why do we do this? */
                    373:                while (p = getprotoent()) {
                    374:                        for (tp = protox; tp->pr_name; tp++)
                    375:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    376:                                        break;
                    377:                        if (tp->pr_name == 0 || tp->pr_wanted == 0)
                    378:                                continue;
                    379:                        printproto(tp, p->p_name);
                    380:                }
                    381:                endprotoent();
                    382:        }
                    383:        if (af == AF_NS || af == AF_UNSPEC)
                    384:                for (tp = nsprotox; tp->pr_name; tp++)
                    385:                        printproto(tp, tp->pr_name);
                    386:        if (af == AF_ISO || af == AF_UNSPEC)
                    387:                for (tp = isoprotox; tp->pr_name; tp++)
                    388:                        printproto(tp, tp->pr_name);
                    389:        if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
                    390:                unixpr(nl[N_UNIXSW].n_value);
                    391:        exit(0);
                    392: }
                    393:
                    394: /*
                    395:  * Print out protocol statistics or control blocks (per sflag).
                    396:  * If the interface was not specifically requested, and the symbol
                    397:  * is not in the namelist, ignore this one.
                    398:  */
                    399: static void
                    400: printproto(tp, name)
                    401:        register struct protox *tp;
                    402:        char *name;
                    403: {
                    404:        void (*pr)();
                    405:        u_long off;
                    406:
                    407:        if (sflag) {
                    408:                pr = tp->pr_stats;
                    409:                off = nl[tp->pr_sindex].n_value;
                    410:        } else {
                    411:                pr = tp->pr_cblocks;
                    412:                off = nl[tp->pr_index].n_value;
                    413:        }
                    414:        if (pr != NULL && (off || af != AF_UNSPEC))
                    415:                (*pr)(off, name);
                    416: }
                    417:
                    418: /*
                    419:  * Read kernel memory, return 0 on success.
                    420:  */
                    421: int
                    422: kread(addr, buf, size)
                    423:        u_long addr;
                    424:        char *buf;
                    425:        int size;
                    426: {
                    427:
                    428:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   429:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   430:                    kvm_geterr(kvmd));
                    431:                return (-1);
                    432:        }
                    433:        return (0);
                    434: }
                    435:
                    436: char *
                    437: plural(n)
                    438:        int n;
                    439: {
                    440:        return (n != 1 ? "s" : "");
                    441: }
                    442:
                    443: char *
                    444: plurales(n)
                    445:        int n;
                    446: {
                    447:        return (n != 1 ? "es" : "");
                    448: }
                    449:
                    450: /*
                    451:  * Find the protox for the given "well-known" name.
                    452:  */
                    453: static struct protox *
                    454: knownname(name)
                    455:        char *name;
                    456: {
                    457:        struct protox **tpp, *tp;
                    458:
                    459:        for (tpp = protoprotox; *tpp; tpp++)
                    460:                for (tp = *tpp; tp->pr_name; tp++)
                    461:                        if (strcmp(tp->pr_name, name) == 0)
                    462:                                return (tp);
                    463:        return (NULL);
                    464: }
                    465:
                    466: /*
                    467:  * Find the protox corresponding to name.
                    468:  */
                    469: static struct protox *
                    470: name2protox(name)
                    471:        char *name;
                    472: {
                    473:        struct protox *tp;
                    474:        char **alias;                   /* alias from p->aliases */
                    475:        struct protoent *p;
                    476:
                    477:        /*
                    478:         * Try to find the name in the list of "well-known" names. If that
                    479:         * fails, check if name is an alias for an Internet protocol.
                    480:         */
                    481:        if (tp = knownname(name))
                    482:                return (tp);
                    483:
                    484:        setprotoent(1);                 /* make protocol lookup cheaper */
                    485:        while (p = getprotoent()) {
                    486:                /* assert: name not same as p->name */
                    487:                for (alias = p->p_aliases; *alias; alias++)
                    488:                        if (strcmp(name, *alias) == 0) {
                    489:                                endprotoent();
                    490:                                return (knownname(p->p_name));
                    491:                        }
                    492:        }
                    493:        endprotoent();
                    494:        return (NULL);
                    495: }
                    496:
                    497: static void
                    498: usage()
                    499: {
                    500:        (void)fprintf(stderr,
1.2       deraadt   501: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   502:        (void)fprintf(stderr,
1.2       deraadt   503: "       %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   504:        (void)fprintf(stderr,
1.2       deraadt   505: "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
1.1       deraadt   506:        (void)fprintf(stderr,
1.2       deraadt   507: "       %s [-M core] [-N system] [-p protocol]\n", __progname);
1.1       deraadt   508:        exit(1);
                    509: }