Annotation of 43BSDReno/usr.bin/netstat/ns.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1985, 1988 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted provided
                      6:  * that: (1) source distributions retain this entire copyright notice and
                      7:  * comment, and (2) distributions including binaries display the following
                      8:  * acknowledgement:  ``This product includes software developed by the
                      9:  * University of California, Berkeley and its contributors'' in the
                     10:  * documentation or other materials provided with the distribution and in
                     11:  * all advertising materials mentioning features or use of this software.
                     12:  * Neither the name of the University nor the names of its contributors may
                     13:  * be used to endorse or promote products derived from this software without
                     14:  * specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
                     16:  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
                     17:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)ns.c       5.12 (Berkeley) 6/18/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include <stdio.h>
                     25: #include <errno.h>
                     26: #include <nlist.h>
                     27: 
                     28: #include <sys/param.h>
                     29: #include <sys/socket.h>
                     30: #include <sys/socketvar.h>
                     31: #include <sys/mbuf.h>
                     32: #include <sys/protosw.h>
                     33: 
                     34: #include <net/route.h>
                     35: #include <net/if.h>
                     36: 
                     37: #include <netinet/tcp_fsm.h>
                     38: 
                     39: #include <netns/ns.h>
                     40: #include <netns/ns_pcb.h>
                     41: #include <netns/idp.h>
                     42: #include <netns/idp_var.h>
                     43: #include <netns/ns_error.h>
                     44: #include <netns/sp.h>
                     45: #include <netns/spidp.h>
                     46: #include <netns/spp_timer.h>
                     47: #include <netns/spp_var.h>
                     48: #define SANAMES
                     49: #include <netns/spp_debug.h>
                     50: 
                     51: 
                     52: struct nspcb nspcb;
                     53: struct sppcb sppcb;
                     54: struct socket sockb;
                     55: extern int Aflag;
                     56: extern int aflag;
                     57: extern int nflag;
                     58: extern char *plural();
                     59: char *ns_prpr();
                     60: 
                     61: static int first = 1;
                     62: 
                     63: /*
                     64:  * Print a summary of connections related to a Network Systems
                     65:  * protocol.  For SPP, also give state of connection.
                     66:  * Listening processes (aflag) are suppressed unless the
                     67:  * -a (all) flag is specified.
                     68:  */
                     69: 
                     70: nsprotopr(off, name)
                     71:        off_t off;
                     72:        char *name;
                     73: {
                     74:        struct nspcb cb;
                     75:        register struct nspcb *prev, *next;
                     76:        int isspp;
                     77: 
                     78:        if (off == 0)
                     79:                return;
                     80:        isspp = strcmp(name, "spp") == 0;
                     81:        kvm_read(off, (char *)&cb, sizeof (struct nspcb));
                     82:        nspcb = cb;
                     83:        prev = (struct nspcb *)off;
                     84:        if (nspcb.nsp_next == (struct nspcb *)off)
                     85:                return;
                     86:        for (;nspcb.nsp_next != (struct nspcb *)off; prev = next) {
                     87:                off_t ppcb;
                     88: 
                     89:                next = nspcb.nsp_next;
                     90:                kvm_read((off_t)next, (char *)&nspcb, sizeof (nspcb));
                     91:                if (nspcb.nsp_prev != prev) {
                     92:                        printf("???\n");
                     93:                        break;
                     94:                }
                     95:                if (!aflag && ns_nullhost(nspcb.nsp_faddr) ) {
                     96:                        continue;
                     97:                }
                     98:                kvm_read((off_t)nspcb.nsp_socket,
                     99:                                (char *)&sockb, sizeof (sockb));
                    100:                ppcb = (off_t) nspcb.nsp_pcb;
                    101:                if (ppcb) {
                    102:                        if (isspp) {
                    103:                                kvm_read(ppcb, (char *)&sppcb, sizeof (sppcb));
                    104:                        } else continue;
                    105:                } else
                    106:                        if (isspp) continue;
                    107:                if (first) {
                    108:                        printf("Active NS 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:                        printf("%8x ", ppcb);
                    123:                printf("%-5.5s %6d %6d ", name, sockb.so_rcv.sb_cc,
                    124:                        sockb.so_snd.sb_cc);
                    125:                printf("  %-22.22s", ns_prpr(&nspcb.nsp_laddr));
                    126:                printf(" %-22.22s", ns_prpr(&nspcb.nsp_faddr));
                    127:                if (isspp) {
                    128:                        extern char *tcpstates[];
                    129:                        if (sppcb.s_state >= TCP_NSTATES)
                    130:                                printf(" %d", sppcb.s_state);
                    131:                        else
                    132:                                printf(" %s", tcpstates[sppcb.s_state]);
                    133:                }
                    134:                putchar('\n');
                    135:                prev = next;
                    136:        }
                    137: }
                    138: #define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s -- %s\n",x,y,plural(x),z,"x") : 0)
                    139: 
                    140: /*
                    141:  * Dump SPP statistics structure.
                    142:  */
                    143: spp_stats(off, name)
                    144:        off_t off;
                    145:        char *name;
                    146: {
                    147:        struct spp_istat spp_istat;
                    148: #define sppstat spp_istat.newstats
                    149: 
                    150:        if (off == 0)
                    151:                return;
                    152:        kvm_read(off, (char *)&spp_istat, sizeof (spp_istat));
                    153:        printf("%s:\n", name);
                    154:        ANY(spp_istat.nonucn, "connection", " dropped due to no new sockets ");
                    155:        ANY(spp_istat.gonawy, "connection", " terminated due to our end dying");
                    156:        ANY(spp_istat.nonucn, "connection", " dropped due to inability to connect");
                    157:        ANY(spp_istat.noconn, "connection", " dropped due to inability to connect");
                    158:        ANY(spp_istat.notme, "connection", " incompleted due to mismatched id's");
                    159:        ANY(spp_istat.wrncon, "connection", " dropped due to mismatched id's");
                    160:        ANY(spp_istat.bdreas, "packet", " dropped out of sequence");
                    161:        ANY(spp_istat.lstdup, "packet", " duplicating the highest packet");
                    162:        ANY(spp_istat.notyet, "packet", " refused as exceeding allocation");
                    163:        ANY(sppstat.spps_connattempt, "connection", " initiated");
                    164:        ANY(sppstat.spps_accepts, "connection", " accepted");
                    165:        ANY(sppstat.spps_connects, "connection", " established");
                    166:        ANY(sppstat.spps_drops, "connection", " dropped");
                    167:        ANY(sppstat.spps_conndrops, "embryonic connection", " dropped");
                    168:        ANY(sppstat.spps_closed, "connection", " closed (includes drops)");
                    169:        ANY(sppstat.spps_segstimed, "packet", " where we tried to get rtt");
                    170:        ANY(sppstat.spps_rttupdated, "time", " we got rtt");
                    171:        ANY(sppstat.spps_delack, "delayed ack", " sent");
                    172:        ANY(sppstat.spps_timeoutdrop, "connection", " dropped in rxmt timeout");
                    173:        ANY(sppstat.spps_rexmttimeo, "retransmit timeout", "");
                    174:        ANY(sppstat.spps_persisttimeo, "persist timeout", "");
                    175:        ANY(sppstat.spps_keeptimeo, "keepalive timeout", "");
                    176:        ANY(sppstat.spps_keepprobe, "keepalive probe", " sent");
                    177:        ANY(sppstat.spps_keepdrops, "connection", " dropped in keepalive");
                    178:        ANY(sppstat.spps_sndtotal, "total packet", " sent");
                    179:        ANY(sppstat.spps_sndpack, "data packet", " sent");
                    180:        ANY(sppstat.spps_sndbyte, "data byte", " sent");
                    181:        ANY(sppstat.spps_sndrexmitpack, "data packet", " retransmitted");
                    182:        ANY(sppstat.spps_sndrexmitbyte, "data byte", " retransmitted");
                    183:        ANY(sppstat.spps_sndacks, "ack-only packet", " sent");
                    184:        ANY(sppstat.spps_sndprobe, "window probe", " sent");
                    185:        ANY(sppstat.spps_sndurg, "packet", " sent with URG only");
                    186:        ANY(sppstat.spps_sndwinup, "window update-only packet", " sent");
                    187:        ANY(sppstat.spps_sndctrl, "control (SYN|FIN|RST) packet", " sent");
                    188:        ANY(sppstat.spps_sndvoid, "request", " to send a non-existant packet");
                    189:        ANY(sppstat.spps_rcvtotal, "total packet", " received");
                    190:        ANY(sppstat.spps_rcvpack, "packet", " received in sequence");
                    191:        ANY(sppstat.spps_rcvbyte, "byte", " received in sequence");
                    192:        ANY(sppstat.spps_rcvbadsum, "packet", " received with ccksum errs");
                    193:        ANY(sppstat.spps_rcvbadoff, "packet", " received with bad offset");
                    194:        ANY(sppstat.spps_rcvshort, "packet", " received too short");
                    195:        ANY(sppstat.spps_rcvduppack, "duplicate-only packet", " received");
                    196:        ANY(sppstat.spps_rcvdupbyte, "duplicate-only byte", " received");
                    197:        ANY(sppstat.spps_rcvpartduppack, "packet", " with some duplicate data");
                    198:        ANY(sppstat.spps_rcvpartdupbyte, "dup. byte", " in part-dup. packet");
                    199:        ANY(sppstat.spps_rcvoopack, "out-of-order packet", " received");
                    200:        ANY(sppstat.spps_rcvoobyte, "out-of-order byte", " received");
                    201:        ANY(sppstat.spps_rcvpackafterwin, "packet", " with data after window");
                    202:        ANY(sppstat.spps_rcvbyteafterwin, "byte", " rcvd after window");
                    203:        ANY(sppstat.spps_rcvafterclose, "packet", " rcvd after 'close'");
                    204:        ANY(sppstat.spps_rcvwinprobe, "rcvd window probe packet", "");
                    205:        ANY(sppstat.spps_rcvdupack, "rcvd duplicate ack", "");
                    206:        ANY(sppstat.spps_rcvacktoomuch, "rcvd ack", " for unsent data");
                    207:        ANY(sppstat.spps_rcvackpack, "rcvd ack packet", "");
                    208:        ANY(sppstat.spps_rcvackbyte, "byte", " acked by rcvd acks");
                    209:        ANY(sppstat.spps_rcvwinupd, "rcvd window update packet", "");
                    210: }
                    211: #undef ANY
                    212: #define ANY(x,y,z)  ((x) ? printf("\t%d %s%s%s\n",x,y,plural(x),z) : 0)
                    213: 
                    214: /*
                    215:  * Dump IDP statistics structure.
                    216:  */
                    217: idp_stats(off, name)
                    218:        off_t off;
                    219:        char *name;
                    220: {
                    221:        struct idpstat idpstat;
                    222: 
                    223:        if (off == 0)
                    224:                return;
                    225:        kvm_read(off, (char *)&idpstat, sizeof (idpstat));
                    226:        printf("%s:\n", name);
                    227:        ANY(idpstat.idps_toosmall, "packet", " smaller than a header");
                    228:        ANY(idpstat.idps_tooshort, "packet", " smaller than advertised");
                    229:        ANY(idpstat.idps_badsum, "packet", " with bad checksums");
                    230: }
                    231: 
                    232: static struct {
                    233:        u_short code;
                    234:        char *name;
                    235:        char *where;
                    236: } ns_errnames[] = {
                    237:        {0, "Unspecified Error", " at Destination"},
                    238:        {1, "Bad Checksum", " at Destination"},
                    239:        {2, "No Listener", " at Socket"},
                    240:        {3, "Packet", " Refused due to lack of space at Destination"},
                    241:        {01000, "Unspecified Error", " while gatewayed"},
                    242:        {01001, "Bad Checksum", " while gatewayed"},
                    243:        {01002, "Packet", " forwarded too many times"},
                    244:        {01003, "Packet", " too large to be forwarded"},
                    245:        {-1, 0, 0},
                    246: };
                    247: 
                    248: /*
                    249:  * Dump NS Error statistics structure.
                    250:  */
                    251: /*ARGSUSED*/
                    252: nserr_stats(off, name)
                    253:        off_t off;
                    254:        char *name;
                    255: {
                    256:        struct ns_errstat ns_errstat;
                    257:        register int j;
                    258:        register int histoprint = 1;
                    259:        int z;
                    260: 
                    261:        if (off == 0)
                    262:                return;
                    263:        kvm_read(off, (char *)&ns_errstat, sizeof (ns_errstat));
                    264:        printf("NS error statistics:\n");
                    265:        ANY(ns_errstat.ns_es_error, "call", " to ns_error");
                    266:        ANY(ns_errstat.ns_es_oldshort, "error",
                    267:                " ignored due to insufficient addressing");
                    268:        ANY(ns_errstat.ns_es_oldns_err, "error request",
                    269:                " in response to error packets");
                    270:        ANY(ns_errstat.ns_es_tooshort, "error packet",
                    271:                " received incomplete");
                    272:        ANY(ns_errstat.ns_es_badcode, "error packet",
                    273:                " received of unknown type");
                    274:        for(j = 0; j < NS_ERR_MAX; j ++) {
                    275:                z = ns_errstat.ns_es_outhist[j];
                    276:                if (z && histoprint) {
                    277:                        printf("Output Error Histogram:\n");
                    278:                        histoprint = 0;
                    279:                }
                    280:                ns_erputil(z, ns_errstat.ns_es_codes[j]);
                    281: 
                    282:        }
                    283:        histoprint = 1;
                    284:        for(j = 0; j < NS_ERR_MAX; j ++) {
                    285:                z = ns_errstat.ns_es_inhist[j];
                    286:                if (z && histoprint) {
                    287:                        printf("Input Error Histogram:\n");
                    288:                        histoprint = 0;
                    289:                }
                    290:                ns_erputil(z, ns_errstat.ns_es_codes[j]);
                    291:        }
                    292: }
                    293: static
                    294: ns_erputil(z, c)
                    295: {
                    296:        int j;
                    297:        char codebuf[30];
                    298:        char *name, *where;
                    299:        for(j = 0;; j ++) {
                    300:                if ((name = ns_errnames[j].name) == 0)
                    301:                        break;
                    302:                if (ns_errnames[j].code == c)
                    303:                        break;
                    304:        }
                    305:        if (name == 0)  {
                    306:                if (c > 01000)
                    307:                        where = "in transit";
                    308:                else
                    309:                        where = "at destination";
                    310:                sprintf(codebuf, "Unknown XNS error code 0%o", c);
                    311:                name = codebuf;
                    312:        } else 
                    313:                where =  ns_errnames[j].where;
                    314:        ANY(z, name, where);
                    315: }
                    316: static struct sockaddr_ns ssns = {AF_NS};
                    317: 
                    318: char *ns_prpr(x)
                    319: struct ns_addr *x;
                    320: {
                    321:        extern char *ns_print();
                    322:        struct sockaddr_ns *sns = &ssns;
                    323:        sns->sns_addr = *x;
                    324:        return(ns_print(sns));
                    325: }

unix.superglobalmegacorp.com

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