1: /*
   2:  * Copyright (c) 1983 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #if defined(DO_SCCS) && !defined(lint)
   8: static char sccsid[] = "@(#)inet.c	5.4 (Berkeley) 2/25/86";
   9: #endif
  10: 
  11: #include <sys/param.h>
  12: #include <sys/socket.h>
  13: #include <sys/socketvar.h>
  14: #include <sys/mbuf.h>
  15: #include <sys/protosw.h>
  16: 
  17: #include <net/route.h>
  18: #include <netinet/in.h>
  19: #include <netinet/in_systm.h>
  20: #include <netinet/in_pcb.h>
  21: #include <netinet/ip.h>
  22: #include <netinet/ip_icmp.h>
  23: #include <netinet/icmp_var.h>
  24: #include <netinet/ip_var.h>
  25: #include <netinet/tcp.h>
  26: #include <netinet/tcpip.h>
  27: #include <netinet/tcp_seq.h>
  28: #define TCPSTATES
  29: #include <netinet/tcp_fsm.h>
  30: #include <netinet/tcp_timer.h>
  31: #include <netinet/tcp_var.h>
  32: #include <netinet/tcp_debug.h>
  33: #include <netinet/udp.h>
  34: #include <netinet/udp_var.h>
  35: 
  36: #include <arpa/inet.h>
  37: #include <netdb.h>
  38: #include "net.h"    /* for CRASH only */
  39: 
  40: extern  int kmem;
  41: extern  int Aflag;
  42: extern  int aflag;
  43: extern  int nflag;
  44: 
  45: static  int first = 1;
  46: char    *inetname();
  47: 
  48: /*
  49:  * Print a summary of connections related to an Internet
  50:  * protocol.  For TCP, also give state of connection.
  51:  * Listening processes (aflag) are suppressed unless the
  52:  * -a (all) flag is specified.
  53:  */
  54: protopr(off, name)
  55:     off_t off;
  56:     char *name;
  57: {
  58:     register struct inpcb *prev, *next;
  59:     struct inpcb inpcb, *inpcbp = &inpcb;
  60:     struct socket sockb, *sockp = &sockb;
  61:     struct tcpcb tcpcb, *tcpcbp = &tcpcb;
  62:     int rcvct, sndct;   /* number of mbufs in receive/send queue */
  63:     int istcp;
  64: 
  65:     if (off == 0)
  66:         return;
  67:     istcp = strcmp(name, "tcp") == 0;
  68:     klseek(kmem, (off_t)off, 0);
  69:     read(kmem, inpcbp, sizeof (struct inpcb));
  70:     prev = (struct inpcb *)off;
  71:     if (inpcbp->inp_next == (struct inpcb *)off)
  72:         return;
  73:     while (inpcbp->inp_next != (struct inpcb *)off) {
  74:         char *cp;
  75: 
  76:         next = inpcbp->inp_next;
  77: #ifdef  CRASH
  78:         if (!VALADD(next, struct inpcb)) {
  79:             printf("bad inpcb address (");
  80:             DUMP((unsigned)next);
  81:             puts(")");
  82:             break;
  83:         }
  84:         (putars((unsigned)next, sizeof(struct inpcb),
  85:             AS_INPCB))->as_ref++;
  86:         inpcbp = XLATE(next, struct inpcb *);
  87: #else
  88:         klseek(kmem, (off_t)next, 0);
  89:         read(kmem, inpcbp, sizeof (struct inpcb));
  90: #endif	CRASH
  91:         if (inpcbp->inp_prev != prev) {
  92:             printf("???\n");
  93:             break;
  94:         }
  95:         if (!aflag &&
  96:           inet_lnaof(inpcbp->inp_laddr.s_addr) == INADDR_ANY) {
  97:             prev = next;
  98:             continue;
  99:         }
 100: #ifdef  CRASH
 101:         if (!VALADD(inpcbp->inp_socket, struct socket)) {
 102:             printf("bad socket address (");
 103:             DUMP((unsigned)inpcbp->inp_socket);
 104:             puts(")");
 105:             break;
 106:         }
 107:         (putars((unsigned)inpcbp->inp_socket,sizeof(struct socket),
 108:             AS_SOCK))->as_ref++;
 109:         sockp = XLATE(inpcbp->inp_socket, struct socket *);
 110: #else
 111:         klseek(kmem, (off_t)inpcbp->inp_socket, 0);
 112:         read(kmem, sockp, sizeof (struct socket));
 113: #endif	CRASH
 114:         if (istcp) {
 115: #ifdef  CRASH
 116:             if (!VALADD(inpcbp->inp_ppcb, struct tcpcb)) {
 117:                 printf("bad tcpcb address (");
 118:                 DUMP((unsigned)inpcbp->inp_ppcb);
 119:                 puts(")");
 120:                 break;
 121:             }
 122:             (putars((unsigned)inpcbp->inp_ppcb,
 123:                 sizeof(struct tcpcb), AS_TCPCB))->as_ref++;
 124:             tcpcbp = XLATE(inpcbp->inp_ppcb, struct tcpcb *);
 125: #else
 126:             klseek(kmem, (off_t)inpcbp->inp_ppcb, 0);
 127:             read(kmem, &tcpcb, sizeof (tcpcb));
 128: #endif	CRASH
 129:         }
 130:         if (first) {
 131:             printf("Active Internet connections");
 132:             if (aflag)
 133:                 printf(" (including servers)");
 134:             putchar('\n');
 135:             if (Aflag)
 136:                 printf("%-8.8s ", "PCB");
 137:             printf(Aflag ?
 138:                 "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
 139:                 "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
 140:                 "Proto", "Recv-Q", "Send-Q",
 141:                 "Local Address", "Foreign Address", "(state)");
 142:             first = 0;
 143:         }
 144: #ifdef  CRASH       /* chase the chains of mbufs */
 145:         rcvct = chasem(sockp->so_rcv.sb_mb,"recv",sockp->so_rcv.sb_cc);
 146:         sndct = chasem(sockp->so_snd.sb_mb,"send",sockp->so_snd.sb_cc);
 147: #endif	CRASH
 148:         if (Aflag)
 149:             if (istcp)
 150:                 printf("%8x ", inpcbp->inp_ppcb);
 151:             else
 152:                 printf("%8x ", next);
 153:         printf("%-5.5s %6d %6d ", name, sockp->so_rcv.sb_cc,
 154:             sockp->so_snd.sb_cc);
 155:         inetprint(&inpcbp->inp_laddr, inpcbp->inp_lport, name);
 156:         inetprint(&inpcbp->inp_faddr, inpcbp->inp_fport, name);
 157:         if (istcp) {
 158:             if (tcpcbp->t_state < 0 ||
 159:                tcpcbp->t_state >= TCP_NSTATES)
 160:                 printf(" %d", tcpcbp->t_state);
 161:             else
 162:                 printf(" %s", tcpstates[tcpcbp->t_state]);
 163:         }
 164:         putchar('\n');
 165: #ifdef  CRASH
 166:         if (istcp) {
 167:             if (tcpcbp->t_tcpopt || tcpcbp->t_inpcb != next)
 168:             printf("--tcp opts=%4x t_inpcb=%4x (actual inpcb=%4x)\n",
 169:                 tcpcbp->t_tcpopt, tcpcbp->t_inpcb, next);
 170:         }
 171: #endif	CRASH
 172:         prev = next;
 173:     }
 174: }
 175: 
 176: /*
 177:  * Dump TCP statistics structure.
 178:  */
 179: tcp_stats(off, name)
 180:     off_t off;
 181:     char *name;
 182: {
 183:     struct tcpstat tcpstat;
 184: 
 185:     if (off == 0)
 186:         return;
 187:     klseek(kmem, (off_t)off, 0);
 188:     read(kmem, (char *)&tcpstat, sizeof (tcpstat));
 189:     printf("%s:\n\t%D incomplete header%s\n", name,
 190:         tcpstat.tcps_hdrops, plural(tcpstat.tcps_hdrops));
 191:     printf("\t%D bad checksum%s\n",
 192:         tcpstat.tcps_badsum, plural(tcpstat.tcps_badsum));
 193:     printf("\t%D bad header offset field%s\n",
 194:         tcpstat.tcps_badoff, plural(tcpstat.tcps_badoff));
 195:     printf("\t%D bad segment%s\n",
 196:         tcpstat.tcps_badsegs, plural(tcpstat.tcps_badsegs));
 197:     printf("\t%D unacknowledged packet%s\n",
 198:         tcpstat.tcps_unack, plural(tcpstat.tcps_unack));
 199: }
 200: 
 201: /*
 202:  * Dump UDP statistics structure.
 203:  */
 204: udp_stats(off, name)
 205:     off_t off;
 206:     char *name;
 207: {
 208:     struct udpstat udpstat;
 209: 
 210:     if (off == 0)
 211:         return;
 212:     klseek(kmem, (off_t)off, 0);
 213:     read(kmem, (char *)&udpstat, sizeof (udpstat));
 214:     printf("%s:\n\t%D incomplete header%s\n", name,
 215:         udpstat.udps_hdrops, plural(udpstat.udps_hdrops));
 216:     printf("\t%D bad data length field%s\n",
 217:         udpstat.udps_badlen, plural(udpstat.udps_badlen));
 218:     printf("\t%D bad checksum%s\n",
 219:         udpstat.udps_badsum, plural(udpstat.udps_badsum));
 220: }
 221: 
 222: /*
 223:  * Dump IP statistics structure.
 224:  */
 225: ip_stats(off, name)
 226:     off_t off;
 227:     char *name;
 228: {
 229:     struct ipstat ipstat;
 230: 
 231:     if (off == 0)
 232:         return;
 233:     klseek(kmem, (off_t)off, 0);
 234:     read(kmem, (char *)&ipstat, sizeof (ipstat));
 235:     printf("%s:\n\t%D total packets received\n", name,
 236:         ipstat.ips_total);
 237:     printf("\t%D bad header checksum%s\n",
 238:         ipstat.ips_badsum, plural(ipstat.ips_badsum));
 239:     printf("\t%D with size smaller than minimum\n", ipstat.ips_tooshort);
 240:     printf("\t%D with data size < data length\n", ipstat.ips_toosmall);
 241:     printf("\t%D with header length < data size\n", ipstat.ips_badhlen);
 242:     printf("\t%D with data length < header length\n", ipstat.ips_badlen);
 243:     printf("\t%D fragment%s received\n",
 244:         ipstat.ips_fragments, plural(ipstat.ips_fragments));
 245:     printf("\t%D fragment%s dropped (dup or out of space)\n",
 246:         ipstat.ips_fragdropped, plural(ipstat.ips_fragdropped));
 247:     printf("\t%D fragment%s dropped after timeout\n",
 248:         ipstat.ips_fragtimeout, plural(ipstat.ips_fragtimeout));
 249:     printf("\t%D packet%s forwarded\n",
 250:         ipstat.ips_forward, plural(ipstat.ips_forward));
 251:     printf("\t%D packet%s not forwardable\n",
 252:         ipstat.ips_cantforward, plural(ipstat.ips_cantforward));
 253:     printf("\t%D redirect%s sent\n",
 254:         ipstat.ips_redirectsent, plural(ipstat.ips_redirectsent));
 255: }
 256: 
 257: static  char *icmpnames[] = {
 258:     "echo reply",
 259:     "#1",
 260:     "#2",
 261:     "destination unreachable",
 262:     "source quench",
 263:     "routing redirect",
 264:     "#6",
 265:     "#7",
 266:     "echo",
 267:     "#9",
 268:     "#10",
 269:     "time exceeded",
 270:     "parameter problem",
 271:     "time stamp",
 272:     "time stamp reply",
 273:     "information request",
 274:     "information request reply",
 275:     "address mask request",
 276:     "address mask reply",
 277: };
 278: 
 279: /*
 280:  * Dump ICMP statistics.
 281:  */
 282: icmp_stats(off, name)
 283:     off_t off;
 284:     char *name;
 285: {
 286:     struct icmpstat icmpstat;
 287:     register int i, first;
 288: 
 289:     if (off == 0)
 290:         return;
 291:     klseek(kmem, (off_t)off, 0);
 292:     read(kmem, (char *)&icmpstat, sizeof (icmpstat));
 293:     printf("%s:\n\t%D call%s to icmp_error\n", name,
 294:         icmpstat.icps_error, plural(icmpstat.icps_error));
 295:     printf("\t%D error%s not generated 'cuz old message was icmp\n",
 296:         icmpstat.icps_oldicmp, plural(icmpstat.icps_oldicmp));
 297:     for (first = 1, i = 0; i < ICMP_IREQREPLY + 1; i++)
 298:         if (icmpstat.icps_outhist[i] != 0) {
 299:             if (first) {
 300:                 printf("\tOutput histogram:\n");
 301:                 first = 0;
 302:             }
 303:             printf("\t\t%s: %D\n", icmpnames[i],
 304:                 icmpstat.icps_outhist[i]);
 305:         }
 306:     printf("\t%D message%s with bad code fields\n",
 307:         icmpstat.icps_badcode, plural(icmpstat.icps_badcode));
 308:     printf("\t%D message%s < minimum length\n",
 309:         icmpstat.icps_tooshort, plural(icmpstat.icps_tooshort));
 310:     printf("\t%D bad checksum%s\n",
 311:         icmpstat.icps_checksum, plural(icmpstat.icps_checksum));
 312:     printf("\t%D message%s with bad length\n",
 313:         icmpstat.icps_badlen, plural(icmpstat.icps_badlen));
 314:     for (first = 1, i = 0; i < ICMP_IREQREPLY + 1; i++)
 315:         if (icmpstat.icps_inhist[i] != 0) {
 316:             if (first) {
 317:                 printf("\tInput histogram:\n");
 318:                 first = 0;
 319:             }
 320:             printf("\t\t%s: %D\n", icmpnames[i],
 321:                 icmpstat.icps_inhist[i]);
 322:         }
 323:     printf("\t%D message response%s generated\n",
 324:         icmpstat.icps_reflect, plural(icmpstat.icps_reflect));
 325: }
 326: 
 327: /*
 328:  * Pretty print an Internet address (net address + port).
 329:  * If the nflag was specified, use numbers instead of names.
 330:  */
 331: inetprint(in, port, proto)
 332:     register struct in_addr *in;
 333:     int port;
 334:     char *proto;
 335: {
 336:     struct servent *sp = 0;
 337:     char line[80], *cp, *index();
 338:     int width;
 339: 
 340:     sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(*in));
 341:     cp = index(line, '\0');
 342:     if (!nflag && port)
 343:         sp = getservbyport(port, proto);
 344:     if (sp || port == 0)
 345:         sprintf(cp, "%.8s", sp ? sp->s_name : "*");
 346:     else
 347:         sprintf(cp, "%d", ntohs((u_short)port));
 348:     width = Aflag ? 18 : 22;
 349:     printf(" %-*.*s", width, width, line);
 350: }
 351: 
 352: /*
 353:  * Construct an Internet address representation.
 354:  * If the nflag has been supplied, give
 355:  * numeric value, otherwise try for symbolic name.
 356:  */
 357: char *
 358: inetname(in)
 359:     struct in_addr in;
 360: {
 361:     register char *cp;
 362:     char *index();
 363:     static char line[50];
 364:     struct hostent *hp;
 365:     struct netent *np;
 366:     static char domain[MAXHOSTNAMELEN + 1];
 367:     static int first = 1;
 368: 
 369:     if (first && !nflag) {
 370:         first = 0;
 371:         if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
 372:             (cp = index(domain, '.')))
 373:             (void) strcpy(domain, cp + 1);
 374:         else
 375:             domain[0] = 0;
 376:     }
 377:     cp = 0;
 378:     if (!nflag && in.s_addr != INADDR_ANY) {
 379:         u_long net = inet_netof(in);
 380:         u_long lna = inet_lnaof(in);
 381: 
 382:         if (lna == INADDR_ANY) {
 383:             np = getnetbyaddr(net, AF_INET);
 384:             if (np)
 385:                 cp = np->n_name;
 386:         }
 387:         if (cp == 0) {
 388:             hp = gethostbyaddr(&in, sizeof (in), AF_INET);
 389:             if (hp) {
 390:                 if ((cp = index(hp->h_name, '.')) &&
 391:                     !strcmp(cp + 1, domain))
 392:                     *cp = 0;
 393:                 cp = hp->h_name;
 394:             }
 395:         }
 396:     }
 397:     if (in.s_addr == INADDR_ANY)
 398:         strcpy(line, "*");
 399:     else if (cp)
 400:         strcpy(line, cp);
 401:     else {
 402:         in.s_addr = ntohl(in.s_addr);
 403: #define C(x)    ((x) & 0xff)
 404:         sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
 405:             C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
 406:     }
 407:     return (line);
 408: }

Defined functions

icmp_stats defined in line 282; used 1 times
inetname defined in line 357; used 2 times
inetprint defined in line 331; used 3 times
ip_stats defined in line 225; used 1 times
protopr defined in line 54; used 2 times
tcp_stats defined in line 179; used 1 times
udp_stats defined in line 204; used 1 times

Defined variables

first defined in line 45; used 12 times
icmpnames defined in line 257; used 2 times
sccsid defined in line 8; never used

Defined macros

C defined in line 403; used 4 times
TCPSTATES defined in line 28; never used
Last modified: 1992-12-24
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3480
Valid CSS Valid XHTML 1.0 Strict