xref: /netbsd-src/usr.bin/netstat/inet.c (revision dc306354b0b29af51801a7632f1e95265a68cd81)
1 /*	$NetBSD: inet.c,v 1.34 1998/08/25 20:59:39 ross Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "from: @(#)inet.c	8.4 (Berkeley) 4/20/94";
40 #else
41 __RCSID("$NetBSD: inet.c,v 1.34 1998/08/25 20:59:39 ross Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/param.h>
46 #include <sys/queue.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/mbuf.h>
50 #include <sys/protosw.h>
51 
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/in_pcb.h>
57 #include <netinet/ip_icmp.h>
58 #include <netinet/icmp_var.h>
59 #include <netinet/igmp_var.h>
60 #include <netinet/ip_var.h>
61 #include <netinet/tcp.h>
62 #include <netinet/tcpip.h>
63 #include <netinet/tcp_seq.h>
64 #define TCPSTATES
65 #include <netinet/tcp_fsm.h>
66 #define	TCPTIMERS
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/tcp_debug.h>
70 #include <netinet/udp.h>
71 #include <netinet/udp_var.h>
72 
73 #include <arpa/inet.h>
74 #include <netdb.h>
75 #include <stdio.h>
76 #include <string.h>
77 #include <unistd.h>
78 #include "netstat.h"
79 
80 struct	inpcb inpcb;
81 struct	tcpcb tcpcb;
82 struct	socket sockb;
83 
84 char	*inetname __P((struct in_addr *));
85 void	inetprint __P((struct in_addr *, u_int16_t, const char *, int));
86 
87 /*
88  * Print a summary of connections related to an Internet
89  * protocol.  For TCP, also give state of connection.
90  * Listening processes (aflag) are suppressed unless the
91  * -a (all) flag is specified.
92  */
93 void
94 protopr(off, name)
95 	u_long off;
96 	char *name;
97 {
98 	struct inpcbtable table;
99 	struct inpcb *head, *next, *prev;
100 	struct inpcb inpcb;
101 	int istcp;
102 	static int first = 1;
103 
104 	if (off == 0)
105 		return;
106 	istcp = strcmp(name, "tcp") == 0;
107 	kread(off, (char *)&table, sizeof table);
108 	prev = head =
109 	    (struct inpcb *)&((struct inpcbtable *)off)->inpt_queue.cqh_first;
110 	next = table.inpt_queue.cqh_first;
111 
112 	while (next != head) {
113 		kread((u_long)next, (char *)&inpcb, sizeof inpcb);
114 		if (inpcb.inp_queue.cqe_prev != prev) {
115 			printf("???\n");
116 			break;
117 		}
118 		prev = next;
119 		next = inpcb.inp_queue.cqe_next;
120 
121 		if (!aflag &&
122 		    inet_lnaof(inpcb.inp_laddr) == INADDR_ANY)
123 			continue;
124 		kread((u_long)inpcb.inp_socket, (char *)&sockb, sizeof (sockb));
125 		if (istcp) {
126 			kread((u_long)inpcb.inp_ppcb,
127 			    (char *)&tcpcb, sizeof (tcpcb));
128 		}
129 		if (first) {
130 			printf("Active Internet connections");
131 			if (aflag)
132 				printf(" (including servers)");
133 			putchar('\n');
134 			if (Aflag)
135 				printf("%-8.8s ", "PCB");
136 			printf(Aflag ?
137 				"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
138 				"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
139 				"Proto", "Recv-Q", "Send-Q",
140 				"Local Address", "Foreign Address", "(state)");
141 			first = 0;
142 		}
143 		if (Aflag) {
144 			if (istcp)
145 				printf("%8lx ", (u_long) inpcb.inp_ppcb);
146 			else
147 				printf("%8lx ", (u_long) prev);
148 		}
149 		printf("%-5.5s %6ld %6ld ", name, sockb.so_rcv.sb_cc,
150 			sockb.so_snd.sb_cc);
151 		if (nflag) {
152 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 1);
153 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name, 1);
154 		} else if (inpcb.inp_flags & INP_ANONPORT) {
155 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 1);
156 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name, 0);
157 		} else {
158 			inetprint(&inpcb.inp_laddr, inpcb.inp_lport, name, 0);
159 			inetprint(&inpcb.inp_faddr, inpcb.inp_fport, name,
160 			    inpcb.inp_lport != inpcb.inp_fport);
161 		}
162 		if (istcp) {
163 			if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
164 				printf(" %d", tcpcb.t_state);
165 			else
166 				printf(" %s", tcpstates[tcpcb.t_state]);
167 		}
168 		putchar('\n');
169 	}
170 }
171 
172 /*
173  * Dump TCP statistics structure.
174  */
175 void
176 tcp_stats(off, name)
177 	u_long off;
178 	char *name;
179 {
180 	struct tcpstat tcpstat;
181 
182 	if (off == 0)
183 		return;
184 	printf ("%s:\n", name);
185 	kread(off, (char *)&tcpstat, sizeof (tcpstat));
186 
187 #define	ps(f, m) if (tcpstat.f || sflag <= 1) \
188     printf(m, tcpstat.f)
189 #define	p(f, m) if (tcpstat.f || sflag <= 1) \
190     printf(m, tcpstat.f, plural(tcpstat.f))
191 #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
192     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
193 #define	p2s(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
194     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
195 #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
196     printf(m, tcpstat.f, plurales(tcpstat.f))
197 
198 	p(tcps_sndtotal, "\t%lu packet%s sent\n");
199 	p2(tcps_sndpack,tcps_sndbyte,
200 		"\t\t%lu data packet%s (%lu byte%s)\n");
201 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
202 		"\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
203 	p2s(tcps_sndacks, tcps_delack,
204 		"\t\t%lu ack-only packet%s (%lu delayed)\n");
205 	p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
206 	p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
207 	p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
208 	p(tcps_sndctrl, "\t\t%lu control packet%s\n");
209 	p(tcps_rcvtotal, "\t%lu packet%s received\n");
210 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%lu ack%s (for %lu byte%s)\n");
211 	p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
212 	p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
213 	p2(tcps_rcvpack, tcps_rcvbyte,
214 		"\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
215 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
216 		"\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
217 	p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
218 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
219 		"\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
220 	p2(tcps_rcvoopack, tcps_rcvoobyte,
221 		"\t\t%lu out-of-order packet%s (%lu byte%s)\n");
222 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
223 		"\t\t%lu packet%s (%lu byte%s) of data after window\n");
224 	p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
225 	p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
226 	p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
227 	p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
228 	p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
229 	ps(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
230 	p(tcps_connattempt, "\t%lu connection request%s\n");
231 	p(tcps_accepts, "\t%lu connection accept%s\n");
232 	p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
233 	p2(tcps_closed, tcps_drops,
234 		"\t%lu connection%s closed (including %lu drop%s)\n");
235 	p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
236 	p2(tcps_rttupdated, tcps_segstimed,
237 		"\t%lu segment%s updated rtt (of %lu attempt%s)\n");
238 	p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
239 	p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
240 	p2(tcps_persisttimeo, tcps_persistdrops,
241 	   "\t%lu persist timeout%s (resulting in %lu dropped connection%s)\n");
242 	p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
243 	p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
244 	p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
245 	p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
246 	p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
247 	p3(tcps_pcbhashmiss, "\t%lu PCB hash miss%s\n");
248 	ps(tcps_noport, "\t%lu dropped due to no socket\n");
249 	p(tcps_connsdrained, "\t%lu connection%s drained due to memory shortage\n");
250 
251 	p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
252 	ps(tcps_sc_added, "\t%lu SYN cache entries added\n");
253 	p(tcps_sc_collisions, "\t\t%lu hash collision%s\n");
254 	ps(tcps_sc_completed, "\t\t%lu completed\n");
255 	ps(tcps_sc_aborted, "\t\t%lu aborted (no space to build PCB)\n");
256 	ps(tcps_sc_timed_out, "\t\t%lu timed out\n");
257 	ps(tcps_sc_overflowed, "\t\t%lu dropped due to overflow\n");
258 	ps(tcps_sc_bucketoverflow, "\t\t%lu dropped due to bucket overflow\n");
259 	ps(tcps_sc_reset, "\t\t%lu dropped due to RST\n");
260 	ps(tcps_sc_unreach, "\t\t%lu dropped due to ICMP unreachable\n");
261 	p(tcps_sc_dupesyn, "\t%lu duplicate SYN%s received for entries already in the cache\n");
262 	p(tcps_sc_dropped, "\t%lu SYN%s dropped (no route or no space)\n");
263 
264 #undef p
265 #undef ps
266 #undef p2
267 #undef p2s
268 #undef p3
269 }
270 
271 /*
272  * Dump UDP statistics structure.
273  */
274 void
275 udp_stats(off, name)
276 	u_long off;
277 	char *name;
278 {
279 	struct udpstat udpstat;
280 	u_long delivered;
281 
282 	if (off == 0)
283 		return;
284 	printf("%s:\n", name);
285 	kread(off, (char *)&udpstat, sizeof (udpstat));
286 
287 #define	ps(f, m) if (udpstat.f || sflag <= 1) \
288     printf(m, udpstat.f)
289 #define	p(f, m) if (udpstat.f || sflag <= 1) \
290     printf(m, udpstat.f, plural(udpstat.f))
291 #define	p3(f, m) if (udpstat.f || sflag <= 1) \
292     printf(m, udpstat.f, plurales(udpstat.f))
293 
294 	p(udps_ipackets, "\t%lu datagram%s received\n");
295 	ps(udps_hdrops, "\t%lu with incomplete header\n");
296 	ps(udps_badlen, "\t%lu with bad data length field\n");
297 	ps(udps_badsum, "\t%lu with bad checksum\n");
298 	ps(udps_noport, "\t%lu dropped due to no socket\n");
299 	p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
300 	ps(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
301 	delivered = udpstat.udps_ipackets -
302 		    udpstat.udps_hdrops -
303 		    udpstat.udps_badlen -
304 		    udpstat.udps_badsum -
305 		    udpstat.udps_noport -
306 		    udpstat.udps_noportbcast -
307 		    udpstat.udps_fullsock;
308 	if (delivered || sflag <= 1)
309 		printf("\t%lu delivered\n", delivered);
310 	p3(udps_pcbhashmiss, "\t%lu PCB hash miss%s\n");
311 	p(udps_opackets, "\t%lu datagram%s output\n");
312 
313 #undef ps
314 #undef p
315 #undef p3
316 }
317 
318 /*
319  * Dump IP statistics structure.
320  */
321 void
322 ip_stats(off, name)
323 	u_long off;
324 	char *name;
325 {
326 	struct ipstat ipstat;
327 
328 	if (off == 0)
329 		return;
330 	kread(off, (char *)&ipstat, sizeof (ipstat));
331 	printf("%s:\n", name);
332 
333 #define	ps(f, m) if (ipstat.f || sflag <= 1) \
334     printf(m, ipstat.f)
335 #define	p(f, m) if (ipstat.f || sflag <= 1) \
336     printf(m, ipstat.f, plural(ipstat.f))
337 
338 	p(ips_total, "\t%lu total packet%s received\n");
339 	p(ips_badsum, "\t%lu bad header checksum%s\n");
340 	ps(ips_toosmall, "\t%lu with size smaller than minimum\n");
341 	ps(ips_tooshort, "\t%lu with data size < data length\n");
342 	ps(ips_toolong, "\t%lu with length > max ip packet size\n");
343 	ps(ips_badhlen, "\t%lu with header length < data size\n");
344 	ps(ips_badlen, "\t%lu with data length < header length\n");
345 	ps(ips_badoptions, "\t%lu with bad options\n");
346 	ps(ips_badvers, "\t%lu with incorrect version number\n");
347 	p(ips_fragments, "\t%lu fragment%s received");
348 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
349 	p(ips_badfrags, "\t%lu malformed fragment%s dropped\n");
350 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
351 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
352 	p(ips_delivered, "\t%lu packet%s for this host\n");
353 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
354 	p(ips_forward, "\t%lu packet%s forwarded");
355 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
356 	if (ipstat.ips_forward || sflag <= 1)
357 		putchar('\n');
358 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
359 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
360 	p(ips_localout, "\t%lu packet%s sent from this host\n");
361 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
362 	p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n");
363 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
364 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
365 	p(ips_ofragments, "\t%lu fragment%s created\n");
366 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
367 #undef ps
368 #undef p
369 }
370 
371 static	char *icmpnames[] = {
372 	"echo reply",
373 	"#1",
374 	"#2",
375 	"destination unreachable",
376 	"source quench",
377 	"routing redirect",
378 	"#6",
379 	"#7",
380 	"echo",
381 	"#9",
382 	"#10",
383 	"time exceeded",
384 	"parameter problem",
385 	"time stamp",
386 	"time stamp reply",
387 	"information request",
388 	"information request reply",
389 	"address mask request",
390 	"address mask reply",
391 };
392 
393 /*
394  * Dump ICMP statistics.
395  */
396 void
397 icmp_stats(off, name)
398 	u_long off;
399 	char *name;
400 {
401 	struct icmpstat icmpstat;
402 	int i, first;
403 
404 	if (off == 0)
405 		return;
406 	kread(off, (char *)&icmpstat, sizeof (icmpstat));
407 	printf("%s:\n", name);
408 
409 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
410     printf(m, icmpstat.f, plural(icmpstat.f))
411 
412 	p(icps_error, "\t%lu call%s to icmp_error\n");
413 	p(icps_oldicmp,
414 	    "\t%lu error%s not generated because old message was icmp\n");
415 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
416 		if (icmpstat.icps_outhist[i] != 0) {
417 			if (first) {
418 				printf("\tOutput histogram:\n");
419 				first = 0;
420 			}
421 			printf("\t\t%s: %lu\n", icmpnames[i],
422 				icmpstat.icps_outhist[i]);
423 		}
424 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
425 	p(icps_tooshort, "\t%lu message%s < minimum length\n");
426 	p(icps_checksum, "\t%lu bad checksum%s\n");
427 	p(icps_badlen, "\t%lu message%s with bad length\n");
428 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
429 		if (icmpstat.icps_inhist[i] != 0) {
430 			if (first) {
431 				printf("\tInput histogram:\n");
432 				first = 0;
433 			}
434 			printf("\t\t%s: %lu\n", icmpnames[i],
435 				icmpstat.icps_inhist[i]);
436 		}
437 	p(icps_reflect, "\t%lu message response%s generated\n");
438 #undef p
439 }
440 
441 /*
442  * Dump IGMP statistics structure.
443  */
444 void
445 igmp_stats(off, name)
446 	u_long off;
447 	char *name;
448 {
449 	struct igmpstat igmpstat;
450 
451 	if (off == 0)
452 		return;
453 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
454 	printf("%s:\n", name);
455 
456 #define	p(f, m) if (igmpstat.f || sflag <= 1) \
457     printf(m, igmpstat.f, plural(igmpstat.f))
458 #define	py(f, m) if (igmpstat.f || sflag <= 1) \
459     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
460 	p(igps_rcv_total, "\t%lu message%s received\n");
461         p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
462         p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
463         py(igps_rcv_queries, "\t%lu membership quer%s received\n");
464         py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
465         p(igps_rcv_reports, "\t%lu membership report%s received\n");
466         p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
467         p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
468         p(igps_snd_reports, "\t%lu membership report%s sent\n");
469 #undef p
470 #undef py
471 }
472 
473 /*
474  * Pretty print an Internet address (net address + port).
475  * If the nflag was specified, use numbers instead of names.
476  */
477 void
478 inetprint(in, port, proto, numeric)
479 	struct in_addr *in;
480 	u_int16_t port;
481 	const char *proto;
482 	int numeric;
483 {
484 	struct servent *sp = 0;
485 	char line[80], *cp;
486 	int width;
487 	size_t space;
488 
489 	(void)snprintf(line, sizeof line, "%.*s.",
490 	    (Aflag && !numeric) ? 12 : 16, inetname(in));
491 	cp = strchr(line, '\0');
492 	if (!numeric && port)
493 		sp = getservbyport((int)port, proto);
494 	space = sizeof line - (cp-line);
495 	if (sp || port == 0)
496 		(void)snprintf(cp, space, "%.8s", sp ? sp->s_name : "*");
497 	else
498 		(void)snprintf(cp, space, "%u", ntohs(port));
499 	width = Aflag ? 18 : 22;
500 	(void)printf(" %-*.*s", width, width, line);
501 }
502 
503 /*
504  * Construct an Internet address representation.
505  * If the nflag has been supplied, give
506  * numeric value, otherwise try for symbolic name.
507  */
508 char *
509 inetname(inp)
510 	struct in_addr *inp;
511 {
512 	char *cp;
513 	static char line[50];
514 	struct hostent *hp;
515 	struct netent *np;
516 	static char domain[MAXHOSTNAMELEN + 1];
517 	static int first = 1;
518 
519 	if (first && !nflag) {
520 		first = 0;
521 		if (gethostname(domain, sizeof domain) == 0) {
522 			domain[sizeof(domain) - 1] = '\0';
523 			if ((cp = strchr(domain, '.')))
524 				(void) strcpy(domain, cp + 1);
525 			else
526 				domain[0] = 0;
527 		} else
528 			domain[0] = 0;
529 	}
530 	cp = 0;
531 	if (!nflag && inp->s_addr != INADDR_ANY) {
532 		int net = inet_netof(*inp);
533 		int lna = inet_lnaof(*inp);
534 
535 		if (lna == INADDR_ANY) {
536 			np = getnetbyaddr(net, AF_INET);
537 			if (np)
538 				cp = np->n_name;
539 		}
540 		if (cp == 0) {
541 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
542 			if (hp) {
543 				if ((cp = strchr(hp->h_name, '.')) &&
544 				    !strcmp(cp + 1, domain))
545 					*cp = 0;
546 				cp = hp->h_name;
547 			}
548 		}
549 	}
550 	if (inp->s_addr == INADDR_ANY)
551 		strncpy(line, "*", sizeof line);
552 	else if (cp)
553 		strncpy(line, cp, sizeof line);
554 	else {
555 		inp->s_addr = ntohl(inp->s_addr);
556 #define C(x)	((x) & 0xff)
557 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
558 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
559 		    C(inp->s_addr >> 8), C(inp->s_addr));
560 #undef C
561 	}
562 	line[sizeof(line) - 1] = '\0';
563 	return (line);
564 }
565 
566 /*
567  * Dump the contents of a TCP PCB.
568  */
569 void
570 tcp_dump(pcbaddr)
571 	u_long pcbaddr;
572 {
573 	struct tcpcb tcpcb;
574 	int i;
575 
576 	kread(pcbaddr, (char *)&tcpcb, sizeof(tcpcb));
577 
578 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
579 
580 	printf("Timers:\n");
581 	for (i = 0; i < TCPT_NTIMERS; i++)
582 		printf("\t%s: %u", tcptimers[i], tcpcb.t_timer[i]);
583 	printf("\n\n");
584 
585 	if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
586 		printf("State: %d", tcpcb.t_state);
587 	else
588 		printf("State: %s", tcpstates[tcpcb.t_state]);
589 	printf(", flags 0x%x, inpcb 0x%lx\n\n", tcpcb.t_flags,
590 	    (u_long)tcpcb.t_inpcb);
591 
592 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcpcb.t_rxtshift,
593 	    tcpcb.t_rxtcur, tcpcb.t_dupacks);
594 	printf("peermss %u, ourmss %u, segsz %u\n\n", tcpcb.t_peermss,
595 	    tcpcb.t_ourmss, tcpcb.t_segsz);
596 
597 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
598 	    tcpcb.snd_una, tcpcb.snd_nxt, tcpcb.snd_up);
599 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %lu\n\n",
600 	    tcpcb.snd_wl1, tcpcb.snd_wl2, tcpcb.iss, tcpcb.snd_wnd);
601 
602 	printf("rcv_wnd %lu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
603 	    tcpcb.rcv_wnd, tcpcb.rcv_nxt, tcpcb.rcv_up, tcpcb.irs);
604 
605 	printf("rcv_adv %u, snd_max %u, snd_cwnd %lu, snd_ssthresh %lu\n",
606 	    tcpcb.rcv_adv, tcpcb.snd_max, tcpcb.snd_cwnd, tcpcb.snd_ssthresh);
607 
608 	printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, "
609 	    "max_sndwnd %lu\n\n", tcpcb.t_idle, tcpcb.t_rtt, tcpcb.t_rtseq,
610 	    tcpcb.t_srtt, tcpcb.t_rttvar, tcpcb.t_rttmin, tcpcb.max_sndwnd);
611 
612 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcpcb.t_oobflags,
613 	    tcpcb.t_iobc, tcpcb.t_softerror);
614 
615 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
616 	    tcpcb.snd_scale, tcpcb.rcv_scale, tcpcb.request_r_scale,
617 	    tcpcb.requested_s_scale);
618 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
619 	    tcpcb.ts_recent, tcpcb.ts_recent_age, tcpcb.last_ack_sent);
620 }
621