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