xref: /freebsd/usr.bin/netstat/main.c (revision 100b98db7576b0ca1bbc465795480b0202014f4b)
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>
669b50d902SRodney W. Grimes #include <stdio.h>
679b50d902SRodney W. Grimes #include <stdlib.h>
689b50d902SRodney W. Grimes #include <string.h>
699b50d902SRodney W. Grimes #include <unistd.h>
709b50d902SRodney W. Grimes #include "netstat.h"
719b50d902SRodney W. Grimes 
72c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
734f81ef50SGarrett Wollman #define	N_IFNET		0
749b50d902SRodney W. Grimes 	{ "_ifnet" },
75ab54ea99SPeter Wemm #define	N_RTSTAT	1
769b50d902SRodney W. Grimes 	{ "_rtstat" },
77ab54ea99SPeter Wemm #define N_RTREE		2
789b50d902SRodney W. Grimes 	{ "_rt_tables"},
79ab54ea99SPeter Wemm #define N_MRTSTAT	3
809b50d902SRodney W. Grimes 	{ "_mrtstat" },
81ab54ea99SPeter Wemm #define N_MFCTABLE	4
82ef105c25SGarrett Wollman 	{ "_mfctable" },
83ab54ea99SPeter Wemm #define N_VIFTABLE	5
849b50d902SRodney W. Grimes 	{ "_viftable" },
85ab54ea99SPeter Wemm #define N_IPX		6
86fff3e238SRobert Watson 	{ "_ipxpcb_list"},
87ab54ea99SPeter Wemm #define N_IPXSTAT	7
88cc6a66f2SJulian Elischer 	{ "_ipxstat"},
89ab54ea99SPeter Wemm #define N_SPXSTAT	8
90cc6a66f2SJulian Elischer 	{ "_spx_istat"},
91ab54ea99SPeter Wemm #define N_DDPSTAT	9
9263bf4575SJulian Elischer 	{ "_ddpstat"},
93ab54ea99SPeter Wemm #define N_DDPCB		10
9463bf4575SJulian Elischer 	{ "_ddpcb"},
95ab54ea99SPeter Wemm #define N_NGSOCKS	11
964cf49a43SJulian Elischer 	{ "_ngsocklist"},
97ab54ea99SPeter Wemm #define N_IP6STAT	12
98cfa1ca9dSYoshinobu Inoue 	{ "_ip6stat" },
99ab54ea99SPeter Wemm #define N_ICMP6STAT	13
100cfa1ca9dSYoshinobu Inoue 	{ "_icmp6stat" },
101ab54ea99SPeter Wemm #define N_IPSECSTAT	14
102cfa1ca9dSYoshinobu Inoue 	{ "_ipsecstat" },
103ab54ea99SPeter Wemm #define N_IPSEC6STAT	15
104cfa1ca9dSYoshinobu Inoue 	{ "_ipsec6stat" },
105ab54ea99SPeter Wemm #define N_PIM6STAT	16
106cfa1ca9dSYoshinobu Inoue 	{ "_pim6stat" },
107ab54ea99SPeter Wemm #define N_MRT6STAT	17
108cfa1ca9dSYoshinobu Inoue 	{ "_mrt6stat" },
109ab54ea99SPeter Wemm #define N_MF6CTABLE	18
110cfa1ca9dSYoshinobu Inoue 	{ "_mf6ctable" },
111ab54ea99SPeter Wemm #define N_MIF6TABLE	19
112cfa1ca9dSYoshinobu Inoue 	{ "_mif6table" },
113ab54ea99SPeter Wemm #define N_PFKEYSTAT	20
1143b8a8567SJun-ichiro itojun Hagino 	{ "_pfkeystat" },
115ab54ea99SPeter Wemm #define N_MBSTAT	21
116d15c5f56SRuslan Ermilov 	{ "_mbstat" },
117ab54ea99SPeter Wemm #define N_MBTYPES	22
118d15c5f56SRuslan Ermilov 	{ "_mbtypes" },
119ab54ea99SPeter Wemm #define N_NMBCLUSTERS	23
120d15c5f56SRuslan Ermilov 	{ "_nmbclusters" },
121ab54ea99SPeter Wemm #define N_NMBUFS	24
122d15c5f56SRuslan Ermilov 	{ "_nmbufs" },
123ab54ea99SPeter Wemm #define	N_MBHI		25
124025b4be1SBosko Milekic 	{ "_mbuf_hiwm" },
125ab54ea99SPeter Wemm #define	N_CLHI		26
126025b4be1SBosko Milekic 	{ "_clust_hiwm" },
127ab54ea99SPeter Wemm #define	N_NCPUS		27
12808442f8aSBosko Milekic 	{ "_smp_cpus" },
129ab54ea99SPeter Wemm #define	N_PAGESZ	28
13008442f8aSBosko Milekic 	{ "_pagesize" },
131ab54ea99SPeter Wemm #define	N_MBPSTAT	29
13208442f8aSBosko Milekic 	{ "_mb_statpcpu" },
133ab54ea99SPeter Wemm #define	N_RTTRASH	30
1341093cc31SRuslan Ermilov 	{ "_rttrash" },
135ab54ea99SPeter Wemm #define	N_MBLO		31
136025b4be1SBosko Milekic 	{ "_mbuf_lowm" },
137ab54ea99SPeter Wemm #define	N_CLLO		32
138025b4be1SBosko Milekic 	{ "_clust_lowm" },
139a9771948SGleb Smirnoff #define N_CARPSTAT	33
140a9771948SGleb Smirnoff 	{ "_carpstats" },
1412e37c5a3SMax Laier #define N_PFSYNCSTAT	34
1422e37c5a3SMax Laier 	{ "_pfsyncstats" },
143100b98dbSKelly Yancey #define	N_FAST_IPSECSTAT 35
144100b98dbSKelly Yancey 	{ "_newipsecstat" },
145100b98dbSKelly Yancey #define	N_AHSTAT	36
146100b98dbSKelly Yancey 	{ "_ahstat" },
147100b98dbSKelly Yancey #define	N_ESPSTAT	37
148100b98dbSKelly Yancey 	{ "_espstat" },
149100b98dbSKelly Yancey #define	N_IPCOMPSTAT	38
150100b98dbSKelly Yancey 	{ "_ipcompstat" },
1517d56c0eeSAlexander Langer 	{ "" },
1529b50d902SRodney W. Grimes };
1539b50d902SRodney W. Grimes 
1549b50d902SRodney W. Grimes struct protox {
1559b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1569b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1579b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
158a01e3379SDavid Malone 	void	(*pr_cblocks)(u_long, const char *, int);
1595e051718SAssar Westerlund 					/* control blocks printing routine */
160a01e3379SDavid Malone 	void	(*pr_stats)(u_long, const char *, int);
1615e051718SAssar Westerlund 					/* statistics printing routine */
1625e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
163fa6d48c0SMark Murray 	const char	*pr_name;		/* well-known name */
1646bb3f207SRuslan Ermilov 	u_long	pr_usesysctl;		/* non-zero if we use sysctl, not kvm */
1659b50d902SRodney W. Grimes } protox[] = {
1664f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
167cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1684f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
169cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1704f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
171cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1724f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
173cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1744f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
175cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1764f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
177cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
178cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
1796bb3f207SRuslan Ermilov 	{ -1,		N_IPSECSTAT,	1,	NULL,
180cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
181100b98dbSKelly Yancey #ifdef FAST_IPSEC
182100b98dbSKelly Yancey 	{ -1,		N_FAST_IPSECSTAT, 1,	0,
183100b98dbSKelly Yancey 	  ipsec_stats_new, NULL,	"fastipsec",	0},
184100b98dbSKelly Yancey 	{ -1,		N_AHSTAT,	1,	0,
185100b98dbSKelly Yancey 	  ah_stats,	NULL,		"ah",		0},
186100b98dbSKelly Yancey 	{ -1,		N_ESPSTAT,	1,	0,
187100b98dbSKelly Yancey 	  esp_stats,	NULL,		"esp",		0},
188100b98dbSKelly Yancey 	{ -1,		N_IPCOMPSTAT,	1,	0,
189100b98dbSKelly Yancey 	  ipcomp_stats,	NULL,		"ipcomp",	0},
190100b98dbSKelly Yancey #endif
191cfa1ca9dSYoshinobu Inoue #endif
192c7b9b5bbSJeffrey Hsu 	{ -1,		-1,		1,	protopr,
193c7b9b5bbSJeffrey Hsu 	  pim_stats,	NULL,		"pim",	IPPROTO_PIM },
194a9771948SGleb Smirnoff 	{ -1,		N_CARPSTAT,	1,	0,
195a9771948SGleb Smirnoff 	  carp_stats,	NULL,		"carp",		0},
1962e37c5a3SMax Laier 	{ -1,		-1,		1,	NULL,
1972e37c5a3SMax Laier 	  pfsync_stats,	NULL,		"pfsync",	1},
1986bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
1996bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
2009b50d902SRodney W. Grimes };
2019b50d902SRodney W. Grimes 
202cfa1ca9dSYoshinobu Inoue #ifdef INET6
203cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
204cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
205cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
206cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
207cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
208785d213dSHajimu UMEMOTO 	{ -1,		N_IP6STAT,	1,	protopr,
209785d213dSHajimu UMEMOTO 	  ip6_stats,	ip6_ifstats,	"ip6",	IPPROTO_RAW },
210785d213dSHajimu UMEMOTO 	{ -1,		N_ICMP6STAT,	1,	protopr,
211785d213dSHajimu UMEMOTO 	  icmp6_stats,	icmp6_ifstats,	"icmp6",IPPROTO_ICMPV6 },
212cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
2136bb3f207SRuslan Ermilov 	{ -1,		N_IPSEC6STAT,	1,	NULL,
214cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
215cfa1ca9dSYoshinobu Inoue #endif
216cfa1ca9dSYoshinobu Inoue #ifdef notyet
2176bb3f207SRuslan Ermilov 	{ -1,		N_PIM6STAT,	1,	NULL,
218cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
219cfa1ca9dSYoshinobu Inoue #endif
2206bb3f207SRuslan Ermilov 	{ -1,		-1,		1,	NULL,
22133841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
2226bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2236bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
224cfa1ca9dSYoshinobu Inoue };
225cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
226cfa1ca9dSYoshinobu Inoue 
2273b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2283b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2296bb3f207SRuslan Ermilov 	{ -1,		N_PFKEYSTAT,	1,	NULL,
2303b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2316bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2326bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
2333b8a8567SJun-ichiro itojun Hagino };
2343b8a8567SJun-ichiro itojun Hagino #endif
2353b8a8567SJun-ichiro itojun Hagino 
23663bf4575SJulian Elischer struct protox atalkprotox[] = {
23763bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
238a01e3379SDavid Malone 	  ddp_stats,	NULL,		"ddp",	0 },
2396bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2406bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
24163bf4575SJulian Elischer };
24263bf4575SJulian Elischer 
2434cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2444cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
245a01e3379SDavid Malone 	  NULL,		NULL,		"ctrl",	0 },
2464cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
247a01e3379SDavid Malone 	  NULL,		NULL,		"data",	0 },
2486bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2496bb3f207SRuslan Ermilov 	  NULL,		NULL,		NULL,	0 }
2504cf49a43SJulian Elischer };
2514cf49a43SJulian Elischer 
2529cc22e5cSPoul-Henning Kamp #ifdef IPX
253cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
254cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
255cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
256cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
257cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
2586bb3f207SRuslan Ermilov 	{ -1,		-1,		0,	NULL,
2596bb3f207SRuslan Ermilov 	  NULL,		NULL,		0,	0 }
260cc6a66f2SJulian Elischer };
2619cc22e5cSPoul-Henning Kamp #endif
262cc6a66f2SJulian Elischer 
263cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
264cfa1ca9dSYoshinobu Inoue 					 protox,
265cfa1ca9dSYoshinobu Inoue #ifdef INET6
266cfa1ca9dSYoshinobu Inoue 					 ip6protox,
267cfa1ca9dSYoshinobu Inoue #endif
2683b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2693b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2703b8a8567SJun-ichiro itojun Hagino #endif
2719cc22e5cSPoul-Henning Kamp #ifdef IPX
2729cc22e5cSPoul-Henning Kamp 					 ipxprotox,
2739cc22e5cSPoul-Henning Kamp #endif
2749cc22e5cSPoul-Henning Kamp 					 atalkprotox, NULL };
2759b50d902SRodney W. Grimes 
2766cc6f122SPhilippe Charnier const char *pluralies(int);
277fa6d48c0SMark Murray static void printproto(struct protox *, const char *);
2785e051718SAssar Westerlund static void usage(void);
2795e051718SAssar Westerlund static struct protox *name2protox(char *);
2805e051718SAssar Westerlund static struct protox *knownname(char *);
2819b50d902SRodney W. Grimes 
282c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
283080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
284080b7f49SDag-Erling Smørgrav 
285080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
286080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
2876b463eedSChristian S.J. Peron int	Bflag;		/* show information about bpf consumers */
288080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
289080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
290080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
291c2dfd19fSGleb Smirnoff int	hflag;		/* show counters in human readable format */
292080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
293080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
294080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
29565ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
29665ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
297cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
298080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
299080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
300080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
301080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
302c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
303080b7f49SDag-Erling Smørgrav 
304080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
305080b7f49SDag-Erling Smørgrav 
306080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
307080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
308080b7f49SDag-Erling Smørgrav 
309080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3109b50d902SRodney W. Grimes 
3119b50d902SRodney W. Grimes int
312a01e3379SDavid Malone main(int argc, char *argv[])
3139b50d902SRodney W. Grimes {
314a01e3379SDavid Malone 	struct protox *tp = NULL;  /* for printing cblocks & stats */
3159b50d902SRodney W. Grimes 	int ch;
3169b50d902SRodney W. Grimes 
3179b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3189b50d902SRodney W. Grimes 
3196b463eedSChristian S.J. Peron 	while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:rSstuWw:z")) != -1)
3209b50d902SRodney W. Grimes 		switch(ch) {
3219b50d902SRodney W. Grimes 		case 'A':
3229b50d902SRodney W. Grimes 			Aflag = 1;
3239b50d902SRodney W. Grimes 			break;
3249b50d902SRodney W. Grimes 		case 'a':
3259b50d902SRodney W. Grimes 			aflag = 1;
3269b50d902SRodney W. Grimes 			break;
3276b463eedSChristian S.J. Peron 		case 'B':
3286b463eedSChristian S.J. Peron 			Bflag = 1;
3296b463eedSChristian S.J. Peron 			break;
330e1e293a5SDavid Greenman 		case 'b':
331e1e293a5SDavid Greenman 			bflag = 1;
332e1e293a5SDavid Greenman 			break;
3339b50d902SRodney W. Grimes 		case 'd':
3349b50d902SRodney W. Grimes 			dflag = 1;
3359b50d902SRodney W. Grimes 			break;
3369b50d902SRodney W. Grimes 		case 'f':
337cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
338cc6a66f2SJulian Elischer 				af = AF_IPX;
3399b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3409b50d902SRodney W. Grimes 				af = AF_INET;
341cfa1ca9dSYoshinobu Inoue #ifdef INET6
342cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
343cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
344e5134d2eSMax Laier #endif
345e5134d2eSMax Laier #ifdef IPSEC
3463b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3473b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
348e5134d2eSMax Laier #endif
3499b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3509b50d902SRodney W. Grimes 				af = AF_UNIX;
35163bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
35263bf4575SJulian Elischer 				af = AF_APPLETALK;
3534cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3544cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3554cf49a43SJulian Elischer 				af = AF_NETGRAPH;
356d44ddba9SRuslan Ermilov 			else if (strcmp(optarg, "link") == 0)
357d44ddba9SRuslan Ermilov 				af = AF_LINK;
3589b50d902SRodney W. Grimes 			else {
35951e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3609b50d902SRodney W. Grimes 			}
3619b50d902SRodney W. Grimes 			break;
3629b50d902SRodney W. Grimes 		case 'g':
3639b50d902SRodney W. Grimes 			gflag = 1;
3649b50d902SRodney W. Grimes 			break;
365c2dfd19fSGleb Smirnoff 		case 'h':
366c2dfd19fSGleb Smirnoff 			hflag = 1;
367c2dfd19fSGleb Smirnoff 			break;
3689b50d902SRodney W. Grimes 		case 'I': {
3699b50d902SRodney W. Grimes 			char *cp;
3709b50d902SRodney W. Grimes 
3719b50d902SRodney W. Grimes 			iflag = 1;
3729b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3739b50d902SRodney W. Grimes 				continue;
3749b50d902SRodney W. Grimes 			unit = atoi(cp);
3759b50d902SRodney W. Grimes 			break;
3769b50d902SRodney W. Grimes 		}
3779b50d902SRodney W. Grimes 		case 'i':
3789b50d902SRodney W. Grimes 			iflag = 1;
3799b50d902SRodney W. Grimes 			break;
380ac55add0SGuido van Rooij 		case 'L':
381ac55add0SGuido van Rooij 			Lflag = 1;
382ac55add0SGuido van Rooij 			break;
3839b50d902SRodney W. Grimes 		case 'M':
3849b50d902SRodney W. Grimes 			memf = optarg;
3859b50d902SRodney W. Grimes 			break;
3869b50d902SRodney W. Grimes 		case 'm':
3879b50d902SRodney W. Grimes 			mflag = 1;
3889b50d902SRodney W. Grimes 			break;
3899b50d902SRodney W. Grimes 		case 'N':
3909b50d902SRodney W. Grimes 			nlistf = optarg;
3919b50d902SRodney W. Grimes 			break;
3929b50d902SRodney W. Grimes 		case 'n':
39365ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
3949b50d902SRodney W. Grimes 			break;
3959b50d902SRodney W. Grimes 		case 'p':
3969b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
39751e7d42cSGarrett Wollman 				errx(1,
39851e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
39951e7d42cSGarrett Wollman 				     optarg);
4009b50d902SRodney W. Grimes 			}
4019b50d902SRodney W. Grimes 			pflag = 1;
4029b50d902SRodney W. Grimes 			break;
4039b50d902SRodney W. Grimes 		case 'r':
4049b50d902SRodney W. Grimes 			rflag = 1;
4059b50d902SRodney W. Grimes 			break;
4069b50d902SRodney W. Grimes 		case 's':
4079b50d902SRodney W. Grimes 			++sflag;
4089b50d902SRodney W. Grimes 			break;
40965ea0024SAssar Westerlund 		case 'S':
41065ea0024SAssar Westerlund 			numeric_addr = 1;
41165ea0024SAssar Westerlund 			break;
4129b50d902SRodney W. Grimes 		case 't':
4139b50d902SRodney W. Grimes 			tflag = 1;
4149b50d902SRodney W. Grimes 			break;
4159b50d902SRodney W. Grimes 		case 'u':
4169b50d902SRodney W. Grimes 			af = AF_UNIX;
4179b50d902SRodney W. Grimes 			break;
418080b7f49SDag-Erling Smørgrav 		case 'W':
41970057abfSRuslan Ermilov 		case 'l':
420080b7f49SDag-Erling Smørgrav 			Wflag = 1;
421080b7f49SDag-Erling Smørgrav 			break;
4229b50d902SRodney W. Grimes 		case 'w':
4239b50d902SRodney W. Grimes 			interval = atoi(optarg);
4249b50d902SRodney W. Grimes 			iflag = 1;
4259b50d902SRodney W. Grimes 			break;
426c73d99b5SRuslan Ermilov 		case 'z':
427c73d99b5SRuslan Ermilov 			zflag = 1;
428c73d99b5SRuslan Ermilov 			break;
4299b50d902SRodney W. Grimes 		case '?':
4309b50d902SRodney W. Grimes 		default:
4319b50d902SRodney W. Grimes 			usage();
4329b50d902SRodney W. Grimes 		}
4339b50d902SRodney W. Grimes 	argv += optind;
4349b50d902SRodney W. Grimes 	argc -= optind;
4359b50d902SRodney W. Grimes 
4369b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4379b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4389b50d902SRodney W. Grimes 	if (*argv) {
4399b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4409b50d902SRodney W. Grimes 			interval = atoi(*argv);
4419b50d902SRodney W. Grimes 			if (interval <= 0)
4429b50d902SRodney W. Grimes 				usage();
4439b50d902SRodney W. Grimes 			++argv;
4449b50d902SRodney W. Grimes 			iflag = 1;
4459b50d902SRodney W. Grimes 		}
4469b50d902SRodney W. Grimes 		if (*argv) {
4479b50d902SRodney W. Grimes 			nlistf = *argv;
4489b50d902SRodney W. Grimes 			if (*++argv)
4499b50d902SRodney W. Grimes 				memf = *argv;
4509b50d902SRodney W. Grimes 		}
4519b50d902SRodney W. Grimes 	}
4529b50d902SRodney W. Grimes #endif
4539b50d902SRodney W. Grimes 
4549b50d902SRodney W. Grimes 	/*
4559b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4569b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4579b50d902SRodney W. Grimes 	 */
4589b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4599b50d902SRodney W. Grimes 		setgid(getgid());
4609b50d902SRodney W. Grimes 
4616b463eedSChristian S.J. Peron 	if (Bflag) {
4626b463eedSChristian S.J. Peron 		bpf_stats(interface);
4636b463eedSChristian S.J. Peron 		exit(0);
4646b463eedSChristian S.J. Peron 	}
4659b50d902SRodney W. Grimes 	if (mflag) {
466d15c5f56SRuslan Ermilov 		if (memf != NULL) {
467d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
468d4426f28SRobert Watson 				mbpr(kvmd, nl[N_MBSTAT].n_value);
469d15c5f56SRuslan Ermilov 		} else
470d4426f28SRobert Watson 			mbpr(NULL, 0);
4719b50d902SRodney W. Grimes 		exit(0);
4729b50d902SRodney W. Grimes 	}
473cc63cd56SPeter Wemm #if 0
4749b50d902SRodney W. Grimes 	/*
4759b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
4769b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
4779b50d902SRodney W. Grimes 	 */
4789b50d902SRodney W. Grimes 	sethostent(1);
4799b50d902SRodney W. Grimes 	setnetent(1);
480cc63cd56SPeter Wemm #else
481cc63cd56SPeter Wemm 	/*
482cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
483cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
484cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
485cc63cd56SPeter Wemm 	 */
486cc63cd56SPeter Wemm #endif
487cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
488c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
489cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
4909b50d902SRodney W. Grimes 		exit(0);
4919b50d902SRodney W. Grimes 	}
4929b50d902SRodney W. Grimes 	if (rflag) {
493c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
4949b50d902SRodney W. Grimes 		if (sflag)
4951093cc31SRuslan Ermilov 			rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
4969b50d902SRodney W. Grimes 		else
4979b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
4989b50d902SRodney W. Grimes 		exit(0);
4999b50d902SRodney W. Grimes 	}
5009b50d902SRodney W. Grimes 	if (gflag) {
501c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
502cfa1ca9dSYoshinobu Inoue 		if (sflag) {
503cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
50467a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
505cfa1ca9dSYoshinobu Inoue #ifdef INET6
506cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
507cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
508cfa1ca9dSYoshinobu Inoue #endif
509cfa1ca9dSYoshinobu Inoue 		} else {
510cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
51167a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5129b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
513cfa1ca9dSYoshinobu Inoue #ifdef INET6
514cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
515cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
516cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
517cfa1ca9dSYoshinobu Inoue #endif
518cfa1ca9dSYoshinobu Inoue 		}
5199fcc066dSBruce M Simpson 		ifmalist_dump();
5209b50d902SRodney W. Grimes 		exit(0);
5219b50d902SRodney W. Grimes 	}
522cfa1ca9dSYoshinobu Inoue 
523cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
524cf5e44f8SRuslan Ermilov 	if (tp) {
525100b98dbSKelly Yancey #ifdef FAST_IPSEC
526100b98dbSKelly Yancey 		/*
527100b98dbSKelly Yancey 		 * HACK: fallback to printing the new FAST IPSEC stats
528100b98dbSKelly Yancey 		 *	 if the kernel was built with FAST_IPSEC rather
529100b98dbSKelly Yancey 		 *	 than the KAME IPSEC stack (the two are mutually
530100b98dbSKelly Yancey 		 *	 exclusive).
531100b98dbSKelly Yancey 		 */
532100b98dbSKelly Yancey 		if (nl[tp->pr_sindex].n_value == 0 &&
533100b98dbSKelly Yancey 		    strcmp(tp->pr_name, "ipsec") == 0)
534100b98dbSKelly Yancey 			tp = name2protox("fastipsec");
535100b98dbSKelly Yancey #endif
536cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
537cf5e44f8SRuslan Ermilov 		exit(0);
538cf5e44f8SRuslan Ermilov 	}
539cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5409b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
541cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
542cfa1ca9dSYoshinobu Inoue #ifdef INET6
543cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
544cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
545cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
546cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5473b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5483b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5493b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5503b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5513b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
5529cc22e5cSPoul-Henning Kamp #ifdef IPX
553e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
554e30e913cSJohn Hay 		kread(0, 0, 0);
555cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
556cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
557e30e913cSJohn Hay 	}
5589cc22e5cSPoul-Henning Kamp #endif /* IPX */
55963bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
56063bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
56163bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5624cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5634cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5644cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
565ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
5664f81ef50SGarrett Wollman 		unixpr();
5679b50d902SRodney W. Grimes 	exit(0);
5689b50d902SRodney W. Grimes }
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes /*
5719b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5729b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5739b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
5749b50d902SRodney W. Grimes  */
5759b50d902SRodney W. Grimes static void
5769b50d902SRodney W. Grimes printproto(tp, name)
577a01e3379SDavid Malone 	struct protox *tp;
578fa6d48c0SMark Murray 	const char *name;
5799b50d902SRodney W. Grimes {
580a01e3379SDavid Malone 	void (*pr)(u_long, const char *, int);
5819b50d902SRodney W. Grimes 	u_long off;
5829b50d902SRodney W. Grimes 
5839b50d902SRodney W. Grimes 	if (sflag) {
584cfa1ca9dSYoshinobu Inoue 		if (iflag) {
585cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
586cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
587cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
588cf5e44f8SRuslan Ermilov 			else if (pflag)
589cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
590cf5e44f8SRuslan Ermilov 				    tp->pr_name);
591cfa1ca9dSYoshinobu Inoue 			return;
592cfa1ca9dSYoshinobu Inoue 		}
593cfa1ca9dSYoshinobu Inoue 		else {
5949b50d902SRodney W. Grimes 			pr = tp->pr_stats;
595cf5e44f8SRuslan Ermilov 			if (!pr) {
596cf5e44f8SRuslan Ermilov 				if (pflag)
597cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
598cf5e44f8SRuslan Ermilov 					    tp->pr_name);
599cf5e44f8SRuslan Ermilov 				return;
600cf5e44f8SRuslan Ermilov 			}
6014f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6024f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
603cfa1ca9dSYoshinobu Inoue 		}
6049b50d902SRodney W. Grimes 	} else {
6059b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
606cf5e44f8SRuslan Ermilov 		if (!pr) {
607cf5e44f8SRuslan Ermilov 			if (pflag)
608cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
609cf5e44f8SRuslan Ermilov 			return;
610cf5e44f8SRuslan Ermilov 		}
6114f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6124f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6139b50d902SRodney W. Grimes 	}
6149b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
615cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6169b50d902SRodney W. Grimes }
6179b50d902SRodney W. Grimes 
6189b50d902SRodney W. Grimes /*
6199b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6209b50d902SRodney W. Grimes  */
6219b50d902SRodney W. Grimes int
6225e051718SAssar Westerlund kread(u_long addr, char *buf, int size)
6239b50d902SRodney W. Grimes {
6244f81ef50SGarrett Wollman 	if (kvmd == 0) {
62599453c6aSPoul-Henning Kamp 		/*
62699453c6aSPoul-Henning Kamp 		 * XXX.
62799453c6aSPoul-Henning Kamp 		 */
62899453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
6293b7e5cccSRuslan Ermilov 		setgid(getgid());
63099453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
63199453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
63299453c6aSPoul-Henning Kamp 				if(nlistf)
63399453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
63499453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
63599453c6aSPoul-Henning Kamp 				else
63699453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
63799453c6aSPoul-Henning Kamp 			}
63899453c6aSPoul-Henning Kamp 
63999453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
64099453c6aSPoul-Henning Kamp 				if(nlistf)
64199453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
64299453c6aSPoul-Henning Kamp 				else
64399453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
64499453c6aSPoul-Henning Kamp 			}
64599453c6aSPoul-Henning Kamp 		} else {
64699453c6aSPoul-Henning Kamp 			warnx("kvm not available");
64799453c6aSPoul-Henning Kamp 			return(-1);
64899453c6aSPoul-Henning Kamp 		}
6494f81ef50SGarrett Wollman 	}
650c6620a13SPoul-Henning Kamp 	if (!buf)
651c6620a13SPoul-Henning Kamp 		return (0);
6529b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6533aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6549b50d902SRodney W. Grimes 		return (-1);
6559b50d902SRodney W. Grimes 	}
6569b50d902SRodney W. Grimes 	return (0);
6579b50d902SRodney W. Grimes }
6589b50d902SRodney W. Grimes 
659a01e3379SDavid Malone const char *
6605e051718SAssar Westerlund plural(int n)
6619b50d902SRodney W. Grimes {
6629b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6639b50d902SRodney W. Grimes }
6649b50d902SRodney W. Grimes 
665a01e3379SDavid Malone const char *
6665e051718SAssar Westerlund plurales(int n)
6679b50d902SRodney W. Grimes {
6689b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
6699b50d902SRodney W. Grimes }
6709b50d902SRodney W. Grimes 
671f99a4046SMike Makonnen const char *
672f99a4046SMike Makonnen pluralies(int n)
673f99a4046SMike Makonnen {
674f99a4046SMike Makonnen 	return (n != 1 ? "ies" : "y");
675f99a4046SMike Makonnen }
676f99a4046SMike Makonnen 
6779b50d902SRodney W. Grimes /*
6789b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
6799b50d902SRodney W. Grimes  */
6809b50d902SRodney W. Grimes static struct protox *
6815e051718SAssar Westerlund knownname(char *name)
6829b50d902SRodney W. Grimes {
6839b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
6849b50d902SRodney W. Grimes 
6859b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
6869b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
6879b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
6889b50d902SRodney W. Grimes 				return (tp);
6899b50d902SRodney W. Grimes 	return (NULL);
6909b50d902SRodney W. Grimes }
6919b50d902SRodney W. Grimes 
6929b50d902SRodney W. Grimes /*
6939b50d902SRodney W. Grimes  * Find the protox corresponding to name.
6949b50d902SRodney W. Grimes  */
6959b50d902SRodney W. Grimes static struct protox *
6965e051718SAssar Westerlund name2protox(char *name)
6979b50d902SRodney W. Grimes {
6989b50d902SRodney W. Grimes 	struct protox *tp;
6999b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7009b50d902SRodney W. Grimes 	struct protoent *p;
7019b50d902SRodney W. Grimes 
7029b50d902SRodney W. Grimes 	/*
7039b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7049b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7059b50d902SRodney W. Grimes 	 */
706cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7079b50d902SRodney W. Grimes 		return (tp);
7089b50d902SRodney W. Grimes 
7099b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
710cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7119b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7129b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7139b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7149b50d902SRodney W. Grimes 				endprotoent();
7159b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7169b50d902SRodney W. Grimes 			}
7179b50d902SRodney W. Grimes 	}
7189b50d902SRodney W. Grimes 	endprotoent();
7199b50d902SRodney W. Grimes 	return (NULL);
7209b50d902SRodney W. Grimes }
7219b50d902SRodney W. Grimes 
7229b50d902SRodney W. Grimes static void
7235e051718SAssar Westerlund usage(void)
7249b50d902SRodney W. Grimes {
7255dbd2da1SChristian 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",
7261cb467b1SRuslan Ermilov "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
7271cb467b1SRuslan Ermilov "               [-M core] [-N system]",
728c2dfd19fSGleb Smirnoff "       netstat -i | -I interface [-abdhnt] [-f address_family]\n"
729d44ddba9SRuslan Ermilov "               [-M core] [-N system]",
7301cb467b1SRuslan Ermilov "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
7311cb467b1SRuslan Ermilov "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
7321cb467b1SRuslan Ermilov "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
7331cb467b1SRuslan Ermilov "               [-M core] [-N system]",
73449c2dc64SMaxim Konovalov "       netstat -m [-M core] [-N system]",
7355dbd2da1SChristian S.J. Peron "       netstat -B [ -I interface]",
736dbe0253aSAlfred Perlstein "       netstat -r [-AenW] [-f address_family] [-M core] [-N system]",
7371cb467b1SRuslan Ermilov "       netstat -rs [-s] [-M core] [-N system]",
7381cb467b1SRuslan Ermilov "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
7391cb467b1SRuslan Ermilov "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
7409b50d902SRodney W. Grimes 	exit(1);
7419b50d902SRodney W. Grimes }
742