xref: /freebsd/usr.bin/netstat/inet.c (revision a9148abd9da5db2f1c682fb17bed791845fc41c9)
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #if 0
35 #ifndef lint
36 static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
37 #endif /* not lint */
38 #endif
39 
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD$");
42 
43 #include <sys/param.h>
44 #include <sys/queue.h>
45 #include <sys/domain.h>
46 #include <sys/protosw.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/sysctl.h>
50 
51 #include <net/route.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_carp.h>
56 #ifdef INET6
57 #include <netinet/ip6.h>
58 #endif /* INET6 */
59 #include <netinet/in_pcb.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/icmp_var.h>
62 #include <netinet/igmp_var.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/pim_var.h>
65 #include <netinet/tcp.h>
66 #include <netinet/tcpip.h>
67 #include <netinet/tcp_seq.h>
68 #define	TCPSTATES
69 #include <netinet/tcp_fsm.h>
70 #include <netinet/tcp_timer.h>
71 #include <netinet/tcp_var.h>
72 #include <netinet/tcp_debug.h>
73 #include <netinet/udp.h>
74 #include <netinet/udp_var.h>
75 
76 #include <arpa/inet.h>
77 #include <err.h>
78 #include <errno.h>
79 #include <libutil.h>
80 #include <netdb.h>
81 #include <stdint.h>
82 #include <stdio.h>
83 #include <stdlib.h>
84 #include <string.h>
85 #include <unistd.h>
86 #include "netstat.h"
87 
88 char	*inetname(struct in_addr *);
89 void	inetprint(struct in_addr *, int, const char *, int);
90 #ifdef INET6
91 static int udp_done, tcp_done;
92 #endif /* INET6 */
93 
94 static int
95 pcblist_sysctl(int proto, char **bufp, int istcp)
96 {
97 	const char *mibvar;
98 	char *buf;
99 	size_t len;
100 
101 	switch (proto) {
102 	case IPPROTO_TCP:
103 		mibvar = "net.inet.tcp.pcblist";
104 		break;
105 	case IPPROTO_UDP:
106 		mibvar = "net.inet.udp.pcblist";
107 		break;
108 	case IPPROTO_DIVERT:
109 		mibvar = "net.inet.divert.pcblist";
110 		break;
111 	default:
112 		mibvar = "net.inet.raw.pcblist";
113 		break;
114 	}
115 
116 	len = 0;
117 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
118 		if (errno != ENOENT)
119 			warn("sysctl: %s", mibvar);
120 		return (0);
121 	}
122 	if ((buf = malloc(len)) == 0) {
123 		warnx("malloc %lu bytes", (u_long)len);
124 		return (0);
125 	}
126 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
127 		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_cc;
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)) == 0) {
210 		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 		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_vflag & 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 
316 	istcp = 0;
317 	switch (proto) {
318 	case IPPROTO_TCP:
319 #ifdef INET6
320 		if (tcp_done != 0)
321 			return;
322 		else
323 			tcp_done = 1;
324 #endif
325 		istcp = 1;
326 		break;
327 	case IPPROTO_UDP:
328 #ifdef INET6
329 		if (udp_done != 0)
330 			return;
331 		else
332 			udp_done = 1;
333 #endif
334 		break;
335 	}
336 	if (live) {
337 		if (!pcblist_sysctl(proto, &buf, istcp))
338 			return;
339 	} else {
340 		if (!pcblist_kvm(off, &buf, istcp))
341 			return;
342 	}
343 
344 	oxig = xig = (struct xinpgen *)buf;
345 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
346 	     xig->xig_len > sizeof(struct xinpgen);
347 	     xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
348 		if (istcp) {
349 			tp = &((struct xtcpcb *)xig)->xt_tp;
350 			inp = &((struct xtcpcb *)xig)->xt_inp;
351 			so = &((struct xtcpcb *)xig)->xt_socket;
352 		} else {
353 			inp = &((struct xinpcb *)xig)->xi_inp;
354 			so = &((struct xinpcb *)xig)->xi_socket;
355 		}
356 
357 		/* Ignore sockets for protocols other than the desired one. */
358 		if (so->xso_protocol != proto)
359 			continue;
360 
361 		/* Ignore PCBs which were freed during copyout. */
362 		if (inp->inp_gencnt > oxig->xig_gen)
363 			continue;
364 
365 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
366 #ifdef INET6
367 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
368 #endif /* INET6 */
369 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
370 #ifdef INET6
371 					  && (inp->inp_vflag & INP_IPV6) == 0
372 #endif /* INET6 */
373 			))
374 		    )
375 			continue;
376 		if (!aflag &&
377 		    (
378 		     (istcp && tp->t_state == TCPS_LISTEN)
379 		     || (af1 == AF_INET &&
380 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
381 #ifdef INET6
382 		     || (af1 == AF_INET6 &&
383 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
384 #endif /* INET6 */
385 		     || (af1 == AF_UNSPEC &&
386 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
387 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
388 #ifdef INET6
389 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
390 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
391 #endif
392 			  ))
393 		     ))
394 			continue;
395 
396 		if (first) {
397 			if (!Lflag) {
398 				printf("Active Internet connections");
399 				if (aflag)
400 					printf(" (including servers)");
401 			} else
402 				printf(
403 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
404 			putchar('\n');
405 			if (Aflag)
406 				printf("%-8.8s ", "Tcpcb");
407 			if (Lflag)
408 				printf("%-5.5s %-14.14s %-22.22s\n",
409 				    "Proto", "Listen", "Local Address");
410 			printf((Aflag && !Wflag) ?
411 			       "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s" :
412 			       "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s",
413 			       "Proto", "Recv-Q", "Send-Q",
414 			       "Local Address", "Foreign Address");
415 			if (xflag)
416 				printf("%-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %s\n",
417 				       "R-MBUF", "S-MBUF", "R-CLUS", "S-CLUS",
418 				       "R-HIWA", "S-HIWA", "R-LOWA", "S-LOWA",
419 				       "R-BCNT", "S-BCNT", "R-BMAX", "S-BMAX",
420 				       "(state)");
421 			else
422 				printf("(state)\n");
423 			first = 0;
424 		}
425 		if (Lflag && so->so_qlimit == 0)
426 			continue;
427 		if (Aflag) {
428 			if (istcp)
429 				printf("%8lx ", (u_long)inp->inp_ppcb);
430 			else
431 				printf("%8lx ", (u_long)so->so_pcb);
432 		}
433 #ifdef INET6
434 		if ((inp->inp_vflag & INP_IPV6) != 0)
435 			vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
436 			    "46" : "6 ";
437 		else
438 #endif
439 		vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
440 		    "4 " : "  ";
441 		printf("%-3.3s%-2.2s ", name, vchar);
442 		if (Lflag) {
443 			char buf1[15];
444 
445 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
446 			    so->so_incqlen, so->so_qlimit);
447 			printf("%-14.14s ", buf1);
448 		} else {
449 			printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
450 		}
451 		if (numeric_port) {
452 			if (inp->inp_vflag & INP_IPV4) {
453 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
454 				    name, 1);
455 				if (!Lflag)
456 					inetprint(&inp->inp_faddr,
457 					    (int)inp->inp_fport, name, 1);
458 			}
459 #ifdef INET6
460 			else if (inp->inp_vflag & INP_IPV6) {
461 				inet6print(&inp->in6p_laddr,
462 				    (int)inp->inp_lport, name, 1);
463 				if (!Lflag)
464 					inet6print(&inp->in6p_faddr,
465 					    (int)inp->inp_fport, name, 1);
466 			} /* else nothing printed now */
467 #endif /* INET6 */
468 		} else if (inp->inp_flags & INP_ANONPORT) {
469 			if (inp->inp_vflag & INP_IPV4) {
470 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
471 				    name, 1);
472 				if (!Lflag)
473 					inetprint(&inp->inp_faddr,
474 					    (int)inp->inp_fport, name, 0);
475 			}
476 #ifdef INET6
477 			else if (inp->inp_vflag & INP_IPV6) {
478 				inet6print(&inp->in6p_laddr,
479 				    (int)inp->inp_lport, name, 1);
480 				if (!Lflag)
481 					inet6print(&inp->in6p_faddr,
482 					    (int)inp->inp_fport, name, 0);
483 			} /* else nothing printed now */
484 #endif /* INET6 */
485 		} else {
486 			if (inp->inp_vflag & INP_IPV4) {
487 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
488 				    name, 0);
489 				if (!Lflag)
490 					inetprint(&inp->inp_faddr,
491 					    (int)inp->inp_fport, name,
492 					    inp->inp_lport != inp->inp_fport);
493 			}
494 #ifdef INET6
495 			else if (inp->inp_vflag & INP_IPV6) {
496 				inet6print(&inp->in6p_laddr,
497 				    (int)inp->inp_lport, name, 0);
498 				if (!Lflag)
499 					inet6print(&inp->in6p_faddr,
500 					    (int)inp->inp_fport, name,
501 					    inp->inp_lport != inp->inp_fport);
502 			} /* else nothing printed now */
503 #endif /* INET6 */
504 		}
505 		if (xflag) {
506 			if (Lflag)
507 				printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
508 				       " ",
509 				       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
510 				       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
511 				       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
512 				       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
513 				       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
514 				       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
515 			else
516 				printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
517 				       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
518 				       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
519 				       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
520 				       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
521 				       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
522 				       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
523 		}
524 		if (istcp && !Lflag) {
525 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
526 				printf("%d", tp->t_state);
527 			else {
528 				printf("%s", tcpstates[tp->t_state]);
529 #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
530 				/* Show T/TCP `hidden state' */
531 				if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
532 					putchar('*');
533 #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
534 			}
535 		}
536 		putchar('\n');
537 	}
538 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
539 		if (oxig->xig_count > xig->xig_count) {
540 			printf("Some %s sockets may have been deleted.\n",
541 			    name);
542 		} else if (oxig->xig_count < xig->xig_count) {
543 			printf("Some %s sockets may have been created.\n",
544 			    name);
545 		} else {
546 			printf(
547 	"Some %s sockets may have been created or deleted.\n",
548 			    name);
549 		}
550 	}
551 	free(buf);
552 }
553 
554 /*
555  * Dump TCP statistics structure.
556  */
557 void
558 tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
559 {
560 	struct tcpstat tcpstat, zerostat;
561 	size_t len = sizeof tcpstat;
562 
563 #ifdef INET6
564 	if (tcp_done != 0)
565 		return;
566 	else
567 		tcp_done = 1;
568 #endif
569 
570 	if (live) {
571 		if (zflag)
572 			memset(&zerostat, 0, len);
573 		if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
574 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
575 			warn("sysctl: net.inet.tcp.stats");
576 			return;
577 		}
578 	} else
579 		kread(off, &tcpstat, len);
580 
581 	printf ("%s:\n", name);
582 
583 #define	p(f, m) if (tcpstat.f || sflag <= 1) \
584     printf(m, tcpstat.f, plural(tcpstat.f))
585 #define	p1a(f, m) if (tcpstat.f || sflag <= 1) \
586     printf(m, tcpstat.f)
587 #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
588     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
589 #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
590     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
591 #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
592     printf(m, tcpstat.f, pluralies(tcpstat.f))
593 
594 	p(tcps_sndtotal, "\t%lu packet%s sent\n");
595 	p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
596 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
597 	    "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
598 	p(tcps_sndrexmitbad,
599 	    "\t\t%lu data packet%s unnecessarily retransmitted\n");
600 	p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
601 	p2a(tcps_sndacks, tcps_delack,
602 	    "\t\t%lu ack-only packet%s (%lu delayed)\n");
603 	p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
604 	p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
605 	p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
606 	p(tcps_sndctrl, "\t\t%lu control packet%s\n");
607 	p(tcps_rcvtotal, "\t%lu packet%s received\n");
608 	p2(tcps_rcvackpack, tcps_rcvackbyte,
609 	    "\t\t%lu ack%s (for %lu byte%s)\n");
610 	p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
611 	p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
612 	p2(tcps_rcvpack, tcps_rcvbyte,
613 	    "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
614 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
615 	    "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
616 	p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
617 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
618 	    "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
619 	p2(tcps_rcvoopack, tcps_rcvoobyte,
620 	    "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
621 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
622 	    "\t\t%lu packet%s (%lu byte%s) of data after window\n");
623 	p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
624 	p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
625 	p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
626 	p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
627 	p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
628 	p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
629 	p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
630 	p(tcps_connattempt, "\t%lu connection request%s\n");
631 	p(tcps_accepts, "\t%lu connection accept%s\n");
632 	p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
633 	p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
634 	p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
635 	p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
636 	p2(tcps_closed, tcps_drops,
637 	    "\t%lu connection%s closed (including %lu drop%s)\n");
638 	p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
639 	p(tcps_cachedrttvar,
640 	    "\t\t%lu connection%s updated cached RTT variance on close\n");
641 	p(tcps_cachedssthresh,
642 	    "\t\t%lu connection%s updated cached ssthresh on close\n");
643 	p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
644 	p2(tcps_rttupdated, tcps_segstimed,
645 	    "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
646 	p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
647 	p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
648 	p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
649 	p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
650 	p(tcps_finwait2_drops,
651 	    "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
652 	p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
653 	p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
654 	p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
655 	p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
656 	p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
657 
658 	p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
659 	p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
660 	p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
661 	p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
662 	p1a(tcps_sc_completed, "\t\t%lu completed\n");
663 	p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
664 	p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
665 	p1a(tcps_sc_reset, "\t\t%lu reset\n");
666 	p1a(tcps_sc_stale, "\t\t%lu stale\n");
667 	p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
668 	p1a(tcps_sc_badack, "\t\t%lu badack\n");
669 	p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
670 	p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
671 	p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
672 	p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
673 
674 	p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
675 	p(tcps_sack_rexmits,
676 	    "\t%lu segment rexmit%s in SACK recovery episodes\n");
677 	p(tcps_sack_rexmit_bytes,
678 	    "\t%lu byte rexmit%s in SACK recovery episodes\n");
679 	p(tcps_sack_rcv_blocks,
680 	    "\t%lu SACK option%s (SACK blocks) received\n");
681 	p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
682 	p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
683 
684 	p(tcps_ecn_ce, "\t%lu packet%s with ECN CE bit set\n");
685 	p(tcps_ecn_ect0, "\t%lu packet%s with ECN ECT(0) bit set\n");
686 	p(tcps_ecn_ect1, "\t%lu packet%s with ECN ECT(1) bit set\n");
687 	p(tcps_ecn_shs, "\t%lu successful ECN handshake%s\n");
688 	p(tcps_ecn_rcwnd, "\t%lu time%s ECN reduced the congestion window\n");
689 #undef p
690 #undef p1a
691 #undef p2
692 #undef p2a
693 #undef p3
694 }
695 
696 /*
697  * Dump UDP statistics structure.
698  */
699 void
700 udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
701 {
702 	struct udpstat udpstat, zerostat;
703 	size_t len = sizeof udpstat;
704 	u_long delivered;
705 
706 #ifdef INET6
707 	if (udp_done != 0)
708 		return;
709 	else
710 		udp_done = 1;
711 #endif
712 
713 	if (live) {
714 		if (zflag)
715 			memset(&zerostat, 0, len);
716 		if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
717 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
718 			warn("sysctl: net.inet.udp.stats");
719 			return;
720 		}
721 	} else
722 		kread(off, &udpstat, len);
723 
724 	printf("%s:\n", name);
725 #define	p(f, m) if (udpstat.f || sflag <= 1) \
726     printf(m, udpstat.f, plural(udpstat.f))
727 #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
728     printf(m, udpstat.f)
729 	p(udps_ipackets, "\t%lu datagram%s received\n");
730 	p1a(udps_hdrops, "\t%lu with incomplete header\n");
731 	p1a(udps_badlen, "\t%lu with bad data length field\n");
732 	p1a(udps_badsum, "\t%lu with bad checksum\n");
733 	p1a(udps_nosum, "\t%lu with no checksum\n");
734 	p1a(udps_noport, "\t%lu dropped due to no socket\n");
735 	p(udps_noportbcast,
736 	    "\t%lu broadcast/multicast datagram%s undelivered\n");
737 	p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
738 	p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
739 	delivered = udpstat.udps_ipackets -
740 		    udpstat.udps_hdrops -
741 		    udpstat.udps_badlen -
742 		    udpstat.udps_badsum -
743 		    udpstat.udps_noport -
744 		    udpstat.udps_noportbcast -
745 		    udpstat.udps_fullsock;
746 	if (delivered || sflag <= 1)
747 		printf("\t%lu delivered\n", delivered);
748 	p(udps_opackets, "\t%lu datagram%s output\n");
749 	/* the next statistic is cumulative in udps_noportbcast */
750 	p(udps_filtermcast,
751 	    "\t%lu time%s multicast source filter matched\n");
752 #undef p
753 #undef p1a
754 }
755 
756 /*
757  * Dump CARP statistics structure.
758  */
759 void
760 carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
761 {
762 	struct carpstats carpstat, zerostat;
763 	size_t len = sizeof(struct carpstats);
764 
765 	if (live) {
766 		if (zflag)
767 			memset(&zerostat, 0, len);
768 		if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
769 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
770 			if (errno != ENOENT)
771 				warn("sysctl: net.inet.carp.stats");
772 			return;
773 		}
774 	} else {
775 		if (off == 0)
776 			return;
777 		kread(off, &carpstat, len);
778 	}
779 
780 	printf("%s:\n", name);
781 
782 #define	p(f, m) if (carpstat.f || sflag <= 1) \
783 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
784 #define	p2(f, m) if (carpstat.f || sflag <= 1) \
785 	printf(m, (uintmax_t)carpstat.f)
786 
787 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
788 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
789 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
790 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
791 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
792 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
793 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
794 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
795 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
796 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
797 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
798 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
799 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
800 #if notyet
801 	p(carps_ostates, "\t\t%s state update%s sent\n");
802 #endif
803 #undef p
804 #undef p2
805 }
806 
807 /*
808  * Dump IP statistics structure.
809  */
810 void
811 ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
812 {
813 	struct ipstat ipstat, zerostat;
814 	size_t len = sizeof ipstat;
815 
816 	if (live) {
817 		if (zflag)
818 			memset(&zerostat, 0, len);
819 		if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
820 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
821 			warn("sysctl: net.inet.ip.stats");
822 			return;
823 		}
824 	} else
825 		kread(off, &ipstat, len);
826 
827 	printf("%s:\n", name);
828 
829 #define	p(f, m) if (ipstat.f || sflag <= 1) \
830     printf(m, ipstat.f, plural(ipstat.f))
831 #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
832     printf(m, ipstat.f)
833 
834 	p(ips_total, "\t%lu total packet%s received\n");
835 	p(ips_badsum, "\t%lu bad header checksum%s\n");
836 	p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
837 	p1a(ips_tooshort, "\t%lu with data size < data length\n");
838 	p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
839 	p1a(ips_badhlen, "\t%lu with header length < data size\n");
840 	p1a(ips_badlen, "\t%lu with data length < header length\n");
841 	p1a(ips_badoptions, "\t%lu with bad options\n");
842 	p1a(ips_badvers, "\t%lu with incorrect version number\n");
843 	p(ips_fragments, "\t%lu fragment%s received\n");
844 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
845 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
846 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
847 	p(ips_delivered, "\t%lu packet%s for this host\n");
848 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
849 	p(ips_forward, "\t%lu packet%s forwarded");
850 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
851 	if (ipstat.ips_forward || sflag <= 1)
852 		putchar('\n');
853 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
854 	p(ips_notmember,
855 	    "\t%lu packet%s received for unknown multicast group\n");
856 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
857 	p(ips_localout, "\t%lu packet%s sent from this host\n");
858 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
859 	p(ips_odropped,
860 	    "\t%lu output packet%s dropped due to no bufs, etc.\n");
861 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
862 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
863 	p(ips_ofragments, "\t%lu fragment%s created\n");
864 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
865 	p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
866 	p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
867 #undef p
868 #undef p1a
869 }
870 
871 static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
872 	"echo reply",			/* RFC 792 */
873 	"#1",
874 	"#2",
875 	"destination unreachable",	/* RFC 792 */
876 	"source quench",		/* RFC 792 */
877 	"routing redirect",		/* RFC 792 */
878 	"#6",
879 	"#7",
880 	"echo",				/* RFC 792 */
881 	"router advertisement",		/* RFC 1256 */
882 	"router solicitation",		/* RFC 1256 */
883 	"time exceeded",		/* RFC 792 */
884 	"parameter problem",		/* RFC 792 */
885 	"time stamp",			/* RFC 792 */
886 	"time stamp reply",		/* RFC 792 */
887 	"information request",		/* RFC 792 */
888 	"information request reply",	/* RFC 792 */
889 	"address mask request",		/* RFC 950 */
890 	"address mask reply",		/* RFC 950 */
891 	"#19",
892 	"#20",
893 	"#21",
894 	"#22",
895 	"#23",
896 	"#24",
897 	"#25",
898 	"#26",
899 	"#27",
900 	"#28",
901 	"#29",
902 	"icmp traceroute",		/* RFC 1393 */
903 	"datagram conversion error",	/* RFC 1475 */
904 	"mobile host redirect",
905 	"IPv6 where-are-you",
906 	"IPv6 i-am-here",
907 	"mobile registration req",
908 	"mobile registration reply",
909 	"domain name request",		/* RFC 1788 */
910 	"domain name reply",		/* RFC 1788 */
911 	"icmp SKIP",
912 	"icmp photuris",		/* RFC 2521 */
913 };
914 
915 /*
916  * Dump ICMP statistics.
917  */
918 void
919 icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
920 {
921 	struct icmpstat icmpstat, zerostat;
922 	int i, first;
923 	size_t len;
924 
925 	len = sizeof icmpstat;
926 	if (live) {
927 		if (zflag)
928 			memset(&zerostat, 0, len);
929 		if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
930 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
931 			warn("sysctl: net.inet.icmp.stats");
932 			return;
933 		}
934 	} else
935 		kread(off, &icmpstat, len);
936 
937 	printf("%s:\n", name);
938 
939 #define	p(f, m) if (icmpstat.f || sflag <= 1) \
940     printf(m, icmpstat.f, plural(icmpstat.f))
941 #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
942     printf(m, icmpstat.f)
943 #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
944     printf(m, icmpstat.f, plurales(icmpstat.f))
945 
946 	p(icps_error, "\t%lu call%s to icmp_error\n");
947 	p(icps_oldicmp,
948 	    "\t%lu error%s not generated in response to an icmp message\n");
949 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
950 		if (icmpstat.icps_outhist[i] != 0) {
951 			if (first) {
952 				printf("\tOutput histogram:\n");
953 				first = 0;
954 			}
955 			if (icmpnames[i] != NULL)
956 				printf("\t\t%s: %lu\n", icmpnames[i],
957 					icmpstat.icps_outhist[i]);
958 			else
959 				printf("\t\tunknown ICMP #%d: %lu\n", i,
960 					icmpstat.icps_outhist[i]);
961 		}
962 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
963 	p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
964 	p(icps_checksum, "\t%lu message%s with bad checksum\n");
965 	p(icps_badlen, "\t%lu message%s with bad length\n");
966 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
967 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
968 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
969 		if (icmpstat.icps_inhist[i] != 0) {
970 			if (first) {
971 				printf("\tInput histogram:\n");
972 				first = 0;
973 			}
974 			if (icmpnames[i] != NULL)
975 				printf("\t\t%s: %lu\n", icmpnames[i],
976 				    icmpstat.icps_inhist[i]);
977 			else
978 				printf("\t\tunknown ICMP #%d: %lu\n", i,
979 				    icmpstat.icps_inhist[i]);
980 		}
981 	p(icps_reflect, "\t%lu message response%s generated\n");
982 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
983 	p(icps_noroute, "\t%lu no return route%s\n");
984 #undef p
985 #undef p1a
986 #undef p2
987 	if (live) {
988 		len = sizeof i;
989 		if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
990 		    0)
991 			return;
992 		printf("\tICMP address mask responses are %sabled\n",
993 		    i ? "en" : "dis");
994 	}
995 }
996 
997 /*
998  * Dump IGMP statistics structure.
999  */
1000 void
1001 igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1002 {
1003 	struct igmpstat igmpstat, zerostat;
1004 	size_t len = sizeof igmpstat;
1005 
1006 	if (live) {
1007 		if (zflag)
1008 			memset(&zerostat, 0, len);
1009 		if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1010 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1011 			warn("sysctl: net.inet.igmp.stats");
1012 			return;
1013 		}
1014 	} else
1015 		kread(off, &igmpstat, len);
1016 
1017 	printf("%s:\n", name);
1018 
1019 #define	p(f, m) if (igmpstat.f || sflag <= 1) \
1020     printf(m, igmpstat.f, plural(igmpstat.f))
1021 #define	py(f, m) if (igmpstat.f || sflag <= 1) \
1022     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
1023 	p(igps_rcv_total, "\t%u message%s received\n");
1024 	p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
1025 	p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
1026 	py(igps_rcv_queries, "\t%u membership quer%s received\n");
1027 	py(igps_rcv_badqueries,
1028 	    "\t%u membership quer%s received with invalid field(s)\n");
1029 	p(igps_rcv_reports, "\t%u membership report%s received\n");
1030 	p(igps_rcv_badreports,
1031 	    "\t%u membership report%s received with invalid field(s)\n");
1032 	p(igps_rcv_ourreports,
1033 "\t%u membership report%s received for groups to which we belong\n");
1034         p(igps_snd_reports, "\t%u membership report%s sent\n");
1035 #undef p
1036 #undef py
1037 }
1038 
1039 /*
1040  * Dump PIM statistics structure.
1041  */
1042 void
1043 pim_stats(u_long off __unused, const char *name, int af1 __unused,
1044     int proto __unused)
1045 {
1046 	struct pimstat pimstat, zerostat;
1047 	size_t len = sizeof pimstat;
1048 
1049 	if (live) {
1050 		if (zflag)
1051 			memset(&zerostat, 0, len);
1052 		if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1053 		    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1054 			if (errno != ENOENT)
1055 				warn("sysctl: net.inet.pim.stats");
1056 			return;
1057 		}
1058 	} else {
1059 		if (off == 0)
1060 			return;
1061 		kread(off, &pimstat, len);
1062 	}
1063 
1064 	printf("%s:\n", name);
1065 
1066 #define	p(f, m) if (pimstat.f || sflag <= 1) \
1067     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1068 #define	py(f, m) if (pimstat.f || sflag <= 1) \
1069     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1070 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1071 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1072 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1073         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1074 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1075 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1076 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1077 	p(pims_rcv_registers_wrongiif,
1078 	    "\t%ju data register message%s received on wrong iif\n");
1079 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1080 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1081 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1082 #undef p
1083 #undef py
1084 }
1085 
1086 /*
1087  * Pretty print an Internet address (net address + port).
1088  */
1089 void
1090 inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1091 {
1092 	struct servent *sp = 0;
1093 	char line[80], *cp;
1094 	int width;
1095 
1096 	if (Wflag)
1097 	    sprintf(line, "%s.", inetname(in));
1098 	else
1099 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1100 	cp = index(line, '\0');
1101 	if (!num_port && port)
1102 		sp = getservbyport((int)port, proto);
1103 	if (sp || port == 0)
1104 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1105 	else
1106 		sprintf(cp, "%d ", ntohs((u_short)port));
1107 	width = (Aflag && !Wflag) ? 18 : 22;
1108 	if (Wflag)
1109 	    printf("%-*s ", width, line);
1110 	else
1111 	    printf("%-*.*s ", width, width, line);
1112 }
1113 
1114 /*
1115  * Construct an Internet address representation.
1116  * If numeric_addr has been supplied, give
1117  * numeric value, otherwise try for symbolic name.
1118  */
1119 char *
1120 inetname(struct in_addr *inp)
1121 {
1122 	char *cp;
1123 	static char line[MAXHOSTNAMELEN];
1124 	struct hostent *hp;
1125 	struct netent *np;
1126 
1127 	cp = 0;
1128 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1129 		int net = inet_netof(*inp);
1130 		int lna = inet_lnaof(*inp);
1131 
1132 		if (lna == INADDR_ANY) {
1133 			np = getnetbyaddr(net, AF_INET);
1134 			if (np)
1135 				cp = np->n_name;
1136 		}
1137 		if (cp == 0) {
1138 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1139 			if (hp) {
1140 				cp = hp->h_name;
1141 				trimdomain(cp, strlen(cp));
1142 			}
1143 		}
1144 	}
1145 	if (inp->s_addr == INADDR_ANY)
1146 		strcpy(line, "*");
1147 	else if (cp) {
1148 		strlcpy(line, cp, sizeof(line));
1149 	} else {
1150 		inp->s_addr = ntohl(inp->s_addr);
1151 #define	C(x)	((u_int)((x) & 0xff))
1152 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1153 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
1154 	}
1155 	return (line);
1156 }
1157