xref: /freebsd/usr.bin/netstat/main.c (revision c73d99b567e5585d0c584a80cdeb62544a418349)
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" },
148d15c5f56SRuslan Ermilov #define N_MBSTAT	38
149d15c5f56SRuslan Ermilov 	{ "_mbstat" },
150d15c5f56SRuslan Ermilov #define N_MBTYPES	39
151d15c5f56SRuslan Ermilov 	{ "_mbtypes" },
152d15c5f56SRuslan Ermilov #define N_NMBCLUSTERS	40
153d15c5f56SRuslan Ermilov 	{ "_nmbclusters" },
154d15c5f56SRuslan Ermilov #define N_NMBUFS	41
155d15c5f56SRuslan Ermilov 	{ "_nmbufs" },
15608442f8aSBosko Milekic #define	N_MBLIM		42
15708442f8aSBosko Milekic 	{ "_mbuf_limit" },
15808442f8aSBosko Milekic #define	N_CLLIM		43
15908442f8aSBosko Milekic 	{ "_clust_limit" },
16008442f8aSBosko Milekic #define	N_NCPUS		44
16108442f8aSBosko Milekic 	{ "_smp_cpus" },
16208442f8aSBosko Milekic #define	N_PAGESZ	45
16308442f8aSBosko Milekic 	{ "_pagesize" },
16408442f8aSBosko Milekic #define	N_MBPSTAT	46
16508442f8aSBosko Milekic 	{ "_mb_statpcpu" },
1667d56c0eeSAlexander Langer 	{ "" },
1679b50d902SRodney W. Grimes };
1689b50d902SRodney W. Grimes 
1699b50d902SRodney W. Grimes struct protox {
1709b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1719b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1729b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
1735e051718SAssar Westerlund 	void	(*pr_cblocks)(u_long, char *, int);
1745e051718SAssar Westerlund 					/* control blocks printing routine */
1755e051718SAssar Westerlund 	void	(*pr_stats)(u_long, char *, int);
1765e051718SAssar Westerlund 					/* statistics printing routine */
1775e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
1789b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1794f81ef50SGarrett Wollman 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
1809b50d902SRodney W. Grimes } protox[] = {
1814f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
182cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1834f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
184cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1854f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
186cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1874f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
188cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1894f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
190cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1914f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
192cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
193cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
194cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSECSTAT,	1,	0,
195cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
196cfa1ca9dSYoshinobu Inoue #endif
19761793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
198cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
1999b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
200cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2019b50d902SRodney W. Grimes };
2029b50d902SRodney W. Grimes 
203cfa1ca9dSYoshinobu Inoue #ifdef INET6
204cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
205cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
206cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
207cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
208cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
209cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IP6STAT,	1,	0,
210cfa1ca9dSYoshinobu Inoue 	  ip6_stats,	ip6_ifstats,	"ip6",	0 },
211cfa1ca9dSYoshinobu Inoue 	{ -1,		N_ICMP6STAT,	1,	0,
212cfa1ca9dSYoshinobu Inoue 	  icmp6_stats,	icmp6_ifstats,	"icmp6",0 },
213cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
214cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSEC6STAT,	1,	0,
215cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
216cfa1ca9dSYoshinobu Inoue #endif
217cfa1ca9dSYoshinobu Inoue #ifdef notyet
218cfa1ca9dSYoshinobu Inoue 	{ -1,		N_PIM6STAT,	1,	0,
219cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
220cfa1ca9dSYoshinobu Inoue #endif
22161793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
22233841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
22333841545SHajimu UMEMOTO 	{ -1,		-1,		1,	0,
224cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
225cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		0,	0,
226cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
227cfa1ca9dSYoshinobu Inoue };
228cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
229cfa1ca9dSYoshinobu Inoue 
2303b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2313b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2323b8a8567SJun-ichiro itojun Hagino 	{ -1,		N_PFKEYSTAT,	1,	0,
2333b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2343b8a8567SJun-ichiro itojun Hagino 	{ -1,		-1,		0,	0,
2353b8a8567SJun-ichiro itojun Hagino 	  0,		NULL,		0,	0 }
2363b8a8567SJun-ichiro itojun Hagino };
2373b8a8567SJun-ichiro itojun Hagino #endif
2383b8a8567SJun-ichiro itojun Hagino 
23963bf4575SJulian Elischer struct protox atalkprotox[] = {
24063bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
241cfa1ca9dSYoshinobu Inoue 	  ddp_stats,	NULL,		"ddp" },
24263bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
243cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
24463bf4575SJulian Elischer };
24563bf4575SJulian Elischer 
2464cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2474cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
248cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"ctrl" },
2494cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
250cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"data" },
251cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
252cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2534cf49a43SJulian Elischer };
2544cf49a43SJulian Elischer 
255cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
256cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
257cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
258cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
259cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
260cc6a66f2SJulian Elischer 	{ -1,		-1,		0,	0,
261cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
262cc6a66f2SJulian Elischer };
263cc6a66f2SJulian Elischer 
264cbc17e71SGarrett Wollman #ifdef NS
2659b50d902SRodney W. Grimes struct protox nsprotox[] = {
2669b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
267cfa1ca9dSYoshinobu Inoue 	  idp_stats,	NULL,		"idp" },
2689b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
269cfa1ca9dSYoshinobu Inoue 	  spp_stats,	NULL,		"spp" },
2709b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
271cfa1ca9dSYoshinobu Inoue 	  nserr_stats,	NULL,		"ns_err" },
2729b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
273cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2749b50d902SRodney W. Grimes };
275cbc17e71SGarrett Wollman #endif
2769b50d902SRodney W. Grimes 
2770761cb29SGarrett Wollman #ifdef ISO
2789b50d902SRodney W. Grimes struct protox isoprotox[] = {
2799b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
280cfa1ca9dSYoshinobu Inoue 	  tp_stats,	NULL,		"tp" },
2819b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
282cfa1ca9dSYoshinobu Inoue 	  cltp_stats,	NULL,		"cltp" },
2839b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
284cfa1ca9dSYoshinobu Inoue 	  clnp_stats,	NULL,		"clnp"},
2859b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
286cfa1ca9dSYoshinobu Inoue 	  esis_stats,	NULL,		"esis"},
2879b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
288cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2899b50d902SRodney W. Grimes };
2900761cb29SGarrett Wollman #endif
2919b50d902SRodney W. Grimes 
292cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
293cfa1ca9dSYoshinobu Inoue 					 protox,
294cfa1ca9dSYoshinobu Inoue #ifdef INET6
295cfa1ca9dSYoshinobu Inoue 					 ip6protox,
296cfa1ca9dSYoshinobu Inoue #endif
2973b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2983b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2993b8a8567SJun-ichiro itojun Hagino #endif
300cfa1ca9dSYoshinobu Inoue 					 ipxprotox, atalkprotox,
301cbc17e71SGarrett Wollman #ifdef NS
302cbc17e71SGarrett Wollman 					 nsprotox,
303cbc17e71SGarrett Wollman #endif
3040761cb29SGarrett Wollman #ifdef ISO
3050761cb29SGarrett Wollman 					 isoprotox,
3060761cb29SGarrett Wollman #endif
3070761cb29SGarrett Wollman 					 NULL };
3089b50d902SRodney W. Grimes 
3095e051718SAssar Westerlund static void printproto (struct protox *, char *);
3105e051718SAssar Westerlund static void usage (void);
3115e051718SAssar Westerlund static struct protox *name2protox (char *);
3125e051718SAssar Westerlund static struct protox *knownname (char *);
3139b50d902SRodney W. Grimes 
314c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
315080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
316080b7f49SDag-Erling Smørgrav 
317080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
318080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
319080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
320080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
321080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
322080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
323080b7f49SDag-Erling Smørgrav int	lflag;		/* show routing table with use and ref */
324080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
325080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
32665ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
32765ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
328cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
329080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
330080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
331080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
332080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
333c73d99b5SRuslan Ermilov int	zflag;		/* zero stats */
334080b7f49SDag-Erling Smørgrav 
335080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
336080b7f49SDag-Erling Smørgrav 
337080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
338080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
339080b7f49SDag-Erling Smørgrav 
340080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3419b50d902SRodney W. Grimes 
3429b50d902SRodney W. Grimes int
3439b50d902SRodney W. Grimes main(argc, argv)
3449b50d902SRodney W. Grimes 	int argc;
3459b50d902SRodney W. Grimes 	char *argv[];
3469b50d902SRodney W. Grimes {
3474cf49a43SJulian Elischer 	register struct protox *tp = NULL;  /* for printing cblocks & stats */
3489b50d902SRodney W. Grimes 	int ch;
3499b50d902SRodney W. Grimes 
3509b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3519b50d902SRodney W. Grimes 
352c73d99b5SRuslan Ermilov 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
3539b50d902SRodney W. Grimes 		switch(ch) {
3549b50d902SRodney W. Grimes 		case 'A':
3559b50d902SRodney W. Grimes 			Aflag = 1;
3569b50d902SRodney W. Grimes 			break;
3579b50d902SRodney W. Grimes 		case 'a':
3589b50d902SRodney W. Grimes 			aflag = 1;
3599b50d902SRodney W. Grimes 			break;
360e1e293a5SDavid Greenman 		case 'b':
361e1e293a5SDavid Greenman 			bflag = 1;
362e1e293a5SDavid Greenman 			break;
3639b50d902SRodney W. Grimes 		case 'd':
3649b50d902SRodney W. Grimes 			dflag = 1;
3659b50d902SRodney W. Grimes 			break;
3669b50d902SRodney W. Grimes 		case 'f':
367cbc17e71SGarrett Wollman #ifdef NS
3689b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3699b50d902SRodney W. Grimes 				af = AF_NS;
370cbc17e71SGarrett Wollman 			else
371cbc17e71SGarrett Wollman #endif
372cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
373cc6a66f2SJulian Elischer 				af = AF_IPX;
3749b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3759b50d902SRodney W. Grimes 				af = AF_INET;
376cfa1ca9dSYoshinobu Inoue #ifdef INET6
377cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
378cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
379cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3803b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3813b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3823b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3833b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
3849b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3859b50d902SRodney W. Grimes 				af = AF_UNIX;
38663bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
38763bf4575SJulian Elischer 				af = AF_APPLETALK;
3884cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3894cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3904cf49a43SJulian Elischer 				af = AF_NETGRAPH;
3910761cb29SGarrett Wollman #ifdef ISO
3929b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3939b50d902SRodney W. Grimes 				af = AF_ISO;
3940761cb29SGarrett Wollman #endif
3959b50d902SRodney W. Grimes 			else {
39651e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3979b50d902SRodney W. Grimes 			}
3989b50d902SRodney W. Grimes 			break;
3999b50d902SRodney W. Grimes 		case 'g':
4009b50d902SRodney W. Grimes 			gflag = 1;
4019b50d902SRodney W. Grimes 			break;
4029b50d902SRodney W. Grimes 		case 'I': {
4039b50d902SRodney W. Grimes 			char *cp;
4049b50d902SRodney W. Grimes 
4059b50d902SRodney W. Grimes 			iflag = 1;
4069b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
4079b50d902SRodney W. Grimes 				continue;
4089b50d902SRodney W. Grimes 			unit = atoi(cp);
4099b50d902SRodney W. Grimes 			break;
4109b50d902SRodney W. Grimes 		}
4119b50d902SRodney W. Grimes 		case 'i':
4129b50d902SRodney W. Grimes 			iflag = 1;
4139b50d902SRodney W. Grimes 			break;
414cfa1ca9dSYoshinobu Inoue 		case 'l':
415cfa1ca9dSYoshinobu Inoue 			lflag = 1;
416cfa1ca9dSYoshinobu Inoue 			break;
417ac55add0SGuido van Rooij 		case 'L':
418ac55add0SGuido van Rooij 			Lflag = 1;
419ac55add0SGuido van Rooij 			break;
4209b50d902SRodney W. Grimes 		case 'M':
4219b50d902SRodney W. Grimes 			memf = optarg;
4229b50d902SRodney W. Grimes 			break;
4239b50d902SRodney W. Grimes 		case 'm':
4249b50d902SRodney W. Grimes 			mflag = 1;
4259b50d902SRodney W. Grimes 			break;
4269b50d902SRodney W. Grimes 		case 'N':
4279b50d902SRodney W. Grimes 			nlistf = optarg;
4289b50d902SRodney W. Grimes 			break;
4299b50d902SRodney W. Grimes 		case 'n':
43065ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4319b50d902SRodney W. Grimes 			break;
4329b50d902SRodney W. Grimes 		case 'p':
4339b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
43451e7d42cSGarrett Wollman 				errx(1,
43551e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
43651e7d42cSGarrett Wollman 				     optarg);
4379b50d902SRodney W. Grimes 			}
4389b50d902SRodney W. Grimes 			pflag = 1;
4399b50d902SRodney W. Grimes 			break;
4409b50d902SRodney W. Grimes 		case 'r':
4419b50d902SRodney W. Grimes 			rflag = 1;
4429b50d902SRodney W. Grimes 			break;
4439b50d902SRodney W. Grimes 		case 's':
4449b50d902SRodney W. Grimes 			++sflag;
4459b50d902SRodney W. Grimes 			break;
44665ea0024SAssar Westerlund 		case 'S':
44765ea0024SAssar Westerlund 			numeric_addr = 1;
44865ea0024SAssar Westerlund 			break;
4499b50d902SRodney W. Grimes 		case 't':
4509b50d902SRodney W. Grimes 			tflag = 1;
4519b50d902SRodney W. Grimes 			break;
4529b50d902SRodney W. Grimes 		case 'u':
4539b50d902SRodney W. Grimes 			af = AF_UNIX;
4549b50d902SRodney W. Grimes 			break;
455080b7f49SDag-Erling Smørgrav 		case 'W':
456080b7f49SDag-Erling Smørgrav 			Wflag = 1;
457080b7f49SDag-Erling Smørgrav 			break;
4589b50d902SRodney W. Grimes 		case 'w':
4599b50d902SRodney W. Grimes 			interval = atoi(optarg);
4609b50d902SRodney W. Grimes 			iflag = 1;
4619b50d902SRodney W. Grimes 			break;
462c73d99b5SRuslan Ermilov 		case 'z':
463c73d99b5SRuslan Ermilov 			zflag = 1;
464c73d99b5SRuslan Ermilov 			break;
4659b50d902SRodney W. Grimes 		case '?':
4669b50d902SRodney W. Grimes 		default:
4679b50d902SRodney W. Grimes 			usage();
4689b50d902SRodney W. Grimes 		}
4699b50d902SRodney W. Grimes 	argv += optind;
4709b50d902SRodney W. Grimes 	argc -= optind;
4719b50d902SRodney W. Grimes 
4729b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4739b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4749b50d902SRodney W. Grimes 	if (*argv) {
4759b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4769b50d902SRodney W. Grimes 			interval = atoi(*argv);
4779b50d902SRodney W. Grimes 			if (interval <= 0)
4789b50d902SRodney W. Grimes 				usage();
4799b50d902SRodney W. Grimes 			++argv;
4809b50d902SRodney W. Grimes 			iflag = 1;
4819b50d902SRodney W. Grimes 		}
4829b50d902SRodney W. Grimes 		if (*argv) {
4839b50d902SRodney W. Grimes 			nlistf = *argv;
4849b50d902SRodney W. Grimes 			if (*++argv)
4859b50d902SRodney W. Grimes 				memf = *argv;
4869b50d902SRodney W. Grimes 		}
4879b50d902SRodney W. Grimes 	}
4889b50d902SRodney W. Grimes #endif
4899b50d902SRodney W. Grimes 
4909b50d902SRodney W. Grimes 	/*
4919b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4929b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4939b50d902SRodney W. Grimes 	 */
4949b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4959b50d902SRodney W. Grimes 		setgid(getgid());
4969b50d902SRodney W. Grimes 
4979b50d902SRodney W. Grimes 	if (mflag) {
498d15c5f56SRuslan Ermilov 		if (memf != NULL) {
499d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
500d15c5f56SRuslan Ermilov 				mbpr(nl[N_MBSTAT].n_value,
501d15c5f56SRuslan Ermilov 				    nl[N_MBTYPES].n_value,
502d15c5f56SRuslan Ermilov 				    nl[N_NMBCLUSTERS].n_value,
50308442f8aSBosko Milekic 				    nl[N_NMBUFS].n_value,
50408442f8aSBosko Milekic 				    nl[N_MBLIM].n_value,
50508442f8aSBosko Milekic 				    nl[N_CLLIM].n_value,
50608442f8aSBosko Milekic 				    nl[N_NCPUS].n_value,
50708442f8aSBosko Milekic 				    nl[N_PAGESZ].n_value,
50808442f8aSBosko Milekic 				    nl[N_MBPSTAT].n_value);
509d15c5f56SRuslan Ermilov 		} else
51008442f8aSBosko Milekic 			mbpr(0, 0, 0, 0, 0, 0, 0, 0, 0);
5119b50d902SRodney W. Grimes 		exit(0);
5129b50d902SRodney W. Grimes 	}
513cc63cd56SPeter Wemm #if 0
5149b50d902SRodney W. Grimes 	/*
5159b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
5169b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
5179b50d902SRodney W. Grimes 	 */
5189b50d902SRodney W. Grimes 	sethostent(1);
5199b50d902SRodney W. Grimes 	setnetent(1);
520cc63cd56SPeter Wemm #else
521cc63cd56SPeter Wemm 	/*
522cc63cd56SPeter Wemm 	 * This does not make sense any more with DNS being default over
523cc63cd56SPeter Wemm 	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
524cc63cd56SPeter Wemm 	 * used for the queries, which is slower.
525cc63cd56SPeter Wemm 	 */
526cc63cd56SPeter Wemm #endif
527cf5e44f8SRuslan Ermilov 	if (iflag && !sflag) {
528c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
529cfa1ca9dSYoshinobu Inoue 		intpr(interval, nl[N_IFNET].n_value, NULL);
5309b50d902SRodney W. Grimes 		exit(0);
5319b50d902SRodney W. Grimes 	}
5329b50d902SRodney W. Grimes 	if (rflag) {
533c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
5349b50d902SRodney W. Grimes 		if (sflag)
5359b50d902SRodney W. Grimes 			rt_stats(nl[N_RTSTAT].n_value);
5369b50d902SRodney W. Grimes 		else
5379b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
5389b50d902SRodney W. Grimes 		exit(0);
5399b50d902SRodney W. Grimes 	}
5409b50d902SRodney W. Grimes 	if (gflag) {
541c6620a13SPoul-Henning Kamp 		kread(0, 0, 0);
542cfa1ca9dSYoshinobu Inoue 		if (sflag) {
543cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
54467a451ccSBill Fenner 				mrt_stats(nl[N_MRTSTAT].n_value);
545cfa1ca9dSYoshinobu Inoue #ifdef INET6
546cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
547cfa1ca9dSYoshinobu Inoue 				mrt6_stats(nl[N_MRT6STAT].n_value);
548cfa1ca9dSYoshinobu Inoue #endif
549cfa1ca9dSYoshinobu Inoue 		} else {
550cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET || af == AF_UNSPEC)
55167a451ccSBill Fenner 				mroutepr(nl[N_MFCTABLE].n_value,
5529b50d902SRodney W. Grimes 					 nl[N_VIFTABLE].n_value);
553cfa1ca9dSYoshinobu Inoue #ifdef INET6
554cfa1ca9dSYoshinobu Inoue 			if (af == AF_INET6 || af == AF_UNSPEC)
555cfa1ca9dSYoshinobu Inoue 				mroute6pr(nl[N_MF6CTABLE].n_value,
556cfa1ca9dSYoshinobu Inoue 					  nl[N_MIF6TABLE].n_value);
557cfa1ca9dSYoshinobu Inoue #endif
558cfa1ca9dSYoshinobu Inoue 		}
5599b50d902SRodney W. Grimes 		exit(0);
5609b50d902SRodney W. Grimes 	}
561cfa1ca9dSYoshinobu Inoue 
562cfa1ca9dSYoshinobu Inoue 	kread(0, 0, 0);
563cf5e44f8SRuslan Ermilov 	if (tp) {
564cf5e44f8SRuslan Ermilov 		printproto(tp, tp->pr_name);
565cf5e44f8SRuslan Ermilov 		exit(0);
566cf5e44f8SRuslan Ermilov 	}
567cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET || af == AF_UNSPEC)
5689b50d902SRodney W. Grimes 		for (tp = protox; tp->pr_name; tp++)
569cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
570cfa1ca9dSYoshinobu Inoue #ifdef INET6
571cfa1ca9dSYoshinobu Inoue 	if (af == AF_INET6 || af == AF_UNSPEC)
572cfa1ca9dSYoshinobu Inoue 		for (tp = ip6protox; tp->pr_name; tp++)
573cfa1ca9dSYoshinobu Inoue 			printproto(tp, tp->pr_name);
574cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
5753b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
5763b8a8567SJun-ichiro itojun Hagino 	if (af == PF_KEY || af == AF_UNSPEC)
5773b8a8567SJun-ichiro itojun Hagino 		for (tp = pfkeyprotox; tp->pr_name; tp++)
5783b8a8567SJun-ichiro itojun Hagino 			printproto(tp, tp->pr_name);
5793b8a8567SJun-ichiro itojun Hagino #endif /*IPSEC*/
580e30e913cSJohn Hay 	if (af == AF_IPX || af == AF_UNSPEC) {
581e30e913cSJohn Hay 		kread(0, 0, 0);
582cc6a66f2SJulian Elischer 		for (tp = ipxprotox; tp->pr_name; tp++)
583cc6a66f2SJulian Elischer 			printproto(tp, tp->pr_name);
584e30e913cSJohn Hay 	}
58563bf4575SJulian Elischer 	if (af == AF_APPLETALK || af == AF_UNSPEC)
58663bf4575SJulian Elischer 		for (tp = atalkprotox; tp->pr_name; tp++)
58763bf4575SJulian Elischer 			printproto(tp, tp->pr_name);
5884cf49a43SJulian Elischer 	if (af == AF_NETGRAPH || af == AF_UNSPEC)
5894cf49a43SJulian Elischer 		for (tp = netgraphprotox; tp->pr_name; tp++)
5904cf49a43SJulian Elischer 			printproto(tp, tp->pr_name);
591cbc17e71SGarrett Wollman #ifdef NS
5929b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
5939b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
5949b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
595cbc17e71SGarrett Wollman #endif
5960761cb29SGarrett Wollman #ifdef ISO
5979b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
5989b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
5999b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
6000761cb29SGarrett Wollman #endif
601ac55add0SGuido van Rooij 	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
6024f81ef50SGarrett Wollman 		unixpr();
6039b50d902SRodney W. Grimes 	exit(0);
6049b50d902SRodney W. Grimes }
6059b50d902SRodney W. Grimes 
6069b50d902SRodney W. Grimes /*
6079b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
6089b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
6099b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
6109b50d902SRodney W. Grimes  */
6119b50d902SRodney W. Grimes static void
6129b50d902SRodney W. Grimes printproto(tp, name)
6139b50d902SRodney W. Grimes 	register struct protox *tp;
6149b50d902SRodney W. Grimes 	char *name;
6159b50d902SRodney W. Grimes {
616f964d60dSAssar Westerlund 	void (*pr)(u_long, char *, int);
6179b50d902SRodney W. Grimes 	u_long off;
6189b50d902SRodney W. Grimes 
6199b50d902SRodney W. Grimes 	if (sflag) {
620cfa1ca9dSYoshinobu Inoue 		if (iflag) {
621cfa1ca9dSYoshinobu Inoue 			if (tp->pr_istats)
622cfa1ca9dSYoshinobu Inoue 				intpr(interval, nl[N_IFNET].n_value,
623cfa1ca9dSYoshinobu Inoue 				      tp->pr_istats);
624cf5e44f8SRuslan Ermilov 			else if (pflag)
625cf5e44f8SRuslan Ermilov 				printf("%s: no per-interface stats routine\n",
626cf5e44f8SRuslan Ermilov 				    tp->pr_name);
627cfa1ca9dSYoshinobu Inoue 			return;
628cfa1ca9dSYoshinobu Inoue 		}
629cfa1ca9dSYoshinobu Inoue 		else {
6309b50d902SRodney W. Grimes 			pr = tp->pr_stats;
631cf5e44f8SRuslan Ermilov 			if (!pr) {
632cf5e44f8SRuslan Ermilov 				if (pflag)
633cf5e44f8SRuslan Ermilov 					printf("%s: no stats routine\n",
634cf5e44f8SRuslan Ermilov 					    tp->pr_name);
635cf5e44f8SRuslan Ermilov 				return;
636cf5e44f8SRuslan Ermilov 			}
6374f81ef50SGarrett Wollman 			off = tp->pr_usesysctl ? tp->pr_usesysctl
6384f81ef50SGarrett Wollman 				: nl[tp->pr_sindex].n_value;
639cfa1ca9dSYoshinobu Inoue 		}
6409b50d902SRodney W. Grimes 	} else {
6419b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
642cf5e44f8SRuslan Ermilov 		if (!pr) {
643cf5e44f8SRuslan Ermilov 			if (pflag)
644cf5e44f8SRuslan Ermilov 				printf("%s: no PCB routine\n", tp->pr_name);
645cf5e44f8SRuslan Ermilov 			return;
646cf5e44f8SRuslan Ermilov 		}
6474f81ef50SGarrett Wollman 		off = tp->pr_usesysctl ? tp->pr_usesysctl
6484f81ef50SGarrett Wollman 			: nl[tp->pr_index].n_value;
6499b50d902SRodney W. Grimes 	}
6509b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
651cfa1ca9dSYoshinobu Inoue 		(*pr)(off, name, af);
6529b50d902SRodney W. Grimes }
6539b50d902SRodney W. Grimes 
6549b50d902SRodney W. Grimes /*
6559b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
6569b50d902SRodney W. Grimes  */
6579b50d902SRodney W. Grimes int
6585e051718SAssar Westerlund kread(u_long addr, char *buf, int size)
6599b50d902SRodney W. Grimes {
6604f81ef50SGarrett Wollman 	if (kvmd == 0) {
66199453c6aSPoul-Henning Kamp 		/*
66299453c6aSPoul-Henning Kamp 		 * XXX.
66399453c6aSPoul-Henning Kamp 		 */
66499453c6aSPoul-Henning Kamp 		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
66599453c6aSPoul-Henning Kamp 		if (kvmd != NULL) {
66699453c6aSPoul-Henning Kamp 			if (kvm_nlist(kvmd, nl) < 0) {
66799453c6aSPoul-Henning Kamp 				if(nlistf)
66899453c6aSPoul-Henning Kamp 					errx(1, "%s: kvm_nlist: %s", nlistf,
66999453c6aSPoul-Henning Kamp 					     kvm_geterr(kvmd));
67099453c6aSPoul-Henning Kamp 				else
67199453c6aSPoul-Henning Kamp 					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
67299453c6aSPoul-Henning Kamp 			}
67399453c6aSPoul-Henning Kamp 
67499453c6aSPoul-Henning Kamp 			if (nl[0].n_type == 0) {
67599453c6aSPoul-Henning Kamp 				if(nlistf)
67699453c6aSPoul-Henning Kamp 					errx(1, "%s: no namelist", nlistf);
67799453c6aSPoul-Henning Kamp 				else
67899453c6aSPoul-Henning Kamp 					errx(1, "no namelist");
67999453c6aSPoul-Henning Kamp 			}
68099453c6aSPoul-Henning Kamp 		} else {
68199453c6aSPoul-Henning Kamp 			warnx("kvm not available");
68299453c6aSPoul-Henning Kamp 			return(-1);
68399453c6aSPoul-Henning Kamp 		}
6844f81ef50SGarrett Wollman 	}
685c6620a13SPoul-Henning Kamp 	if (!buf)
686c6620a13SPoul-Henning Kamp 		return (0);
6879b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
6883aa80b1dSDavid Greenman 		warnx("%s", kvm_geterr(kvmd));
6899b50d902SRodney W. Grimes 		return (-1);
6909b50d902SRodney W. Grimes 	}
6919b50d902SRodney W. Grimes 	return (0);
6929b50d902SRodney W. Grimes }
6939b50d902SRodney W. Grimes 
6949b50d902SRodney W. Grimes char *
6955e051718SAssar Westerlund plural(int n)
6969b50d902SRodney W. Grimes {
6979b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
6989b50d902SRodney W. Grimes }
6999b50d902SRodney W. Grimes 
7009b50d902SRodney W. Grimes char *
7015e051718SAssar Westerlund plurales(int n)
7029b50d902SRodney W. Grimes {
7039b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
7049b50d902SRodney W. Grimes }
7059b50d902SRodney W. Grimes 
7069b50d902SRodney W. Grimes /*
7079b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
7089b50d902SRodney W. Grimes  */
7099b50d902SRodney W. Grimes static struct protox *
7105e051718SAssar Westerlund knownname(char *name)
7119b50d902SRodney W. Grimes {
7129b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
7139b50d902SRodney W. Grimes 
7149b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
7159b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
7169b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
7179b50d902SRodney W. Grimes 				return (tp);
7189b50d902SRodney W. Grimes 	return (NULL);
7199b50d902SRodney W. Grimes }
7209b50d902SRodney W. Grimes 
7219b50d902SRodney W. Grimes /*
7229b50d902SRodney W. Grimes  * Find the protox corresponding to name.
7239b50d902SRodney W. Grimes  */
7249b50d902SRodney W. Grimes static struct protox *
7255e051718SAssar Westerlund name2protox(char *name)
7269b50d902SRodney W. Grimes {
7279b50d902SRodney W. Grimes 	struct protox *tp;
7289b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
7299b50d902SRodney W. Grimes 	struct protoent *p;
7309b50d902SRodney W. Grimes 
7319b50d902SRodney W. Grimes 	/*
7329b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
7339b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
7349b50d902SRodney W. Grimes 	 */
735cfa1ca9dSYoshinobu Inoue 	if ((tp = knownname(name)) != NULL)
7369b50d902SRodney W. Grimes 		return (tp);
7379b50d902SRodney W. Grimes 
7389b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
739cfa1ca9dSYoshinobu Inoue 	while ((p = getprotoent()) != NULL) {
7409b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
7419b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
7429b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
7439b50d902SRodney W. Grimes 				endprotoent();
7449b50d902SRodney W. Grimes 				return (knownname(p->p_name));
7459b50d902SRodney W. Grimes 			}
7469b50d902SRodney W. Grimes 	}
7479b50d902SRodney W. Grimes 	endprotoent();
7489b50d902SRodney W. Grimes 	return (NULL);
7499b50d902SRodney W. Grimes }
7509b50d902SRodney W. Grimes 
7519b50d902SRodney W. Grimes static void
7525e051718SAssar Westerlund usage(void)
7539b50d902SRodney W. Grimes {
754d15c5f56SRuslan Ermilov 	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
755080b7f49SDag-Erling Smørgrav "usage: netstat [-AaLlnW] [-f address_family] [-M core] [-N system]",
75665ea0024SAssar Westerlund "       netstat [-abdgilnrsS] [-f address_family] [-M core] [-N system]",
7575d422d6aSPhilippe Charnier "       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
758d15c5f56SRuslan Ermilov "       netstat -m [-M core] [-N system]",
7595d422d6aSPhilippe Charnier "       netstat [-M core] [-N system] [-p protocol]");
7609b50d902SRodney W. Grimes 	exit(1);
7619b50d902SRodney W. Grimes }
762