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