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