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