xref: /freebsd/usr.bin/netstat/main.c (revision 5e0517186d418529323a5eb007287145e4fd3b50)
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" },
1567d56c0eeSAlexander Langer 	{ "" },
1579b50d902SRodney W. Grimes };
1589b50d902SRodney W. Grimes 
1599b50d902SRodney W. Grimes struct protox {
1609b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1619b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1629b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
1635e051718SAssar Westerlund 	void	(*pr_cblocks)(u_long, char *, int);
1645e051718SAssar Westerlund 					/* control blocks printing routine */
1655e051718SAssar Westerlund 	void	(*pr_stats)(u_long, char *, int);
1665e051718SAssar Westerlund 					/* statistics printing routine */
1675e051718SAssar Westerlund 	void	(*pr_istats)(char *);	/* per/if statistics printing routine */
1689b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1694f81ef50SGarrett Wollman 	int	pr_usesysctl;		/* true if we use sysctl, not kvm */
1709b50d902SRodney W. Grimes } protox[] = {
1714f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
172cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
1734f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
174cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
1754f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
176cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"divert",IPPROTO_DIVERT },
1774f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
178cfa1ca9dSYoshinobu Inoue 	  ip_stats,	NULL,		"ip",	IPPROTO_RAW },
1794f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
180cfa1ca9dSYoshinobu Inoue 	  icmp_stats,	NULL,		"icmp",	IPPROTO_ICMP },
1814f81ef50SGarrett Wollman 	{ -1,		-1,		1,	protopr,
182cfa1ca9dSYoshinobu Inoue 	  igmp_stats,	NULL,		"igmp",	IPPROTO_IGMP },
183cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
184cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSECSTAT,	1,	0,
185cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec",	0},
186cfa1ca9dSYoshinobu Inoue #endif
18761793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
188cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
1899b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
190cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
1919b50d902SRodney W. Grimes };
1929b50d902SRodney W. Grimes 
193cfa1ca9dSYoshinobu Inoue #ifdef INET6
194cfa1ca9dSYoshinobu Inoue struct protox ip6protox[] = {
195cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
196cfa1ca9dSYoshinobu Inoue 	  tcp_stats,	NULL,		"tcp",	IPPROTO_TCP },
197cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		1,	protopr,
198cfa1ca9dSYoshinobu Inoue 	  udp_stats,	NULL,		"udp",	IPPROTO_UDP },
199cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IP6STAT,	1,	0,
200cfa1ca9dSYoshinobu Inoue 	  ip6_stats,	ip6_ifstats,	"ip6",	0 },
201cfa1ca9dSYoshinobu Inoue 	{ -1,		N_ICMP6STAT,	1,	0,
202cfa1ca9dSYoshinobu Inoue 	  icmp6_stats,	icmp6_ifstats,	"icmp6",0 },
203cfa1ca9dSYoshinobu Inoue #ifdef IPSEC
204cfa1ca9dSYoshinobu Inoue 	{ -1,		N_IPSEC6STAT,	1,	0,
205cfa1ca9dSYoshinobu Inoue 	  ipsec_stats,	NULL,		"ipsec6",0 },
206cfa1ca9dSYoshinobu Inoue #endif
207cfa1ca9dSYoshinobu Inoue #ifdef notyet
208cfa1ca9dSYoshinobu Inoue 	{ -1,		N_PIM6STAT,	1,	0,
209cfa1ca9dSYoshinobu Inoue 	  pim6_stats,	NULL,		"pim6",	0 },
210cfa1ca9dSYoshinobu Inoue #endif
21161793a0cSRuslan Ermilov 	{ -1,		-1,		1,	0,
21233841545SHajimu UMEMOTO 	  rip6_stats,	NULL,		"rip6",	0 },
21333841545SHajimu UMEMOTO 	{ -1,		-1,		1,	0,
214cfa1ca9dSYoshinobu Inoue 	  bdg_stats,	NULL,		"bdg",	1 /* bridging... */ },
215cfa1ca9dSYoshinobu Inoue 	{ -1,		-1,		0,	0,
216cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
217cfa1ca9dSYoshinobu Inoue };
218cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
219cfa1ca9dSYoshinobu Inoue 
2203b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2213b8a8567SJun-ichiro itojun Hagino struct protox pfkeyprotox[] = {
2223b8a8567SJun-ichiro itojun Hagino 	{ -1,		N_PFKEYSTAT,	1,	0,
2233b8a8567SJun-ichiro itojun Hagino 	  pfkey_stats,	NULL,		"pfkey", 0 },
2243b8a8567SJun-ichiro itojun Hagino 	{ -1,		-1,		0,	0,
2253b8a8567SJun-ichiro itojun Hagino 	  0,		NULL,		0,	0 }
2263b8a8567SJun-ichiro itojun Hagino };
2273b8a8567SJun-ichiro itojun Hagino #endif
2283b8a8567SJun-ichiro itojun Hagino 
22963bf4575SJulian Elischer struct protox atalkprotox[] = {
23063bf4575SJulian Elischer 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
231cfa1ca9dSYoshinobu Inoue 	  ddp_stats,	NULL,		"ddp" },
23263bf4575SJulian Elischer 	{ -1,		-1,		0,	0,
233cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
23463bf4575SJulian Elischer };
23563bf4575SJulian Elischer 
2364cf49a43SJulian Elischer struct protox netgraphprotox[] = {
2374cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
238cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"ctrl" },
2394cf49a43SJulian Elischer 	{ N_NGSOCKS,	-1,		1,	netgraphprotopr,
240cfa1ca9dSYoshinobu Inoue 	  NULL,		NULL,		"data" },
241cfa1ca9dSYoshinobu Inoue 	{ -1,		NULL,		0,	0,
242cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2434cf49a43SJulian Elischer };
2444cf49a43SJulian Elischer 
245cc6a66f2SJulian Elischer struct protox ipxprotox[] = {
246cc6a66f2SJulian Elischer 	{ N_IPX,	N_IPXSTAT,	1,	ipxprotopr,
247cfa1ca9dSYoshinobu Inoue 	  ipx_stats,	NULL,		"ipx",	0 },
248cc6a66f2SJulian Elischer 	{ N_IPX,	N_SPXSTAT,	1,	ipxprotopr,
249cfa1ca9dSYoshinobu Inoue 	  spx_stats,	NULL,		"spx",	0 },
250cc6a66f2SJulian Elischer 	{ -1,		-1,		0,	0,
251cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0,	0 }
252cc6a66f2SJulian Elischer };
253cc6a66f2SJulian Elischer 
254cbc17e71SGarrett Wollman #ifdef NS
2559b50d902SRodney W. Grimes struct protox nsprotox[] = {
2569b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
257cfa1ca9dSYoshinobu Inoue 	  idp_stats,	NULL,		"idp" },
2589b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
259cfa1ca9dSYoshinobu Inoue 	  spp_stats,	NULL,		"spp" },
2609b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
261cfa1ca9dSYoshinobu Inoue 	  nserr_stats,	NULL,		"ns_err" },
2629b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
263cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2649b50d902SRodney W. Grimes };
265cbc17e71SGarrett Wollman #endif
2669b50d902SRodney W. Grimes 
2670761cb29SGarrett Wollman #ifdef ISO
2689b50d902SRodney W. Grimes struct protox isoprotox[] = {
2699b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
270cfa1ca9dSYoshinobu Inoue 	  tp_stats,	NULL,		"tp" },
2719b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
272cfa1ca9dSYoshinobu Inoue 	  cltp_stats,	NULL,		"cltp" },
2739b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
274cfa1ca9dSYoshinobu Inoue 	  clnp_stats,	NULL,		"clnp"},
2759b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
276cfa1ca9dSYoshinobu Inoue 	  esis_stats,	NULL,		"esis"},
2779b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
278cfa1ca9dSYoshinobu Inoue 	  0,		NULL,		0 }
2799b50d902SRodney W. Grimes };
2800761cb29SGarrett Wollman #endif
2819b50d902SRodney W. Grimes 
282cfa1ca9dSYoshinobu Inoue struct protox *protoprotox[] = {
283cfa1ca9dSYoshinobu Inoue 					 protox,
284cfa1ca9dSYoshinobu Inoue #ifdef INET6
285cfa1ca9dSYoshinobu Inoue 					 ip6protox,
286cfa1ca9dSYoshinobu Inoue #endif
2873b8a8567SJun-ichiro itojun Hagino #ifdef IPSEC
2883b8a8567SJun-ichiro itojun Hagino 					 pfkeyprotox,
2893b8a8567SJun-ichiro itojun Hagino #endif
290cfa1ca9dSYoshinobu Inoue 					 ipxprotox, atalkprotox,
291cbc17e71SGarrett Wollman #ifdef NS
292cbc17e71SGarrett Wollman 					 nsprotox,
293cbc17e71SGarrett Wollman #endif
2940761cb29SGarrett Wollman #ifdef ISO
2950761cb29SGarrett Wollman 					 isoprotox,
2960761cb29SGarrett Wollman #endif
2970761cb29SGarrett Wollman 					 NULL };
2989b50d902SRodney W. Grimes 
2995e051718SAssar Westerlund static void printproto (struct protox *, char *);
3005e051718SAssar Westerlund static void usage (void);
3015e051718SAssar Westerlund static struct protox *name2protox (char *);
3025e051718SAssar Westerlund static struct protox *knownname (char *);
3039b50d902SRodney W. Grimes 
304c6620a13SPoul-Henning Kamp static kvm_t *kvmd;
305080b7f49SDag-Erling Smørgrav static char *nlistf = NULL, *memf = NULL;
306080b7f49SDag-Erling Smørgrav 
307080b7f49SDag-Erling Smørgrav int	Aflag;		/* show addresses of protocol control block */
308080b7f49SDag-Erling Smørgrav int	aflag;		/* show all sockets (including servers) */
309080b7f49SDag-Erling Smørgrav int	bflag;		/* show i/f total bytes in/out */
310080b7f49SDag-Erling Smørgrav int	dflag;		/* show i/f dropped packets */
311080b7f49SDag-Erling Smørgrav int	gflag;		/* show group (multicast) routing or stats */
312080b7f49SDag-Erling Smørgrav int	iflag;		/* show interfaces */
313080b7f49SDag-Erling Smørgrav int	lflag;		/* show routing table with use and ref */
314080b7f49SDag-Erling Smørgrav int	Lflag;		/* show size of listen queues */
315080b7f49SDag-Erling Smørgrav int	mflag;		/* show memory stats */
31665ea0024SAssar Westerlund int	numeric_addr;	/* show addresses numerically */
31765ea0024SAssar Westerlund int	numeric_port;	/* show ports numerically */
318cf5e44f8SRuslan Ermilov static int pflag;	/* show given protocol */
319080b7f49SDag-Erling Smørgrav int	rflag;		/* show routing tables (or routing stats) */
320080b7f49SDag-Erling Smørgrav int	sflag;		/* show protocol statistics */
321080b7f49SDag-Erling Smørgrav int	tflag;		/* show i/f watchdog timers */
322080b7f49SDag-Erling Smørgrav int	Wflag;		/* wide display */
323080b7f49SDag-Erling Smørgrav 
324080b7f49SDag-Erling Smørgrav int	interval;	/* repeat interval for i/f stats */
325080b7f49SDag-Erling Smørgrav 
326080b7f49SDag-Erling Smørgrav char	*interface;	/* desired i/f for stats, or NULL for all i/fs */
327080b7f49SDag-Erling Smørgrav int	unit;		/* unit number for above */
328080b7f49SDag-Erling Smørgrav 
329080b7f49SDag-Erling Smørgrav int	af;		/* address family */
3309b50d902SRodney W. Grimes 
3319b50d902SRodney W. Grimes int
3329b50d902SRodney W. Grimes main(argc, argv)
3339b50d902SRodney W. Grimes 	int argc;
3349b50d902SRodney W. Grimes 	char *argv[];
3359b50d902SRodney W. Grimes {
3364cf49a43SJulian Elischer 	register struct protox *tp = NULL;  /* for printing cblocks & stats */
3379b50d902SRodney W. Grimes 	int ch;
3389b50d902SRodney W. Grimes 
3399b50d902SRodney W. Grimes 	af = AF_UNSPEC;
3409b50d902SRodney W. Grimes 
341cf5e44f8SRuslan Ermilov 	while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:")) != -1)
3429b50d902SRodney W. Grimes 		switch(ch) {
3439b50d902SRodney W. Grimes 		case 'A':
3449b50d902SRodney W. Grimes 			Aflag = 1;
3459b50d902SRodney W. Grimes 			break;
3469b50d902SRodney W. Grimes 		case 'a':
3479b50d902SRodney W. Grimes 			aflag = 1;
3489b50d902SRodney W. Grimes 			break;
349e1e293a5SDavid Greenman 		case 'b':
350e1e293a5SDavid Greenman 			bflag = 1;
351e1e293a5SDavid Greenman 			break;
3529b50d902SRodney W. Grimes 		case 'd':
3539b50d902SRodney W. Grimes 			dflag = 1;
3549b50d902SRodney W. Grimes 			break;
3559b50d902SRodney W. Grimes 		case 'f':
356cbc17e71SGarrett Wollman #ifdef NS
3579b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
3589b50d902SRodney W. Grimes 				af = AF_NS;
359cbc17e71SGarrett Wollman 			else
360cbc17e71SGarrett Wollman #endif
361cbc17e71SGarrett Wollman 			if (strcmp(optarg, "ipx") == 0)
362cc6a66f2SJulian Elischer 				af = AF_IPX;
3639b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
3649b50d902SRodney W. Grimes 				af = AF_INET;
365cfa1ca9dSYoshinobu Inoue #ifdef INET6
366cfa1ca9dSYoshinobu Inoue 			else if (strcmp(optarg, "inet6") == 0)
367cfa1ca9dSYoshinobu Inoue 				af = AF_INET6;
368cfa1ca9dSYoshinobu Inoue #endif /*INET6*/
3693b8a8567SJun-ichiro itojun Hagino #ifdef INET6
3703b8a8567SJun-ichiro itojun Hagino 			else if (strcmp(optarg, "pfkey") == 0)
3713b8a8567SJun-ichiro itojun Hagino 				af = PF_KEY;
3723b8a8567SJun-ichiro itojun Hagino #endif /*INET6*/
3739b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
3749b50d902SRodney W. Grimes 				af = AF_UNIX;
37563bf4575SJulian Elischer 			else if (strcmp(optarg, "atalk") == 0)
37663bf4575SJulian Elischer 				af = AF_APPLETALK;
3774cf49a43SJulian Elischer 			else if (strcmp(optarg, "ng") == 0
3784cf49a43SJulian Elischer 			    || strcmp(optarg, "netgraph") == 0)
3794cf49a43SJulian Elischer 				af = AF_NETGRAPH;
3800761cb29SGarrett Wollman #ifdef ISO
3819b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
3829b50d902SRodney W. Grimes 				af = AF_ISO;
3830761cb29SGarrett Wollman #endif
3849b50d902SRodney W. Grimes 			else {
38551e7d42cSGarrett Wollman 				errx(1, "%s: unknown address family", optarg);
3869b50d902SRodney W. Grimes 			}
3879b50d902SRodney W. Grimes 			break;
3889b50d902SRodney W. Grimes 		case 'g':
3899b50d902SRodney W. Grimes 			gflag = 1;
3909b50d902SRodney W. Grimes 			break;
3919b50d902SRodney W. Grimes 		case 'I': {
3929b50d902SRodney W. Grimes 			char *cp;
3939b50d902SRodney W. Grimes 
3949b50d902SRodney W. Grimes 			iflag = 1;
3959b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
3969b50d902SRodney W. Grimes 				continue;
3979b50d902SRodney W. Grimes 			unit = atoi(cp);
3989b50d902SRodney W. Grimes 			break;
3999b50d902SRodney W. Grimes 		}
4009b50d902SRodney W. Grimes 		case 'i':
4019b50d902SRodney W. Grimes 			iflag = 1;
4029b50d902SRodney W. Grimes 			break;
403cfa1ca9dSYoshinobu Inoue 		case 'l':
404cfa1ca9dSYoshinobu Inoue 			lflag = 1;
405cfa1ca9dSYoshinobu Inoue 			break;
406ac55add0SGuido van Rooij 		case 'L':
407ac55add0SGuido van Rooij 			Lflag = 1;
408ac55add0SGuido van Rooij 			break;
4099b50d902SRodney W. Grimes 		case 'M':
4109b50d902SRodney W. Grimes 			memf = optarg;
4119b50d902SRodney W. Grimes 			break;
4129b50d902SRodney W. Grimes 		case 'm':
4139b50d902SRodney W. Grimes 			mflag = 1;
4149b50d902SRodney W. Grimes 			break;
4159b50d902SRodney W. Grimes 		case 'N':
4169b50d902SRodney W. Grimes 			nlistf = optarg;
4179b50d902SRodney W. Grimes 			break;
4189b50d902SRodney W. Grimes 		case 'n':
41965ea0024SAssar Westerlund 			numeric_addr = numeric_port = 1;
4209b50d902SRodney W. Grimes 			break;
4219b50d902SRodney W. Grimes 		case 'p':
4229b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
42351e7d42cSGarrett Wollman 				errx(1,
42451e7d42cSGarrett Wollman 				     "%s: unknown or uninstrumented protocol",
42551e7d42cSGarrett Wollman 				     optarg);
4269b50d902SRodney W. Grimes 			}
4279b50d902SRodney W. Grimes 			pflag = 1;
4289b50d902SRodney W. Grimes 			break;
4299b50d902SRodney W. Grimes 		case 'r':
4309b50d902SRodney W. Grimes 			rflag = 1;
4319b50d902SRodney W. Grimes 			break;
4329b50d902SRodney W. Grimes 		case 's':
4339b50d902SRodney W. Grimes 			++sflag;
4349b50d902SRodney W. Grimes 			break;
43565ea0024SAssar Westerlund 		case 'S':
43665ea0024SAssar Westerlund 			numeric_addr = 1;
43765ea0024SAssar Westerlund 			break;
4389b50d902SRodney W. Grimes 		case 't':
4399b50d902SRodney W. Grimes 			tflag = 1;
4409b50d902SRodney W. Grimes 			break;
4419b50d902SRodney W. Grimes 		case 'u':
4429b50d902SRodney W. Grimes 			af = AF_UNIX;
4439b50d902SRodney W. Grimes 			break;
444080b7f49SDag-Erling Smørgrav 		case 'W':
445080b7f49SDag-Erling Smørgrav 			Wflag = 1;
446080b7f49SDag-Erling Smørgrav 			break;
4479b50d902SRodney W. Grimes 		case 'w':
4489b50d902SRodney W. Grimes 			interval = atoi(optarg);
4499b50d902SRodney W. Grimes 			iflag = 1;
4509b50d902SRodney W. Grimes 			break;
4519b50d902SRodney W. Grimes 		case '?':
4529b50d902SRodney W. Grimes 		default:
4539b50d902SRodney W. Grimes 			usage();
4549b50d902SRodney W. Grimes 		}
4559b50d902SRodney W. Grimes 	argv += optind;
4569b50d902SRodney W. Grimes 	argc -= optind;
4579b50d902SRodney W. Grimes 
4589b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
4599b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
4609b50d902SRodney W. Grimes 	if (*argv) {
4619b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
4629b50d902SRodney W. Grimes 			interval = atoi(*argv);
4639b50d902SRodney W. Grimes 			if (interval <= 0)
4649b50d902SRodney W. Grimes 				usage();
4659b50d902SRodney W. Grimes 			++argv;
4669b50d902SRodney W. Grimes 			iflag = 1;
4679b50d902SRodney W. Grimes 		}
4689b50d902SRodney W. Grimes 		if (*argv) {
4699b50d902SRodney W. Grimes 			nlistf = *argv;
4709b50d902SRodney W. Grimes 			if (*++argv)
4719b50d902SRodney W. Grimes 				memf = *argv;
4729b50d902SRodney W. Grimes 		}
4739b50d902SRodney W. Grimes 	}
4749b50d902SRodney W. Grimes #endif
4759b50d902SRodney W. Grimes 
4769b50d902SRodney W. Grimes 	/*
4779b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
4789b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
4799b50d902SRodney W. Grimes 	 */
4809b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
4819b50d902SRodney W. Grimes 		setgid(getgid());
4829b50d902SRodney W. Grimes 
4839b50d902SRodney W. Grimes 	if (mflag) {
484d15c5f56SRuslan Ermilov 		if (memf != NULL) {
485d15c5f56SRuslan Ermilov 			if (kread(0, 0, 0) == 0)
486d15c5f56SRuslan Ermilov 				mbpr(nl[N_MBSTAT].n_value,
487d15c5f56SRuslan Ermilov 				    nl[N_MBTYPES].n_value,
488d15c5f56SRuslan Ermilov 				    nl[N_NMBCLUSTERS].n_value,
489d15c5f56SRuslan Ermilov 				    nl[N_NMBUFS].n_value);
490d15c5f56SRuslan Ermilov 		} else
491d15c5f56SRuslan Ermilov 			mbpr(0, 0, 0, 0);
4929b50d902SRodney W. Grimes 		exit(0);
4939b50d902SRodney W. Grimes 	}
494e9370a2eSAssar Westerlund 	if (pflag) {
495e9370a2eSAssar Westerlund 		if (iflag && tp->pr_istats) {
496e9370a2eSAssar Westerlund 			kread(0, 0, 0);
497e9370a2eSAssar Westerlund 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
498e9370a2eSAssar Westerlund 			exit(0);
499e9370a2eSAssar Westerlund 		}
500e9370a2eSAssar Westerlund 		if (!tp->pr_stats) {
501e9370a2eSAssar Westerlund 			printf("%s: no stats routine\n", tp->pr_name);
502e9370a2eSAssar Westerlund 			exit(0);
503e9370a2eSAssar Westerlund 		}
504e9370a2eSAssar Westerlund 		if (tp->pr_usesysctl) {
505e9370a2eSAssar Westerlund 			(*tp->pr_stats)(tp->pr_usesysctl, tp->pr_name);
506e9370a2eSAssar Westerlund 		} else {
507e9370a2eSAssar Westerlund 			kread(0, 0, 0);
508e9370a2eSAssar Westerlund 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
509e9370a2eSAssar Westerlund 					tp->pr_name);
510e9370a2eSAssar Westerlund 		}
511e9370a2eSAssar Westerlund 		exit(0);
512e9370a2eSAssar Westerlund 	}
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 {
616e9370a2eSAssar Westerlund 	void (*pr)();
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