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

1.9     ! angelos     1: /*     $OpenBSD: main.c,v 1.8 1997/02/21 09:09:52 angelos 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.9     ! angelos    47: static char *rcsid = "$OpenBSD: main.c,v 1.8 1997/02/21 09:09:52 angelos 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" },
1.4       mickey    136: #define N_IPX          32
                    137:        { "_ipxcbtable"},
                    138: #define N_IPXSTAT      33
                    139:        { "_ipxstat"},
                    140: #define N_SPXSTAT      34
                    141:        { "_spx_istat"},
                    142: #define N_IPXERR       35
                    143:        { "_ipx_errstat"},
1.8       angelos   144: #define N_AHSTAT       36
                    145:        { "_ahstat"},
                    146: #define N_ESPSTAT      37
                    147:        { "_espstat"},
                    148: #define N_IP4STAT      38
                    149:        { "_ip4stat"},
1.1       deraadt   150:        "",
                    151: };
                    152:
                    153: struct protox {
                    154:        u_char  pr_index;               /* index into nlist of cb head */
                    155:        u_char  pr_sindex;              /* index into nlist of stat block */
                    156:        u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
                    157:        void    (*pr_cblocks)();        /* control blocks printing routine */
                    158:        void    (*pr_stats)();          /* statistics printing routine */
                    159:        char    *pr_name;               /* well-known name */
                    160: } protox[] = {
                    161:        { N_TCBTABLE,   N_TCPSTAT,      1,      protopr,
                    162:          tcp_stats,    "tcp" },
                    163:        { N_UDBTABLE,   N_UDPSTAT,      1,      protopr,
                    164:          udp_stats,    "udp" },
                    165:        { -1,           N_IPSTAT,       1,      0,
                    166:          ip_stats,     "ip" },
                    167:        { -1,           N_ICMPSTAT,     1,      0,
                    168:          icmp_stats,   "icmp" },
                    169:        { -1,           N_IGMPSTAT,     1,      0,
                    170:          igmp_stats,   "igmp" },
1.8       angelos   171:        { -1,           N_AHSTAT,       1,      0,
                    172:          ah_stats,     "sipp-ah" },
                    173:        { -1,           N_ESPSTAT,      1,      0,
                    174:          esp_stats,    "sipp-esp" },
                    175:        { -1,           N_IP4STAT,      1,      0,
1.9     ! angelos   176:          ip4_stats,    "ipencap" },
1.1       deraadt   177:        { -1,           -1,             0,      0,
                    178:          0,            0 }
                    179: };
                    180:
1.4       mickey    181: struct protox ipxprotox[] = {
                    182:        { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
                    183:          ipx_stats,    "ipx" },
                    184:        { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
                    185:          spx_stats,    "spx" },
                    186:        { -1,           N_IPXERR,       1,      0,
                    187:          ipxerr_stats, "ipx_err" },
                    188:        { -1,           -1,             0,      0,
                    189:          0,            0 }
                    190: };
                    191:
1.1       deraadt   192: struct protox nsprotox[] = {
                    193:        { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
                    194:          idp_stats,    "idp" },
                    195:        { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
                    196:          spp_stats,    "spp" },
                    197:        { -1,           N_NSERR,        1,      0,
                    198:          nserr_stats,  "ns_err" },
                    199:        { -1,           -1,             0,      0,
                    200:          0,            0 }
                    201: };
                    202:
                    203: struct protox isoprotox[] = {
                    204:        { ISO_TP,       N_TPSTAT,       1,      iso_protopr,
                    205:          tp_stats,     "tp" },
                    206:        { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
                    207:          cltp_stats,   "cltp" },
                    208:        { -1,           N_CLNPSTAT,     1,       0,
                    209:          clnp_stats,   "clnp"},
                    210:        { -1,           N_ESISSTAT,     1,       0,
                    211:          esis_stats,   "esis"},
                    212:        { -1,           -1,             0,      0,
                    213:          0,            0 }
                    214: };
                    215:
