xref: /freebsd/usr.bin/netstat/inet.c (revision 4063583a62a4b8a4c602f29b11c85659d7941572)
19b50d902SRodney W. Grimes /*
205ddff6eSPeter Wemm  * Copyright (c) 1983, 1988, 1993, 1995
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
346cc6f122SPhilippe Charnier #if 0
359b50d902SRodney W. Grimes #ifndef lint
3605ddff6eSPeter Wemm static char sccsid[] = "@(#)inet.c	8.5 (Berkeley) 5/24/95";
379b50d902SRodney W. Grimes #endif /* not lint */
386cc6f122SPhilippe Charnier #endif
396cc6f122SPhilippe Charnier 
406cc6f122SPhilippe Charnier #include <sys/cdefs.h>
416cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <sys/param.h>
44d8d89152SDavid Greenman #include <sys/queue.h>
459b50d902SRodney W. Grimes #include <sys/socket.h>
469b50d902SRodney W. Grimes #include <sys/socketvar.h>
474e00c309SGarrett Wollman #include <sys/sysctl.h>
489b50d902SRodney W. Grimes #include <sys/protosw.h>
499b50d902SRodney W. Grimes 
509b50d902SRodney W. Grimes #include <net/route.h>
519b50d902SRodney W. Grimes #include <netinet/in.h>
529b50d902SRodney W. Grimes #include <netinet/in_systm.h>
539b50d902SRodney W. Grimes #include <netinet/ip.h>
54a9771948SGleb Smirnoff #include <netinet/ip_carp.h>
55cfa1ca9dSYoshinobu Inoue #ifdef INET6
56cfa1ca9dSYoshinobu Inoue #include <netinet/ip6.h>
57cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
589b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
599b50d902SRodney W. Grimes #include <netinet/ip_icmp.h>
609b50d902SRodney W. Grimes #include <netinet/icmp_var.h>
619b50d902SRodney W. Grimes #include <netinet/igmp_var.h>
629b50d902SRodney W. Grimes #include <netinet/ip_var.h>
63c7b9b5bbSJeffrey Hsu #include <netinet/pim_var.h>
649b50d902SRodney W. Grimes #include <netinet/tcp.h>
659b50d902SRodney W. Grimes #include <netinet/tcpip.h>
669b50d902SRodney W. Grimes #include <netinet/tcp_seq.h>
679b50d902SRodney W. Grimes #define TCPSTATES
689b50d902SRodney W. Grimes #include <netinet/tcp_fsm.h>
699b50d902SRodney W. Grimes #include <netinet/tcp_timer.h>
709b50d902SRodney W. Grimes #include <netinet/tcp_var.h>
719b50d902SRodney W. Grimes #include <netinet/tcp_debug.h>
729b50d902SRodney W. Grimes #include <netinet/udp.h>
739b50d902SRodney W. Grimes #include <netinet/udp_var.h>
749b50d902SRodney W. Grimes 
759b50d902SRodney W. Grimes #include <arpa/inet.h>
764f81ef50SGarrett Wollman #include <err.h>
774f81ef50SGarrett Wollman #include <errno.h>
78d121b556SBrian Somers #include <libutil.h>
799b50d902SRodney W. Grimes #include <netdb.h>
807b95a1ebSYaroslav Tykhiy #include <stdint.h>
819b50d902SRodney W. Grimes #include <stdio.h>
824f81ef50SGarrett Wollman #include <stdlib.h>
839b50d902SRodney W. Grimes #include <string.h>
849b50d902SRodney W. Grimes #include <unistd.h>
859b50d902SRodney W. Grimes #include "netstat.h"
869b50d902SRodney W. Grimes 
875e051718SAssar Westerlund char	*inetname (struct in_addr *);
88a01e3379SDavid Malone void	inetprint (struct in_addr *, int, const char *, int);
89cfa1ca9dSYoshinobu Inoue #ifdef INET6
90cfa1ca9dSYoshinobu Inoue static int udp_done, tcp_done;
91cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
929b50d902SRodney W. Grimes 
939b50d902SRodney W. Grimes /*
949b50d902SRodney W. Grimes  * Print a summary of connections related to an Internet
959b50d902SRodney W. Grimes  * protocol.  For TCP, also give state of connection.
969b50d902SRodney W. Grimes  * Listening processes (aflag) are suppressed unless the
979b50d902SRodney W. Grimes  * -a (all) flag is specified.
989b50d902SRodney W. Grimes  */
999b50d902SRodney W. Grimes void
1005e051718SAssar Westerlund protopr(u_long proto,		/* for sysctl version we pass proto # */
101a01e3379SDavid Malone 	const char *name, int af1)
1029b50d902SRodney W. Grimes {
1039b50d902SRodney W. Grimes 	int istcp;
1049b50d902SRodney W. Grimes 	static int first = 1;
1054f81ef50SGarrett Wollman 	char *buf;
106fb5d0fbdSRuslan Ermilov 	const char *mibvar, *vchar;
107a97a9922SJulian Elischer 	struct tcpcb *tp = NULL;
1084f81ef50SGarrett Wollman 	struct inpcb *inp;
1094f81ef50SGarrett Wollman 	struct xinpgen *xig, *oxig;
1104f81ef50SGarrett Wollman 	struct xsocket *so;
1114f81ef50SGarrett Wollman 	size_t len;
1129b50d902SRodney W. Grimes 
1134f81ef50SGarrett Wollman 	istcp = 0;
1144f81ef50SGarrett Wollman 	switch (proto) {
1154f81ef50SGarrett Wollman 	case IPPROTO_TCP:
116cfa1ca9dSYoshinobu Inoue #ifdef INET6
117cfa1ca9dSYoshinobu Inoue 		if (tcp_done != 0)
118cfa1ca9dSYoshinobu Inoue 			return;
119cfa1ca9dSYoshinobu Inoue 		else
120cfa1ca9dSYoshinobu Inoue 			tcp_done = 1;
121cfa1ca9dSYoshinobu Inoue #endif
1224f81ef50SGarrett Wollman 		istcp = 1;
1234f81ef50SGarrett Wollman 		mibvar = "net.inet.tcp.pcblist";
1244f81ef50SGarrett Wollman 		break;
1254f81ef50SGarrett Wollman 	case IPPROTO_UDP:
126cfa1ca9dSYoshinobu Inoue #ifdef INET6
127cfa1ca9dSYoshinobu Inoue 		if (udp_done != 0)
128cfa1ca9dSYoshinobu Inoue 			return;
129cfa1ca9dSYoshinobu Inoue 		else
130cfa1ca9dSYoshinobu Inoue 			udp_done = 1;
131cfa1ca9dSYoshinobu Inoue #endif
1324f81ef50SGarrett Wollman 		mibvar = "net.inet.udp.pcblist";
1334f81ef50SGarrett Wollman 		break;
1344f81ef50SGarrett Wollman 	case IPPROTO_DIVERT:
1354f81ef50SGarrett Wollman 		mibvar = "net.inet.divert.pcblist";
1364f81ef50SGarrett Wollman 		break;
1374f81ef50SGarrett Wollman 	default:
1384f81ef50SGarrett Wollman 		mibvar = "net.inet.raw.pcblist";
1394f81ef50SGarrett Wollman 		break;
1404f81ef50SGarrett Wollman 	}
1414f81ef50SGarrett Wollman 	len = 0;
1424f81ef50SGarrett Wollman 	if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
1434f81ef50SGarrett Wollman 		if (errno != ENOENT)
1444f81ef50SGarrett Wollman 			warn("sysctl: %s", mibvar);
1459b50d902SRodney W. Grimes 		return;
1463aa80b1dSDavid Greenman 	}
1474f81ef50SGarrett Wollman 	if ((buf = malloc(len)) == 0) {
1486cc6f122SPhilippe Charnier 		warnx("malloc %lu bytes", (u_long)len);
1494f81ef50SGarrett Wollman 		return;
1509b50d902SRodney W. Grimes 	}
1514f81ef50SGarrett Wollman 	if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
1524f81ef50SGarrett Wollman 		warn("sysctl: %s", mibvar);
1534f81ef50SGarrett Wollman 		free(buf);
1544f81ef50SGarrett Wollman 		return;
1554f81ef50SGarrett Wollman 	}
1564f81ef50SGarrett Wollman 
1574f81ef50SGarrett Wollman 	oxig = xig = (struct xinpgen *)buf;
1584f81ef50SGarrett Wollman 	for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
1594f81ef50SGarrett Wollman 	     xig->xig_len > sizeof(struct xinpgen);
1604f81ef50SGarrett Wollman 	     xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
1619b50d902SRodney W. Grimes 		if (istcp) {
1624f81ef50SGarrett Wollman 			tp = &((struct xtcpcb *)xig)->xt_tp;
1634f81ef50SGarrett Wollman 			inp = &((struct xtcpcb *)xig)->xt_inp;
1644f81ef50SGarrett Wollman 			so = &((struct xtcpcb *)xig)->xt_socket;
1654f81ef50SGarrett Wollman 		} else {
1664f81ef50SGarrett Wollman 			inp = &((struct xinpcb *)xig)->xi_inp;
1674f81ef50SGarrett Wollman 			so = &((struct xinpcb *)xig)->xi_socket;
1689b50d902SRodney W. Grimes 		}
1694f81ef50SGarrett Wollman 
1704f81ef50SGarrett Wollman 		/* Ignore sockets for protocols other than the desired one. */
171f964d60dSAssar Westerlund 		if (so->xso_protocol != (int)proto)
1724f81ef50SGarrett Wollman 			continue;
1734f81ef50SGarrett Wollman 
1744f81ef50SGarrett Wollman 		/* Ignore PCBs which were freed during copyout. */
1754f81ef50SGarrett Wollman 		if (inp->inp_gencnt > oxig->xig_gen)
1764f81ef50SGarrett Wollman 			continue;
1774f81ef50SGarrett Wollman 
178a01e3379SDavid Malone 		if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
179cfa1ca9dSYoshinobu Inoue #ifdef INET6
180a01e3379SDavid Malone 		    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
181cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
182a01e3379SDavid Malone 		    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
183cfa1ca9dSYoshinobu Inoue #ifdef INET6
184cfa1ca9dSYoshinobu Inoue 					    && (inp->inp_vflag &
185cfa1ca9dSYoshinobu Inoue 						INP_IPV6) == 0
186cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
187cfa1ca9dSYoshinobu Inoue 			))
188cfa1ca9dSYoshinobu Inoue 		    )
189cfa1ca9dSYoshinobu Inoue 			continue;
190cfa1ca9dSYoshinobu Inoue 		if (!aflag &&
191cfa1ca9dSYoshinobu Inoue 		    (
1922b286cedSBruce M Simpson 		     (istcp && tp->t_state == TCPS_LISTEN)
1932b286cedSBruce M Simpson 		     || (af1 == AF_INET &&
194cfa1ca9dSYoshinobu Inoue 		      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
195cfa1ca9dSYoshinobu Inoue #ifdef INET6
196a01e3379SDavid Malone 		     || (af1 == AF_INET6 &&
197cfa1ca9dSYoshinobu Inoue 			 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
198cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
199a01e3379SDavid Malone 		     || (af1 == AF_UNSPEC &&
200cfa1ca9dSYoshinobu Inoue 			 (((inp->inp_vflag & INP_IPV4) != 0 &&
201cfa1ca9dSYoshinobu Inoue 			   inet_lnaof(inp->inp_laddr) == INADDR_ANY)
202cfa1ca9dSYoshinobu Inoue #ifdef INET6
203cfa1ca9dSYoshinobu Inoue 			  || ((inp->inp_vflag & INP_IPV6) != 0 &&
204cfa1ca9dSYoshinobu Inoue 			      IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
205cfa1ca9dSYoshinobu Inoue #endif
206cfa1ca9dSYoshinobu Inoue 			  ))
207cfa1ca9dSYoshinobu Inoue 		     ))
2084f81ef50SGarrett Wollman 			continue;
2094f81ef50SGarrett Wollman 
2109b50d902SRodney W. Grimes 		if (first) {
211ac55add0SGuido van Rooij 			if (!Lflag) {
2129b50d902SRodney W. Grimes 				printf("Active Internet connections");
2139b50d902SRodney W. Grimes 				if (aflag)
2149b50d902SRodney W. Grimes 					printf(" (including servers)");
215ac55add0SGuido van Rooij 			} else
216ac55add0SGuido van Rooij 				printf(
217ac55add0SGuido van Rooij 	"Current listen queue sizes (qlen/incqlen/maxqlen)");
2189b50d902SRodney W. Grimes 			putchar('\n');
2199b50d902SRodney W. Grimes 			if (Aflag)
2204f81ef50SGarrett Wollman 				printf("%-8.8s ", "Socket");
221ac55add0SGuido van Rooij 			if (Lflag)
222fb5d0fbdSRuslan Ermilov 				printf("%-5.5s %-14.14s %-22.22s\n",
223fb5d0fbdSRuslan Ermilov 					"Proto", "Listen", "Local Address");
224ac55add0SGuido van Rooij 			else
225080b7f49SDag-Erling Smørgrav 				printf((Aflag && !Wflag) ?
226cfa1ca9dSYoshinobu Inoue 		"%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s %s\n" :
227cfa1ca9dSYoshinobu Inoue 		"%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s %s\n",
2289b50d902SRodney W. Grimes 					"Proto", "Recv-Q", "Send-Q",
229ac55add0SGuido van Rooij 					"Local Address", "Foreign Address",
230ac55add0SGuido van Rooij 					"(state)");
2319b50d902SRodney W. Grimes 			first = 0;
2329b50d902SRodney W. Grimes 		}
233fb5d0fbdSRuslan Ermilov 		if (Lflag && so->so_qlimit == 0)
234fb5d0fbdSRuslan Ermilov 			continue;
235e4ec3989SDag-Erling Smørgrav 		if (Aflag) {
236e4ec3989SDag-Erling Smørgrav 			if (istcp)
237e4ec3989SDag-Erling Smørgrav 				printf("%8lx ", (u_long)inp->inp_ppcb);
238e4ec3989SDag-Erling Smørgrav 			else
2394f81ef50SGarrett Wollman 				printf("%8lx ", (u_long)so->so_pcb);
240e4ec3989SDag-Erling Smørgrav 		}
241cfa1ca9dSYoshinobu Inoue #ifdef INET6
242fc60ab7bSYoshinobu Inoue 		if ((inp->inp_vflag & INP_IPV6) != 0)
243fc60ab7bSYoshinobu Inoue 			vchar = ((inp->inp_vflag & INP_IPV4) != 0)
244fc60ab7bSYoshinobu Inoue 				? "46" : "6 ";
245fc60ab7bSYoshinobu Inoue 		else
246cfa1ca9dSYoshinobu Inoue #endif
247fc60ab7bSYoshinobu Inoue 		vchar = ((inp->inp_vflag & INP_IPV4) != 0)
248fc60ab7bSYoshinobu Inoue 				? "4 " : "  ";
249fb5d0fbdSRuslan Ermilov 		printf("%-3.3s%-2.2s ", name, vchar);
250fb5d0fbdSRuslan Ermilov 		if (Lflag) {
251a01e3379SDavid Malone 			char buf1[15];
252fc60ab7bSYoshinobu Inoue 
253a01e3379SDavid Malone 			snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
254fb5d0fbdSRuslan Ermilov 				 so->so_incqlen, so->so_qlimit);
255a01e3379SDavid Malone 			printf("%-14.14s ", buf1);
256fb5d0fbdSRuslan Ermilov 		} else {
257f824b518SJohn Polstra 			printf("%6u %6u  ",
258cfa1ca9dSYoshinobu Inoue 			       so->so_rcv.sb_cc,
2594f81ef50SGarrett Wollman 			       so->so_snd.sb_cc);
260fc60ab7bSYoshinobu Inoue 		}
26165ea0024SAssar Westerlund 		if (numeric_port) {
262cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
2634f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
2648d612dd2SPoul-Henning Kamp 					  name, 1);
265ac55add0SGuido van Rooij 				if (!Lflag)
266ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
267ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name, 1);
268cfa1ca9dSYoshinobu Inoue 			}
269cfa1ca9dSYoshinobu Inoue #ifdef INET6
270cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
271cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
272cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 1);
273ac55add0SGuido van Rooij 				if (!Lflag)
274cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
275cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name, 1);
276cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
277cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
2784f81ef50SGarrett Wollman 		} else if (inp->inp_flags & INP_ANONPORT) {
279cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
2804f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
2818d612dd2SPoul-Henning Kamp 					  name, 1);
282ac55add0SGuido van Rooij 				if (!Lflag)
283ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
284ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name, 0);
285cfa1ca9dSYoshinobu Inoue 			}
286cfa1ca9dSYoshinobu Inoue #ifdef INET6
287cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
288cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
289cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 1);
290ac55add0SGuido van Rooij 				if (!Lflag)
291cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
292cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name, 0);
293cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
294cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
2958d612dd2SPoul-Henning Kamp 		} else {
296cfa1ca9dSYoshinobu Inoue 			if (inp->inp_vflag & INP_IPV4) {
2974f81ef50SGarrett Wollman 				inetprint(&inp->inp_laddr, (int)inp->inp_lport,
2988d612dd2SPoul-Henning Kamp 					  name, 0);
299ac55add0SGuido van Rooij 				if (!Lflag)
300ac55add0SGuido van Rooij 					inetprint(&inp->inp_faddr,
301ac55add0SGuido van Rooij 						  (int)inp->inp_fport, name,
302ac55add0SGuido van Rooij 						  inp->inp_lport !=
303ac55add0SGuido van Rooij 							inp->inp_fport);
304cfa1ca9dSYoshinobu Inoue 			}
305cfa1ca9dSYoshinobu Inoue #ifdef INET6
306cfa1ca9dSYoshinobu Inoue 			else if (inp->inp_vflag & INP_IPV6) {
307cfa1ca9dSYoshinobu Inoue 				inet6print(&inp->in6p_laddr,
308cfa1ca9dSYoshinobu Inoue 					   (int)inp->inp_lport, name, 0);
309ac55add0SGuido van Rooij 				if (!Lflag)
310cfa1ca9dSYoshinobu Inoue 					inet6print(&inp->in6p_faddr,
311cfa1ca9dSYoshinobu Inoue 						   (int)inp->inp_fport, name,
312ac55add0SGuido van Rooij 						   inp->inp_lport !=
313ac55add0SGuido van Rooij 							inp->inp_fport);
314cfa1ca9dSYoshinobu Inoue 			} /* else nothing printed now */
315cfa1ca9dSYoshinobu Inoue #endif /* INET6 */
3168d612dd2SPoul-Henning Kamp 		}
317ac55add0SGuido van Rooij 		if (istcp && !Lflag) {
3184f81ef50SGarrett Wollman 			if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
3194f81ef50SGarrett Wollman 				printf("%d", tp->t_state);
3209a94a597SGarrett Wollman                       else {
3214f81ef50SGarrett Wollman 				printf("%s", tcpstates[tp->t_state]);
322513822ddSAdam David #if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
3239a94a597SGarrett Wollman                               /* Show T/TCP `hidden state' */
3244f81ef50SGarrett Wollman                               if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
3259a94a597SGarrett Wollman                                       putchar('*');
326513822ddSAdam David #endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
3279a94a597SGarrett Wollman                       }
3289b50d902SRodney W. Grimes 		}
3299b50d902SRodney W. Grimes 		putchar('\n');
3309b50d902SRodney W. Grimes 	}
3314f81ef50SGarrett Wollman 	if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
3324f81ef50SGarrett Wollman 		if (oxig->xig_count > xig->xig_count) {
3334f81ef50SGarrett Wollman 			printf("Some %s sockets may have been deleted.\n",
3344f81ef50SGarrett Wollman 			       name);
3354f81ef50SGarrett Wollman 		} else if (oxig->xig_count < xig->xig_count) {
3364f81ef50SGarrett Wollman 			printf("Some %s sockets may have been created.\n",
3374f81ef50SGarrett Wollman 			       name);
3384f81ef50SGarrett Wollman 		} else {
339eb6fe0d9SMax Khon 			printf("Some %s sockets may have been created or deleted.\n",
3404f81ef50SGarrett Wollman 			       name);
3414f81ef50SGarrett Wollman 		}
3424f81ef50SGarrett Wollman 	}
3434f81ef50SGarrett Wollman 	free(buf);
3449b50d902SRodney W. Grimes }
3459b50d902SRodney W. Grimes 
3469b50d902SRodney W. Grimes /*
3479b50d902SRodney W. Grimes  * Dump TCP statistics structure.
3489b50d902SRodney W. Grimes  */
3499b50d902SRodney W. Grimes void
350a01e3379SDavid Malone tcp_stats(u_long off __unused, const char *name, int af1 __unused)
3519b50d902SRodney W. Grimes {
352c73d99b5SRuslan Ermilov 	struct tcpstat tcpstat, zerostat;
3534f81ef50SGarrett Wollman 	size_t len = sizeof tcpstat;
3549b50d902SRodney W. Grimes 
355c73d99b5SRuslan Ermilov 	if (zflag)
356c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
357c73d99b5SRuslan Ermilov 	if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
358c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
3594f81ef50SGarrett Wollman 		warn("sysctl: net.inet.tcp.stats");
3609b50d902SRodney W. Grimes 		return;
3614f81ef50SGarrett Wollman 	}
3624f81ef50SGarrett Wollman 
363cfa1ca9dSYoshinobu Inoue #ifdef INET6
364cfa1ca9dSYoshinobu Inoue 	if (tcp_done != 0)
365cfa1ca9dSYoshinobu Inoue 		return;
366cfa1ca9dSYoshinobu Inoue 	else
367cfa1ca9dSYoshinobu Inoue 		tcp_done = 1;
368cfa1ca9dSYoshinobu Inoue #endif
369cfa1ca9dSYoshinobu Inoue 
3709b50d902SRodney W. Grimes 	printf ("%s:\n", name);
3719b50d902SRodney W. Grimes 
3729b50d902SRodney W. Grimes #define	p(f, m) if (tcpstat.f || sflag <= 1) \
3739b50d902SRodney W. Grimes     printf(m, tcpstat.f, plural(tcpstat.f))
37422694ebaSBruce Evans #define	p1a(f, m) if (tcpstat.f || sflag <= 1) \
37522694ebaSBruce Evans     printf(m, tcpstat.f)
3769b50d902SRodney W. Grimes #define	p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
3779b50d902SRodney W. Grimes     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
37822694ebaSBruce Evans #define	p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
37922694ebaSBruce Evans     printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
3809b50d902SRodney W. Grimes #define	p3(f, m) if (tcpstat.f || sflag <= 1) \
3819b50d902SRodney W. Grimes     printf(m, tcpstat.f, plurales(tcpstat.f))
3829b50d902SRodney W. Grimes 
383e1fb4daaSPaul Traina 	p(tcps_sndtotal, "\t%lu packet%s sent\n");
3849b50d902SRodney W. Grimes 	p2(tcps_sndpack,tcps_sndbyte,
385e1fb4daaSPaul Traina 		"\t\t%lu data packet%s (%lu byte%s)\n");
3869b50d902SRodney W. Grimes 	p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
387e1fb4daaSPaul Traina 		"\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
388d65bf08aSMatthew Dillon 	p(tcps_sndrexmitbad,
389d65bf08aSMatthew Dillon 		"\t\t%lu data packet%s unnecessarily retransmitted\n");
390e1fb4daaSPaul Traina 	p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
39122694ebaSBruce Evans 	p2a(tcps_sndacks, tcps_delack,
392e1fb4daaSPaul Traina 		"\t\t%lu ack-only packet%s (%lu delayed)\n");
393e1fb4daaSPaul Traina 	p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
394e1fb4daaSPaul Traina 	p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
395e1fb4daaSPaul Traina 	p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
396e1fb4daaSPaul Traina 	p(tcps_sndctrl, "\t\t%lu control packet%s\n");
397e1fb4daaSPaul Traina 	p(tcps_rcvtotal, "\t%lu packet%s received\n");
398e1fb4daaSPaul Traina 	p2(tcps_rcvackpack, tcps_rcvackbyte, "\t\t%lu ack%s (for %lu byte%s)\n");
399e1fb4daaSPaul Traina 	p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
400e1fb4daaSPaul Traina 	p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
4019b50d902SRodney W. Grimes 	p2(tcps_rcvpack, tcps_rcvbyte,
402e1fb4daaSPaul Traina 		"\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
4039b50d902SRodney W. Grimes 	p2(tcps_rcvduppack, tcps_rcvdupbyte,
404e1fb4daaSPaul Traina 		"\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
405e1fb4daaSPaul Traina 	p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
4069b50d902SRodney W. Grimes 	p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
407e1fb4daaSPaul Traina 		"\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
4089b50d902SRodney W. Grimes 	p2(tcps_rcvoopack, tcps_rcvoobyte,
409e1fb4daaSPaul Traina 		"\t\t%lu out-of-order packet%s (%lu byte%s)\n");
4109b50d902SRodney W. Grimes 	p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
411e1fb4daaSPaul Traina 		"\t\t%lu packet%s (%lu byte%s) of data after window\n");
412e1fb4daaSPaul Traina 	p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
413e1fb4daaSPaul Traina 	p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
414e1fb4daaSPaul Traina 	p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
415e1fb4daaSPaul Traina 	p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
416e1fb4daaSPaul Traina 	p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
41722694ebaSBruce Evans 	p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
41860e15662SWojciech A. Koszek 	p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
419e1fb4daaSPaul Traina 	p(tcps_connattempt, "\t%lu connection request%s\n");
420e1fb4daaSPaul Traina 	p(tcps_accepts, "\t%lu connection accept%s\n");
421e1fb4daaSPaul Traina 	p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
422e1fb4daaSPaul Traina 	p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
42301d3e1c0SRuslan Ermilov 	p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
424e1fb4daaSPaul Traina 	p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
4259b50d902SRodney W. Grimes 	p2(tcps_closed, tcps_drops,
426e1fb4daaSPaul Traina 		"\t%lu connection%s closed (including %lu drop%s)\n");
427e1fb4daaSPaul Traina 	p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
428861b1828SGarrett Wollman 	p(tcps_cachedrttvar,
429e1fb4daaSPaul Traina 	  "\t\t%lu connection%s updated cached RTT variance on close\n");
430861b1828SGarrett Wollman 	p(tcps_cachedssthresh,
431e1fb4daaSPaul Traina 	  "\t\t%lu connection%s updated cached ssthresh on close\n");
432e1fb4daaSPaul Traina 	p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
4339b50d902SRodney W. Grimes 	p2(tcps_rttupdated, tcps_segstimed,
434e1fb4daaSPaul Traina 		"\t%lu segment%s updated rtt (of %lu attempt%s)\n");
435e1fb4daaSPaul Traina 	p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
436e1fb4daaSPaul Traina 	p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
437e1fb4daaSPaul Traina 	p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
438e1fb4daaSPaul Traina 	p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
4397c72af87SMohan Srinivasan 	p(tcps_finwait2_drops, "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
440e1fb4daaSPaul Traina 	p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
441e1fb4daaSPaul Traina 	p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
442e1fb4daaSPaul Traina 	p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
443e1fb4daaSPaul Traina 	p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
444e1fb4daaSPaul Traina 	p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
44560a31b3aSJonathan Lemon 
446a01e3379SDavid Malone 	p(tcps_sc_added, "\t%lu syncache entrie%s added\n");
447a01e3379SDavid Malone 	p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
448a01e3379SDavid Malone 	p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
449a01e3379SDavid Malone 	p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
450a01e3379SDavid Malone 	p1a(tcps_sc_completed, "\t\t%lu completed\n");
451a01e3379SDavid Malone 	p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
452a01e3379SDavid Malone 	p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
453a01e3379SDavid Malone 	p1a(tcps_sc_reset, "\t\t%lu reset\n");
454a01e3379SDavid Malone 	p1a(tcps_sc_stale, "\t\t%lu stale\n");
455a01e3379SDavid Malone 	p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
456a01e3379SDavid Malone 	p1a(tcps_sc_badack, "\t\t%lu badack\n");
457a01e3379SDavid Malone 	p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
458a01e3379SDavid Malone 	p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
459a01e3379SDavid Malone 	p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
460a01e3379SDavid Malone 	p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
461b6101dafSPaul Saab 
462b6101dafSPaul Saab 	p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
463b6101dafSPaul Saab 	p(tcps_sack_rexmits,
464b6101dafSPaul Saab 		"\t%lu segment rexmit%s in SACK recovery episodes\n");
465b6101dafSPaul Saab 	p(tcps_sack_rexmit_bytes,
466b6101dafSPaul Saab 		"\t%lu byte rexmit%s in SACK recovery episodes\n");
467b6101dafSPaul Saab 	p(tcps_sack_rcv_blocks,
468b6101dafSPaul Saab 		"\t%lu SACK option%s (SACK blocks) received\n");
469b6101dafSPaul Saab 	p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
470e891d82bSPaul Saab 	p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
471b6101dafSPaul Saab 
4729b50d902SRodney W. Grimes #undef p
47322694ebaSBruce Evans #undef p1a
4749b50d902SRodney W. Grimes #undef p2
47522694ebaSBruce Evans #undef p2a
4769b50d902SRodney W. Grimes #undef p3
4779b50d902SRodney W. Grimes }
4789b50d902SRodney W. Grimes 
4799b50d902SRodney W. Grimes /*
4809b50d902SRodney W. Grimes  * Dump UDP statistics structure.
4819b50d902SRodney W. Grimes  */
4829b50d902SRodney W. Grimes void
483a01e3379SDavid Malone udp_stats(u_long off __unused, const char *name, int af1 __unused)
4849b50d902SRodney W. Grimes {
485c73d99b5SRuslan Ermilov 	struct udpstat udpstat, zerostat;
4864f81ef50SGarrett Wollman 	size_t len = sizeof udpstat;
4879b50d902SRodney W. Grimes 	u_long delivered;
4889b50d902SRodney W. Grimes 
489c73d99b5SRuslan Ermilov 	if (zflag)
490c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
491c73d99b5SRuslan Ermilov 	if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
492c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
4934f81ef50SGarrett Wollman 		warn("sysctl: net.inet.udp.stats");
4949b50d902SRodney W. Grimes 		return;
4954f81ef50SGarrett Wollman 	}
4964f81ef50SGarrett Wollman 
497cfa1ca9dSYoshinobu Inoue #ifdef INET6
498cfa1ca9dSYoshinobu Inoue 	if (udp_done != 0)
499cfa1ca9dSYoshinobu Inoue 		return;
500cfa1ca9dSYoshinobu Inoue 	else
501cfa1ca9dSYoshinobu Inoue 		udp_done = 1;
502cfa1ca9dSYoshinobu Inoue #endif
503cfa1ca9dSYoshinobu Inoue 
5049b50d902SRodney W. Grimes 	printf("%s:\n", name);
5059b50d902SRodney W. Grimes #define	p(f, m) if (udpstat.f || sflag <= 1) \
5069b50d902SRodney W. Grimes     printf(m, udpstat.f, plural(udpstat.f))
50722694ebaSBruce Evans #define	p1a(f, m) if (udpstat.f || sflag <= 1) \
50822694ebaSBruce Evans     printf(m, udpstat.f)
5097d56c0eeSAlexander Langer 	p(udps_ipackets, "\t%lu datagram%s received\n");
51022694ebaSBruce Evans 	p1a(udps_hdrops, "\t%lu with incomplete header\n");
51122694ebaSBruce Evans 	p1a(udps_badlen, "\t%lu with bad data length field\n");
51222694ebaSBruce Evans 	p1a(udps_badsum, "\t%lu with bad checksum\n");
513fb9aaba0SRuslan Ermilov 	p1a(udps_nosum, "\t%lu with no checksum\n");
51422694ebaSBruce Evans 	p1a(udps_noport, "\t%lu dropped due to no socket\n");
51522694ebaSBruce Evans 	p(udps_noportbcast,
51622694ebaSBruce Evans 	    "\t%lu broadcast/multicast datagram%s dropped due to no socket\n");
51722694ebaSBruce Evans 	p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
51822694ebaSBruce Evans 	p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
5199b50d902SRodney W. Grimes 	delivered = udpstat.udps_ipackets -
5209b50d902SRodney W. Grimes 		    udpstat.udps_hdrops -
5219b50d902SRodney W. Grimes 		    udpstat.udps_badlen -
5229b50d902SRodney W. Grimes 		    udpstat.udps_badsum -
5239b50d902SRodney W. Grimes 		    udpstat.udps_noport -
5249b50d902SRodney W. Grimes 		    udpstat.udps_noportbcast -
5259b50d902SRodney W. Grimes 		    udpstat.udps_fullsock;
5269b50d902SRodney W. Grimes 	if (delivered || sflag <= 1)
5277d56c0eeSAlexander Langer 		printf("\t%lu delivered\n", delivered);
5287d56c0eeSAlexander Langer 	p(udps_opackets, "\t%lu datagram%s output\n");
5299b50d902SRodney W. Grimes #undef p
53022694ebaSBruce Evans #undef p1a
5319b50d902SRodney W. Grimes }
5329b50d902SRodney W. Grimes 
5339b50d902SRodney W. Grimes /*
534a9771948SGleb Smirnoff  * Dump CARP statistics structure.
535a9771948SGleb Smirnoff  */
536a9771948SGleb Smirnoff void
537096146f8SYaroslav Tykhiy carp_stats(u_long off __unused, const char *name, int af1 __unused)
538a9771948SGleb Smirnoff {
539a9771948SGleb Smirnoff 	struct carpstats carpstat, zerostat;
540a9771948SGleb Smirnoff 	size_t len = sizeof(struct carpstats);
541a9771948SGleb Smirnoff 
542a9771948SGleb Smirnoff 	if (zflag)
543a9771948SGleb Smirnoff 		memset(&zerostat, 0, len);
544a9771948SGleb Smirnoff 	if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
545a9771948SGleb Smirnoff 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
54645125e14SRuslan Ermilov 		if (errno != ENOENT)
547a9771948SGleb Smirnoff 			warn("sysctl: net.inet.carp.stats");
548a9771948SGleb Smirnoff 		return;
549a9771948SGleb Smirnoff 	}
550a9771948SGleb Smirnoff 
551a9771948SGleb Smirnoff 	printf("%s:\n", name);
552a9771948SGleb Smirnoff 
553a9771948SGleb Smirnoff #define p(f, m) if (carpstat.f || sflag <= 1) \
5547b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
555a9771948SGleb Smirnoff #define p2(f, m) if (carpstat.f || sflag <= 1) \
5567b95a1ebSYaroslav Tykhiy 	printf(m, (uintmax_t)carpstat.f)
557a9771948SGleb Smirnoff 
5587b95a1ebSYaroslav Tykhiy 	p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
5597b95a1ebSYaroslav Tykhiy 	p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
5607b95a1ebSYaroslav Tykhiy 	p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
5617b95a1ebSYaroslav Tykhiy 	p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
5627b95a1ebSYaroslav Tykhiy 	p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
5637b95a1ebSYaroslav Tykhiy 	p(carps_badver,	"\t\t%ju discarded packet%s with a bad version\n");
5647b95a1ebSYaroslav Tykhiy 	p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
5657b95a1ebSYaroslav Tykhiy 	p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
5667b95a1ebSYaroslav Tykhiy 	p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
5677b95a1ebSYaroslav Tykhiy 	p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
5687b95a1ebSYaroslav Tykhiy 	p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
5697b95a1ebSYaroslav Tykhiy 	p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
5707b95a1ebSYaroslav Tykhiy 	p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
571a9771948SGleb Smirnoff #if notyet
572a9771948SGleb Smirnoff 	p(carps_ostates, "\t\t%s state update%s sent\n");
573a9771948SGleb Smirnoff #endif
574a9771948SGleb Smirnoff #undef p
575a9771948SGleb Smirnoff #undef p2
576a9771948SGleb Smirnoff }
577a9771948SGleb Smirnoff 
578a9771948SGleb Smirnoff /*
5799b50d902SRodney W. Grimes  * Dump IP statistics structure.
5809b50d902SRodney W. Grimes  */
5819b50d902SRodney W. Grimes void
582a01e3379SDavid Malone ip_stats(u_long off __unused, const char *name, int af1 __unused)
5839b50d902SRodney W. Grimes {
584c73d99b5SRuslan Ermilov 	struct ipstat ipstat, zerostat;
5854f81ef50SGarrett Wollman 	size_t len = sizeof ipstat;
5869b50d902SRodney W. Grimes 
587c73d99b5SRuslan Ermilov 	if (zflag)
588c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
589c73d99b5SRuslan Ermilov 	if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
590c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
5914f81ef50SGarrett Wollman 		warn("sysctl: net.inet.ip.stats");
5929b50d902SRodney W. Grimes 		return;
5934f81ef50SGarrett Wollman 	}
5944f81ef50SGarrett Wollman 
5959b50d902SRodney W. Grimes 	printf("%s:\n", name);
5969b50d902SRodney W. Grimes 
5979b50d902SRodney W. Grimes #define	p(f, m) if (ipstat.f || sflag <= 1) \
5989b50d902SRodney W. Grimes     printf(m, ipstat.f, plural(ipstat.f))
59922694ebaSBruce Evans #define	p1a(f, m) if (ipstat.f || sflag <= 1) \
60022694ebaSBruce Evans     printf(m, ipstat.f)
6019b50d902SRodney W. Grimes 
6027d56c0eeSAlexander Langer 	p(ips_total, "\t%lu total packet%s received\n");
6037d56c0eeSAlexander Langer 	p(ips_badsum, "\t%lu bad header checksum%s\n");
60422694ebaSBruce Evans 	p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
60522694ebaSBruce Evans 	p1a(ips_tooshort, "\t%lu with data size < data length\n");
606cfa1ca9dSYoshinobu Inoue 	p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
60722694ebaSBruce Evans 	p1a(ips_badhlen, "\t%lu with header length < data size\n");
60822694ebaSBruce Evans 	p1a(ips_badlen, "\t%lu with data length < header length\n");
60922694ebaSBruce Evans 	p1a(ips_badoptions, "\t%lu with bad options\n");
61022694ebaSBruce Evans 	p1a(ips_badvers, "\t%lu with incorrect version number\n");
6117d56c0eeSAlexander Langer 	p(ips_fragments, "\t%lu fragment%s received\n");
6127d56c0eeSAlexander Langer 	p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
6137d56c0eeSAlexander Langer 	p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
6147d56c0eeSAlexander Langer 	p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
6157d56c0eeSAlexander Langer 	p(ips_delivered, "\t%lu packet%s for this host\n");
6167d56c0eeSAlexander Langer 	p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
617958d6f7fSPierre Beyssac 	p(ips_forward, "\t%lu packet%s forwarded");
618958d6f7fSPierre Beyssac 	p(ips_fastforward, " (%lu packet%s fast forwarded)");
619958d6f7fSPierre Beyssac 	if (ipstat.ips_forward || sflag <= 1)
620958d6f7fSPierre Beyssac 		putchar('\n');
6217d56c0eeSAlexander Langer 	p(ips_cantforward, "\t%lu packet%s not forwardable\n");
622a5969f5fSGarrett Wollman 	p(ips_notmember,
623a5969f5fSGarrett Wollman 	  "\t%lu packet%s received for unknown multicast group\n");
6247d56c0eeSAlexander Langer 	p(ips_redirectsent, "\t%lu redirect%s sent\n");
6257d56c0eeSAlexander Langer 	p(ips_localout, "\t%lu packet%s sent from this host\n");
6267d56c0eeSAlexander Langer 	p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
627a5969f5fSGarrett Wollman 	p(ips_odropped,
628a5969f5fSGarrett Wollman 	  "\t%lu output packet%s dropped due to no bufs, etc.\n");
6297d56c0eeSAlexander Langer 	p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
6307d56c0eeSAlexander Langer 	p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
6317d56c0eeSAlexander Langer 	p(ips_ofragments, "\t%lu fragment%s created\n");
6327d56c0eeSAlexander Langer 	p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
633cfa1ca9dSYoshinobu Inoue 	p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
63433841545SHajimu UMEMOTO 	p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
6359b50d902SRodney W. Grimes #undef p
63622694ebaSBruce Evans #undef p1a
6379b50d902SRodney W. Grimes }
6389b50d902SRodney W. Grimes 
6394063583aSMaxim Konovalov static	const char *icmpnames[ICMP_MAXTYPE + 1] = {
6404063583aSMaxim Konovalov 	"echo reply",			/* RFC 792 */
6419b50d902SRodney W. Grimes 	"#1",
6429b50d902SRodney W. Grimes 	"#2",
6434063583aSMaxim Konovalov 	"destination unreachable",	/* RFC 792 */
6444063583aSMaxim Konovalov 	"source quench",		/* RFC 792 */
6454063583aSMaxim Konovalov 	"routing redirect",		/* RFC 792 */
6469b50d902SRodney W. Grimes 	"#6",
6479b50d902SRodney W. Grimes 	"#7",
6484063583aSMaxim Konovalov 	"echo",				/* RFC 792 */
6494063583aSMaxim Konovalov 	"router advertisement",		/* RFC 1256 */
6504063583aSMaxim Konovalov 	"router solicitation",		/* RFC 1256 */
6514063583aSMaxim Konovalov 	"time exceeded",		/* RFC 792 */
6524063583aSMaxim Konovalov 	"parameter problem",		/* RFC 792 */
6534063583aSMaxim Konovalov 	"time stamp",			/* RFC 792 */
6544063583aSMaxim Konovalov 	"time stamp reply",		/* RFC 792 */
6554063583aSMaxim Konovalov 	"information request",		/* RFC 792 */
6564063583aSMaxim Konovalov 	"information request reply",	/* RFC 792 */
6574063583aSMaxim Konovalov 	"address mask request",		/* RFC 950 */
6584063583aSMaxim Konovalov 	"address mask reply",		/* RFC 950 */
6594063583aSMaxim Konovalov 	"#19",
6604063583aSMaxim Konovalov 	"#20",
6614063583aSMaxim Konovalov 	"#21",
6624063583aSMaxim Konovalov 	"#22",
6634063583aSMaxim Konovalov 	"#23",
6644063583aSMaxim Konovalov 	"#24",
6654063583aSMaxim Konovalov 	"#25",
6664063583aSMaxim Konovalov 	"#26",
6674063583aSMaxim Konovalov 	"#27",
6684063583aSMaxim Konovalov 	"#28",
6694063583aSMaxim Konovalov 	"#29",
6704063583aSMaxim Konovalov 	"icmp traceroute",		/* RFC 1393 */
6714063583aSMaxim Konovalov 	"datagram conversion error",	/* RFC 1475 */
6724063583aSMaxim Konovalov 	"mobile host redirect",
6734063583aSMaxim Konovalov 	"IPv6 where-are-you",
6744063583aSMaxim Konovalov 	"IPv6 i-am-here",
6754063583aSMaxim Konovalov 	"mobile registration req",
6764063583aSMaxim Konovalov 	"mobile registration reply",
6774063583aSMaxim Konovalov 	"domain name request",		/* RFC 1788 */
6784063583aSMaxim Konovalov 	"domain name reply",		/* RFC 1788 */
6794063583aSMaxim Konovalov 	"icmp SKIP",
6804063583aSMaxim Konovalov 	"icmp photuris",		/* RFC 2521 */
6819b50d902SRodney W. Grimes };
6829b50d902SRodney W. Grimes 
6839b50d902SRodney W. Grimes /*
6849b50d902SRodney W. Grimes  * Dump ICMP statistics.
6859b50d902SRodney W. Grimes  */
6869b50d902SRodney W. Grimes void
687a01e3379SDavid Malone icmp_stats(u_long off __unused, const char *name, int af1 __unused)
6889b50d902SRodney W. Grimes {
689c73d99b5SRuslan Ermilov 	struct icmpstat icmpstat, zerostat;
6904e00c309SGarrett Wollman 	int i, first;
6914e00c309SGarrett Wollman 	int mib[4];		/* CTL_NET + PF_INET + IPPROTO_ICMP + req */
6924e00c309SGarrett Wollman 	size_t len;
6939b50d902SRodney W. Grimes 
6944e00c309SGarrett Wollman 	mib[0] = CTL_NET;
6954e00c309SGarrett Wollman 	mib[1] = PF_INET;
6964e00c309SGarrett Wollman 	mib[2] = IPPROTO_ICMP;
6974e00c309SGarrett Wollman 	mib[3] = ICMPCTL_STATS;
6984e00c309SGarrett Wollman 
6994e00c309SGarrett Wollman 	len = sizeof icmpstat;
700c73d99b5SRuslan Ermilov 	if (zflag)
701c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
702c73d99b5SRuslan Ermilov 	if (sysctl(mib, 4, &icmpstat, &len,
703c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
704c73d99b5SRuslan Ermilov 		warn("sysctl: net.inet.icmp.stats");
705c73d99b5SRuslan Ermilov 		return;
706c73d99b5SRuslan Ermilov 	}
7074e00c309SGarrett Wollman 
7089b50d902SRodney W. Grimes 	printf("%s:\n", name);
7099b50d902SRodney W. Grimes 
7109b50d902SRodney W. Grimes #define	p(f, m) if (icmpstat.f || sflag <= 1) \
7119b50d902SRodney W. Grimes     printf(m, icmpstat.f, plural(icmpstat.f))
71222694ebaSBruce Evans #define	p1a(f, m) if (icmpstat.f || sflag <= 1) \
71322694ebaSBruce Evans     printf(m, icmpstat.f)
714bd714208SRuslan Ermilov #define	p2(f, m) if (icmpstat.f || sflag <= 1) \
715bd714208SRuslan Ermilov     printf(m, icmpstat.f, plurales(icmpstat.f))
7169b50d902SRodney W. Grimes 
7177d56c0eeSAlexander Langer 	p(icps_error, "\t%lu call%s to icmp_error\n");
7189b50d902SRodney W. Grimes 	p(icps_oldicmp,
719f99a4046SMike Makonnen 	    "\t%lu error%s not generated in response to an icmp message\n");
7209b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
7219b50d902SRodney W. Grimes 		if (icmpstat.icps_outhist[i] != 0) {
7229b50d902SRodney W. Grimes 			if (first) {
7239b50d902SRodney W. Grimes 				printf("\tOutput histogram:\n");
7249b50d902SRodney W. Grimes 				first = 0;
7259b50d902SRodney W. Grimes 			}
7264063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
7277d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
7289b50d902SRodney W. Grimes 					icmpstat.icps_outhist[i]);
7294063583aSMaxim Konovalov 			else
7304063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
7314063583aSMaxim Konovalov 					icmpstat.icps_outhist[i]);
7329b50d902SRodney W. Grimes 		}
7337d56c0eeSAlexander Langer 	p(icps_badcode, "\t%lu message%s with bad code fields\n");
7347d56c0eeSAlexander Langer 	p(icps_tooshort, "\t%lu message%s < minimum length\n");
7357d56c0eeSAlexander Langer 	p(icps_checksum, "\t%lu bad checksum%s\n");
7367d56c0eeSAlexander Langer 	p(icps_badlen, "\t%lu message%s with bad length\n");
73722694ebaSBruce Evans 	p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
73822694ebaSBruce Evans 	p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
7399b50d902SRodney W. Grimes 	for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
7409b50d902SRodney W. Grimes 		if (icmpstat.icps_inhist[i] != 0) {
7419b50d902SRodney W. Grimes 			if (first) {
7429b50d902SRodney W. Grimes 				printf("\tInput histogram:\n");
7439b50d902SRodney W. Grimes 				first = 0;
7449b50d902SRodney W. Grimes 			}
7454063583aSMaxim Konovalov 			if (icmpnames[i] != NULL)
7467d56c0eeSAlexander Langer 				printf("\t\t%s: %lu\n", icmpnames[i],
7479b50d902SRodney W. Grimes 			 		icmpstat.icps_inhist[i]);
7484063583aSMaxim Konovalov 			else
7494063583aSMaxim Konovalov 				printf("\t\tunknown ICMP #%d: %lu\n", i,
7504063583aSMaxim Konovalov 					icmpstat.icps_inhist[i]);
7519b50d902SRodney W. Grimes 		}
7527d56c0eeSAlexander Langer 	p(icps_reflect, "\t%lu message response%s generated\n");
753bd714208SRuslan Ermilov 	p2(icps_badaddr, "\t%lu invalid return address%s\n");
7540237ca7bSRuslan Ermilov 	p(icps_noroute, "\t%lu no return route%s\n");
7559b50d902SRodney W. Grimes #undef p
75622694ebaSBruce Evans #undef p1a
757bd714208SRuslan Ermilov #undef p2
7584e00c309SGarrett Wollman 	mib[3] = ICMPCTL_MASKREPL;
7594e00c309SGarrett Wollman 	len = sizeof i;
7604e00c309SGarrett Wollman 	if (sysctl(mib, 4, &i, &len, (void *)0, 0) < 0)
7614e00c309SGarrett Wollman 		return;
7624e00c309SGarrett Wollman 	printf("\tICMP address mask responses are %sabled\n",
7634e00c309SGarrett Wollman 	       i ? "en" : "dis");
7649b50d902SRodney W. Grimes }
7659b50d902SRodney W. Grimes 
7669b50d902SRodney W. Grimes /*
7679b50d902SRodney W. Grimes  * Dump IGMP statistics structure.
7689b50d902SRodney W. Grimes  */
7699b50d902SRodney W. Grimes void
770a01e3379SDavid Malone igmp_stats(u_long off __unused, const char *name, int af1 __unused)
7719b50d902SRodney W. Grimes {
772c73d99b5SRuslan Ermilov 	struct igmpstat igmpstat, zerostat;
7734f81ef50SGarrett Wollman 	size_t len = sizeof igmpstat;
7749b50d902SRodney W. Grimes 
775c73d99b5SRuslan Ermilov 	if (zflag)
776c73d99b5SRuslan Ermilov 		memset(&zerostat, 0, len);
777c73d99b5SRuslan Ermilov 	if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
778c73d99b5SRuslan Ermilov 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
7794f81ef50SGarrett Wollman 		warn("sysctl: net.inet.igmp.stats");
7809b50d902SRodney W. Grimes 		return;
7814f81ef50SGarrett Wollman 	}
7824f81ef50SGarrett Wollman 
7839b50d902SRodney W. Grimes 	printf("%s:\n", name);
7849b50d902SRodney W. Grimes 
7859b50d902SRodney W. Grimes #define	p(f, m) if (igmpstat.f || sflag <= 1) \
7869b50d902SRodney W. Grimes     printf(m, igmpstat.f, plural(igmpstat.f))
7879b50d902SRodney W. Grimes #define	py(f, m) if (igmpstat.f || sflag <= 1) \
7889b50d902SRodney W. Grimes     printf(m, igmpstat.f, igmpstat.f != 1 ? "ies" : "y")
7899b50d902SRodney W. Grimes 	p(igps_rcv_total, "\t%u message%s received\n");
7909b50d902SRodney W. Grimes         p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
7919b50d902SRodney W. Grimes         p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
7929b50d902SRodney W. Grimes         py(igps_rcv_queries, "\t%u membership quer%s received\n");
7939b50d902SRodney W. Grimes         py(igps_rcv_badqueries, "\t%u membership quer%s received with invalid field(s)\n");
7949b50d902SRodney W. Grimes         p(igps_rcv_reports, "\t%u membership report%s received\n");
7959b50d902SRodney W. Grimes         p(igps_rcv_badreports, "\t%u membership report%s received with invalid field(s)\n");
7969b50d902SRodney W. Grimes         p(igps_rcv_ourreports, "\t%u membership report%s received for groups to which we belong\n");
7979b50d902SRodney W. Grimes         p(igps_snd_reports, "\t%u membership report%s sent\n");
7989b50d902SRodney W. Grimes #undef p
7999b50d902SRodney W. Grimes #undef py
8009b50d902SRodney W. Grimes }
8019b50d902SRodney W. Grimes 
8029b50d902SRodney W. Grimes /*
803c7b9b5bbSJeffrey Hsu  * Dump PIM statistics structure.
804c7b9b5bbSJeffrey Hsu  */
805c7b9b5bbSJeffrey Hsu void
806c7b9b5bbSJeffrey Hsu pim_stats(u_long off __unused, const char *name, int af1 __unused)
807c7b9b5bbSJeffrey Hsu {
808c7b9b5bbSJeffrey Hsu 	struct pimstat pimstat, zerostat;
809c7b9b5bbSJeffrey Hsu 	size_t len = sizeof pimstat;
810c7b9b5bbSJeffrey Hsu 
811c7b9b5bbSJeffrey Hsu 	if (zflag)
812c7b9b5bbSJeffrey Hsu 		memset(&zerostat, 0, len);
813c7b9b5bbSJeffrey Hsu 	if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
814c7b9b5bbSJeffrey Hsu 	    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
8156d7c0d2fSDag-Erling Smørgrav 		if (errno != ENOENT)
816c7b9b5bbSJeffrey Hsu 			warn("sysctl: net.inet.pim.stats");
817c7b9b5bbSJeffrey Hsu 		return;
818c7b9b5bbSJeffrey Hsu 	}
819c7b9b5bbSJeffrey Hsu 
820c7b9b5bbSJeffrey Hsu 	printf("%s:\n", name);
821c7b9b5bbSJeffrey Hsu 
822c7b9b5bbSJeffrey Hsu #define	p(f, m) if (pimstat.f || sflag <= 1) \
8237b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
824c7b9b5bbSJeffrey Hsu #define	py(f, m) if (pimstat.f || sflag <= 1) \
8257b95a1ebSYaroslav Tykhiy     printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
8267b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_msgs, "\t%ju message%s received\n");
8277b95a1ebSYaroslav Tykhiy 	p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
8287b95a1ebSYaroslav Tykhiy 	p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
8297b95a1ebSYaroslav Tykhiy         p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
8307b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
8317b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
8327b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
8337b95a1ebSYaroslav Tykhiy 	p(pims_rcv_registers_wrongiif, "\t%ju data register message%s received on wrong iif\n");
8347b95a1ebSYaroslav Tykhiy 	p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
8357b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
8367b95a1ebSYaroslav Tykhiy 	p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
837c7b9b5bbSJeffrey Hsu #undef p
838c7b9b5bbSJeffrey Hsu #undef py
839c7b9b5bbSJeffrey Hsu }
840c7b9b5bbSJeffrey Hsu 
841c7b9b5bbSJeffrey Hsu /*
8429b50d902SRodney W. Grimes  * Pretty print an Internet address (net address + port).
8439b50d902SRodney W. Grimes  */
8449b50d902SRodney W. Grimes void
845a01e3379SDavid Malone inetprint(struct in_addr *in, int port, const char *proto, int num_port)
8469b50d902SRodney W. Grimes {
8479b50d902SRodney W. Grimes 	struct servent *sp = 0;
8489b50d902SRodney W. Grimes 	char line[80], *cp;
849cfa1ca9dSYoshinobu Inoue 	int width;
8509b50d902SRodney W. Grimes 
851080b7f49SDag-Erling Smørgrav 	if (Wflag)
852080b7f49SDag-Erling Smørgrav 	    sprintf(line, "%s.", inetname(in));
853080b7f49SDag-Erling Smørgrav 	else
854a01e3379SDavid Malone 	    sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
8559b50d902SRodney W. Grimes 	cp = index(line, '\0');
856a01e3379SDavid Malone 	if (!num_port && port)
8579b50d902SRodney W. Grimes 		sp = getservbyport((int)port, proto);
8589b50d902SRodney W. Grimes 	if (sp || port == 0)
8591ef69972SAndrey A. Chernov 		sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
8609b50d902SRodney W. Grimes 	else
8619b50d902SRodney W. Grimes 		sprintf(cp, "%d ", ntohs((u_short)port));
862080b7f49SDag-Erling Smørgrav 	width = (Aflag && !Wflag) ? 18 : 22;
863080b7f49SDag-Erling Smørgrav 	if (Wflag)
864080b7f49SDag-Erling Smørgrav 	    printf("%-*s ", width, line);
865080b7f49SDag-Erling Smørgrav 	else
866cfa1ca9dSYoshinobu Inoue 	    printf("%-*.*s ", width, width, line);
8679b50d902SRodney W. Grimes }
8689b50d902SRodney W. Grimes 
8699b50d902SRodney W. Grimes /*
8709b50d902SRodney W. Grimes  * Construct an Internet address representation.
87165ea0024SAssar Westerlund  * If numeric_addr has been supplied, give
8729b50d902SRodney W. Grimes  * numeric value, otherwise try for symbolic name.
8739b50d902SRodney W. Grimes  */
8749b50d902SRodney W. Grimes char *
8755e051718SAssar Westerlund inetname(struct in_addr *inp)
8769b50d902SRodney W. Grimes {
877a01e3379SDavid Malone 	char *cp;
878d121b556SBrian Somers 	static char line[MAXHOSTNAMELEN];
8799b50d902SRodney W. Grimes 	struct hostent *hp;
8809b50d902SRodney W. Grimes 	struct netent *np;
8819b50d902SRodney W. Grimes 
8829b50d902SRodney W. Grimes 	cp = 0;
88365ea0024SAssar Westerlund 	if (!numeric_addr && inp->s_addr != INADDR_ANY) {
8849b50d902SRodney W. Grimes 		int net = inet_netof(*inp);
8859b50d902SRodney W. Grimes 		int lna = inet_lnaof(*inp);
8869b50d902SRodney W. Grimes 
8879b50d902SRodney W. Grimes 		if (lna == INADDR_ANY) {
8889b50d902SRodney W. Grimes 			np = getnetbyaddr(net, AF_INET);
8899b50d902SRodney W. Grimes 			if (np)
8909b50d902SRodney W. Grimes 				cp = np->n_name;
8919b50d902SRodney W. Grimes 		}
8929b50d902SRodney W. Grimes 		if (cp == 0) {
8939b50d902SRodney W. Grimes 			hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
8949b50d902SRodney W. Grimes 			if (hp) {
8959b50d902SRodney W. Grimes 				cp = hp->h_name;
896d121b556SBrian Somers 				trimdomain(cp, strlen(cp));
8979b50d902SRodney W. Grimes 			}
8989b50d902SRodney W. Grimes 		}
8999b50d902SRodney W. Grimes 	}
9009b50d902SRodney W. Grimes 	if (inp->s_addr == INADDR_ANY)
9019b50d902SRodney W. Grimes 		strcpy(line, "*");
9029a1f6729SWarner Losh 	else if (cp) {
9039a1f6729SWarner Losh 		strncpy(line, cp, sizeof(line) - 1);
9049a1f6729SWarner Losh 		line[sizeof(line) - 1] = '\0';
9059a1f6729SWarner Losh 	} else {
9069b50d902SRodney W. Grimes 		inp->s_addr = ntohl(inp->s_addr);
90722694ebaSBruce Evans #define C(x)	((u_int)((x) & 0xff))
90822694ebaSBruce Evans 		sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
9099b50d902SRodney W. Grimes 		    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
9109b50d902SRodney W. Grimes 	}
9119b50d902SRodney W. Grimes 	return (line);
9129b50d902SRodney W. Grimes }
913