xref: /freebsd/usr.bin/netstat/inet.c (revision b0b1dbdd)
1 /*-
2  * Copyright (c) 1983, 1988, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
33 #endif /* not lint */
34 #endif
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/domain.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 
47 #include <net/route.h>
48 #include <net/if_arp.h>
49 #include <netinet/in.h>
50 #include <netinet/in_systm.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_carp.h>
53 #ifdef INET6
54 #include <netinet/ip6.h>
55 #endif /* INET6 */
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/pim_var.h>
62 #include <netinet/tcp.h>
63 #include <netinet/tcpip.h>
64 #include <netinet/tcp_seq.h>
65 #define	TCPSTATES
66 #include <netinet/tcp_fsm.h>
67 #include <netinet/tcp_timer.h>
68 #include <netinet/tcp_var.h>
69 #include <netinet/udp.h>
70 #include <netinet/udp_var.h>
71 
72 #include <arpa/inet.h>
73 #include <err.h>
74 #include <errno.h>
75 #include <libutil.h>
76 #include <netdb.h>
77 #include <stdint.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <stdbool.h>
81 #include <string.h>
82 #include <unistd.h>
83 #include <libxo/xo.h>
84 #include "netstat.h"
85 #include "nl_defs.h"
86 
87 void	inetprint(const char *, struct in_addr *, int, const char *, int,
88     const int);
89 #ifdef INET6
90 static int udp_done, tcp_done, sdp_done;
91 #endif /* INET6 */
92 
93 static int
94 pcblist_sysctl(int proto, const char *name, char **bufp, int istcp __unused)
95 {
96 	const char *mibvar;
97 	char *buf;
98 	size_t len;
99 
100 	switch (proto) {
101 	case IPPROTO_TCP:
102 		mibvar = "net.inet.tcp.pcblist";
103 		break;
104 	case IPPROTO_UDP:
105 		mibvar = "net.inet.udp.pcblist";
106 		break;
107 	case IPPROTO_DIVERT:
108 		mibvar = "net.inet.divert.pcblist";
109 		break;
110 	default:
111 		mibvar = "net.inet.raw.pcblist";
112 		break;
113 	}
114 	if (strncmp(name, "sdp", 3) == 0)
115 		mibvar = "net.inet.sdp.pcblist";
116 	len = 0;
117 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118 		if (errno != ENOENT)
119 			xo_warn("sysctl: %s", mibvar);
120 		return (0);
121 	}
122 	if ((buf = malloc(len)) == NULL) {
123 		xo_warnx("malloc %lu bytes", (u_long)len);
124 		return (0);
125 	}
126 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 		xo_warn("sysctl: %s", mibvar);
128 		free(buf);
129 		return (0);
130 	}
131 	*bufp = buf;
132 	return (1);
133 }
134 
135 /*
136  * Copied directly from uipc_socket2.c.  We leave out some fields that are in
137  * nested structures that aren't used to avoid extra work.
138  */
139 static void
140 sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
141 {
142 	xsb->sb_cc = sb->sb_ccc;
143 	xsb->sb_hiwat = sb->sb_hiwat;
144 	xsb->sb_mbcnt = sb->sb_mbcnt;
145 	xsb->sb_mcnt = sb->sb_mcnt;
146 	xsb->sb_ccnt = sb->sb_ccnt;
147 	xsb->sb_mbmax = sb->sb_mbmax;
148 	xsb->sb_lowat = sb->sb_lowat;
149 	xsb->sb_flags = sb->sb_flags;
150 	xsb->sb_timeo = sb->sb_timeo;
151 }
152 
153 int
154 sotoxsocket(struct socket *so, struct xsocket *xso)
155 {
156 	struct protosw proto;
157 	struct domain domain;
158 
159 	bzero(xso, sizeof *xso);
160 	xso->xso_len = sizeof *xso;
161 	xso->xso_so = so;
162 	xso->so_type = so->so_type;
163 	xso->so_options = so->so_options;
164 	xso->so_linger = so->so_linger;
165 	xso->so_state = so->so_state;
166 	xso->so_pcb = so->so_pcb;
167 	if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
168 		return (-1);
169 	xso->xso_protocol = proto.pr_protocol;
170 	if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
171 		return (-1);
172 	xso->xso_family = domain.dom_family;
173 	xso->so_qlen = so->so_qlen;
174 	xso->so_incqlen = so->so_incqlen;
175 	xso->so_qlimit = so->so_qlimit;
176 	xso->so_timeo = so->so_timeo;
177 	xso->so_error = so->so_error;
178 	xso->so_oobmark = so->so_oobmark;
179 	sbtoxsockbuf(&so->so_snd, &xso->so_snd);
180 	sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
181 	return (0);
182 }
183 
184 static int
185 pcblist_kvm(u_long off, char **bufp, int istcp)
186 {
187 	struct inpcbinfo pcbinfo;
188 	struct inpcbhead listhead;
189 	struct inpcb *inp;
190 	struct xinpcb xi;
191 	struct xinpgen xig;
192 	struct xtcpcb xt;
193 	struct socket so;
194 	struct xsocket *xso;
195 	char *buf, *p;
196 	size_t len;
197 
198 	if (off == 0)
199 		return (0);
200 	kread(off, &pcbinfo, sizeof(pcbinfo));
201 	if (istcp)
202 		len = 2 * sizeof(xig) +
203 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
204 		    sizeof(struct xtcpcb);
205 	else
206 		len = 2 * sizeof(xig) +
207 		    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
208 		    sizeof(struct xinpcb);
209 	if ((buf = malloc(len)) == NULL) {
210 		xo_warnx("malloc %lu bytes", (u_long)len);
211 		return (0);
212 	}
213 	p = buf;
214 
215 #define	COPYOUT(obj, size) do {						\
216 	if (len < (size)) {						\
217 		xo_warnx("buffer size exceeded");			\
218 		goto fail;						\
219 	}								\
220 	bcopy((obj), p, (size));					\
221 	len -= (size);							\
222 	p += (size);							\
223 } while (0)
224 
225 #define	KREAD(off, buf, len) do {					\
226 	if (kread((uintptr_t)(off), (buf), (len)) != 0)			\
227 		goto fail;						\
228 } while (0)
229 
230 	/* Write out header. */
231 	xig.xig_len = sizeof xig;
232 	xig.xig_count = pcbinfo.ipi_count;
233 	xig.xig_gen = pcbinfo.ipi_gencnt;
234 	xig.xig_sogen = 0;
235 	COPYOUT(&xig, sizeof xig);
236 
237 	/* Walk the PCB list. */
238 	xt.xt_len = sizeof xt;
239 	xi.xi_len = sizeof xi;
240 	if (istcp)
241 		xso = &xt.xt_socket;
242 	else
243 		xso = &xi.xi_socket;
244 	KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
245 	LIST_FOREACH(inp, &listhead, inp_list) {
246 		if (istcp) {
247 			KREAD(inp, &xt.xt_inp, sizeof(*inp));
248 			inp = &xt.xt_inp;
249 		} else {
250 			KREAD(inp, &xi.xi_inp, sizeof(*inp));
251 			inp = &xi.xi_inp;
252 		}
253 
254 		if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
255 			continue;
256 
257 		if (istcp) {
258 			if (inp->inp_ppcb == NULL)
259 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
260 			else if (inp->inp_flags & INP_TIMEWAIT) {
261 				bzero(&xt.xt_tp, sizeof xt.xt_tp);
262 				xt.xt_tp.t_state = TCPS_TIME_WAIT;
263 			} else
264 				KREAD(inp->inp_ppcb, &xt.xt_tp,
265 				    sizeof xt.xt_tp);
266 		}
267 		if (inp->inp_socket) {
268 			KREAD(inp->inp_socket, &so, sizeof(so));
269 			if (sotoxsocket(&so, xso) != 0)
270 				goto fail;
271 		} else {
272 			bzero(xso, sizeof(*xso));
273 			if (istcp)
274 				xso->xso_protocol = IPPROTO_TCP;
275 		}
276 		if (istcp)
277 			COPYOUT(&xt, sizeof xt);
278 		else
279 			COPYOUT(&xi, sizeof xi);
280 	}
281 
282 	/* Reread the pcbinfo and write out the footer. */
283 	kread(off, &pcbinfo, sizeof(pcbinfo));
284 	xig.xig_count = pcbinfo.ipi_count;
285 	xig.xig_gen = pcbinfo.ipi_gencnt;
286 	COPYOUT(&xig, sizeof xig);
287 
288 	*bufp = buf;
289 	return (1);
290 
291 fail:
292 	free(buf);
293 	return (0);
294 #undef COPYOUT
295 #undef KREAD
296 }
297 
298 /*
299  * Print a summary of connections related to an Internet
300  * protocol.  For TCP, also give state of connection.
301  * Listening processes (aflag) are suppressed unless the
302  * -a (all) flag is specified.
303  */
304 void
305 protopr(u_long off, const char *name, int af1, int proto)
306 {
307 	int istcp;
308 	static int first = 1;
309 	char *buf;
310 	const char *vchar;
311 	struct tcpcb *tp = NULL;
312 	struct inpcb *inp;
313 	struct xinpgen *xig, *oxig;
314 	struct xsocket *so;
315 	struct xtcp_timer *timer;
316 
317 	istcp = 0;
318 	switch (proto) {
319 	case IPPROTO_TCP:
320 #ifdef INET6
321 		if (strncmp(name, "sdp", 3) != 0) {
322 			if (tcp_done != 0)
323 				return;
324 			else
325 				tcp_done = 1;
326 		} else {
327 			if (sdp_done != 0)
328 				return;
329 			else
330 				sdp_done = 1;
331 		}
332 #endif
333 		istcp = 1;
334 		break;
335 	case IPPROTO_UDP:
336 #ifdef INET6
337 		if (udp_done != 0)
338 			return;
339 		else
340 			udp_done = 1;
341 #endif
342 		break;
343 	}
344 	if (live) {
345 		if (!pcblist_sysctl(proto, name, &buf, istcp))
346 			return;
347 	} else {
348 		if (!pcblist_kvm(off, &buf, istcp))
349 			return;
350 	}
351 
352 	oxig = xig = (struct xinpgen *)buf;
353 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
354 	    xig->xig_len > sizeof(struct xinpgen);
355 	    xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
356 		if (istcp) {
357 			timer = &((struct xtcpcb *)xig)->xt_timer;
358 			tp = &((struct xtcpcb *)xig)->xt_tp;
359 			inp = &((struct xtcpcb *)xig)->xt_inp;
360 			so = &((struct xtcpcb *)xig)->xt_socket;
361 		} else {
362 			inp = &((struct xinpcb *)xig)->xi_inp;
363 			so = &((struct xinpcb *)xig)->xi_socket;
364 			timer = NULL;
365 		}
366 
367 		/* Ignore sockets for protocols other than the desired one. */
368 		if (so->xso_protocol != proto)
369 			continue;
370 
371 		/* Ignore PCBs which were freed during copyout. */
372 		if (inp->inp_gencnt > oxig->xig_gen)
373 			continue;
374 
375 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
376 #ifdef INET6
377 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
378 #endif /* INET6 */
379 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
380 #ifdef INET6
381 					  && (inp->inp_vflag & INP_IPV6) == 0
382 #endif /* INET6 */
383 			))
384 		    )
385 			continue;
386 		if (!aflag &&
387 		    (
388 		     (istcp && tp->t_state == TCPS_LISTEN)
389 		     || (af1 == AF_INET &&
390 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
391 #ifdef INET6
392 		     || (af1 == AF_INET6 &&
393 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
394 #endif /* INET6 */
395 		     || (af1 == AF_UNSPEC &&
396 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
397 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
398 #ifdef INET6
399 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
400 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
401 #endif
402 			  ))
403 		     ))
404 			continue;
405 
406 		if (first) {
407 			if (!Lflag) {
408 				xo_emit("Active Internet connections");
409 				if (aflag)
410 					xo_emit(" (including servers)");
411 			} else
412 				xo_emit(
413 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
414 			xo_emit("\n");
415 			if (Aflag)
416 				xo_emit("{T:/%-*s} ", 2 * (int)sizeof(void *),
417 				    "Tcpcb");
418 			if (Lflag)
419 				xo_emit((Aflag && !Wflag) ?
420 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-18.18s}" :
421 				    ((!Wflag || af1 == AF_INET) ?
422 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-22.22s}" :
423 				    "{T:/%-5.5s} {T:/%-32.32s} {T:/%-45.45s}"),
424 				    "Proto", "Listen", "Local Address");
425 			else if (Tflag)
426 				xo_emit((Aflag && !Wflag) ?
427     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%s}" :
428 				    ((!Wflag || af1 == AF_INET) ?
429     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%s}" :
430     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%s}"),
431 				    "Proto", "Rexmit", "OOORcv", "0-win",
432 				    "Local Address", "Foreign Address");
433 			else {
434 				xo_emit((Aflag && !Wflag) ?
435     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-18.18s} {T:/%-18.18s}" :
436 				    ((!Wflag || af1 == AF_INET) ?
437     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-22.22s} {T:/%-22.22s}" :
438     "{T:/%-5.5s} {T:/%-6.6s} {T:/%-6.6s} {T:/%-45.45s} {T:/%-45.45s}"),
439 				    "Proto", "Recv-Q", "Send-Q",
440 				    "Local Address", "Foreign Address");
441 				if (!xflag && !Rflag)
442 					xo_emit(" (state)");
443 			}
444 			if (xflag) {
445 				xo_emit(" {T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
446 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
447 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s} "
448 				    "{T:/%-6.6s} {T:/%-6.6s} {T:/%-6.6s}",
449 				    "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
450 				    "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
451 				    "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX");
452 				xo_emit(" {T:/%7.7s} {T:/%7.7s} {T:/%7.7s} "
453 				    "{T:/%7.7s} {T:/%7.7s} {T:/%7.7s}",
454 				    "rexmt", "persist", "keep", "2msl",
455 				    "delack", "rcvtime");
456 			} else if (Rflag) {
457 				xo_emit("  {T:/%8.8s} {T:/%5.5s}",
458 				    "flowid", "ftype");
459 			}
460 			xo_emit("\n");
461 			first = 0;
462 		}
463 		if (Lflag && so->so_qlimit == 0)
464 			continue;
465 		xo_open_instance("socket");
466 		if (Aflag) {
467 			if (istcp)
468 				xo_emit("{q:address/%*lx} ",
469 				    2 * (int)sizeof(void *),
470 				    (u_long)inp->inp_ppcb);
471 			else
472 				xo_emit("{q:address/%*lx} ",
473 				    2 * (int)sizeof(void *),
474 				    (u_long)so->so_pcb);
475 		}
476 #ifdef INET6
477 		if ((inp->inp_vflag & INP_IPV6) != 0)
478 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
479 			    "46" : "6";
480 		else
481 #endif
482 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
483 		    "4" : "";
484 		if (istcp && (tp->t_flags & TF_TOE) != 0)
485 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", "toe", vchar);
486 		else
487 			xo_emit("{:protocol/%-3.3s%-2.2s/%s%s} ", name, vchar);
488 		if (Lflag) {
489 			char buf1[33];
490 
491 			snprintf(buf1, sizeof buf1, "%u/%u/%u", so->so_qlen,
492 			    so->so_incqlen, so->so_qlimit);
493 			xo_emit("{:listen-queue-sizes/%-32.32s} ", buf1);
494 		} else if (Tflag) {
495 			if (istcp)
496 				xo_emit("{:sent-retransmit-packets/%6u} "
497 				    "{:received-out-of-order-packets/%6u} "
498 				    "{:sent-zero-window/%6u} ",
499 				    tp->t_sndrexmitpack, tp->t_rcvoopack,
500 				    tp->t_sndzerowin);
501 			else
502 				xo_emit("{P:/%21s}", "");
503 		} else {
504 			xo_emit("{:receive-bytes-waiting/%6u} "
505 			    "{:send-bytes-waiting/%6u} ",
506 			    so->so_rcv.sb_cc, so->so_snd.sb_cc);
507 		}
508 		if (numeric_port) {
509 			if (inp->inp_vflag & INP_IPV4) {
510 				inetprint("local", &inp->inp_laddr,
511 				    (int)inp->inp_lport, name, 1, af1);
512 				if (!Lflag)
513 					inetprint("remote", &inp->inp_faddr,
514 					    (int)inp->inp_fport, name, 1, af1);
515 			}
516 #ifdef INET6
517 			else if (inp->inp_vflag & INP_IPV6) {
518 				inet6print("local", &inp->in6p_laddr,
519 				    (int)inp->inp_lport, name, 1);
520 				if (!Lflag)
521 					inet6print("remote", &inp->in6p_faddr,
522 					    (int)inp->inp_fport, name, 1);
523 			} /* else nothing printed now */
524 #endif /* INET6 */
525 		} else if (inp->inp_flags & INP_ANONPORT) {
526 			if (inp->inp_vflag & INP_IPV4) {
527 				inetprint("local", &inp->inp_laddr,
528 				    (int)inp->inp_lport, name, 1, af1);
529 				if (!Lflag)
530 					inetprint("remote", &inp->inp_faddr,
531 					    (int)inp->inp_fport, name, 0, af1);
532 			}
533 #ifdef INET6
534 			else if (inp->inp_vflag & INP_IPV6) {
535 				inet6print("local", &inp->in6p_laddr,
536 				    (int)inp->inp_lport, name, 1);
537 				if (!Lflag)
538 					inet6print("remote", &inp->in6p_faddr,
539 					    (int)inp->inp_fport, name, 0);
540 			} /* else nothing printed now */
541 #endif /* INET6 */
542 		} else {
543 			if (inp->inp_vflag & INP_IPV4) {
544 				inetprint("local", &inp->inp_laddr,
545 				    (int)inp->inp_lport, name, 0, af1);
546 				if (!Lflag)
547 					inetprint("remote", &inp->inp_faddr,
548 					    (int)inp->inp_fport, name,
549 					    inp->inp_lport != inp->inp_fport,
550 					    af1);
551 			}
552 #ifdef INET6
553 			else if (inp->inp_vflag & INP_IPV6) {
554 				inet6print("local", &inp->in6p_laddr,
555 				    (int)inp->inp_lport, name, 0);
556 				if (!Lflag)
557 					inet6print("remote", &inp->in6p_faddr,
558 					    (int)inp->inp_fport, name,
559 					    inp->inp_lport != inp->inp_fport);
560 			} /* else nothing printed now */
561 #endif /* INET6 */
562 		}
563 		if (xflag) {
564 			xo_emit("{:receive-mbufs/%6u} {:send-mbufs/%6u} "
565 			    "{:receive-clusters/%6u} {:send-clusters/%6u} "
566 			    "{:receive-high-water/%6u} {:send-high-water/%6u} "
567 			    "{:receive-low-water/%6u} {:send-low-water/%6u} "
568 			    "{:receive-mbuf-bytes/%6u} {:send-mbuf-bytes/%6u} "
569 			    "{:receive-mbuf-bytes-max/%6u} "
570 			    "{:send-mbuf-bytes-max/%6u}",
571 			    so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
572 			    so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
573 			    so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
574 			    so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
575 			    so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
576 			    so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
577 			if (timer != NULL)
578 				xo_emit(" {:retransmit-timer/%4d.%02d} "
579 				    "{:persist-timer/%4d.%02d} "
580 				    "{:keepalive-timer/%4d.%02d} "
581 				    "{:msl2-timer/%4d.%02d} "
582 				    "{:delay-ack-timer/%4d.%02d} "
583 				    "{:inactivity-timer/%4d.%02d}",
584 				    timer->tt_rexmt / 1000,
585 				    (timer->tt_rexmt % 1000) / 10,
586 				    timer->tt_persist / 1000,
587 				    (timer->tt_persist % 1000) / 10,
588 				    timer->tt_keep / 1000,
589 				    (timer->tt_keep % 1000) / 10,
590 				    timer->tt_2msl / 1000,
591 				    (timer->tt_2msl % 1000) / 10,
592 				    timer->tt_delack / 1000,
593 				    (timer->tt_delack % 1000) / 10,
594 				    timer->t_rcvtime / 1000,
595 				    (timer->t_rcvtime % 1000) / 10);
596 		}
597 		if (istcp && !Lflag && !xflag && !Tflag && !Rflag) {
598 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
599 				xo_emit("{:tcp-state/%d}", tp->t_state);
600 			else {
601 				xo_emit("{:tcp-state/%s}",
602 				    tcpstates[tp->t_state]);
603 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
604 				/* Show T/TCP `hidden state' */
605 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
606 					xo_emit("{:need-syn-or-fin/*}");
607 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
608 			}
609 		}
610 		if (Rflag) {
611 			/* XXX: is this right Alfred */
612 			xo_emit(" {:flow-id/%08x} {:flow-type/%5d}",
613 			    inp->inp_flowid,
614 			    inp->inp_flowtype);
615 		}
616 		xo_emit("\n");
617 		xo_close_instance("socket");
618 	}
619 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
620 		if (oxig->xig_count > xig->xig_count) {
621 			xo_emit("Some {d:lost/%s} sockets may have been "
622 			    "deleted.\n", name);
623 		} else if (oxig->xig_count < xig->xig_count) {
624 			xo_emit("Some {d:created/%s} sockets may have been "
625 			    "created.\n", name);
626 		} else {
627 			xo_emit("Some {d:changed/%s} sockets may have been "
628 			    "created or deleted.\n", name);
629 		}
630 	}
631 	free(buf);
632 }
633 
634 /*
635  * Dump TCP statistics structure.
636  */
637 void
638 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
639 {
640 	struct tcpstat tcpstat;
641 	uint64_t tcps_states[TCP_NSTATES];
642 
643 #ifdef INET6
644 	if (tcp_done != 0)
645 		return;
646 	else
647 		tcp_done = 1;
648 #endif
649 
650 	if (fetch_stats("net.inet.tcp.stats", off, &tcpstat,
651 	    sizeof(tcpstat), kread_counters) != 0)
652 		return;
653 
654 	if (fetch_stats_ro("net.inet.tcp.states", nl[N_TCPS_STATES].n_value,
655 	    &tcps_states, sizeof(tcps_states), kread_counters) != 0)
656 		return;
657 
658 	xo_open_container("tcp");
659 	xo_emit("{T:/%s}:\n", name);
660 
661 #define	p(f, m) if (tcpstat.f || sflag <= 1)				\
662 	xo_emit(m, (uintmax_t )tcpstat.f, plural(tcpstat.f))
663 #define	p1a(f, m) if (tcpstat.f || sflag <= 1)				\
664 	xo_emit(m, (uintmax_t )tcpstat.f)
665 #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
666 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
667 	    (uintmax_t )tcpstat.f2, plural(tcpstat.f2))
668 #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1)	\
669 	xo_emit(m, (uintmax_t )tcpstat.f1, plural(tcpstat.f1),		\
670 	    (uintmax_t )tcpstat.f2)
671 #define	p3(f, m) if (tcpstat.f || sflag <= 1)				\
672 	xo_emit(m, (uintmax_t )tcpstat.f, pluralies(tcpstat.f))
673 
674 	p(tcps_sndtotal, "\t{:sent-packets/%ju} {N:/packet%s sent}\n");
675 	p2(tcps_sndpack,tcps_sndbyte, "\t\t{:sent-data-packets/%ju} "
676 	    "{N:/data packet%s} ({:sent-data-bytes/%ju} {N:/byte%s})\n");
677 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte, "\t\t"
678 	    "{:sent-retransmitted-packets/%ju} {N:/data packet%s} "
679 	    "({:sent-retransmitted-bytes/%ju} {N:/byte%s}) "
680 	    "{N:retransmitted}\n");
681 	p(tcps_sndrexmitbad, "\t\t"
682 	    "{:sent-unnecessary-retransmitted-packets/%ju} "
683 	    "{N:/data packet%s unnecessarily retransmitted}\n");
684 	p(tcps_mturesent, "\t\t{:sent-resends-by-mtu-discovery/%ju} "
685 	    "{N:/resend%s initiated by MTU discovery}\n");
686 	p2a(tcps_sndacks, tcps_delack, "\t\t{:sent-ack-only-packets/%ju} "
687 	    "{N:/ack-only packet%s/} ({:sent-packets-delayed/%ju} "
688 	    "{N:delayed})\n");
689 	p(tcps_sndurg, "\t\t{:sent-urg-only-packets/%ju} "
690 	    "{N:/URG only packet%s}\n");
691 	p(tcps_sndprobe, "\t\t{:sent-window-probe-packets/%ju} "
692 	    "{N:/window probe packet%s}\n");
693 	p(tcps_sndwinup, "\t\t{:sent-window-update-packets/%ju} "
694 	    "{N:/window update packet%s}\n");
695 	p(tcps_sndctrl, "\t\t{:sent-control-packets/%ju} "
696 	    "{N:/control packet%s}\n");
697 	p(tcps_rcvtotal, "\t{:received-packets/%ju} "
698 	    "{N:/packet%s received}\n");
699 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t"
700 	    "{:received-ack-packets/%ju} {N:/ack%s} "
701 	    "{N:(for} {:received-ack-bytes/%ju} {N:/byte%s})\n");
702 	p(tcps_rcvdupack, "\t\t{:received-duplicate-acks/%ju} "
703 	    "{N:/duplicate ack%s}\n");
704 	p(tcps_rcvacktoomuch, "\t\t{:received-acks-for-unsent-data/%ju} "
705 	    "{N:/ack%s for unsent data}\n");
706 	p2(tcps_rcvpack, tcps_rcvbyte, "\t\t"
707 	    "{:received-in-sequence-packets/%ju} {N:/packet%s} "
708 	    "({:received-in-sequence-bytes/%ju} {N:/byte%s}) "
709 	    "{N:received in-sequence}\n");
710 	p2(tcps_rcvduppack, tcps_rcvdupbyte, "\t\t"
711 	    "{:received-completely-duplicate-packets/%ju} "
712 	    "{N:/completely duplicate packet%s} "
713 	    "({:received-completely-duplicate-bytes/%ju} {N:/byte%s})\n");
714 	p(tcps_pawsdrop, "\t\t{:received-old-duplicate-packets/%ju} "
715 	    "{N:/old duplicate packet%s}\n");
716 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte, "\t\t"
717 	    "{:received-some-duplicate-packets/%ju} "
718 	    "{N:/packet%s with some dup. data} "
719 	    "({:received-some-duplicate-bytes/%ju} {N:/byte%s duped/})\n");
720 	p2(tcps_rcvoopack, tcps_rcvoobyte, "\t\t{:received-out-of-order/%ju} "
721 	    "{N:/out-of-order packet%s} "
722 	    "({:received-out-of-order-bytes/%ju} {N:/byte%s})\n");
723 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin, "\t\t"
724 	    "{:received-after-window-packets/%ju} {N:/packet%s} "
725 	    "({:received-after-window-bytes/%ju} {N:/byte%s}) "
726 	    "{N:of data after window}\n");
727 	p(tcps_rcvwinprobe, "\t\t{:received-window-probes/%ju} "
728 	    "{N:/window probe%s}\n");
729 	p(tcps_rcvwinupd, "\t\t{:receive-window-update-packets/%ju} "
730 	    "{N:/window update packet%s}\n");
731 	p(tcps_rcvafterclose, "\t\t{:received-after-close-packets/%ju} "
732 	    "{N:/packet%s received after close}\n");
733 	p(tcps_rcvbadsum, "\t\t{:discard-bad-checksum/%ju} "
734 	    "{N:/discarded for bad checksum%s}\n");
735 	p(tcps_rcvbadoff, "\t\t{:discard-bad-header-offset/%ju} "
736 	    "{N:/discarded for bad header offset field%s}\n");
737 	p1a(tcps_rcvshort, "\t\t{:discard-too-short/%ju} "
738 	    "{N:discarded because packet too short}\n");
739 	p1a(tcps_rcvmemdrop, "\t\t{:discard-memory-problems/%ju} "
740 	    "{N:discarded due to memory problems}\n");
741 	p(tcps_connattempt, "\t{:connection-requests/%ju} "
742 	    "{N:/connection request%s}\n");
743 	p(tcps_accepts, "\t{:connections-accepts/%ju} "
744 	    "{N:/connection accept%s}\n");
745 	p(tcps_badsyn, "\t{:bad-connection-attempts/%ju} "
746 	    "{N:/bad connection attempt%s}\n");
747 	p(tcps_listendrop, "\t{:listen-queue-overflows/%ju} "
748 	    "{N:/listen queue overflow%s}\n");
749 	p(tcps_badrst, "\t{:ignored-in-window-resets/%ju} "
750 	    "{N:/ignored RSTs in the window%s}\n");
751 	p(tcps_connects, "\t{:connections-established/%ju} "
752 	    "{N:/connection%s established (including accepts)}\n");
753 	p(tcps_usedrtt, "\t\t{:connections-hostcache-rtt/%ju} "
754 	    "{N:/time%s used RTT from hostcache}\n");
755 	p(tcps_usedrttvar, "\t\t{:connections-hostcache-rttvar/%ju} "
756 	    "{N:/time%s used RTT variance from hostcache}\n");
757 	p(tcps_usedssthresh, "\t\t{:connections-hostcache-ssthresh/%ju} "
758 	    "{N:/time%s used slow-start threshold from hostcache}\n");
759 	p2(tcps_closed, tcps_drops, "\t{:connections-closed/%ju} "
760 	    "{N:/connection%s closed (including} "
761 	    "{:connection-drops/%ju} {N:/drop%s})\n");
762 	p(tcps_cachedrtt, "\t\t{:connections-updated-rtt-on-close/%ju} "
763 	    "{N:/connection%s updated cached RTT on close}\n");
764 	p(tcps_cachedrttvar, "\t\t"
765 	    "{:connections-updated-variance-on-close/%ju} "
766 	    "{N:/connection%s updated cached RTT variance on close}\n");
767 	p(tcps_cachedssthresh, "\t\t"
768 	    "{:connections-updated-ssthresh-on-close/%ju} "
769 	    "{N:/connection%s updated cached ssthresh on close}\n");
770 	p(tcps_conndrops, "\t{:embryonic-connections-dropped/%ju} "
771 	    "{N:/embryonic connection%s dropped}\n");
772 	p2(tcps_rttupdated, tcps_segstimed, "\t{:segments-updated-rtt/%ju} "
773 	    "{N:/segment%s updated rtt (of} "
774 	    "{:segment-update-attempts/%ju} {N:/attempt%s})\n");
775 	p(tcps_rexmttimeo, "\t{:retransmit-timeouts/%ju} "
776 	    "{N:/retransmit timeout%s}\n");
777 	p(tcps_timeoutdrop, "\t\t"
778 	    "{:connections-dropped-by-retransmit-timeout/%ju} "
779 	    "{N:/connection%s dropped by rexmit timeout}\n");
780 	p(tcps_persisttimeo, "\t{:persist-timeout/%ju} "
781 	    "{N:/persist timeout%s}\n");
782 	p(tcps_persistdrop, "\t\t"
783 	    "{:connections-dropped-by-persist-timeout/%ju} "
784 	    "{N:/connection%s dropped by persist timeout}\n");
785 	p(tcps_finwait2_drops, "\t"
786 	    "{:connections-dropped-by-finwait2-timeout/%ju} "
787 	    "{N:/Connection%s (fin_wait_2) dropped because of timeout}\n");
788 	p(tcps_keeptimeo, "\t{:keepalive-timeout/%ju} "
789 	    "{N:/keepalive timeout%s}\n");
790 	p(tcps_keepprobe, "\t\t{:keepalive-probes/%ju} "
791 	    "{N:/keepalive probe%s sent}\n");
792 	p(tcps_keepdrops, "\t\t{:connections-dropped-by-keepalives/%ju} "
793 	    "{N:/connection%s dropped by keepalive}\n");
794 	p(tcps_predack, "\t{:ack-header-predictions/%ju} "
795 	    "{N:/correct ACK header prediction%s}\n");
796 	p(tcps_preddat, "\t{:data-packet-header-predictions/%ju} "
797 	    "{N:/correct data packet header prediction%s}\n");
798 
799 	xo_open_container("syncache");
800 
801 	p3(tcps_sc_added, "\t{:entries-added/%ju} "
802 	    "{N:/syncache entr%s added}\n");
803 	p1a(tcps_sc_retransmitted, "\t\t{:retransmitted/%ju} "
804 	    "{N:/retransmitted}\n");
805 	p1a(tcps_sc_dupsyn, "\t\t{:duplicates/%ju} {N:/dupsyn}\n");
806 	p1a(tcps_sc_dropped, "\t\t{:dropped/%ju} {N:/dropped}\n");
807 	p1a(tcps_sc_completed, "\t\t{:completed/%ju} {N:/completed}\n");
808 	p1a(tcps_sc_bucketoverflow, "\t\t{:bucket-overflow/%ju} "
809 	    "{N:/bucket overflow}\n");
810 	p1a(tcps_sc_cacheoverflow, "\t\t{:cache-overflow/%ju} "
811 	    "{N:/cache overflow}\n");
812 	p1a(tcps_sc_reset, "\t\t{:reset/%ju} {N:/reset}\n");
813 	p1a(tcps_sc_stale, "\t\t{:stale/%ju} {N:/stale}\n");
814 	p1a(tcps_sc_aborted, "\t\t{:aborted/%ju} {N:/aborted}\n");
815 	p1a(tcps_sc_badack, "\t\t{:bad-ack/%ju} {N:/badack}\n");
816 	p1a(tcps_sc_unreach, "\t\t{:unreachable/%ju} {N:/unreach}\n");
817 	p(tcps_sc_zonefail, "\t\t{:zone-failures/%ju} {N:/zone failure%s}\n");
818 	p(tcps_sc_sendcookie, "\t{:sent-cookies/%ju} {N:/cookie%s sent}\n");
819 	p(tcps_sc_recvcookie, "\t{:receivd-cookies/%ju} "
820 	    "{N:/cookie%s received}\n");
821 
822 	xo_close_container("syncache");
823 
824 	xo_open_container("hostcache");
825 
826 	p3(tcps_hc_added, "\t{:entries-added/%ju} "
827 	    "{N:/hostcache entr%s added}\n");
828 	p1a(tcps_hc_bucketoverflow, "\t\t{:buffer-overflows/%ju} "
829 	    "{N:/bucket overflow}\n");
830 
831 	xo_close_container("hostcache");
832 
833 	xo_open_container("sack");
834 
835 	p(tcps_sack_recovery_episode, "\t{:recovery-episodes/%ju} "
836 	    "{N:/SACK recovery episode%s}\n");
837  	p(tcps_sack_rexmits, "\t{:segment-retransmits/%ju} "
838 	    "{N:/segment rexmit%s in SACK recovery episodes}\n");
839  	p(tcps_sack_rexmit_bytes, "\t{:byte-retransmits/%ju} "
840 	    "{N:/byte rexmit%s in SACK recovery episodes}\n");
841  	p(tcps_sack_rcv_blocks, "\t{:received-blocks/%ju} "
842 	    "{N:/SACK option%s (SACK blocks) received}\n");
843 	p(tcps_sack_send_blocks, "\t{:sent-option-blocks/%ju} "
844 	    "{N:/SACK option%s (SACK blocks) sent}\n");
845 	p1a(tcps_sack_sboverflow, "\t{:scoreboard-overflows/%ju} "
846 	    "{N:/SACK scoreboard overflow}\n");
847 
848 	xo_close_container("sack");
849 	xo_open_container("ecn");
850 
851 	p(tcps_ecn_ce, "\t{:ce-packets/%ju} "
852 	    "{N:/packet%s with ECN CE bit set}\n");
853 	p(tcps_ecn_ect0, "\t{:ect0-packets/%ju} "
854 	    "{N:/packet%s with ECN ECT(0) bit set}\n");
855 	p(tcps_ecn_ect1, "\t{:ect1-packets/%ju} "
856 	    "{N:/packet%s with ECN ECT(1) bit set}\n");
857 	p(tcps_ecn_shs, "\t{:handshakes/%ju} "
858 	    "{N:/successful ECN handshake%s}\n");
859 	p(tcps_ecn_rcwnd, "\t{:congestion-reductions/%ju} "
860 	    "{N:/time%s ECN reduced the congestion window}\n");
861 
862 	xo_close_container("ecn");
863 	xo_open_container("tcp-signature");
864 	p(tcps_sig_rcvgoodsig, "\t{:received-good-signature/%ju} "
865 	    "{N:/packet%s with matching signature received}\n");
866 	p(tcps_sig_rcvbadsig, "\t{:received-bad-signature/%ju} "
867 	    "{N:/packet%s with bad signature received}\n");
868 	p(tcps_sig_err_buildsig, "\t{:failed-make-signature/%ju} "
869 	    "{N:/time%s failed to make signature due to no SA}\n");
870 	p(tcps_sig_err_sigopt, "\t{:no-signature-expected/%ju} "
871 	    "{N:/time%s unexpected signature received}\n");
872 	p(tcps_sig_err_nosigopt, "\t{:no-signature-provided/%ju} "
873 	    "{N:/time%s no signature provided by segment}\n");
874  #undef p
875  #undef p1a
876  #undef p2
877  #undef p2a
878  #undef p3
879 	xo_close_container("tcp-signature");
880 
881 	xo_open_container("TCP connection count by state");
882 	xo_emit("{T:/TCP connection count by state}:\n");
883 	for (int i = 0; i < TCP_NSTATES; i++) {
884 		/*
885 		 * XXXGL: is there a way in libxo to use %s
886 		 * in the "content string" of a format
887 		 * string? I failed to do that, that's why
888 		 * a temporary buffer is used to construct
889 		 * format string for xo_emit().
890 		 */
891 		char fmtbuf[80];
892 
893 		if (sflag > 1 && tcps_states[i] == 0)
894 			continue;
895 		snprintf(fmtbuf, sizeof(fmtbuf), "\t{:%s/%%ju} "
896                     "{Np:/connection ,connections} in %s state\n",
897 		    tcpstates[i], tcpstates[i]);
898 		xo_emit(fmtbuf, (uintmax_t )tcps_states[i]);
899 	}
900 	xo_close_container("TCP connection count by state");
901 
902 	xo_close_container("tcp");
903 }
904 
905 /*
906  * Dump UDP statistics structure.
907  */
908 void
909 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
910 {
911 	struct udpstat udpstat;
912 	uint64_t delivered;
913 
914 #ifdef INET6
915 	if (udp_done != 0)
916 		return;
917 	else
918 		udp_done = 1;
919 #endif
920 
921 	if (fetch_stats("net.inet.udp.stats", off, &udpstat,
922 	    sizeof(udpstat), kread_counters) != 0)
923 		return;
924 
925 	xo_open_container("udp");
926 	xo_emit("{T:/%s}:\n", name);
927 
928 #define	p(f, m) if (udpstat.f || sflag <= 1) \
929 	xo_emit("\t" m, (uintmax_t)udpstat.f, plural(udpstat.f))
930 #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
931 	xo_emit("\t" m, (uintmax_t)udpstat.f)
932 
933 	p(udps_ipackets, "{:received-datagrams/%ju} "
934 	    "{N:/datagram%s received}\n");
935 	p1a(udps_hdrops, "{:dropped-incomplete-headers/%ju} "
936 	    "{N:/with incomplete header}\n");
937 	p1a(udps_badlen, "{:dropped-bad-data-length/%ju} "
938 	    "{N:/with bad data length field}\n");
939 	p1a(udps_badsum, "{:dropped-bad-checksum/%ju} "
940 	    "{N:/with bad checksum}\n");
941 	p1a(udps_nosum, "{:dropped-no-checksum/%ju} "
942 	    "{N:/with no checksum}\n");
943 	p1a(udps_noport, "{:dropped-no-socket/%ju} "
944 	    "{N:/dropped due to no socket}\n");
945 	p(udps_noportbcast, "{:dropped-broadcast-multicast/%ju} "
946 	    "{N:/broadcast\\/multicast datagram%s undelivered}\n");
947 	p1a(udps_fullsock, "{:dropped-full-socket-buffer/%ju} "
948 	    "{N:/dropped due to full socket buffers}\n");
949 	p1a(udpps_pcbhashmiss, "{:not-for-hashed-pcb/%ju} "
950 	    "{N:/not for hashed pcb}\n");
951 	delivered = udpstat.udps_ipackets -
952 		    udpstat.udps_hdrops -
953 		    udpstat.udps_badlen -
954 		    udpstat.udps_badsum -
955 		    udpstat.udps_noport -
956 		    udpstat.udps_noportbcast -
957 		    udpstat.udps_fullsock;
958 	if (delivered || sflag <= 1)
959 		xo_emit("\t{:delivered-packets/%ju} {N:/delivered}\n",
960 		    (uint64_t)delivered);
961 	p(udps_opackets, "{:output-packets/%ju} {N:/datagram%s output}\n");
962 	/* the next statistic is cumulative in udps_noportbcast */
963 	p(udps_filtermcast, "{:multicast-source-filter-matches/%ju} "
964 	    "{N:/time%s multicast source filter matched}\n");
965 #undef p
966 #undef p1a
967 	xo_close_container("udp");
968 }
969 
970 /*
971  * Dump CARP statistics structure.
972  */
973 void
974 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
975 {
976 	struct carpstats carpstat;
977 
978 	if (fetch_stats("net.inet.carp.stats", off, &carpstat,
979 	    sizeof(carpstat), kread_counters) != 0)
980 		return;
981 
982 	xo_open_container(name);
983 	xo_emit("{T:/%s}:\n", name);
984 
985 #define	p(f, m) if (carpstat.f || sflag <= 1) \
986 	xo_emit(m, (uintmax_t)carpstat.f, plural(carpstat.f))
987 #define	p2(f, m) if (carpstat.f || sflag <= 1) \
988 	xo_emit(m, (uintmax_t)carpstat.f)
989 
990 	p(carps_ipackets, "\t{:received-inet-packets/%ju} "
991 	    "{N:/packet%s received (IPv4)}\n");
992 	p(carps_ipackets6, "\t{:received-inet6-packets/%ju} "
993 	    "{N:/packet%s received (IPv6)}\n");
994 	p(carps_badttl, "\t\t{:dropped-wrong-ttl/%ju} "
995 	    "{N:/packet%s discarded for wrong TTL}\n");
996 	p(carps_hdrops, "\t\t{:dropped-short-header/%ju} "
997 	    "{N:/packet%s shorter than header}\n");
998 	p(carps_badsum, "\t\t{:dropped-bad-checksum/%ju} "
999 	    "{N:/discarded for bad checksum%s}\n");
1000 	p(carps_badver,	"\t\t{:dropped-bad-version/%ju} "
1001 	    "{N:/discarded packet%s with a bad version}\n");
1002 	p2(carps_badlen, "\t\t{:dropped-short-packet/%ju} "
1003 	    "{N:/discarded because packet too short}\n");
1004 	p2(carps_badauth, "\t\t{:dropped-bad-authentication/%ju} "
1005 	    "{N:/discarded for bad authentication}\n");
1006 	p2(carps_badvhid, "\t\t{:dropped-bad-vhid/%ju} "
1007 	    "{N:/discarded for bad vhid}\n");
1008 	p2(carps_badaddrs, "\t\t{:dropped-bad-address-list/%ju} "
1009 	    "{N:/discarded because of a bad address list}\n");
1010 	p(carps_opackets, "\t{:sent-inet-packets/%ju} "
1011 	    "{N:/packet%s sent (IPv4)}\n");
1012 	p(carps_opackets6, "\t{:sent-inet6-packets/%ju} "
1013 	    "{N:/packet%s sent (IPv6)}\n");
1014 	p2(carps_onomem, "\t\t{:send-failed-memory-error/%ju} "
1015 	    "{N:/send failed due to mbuf memory error}\n");
1016 #if notyet
1017 	p(carps_ostates, "\t\t{:send-state-updates/%s} "
1018 	    "{N:/state update%s sent}\n");
1019 #endif
1020 #undef p
1021 #undef p2
1022 	xo_close_container(name);
1023 }
1024 
1025 /*
1026  * Dump IP statistics structure.
1027  */
1028 void
1029 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1030 {
1031 	struct ipstat ipstat;
1032 
1033 	if (fetch_stats("net.inet.ip.stats", off, &ipstat,
1034 	    sizeof(ipstat), kread_counters) != 0)
1035 		return;
1036 
1037 	xo_open_container(name);
1038 	xo_emit("{T:/%s}:\n", name);
1039 
1040 #define	p(f, m) if (ipstat.f || sflag <= 1) \
1041 	xo_emit(m, (uintmax_t )ipstat.f, plural(ipstat.f))
1042 #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
1043 	xo_emit(m, (uintmax_t )ipstat.f)
1044 
1045 	p(ips_total, "\t{:received-packets/%ju} "
1046 	    "{N:/total packet%s received}\n");
1047 	p(ips_badsum, "\t{:dropped-bad-checksum/%ju} "
1048 	    "{N:/bad header checksum%s}\n");
1049 	p1a(ips_toosmall, "\t{:dropped-below-minimum-size/%ju} "
1050 	    "{N:/with size smaller than minimum}\n");
1051 	p1a(ips_tooshort, "\t{:dropped-short-packets/%ju} "
1052 	    "{N:/with data size < data length}\n");
1053 	p1a(ips_toolong, "\t{:dropped-too-long/%ju} "
1054 	    "{N:/with ip length > max ip packet size}\n");
1055 	p1a(ips_badhlen, "\t{:dropped-short-header-length/%ju} "
1056 	    "{N:/with header length < data size}\n");
1057 	p1a(ips_badlen, "\t{:dropped-short-data/%ju} "
1058 	    "{N:/with data length < header length}\n");
1059 	p1a(ips_badoptions, "\t{:dropped-bad-options/%ju} "
1060 	    "{N:/with bad options}\n");
1061 	p1a(ips_badvers, "\t{:dropped-bad-version/%ju} "
1062 	    "{N:/with incorrect version number}\n");
1063 	p(ips_fragments, "\t{:received-fragments/%ju} "
1064 	    "{N:/fragment%s received}\n");
1065 	p(ips_fragdropped, "\t{:dropped-fragments/%ju} "
1066 	    "{N:/fragment%s dropped (dup or out of space)}\n");
1067 	p(ips_fragtimeout, "\t{:dropped-fragments-after-timeout/%ju} "
1068 	    "{N:/fragment%s dropped after timeout}\n");
1069 	p(ips_reassembled, "\t{:reassembled-packets/%ju} "
1070 	    "{N:/packet%s reassembled ok}\n");
1071 	p(ips_delivered, "\t{:received-local-packets/%ju} "
1072 	    "{N:/packet%s for this host}\n");
1073 	p(ips_noproto, "\t{:dropped-unknown-protocol/%ju} "
1074 	    "{N:/packet%s for unknown\\/unsupported protocol}\n");
1075 	p(ips_forward, "\t{:forwarded-packets/%ju} "
1076 	    "{N:/packet%s forwarded}");
1077 	p(ips_fastforward, " ({:fast-forwarded-packets/%ju} "
1078 	    "{N:/packet%s fast forwarded})");
1079 	if (ipstat.ips_forward || sflag <= 1)
1080 		xo_emit("\n");
1081 	p(ips_cantforward, "\t{:packets-cannot-forward/%ju} "
1082 	    "{N:/packet%s not forwardable}\n");
1083 	p(ips_notmember, "\t{:received-unknown-multicast-group/%ju} "
1084 	    "{N:/packet%s received for unknown multicast group}\n");
1085 	p(ips_redirectsent, "\t{:redirects-sent/%ju} "
1086 	    "{N:/redirect%s sent}\n");
1087 	p(ips_localout, "\t{:sent-packets/%ju} "
1088 	    "{N:/packet%s sent from this host}\n");
1089 	p(ips_rawout, "\t{:send-packets-fabricated-header/%ju} "
1090 	    "{N:/packet%s sent with fabricated ip header}\n");
1091 	p(ips_odropped, "\t{:discard-no-mbufs/%ju} "
1092 	    "{N:/output packet%s dropped due to no bufs, etc.}\n");
1093 	p(ips_noroute, "\t{:discard-no-route/%ju} "
1094 	    "{N:/output packet%s discarded due to no route}\n");
1095 	p(ips_fragmented, "\t{:sent-fragments/%ju} "
1096 	    "{N:/output datagram%s fragmented}\n");
1097 	p(ips_ofragments, "\t{:fragments-created/%ju} "
1098 	    "{N:/fragment%s created}\n");
1099 	p(ips_cantfrag, "\t{:discard-cannot-fragment/%ju} "
1100 	    "{N:/datagram%s that can't be fragmented}\n");
1101 	p(ips_nogif, "\t{:discard-tunnel-no-gif/%ju} "
1102 	    "{N:/tunneling packet%s that can't find gif}\n");
1103 	p(ips_badaddr, "\t{:discard-bad-address/%ju} "
1104 	    "{N:/datagram%s with bad address in header}\n");
1105 #undef p
1106 #undef p1a
1107 	xo_close_container(name);
1108 }
1109 
1110 /*
1111  * Dump ARP statistics structure.
1112  */
1113 void
1114 arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1115 {
1116 	struct arpstat arpstat;
1117 
1118 	if (fetch_stats("net.link.ether.arp.stats", off, &arpstat,
1119 	    sizeof(arpstat), kread_counters) != 0)
1120 		return;
1121 
1122 	xo_open_container(name);
1123 	xo_emit("{T:/%s}:\n", name);
1124 
1125 #define	p(f, m) if (arpstat.f || sflag <= 1) \
1126 	xo_emit("\t" m, (uintmax_t)arpstat.f, plural(arpstat.f))
1127 #define	p2(f, m) if (arpstat.f || sflag <= 1) \
1128 	xo_emit("\t" m, (uintmax_t)arpstat.f, pluralies(arpstat.f))
1129 
1130 	p(txrequests, "{:sent-requests/%ju} {N:/ARP request%s sent}\n");
1131 	p2(txreplies, "{:sent-replies/%ju} {N:/ARP repl%s sent}\n");
1132 	p(rxrequests, "{:received-requests/%ju} "
1133 	    "{N:/ARP request%s received}\n");
1134 	p2(rxreplies, "{:received-replies/%ju} "
1135 	    "{N:/ARP repl%s received}\n");
1136 	p(received, "{:received-packers/%ju} "
1137 	    "{N:/ARP packet%s received}\n");
1138 	p(dropped, "{:dropped-no-entry/%ju} "
1139 	    "{N:/total packet%s dropped due to no ARP entry}\n");
1140 	p(timeouts, "{:entries-timeout/%ju} "
1141 	    "{N:/ARP entry%s timed out}\n");
1142 	p(dupips, "{:dropped-duplicate-address/%ju} "
1143 	    "{N:/Duplicate IP%s seen}\n");
1144 #undef p
1145 #undef p2
1146 	xo_close_container(name);
1147 }
1148 
1149 
1150 
1151 static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
1152 	"echo reply",			/* RFC 792 */
1153 	"#1",
1154 	"#2",
1155 	"destination unreachable",	/* RFC 792 */
1156 	"source quench",		/* RFC 792 */
1157 	"routing redirect",		/* RFC 792 */
1158 	"#6",
1159 	"#7",
1160 	"echo",				/* RFC 792 */
1161 	"router advertisement",		/* RFC 1256 */
1162 	"router solicitation",		/* RFC 1256 */
1163 	"time exceeded",		/* RFC 792 */
1164 	"parameter problem",		/* RFC 792 */
1165 	"time stamp",			/* RFC 792 */
1166 	"time stamp reply",		/* RFC 792 */
1167 	"information request",		/* RFC 792 */
1168 	"information request reply",	/* RFC 792 */
1169 	"address mask request",		/* RFC 950 */
1170 	"address mask reply",		/* RFC 950 */
1171 	"#19",
1172 	"#20",
1173 	"#21",
1174 	"#22",
1175 	"#23",
1176 	"#24",
1177 	"#25",
1178 	"#26",
1179 	"#27",
1180 	"#28",
1181 	"#29",
1182 	"icmp traceroute",		/* RFC 1393 */
1183 	"datagram conversion error",	/* RFC 1475 */
1184 	"mobile host redirect",
1185 	"IPv6 where-are-you",
1186 	"IPv6 i-am-here",
1187 	"mobile registration req",
1188 	"mobile registration reply",
1189 	"domain name request",		/* RFC 1788 */
1190 	"domain name reply",		/* RFC 1788 */
1191 	"icmp SKIP",
1192 	"icmp photuris",		/* RFC 2521 */
1193 };
1194 
1195 /*
1196  * Dump ICMP statistics.
1197  */
1198 void
1199 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1200 {
1201 	struct icmpstat icmpstat;
1202 	size_t len;
1203 	int i, first;
1204 
1205 	if (fetch_stats("net.inet.icmp.stats", off, &icmpstat,
1206 	    sizeof(icmpstat), kread_counters) != 0)
1207 		return;
1208 
1209 	xo_open_container(name);
1210 	xo_emit("{T:/%s}:\n", name);
1211 
1212 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
1213 	xo_emit(m, icmpstat.f, plural(icmpstat.f))
1214 #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
1215 	xo_emit(m, icmpstat.f)
1216 #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
1217 	xo_emit(m, icmpstat.f, plurales(icmpstat.f))
1218 
1219 	p(icps_error, "\t{:icmp-calls/%lu} "
1220 	    "{N:/call%s to icmp_error}\n");
1221 	p(icps_oldicmp, "\t{:errors-not-from-message/%lu} "
1222 	    "{N:/error%s not generated in response to an icmp message}\n");
1223 
1224 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1225 		if (icmpstat.icps_outhist[i] != 0) {
1226 			if (first) {
1227 				xo_open_list("output-histogram");
1228 				xo_emit("\tOutput histogram:\n");
1229 				first = 0;
1230 			}
1231 			xo_open_instance("output-histogram");
1232 			if (icmpnames[i] != NULL)
1233 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1234 				    icmpnames[i], icmpstat.icps_outhist[i]);
1235 			else
1236 				xo_emit("\t\tunknown ICMP #{k:name/%d}: "
1237 				    "{:count/%lu}\n",
1238 				    i, icmpstat.icps_outhist[i]);
1239 			xo_close_instance("output-histogram");
1240 		}
1241 	}
1242 	if (!first)
1243 		xo_close_list("output-histogram");
1244 
1245 	p(icps_badcode, "\t{:dropped-bad-code/%lu} "
1246 	    "{N:/message%s with bad code fields}\n");
1247 	p(icps_tooshort, "\t{:dropped-too-short/%lu} "
1248 	    "{N:/message%s less than the minimum length}\n");
1249 	p(icps_checksum, "\t{:dropped-bad-checksum/%lu} "
1250 	    "{N:/message%s with bad checksum}\n");
1251 	p(icps_badlen, "\t{:dropped-bad-length/%lu} "
1252 	    "{N:/message%s with bad length}\n");
1253 	p1a(icps_bmcastecho, "\t{:dropped-multicast-echo/%lu} "
1254 	    "{N:/multicast echo requests ignored}\n");
1255 	p1a(icps_bmcasttstamp, "\t{:dropped-multicast-timestamp/%lu} "
1256 	    "{N:/multicast timestamp requests ignored}\n");
1257 
1258 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++) {
1259 		if (icmpstat.icps_inhist[i] != 0) {
1260 			if (first) {
1261 				xo_open_list("input-histogram");
1262 				xo_emit("\tInput histogram:\n");
1263 				first = 0;
1264 			}
1265 			xo_open_instance("input-histogram");
1266 			if (icmpnames[i] != NULL)
1267 				xo_emit("\t\t{k:name/%s}: {:count/%lu}\n",
1268 					icmpnames[i],
1269 					icmpstat.icps_inhist[i]);
1270 			else
1271 				xo_emit(
1272 			"\t\tunknown ICMP #{k:name/%d}: {:count/%lu}\n",
1273 					i, icmpstat.icps_inhist[i]);
1274 			xo_close_instance("input-histogram");
1275 		}
1276 	}
1277 	if (!first)
1278 		xo_close_list("input-histogram");
1279 
1280 	p(icps_reflect, "\t{:sent-packets/%lu} "
1281 	    "{N:/message response%s generated}\n");
1282 	p2(icps_badaddr, "\t{:discard-invalid-return-address/%lu} "
1283 	    "{N:/invalid return address%s}\n");
1284 	p(icps_noroute, "\t{:discard-no-route/%lu} "
1285 	    "{N:/no return route%s}\n");
1286 #undef p
1287 #undef p1a
1288 #undef p2
1289 	if (live) {
1290 		len = sizeof i;
1291 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1292 		    0)
1293 			return;
1294 		xo_emit("\tICMP address mask responses are "
1295 		    "{q:icmp-address-responses/%sabled}\n", i ? "en" : "dis");
1296 	}
1297 
1298 	xo_close_container(name);
1299 }
1300 
1301 /*
1302  * Dump IGMP statistics structure.
1303  */
1304 void
1305 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1306 {
1307 	struct igmpstat igmpstat;
1308 
1309 	if (fetch_stats("net.inet.igmp.stats", 0, &igmpstat,
1310 	    sizeof(igmpstat), kread) != 0)
1311 		return;
1312 
1313 	if (igmpstat.igps_version != IGPS_VERSION_3) {
1314 		xo_warnx("%s: version mismatch (%d != %d)", __func__,
1315 		    igmpstat.igps_version, IGPS_VERSION_3);
1316 	}
1317 	if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1318 		xo_warnx("%s: size mismatch (%d != %d)", __func__,
1319 		    igmpstat.igps_len, IGPS_VERSION3_LEN);
1320 	}
1321 
1322 	xo_open_container(name);
1323 	xo_emit("{T:/%s}:\n", name);
1324 
1325 #define	p64(f, m) if (igmpstat.f || sflag <= 1) \
1326 	xo_emit(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1327 #define	py64(f, m) if (igmpstat.f || sflag <= 1) \
1328 	xo_emit(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1329 
1330 	p64(igps_rcv_total, "\t{:received-messages/%ju} "
1331 	    "{N:/message%s received}\n");
1332 	p64(igps_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1333 	    "{N:/message%s received with too few bytes}\n");
1334 	p64(igps_rcv_badttl, "\t{:dropped-wrong-ttl/%ju} "
1335 	    "{N:/message%s received with wrong TTL}\n");
1336 	p64(igps_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1337 	    "{N:/message%s received with bad checksum}\n");
1338 	py64(igps_rcv_v1v2_queries, "\t{:received-membership-queries/%ju} "
1339 	    "{N:/V1\\/V2 membership quer%s received}\n");
1340 	py64(igps_rcv_v3_queries, "\t{:received-v3-membership-queries/%ju} "
1341 	    "{N:/V3 membership quer%s received}\n");
1342 	py64(igps_rcv_badqueries, "\t{:dropped-membership-queries/%ju} "
1343 	    "{N:/membership quer%s received with invalid field(s)}\n");
1344 	py64(igps_rcv_gen_queries, "\t{:received-general-queries/%ju} "
1345 	    "{N:/general quer%s received}\n");
1346 	py64(igps_rcv_group_queries, "\t{:received-group-queries/%ju} "
1347 	    "{N:/group quer%s received}\n");
1348 	py64(igps_rcv_gsr_queries, "\t{:received-group-source-queries/%ju} "
1349 	    "{N:/group-source quer%s received}\n");
1350 	py64(igps_drop_gsr_queries, "\t{:dropped-group-source-queries/%ju} "
1351 	    "{N:/group-source quer%s dropped}\n");
1352 	p64(igps_rcv_reports, "\t{:received-membership-requests/%ju} "
1353 	    "{N:/membership report%s received}\n");
1354 	p64(igps_rcv_badreports, "\t{:dropped-membership-reports/%ju} "
1355 	    "{N:/membership report%s received with invalid field(s)}\n");
1356 	p64(igps_rcv_ourreports, "\t"
1357 	    "{:received-membership-reports-matching/%ju} "
1358 	    "{N:/membership report%s received for groups to which we belong}"
1359 	    "\n");
1360 	p64(igps_rcv_nora, "\t{:received-v3-reports-no-router-alert/%ju} "
1361 	    "{N:/V3 report%s received without Router Alert}\n");
1362 	p64(igps_snd_reports, "\t{:sent-membership-reports/%ju} "
1363 	    "{N:/membership report%s sent}\n");
1364 #undef p64
1365 #undef py64
1366 	xo_close_container(name);
1367 }
1368 
1369 /*
1370  * Dump PIM statistics structure.
1371  */
1372 void
1373 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1374     int proto __unused)
1375 {
1376 	struct pimstat pimstat;
1377 
1378 	if (fetch_stats("net.inet.pim.stats", off, &pimstat,
1379 	    sizeof(pimstat), kread_counters) != 0)
1380 		return;
1381 
1382 	xo_open_container(name);
1383 	xo_emit("{T:/%s}:\n", name);
1384 
1385 #define	p(f, m) if (pimstat.f || sflag <= 1) \
1386 	xo_emit(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1387 #define	py(f, m) if (pimstat.f || sflag <= 1) \
1388 	xo_emit(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1389 
1390 	p(pims_rcv_total_msgs, "\t{:received-messages/%ju} "
1391 	    "{N:/message%s received}\n");
1392 	p(pims_rcv_total_bytes, "\t{:received-bytes/%ju} "
1393 	    "{N:/byte%s received}\n");
1394 	p(pims_rcv_tooshort, "\t{:dropped-too-short/%ju} "
1395 	    "{N:/message%s received with too few bytes}\n");
1396 	p(pims_rcv_badsum, "\t{:dropped-bad-checksum/%ju} "
1397 	    "{N:/message%s received with bad checksum}\n");
1398 	p(pims_rcv_badversion, "\t{:dropped-bad-version/%ju} "
1399 	    "{N:/message%s received with bad version}\n");
1400 	p(pims_rcv_registers_msgs, "\t{:received-data-register-messages/%ju} "
1401 	    "{N:/data register message%s received}\n");
1402 	p(pims_rcv_registers_bytes, "\t{:received-data-register-bytes/%ju} "
1403 	    "{N:/data register byte%s received}\n");
1404 	p(pims_rcv_registers_wrongiif, "\t"
1405 	    "{:received-data-register-wrong-interface/%ju} "
1406 	    "{N:/data register message%s received on wrong iif}\n");
1407 	p(pims_rcv_badregisters, "\t{:received-bad-registers/%ju} "
1408 	    "{N:/bad register%s received}\n");
1409 	p(pims_snd_registers_msgs, "\t{:sent-data-register-messages/%ju} "
1410 	    "{N:/data register message%s sent}\n");
1411 	p(pims_snd_registers_bytes, "\t{:sent-data-register-bytes/%ju} "
1412 	    "{N:/data register byte%s sent}\n");
1413 #undef p
1414 #undef py
1415 	xo_close_container(name);
1416 }
1417 
1418 /*
1419  * Pretty print an Internet address (net address + port).
1420  */
1421 void
1422 inetprint(const char *container, struct in_addr *in, int port,
1423     const char *proto, int num_port, const int af1)
1424 {
1425 	struct servent *sp = 0;
1426 	char line[80], *cp;
1427 	int width;
1428 	size_t alen, plen;
1429 
1430 	if (container)
1431 		xo_open_container(container);
1432 
1433 	if (Wflag)
1434 	    snprintf(line, sizeof(line), "%s.", inetname(in));
1435 	else
1436 	    snprintf(line, sizeof(line), "%.*s.",
1437 		(Aflag && !num_port) ? 12 : 16, inetname(in));
1438 	alen = strlen(line);
1439 	cp = line + alen;
1440 	if (!num_port && port)
1441 		sp = getservbyport((int)port, proto);
1442 	if (sp || port == 0)
1443 		snprintf(cp, sizeof(line) - alen,
1444 		    "%.15s ", sp ? sp->s_name : "*");
1445 	else
1446 		snprintf(cp, sizeof(line) - alen,
1447 		    "%d ", ntohs((u_short)port));
1448 	width = (Aflag && !Wflag) ? 18 :
1449 		((!Wflag || af1 == AF_INET) ? 22 : 45);
1450 	if (Wflag)
1451 		xo_emit("{d:target/%-*s} ", width, line);
1452 	else
1453 		xo_emit("{d:target/%-*.*s} ", width, width, line);
1454 
1455 	plen = strlen(cp) - 1;
1456 	alen--;
1457 	xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
1458 	    plen, cp);
1459 
1460 	if (container)
1461 		xo_close_container(container);
1462 }
1463 
1464 /*
1465  * Construct an Internet address representation.
1466  * If numeric_addr has been supplied, give
1467  * numeric value, otherwise try for symbolic name.
1468  */
1469 char *
1470 inetname(struct in_addr *inp)
1471 {
1472 	char *cp;
1473 	static char line[MAXHOSTNAMELEN];
1474 	struct hostent *hp;
1475 	struct netent *np;
1476 
1477 	cp = 0;
1478 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1479 		int net = inet_netof(*inp);
1480 		int lna = inet_lnaof(*inp);
1481 
1482 		if (lna == INADDR_ANY) {
1483 			np = getnetbyaddr(net, AF_INET);
1484 			if (np)
1485 				cp = np->n_name;
1486 		}
1487 		if (cp == NULL) {
1488 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1489 			if (hp) {
1490 				cp = hp->h_name;
1491 				trimdomain(cp, strlen(cp));
1492 			}
1493 		}
1494 	}
1495 	if (inp->s_addr == INADDR_ANY)
1496 		strcpy(line, "*");
1497 	else if (cp) {
1498 		strlcpy(line, cp, sizeof(line));
1499 	} else {
1500 		inp->s_addr = ntohl(inp->s_addr);
1501 #define	C(x)	((u_int)((x) & 0xff))
1502 		snprintf(line, sizeof(line), "%u.%u.%u.%u",
1503 		    C(inp->s_addr >> 24), C(inp->s_addr >> 16),
1504 		    C(inp->s_addr >> 8), C(inp->s_addr));
1505 	}
1506 	return (line);
1507 }
1508