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