xref: /freebsd/usr.bin/netstat/main.c (revision 080b7f49675257081fd3e988c269fb2b8374080a)
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 
409b50d902SRodney W. Grimes #ifndef lint
415d422d6aSPhilippe Charnier #if 0
429b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
435d422d6aSPhilippe Charnier #endif
445d422d6aSPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/param.h>
499b50d902SRodney W. Grimes #include <sys/file.h>
509b50d902SRodney W. Grimes #include <sys/protosw.h>
519b50d902SRodney W. Grimes #include <sys/socket.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include <netinet/in.h>
549b50d902SRodney W. Grimes 
554cf49a43SJulian Elischer #include <netgraph/ng_socket.h>
564cf49a43SJulian Elischer 
579b50d902SRodney W. Grimes #include <ctype.h>
585d422d6aSPhilippe Charnier #include <err.h>
599b50d902SRodney W. Grimes #include <errno.h>
609b50d902SRodney W. Grimes #include <kvm.h>
619b50d902SRodney W. Grimes #include <limits.h>
629b50d902SRodney W. Grimes #include <netdb.h>
639b50d902SRodney W. Grimes #include <nlist.h>
649b50d902SRodney W. Grimes #include <paths.h>
659b50d902SRodney W. Grimes #include <stdio.h>
669b50d902SRodney W. Grimes #include <stdlib.h>
679b50d902SRodney W. Grimes #include <string.h>
689b50d902SRodney W. Grimes #include <unistd.h>
699b50d902SRodney W. Grimes #include "netstat.h"
709b50d902SRodney W. Grimes 
71c6620a13SPoul-Henning Kamp static struct nlist nl[] = {
724f81ef50SGarrett Wollman #define	N_IFNET		0
739b50d902SRodney W. Grimes 	{ "_ifnet" },
744f81ef50SGarrett Wollman #define	N_IMP		1
759b50d902SRodney W. Grimes 	{ "_imp_softc" },
764f81ef50SGarrett Wollman #define	N_RTSTAT	2
779b50d902SRodney W. Grimes 	{ "_rtstat" },
784f81ef50SGarrett Wollman #define	N_UNIXSW	3
79f3117d66SPeter Wemm 	{ "_localsw" },
804f81ef50SGarrett Wollman #define N_IDP		4
819b50d902SRodney W. Grimes 	{ "_nspcb"},
824f81ef50SGarrett Wollman #define N_IDPSTAT	5
839b50d902SRodney W. Grimes 	{ "_idpstat"},
844f81ef50SGarrett Wollman #define N_SPPSTAT	6
859b50d902SRodney W. Grimes 	{ "_spp_istat"},
864f81ef50SGarrett Wollman #define N_NSERR		7
879b50d902SRodney W. Grimes 	{ "_ns_errstat"},
884f81ef50SGarrett Wollman #define	N_CLNPSTAT	8
899b50d902SRodney W. Grimes 	{ "_clnp_stat"},
904f81ef50SGarrett Wollman #define	IN_NOTUSED	9
919b50d902SRodney W. Grimes 	{ "_tp_inpcb" },
924f81ef50SGarrett Wollman #define	ISO_TP		10
939b50d902SRodney W. Grimes 	{ "_tp_refinfo" },
944f81ef50SGarrett Wollman #define	N_TPSTAT	11
959b50d902SRodney W. Grimes 	{ "_tp_stat" },
964f81ef50SGarrett Wollman #define	N_ESISSTAT	12
979b50d902SRodney W. Grimes 	{ "_esis_stat"},
984f81ef50SGarrett Wollman #define N_NIMP		13
999b50d902SRodney W. Grimes 	{ "_nimp"},
1004f81ef50SGarrett Wollman #define N_RTREE		14
1019b50d902SRodney W. Grimes 	{ "_rt_tables"},
1024f81ef50SGarrett Wollman #define N_CLTP		15
1039b50d902SRodney W. Grimes 	{ "_cltb"},
1044f81ef50SGarrett Wollman #define N_CLTPSTAT	16
1059b50d902SRodney W. Grimes 	{ "_cltpstat"},
1064f81ef50SGarrett Wollman #define	N_NFILE		17
1079b50d902SRodney W. Grimes 	{ "_nfile" },
1084f81ef50SGarrett Wollman #define	N_FILE		18
1099b50d902SRodney W. Grimes 	{ "_file" },
11067a451ccSBill Fenner #define N_MRTSTAT	19
1119b50d902SRodney W. Grimes 	{ "_mrtstat" },
11267a451ccSBill Fenner #define N_MFCTABLE	20
113ef105c25SGarrett Wollman 	{ "_mfctable" },
11467a451ccSBill Fenner #define N_VIFTABLE	21
1159b50d902SRodney W. Grimes 	{ "_viftable" },
11667a451ccSBill Fenner #define N_IPX		22
117cc6a66f2SJulian Elischer 	{ "_ipxpcb"},
11867a451ccSBill Fenner #define N_IPXSTAT	23
119cc6a66f2SJulian Elischer 	{ "_ipxstat"},
12067a451ccSBill Fenner #define N_SPXSTAT	24
121cc6a66f2SJulian Elischer 	{ "_spx_istat"},
12267a451ccSBill Fenner #define N_DDPSTAT	25
12363bf4575SJulian Elischer 	{ "_ddpstat"},
12467a451ccSBill Fenner #define N_DDPCB		26
12563bf4575SJulian Elischer 	{ "_ddpcb"},
1264cf49a43SJulian Elischer #define N_NGSOCKS	27
1274cf49a43SJulian Elischer 	{ "_ngsocklist"},
128cfa1ca9dSYoshinobu Inoue #define N_IP6STAT	28
129cfa1ca9dSYoshinobu Inoue 	{ "_ip6stat" },
130cfa1ca9dSYoshinobu Inoue #define N_ICMP6STAT	29
131cfa1ca9dSYoshinobu Inoue 	{ "_icmp6stat" },
132cfa1ca9dSYoshinobu Inoue #define N_IPSECSTAT	30
133cfa1ca9dSYoshinobu Inoue 	{ "_ipsecstat" },
134cfa1ca9dSYoshinobu Inoue #define N_IPSEC6STAT	31
135cfa1ca9dSYoshinobu Inoue 	{ "_ipsec6stat" },
136cfa1ca9dSYoshinobu Inoue #define N_PIM6STAT	32
137cfa1ca9dSYoshinobu Inoue 	{ "_pim6stat" },
138cfa1ca9dSYoshinobu Inoue #define N_MRT6PROTO	33
139cfa1ca9dSYoshinobu Inoue 	{ "_ip6_mrtproto" },
140cfa1ca9dSYoshinobu Inoue #define N_MRT6STAT	34
141cfa1ca9dSYoshinobu Inoue 	{ "_mrt6stat" },
142cfa1ca9dSYoshinobu Inoue #define N_MF6CTABLE	35
143cfa1ca9dSYoshinobu Inoue 	{ "_mf6ctable" },
144cfa1ca9dSYoshinobu Inoue #define N_MIF6TABLE	36
145cfa1ca9dSYoshinobu Inoue 	{ "_mif6table" },
1463b8a8567SJun-ichiro itojun Hagino #define N_PFKEYSTAT	37
1473b8a8567SJun-ichiro itojun Hagino 	{ "_pfkeystat" },
1487d56c0eeSAlexander Langer 	{ "" },
1499b50d902SRodney W. Grimes };
1509b50d902SRodney W. Grimes 
1519b50d902SRodney W. Grimes struct protox {
1529b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1539b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1549b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
1559b50d902SRodney W. Grimes 	void	(*pr_cblocks)();	/* control blocks printing routine */
1569b50d902SRodney W. Grimes 	void	(*pr_stats)();		/* statistics printing routine */
157cfa1ca9dSYoshinobu Inoue 	void	(*pr_istats)();		/* per/if statistics printing routine */
1589b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1594f81ef50SGarrett Wollman 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
1609b50d902SRodney W. Grimes } protox[] = {
1614f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
162cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1634f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
164cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1654f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
166cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1674f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
168cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1694f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
170cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1714f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
172cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
173cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
174cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSECSTAT,	1,	0,
175cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
176cfa1ca9dSYoshinobu Inoue #endif
17761793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
178cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
1799b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
180cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
1819b50d902SRodney W. Grimes };
1829b50d902SRodney W. Grimes 
183cfa1ca9dSYoshinobu Inoue #ifdef INET6
184cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
185cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
186cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
187cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
188cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
189cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IP6STAT,	1,	0,
190cfa1ca9dSYoshinobu Inoue 	  ip6_stats,	ip6_ifstats,	"ip6",	0 },
191cfa1ca9dSYoshinobu Inoue 	{ -1,		N_ICMP6STAT,	1,	0,
192cfa1ca9dSYoshinobu Inoue 	  icmp6_stats,	icmp6_ifstats,	"icmp6",0 },
193cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
194cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSEC6STAT,	1,	0,
195cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
196cfa1ca9dSYoshinobu Inoue #endif
197cfa1ca9dSYoshinobu Inoue #ifdef notyet
198cfa1ca9dSYoshinobu Inoue 	{ -1,		N_PIM6STAT,	1,	0,
199cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
200cfa1ca9dSYoshinobu Inoue #endif
20161793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
202cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
203cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		0,	0,
204cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
205cfa1ca9dSYoshinobu Inoue };
206cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
207cfa1ca9dSYoshinobu Inoue 
2083b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2093b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2103b8a8567SJun-ichiro itojun Hagino 	{ -1,		N_PFKEYSTAT,	1,	0,
2113b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2123b8a8567SJun-ichiro itojun Hagino 	{ -1,		-1,		0,	0,
2133b8a8567SJun-ichiro itojun Hagino 	  0,		NULL,		0,	0 }
2143b8a8567SJun-ichiro itojun Hagino };
2153b8a8567SJun-ichiro itojun Hagino #endif
2163b8a8567SJun-ichiro itojun Hagino 
21763bf4575SJulian Elischer struct protox atalkprotox[] = {
21863bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
219cfa1ca9dSYoshinobu Inoue 	  ddp_stats,	NULL,		"ddp" },
22063bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
221cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
22263bf4575SJulian Elischer };
22363bf4575SJulian Elischer 
2244cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2254cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
226cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"ctrl" },
2274cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
228cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"data" },
229cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
230cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2314cf49a43SJulian Elischer };
2324cf49a43SJulian Elischer 
233cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
234cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
235cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
236cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
237cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
238cc6a66f2SJulian Elischer 	{ -1,		-1,		0,	0,
239cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
240cc6a66f2SJulian Elischer };
241cc6a66f2SJulian Elischer 
242cbc17e71SGarrett Wollman #ifdef NS
2439b50d902SRodney W. Grimes struct protox nsprotox[] = {
2449b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
245cfa1ca9dSYoshinobu Inoue 	  idp_stats,	NULL,		"idp" },
2469b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
247cfa1ca9dSYoshinobu Inoue 	  spp_stats,	NULL,		"spp" },
2489b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
249cfa1ca9dSYoshinobu Inoue 	  nserr_stats,	NULL,		"ns_err" },
2509b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
251cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2529b50d902SRodney W. Grimes };
253cbc17e71SGarrett Wollman #endif
2549b50d902SRodney W. Grimes 
2550761cb29SGarrett Wollman #ifdef ISO
2569b50d902SRodney W. Grimes struct protox isoprotox[] = {
2579b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
258cfa1ca9dSYoshinobu Inoue 	  tp_stats,	NULL,		"tp" },
2599b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
260cfa1ca9dSYoshinobu Inoue 	  cltp_stats,	NULL,		"cltp" },
2619b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
262cfa1ca9dSYoshinobu Inoue 	  clnp_stats,	NULL,		"clnp"},
2639b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
264cfa1ca9dSYoshinobu Inoue 	  esis_stats,	NULL,		"esis"},
2659b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
266cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2679b50d902SRodney W. Grimes };
2680761cb29SGarrett Wollman #endif
2699b50d902SRodney W. Grimes 
270cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
271cfa1ca9dSYoshinobu Inoue 					 protox,
272cfa1ca9dSYoshinobu Inoue #ifdef INET6
273cfa1ca9dSYoshinobu Inoue 					 ip6protox,
274cfa1ca9dSYoshinobu Inoue #endif
2753b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2763b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2773b8a8567SJun-ichiro itojun Hagino #endif
278cfa1ca9dSYoshinobu Inoue 					 ipxprotox, atalkprotox,
279cbc17e71SGarrett Wollman #ifdef NS
280cbc17e71SGarrett Wollman 					 nsprotox,
281cbc17e71SGarrett Wollman #endif
2820761cb29SGarrett Wollman #ifdef ISO
2830761cb29SGarrett Wollman 					 isoprotox,
2840761cb29SGarrett Wollman #endif
2850761cb29SGarrett Wollman 					 NULL };
2869b50d902SRodney W. Grimes 
2879b50d902SRodney W. Grimes static void printproto __P((struct protox *, char *));
2889b50d902SRodney W. Grimes static void usage __P((void));
2899b50d902SRodney W. Grimes static struct protox *name2protox __P((char *));
2909b50d902SRodney W. Grimes static struct protox *knownname __P((char *));
2919b50d902SRodney W. Grimes 
292c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
293080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
294080b7f49SDag-Erling Smørgrav 
295080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
296080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
297080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
298080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
299080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
300080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
301080b7f49SDag-Erling Smørgrav int	lflag;		/* show routing table with use and ref */
302080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
303080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
304080b7f49SDag-Erling Smørgrav int	nflag;		/* show addresses numerically */
305080b7f49SDag-Erling Smørgrav int	pflag;		/* show given protocol */
306080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
307080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
308080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
309080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
310080b7f49SDag-Erling Smørgrav 
311080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
312080b7f49SDag-Erling Smørgrav 
313080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
314080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
315080b7f49SDag-Erling Smørgrav 
316080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3179b50d902SRodney W. Grimes 
3189b50d902SRodney W. Grimes int
3199b50d902SRodney W. Grimes main(argc, argv)
3209b50d902SRodney W. Grimes 	int argc;
3219b50d902SRodney W. Grimes 	char *argv[];
3229b50d902SRodney W. Grimes {
3234cf49a43SJulian Elischer 	register struct protox *tp = NULL;  /* for printing cblocks & stats */
3249b50d902SRodney W. Grimes 	int ch;
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3279b50d902SRodney W. Grimes 
328080b7f49SDag-Erling Smørgrav 	while ((ch = getopt(argc, argv, "Aabdf:ghI:lLiM:mN:np:rstuWw:")) != -1)
3299b50d902SRodney W. Grimes 		switch(ch) {
3309b50d902SRodney W. Grimes 		case 'A':
3319b50d902SRodney W. Grimes 			Aflag = 1;
3329b50d902SRodney W. Grimes 			break;
3339b50d902SRodney W. Grimes 		case 'a':
3349b50d902SRodney W. Grimes 			aflag = 1;
3359b50d902SRodney W. Grimes 			break;
336e1e293a5SDavid Greenman 		case 'b':
337e1e293a5SDavid Greenman 			bflag = 1;
338e1e293a5SDavid Greenman 			break;
3399b50d902SRodney W. Grimes 		case 'd':
3409b50d902SRodney W. Grimes 			dflag = 1;
3419b50d902SRodney W. Grimes 			break;
3429b50d902SRodney W. Grimes 		case 'f':
343cbc17e71SGarrett Wollman #ifdef NS
3449b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3459b50d902SRodney W. Grimes 				af = AF_NS;
346cbc17e71SGarrett Wollman 			else
347cbc17e71SGarrett Wollman #endif
348cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
349cc6a66f2SJulian Elischer 				af = AF_IPX;
3509b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3519b50d902SRodney W. Grimes 				af = AF_INET;
352cfa1ca9dSYoshinobu Inoue #ifdef INET6
353cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
354cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
355cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3563b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3573b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3583b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3593b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
3609b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3619b50d902SRodney W. Grimes 				af = AF_UNIX;
36263bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
36363bf4575SJulian Elischer 				af = AF_APPLETALK;
3644cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3654cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3664cf49a43SJulian Elischer 				af = AF_NETGRAPH;
3670761cb29SGarrett Wollman #ifdef ISO
3689b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3699b50d902SRodney W. Grimes 				af = AF_ISO;
3700761cb29SGarrett Wollman #endif
3719b50d902SRodney W. Grimes 			else {
37251e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3739b50d902SRodney W. Grimes 			}
3749b50d902SRodney W. Grimes 			break;
3759b50d902SRodney W. Grimes 		case 'g':
3769b50d902SRodney W. Grimes 			gflag = 1;
3779b50d902SRodney W. Grimes 			break;
3789b50d902SRodney W. Grimes 		case 'I': {
3799b50d902SRodney W. Grimes 			char *cp;
3809b50d902SRodney W. Grimes 
3819b50d902SRodney W. Grimes 			iflag = 1;
3829b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3839b50d902SRodney W. Grimes 				continue;
3849b50d902SRodney W. Grimes 			unit = atoi(cp);
3859b50d902SRodney W. Grimes 			break;
3869b50d902SRodney W. Grimes 		}
3879b50d902SRodney W. Grimes 		case 'i':
3889b50d902SRodney W. Grimes 			iflag = 1;
3899b50d902SRodney W. Grimes 			break;
390cfa1ca9dSYoshinobu Inoue 		case 'l':
391cfa1ca9dSYoshinobu Inoue 			lflag = 1;
392cfa1ca9dSYoshinobu Inoue 			break;
393ac55add0SGuido van Rooij 		case 'L':
394ac55add0SGuido van Rooij 			Lflag = 1;
395ac55add0SGuido van Rooij 			break;
3969b50d902SRodney W. Grimes 		case 'M':
3979b50d902SRodney W. Grimes 			memf = optarg;
3989b50d902SRodney W. Grimes 			break;
3999b50d902SRodney W. Grimes 		case 'm':
4009b50d902SRodney W. Grimes 			mflag = 1;
4019b50d902SRodney W. Grimes 			break;
4029b50d902SRodney W. Grimes 		case 'N':
4039b50d902SRodney W. Grimes 			nlistf = optarg;
4049b50d902SRodney W. Grimes 			break;
4059b50d902SRodney W. Grimes 		case 'n':
4069b50d902SRodney W. Grimes 			nflag = 1;
4079b50d902SRodney W. Grimes 			break;
4089b50d902SRodney W. Grimes 		case 'p':
4099b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
41051e7d42cSGarrett Wollman 				errx(1,
41151e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
41251e7d42cSGarrett Wollman 				     optarg);
4139b50d902SRodney W. Grimes 			}
4149b50d902SRodney W. Grimes 			pflag = 1;
4159b50d902SRodney W. Grimes 			break;
4169b50d902SRodney W. Grimes 		case 'r':
4179b50d902SRodney W. Grimes 			rflag = 1;
4189b50d902SRodney W. Grimes 			break;
4199b50d902SRodney W. Grimes 		case 's':
4209b50d902SRodney W. Grimes 			++sflag;
4219b50d902SRodney W. Grimes 			break;
4229b50d902SRodney W. Grimes 		case 't':
4239b50d902SRodney W. Grimes 			tflag = 1;
4249b50d902SRodney W. Grimes 			break;
4259b50d902SRodney W. Grimes 		case 'u':
4269b50d902SRodney W. Grimes 			af = AF_UNIX;
4279b50d902SRodney W. Grimes 			break;
428080b7f49SDag-Erling Smørgrav 		case 'W':
429080b7f49SDag-Erling Smørgrav 			Wflag = 1;
430080b7f49SDag-Erling Smørgrav 			break;
4319b50d902SRodney W. Grimes 		case 'w':
4329b50d902SRodney W. Grimes 			interval = atoi(optarg);
4339b50d902SRodney W. Grimes 			iflag = 1;
4349b50d902SRodney W. Grimes 			break;
4359b50d902SRodney W. Grimes 		case '?':
4369b50d902SRodney W. Grimes 		default:
4379b50d902SRodney W. Grimes 			usage();
4389b50d902SRodney W. Grimes 		}
4399b50d902SRodney W. Grimes 	argv += optind;
4409b50d902SRodney W. Grimes 	argc -= optind;
4419b50d902SRodney W. Grimes 
4429b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4439b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4449b50d902SRodney W. Grimes 	if (*argv) {
4459b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4469b50d902SRodney W. Grimes 			interval = atoi(*argv);
4479b50d902SRodney W. Grimes 			if (interval <= 0)
4489b50d902SRodney W. Grimes 				usage();
4499b50d902SRodney W. Grimes 			++argv;
4509b50d902SRodney W. Grimes 			iflag = 1;
4519b50d902SRodney W. Grimes 		}
4529b50d902SRodney W. Grimes 		if (*argv) {
4539b50d902SRodney W. Grimes 			nlistf = *argv;
4549b50d902SRodney W. Grimes 			if (*++argv)
4559b50d902SRodney W. Grimes 				memf = *argv;
4569b50d902SRodney W. Grimes 		}
4579b50d902SRodney W. Grimes 	}
4589b50d902SRodney W. Grimes #endif
4599b50d902SRodney W. Grimes 
4609b50d902SRodney W. Grimes 	/*
4619b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4629b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4639b50d902SRodney W. Grimes 	 */
4649b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4659b50d902SRodney W. Grimes 		setgid(getgid());
4669b50d902SRodney W. Grimes 
4679b50d902SRodney W. Grimes 	if (mflag) {
4684f81ef50SGarrett Wollman 		mbpr();
4699b50d902SRodney W. Grimes 		exit(0);
4709b50d902SRodney W. Grimes 	}
4719b50d902SRodney W. Grimes 	if (pflag) {
472cfa1ca9dSYoshinobu Inoue 		if (iflag && tp->pr_istats) {
473cfa1ca9dSYoshinobu Inoue 			kread(0, 0, 0);
474cfa1ca9dSYoshinobu Inoue 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
475cfa1ca9dSYoshinobu Inoue 			exit(0);
476cfa1ca9dSYoshinobu Inoue 		}
477c6620a13SPoul-Henning Kamp 		if (!tp->pr_stats) {
4789b50d902SRodney W. Grimes 			printf("%s: no stats routine\n", tp->pr_name);
4799b50d902SRodney W. Grimes 			exit(0);
4809b50d902SRodney W. Grimes 		}
481c6620a13SPoul-Henning Kamp 		if (tp->pr_usesysctl) {
482c6620a13SPoul-Henning Kamp 			(*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name);
483c6620a13SPoul-Henning Kamp 		} else {
484c6620a13SPoul-Henning Kamp 			kread(0, 0, 0);
485c6620a13SPoul-Henning Kamp 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
486c6620a13SPoul-Henning Kamp 					tp->pr_name);
487c6620a13SPoul-Henning Kamp 		}
488c6620a13SPoul-Henning Kamp 		exit(0);
489c6620a13SPoul-Henning Kamp 	}
490cc63cd56SPeter Wemm #if 0
4919b50d902SRodney W. Grimes 	/*
4929b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
4939b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
4949b50d902SRodney W. Grimes 	 */
4959b50d902SRodney W. Grimes 	sethostent(1);
4969b50d902SRodney W. Grimes 	setnetent(1);
497cc63cd56SPeter Wemm #else
498cc63cd56SPeter Wemm 	/*
499cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
500cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
501cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
502cc63cd56SPeter Wemm 	 */
503cc63cd56SPeter Wemm #endif
5049b50d902SRodney W. Grimes 	if (iflag) {
505fc60ab7bSYoshinobu Inoue 		if (sflag && af != AF_UNSPEC)
506cfa1ca9dSYoshinobu Inoue 			goto protostat;
507cfa1ca9dSYoshinobu Inoue 
508c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
509cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5109b50d902SRodney W. Grimes 		exit(0);
5119b50d902SRodney W. Grimes 	}
5129b50d902SRodney W. Grimes 	if (rflag) {
513c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
5149b50d902SRodney W. Grimes 		if (sflag)
5159b50d902SRodney W. Grimes 			rt_stats(nl[N_RTSTAT].n_value);
5169b50d902SRodney W. Grimes 		else
5179b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5189b50d902SRodney W. Grimes 		exit(0);
5199b50d902SRodney W. Grimes 	}
5209b50d902SRodney W. Grimes 	if (gflag) {
521c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
522cfa1ca9dSYoshinobu Inoue 		if (sflag) {
523cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
52467a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
525cfa1ca9dSYoshinobu Inoue #ifdef INET6
526cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
527cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
528cfa1ca9dSYoshinobu Inoue #endif
529cfa1ca9dSYoshinobu Inoue 		} else {
530cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
53167a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5329b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
533cfa1ca9dSYoshinobu Inoue #ifdef INET6
534cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
535cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
536cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
537cfa1ca9dSYoshinobu Inoue #endif
538cfa1ca9dSYoshinobu Inoue 		}
5399b50d902SRodney W. Grimes 		exit(0);
5409b50d902SRodney W. Grimes 	}
541cfa1ca9dSYoshinobu Inoue 
542cfa1ca9dSYoshinobu Inoue   protostat:
543cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
544cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5459b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
546cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
547cfa1ca9dSYoshinobu Inoue #ifdef INET6
548cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
549cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
550cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
551cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5523b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5533b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5543b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5553b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5563b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
557e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
558e30e913cSJohn Hay 		kread(0, 0, 0);
559cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
560cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
561e30e913cSJohn Hay 	}
56263bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
56363bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
56463bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5654cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5664cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5674cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
568cbc17e71SGarrett Wollman #ifdef NS
5699b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
5709b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
5719b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
572cbc17e71SGarrett Wollman #endif
5730761cb29SGarrett Wollman #ifdef ISO
5749b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
5759b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
5769b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
5770761cb29SGarrett Wollman #endif
578ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
5794f81ef50SGarrett Wollman 		unixpr();
5809b50d902SRodney W. Grimes 	exit(0);
5819b50d902SRodney W. Grimes }
5829b50d902SRodney W. Grimes 
5839b50d902SRodney W. Grimes /*
5849b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
5859b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
5869b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
5879b50d902SRodney W. Grimes  */
5889b50d902SRodney W. Grimes static void
5899b50d902SRodney W. Grimes printproto(tp, name)
5909b50d902SRodney W. Grimes 	register struct protox *tp;
5919b50d902SRodney W. Grimes 	char *name;
5929b50d902SRodney W. Grimes {
5939b50d902SRodney W. Grimes 	void (*pr)();
5949b50d902SRodney W. Grimes 	u_long off;
5959b50d902SRodney W. Grimes 
5969b50d902SRodney W. Grimes 	if (sflag) {
597cfa1ca9dSYoshinobu Inoue 		if (iflag) {
598cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
599cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
600cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
601cfa1ca9dSYoshinobu Inoue 			return;
602cfa1ca9dSYoshinobu Inoue 		}
603cfa1ca9dSYoshinobu Inoue 		else {
6049b50d902SRodney W. Grimes 			pr = tp->pr_stats;
6054f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6064f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
607cfa1ca9dSYoshinobu Inoue 		}
6089b50d902SRodney W. Grimes 	} else {
6099b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
6104f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6114f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6129b50d902SRodney W. Grimes 	}
6139b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
614cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6159b50d902SRodney W. Grimes }
6169b50d902SRodney W. Grimes 
6179b50d902SRodney W. Grimes /*
6189b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6199b50d902SRodney W. Grimes  */
6209b50d902SRodney W. Grimes int
6219b50d902SRodney W. Grimes kread(addr, buf, size)
6229b50d902SRodney W. Grimes 	u_long addr;
6239b50d902SRodney W. Grimes 	char *buf;
6249b50d902SRodney W. Grimes 	int size;
6259b50d902SRodney W. Grimes {
6264f81ef50SGarrett Wollman 	if (kvmd == 0) {
62799453c6aSPoul-Henning Kamp 		/*
62899453c6aSPoul-Henning Kamp 		 * XXX.
62999453c6aSPoul-Henning Kamp 		 */
63099453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
63199453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
63299453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
63399453c6aSPoul-Henning Kamp 				if(nlistf)
63499453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
63599453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
63699453c6aSPoul-Henning Kamp 				else
63799453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
63899453c6aSPoul-Henning Kamp 			}
63999453c6aSPoul-Henning Kamp 
64099453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
64199453c6aSPoul-Henning Kamp 				if(nlistf)
64299453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
64399453c6aSPoul-Henning Kamp 				else
64499453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
64599453c6aSPoul-Henning Kamp 			}
64699453c6aSPoul-Henning Kamp 		} else {
64799453c6aSPoul-Henning Kamp 			warnx("kvm not available");
64899453c6aSPoul-Henning Kamp 			return(-1);
64999453c6aSPoul-Henning Kamp 		}
6504f81ef50SGarrett Wollman 	}
651c6620a13SPoul-Henning Kamp 	if (!buf)
652c6620a13SPoul-Henning Kamp 		return (0);
6539b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6543aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6559b50d902SRodney W. Grimes 		return (-1);
6569b50d902SRodney W. Grimes 	}
6579b50d902SRodney W. Grimes 	return (0);
6589b50d902SRodney W. Grimes }
6599b50d902SRodney W. Grimes 
6609b50d902SRodney W. Grimes char *
6619b50d902SRodney W. Grimes plural(n)
6629b50d902SRodney W. Grimes 	int n;
6639b50d902SRodney W. Grimes {
6649b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6659b50d902SRodney W. Grimes }
6669b50d902SRodney W. Grimes 
6679b50d902SRodney W. Grimes char *
6689b50d902SRodney W. Grimes plurales(n)
6699b50d902SRodney W. Grimes 	int n;
6709b50d902SRodney W. Grimes {
6719b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
6729b50d902SRodney W. Grimes }
6739b50d902SRodney W. Grimes 
6749b50d902SRodney W. Grimes /*
6759b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
6769b50d902SRodney W. Grimes  */
6779b50d902SRodney W. Grimes static struct protox *
6789b50d902SRodney W. Grimes knownname(name)
6799b50d902SRodney W. Grimes 	char *name;
6809b50d902SRodney W. Grimes {
6819b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
6829b50d902SRodney W. Grimes 
6839b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
6849b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
6859b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
6869b50d902SRodney W. Grimes 				return (tp);
6879b50d902SRodney W. Grimes 	return (NULL);
6889b50d902SRodney W. Grimes }
6899b50d902SRodney W. Grimes 
6909b50d902SRodney W. Grimes /*
6919b50d902SRodney W. Grimes  * Find the protox corresponding to name.
6929b50d902SRodney W. Grimes  */
6939b50d902SRodney W. Grimes static struct protox *
6949b50d902SRodney W. Grimes name2protox(name)
6959b50d902SRodney W. Grimes 	char *name;
6969b50d902SRodney W. Grimes {
6979b50d902SRodney W. Grimes 	struct protox *tp;
6989b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
6999b50d902SRodney W. Grimes 	struct protoent *p;
7009b50d902SRodney W. Grimes 
7019b50d902SRodney W. Grimes 	/*
7029b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7039b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7049b50d902SRodney W. Grimes 	 */
705cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7069b50d902SRodney W. Grimes 		return (tp);
7079b50d902SRodney W. Grimes 
7089b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
709cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7109b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7119b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7129b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7139b50d902SRodney W. Grimes 				endprotoent();
7149b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7159b50d902SRodney W. Grimes 			}
7169b50d902SRodney W. Grimes 	}
7179b50d902SRodney W. Grimes 	endprotoent();
7189b50d902SRodney W. Grimes 	return (NULL);
7199b50d902SRodney W. Grimes }
7209b50d902SRodney W. Grimes 
7219b50d902SRodney W. Grimes static void
7229b50d902SRodney W. Grimes usage()
7239b50d902SRodney W. Grimes {
7245d422d6aSPhilippe Charnier 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n",
725080b7f49SDag-Erling Smørgrav "usage: netstat [-AaLlnW] [-f address_family] [-M core] [-N system]",
7265f2cd675SYoshinobu Inoue "       netstat [-abdghilmnrs] [-f address_family] [-M core] [-N system]",
7275d422d6aSPhilippe Charnier "       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
7285d422d6aSPhilippe Charnier "       netstat [-M core] [-N system] [-p protocol]");
7299b50d902SRodney W. Grimes 	exit(1);
7309b50d902SRodney W. Grimes }
731