1.4       mickey    216: struct protox *protoprotox[] = { protox, ipxprotox, nsprotox, isoprotox, NULL };
1.1       deraadt   217:
                    218: static void printproto __P((struct protox *, char *));
                    219: static void usage __P((void));
                    220: static struct protox *name2protox __P((char *));
                    221: static struct protox *knownname __P((char *));
                    222:
                    223: kvm_t *kvmd;
                    224:
                    225: int
                    226: main(argc, argv)
                    227:        int argc;
                    228:        char *argv[];
                    229: {
                    230:        extern char *optarg;
                    231:        extern int optind;
                    232:        register struct protoent *p;
                    233:        register struct protox *tp;     /* for printing cblocks & stats */
                    234:        register char *cp;
                    235:        int ch;
                    236:        char *nlistf = NULL, *memf = NULL;
                    237:        char buf[_POSIX2_LINE_MAX];
                    238:
                    239:        af = AF_UNSPEC;
                    240:
1.6       millert   241:        while ((ch = getopt(argc, argv, "Aadf:ghI:iM:mN:np:rstuw:")) != -1)
1.1       deraadt   242:                switch(ch) {
                    243:                case 'A':
                    244:                        Aflag = 1;
                    245:                        break;
                    246:                case 'a':
                    247:                        aflag = 1;
                    248:                        break;
                    249:                case 'd':
                    250:                        dflag = 1;
                    251:                        break;
                    252:                case 'f':
1.4       mickey    253:                        if (strcmp(optarg, "inet") == 0)
1.1       deraadt   254:                                af = AF_INET;
1.7       kstailey  255:                        else if (strcmp(optarg, "local") == 0)
                    256:                                af = AF_LOCAL;
1.1       deraadt   257:                        else if (strcmp(optarg, "unix") == 0)
                    258:                                af = AF_UNIX;
1.4       mickey    259:                        else if (strcmp(optarg, "ipx") == 0)
                    260:                                af = AF_IPX;
                    261:                        else if (strcmp(optarg, "ns") == 0)
                    262:                                af = AF_NS;
1.1       deraadt   263:                        else if (strcmp(optarg, "iso") == 0)
                    264:                                af = AF_ISO;
                    265:                        else {
                    266:                                (void)fprintf(stderr,
                    267:                                    "%s: %s: unknown address family\n",
1.2       deraadt   268:                                    __progname, optarg);
1.1       deraadt   269:                                exit(1);
                    270:                        }
                    271:                        break;
                    272:                case 'g':
                    273:                        gflag = 1;
                    274:                        break;
1.2       deraadt   275:                case 'I':
1.1       deraadt   276:                        iflag = 1;
1.2       deraadt   277:                        interface = optarg;
1.1       deraadt   278:                        break;
                    279:                case 'i':
                    280:                        iflag = 1;
                    281:                        break;
                    282:                case 'M':
                    283:                        memf = optarg;
                    284:                        break;
                    285:                case 'm':
                    286:                        mflag = 1;
                    287:                        break;
                    288:                case 'N':
                    289:                        nlistf = optarg;
                    290:                        break;
                    291:                case 'n':
                    292:                        nflag = 1;
                    293:                        break;
                    294:                case 'p':
                    295:                        if ((tp = name2protox(optarg)) == NULL) {
                    296:                                (void)fprintf(stderr,
                    297:                                    "%s: %s: unknown or uninstrumented protocol\n",
1.2       deraadt   298:                                    __progname, optarg);
1.1       deraadt   299:                                exit(1);
                    300:                        }
                    301:                        pflag = 1;
                    302:                        break;
                    303:                case 'r':
                    304:                        rflag = 1;
                    305:                        break;
                    306:                case 's':
                    307:                        ++sflag;
                    308:                        break;
                    309:                case 't':
                    310:                        tflag = 1;
                    311:                        break;
                    312:                case 'u':
                    313:                        af = AF_UNIX;
                    314:                        break;
                    315:                case 'w':
                    316:                        interval = atoi(optarg);
                    317:                        iflag = 1;
                    318:                        break;
                    319:                case '?':
                    320:                default:
                    321:                        usage();
                    322:                }
                    323:        argv += optind;
                    324:        argc -= optind;
                    325:
                    326: #define        BACKWARD_COMPATIBILITY
                    327: #ifdef BACKWARD_COMPATIBILITY
                    328:        if (*argv) {
                    329:                if (isdigit(**argv)) {
                    330:                        interval = atoi(*argv);
                    331:                        if (interval <= 0)
                    332:                                usage();
                    333:                        ++argv;
                    334:                        iflag = 1;
                    335:                }
                    336:                if (*argv) {
                    337:                        nlistf = *argv;
                    338:                        if (*++argv)
                    339:                                memf = *argv;
                    340:                }
                    341:        }
                    342: #endif
                    343:
                    344:        /*
                    345:         * Discard setgid privileges if not the running kernel so that bad
                    346:         * guys can't print interesting stuff from kernel memory.
                    347:         */
1.5       tholo     348:        if (nlistf != NULL || memf != NULL) {
                    349:                setegid(getgid());
1.1       deraadt   350:                setgid(getgid());
1.5       tholo     351:        }
1.1       deraadt   352:
1.2       deraadt   353:        if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
                    354:            buf)) == NULL) {
                    355:                fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
1.1       deraadt   356:                exit(1);
                    357:        }
                    358:        if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
                    359:                if (nlistf)
