Annotation of 43BSDTahoe/ucb/netstat/inet.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1983, 1988 Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * Redistribution and use in source and binary forms are permitted
        !             6:  * provided that the above copyright notice and this paragraph are
        !             7:  * duplicated in all such forms and that any documentation,
        !             8:  * advertising materials, and other materials related to such
        !             9:  * distribution and use acknowledge that the software was developed
        !            10:  * by the University of California, Berkeley.  The name of the
        !            11:  * University may not be used to endorse or promote products derived
        !            12:  * from this software without specific prior written permission.
        !            13:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            14:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            15:  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            16:  */
        !            17: 
        !            18: #ifndef lint
        !            19: static char sccsid[] = "@(#)inet.c     5.11 (Berkeley) 6/29/88";
        !            20: #endif /* not lint */
        !            21: 
        !            22: #include <strings.h>
        !            23: #include <stdio.h>
        !            24: 
        !            25: #include <sys/param.h>
        !            26: #include <sys/socket.h>
        !            27: #include <sys/socketvar.h>
        !            28: #include <sys/mbuf.h>
        !            29: #include <sys/protosw.h>
        !            30: 
        !            31: #include <net/route.h>
        !            32: #include <netinet/in.h>
        !            33: #include <netinet/in_systm.h>
        !            34: #include <netinet/in_pcb.h>
        !            35: #include <netinet/ip.h>
        !            36: #include <netinet/ip_icmp.h>
        !            37: #include <netinet/icmp_var.h>
        !            38: #include <netinet/ip_var.h>
        !            39: #include <netinet/tcp.h>
        !            40: #include <netinet/tcpip.h>
        !            41: #include <netinet/tcp_seq.h>
        !            42: #define TCPSTATES
        !            43: #include <netinet/tcp_fsm.h>
        !            44: #include <netinet/tcp_timer.h>
        !            45: #include <netinet/tcp_var.h>
        !            46: #include <netinet/tcp_debug.h>
        !            47: #include <netinet/udp.h>
        !            48: #include <netinet/udp_var.h>
        !            49: 
        !            50: #include <netdb.h>
        !            51: 
        !            52: struct inpcb inpcb;
        !            53: struct tcpcb tcpcb;
        !            54: struct socket sockb;
        !            55: extern int kmem;
        !            56: extern int Aflag;
        !            57: extern int aflag;
        !            58: extern int nflag;
        !            59: extern char *plural();
        !            60: 
        !            61: char   *inetname();
        !            62: 
        !            63: /*
        !            64:  * Print a summary of connections related to an Internet
        !            65:  * protocol.  For TCP, also give state of connection.
        !            66:  * Listening processes (aflag) are suppressed unless the
        !            67:  * -a (all) flag is specified.
        !            68:  */
        !            69: protopr(off, name)
        !            70:        off_t off;
        !            71:        char *name;
        !            72: {
        !            73:        struct inpcb cb;
        !            74:        register struct inpcb *prev, *next;
        !            75:        int istcp;
        !            76:        static int first = 1;
        !            77: 
        !            78:        if (off == 0)
        !            79:                return;
        !            80:        istcp = strcmp(name, "tcp") == 0;
        !            81:        klseek(kmem, off, 0);
        !            82:        read(kmem, (char *)&cb, sizeof (struct inpcb));
        !            83:        inpcb = cb;
        !            84:        prev = (struct inpcb *)off;
        !            85:        if (inpcb.inp_next == (struct inpcb *)off)
        !            86:                return;
        !            87:        while (inpcb.inp_next != (struct inpcb *)off) {
        !            88: 
        !            89:                next = inpcb.inp_next;
        !            90:                klseek(kmem, (off_t)next, 0);
        !            91:                read(kmem, (char *)&inpcb, sizeof (inpcb));
        !            92:                if (inpcb.inp_prev != prev) {
        !            93:                        printf("???\n");
        !            94:                        break;
        !            95:                }
        !            96:                if (!aflag &&
        !            97:                  inet_lnaof(inpcb.inp_laddr) == INADDR_ANY) {
        !            98:                        prev = next;
        !            99:                        continue;
        !           100:                }
        !           101:                klseek(kmem, (off_t)inpcb.inp_socket, 0);
        !           102:                read(kmem, (char *)&sockb, sizeof (sockb));
        !           103:                if (istcp) {
        !           104:                        klseek(kmem, (off_t)inpcb.inp_ppcb, 0);
        !           105:                        read(kmem, (char *)&tcpcb, sizeof (tcpcb));
        !           106:                }
        !           107:                if (first) {
        !           108:                        printf("Active Internet connections");
        !           109:                        if (aflag)
        !           110:                                printf(" (including servers)");
        !           111:                        putchar('\n');
        !           112:                        if (Aflag)
        !           113:                                printf("%-8.8s ", "PCB");
        !           114:                        printf(Aflag ?
        !           115:                                "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
        !           116:                                "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
        !           117:                                "Proto", "Recv-Q", "Send-Q",
        !           118:                                "Local Address", "Foreign Address", "(state)");
        !           119:                        first = 0;
        !           120:                }
        !           121:                if (Aflag)
        !           122:                        if (istcp)
        !           123:                                printf("%8x ", inpcb.inp_ppcb);
        !           124:                        else
        !           125:                                printf("%8x ", next);
        !           126:                printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
        !           127:                        sockb.so_snd.sb_cc);
        !           128:                inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name);
        !           129:                inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name);
        !           130:                if (istcp) {
        !           131:                        if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
        !           132:                                printf(" %d", tcpcb.t_state);
        !           133:                        else
        !           134:                                printf(" %s", tcpstates[tcpcb.t_state]);
        !           135:                }
        !           136:                putchar('\n');
        !           137:                prev = next;
        !           138:        }
        !           139: }
        !           140: 
        !           141: /*
        !           142:  * Dump TCP statistics structure.
        !           143:  */
        !           144: tcp_stats(off, name)
        !           145:        off_t off;
        !           146:        char *name;
        !           147: {
        !           148:        struct tcpstat tcpstat;
        !           149: 
        !           150:        if (off == 0)
        !           151:                return;
        !           152:        printf ("%s:\n", name);
        !           153:        klseek(kmem, off, 0);
        !           154:        read(kmem, (char *)&tcpstat, sizeof (tcpstat));
        !           155: 
        !           156: #define        p(f, m)         printf(m, tcpstat.f, plural(tcpstat.f))
        !           157: #define        p2(f1, f2, m)   printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
        !           158:   
        !           159:        p(tcps_sndtotal, "\t%d packet%s sent\n");
        !           160:        p2(tcps_sndpack,tcps_sndbyte,
        !           161:                "\t\t%d data packet%s (%d byte%s)\n");
        !           162:        p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
        !           163:                "\t\t%d data packet%s (%d byte%s) retransmitted\n");
        !           164:        p2(tcps_sndacks, tcps_delack,
        !           165:                "\t\t%d ack-only packet%s (%d delayed)\n");
        !           166:        p(tcps_sndurg, "\t\t%d URG only packet%s\n");
        !           167:        p(tcps_sndprobe, "\t\t%d window probe packet%s\n");
        !           168:        p(tcps_sndwinup, "\t\t%d window update packet%s\n");
        !           169:        p(tcps_sndctrl, "\t\t%d control packet%s\n");
        !           170:        p(tcps_rcvtotal, "\t%d packet%s received\n");
        !           171:        p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%d ack%s (for %d byte%s)\n");
        !           172:        p(tcps_rcvdupack, "\t\t%d duplicate ack%s\n");
        !           173:        p(tcps_rcvacktoomuch, "\t\t%d ack%s for unsent data\n");
        !           174:        p2(tcps_rcvpack, tcps_rcvbyte,
        !           175:                "\t\t%d packet%s (%d byte%s) received in-sequence\n");
        !           176:        p2(tcps_rcvduppack, tcps_rcvdupbyte,
        !           177:                "\t\t%d completely duplicate packet%s (%d byte%s)\n");
        !           178:        p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
        !           179:                "\t\t%d packet%s with some dup. data (%d byte%s duped)\n");
        !           180:        p2(tcps_rcvoopack, tcps_rcvoobyte,
        !           181:                "\t\t%d out-of-order packet%s (%d byte%s)\n");
        !           182:        p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
        !           183:                "\t\t%d packet%s (%d byte%s) of data after window\n");
        !           184:        p(tcps_rcvwinprobe, "\t\t%d window probe%s\n");
        !           185:        p(tcps_rcvwinupd, "\t\t%d window update packet%s\n");
        !           186:        p(tcps_rcvafterclose, "\t\t%d packet%s received after close\n");
        !           187:        p(tcps_rcvbadsum, "\t\t%d discarded for bad checksum%s\n");
        !           188:        p(tcps_rcvbadoff, "\t\t%d discarded for bad header offset field%s\n");
        !           189:        p(tcps_rcvshort, "\t\t%d discarded because packet too short\n");
        !           190:        p(tcps_connattempt, "\t%d connection request%s\n");
        !           191:        p(tcps_accepts, "\t%d connection accept%s\n");
        !           192:        p(tcps_connects, "\t%d connection%s established (including accepts)\n");
        !           193:        p2(tcps_closed, tcps_drops,
        !           194:                "\t%d connection%s closed (including %d drop%s)\n");
        !           195:        p(tcps_conndrops, "\t%d embryonic connection%s dropped\n");
        !           196:        p2(tcps_rttupdated, tcps_segstimed,
        !           197:                "\t%d segment%s updated rtt (of %d attempt%s)\n");
        !           198:        p(tcps_rexmttimeo, "\t%d retransmit timeout%s\n");
        !           199:        p(tcps_timeoutdrop, "\t\t%d connection%s dropped by rexmit timeout\n");
        !           200:        p(tcps_persisttimeo, "\t%d persist timeout%s\n");
        !           201:        p(tcps_keeptimeo, "\t%d keepalive timeout%s\n");
        !           202:        p(tcps_keepprobe, "\t\t%d keepalive probe%s sent\n");
        !           203:        p(tcps_keepdrops, "\t\t%d connection%s dropped by keepalive\n");
        !           204: #undef p
        !           205: #undef p2
        !           206: }
        !           207: 
        !           208: /*
        !           209:  * Dump UDP statistics structure.
        !           210:  */
        !           211: udp_stats(off, name)
        !           212:        off_t off;
        !           213:        char *name;
        !           214: {
        !           215:        struct udpstat udpstat;
        !           216: 
        !           217:        if (off == 0)
        !           218:                return;
        !           219:        klseek(kmem, off, 0);
        !           220:        read(kmem, (char *)&udpstat, sizeof (udpstat));
        !           221:        printf("%s:\n\t%u incomplete header%s\n", name,
        !           222:                udpstat.udps_hdrops, plural(udpstat.udps_hdrops));
        !           223:        printf("\t%u bad data length field%s\n",
        !           224:                udpstat.udps_badlen, plural(udpstat.udps_badlen));
        !           225:        printf("\t%u bad checksum%s\n",
        !           226:                udpstat.udps_badsum, plural(udpstat.udps_badsum));
        !           227: }
        !           228: 
        !           229: /*
        !           230:  * Dump IP statistics structure.
        !           231:  */
        !           232: ip_stats(off, name)
        !           233:        off_t off;
        !           234:        char *name;
        !           235: {
        !           236:        struct ipstat ipstat;
        !           237: 
        !           238:        if (off == 0)
        !           239:                return;
        !           240:        klseek(kmem, off, 0);
        !           241:        read(kmem, (char *)&ipstat, sizeof (ipstat));
        !           242:        printf("%s:\n\t%u total packets received\n", name,
        !           243:                ipstat.ips_total);
        !           244:        printf("\t%u bad header checksum%s\n",
        !           245:                ipstat.ips_badsum, plural(ipstat.ips_badsum));
        !           246:        printf("\t%u with size smaller than minimum\n", ipstat.ips_tooshort);
        !           247:        printf("\t%u with data size < data length\n", ipstat.ips_toosmall);
        !           248:        printf("\t%u with header length < data size\n", ipstat.ips_badhlen);
        !           249:        printf("\t%u with data length < header length\n", ipstat.ips_badlen);
        !           250:        printf("\t%u fragment%s received\n",
        !           251:                ipstat.ips_fragments, plural(ipstat.ips_fragments));
        !           252:        printf("\t%u fragment%s dropped (dup or out of space)\n",
        !           253:                ipstat.ips_fragdropped, plural(ipstat.ips_fragdropped));
        !           254:        printf("\t%u fragment%s dropped after timeout\n",
        !           255:                ipstat.ips_fragtimeout, plural(ipstat.ips_fragtimeout));
        !           256:        printf("\t%u packet%s forwarded\n",
        !           257:                ipstat.ips_forward, plural(ipstat.ips_forward));
        !           258:        printf("\t%u packet%s not forwardable\n",
        !           259:                ipstat.ips_cantforward, plural(ipstat.ips_cantforward));
        !           260:        printf("\t%u redirect%s sent\n",
        !           261:                ipstat.ips_redirectsent, plural(ipstat.ips_redirectsent));
        !           262: }
        !           263: 
        !           264: static char *icmpnames[] = {
        !           265:        "echo reply",
        !           266:        "#1",
        !           267:        "#2",
        !           268:        "destination unreachable",
        !           269:        "source quench",
        !           270:        "routing redirect",
        !           271:        "#6",
        !           272:        "#7",
        !           273:        "echo",
        !           274:        "#9",
        !           275:        "#10",
        !           276:        "time exceeded",
        !           277:        "parameter problem",
        !           278:        "time stamp",
        !           279:        "time stamp reply",
        !           280:        "information request",
        !           281:        "information request reply",
        !           282:        "address mask request",
        !           283:        "address mask reply",
        !           284: };
        !           285: 
        !           286: /*
        !           287:  * Dump ICMP statistics.
        !           288:  */
        !           289: icmp_stats(off, name)
        !           290:        off_t off;
        !           291:        char *name;
        !           292: {
        !           293:        struct icmpstat icmpstat;
        !           294:        register int i, first;
        !           295: 
        !           296:        if (off == 0)
        !           297:                return;
        !           298:        klseek(kmem, off, 0);
        !           299:        read(kmem, (char *)&icmpstat, sizeof (icmpstat));
        !           300:        printf("%s:\n\t%u call%s to icmp_error\n", name,
        !           301:                icmpstat.icps_error, plural(icmpstat.icps_error));
        !           302:        printf("\t%u error%s not generated 'cuz old message was icmp\n",
        !           303:                icmpstat.icps_oldicmp, plural(icmpstat.icps_oldicmp));
        !           304:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
        !           305:                if (icmpstat.icps_outhist[i] != 0) {
        !           306:                        if (first) {
        !           307:                                printf("\tOutput histogram:\n");
        !           308:                                first = 0;
        !           309:                        }
        !           310:                        printf("\t\t%s: %u\n", icmpnames[i],
        !           311:                                icmpstat.icps_outhist[i]);
        !           312:                }
        !           313:        printf("\t%u message%s with bad code fields\n",
        !           314:                icmpstat.icps_badcode, plural(icmpstat.icps_badcode));
        !           315:        printf("\t%u message%s < minimum length\n",
        !           316:                icmpstat.icps_tooshort, plural(icmpstat.icps_tooshort));
        !           317:        printf("\t%u bad checksum%s\n",
        !           318:                icmpstat.icps_checksum, plural(icmpstat.icps_checksum));
        !           319:        printf("\t%u message%s with bad length\n",
        !           320:                icmpstat.icps_badlen, plural(icmpstat.icps_badlen));
        !           321:        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
        !           322:                if (icmpstat.icps_inhist[i] != 0) {
        !           323:                        if (first) {
        !           324:                                printf("\tInput histogram:\n");
        !           325:                                first = 0;
        !           326:                        }
        !           327:                        printf("\t\t%s: %u\n", icmpnames[i],
        !           328:                                icmpstat.icps_inhist[i]);
        !           329:                }
        !           330:        printf("\t%u message response%s generated\n",
        !           331:                icmpstat.icps_reflect, plural(icmpstat.icps_reflect));
        !           332: }
        !           333: 
        !           334: /*
        !           335:  * Pretty print an Internet address (net address + port).
        !           336:  * If the nflag was specified, use numbers instead of names.
        !           337:  */
        !           338: inetprint(in, port, proto)
        !           339:        register struct in_addr *in;
        !           340:        u_short port; 
        !           341:        char *proto;
        !           342: {
        !           343:        struct servent *sp = 0;
        !           344:        char line[80], *cp, *index();
        !           345:        int width;
        !           346: 
        !           347:        sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(*in));
        !           348:        cp = index(line, '\0');
        !           349:        if (!nflag && port)
        !           350:                sp = getservbyport((int)port, proto);
        !           351:        if (sp || port == 0)
        !           352:                sprintf(cp, "%.8s", sp ? sp->s_name : "*");
        !           353:        else
        !           354:                sprintf(cp, "%d", ntohs((u_short)port));
        !           355:        width = Aflag ? 18 : 22;
        !           356:        printf(" %-*.*s", width, width, line);
        !           357: }
        !           358: 
        !           359: /*
        !           360:  * Construct an Internet address representation.
        !           361:  * If the nflag has been supplied, give 
        !           362:  * numeric value, otherwise try for symbolic name.
        !           363:  */
        !           364: char *
        !           365: inetname(in)
        !           366:        struct in_addr in;
        !           367: {
        !           368:        register char *cp;
        !           369:        static char line[50];
        !           370:        struct hostent *hp;
        !           371:        struct netent *np;
        !           372:        static char domain[MAXHOSTNAMELEN + 1];
        !           373:        static int first = 1;
        !           374: 
        !           375:        if (first && !nflag) {
        !           376:                first = 0;
        !           377:                if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
        !           378:                    (cp = index(domain, '.')))
        !           379:                        (void) strcpy(domain, cp + 1);
        !           380:                else
        !           381:                        domain[0] = 0;
        !           382:        }
        !           383:        cp = 0;
        !           384:        if (!nflag && in.s_addr != INADDR_ANY) {
        !           385:                int net = inet_netof(in);
        !           386:                int lna = inet_lnaof(in);
        !           387: 
        !           388:                if (lna == INADDR_ANY) {
        !           389:                        np = getnetbyaddr(net, AF_INET);
        !           390:                        if (np)
        !           391:                                cp = np->n_name;
        !           392:                }
        !           393:                if (cp == 0) {
        !           394:                        hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
        !           395:                        if (hp) {
        !           396:                                if ((cp = index(hp->h_name, '.')) &&
        !           397:                                    !strcmp(cp + 1, domain))
        !           398:                                        *cp = 0;
        !           399:                                cp = hp->h_name;
        !           400:                        }
        !           401:                }
        !           402:        }
        !           403:        if (in.s_addr == INADDR_ANY)
        !           404:                strcpy(line, "*");
        !           405:        else if (cp)
        !           406:                strcpy(line, cp);
        !           407:        else {
        !           408:                in.s_addr = ntohl(in.s_addr);
        !           409: #define C(x)   ((x) & 0xff)
        !           410:                sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
        !           411:                        C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
        !           412:        }
        !           413:        return (line);
        !           414: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.