Home | History | Annotate | Line # | Download | only in netstat
inet.c revision 1.36
      1 /*	$NetBSD: inet.c,v 1.36 1999/04/29 03:58:27 thorpej 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.36 1999/04/29 03:58:27 thorpej 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_retransmitted, "\t%lu SYN,ACK%s retransmitted\n");
    281 	p(tcps_sc_dupesyn, "\t%lu duplicate SYN%s received for entries already in the cache\n");
    282 	p(tcps_sc_dropped, "\t%lu SYN%s dropped (no route or no space)\n");
    283 
    284 #undef p
    285 #undef ps
    286 #undef p2
    287 #undef p2s
    288 #undef p3
    289 }
    290 
    291 /*
    292  * Dump UDP statistics structure.
    293  */
    294 void
    295 udp_stats(off, name)
    296 	u_long off;
    297 	char *name;
    298 {
    299 	struct udpstat udpstat;
    300 	u_long delivered;
    301 
    302 	if (off == 0)
    303 		return;
    304 	printf("%s:\n", name);
    305 	kread(off, (char *)&udpstat, sizeof (udpstat));
    306 
    307 #define	ps(f, m) if (udpstat.f || sflag <= 1) \
    308     printf(m, udpstat.f)
    309 #define	p(f, m) if (udpstat.f || sflag <= 1) \
    310     printf(m, udpstat.f, plural(udpstat.f))
    311 #define	p3(f, m) if (udpstat.f || sflag <= 1) \
    312     printf(m, udpstat.f, plurales(udpstat.f))
    313 
    314 	p(udps_ipackets, "\t%lu datagram%s received\n");
    315 	ps(udps_hdrops, "\t%lu with incomplete header\n");
    316 	ps(udps_badlen, "\t%lu with bad data length field\n");
    317 	ps(udps_badsum, "\t%lu with bad checksum\n");
    318 	ps(udps_noport, "\t%lu dropped due to no socket\n");
    319 	p(udps_noportbcast, "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
    320 	ps(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
    321 	delivered = udpstat.udps_ipackets -
    322 		    udpstat.udps_hdrops -
    323 		    udpstat.udps_badlen -
    324 		    udpstat.udps_badsum -
    325 		    udpstat.udps_noport -
    326 		    udpstat.udps_noportbcast -
    327 		    udpstat.udps_fullsock;
    328 	if (delivered || sflag <= 1)
    329 		printf("\t%lu delivered\n", delivered);
    330 	p3(udps_pcbhashmiss, "\t%lu PCB hash miss%s\n");
    331 	p(udps_opackets, "\t%lu datagram%s output\n");
    332 
    333 #undef ps
    334 #undef p
    335 #undef p3
    336 }
    337 
    338 /*
    339  * Dump IP statistics structure.
    340  */
    341 void
    342 ip_stats(off, name)
    343 	u_long off;
    344 	char *name;
    345 {
    346 	struct ipstat ipstat;
    347 
    348 	if (off == 0)
    349 		return;
    350 	kread(off, (char *)&ipstat, sizeof (ipstat));
    351 	printf("%s:\n", name);
    352 
    353 #define	ps(f, m) if (ipstat.f || sflag <= 1) \
    354     printf(m, ipstat.f)
    355 #define	p(f, m) if (ipstat.f || sflag <= 1) \
    356     printf(m, ipstat.f, plural(ipstat.f))
    357 
    358 	p(ips_total, "\t%lu total packet%s received\n");
    359 	p(ips_badsum, "\t%lu bad header checksum%s\n");
    360 	ps(ips_toosmall, "\t%lu with size smaller than minimum\n");
    361 	ps(ips_tooshort, "\t%lu with data size < data length\n");
    362 	ps(ips_toolong, "\t%lu with length > max ip packet size\n");
    363 	ps(ips_badhlen, "\t%lu with header length < data size\n");
    364 	ps(ips_badlen, "\t%lu with data length < header length\n");
    365 	ps(ips_badoptions, "\t%lu with bad options\n");
    366 	ps(ips_badvers, "\t%lu with incorrect version number\n");
    367 	p(ips_fragments, "\t%lu fragment%s received");
    368 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
    369 	p(ips_badfrags, "\t%lu malformed fragment%s dropped\n");
    370 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
    371 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
    372 	p(ips_delivered, "\t%lu packet%s for this host\n");
    373 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
    374 	p(ips_forward, "\t%lu packet%s forwarded");
    375 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
    376 	if (ipstat.ips_forward || sflag <= 1)
    377 		putchar('\n');
    378 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
    379 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
    380 	p(ips_localout, "\t%lu packet%s sent from this host\n");
    381 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
    382 	p(ips_odropped, "\t%lu output packet%s dropped due to no bufs, etc.\n");
    383 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
    384 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
    385 	p(ips_ofragments, "\t%lu fragment%s created\n");
    386 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
    387 #undef ps
    388 #undef p
    389 }
    390 
    391 static	char *icmpnames[] = {
    392 	"echo reply",
    393 	"#1",
    394 	"#2",
    395 	"destination unreachable",
    396 	"source quench",
    397 	"routing redirect",
    398 	"#6",
    399 	"#7",
    400 	"echo",
    401 	"#9",
    402 	"#10",
    403 	"time exceeded",
    404 	"parameter problem",
    405 	"time stamp",
    406 	"time stamp reply",
    407 	"information request",
    408 	"information request reply",
    409 	"address mask request",
    410 	"address mask reply",
    411 };
    412 
    413 /*
    414  * Dump ICMP statistics.
    415  */
    416 void
    417 icmp_stats(off, name)
    418 	u_long off;
    419 	char *name;
    420 {
    421 	struct icmpstat icmpstat;
    422 	int i, first;
    423 
    424 	if (off == 0)
    425 		return;
    426 	kread(off, (char *)&icmpstat, sizeof (icmpstat));
    427 	printf("%s:\n", name);
    428 
    429 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
    430     printf(m, icmpstat.f, plural(icmpstat.f))
    431 
    432 	p(icps_error, "\t%lu call%s to icmp_error\n");
    433 	p(icps_oldicmp,
    434 	    "\t%lu error%s not generated because old message was icmp\n");
    435 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    436 		if (icmpstat.icps_outhist[i] != 0) {
    437 			if (first) {
    438 				printf("\tOutput histogram:\n");
    439 				first = 0;
    440 			}
    441 			printf("\t\t%s: %lu\n", icmpnames[i],
    442 				icmpstat.icps_outhist[i]);
    443 		}
    444 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
    445 	p(icps_tooshort, "\t%lu message%s < minimum length\n");
    446 	p(icps_checksum, "\t%lu bad checksum%s\n");
    447 	p(icps_badlen, "\t%lu message%s with bad length\n");
    448 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
    449 		if (icmpstat.icps_inhist[i] != 0) {
    450 			if (first) {
    451 				printf("\tInput histogram:\n");
    452 				first = 0;
    453 			}
    454 			printf("\t\t%s: %lu\n", icmpnames[i],
    455 				icmpstat.icps_inhist[i]);
    456 		}
    457 	p(icps_reflect, "\t%lu message response%s generated\n");
    458 #undef p
    459 }
    460 
    461 /*
    462  * Dump IGMP statistics structure.
    463  */
    464 void
    465 igmp_stats(off, name)
    466 	u_long off;
    467 	char *name;
    468 {
    469 	struct igmpstat igmpstat;
    470 
    471 	if (off == 0)
    472 		return;
    473 	kread(off, (char *)&igmpstat, sizeof (igmpstat));
    474 	printf("%s:\n", name);
    475 
    476 #define	p(f, m) if (igmpstat.f || sflag <= 1) \
    477     printf(m, igmpstat.f, plural(igmpstat.f))
    478 #define	py(f, m) if (igmpstat.f || sflag <= 1) \
    479     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
    480 	p(igps_rcv_total, "\t%lu message%s received\n");
    481         p(igps_rcv_tooshort, "\t%lu message%s received with too few bytes\n");
    482         p(igps_rcv_badsum, "\t%lu message%s received with bad checksum\n");
    483         py(igps_rcv_queries, "\t%lu membership quer%s received\n");
    484         py(igps_rcv_badqueries, "\t%lu membership quer%s received with invalid field(s)\n");
    485         p(igps_rcv_reports, "\t%lu membership report%s received\n");
    486         p(igps_rcv_badreports, "\t%lu membership report%s received with invalid field(s)\n");
    487         p(igps_rcv_ourreports, "\t%lu membership report%s received for groups to which we belong\n");
    488         p(igps_snd_reports, "\t%lu membership report%s sent\n");
    489 #undef p
    490 #undef py
    491 }
    492 
    493 /*
    494  * Pretty print an Internet address (net address + port).
    495  * If the nflag was specified, use numbers instead of names.
    496  */
    497 void
    498 inetprint(in, port, proto, numeric)
    499 	struct in_addr *in;
    500 	u_int16_t port;
    501 	const char *proto;
    502 	int numeric;
    503 {
    504 	struct servent *sp = 0;
    505 	char line[80], *cp;
    506 	size_t space;
    507 
    508 	(void)snprintf(line, sizeof line, "%.*s.",
    509 	    (Aflag && !nflag) ? 12 : 16, inetname(in));
    510 	cp = strchr(line, '\0');
    511 	if (!numeric && port)
    512 		sp = getservbyport((int)port, proto);
    513 	space = sizeof line - (cp-line);
    514 	if (sp || port == 0)
    515 		(void)snprintf(cp, space, "%.8s", sp ? sp->s_name : "*");
    516 	else
    517 		(void)snprintf(cp, space, "%u", ntohs(port));
    518 	(void)printf(" %-*.*s", width, width, line);
    519 }
    520 
    521 /*
    522  * Construct an Internet address representation.
    523  * If the nflag has been supplied, give
    524  * numeric value, otherwise try for symbolic name.
    525  */
    526 char *
    527 inetname(inp)
    528 	struct in_addr *inp;
    529 {
    530 	char *cp;
    531 	static char line[50];
    532 	struct hostent *hp;
    533 	struct netent *np;
    534 	static char domain[MAXHOSTNAMELEN + 1];
    535 	static int first = 1;
    536 
    537 	if (first && !nflag) {
    538 		first = 0;
    539 		if (gethostname(domain, sizeof domain) == 0) {
    540 			domain[sizeof(domain) - 1] = '\0';
    541 			if ((cp = strchr(domain, '.')))
    542 				(void) strcpy(domain, cp + 1);
    543 			else
    544 				domain[0] = 0;
    545 		} else
    546 			domain[0] = 0;
    547 	}
    548 	cp = 0;
    549 	if (!nflag && inp->s_addr != INADDR_ANY) {
    550 		int net = inet_netof(*inp);
    551 		int lna = inet_lnaof(*inp);
    552 
    553 		if (lna == INADDR_ANY) {
    554 			np = getnetbyaddr(net, AF_INET);
    555 			if (np)
    556 				cp = np->n_name;
    557 		}
    558 		if (cp == 0) {
    559 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
    560 			if (hp) {
    561 				if ((cp = strchr(hp->h_name, '.')) &&
    562 				    !strcmp(cp + 1, domain))
    563 					*cp = 0;
    564 				cp = hp->h_name;
    565 			}
    566 		}
    567 	}
    568 	if (inp->s_addr == INADDR_ANY)
    569 		strncpy(line, "*", sizeof line);
    570 	else if (cp)
    571 		strncpy(line, cp, sizeof line);
    572 	else {
    573 		inp->s_addr = ntohl(inp->s_addr);
    574 #define C(x)	((x) & 0xff)
    575 		(void)snprintf(line, sizeof line, "%u.%u.%u.%u",
    576 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
    577 		    C(inp->s_addr >> 8), C(inp->s_addr));
    578 #undef C
    579 	}
    580 	line[sizeof(line) - 1] = '\0';
    581 	return (line);
    582 }
    583 
    584 /*
    585  * Dump the contents of a TCP PCB.
    586  */
    587 void
    588 tcp_dump(pcbaddr)
    589 	u_long pcbaddr;
    590 {
    591 	struct tcpcb tcpcb;
    592 	int i;
    593 
    594 	kread(pcbaddr, (char *)&tcpcb, sizeof(tcpcb));
    595 
    596 	printf("TCP Protocol Control Block at 0x%08lx:\n\n", pcbaddr);
    597 
    598 	printf("Timers:\n");
    599 	for (i = 0; i < TCPT_NTIMERS; i++)
    600 		printf("\t%s: %u", tcptimers[i], tcpcb.t_timer[i]);
    601 	printf("\n\n");
    602 
    603 	if (tcpcb.t_state < 0 || tcpcb.t_state >= TCP_NSTATES)
    604 		printf("State: %d", tcpcb.t_state);
    605 	else
    606 		printf("State: %s", tcpstates[tcpcb.t_state]);
    607 	printf(", flags 0x%x, inpcb 0x%lx\n\n", tcpcb.t_flags,
    608 	    (u_long)tcpcb.t_inpcb);
    609 
    610 	printf("rxtshift %d, rxtcur %d, dupacks %d\n", tcpcb.t_rxtshift,
    611 	    tcpcb.t_rxtcur, tcpcb.t_dupacks);
    612 	printf("peermss %u, ourmss %u, segsz %u\n\n", tcpcb.t_peermss,
    613 	    tcpcb.t_ourmss, tcpcb.t_segsz);
    614 
    615 	printf("snd_una %u, snd_nxt %u, snd_up %u\n",
    616 	    tcpcb.snd_una, tcpcb.snd_nxt, tcpcb.snd_up);
    617 	printf("snd_wl1 %u, snd_wl2 %u, iss %u, snd_wnd %lu\n\n",
    618 	    tcpcb.snd_wl1, tcpcb.snd_wl2, tcpcb.iss, tcpcb.snd_wnd);
    619 
    620 	printf("rcv_wnd %lu, rcv_nxt %u, rcv_up %u, irs %u\n\n",
    621 	    tcpcb.rcv_wnd, tcpcb.rcv_nxt, tcpcb.rcv_up, tcpcb.irs);
    622 
    623 	printf("rcv_adv %u, snd_max %u, snd_cwnd %lu, snd_ssthresh %lu\n",
    624 	    tcpcb.rcv_adv, tcpcb.snd_max, tcpcb.snd_cwnd, tcpcb.snd_ssthresh);
    625 
    626 	printf("idle %d, rtt %d, rtseq %u, srtt %d, rttvar %d, rttmin %d, "
    627 	    "max_sndwnd %lu\n\n", tcpcb.t_idle, tcpcb.t_rtt, tcpcb.t_rtseq,
    628 	    tcpcb.t_srtt, tcpcb.t_rttvar, tcpcb.t_rttmin, tcpcb.max_sndwnd);
    629 
    630 	printf("oobflags %d, iobc %d, softerror %d\n\n", tcpcb.t_oobflags,
    631 	    tcpcb.t_iobc, tcpcb.t_softerror);
    632 
    633 	printf("snd_scale %d, rcv_scale %d, req_r_scale %d, req_s_scale %d\n",
    634 	    tcpcb.snd_scale, tcpcb.rcv_scale, tcpcb.request_r_scale,
    635 	    tcpcb.requested_s_scale);
    636 	printf("ts_recent %u, ts_regent_age %d, last_ack_sent %u\n",
    637 	    tcpcb.ts_recent, tcpcb.ts_recent_age, tcpcb.last_ack_sent);
    638 }
    639