1.2       deraadt   360:                        fprintf(stderr, "%s: %s: no namelist\n", __progname,
                    361:                            nlistf);
1.1       deraadt   362:                else
1.2       deraadt   363:                        fprintf(stderr, "%s: no namelist\n", __progname);
1.1       deraadt   364:                exit(1);
                    365:        }
                    366:        if (mflag) {
                    367:                mbpr(nl[N_MBSTAT].n_value);
                    368:                exit(0);
                    369:        }
                    370:        if (pflag) {
                    371:                if (tp->pr_stats)
                    372:                        (*tp->pr_stats)(nl[tp->pr_sindex].n_value,
                    373:                                tp->pr_name);
                    374:                else
                    375:                        printf("%s: no stats routine\n", tp->pr_name);
                    376:                exit(0);
                    377:        }
                    378:        /*
                    379:         * Keep file descriptors open to avoid overhead
                    380:         * of open/close on each call to get* routines.
                    381:         */
                    382:        sethostent(1);
                    383:        setnetent(1);
                    384:        if (iflag) {
                    385:                intpr(interval, nl[N_IFNET].n_value);
                    386:                exit(0);
                    387:        }
                    388:        if (rflag) {
                    389:                if (sflag)
                    390:                        rt_stats(nl[N_RTSTAT].n_value);
                    391:                else
                    392:                        routepr(nl[N_RTREE].n_value);
                    393:                exit(0);
                    394:        }
                    395:        if (gflag) {
                    396:                if (sflag)
                    397:                        mrt_stats(nl[N_MRTPROTO].n_value,
                    398:                            nl[N_MRTSTAT].n_value);
                    399:                else
                    400:                        mroutepr(nl[N_MRTPROTO].n_value,
                    401:                            nl[N_MFCHASHTBL].n_value,
                    402:                            nl[N_MFCHASH].n_value,
                    403:                            nl[N_VIFTABLE].n_value);
                    404:                exit(0);
                    405:        }
                    406:        if (af == AF_INET || af == AF_UNSPEC) {
                    407:                setprotoent(1);
                    408:                setservent(1);
                    409:                /* ugh, this is O(MN) ... why do we do this? */
                    410:                while (p = getprotoent()) {
                    411:                        for (tp = protox; tp->pr_name; tp++)
                    412:                                if (strcmp(tp->pr_name, p->p_name) == 0)
                    413:                                        break;
                    414:                        if (tp->pr_name == 0 || tp->pr_wanted == 0)
                    415:                                continue;
                    416:                        printproto(tp, p->p_name);
                    417:                }
                    418:                endprotoent();
                    419:        }
1.4       mickey    420:        if (af == AF_IPX || af == AF_UNSPEC)
                    421:                for (tp = ipxprotox; tp->pr_name; tp++)
                    422:                        printproto(tp, tp->pr_name);
