temp files should be protected; bug report 4.3BSD/usr.bin/186
[unix-history] / usr / src / usr.bin / netstat / inet.c
... / ...
CommitLineData
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
19static char sccsid[] = "@(#)inet.c 5.11 (Berkeley) %G%";
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
52struct inpcb inpcb;
53struct tcpcb tcpcb;
54struct socket sockb;
55extern int kmem;
56extern int Aflag;
57extern int aflag;
58extern int nflag;
59extern char *plural();
60
61char *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 */
69protopr(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 */
144tcp_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 */
211udp_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#ifdef sun
228 printf("\t%d socket overflow%s\n",
229 udpstat.udps_fullsock, plural(udpstat.udps_fullsock));
230#endif
231}
232
233/*
234 * Dump IP statistics structure.
235 */
236ip_stats(off, name)
237 off_t off;
238 char *name;
239{
240 struct ipstat ipstat;
241
242 if (off == 0)
243 return;
244 klseek(kmem, off, 0);
245 read(kmem, (char *)&ipstat, sizeof (ipstat));
246#if BSD>=43
247 printf("%s:\n\t%u total packets received\n", name,
248 ipstat.ips_total);
249#endif
250 printf("\t%u bad header checksum%s\n",
251 ipstat.ips_badsum, plural(ipstat.ips_badsum));
252 printf("\t%u with size smaller than minimum\n", ipstat.ips_tooshort);
253 printf("\t%u with data size < data length\n", ipstat.ips_toosmall);
254 printf("\t%u with header length < data size\n", ipstat.ips_badhlen);
255 printf("\t%u with data length < header length\n", ipstat.ips_badlen);
256#if BSD>=43
257 printf("\t%u fragment%s received\n",
258 ipstat.ips_fragments, plural(ipstat.ips_fragments));
259 printf("\t%u fragment%s dropped (dup or out of space)\n",
260 ipstat.ips_fragdropped, plural(ipstat.ips_fragdropped));
261 printf("\t%u fragment%s dropped after timeout\n",
262 ipstat.ips_fragtimeout, plural(ipstat.ips_fragtimeout));
263 printf("\t%u packet%s forwarded\n",
264 ipstat.ips_forward, plural(ipstat.ips_forward));
265 printf("\t%u packet%s not forwardable\n",
266 ipstat.ips_cantforward, plural(ipstat.ips_cantforward));
267 printf("\t%u redirect%s sent\n",
268 ipstat.ips_redirectsent, plural(ipstat.ips_redirectsent));
269#endif
270}
271
272static char *icmpnames[] = {
273 "echo reply",
274 "#1",
275 "#2",
276 "destination unreachable",
277 "source quench",
278 "routing redirect",
279 "#6",
280 "#7",
281 "echo",
282 "#9",
283 "#10",
284 "time exceeded",
285 "parameter problem",
286 "time stamp",
287 "time stamp reply",
288 "information request",
289 "information request reply",
290 "address mask request",
291 "address mask reply",
292};
293
294/*
295 * Dump ICMP statistics.
296 */
297icmp_stats(off, name)
298 off_t off;
299 char *name;
300{
301 struct icmpstat icmpstat;
302 register int i, first;
303
304 if (off == 0)
305 return;
306 klseek(kmem, off, 0);
307 read(kmem, (char *)&icmpstat, sizeof (icmpstat));
308 printf("%s:\n\t%u call%s to icmp_error\n", name,
309 icmpstat.icps_error, plural(icmpstat.icps_error));
310 printf("\t%u error%s not generated 'cuz old message was icmp\n",
311 icmpstat.icps_oldicmp, plural(icmpstat.icps_oldicmp));
312 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
313 if (icmpstat.icps_outhist[i] != 0) {
314 if (first) {
315 printf("\tOutput histogram:\n");
316 first = 0;
317 }
318 printf("\t\t%s: %u\n", icmpnames[i],
319 icmpstat.icps_outhist[i]);
320 }
321 printf("\t%u message%s with bad code fields\n",
322 icmpstat.icps_badcode, plural(icmpstat.icps_badcode));
323 printf("\t%u message%s < minimum length\n",
324 icmpstat.icps_tooshort, plural(icmpstat.icps_tooshort));
325 printf("\t%u bad checksum%s\n",
326 icmpstat.icps_checksum, plural(icmpstat.icps_checksum));
327 printf("\t%u message%s with bad length\n",
328 icmpstat.icps_badlen, plural(icmpstat.icps_badlen));
329 for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
330 if (icmpstat.icps_inhist[i] != 0) {
331 if (first) {
332 printf("\tInput histogram:\n");
333 first = 0;
334 }
335 printf("\t\t%s: %u\n", icmpnames[i],
336 icmpstat.icps_inhist[i]);
337 }
338 printf("\t%u message response%s generated\n",
339 icmpstat.icps_reflect, plural(icmpstat.icps_reflect));
340}
341
342/*
343 * Pretty print an Internet address (net address + port).
344 * If the nflag was specified, use numbers instead of names.
345 */
346inetprint(in, port, proto)
347 register struct in_addr *in;
348 u_short port;
349 char *proto;
350{
351 struct servent *sp = 0;
352 char line[80], *cp, *index();
353 int width;
354
355 sprintf(line, "%.*s.", (Aflag && !nflag) ? 12 : 16, inetname(*in));
356 cp = index(line, '\0');
357 if (!nflag && port)
358 sp = getservbyport((int)port, proto);
359 if (sp || port == 0)
360 sprintf(cp, "%.8s", sp ? sp->s_name : "*");
361 else
362 sprintf(cp, "%d", ntohs((u_short)port));
363 width = Aflag ? 18 : 22;
364 printf(" %-*.*s", width, width, line);
365}
366
367/*
368 * Construct an Internet address representation.
369 * If the nflag has been supplied, give
370 * numeric value, otherwise try for symbolic name.
371 */
372char *
373inetname(in)
374 struct in_addr in;
375{
376 register char *cp;
377 static char line[50];
378 struct hostent *hp;
379 struct netent *np;
380 static char domain[MAXHOSTNAMELEN + 1];
381 static int first = 1;
382
383 if (first && !nflag) {
384 first = 0;
385 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
386 (cp = index(domain, '.')))
387 (void) strcpy(domain, cp + 1);
388 else
389 domain[0] = 0;
390 }
391 cp = 0;
392 if (!nflag && in.s_addr != INADDR_ANY) {
393 int net = inet_netof(in);
394 int lna = inet_lnaof(in);
395
396 if (lna == INADDR_ANY) {
397 np = getnetbyaddr(net, AF_INET);
398 if (np)
399 cp = np->n_name;
400 }
401 if (cp == 0) {
402 hp = gethostbyaddr((char *)&in, sizeof (in), AF_INET);
403 if (hp) {
404 if ((cp = index(hp->h_name, '.')) &&
405 !strcmp(cp + 1, domain))
406 *cp = 0;
407 cp = hp->h_name;
408 }
409 }
410 }
411 if (in.s_addr == INADDR_ANY)
412 strcpy(line, "*");
413 else if (cp)
414 strcpy(line, cp);
415 else {
416 in.s_addr = ntohl(in.s_addr);
417#define C(x) ((x) & 0xff)
418 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
419 C(in.s_addr >> 16), C(in.s_addr >> 8), C(in.s_addr));
420 }
421 return (line);
422}