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

Defined functions

icmp_stats defined in line 236; used 2 times
inetname defined in line 311; used 4 times
inetprint defined in line 285; used 2 times
ip_stats defined in line 179; used 2 times
protopr defined in line 56; used 3 times
tcp_stats defined in line 131; used 2 times
udp_stats defined in line 158; used 2 times

Defined variables

first defined in line 47; used 12 times
icmpnames defined in line 211; used 2 times
inpcb defined in line 38; used 15 times
proto defined in line 41; used 3 times
sccsid defined in line 8; never used
sockb defined in line 40; used 4 times
tcpcb defined in line 39; used 6 times

Defined macros

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