1.1       deraadt   423:        if (af == AF_NS || af == AF_UNSPEC)
                    424:                for (tp = nsprotox; tp->pr_name; tp++)
                    425:                        printproto(tp, tp->pr_name);
                    426:        if (af == AF_ISO || af == AF_UNSPEC)
                    427:                for (tp = isoprotox; tp->pr_name; tp++)
                    428:                        printproto(tp, tp->pr_name);
                    429:        if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
                    430:                unixpr(nl[N_UNIXSW].n_value);
                    431:        exit(0);
                    432: }
                    433:
                    434: /*
                    435:  * Print out protocol statistics or control blocks (per sflag).
                    436:  * If the interface was not specifically requested, and the symbol
                    437:  * is not in the namelist, ignore this one.
                    438:  */
                    439: static void
                    440: printproto(tp, name)
                    441:        register struct protox *tp;
                    442:        char *name;
                    443: {
                    444:        void (*pr)();
                    445:        u_long off;
                    446:
                    447:        if (sflag) {
                    448:                pr = tp->pr_stats;
                    449:                off = nl[tp->pr_sindex].n_value;
                    450:        } else {
                    451:                pr = tp->pr_cblocks;
                    452:                off = nl[tp->pr_index].n_value;
                    453:        }
                    454:        if (pr != NULL && (off || af != AF_UNSPEC))
                    455:                (*pr)(off, name);
                    456: }
                    457:
                    458: /*
                    459:  * Read kernel memory, return 0 on success.
                    460:  */
                    461: int
                    462: kread(addr, buf, size)
                    463:        u_long addr;
                    464:        char *buf;
                    465:        int size;
                    466: {
                    467:
                    468:        if (kvm_read(kvmd, addr, buf, size) != size) {
1.2       deraadt   469:                (void)fprintf(stderr, "%s: %s\n", __progname,
1.1       deraadt   470:                    kvm_geterr(kvmd));
                    471:                return (-1);
                    472:        }
                    473:        return (0);
                    474: }
                    475:
                    476: char *
                    477: plural(n)
                    478:        int n;
                    479: {
                    480:        return (n != 1 ? "s" : "");
                    481: }
                    482:
                    483: char *
                    484: plurales(n)
                    485:        int n;
                    486: {
                    487:        return (n != 1 ? "es" : "");
                    488: }
                    489:
                    490: /*
                    491:  * Find the protox for the given "well-known" name.
                    492:  */
                    493: static struct protox *
                    494: knownname(name)
                    495:        char *name;
                    496: {
                    497:        struct protox **tpp, *tp;
                    498:
                    499:        for (tpp = protoprotox; *tpp; tpp++)
                    500:                for (tp = *tpp; tp->pr_name; tp++)
                    501:                        if (strcmp(tp->pr_name, name) == 0)
                    502:                                return (tp);
                    503:        return (NULL);
                    504: }
                    505:
                    506: /*
                    507:  * Find the protox corresponding to name.
                    508:  */
                    509: static struct protox *
                    510: name2protox(name)
                    511:        char *name;
                    512: {
                    513:        struct protox *tp;
                    514:        char **alias;                   /* alias from p->aliases */
                    515:        struct protoent *p;
                    516:
                    517:        /*
                    518:         * Try to find the name in the list of "well-known" names. If that
                    519:         * fails, check if name is an alias for an Internet protocol.
                    520:         */
                    521:        if (tp = knownname(name))
                    522:                return (tp);
                    523:
                    524:        setprotoent(1);                 /* make protocol lookup cheaper */
                    525:        while (p = getprotoent()) {
                    526:                /* assert: name not same as p->name */
                    527:                for (alias = p->p_aliases; *alias; alias++)
                    528:                        if (strcmp(name, *alias) == 0) {
                    529:                                endprotoent();
                    530:                                return (knownname(p->p_name));
                    531:                        }
                    532:        }
                    533:        endprotoent();
                    534:        return (NULL);
                    535: }
                    536:
                    537: static void
                    538: usage()
                    539: {
                    540:        (void)fprintf(stderr,
1.2       deraadt   541: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   542:        (void)fprintf(stderr,
1.2       deraadt   543: "       %s [-ghimnrs] [-f address_family] [-M core] [-N system]\n", __progname);
1.1       deraadt   544:        (void)fprintf(stderr,
1.2       deraadt   545: "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", __progname);
1.1       deraadt   546:        (void)fprintf(stderr,
1.2       deraadt   547: "       %s [-M core] [-N system] [-p protocol]\n", __progname);
1.1       deraadt   548:        exit(1);
                    549: }