xref: /freebsd/usr.bin/netstat/main.c (revision 8409aedfa67396980986b68417d5e6ef1351b491)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1983, 1988, 1993
39b50d902SRodney W. Grimes  *	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 
349b50d902SRodney W. Grimes #ifndef lint
355d422d6aSPhilippe Charnier char const copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1983, 1988, 1993\n\
379b50d902SRodney W. Grimes 	Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
405d422d6aSPhilippe Charnier #if 0
416cc6f122SPhilippe Charnier #ifndef lint
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
439b50d902SRodney W. Grimes #endif /* not lint */
446cc6f122SPhilippe Charnier #endif
456cc6f122SPhilippe Charnier 
466cc6f122SPhilippe Charnier #include <sys/cdefs.h>
476cc6f122SPhilippe Charnier __FBSDID("$FreeBSD$");
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <sys/param.h>
509b50d902SRodney W. Grimes #include <sys/file.h>
519b50d902SRodney W. Grimes #include <sys/protosw.h>
529b50d902SRodney W. Grimes #include <sys/socket.h>
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes #include <netinet/in.h>
559b50d902SRodney W. Grimes 
564cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
574cf49a43SJulian Elischer 
589b50d902SRodney W. Grimes #include <ctype.h>
595d422d6aSPhilippe Charnier #include <err.h>
609b50d902SRodney W. Grimes #include <errno.h>
619b50d902SRodney W. Grimes #include <kvm.h>
629b50d902SRodney W. Grimes #include <limits.h>
639b50d902SRodney W. Grimes #include <netdb.h>
649b50d902SRodney W. Grimes #include <nlist.h>
659b50d902SRodney W. Grimes #include <paths.h>
667b95a1ebSYaroslav Tykhiy #include <stdint.h>
679b50d902SRodney W. Grimes #include <stdio.h>
689b50d902SRodney W. Grimes #include <stdlib.h>
699b50d902SRodney W. Grimes #include <string.h>
709b50d902SRodney W. Grimes #include <unistd.h>
719b50d902SRodney W. Grimes #include "netstat.h"
729b50d902SRodney W. Grimes 
73c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
744f81ef50SGarrett Wollman #define	N_IFNET		0
75096146f8SYaroslav Tykhiy 	{ .n_name = "_ifnet" },
76ab54ea99SPeter Wemm #define	N_RTSTAT	1
77096146f8SYaroslav Tykhiy 	{ .n_name = "_rtstat" },
78ab54ea99SPeter Wemm #define N_RTREE		2
79096146f8SYaroslav Tykhiy 	{ .n_name = "_rt_tables"},
80ab54ea99SPeter Wemm #define N_MRTSTAT	3
81096146f8SYaroslav Tykhiy 	{ .n_name = "_mrtstat" },
82ab54ea99SPeter Wemm #define N_MFCTABLE	4
83096146f8SYaroslav Tykhiy 	{ .n_name = "_mfctable" },
84ab54ea99SPeter Wemm #define N_VIFTABLE	5
85096146f8SYaroslav Tykhiy 	{ .n_name = "_viftable" },
86ab54ea99SPeter Wemm #define N_IPX		6
87096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxpcb_list"},
88ab54ea99SPeter Wemm #define N_IPXSTAT	7
89096146f8SYaroslav Tykhiy 	{ .n_name = "_ipxstat"},
90ab54ea99SPeter Wemm #define N_SPXSTAT	8
91096146f8SYaroslav Tykhiy 	{ .n_name = "_spx_istat"},
92ab54ea99SPeter Wemm #define N_DDPSTAT	9
93096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpstat"},
94ab54ea99SPeter Wemm #define N_DDPCB		10
95096146f8SYaroslav Tykhiy 	{ .n_name = "_ddpcb"},
96ab54ea99SPeter Wemm #define N_NGSOCKS	11
97096146f8SYaroslav Tykhiy 	{ .n_name = "_ngsocklist"},
98ab54ea99SPeter Wemm #define N_IP6STAT	12
99096146f8SYaroslav Tykhiy 	{ .n_name = "_ip6stat" },
100ab54ea99SPeter Wemm #define N_ICMP6STAT	13
101096146f8SYaroslav Tykhiy 	{ .n_name = "_icmp6stat" },
102ab54ea99SPeter Wemm #define N_IPSECSTAT	14
1038409aedfSGeorge V. Neville-Neil 	{ .n_name = "_ipsec4stat" },
104ab54ea99SPeter Wemm #define N_IPSEC6STAT	15
105096146f8SYaroslav Tykhiy 	{ .n_name = "_ipsec6stat" },
106ab54ea99SPeter Wemm #define N_PIM6STAT	16
107096146f8SYaroslav Tykhiy 	{ .n_name = "_pim6stat" },
108ab54ea99SPeter Wemm #define N_MRT6STAT	17
109096146f8SYaroslav Tykhiy 	{ .n_name = "_mrt6stat" },
110ab54ea99SPeter Wemm #define N_MF6CTABLE	18
111096146f8SYaroslav Tykhiy 	{ .n_name = "_mf6ctable" },
112ab54ea99SPeter Wemm #define N_MIF6TABLE	19
113096146f8SYaroslav Tykhiy 	{ .n_name = "_mif6table" },
114ab54ea99SPeter Wemm #define N_PFKEYSTAT	20
115096146f8SYaroslav Tykhiy 	{ .n_name = "_pfkeystat" },
116ab54ea99SPeter Wemm #define N_MBSTAT	21
117096146f8SYaroslav Tykhiy 	{ .n_name = "_mbstat" },
118ab54ea99SPeter Wemm #define N_MBTYPES	22
119096146f8SYaroslav Tykhiy 	{ .n_name = "_mbtypes" },
120ab54ea99SPeter Wemm #define N_NMBCLUSTERS	23
121096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbclusters" },
122ab54ea99SPeter Wemm #define N_NMBUFS	24
123096146f8SYaroslav Tykhiy 	{ .n_name = "_nmbufs" },
124ab54ea99SPeter Wemm #define	N_MBHI		25
125096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_hiwm" },
126ab54ea99SPeter Wemm #define	N_CLHI		26
127096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_hiwm" },
128ab54ea99SPeter Wemm #define	N_NCPUS		27
129096146f8SYaroslav Tykhiy 	{ .n_name = "_smp_cpus" },
130ab54ea99SPeter Wemm #define	N_PAGESZ	28
131096146f8SYaroslav Tykhiy 	{ .n_name = "_pagesize" },
132ab54ea99SPeter Wemm #define	N_MBPSTAT	29
133096146f8SYaroslav Tykhiy 	{ .n_name = "_mb_statpcpu" },
134ab54ea99SPeter Wemm #define	N_RTTRASH	30
135096146f8SYaroslav Tykhiy 	{ .n_name = "_rttrash" },
136ab54ea99SPeter Wemm #define	N_MBLO		31
137096146f8SYaroslav Tykhiy 	{ .n_name = "_mbuf_lowm" },
138ab54ea99SPeter Wemm #define	N_CLLO		32
139096146f8SYaroslav Tykhiy 	{ .n_name = "_clust_lowm" },
140a9771948SGleb Smirnoff #define N_CARPSTAT	33
141096146f8SYaroslav Tykhiy 	{ .n_name = "_carpstats" },
1422e37c5a3SMax Laier #define N_PFSYNCSTAT	34
143096146f8SYaroslav Tykhiy 	{ .n_name = "_pfsyncstats" },
144100b98dbSKelly Yancey #define	N_AHSTAT	36
145096146f8SYaroslav Tykhiy 	{ .n_name = "_ahstat" },
146100b98dbSKelly Yancey #define	N_ESPSTAT	37
147096146f8SYaroslav Tykhiy 	{ .n_name = "_espstat" },
148100b98dbSKelly Yancey #define	N_IPCOMPSTAT	38
149096146f8SYaroslav Tykhiy 	{ .n_name = "_ipcompstat" },
150096146f8SYaroslav Tykhiy 	{ .n_name = NULL },
1519b50d902SRodney W. Grimes };
1529b50d902SRodney W. Grimes 
1539b50d902SRodney W. Grimes struct protox {
1549b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1559b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1569b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
157a01e3379SDavid Malone 	void	(*pr_cblocks)(u_long, const char *, int);
1585e051718SAssar Westerlund 					/* control blocks printing routine */
159a01e3379SDavid Malone 	void	(*pr_stats)(u_long, const char *, int);
1605e051718SAssar Westerlund 					/* statistics printing routine */
1615e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
162fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
1636bb3f207SRuslan Ermilov 	u_long	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
1649b50d902SRodney W. Grimes } protox[] = {
1654f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
166cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1674f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
168cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
16974fd40c9SRandall Stewart #ifdef SCTP
17074fd40c9SRandall Stewart 	{ -1,		-1,		1,	sctp_protopr,
17174fd40c9SRandall Stewart 	  sctp_stats,	NULL,		"sctp",	IPPROTO_SCTP },
17274fd40c9SRandall Stewart #endif
1734f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
174cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1754f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
176cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1774f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
178cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1794f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
180cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
181cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
1828409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPSECSTAT,	1,	NULL,	/* keep as compat */
183cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
1848409aedfSGeorge V. Neville-Neil 	{ -1,		N_AHSTAT,	1,	NULL,
185100b98dbSKelly Yancey 	  ah_stats,	NULL,		"ah",		0},
1868409aedfSGeorge V. Neville-Neil 	{ -1,		N_ESPSTAT,	1,	NULL,
187100b98dbSKelly Yancey 	  esp_stats,	NULL,		"esp",		0},
1888409aedfSGeorge V. Neville-Neil 	{ -1,		N_IPCOMPSTAT,	1,	NULL,
189100b98dbSKelly Yancey 	  ipcomp_stats,	NULL,		"ipcomp",	0},
190100b98dbSKelly Yancey #endif
191c7b9b5bbSJeffrey Hsu 	{ -1,		-1,		1,	protopr,
192c7b9b5bbSJeffrey Hsu 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
193a9771948SGleb Smirnoff 	{ -1,		N_CARPSTAT,	1,	0,
194a9771948SGleb Smirnoff 	  carp_stats,	NULL,		"carp",		0},
1952e37c5a3SMax Laier 	{ -1,		-1,		1,	NULL,
1962e37c5a3SMax Laier 	  pfsync_stats,	NULL,		"pfsync",	1},
1976bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
1986bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
1999b50d902SRodney W. Grimes };
2009b50d902SRodney W. Grimes 
201cfa1ca9dSYoshinobu Inoue #ifdef INET6
202cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
203cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
204cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
205cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
206cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
207785d213dSHajimu UMEMOTO 	{ -1,		N_IP6STAT,	1,	protopr,
208785d213dSHajimu UMEMOTO 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
209785d213dSHajimu UMEMOTO 	{ -1,		N_ICMP6STAT,	1,	protopr,
210785d213dSHajimu UMEMOTO 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
211cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2126bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
213cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
214cfa1ca9dSYoshinobu Inoue #endif
215cfa1ca9dSYoshinobu Inoue #ifdef notyet
2166bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
217cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
218cfa1ca9dSYoshinobu Inoue #endif
2196bb3f207SRuslan Ermilov 	{ -1,		-1,		1,	NULL,
22033841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
2216bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2226bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
223cfa1ca9dSYoshinobu Inoue };
224cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
225cfa1ca9dSYoshinobu Inoue 
2263b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2273b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2286bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
2293b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2306bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2316bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
2323b8a8567SJun-ichiro itojun Hagino };
2333b8a8567SJun-ichiro itojun Hagino #endif
2343b8a8567SJun-ichiro itojun Hagino 
23563bf4575SJulian Elischer struct protox atalkprotox[] = {
23663bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
237a01e3379SDavid Malone 	  ddp_stats,	NULL,		"ddp",	0 },
2386bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2396bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
24063bf4575SJulian Elischer };
24163bf4575SJulian Elischer 
2424cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2434cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
244a01e3379SDavid Malone 	  NULL,		NULL,		"ctrl",	0 },
2454cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
246a01e3379SDavid Malone 	  NULL,		NULL,		"data",	0 },
2476bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2486bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
2494cf49a43SJulian Elischer };
2504cf49a43SJulian Elischer 
2519cc22e5cSPoul-Henning Kamp #ifdef IPX
252cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
253cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
254cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
255cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
256cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
2576bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2586bb3f207SRuslan Ermilov 	  NULL,		NULL,		0,	0 }
259cc6a66f2SJulian Elischer };
2609cc22e5cSPoul-Henning Kamp #endif
261cc6a66f2SJulian Elischer 
262cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
263cfa1ca9dSYoshinobu Inoue 					 protox,
264cfa1ca9dSYoshinobu Inoue #ifdef INET6
265cfa1ca9dSYoshinobu Inoue 					 ip6protox,
266cfa1ca9dSYoshinobu Inoue #endif
2673b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2683b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2693b8a8567SJun-ichiro itojun Hagino #endif
2709cc22e5cSPoul-Henning Kamp #ifdef IPX
2719cc22e5cSPoul-Henning Kamp 					 ipxprotox,
2729cc22e5cSPoul-Henning Kamp #endif
2739cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
2749b50d902SRodney W. Grimes 
275fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
2765e051718SAssar Westerlund static void usage(void);
277096146f8SYaroslav Tykhiy static struct protox *name2protox(const char *);
278096146f8SYaroslav Tykhiy static struct protox *knownname(const char *);
2799b50d902SRodney W. Grimes 
280c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
281080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
282080b7f49SDag-Erling Smørgrav 
283080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
284080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
2856b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
286080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
287080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
288080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
289c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
290080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
291080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
292080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
29365ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
29465ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
295cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
296080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
297080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
298080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
299080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
300c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
301080b7f49SDag-Erling Smørgrav 
302080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
303080b7f49SDag-Erling Smørgrav 
304080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
305080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
306080b7f49SDag-Erling Smørgrav 
307080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3089b50d902SRodney W. Grimes 
3099b50d902SRodney W. Grimes int
310a01e3379SDavid Malone main(int argc, char *argv[])
3119b50d902SRodney W. Grimes {
312a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3139b50d902SRodney W. Grimes 	int ch;
3149b50d902SRodney W. Grimes 
3159b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3169b50d902SRodney W. Grimes 
3176b463eedSChristian S.J. Peron 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
3189b50d902SRodney W. Grimes 		switch(ch) {
3199b50d902SRodney W. Grimes 		case 'A':
3209b50d902SRodney W. Grimes 			Aflag = 1;
3219b50d902SRodney W. Grimes 			break;
3229b50d902SRodney W. Grimes 		case 'a':
3239b50d902SRodney W. Grimes 			aflag = 1;
3249b50d902SRodney W. Grimes 			break;
3256b463eedSChristian S.J. Peron 		case 'B':
3266b463eedSChristian S.J. Peron 			Bflag = 1;
3276b463eedSChristian S.J. Peron 			break;
328e1e293a5SDavid Greenman 		case 'b':
329e1e293a5SDavid Greenman 			bflag = 1;
330e1e293a5SDavid Greenman 			break;
3319b50d902SRodney W. Grimes 		case 'd':
3329b50d902SRodney W. Grimes 			dflag = 1;
3339b50d902SRodney W. Grimes 			break;
3349b50d902SRodney W. Grimes 		case 'f':
335cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
336cc6a66f2SJulian Elischer 				af = AF_IPX;
3379b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3389b50d902SRodney W. Grimes 				af = AF_INET;
339cfa1ca9dSYoshinobu Inoue #ifdef INET6
340cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
341cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
342e5134d2eSMax Laier #endif
343e5134d2eSMax Laier #ifdef IPSEC
3443b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3453b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
346e5134d2eSMax Laier #endif
3479b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3489b50d902SRodney W. Grimes 				af = AF_UNIX;
34963bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
35063bf4575SJulian Elischer 				af = AF_APPLETALK;
3514cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3524cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3534cf49a43SJulian Elischer 				af = AF_NETGRAPH;
354d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
355d44ddba9SRuslan Ermilov 				af = AF_LINK;
3569b50d902SRodney W. Grimes 			else {
35751e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3589b50d902SRodney W. Grimes 			}
3599b50d902SRodney W. Grimes 			break;
3609b50d902SRodney W. Grimes 		case 'g':
3619b50d902SRodney W. Grimes 			gflag = 1;
3629b50d902SRodney W. Grimes 			break;
363c2dfd19fSGleb Smirnoff 		case 'h':
364c2dfd19fSGleb Smirnoff 			hflag = 1;
365c2dfd19fSGleb Smirnoff 			break;
3669b50d902SRodney W. Grimes 		case 'I': {
3679b50d902SRodney W. Grimes 			char *cp;
3689b50d902SRodney W. Grimes 
3699b50d902SRodney W. Grimes 			iflag = 1;
3709b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3719b50d902SRodney W. Grimes 				continue;
3729b50d902SRodney W. Grimes 			unit = atoi(cp);
3739b50d902SRodney W. Grimes 			break;
3749b50d902SRodney W. Grimes 		}
3759b50d902SRodney W. Grimes 		case 'i':
3769b50d902SRodney W. Grimes 			iflag = 1;
3779b50d902SRodney W. Grimes 			break;
378ac55add0SGuido van Rooij 		case 'L':
379ac55add0SGuido van Rooij 			Lflag = 1;
380ac55add0SGuido van Rooij 			break;
3819b50d902SRodney W. Grimes 		case 'M':
3829b50d902SRodney W. Grimes 			memf = optarg;
3839b50d902SRodney W. Grimes 			break;
3849b50d902SRodney W. Grimes 		case 'm':
3859b50d902SRodney W. Grimes 			mflag = 1;
3869b50d902SRodney W. Grimes 			break;
3879b50d902SRodney W. Grimes 		case 'N':
3889b50d902SRodney W. Grimes 			nlistf = optarg;
3899b50d902SRodney W. Grimes 			break;
3909b50d902SRodney W. Grimes 		case 'n':
39165ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
3929b50d902SRodney W. Grimes 			break;
3939b50d902SRodney W. Grimes 		case 'p':
3949b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
39551e7d42cSGarrett Wollman 				errx(1,
39651e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
39751e7d42cSGarrett Wollman 				     optarg);
3989b50d902SRodney W. Grimes 			}
3999b50d902SRodney W. Grimes 			pflag = 1;
4009b50d902SRodney W. Grimes 			break;
4019b50d902SRodney W. Grimes 		case 'r':
4029b50d902SRodney W. Grimes 			rflag = 1;
4039b50d902SRodney W. Grimes 			break;
4049b50d902SRodney W. Grimes 		case 's':
4059b50d902SRodney W. Grimes 			++sflag;
4069b50d902SRodney W. Grimes 			break;
40765ea0024SAssar Westerlund 		case 'S':
40865ea0024SAssar Westerlund 			numeric_addr = 1;
40965ea0024SAssar Westerlund 			break;
4109b50d902SRodney W. Grimes 		case 't':
4119b50d902SRodney W. Grimes 			tflag = 1;
4129b50d902SRodney W. Grimes 			break;
4139b50d902SRodney W. Grimes 		case 'u':
4149b50d902SRodney W. Grimes 			af = AF_UNIX;
4159b50d902SRodney W. Grimes 			break;
416080b7f49SDag-Erling Smørgrav 		case 'W':
41770057abfSRuslan Ermilov 		case 'l':
418080b7f49SDag-Erling Smørgrav 			Wflag = 1;
419080b7f49SDag-Erling Smørgrav 			break;
4209b50d902SRodney W. Grimes 		case 'w':
4219b50d902SRodney W. Grimes 			interval = atoi(optarg);
4229b50d902SRodney W. Grimes 			iflag = 1;
4239b50d902SRodney W. Grimes 			break;
424c73d99b5SRuslan Ermilov 		case 'z':
425c73d99b5SRuslan Ermilov 			zflag = 1;
426c73d99b5SRuslan Ermilov 			break;
4279b50d902SRodney W. Grimes 		case '?':
4289b50d902SRodney W. Grimes 		default:
4299b50d902SRodney W. Grimes 			usage();
4309b50d902SRodney W. Grimes 		}
4319b50d902SRodney W. Grimes 	argv += optind;
4329b50d902SRodney W. Grimes 	argc -= optind;
4339b50d902SRodney W. Grimes 
4349b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4359b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4369b50d902SRodney W. Grimes 	if (*argv) {
4379b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4389b50d902SRodney W. Grimes 			interval = atoi(*argv);
4399b50d902SRodney W. Grimes 			if (interval <= 0)
4409b50d902SRodney W. Grimes 				usage();
4419b50d902SRodney W. Grimes 			++argv;
4429b50d902SRodney W. Grimes 			iflag = 1;
4439b50d902SRodney W. Grimes 		}
4449b50d902SRodney W. Grimes 		if (*argv) {
4459b50d902SRodney W. Grimes 			nlistf = *argv;
4469b50d902SRodney W. Grimes 			if (*++argv)
4479b50d902SRodney W. Grimes 				memf = *argv;
4489b50d902SRodney W. Grimes 		}
4499b50d902SRodney W. Grimes 	}
4509b50d902SRodney W. Grimes #endif
4519b50d902SRodney W. Grimes 
4529b50d902SRodney W. Grimes 	/*
4539b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4549b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4559b50d902SRodney W. Grimes 	 */
4569b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4579b50d902SRodney W. Grimes 		setgid(getgid());
4589b50d902SRodney W. Grimes 
4596b463eedSChristian S.J. Peron 	if (Bflag) {
4606b463eedSChristian S.J. Peron 		bpf_stats(interface);
4616b463eedSChristian S.J. Peron 		exit(0);
4626b463eedSChristian S.J. Peron 	}
4639b50d902SRodney W. Grimes 	if (mflag) {
464d15c5f56SRuslan Ermilov 		if (memf != NULL) {
465d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
466d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
467d15c5f56SRuslan Ermilov 		} else
468d4426f28SRobert Watson 			mbpr(NULL, 0);
4699b50d902SRodney W. Grimes 		exit(0);
4709b50d902SRodney W. Grimes 	}
471cc63cd56SPeter Wemm #if 0
4729b50d902SRodney W. Grimes 	/*
4739b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
4749b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
4759b50d902SRodney W. Grimes 	 */
4769b50d902SRodney W. Grimes 	sethostent(1);
4779b50d902SRodney W. Grimes 	setnetent(1);
478cc63cd56SPeter Wemm #else
479cc63cd56SPeter Wemm 	/*
480cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
481cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
482cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
483cc63cd56SPeter Wemm 	 */
484cc63cd56SPeter Wemm #endif
485cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
486c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
487cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
4889b50d902SRodney W. Grimes 		exit(0);
4899b50d902SRodney W. Grimes 	}
4909b50d902SRodney W. Grimes 	if (rflag) {
491c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
4929b50d902SRodney W. Grimes 		if (sflag)
4931093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
4949b50d902SRodney W. Grimes 		else
4959b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
4969b50d902SRodney W. Grimes 		exit(0);
4979b50d902SRodney W. Grimes 	}
4989b50d902SRodney W. Grimes 	if (gflag) {
499c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
500cfa1ca9dSYoshinobu Inoue 		if (sflag) {
501cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
50267a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
503cfa1ca9dSYoshinobu Inoue #ifdef INET6
504cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
505cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
506cfa1ca9dSYoshinobu Inoue #endif
507cfa1ca9dSYoshinobu Inoue 		} else {
508cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
50967a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5109b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
511cfa1ca9dSYoshinobu Inoue #ifdef INET6
512cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
513cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
514cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
515cfa1ca9dSYoshinobu Inoue #endif
516cfa1ca9dSYoshinobu Inoue 		}
5179fcc066dSBruce M Simpson 		ifmalist_dump();
5189b50d902SRodney W. Grimes 		exit(0);
5199b50d902SRodney W. Grimes 	}
520cfa1ca9dSYoshinobu Inoue 
521cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
522cf5e44f8SRuslan Ermilov 	if (tp) {
523cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
524cf5e44f8SRuslan Ermilov 		exit(0);
525cf5e44f8SRuslan Ermilov 	}
526cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5279b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
528cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
529cfa1ca9dSYoshinobu Inoue #ifdef INET6
530cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
531cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
532cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
533cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5343b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5353b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5363b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5373b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5383b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
5399cc22e5cSPoul-Henning Kamp #ifdef IPX
540e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
541e30e913cSJohn Hay 		kread(0, 0, 0);
542cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
543cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
544e30e913cSJohn Hay 	}
5459cc22e5cSPoul-Henning Kamp #endif /* IPX */
54663bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
54763bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
54863bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5494cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5504cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5514cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
552ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
5534f81ef50SGarrett Wollman 		unixpr();
5549b50d902SRodney W. Grimes 	exit(0);
5559b50d902SRodney W. Grimes }
5569b50d902SRodney W. Grimes 
5579b50d902SRodney W. Grimes /*
5589b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5599b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5609b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
5619b50d902SRodney W. Grimes  */
5629b50d902SRodney W. Grimes static void
5639b50d902SRodney W. Grimes printproto(tp, name)
564a01e3379SDavid Malone 	struct protox *tp;
565fa6d48c0SMark Murray 	const char *name;
5669b50d902SRodney W. Grimes {
567a01e3379SDavid Malone 	void (*pr)(u_long, const char *, int);
5689b50d902SRodney W. Grimes 	u_long off;
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes 	if (sflag) {
571cfa1ca9dSYoshinobu Inoue 		if (iflag) {
572cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
573cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
574cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
575cf5e44f8SRuslan Ermilov 			else if (pflag)
576cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
577cf5e44f8SRuslan Ermilov 				    tp->pr_name);
578cfa1ca9dSYoshinobu Inoue 			return;
579cfa1ca9dSYoshinobu Inoue 		}
580cfa1ca9dSYoshinobu Inoue 		else {
5819b50d902SRodney W. Grimes 			pr = tp->pr_stats;
582cf5e44f8SRuslan Ermilov 			if (!pr) {
583cf5e44f8SRuslan Ermilov 				if (pflag)
584cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
585cf5e44f8SRuslan Ermilov 					    tp->pr_name);
586cf5e44f8SRuslan Ermilov 				return;
587cf5e44f8SRuslan Ermilov 			}
5884f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
5894f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
590cfa1ca9dSYoshinobu Inoue 		}
5919b50d902SRodney W. Grimes 	} else {
5929b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
593cf5e44f8SRuslan Ermilov 		if (!pr) {
594cf5e44f8SRuslan Ermilov 			if (pflag)
595cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
596cf5e44f8SRuslan Ermilov 			return;
597cf5e44f8SRuslan Ermilov 		}
5984f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
5994f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6009b50d902SRodney W. Grimes 	}
6019b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
602cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6039b50d902SRodney W. Grimes }
6049b50d902SRodney W. Grimes 
6059b50d902SRodney W. Grimes /*
6069b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6079b50d902SRodney W. Grimes  */
6089b50d902SRodney W. Grimes int
6095e051718SAssar Westerlund kread(u_long addr, char *buf, int size)
6109b50d902SRodney W. Grimes {
6114f81ef50SGarrett Wollman 	if (kvmd == 0) {
61299453c6aSPoul-Henning Kamp 		/*
61399453c6aSPoul-Henning Kamp 		 * XXX.
61499453c6aSPoul-Henning Kamp 		 */
61599453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
6163b7e5cccSRuslan Ermilov 		setgid(getgid());
61799453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
61899453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
61999453c6aSPoul-Henning Kamp 				if(nlistf)
62099453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
62199453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
62299453c6aSPoul-Henning Kamp 				else
62399453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
62499453c6aSPoul-Henning Kamp 			}
62599453c6aSPoul-Henning Kamp 
62699453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
62799453c6aSPoul-Henning Kamp 				if(nlistf)
62899453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
62999453c6aSPoul-Henning Kamp 				else
63099453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
63199453c6aSPoul-Henning Kamp 			}
63299453c6aSPoul-Henning Kamp 		} else {
63399453c6aSPoul-Henning Kamp 			warnx("kvm not available");
63499453c6aSPoul-Henning Kamp 			return(-1);
63599453c6aSPoul-Henning Kamp 		}
6364f81ef50SGarrett Wollman 	}
637c6620a13SPoul-Henning Kamp 	if (!buf)
638c6620a13SPoul-Henning Kamp 		return (0);
6399b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6403aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6419b50d902SRodney W. Grimes 		return (-1);
6429b50d902SRodney W. Grimes 	}
6439b50d902SRodney W. Grimes 	return (0);
6449b50d902SRodney W. Grimes }
6459b50d902SRodney W. Grimes 
646a01e3379SDavid Malone const char *
6477b95a1ebSYaroslav Tykhiy plural(uintmax_t n)
6489b50d902SRodney W. Grimes {
6499b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6509b50d902SRodney W. Grimes }
6519b50d902SRodney W. Grimes 
652a01e3379SDavid Malone const char *
6537b95a1ebSYaroslav Tykhiy plurales(uintmax_t n)
6549b50d902SRodney W. Grimes {
6559b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
6569b50d902SRodney W. Grimes }
6579b50d902SRodney W. Grimes 
658f99a4046SMike Makonnen const char *
6597b95a1ebSYaroslav Tykhiy pluralies(uintmax_t n)
660f99a4046SMike Makonnen {
661f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
662f99a4046SMike Makonnen }
663f99a4046SMike Makonnen 
6649b50d902SRodney W. Grimes /*
6659b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
6669b50d902SRodney W. Grimes  */
6679b50d902SRodney W. Grimes static struct protox *
668096146f8SYaroslav Tykhiy knownname(const char *name)
6699b50d902SRodney W. Grimes {
6709b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
6719b50d902SRodney W. Grimes 
6729b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
6739b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
6749b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
6759b50d902SRodney W. Grimes 				return (tp);
6769b50d902SRodney W. Grimes 	return (NULL);
6779b50d902SRodney W. Grimes }
6789b50d902SRodney W. Grimes 
6799b50d902SRodney W. Grimes /*
6809b50d902SRodney W. Grimes  * Find the protox corresponding to name.
6819b50d902SRodney W. Grimes  */
6829b50d902SRodney W. Grimes static struct protox *
683096146f8SYaroslav Tykhiy name2protox(const char *name)
6849b50d902SRodney W. Grimes {
6859b50d902SRodney W. Grimes 	struct protox *tp;
6869b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
6879b50d902SRodney W. Grimes 	struct protoent *p;
6889b50d902SRodney W. Grimes 
6899b50d902SRodney W. Grimes 	/*
6909b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
6919b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
6929b50d902SRodney W. Grimes 	 */
693cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
6949b50d902SRodney W. Grimes 		return (tp);
6959b50d902SRodney W. Grimes 
6969b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
697cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
6989b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
6999b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7009b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7019b50d902SRodney W. Grimes 				endprotoent();
7029b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7039b50d902SRodney W. Grimes 			}
7049b50d902SRodney W. Grimes 	}
7059b50d902SRodney W. Grimes 	endprotoent();
7069b50d902SRodney W. Grimes 	return (NULL);
7079b50d902SRodney W. Grimes }
7089b50d902SRodney W. Grimes 
7099b50d902SRodney W. Grimes static void
7105e051718SAssar Westerlund usage(void)
7119b50d902SRodney W. Grimes {
7125dbd2da1SChristian S.J. Peron 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
7131cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7141cb467b1SRuslan Ermilov "               [-M core] [-N system]",
715c2dfd19fSGleb Smirnoff "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
716d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7171cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7181cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7191cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7201cb467b1SRuslan Ermilov "               [-M core] [-N system]",
72149c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
7225dbd2da1SChristian S.J. Peron "       netstat -B [ -I interface]",
723dbe0253aSAlfred Perlstein "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
7241cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7251cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7261cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7279b50d902SRodney W. Grimes 	exit(1);
7289b50d902SRodney W. Grimes }
729