xref: /freebsd/usr.bin/netstat/main.c (revision e1e293a5dcff2b0a0caabdc0c4f7d11bbab8867c)
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
359b50d902SRodney W. Grimes char 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
419b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c	8.4 (Berkeley) 3/1/94";
429b50d902SRodney W. Grimes #endif /* not lint */
439b50d902SRodney W. Grimes 
449b50d902SRodney W. Grimes #include <sys/param.h>
459b50d902SRodney W. Grimes #include <sys/file.h>
469b50d902SRodney W. Grimes #include <sys/protosw.h>
479b50d902SRodney W. Grimes #include <sys/socket.h>
489b50d902SRodney W. Grimes 
499b50d902SRodney W. Grimes #include <netinet/in.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include <ctype.h>
529b50d902SRodney W. Grimes #include <errno.h>
539b50d902SRodney W. Grimes #include <kvm.h>
549b50d902SRodney W. Grimes #include <limits.h>
559b50d902SRodney W. Grimes #include <netdb.h>
569b50d902SRodney W. Grimes #include <nlist.h>
579b50d902SRodney W. Grimes #include <paths.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <unistd.h>
629b50d902SRodney W. Grimes #include "netstat.h"
639b50d902SRodney W. Grimes 
649b50d902SRodney W. Grimes struct nlist nl[] = {
659b50d902SRodney W. Grimes #define	N_MBSTAT	0
669b50d902SRodney W. Grimes 	{ "_mbstat" },
679b50d902SRodney W. Grimes #define	N_IPSTAT	1
689b50d902SRodney W. Grimes 	{ "_ipstat" },
699b50d902SRodney W. Grimes #define	N_TCB		2
709b50d902SRodney W. Grimes 	{ "_tcb" },
719b50d902SRodney W. Grimes #define	N_TCPSTAT	3
729b50d902SRodney W. Grimes 	{ "_tcpstat" },
739b50d902SRodney W. Grimes #define	N_UDB		4
749b50d902SRodney W. Grimes 	{ "_udb" },
759b50d902SRodney W. Grimes #define	N_UDPSTAT	5
769b50d902SRodney W. Grimes 	{ "_udpstat" },
779b50d902SRodney W. Grimes #define	N_IFNET		6
789b50d902SRodney W. Grimes 	{ "_ifnet" },
799b50d902SRodney W. Grimes #define	N_IMP		7
809b50d902SRodney W. Grimes 	{ "_imp_softc" },
819b50d902SRodney W. Grimes #define	N_ICMPSTAT	8
829b50d902SRodney W. Grimes 	{ "_icmpstat" },
839b50d902SRodney W. Grimes #define	N_RTSTAT	9
849b50d902SRodney W. Grimes 	{ "_rtstat" },
859b50d902SRodney W. Grimes #define	N_UNIXSW	10
869b50d902SRodney W. Grimes 	{ "_unixsw" },
879b50d902SRodney W. Grimes #define N_IDP		11
889b50d902SRodney W. Grimes 	{ "_nspcb"},
899b50d902SRodney W. Grimes #define N_IDPSTAT	12
909b50d902SRodney W. Grimes 	{ "_idpstat"},
919b50d902SRodney W. Grimes #define N_SPPSTAT	13
929b50d902SRodney W. Grimes 	{ "_spp_istat"},
939b50d902SRodney W. Grimes #define N_NSERR		14
949b50d902SRodney W. Grimes 	{ "_ns_errstat"},
959b50d902SRodney W. Grimes #define	N_CLNPSTAT	15
969b50d902SRodney W. Grimes 	{ "_clnp_stat"},
979b50d902SRodney W. Grimes #define	IN_NOTUSED	16
989b50d902SRodney W. Grimes 	{ "_tp_inpcb" },
999b50d902SRodney W. Grimes #define	ISO_TP		17
1009b50d902SRodney W. Grimes 	{ "_tp_refinfo" },
1019b50d902SRodney W. Grimes #define	N_TPSTAT	18
1029b50d902SRodney W. Grimes 	{ "_tp_stat" },
1039b50d902SRodney W. Grimes #define	N_ESISSTAT	19
1049b50d902SRodney W. Grimes 	{ "_esis_stat"},
1059b50d902SRodney W. Grimes #define N_NIMP		20
1069b50d902SRodney W. Grimes 	{ "_nimp"},
1079b50d902SRodney W. Grimes #define N_RTREE		21
1089b50d902SRodney W. Grimes 	{ "_rt_tables"},
1099b50d902SRodney W. Grimes #define N_CLTP		22
1109b50d902SRodney W. Grimes 	{ "_cltb"},
1119b50d902SRodney W. Grimes #define N_CLTPSTAT	23
1129b50d902SRodney W. Grimes 	{ "_cltpstat"},
1139b50d902SRodney W. Grimes #define	N_NFILE		24
1149b50d902SRodney W. Grimes 	{ "_nfile" },
1159b50d902SRodney W. Grimes #define	N_FILE		25
1169b50d902SRodney W. Grimes 	{ "_file" },
1179b50d902SRodney W. Grimes #define N_IGMPSTAT	26
1189b50d902SRodney W. Grimes 	{ "_igmpstat" },
1199b50d902SRodney W. Grimes #define N_MRTPROTO	27
1209b50d902SRodney W. Grimes 	{ "_ip_mrtproto" },
1219b50d902SRodney W. Grimes #define N_MRTSTAT	28
1229b50d902SRodney W. Grimes 	{ "_mrtstat" },
1239b50d902SRodney W. Grimes #define N_MRTTABLE	29
1249b50d902SRodney W. Grimes 	{ "_mrttable" },
1259b50d902SRodney W. Grimes #define N_VIFTABLE	30
1269b50d902SRodney W. Grimes 	{ "_viftable" },
1279b50d902SRodney W. Grimes 	"",
1289b50d902SRodney W. Grimes };
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes struct protox {
1319b50d902SRodney W. Grimes 	u_char	pr_index;		/* index into nlist of cb head */
1329b50d902SRodney W. Grimes 	u_char	pr_sindex;		/* index into nlist of stat block */
1339b50d902SRodney W. Grimes 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
1349b50d902SRodney W. Grimes 	void	(*pr_cblocks)();	/* control blocks printing routine */
1359b50d902SRodney W. Grimes 	void	(*pr_stats)();		/* statistics printing routine */
1369b50d902SRodney W. Grimes 	char	*pr_name;		/* well-known name */
1379b50d902SRodney W. Grimes } protox[] = {
1389b50d902SRodney W. Grimes 	{ N_TCB,	N_TCPSTAT,	1,	protopr,
1399b50d902SRodney W. Grimes 	  tcp_stats,	"tcp" },
1409b50d902SRodney W. Grimes 	{ N_UDB,	N_UDPSTAT,	1,	protopr,
1419b50d902SRodney W. Grimes 	  udp_stats,	"udp" },
1429b50d902SRodney W. Grimes 	{ -1,		N_IPSTAT,	1,	0,
1439b50d902SRodney W. Grimes 	  ip_stats,	"ip" },
1449b50d902SRodney W. Grimes 	{ -1,		N_ICMPSTAT,	1,	0,
1459b50d902SRodney W. Grimes 	  icmp_stats,	"icmp" },
1469b50d902SRodney W. Grimes 	{ -1,		N_IGMPSTAT,	1,	0,
1479b50d902SRodney W. Grimes 	  igmp_stats,	"igmp" },
1489b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
1499b50d902SRodney W. Grimes 	  0,		0 }
1509b50d902SRodney W. Grimes };
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes struct protox nsprotox[] = {
1539b50d902SRodney W. Grimes 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
1549b50d902SRodney W. Grimes 	  idp_stats,	"idp" },
1559b50d902SRodney W. Grimes 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
1569b50d902SRodney W. Grimes 	  spp_stats,	"spp" },
1579b50d902SRodney W. Grimes 	{ -1,		N_NSERR,	1,	0,
1589b50d902SRodney W. Grimes 	  nserr_stats,	"ns_err" },
1599b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
1609b50d902SRodney W. Grimes 	  0,		0 }
1619b50d902SRodney W. Grimes };
1629b50d902SRodney W. Grimes 
1639b50d902SRodney W. Grimes struct protox isoprotox[] = {
1649b50d902SRodney W. Grimes 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
1659b50d902SRodney W. Grimes 	  tp_stats,	"tp" },
1669b50d902SRodney W. Grimes 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
1679b50d902SRodney W. Grimes 	  cltp_stats,	"cltp" },
1689b50d902SRodney W. Grimes 	{ -1,		N_CLNPSTAT,	1,	 0,
1699b50d902SRodney W. Grimes 	  clnp_stats,	"clnp"},
1709b50d902SRodney W. Grimes 	{ -1,		N_ESISSTAT,	1,	 0,
1719b50d902SRodney W. Grimes 	  esis_stats,	"esis"},
1729b50d902SRodney W. Grimes 	{ -1,		-1,		0,	0,
1739b50d902SRodney W. Grimes 	  0,		0 }
1749b50d902SRodney W. Grimes };
1759b50d902SRodney W. Grimes 
1769b50d902SRodney W. Grimes struct protox *protoprotox[] = { protox, nsprotox, isoprotox, NULL };
1779b50d902SRodney W. Grimes 
1789b50d902SRodney W. Grimes static void printproto __P((struct protox *, char *));
1799b50d902SRodney W. Grimes static void usage __P((void));
1809b50d902SRodney W. Grimes static struct protox *name2protox __P((char *));
1819b50d902SRodney W. Grimes static struct protox *knownname __P((char *));
1829b50d902SRodney W. Grimes 
1839b50d902SRodney W. Grimes kvm_t *kvmd;
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes int
1869b50d902SRodney W. Grimes main(argc, argv)
1879b50d902SRodney W. Grimes 	int argc;
1889b50d902SRodney W. Grimes 	char *argv[];
1899b50d902SRodney W. Grimes {
1909b50d902SRodney W. Grimes 	extern char *optarg;
1919b50d902SRodney W. Grimes 	extern int optind;
1929b50d902SRodney W. Grimes 	register struct protoent *p;
1939b50d902SRodney W. Grimes 	register struct protox *tp;	/* for printing cblocks & stats */
1949b50d902SRodney W. Grimes 	register char *cp;
1959b50d902SRodney W. Grimes 	int ch;
1969b50d902SRodney W. Grimes 	char *nlistf = NULL, *memf = NULL;
1979b50d902SRodney W. Grimes 	char buf[_POSIX2_LINE_MAX];
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 	if (cp = rindex(argv[0], '/'))
2009b50d902SRodney W. Grimes 		prog = cp + 1;
2019b50d902SRodney W. Grimes 	else
2029b50d902SRodney W. Grimes 		prog = argv[0];
2039b50d902SRodney W. Grimes 	af = AF_UNSPEC;
2049b50d902SRodney W. Grimes 
205e1e293a5SDavid Greenman 	while ((ch = getopt(argc, argv, "Aabdf:ghI:iM:mN:np:rstuw:")) != EOF)
2069b50d902SRodney W. Grimes 		switch(ch) {
2079b50d902SRodney W. Grimes 		case 'A':
2089b50d902SRodney W. Grimes 			Aflag = 1;
2099b50d902SRodney W. Grimes 			break;
2109b50d902SRodney W. Grimes 		case 'a':
2119b50d902SRodney W. Grimes 			aflag = 1;
2129b50d902SRodney W. Grimes 			break;
213e1e293a5SDavid Greenman 		case 'b':
214e1e293a5SDavid Greenman 			bflag = 1;
215e1e293a5SDavid Greenman 			break;
2169b50d902SRodney W. Grimes 		case 'd':
2179b50d902SRodney W. Grimes 			dflag = 1;
2189b50d902SRodney W. Grimes 			break;
2199b50d902SRodney W. Grimes 		case 'f':
2209b50d902SRodney W. Grimes 			if (strcmp(optarg, "ns") == 0)
2219b50d902SRodney W. Grimes 				af = AF_NS;
2229b50d902SRodney W. Grimes 			else if (strcmp(optarg, "inet") == 0)
2239b50d902SRodney W. Grimes 				af = AF_INET;
2249b50d902SRodney W. Grimes 			else if (strcmp(optarg, "unix") == 0)
2259b50d902SRodney W. Grimes 				af = AF_UNIX;
2269b50d902SRodney W. Grimes 			else if (strcmp(optarg, "iso") == 0)
2279b50d902SRodney W. Grimes 				af = AF_ISO;
2289b50d902SRodney W. Grimes 			else {
2299b50d902SRodney W. Grimes 				(void)fprintf(stderr,
2309b50d902SRodney W. Grimes 				    "%s: %s: unknown address family\n",
2319b50d902SRodney W. Grimes 				    prog, optarg);
2329b50d902SRodney W. Grimes 				exit(1);
2339b50d902SRodney W. Grimes 			}
2349b50d902SRodney W. Grimes 			break;
2359b50d902SRodney W. Grimes 		case 'g':
2369b50d902SRodney W. Grimes 			gflag = 1;
2379b50d902SRodney W. Grimes 			break;
2389b50d902SRodney W. Grimes 		case 'I': {
2399b50d902SRodney W. Grimes 			char *cp;
2409b50d902SRodney W. Grimes 
2419b50d902SRodney W. Grimes 			iflag = 1;
2429b50d902SRodney W. Grimes 			for (cp = interface = optarg; isalpha(*cp); cp++)
2439b50d902SRodney W. Grimes 				continue;
2449b50d902SRodney W. Grimes 			unit = atoi(cp);
2459b50d902SRodney W. Grimes 			*cp = '\0';
2469b50d902SRodney W. Grimes 			break;
2479b50d902SRodney W. Grimes 		}
2489b50d902SRodney W. Grimes 		case 'i':
2499b50d902SRodney W. Grimes 			iflag = 1;
2509b50d902SRodney W. Grimes 			break;
2519b50d902SRodney W. Grimes 		case 'M':
2529b50d902SRodney W. Grimes 			memf = optarg;
2539b50d902SRodney W. Grimes 			break;
2549b50d902SRodney W. Grimes 		case 'm':
2559b50d902SRodney W. Grimes 			mflag = 1;
2569b50d902SRodney W. Grimes 			break;
2579b50d902SRodney W. Grimes 		case 'N':
2589b50d902SRodney W. Grimes 			nlistf = optarg;
2599b50d902SRodney W. Grimes 			break;
2609b50d902SRodney W. Grimes 		case 'n':
2619b50d902SRodney W. Grimes 			nflag = 1;
2629b50d902SRodney W. Grimes 			break;
2639b50d902SRodney W. Grimes 		case 'p':
2649b50d902SRodney W. Grimes 			if ((tp = name2protox(optarg)) == NULL) {
2659b50d902SRodney W. Grimes 				(void)fprintf(stderr,
2669b50d902SRodney W. Grimes 				    "%s: %s: unknown or uninstrumented protocol\n",
2679b50d902SRodney W. Grimes 				    prog, optarg);
2689b50d902SRodney W. Grimes 				exit(1);
2699b50d902SRodney W. Grimes 			}
2709b50d902SRodney W. Grimes 			pflag = 1;
2719b50d902SRodney W. Grimes 			break;
2729b50d902SRodney W. Grimes 		case 'r':
2739b50d902SRodney W. Grimes 			rflag = 1;
2749b50d902SRodney W. Grimes 			break;
2759b50d902SRodney W. Grimes 		case 's':
2769b50d902SRodney W. Grimes 			++sflag;
2779b50d902SRodney W. Grimes 			break;
2789b50d902SRodney W. Grimes 		case 't':
2799b50d902SRodney W. Grimes 			tflag = 1;
2809b50d902SRodney W. Grimes 			break;
2819b50d902SRodney W. Grimes 		case 'u':
2829b50d902SRodney W. Grimes 			af = AF_UNIX;
2839b50d902SRodney W. Grimes 			break;
2849b50d902SRodney W. Grimes 		case 'w':
2859b50d902SRodney W. Grimes 			interval = atoi(optarg);
2869b50d902SRodney W. Grimes 			iflag = 1;
2879b50d902SRodney W. Grimes 			break;
2889b50d902SRodney W. Grimes 		case '?':
2899b50d902SRodney W. Grimes 		default:
2909b50d902SRodney W. Grimes 			usage();
2919b50d902SRodney W. Grimes 		}
2929b50d902SRodney W. Grimes 	argv += optind;
2939b50d902SRodney W. Grimes 	argc -= optind;
2949b50d902SRodney W. Grimes 
2959b50d902SRodney W. Grimes #define	BACKWARD_COMPATIBILITY
2969b50d902SRodney W. Grimes #ifdef	BACKWARD_COMPATIBILITY
2979b50d902SRodney W. Grimes 	if (*argv) {
2989b50d902SRodney W. Grimes 		if (isdigit(**argv)) {
2999b50d902SRodney W. Grimes 			interval = atoi(*argv);
3009b50d902SRodney W. Grimes 			if (interval <= 0)
3019b50d902SRodney W. Grimes 				usage();
3029b50d902SRodney W. Grimes 			++argv;
3039b50d902SRodney W. Grimes 			iflag = 1;
3049b50d902SRodney W. Grimes 		}
3059b50d902SRodney W. Grimes 		if (*argv) {
3069b50d902SRodney W. Grimes 			nlistf = *argv;
3079b50d902SRodney W. Grimes 			if (*++argv)
3089b50d902SRodney W. Grimes 				memf = *argv;
3099b50d902SRodney W. Grimes 		}
3109b50d902SRodney W. Grimes 	}
3119b50d902SRodney W. Grimes #endif
3129b50d902SRodney W. Grimes 
3139b50d902SRodney W. Grimes 	/*
3149b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
3159b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
3169b50d902SRodney W. Grimes 	 */
3179b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
3189b50d902SRodney W. Grimes 		setgid(getgid());
3199b50d902SRodney W. Grimes 
3209b50d902SRodney W. Grimes 	if ((kvmd = kvm_open(nlistf, memf, NULL, O_RDONLY, prog)) == NULL) {
3219b50d902SRodney W. Grimes 		fprintf(stderr, "%s: kvm_open: %s\n", prog, buf);
3229b50d902SRodney W. Grimes 		exit(1);
3239b50d902SRodney W. Grimes 	}
3249b50d902SRodney W. Grimes 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
3259b50d902SRodney W. Grimes 		if (nlistf)
3269b50d902SRodney W. Grimes 			fprintf(stderr, "%s: %s: no namelist\n", prog, nlistf);
3279b50d902SRodney W. Grimes 		else
3289b50d902SRodney W. Grimes 			fprintf(stderr, "%s: no namelist\n", prog);
3299b50d902SRodney W. Grimes 		exit(1);
3309b50d902SRodney W. Grimes 	}
3319b50d902SRodney W. Grimes 	if (mflag) {
3329b50d902SRodney W. Grimes 		mbpr(nl[N_MBSTAT].n_value);
3339b50d902SRodney W. Grimes 		exit(0);
3349b50d902SRodney W. Grimes 	}
3359b50d902SRodney W. Grimes 	if (pflag) {
3369b50d902SRodney W. Grimes 		if (tp->pr_stats)
3379b50d902SRodney W. Grimes 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
3389b50d902SRodney W. Grimes 				tp->pr_name);
3399b50d902SRodney W. Grimes 		else
3409b50d902SRodney W. Grimes 			printf("%s: no stats routine\n", tp->pr_name);
3419b50d902SRodney W. Grimes 		exit(0);
3429b50d902SRodney W. Grimes 	}
3439b50d902SRodney W. Grimes 	/*
3449b50d902SRodney W. Grimes 	 * Keep file descriptors open to avoid overhead
3459b50d902SRodney W. Grimes 	 * of open/close on each call to get* routines.
3469b50d902SRodney W. Grimes 	 */
3479b50d902SRodney W. Grimes 	sethostent(1);
3489b50d902SRodney W. Grimes 	setnetent(1);
3499b50d902SRodney W. Grimes 	if (iflag) {
3509b50d902SRodney W. Grimes 		intpr(interval, nl[N_IFNET].n_value);
3519b50d902SRodney W. Grimes 		exit(0);
3529b50d902SRodney W. Grimes 	}
3539b50d902SRodney W. Grimes 	if (rflag) {
3549b50d902SRodney W. Grimes 		if (sflag)
3559b50d902SRodney W. Grimes 			rt_stats(nl[N_RTSTAT].n_value);
3569b50d902SRodney W. Grimes 		else
3579b50d902SRodney W. Grimes 			routepr(nl[N_RTREE].n_value);
3589b50d902SRodney W. Grimes 		exit(0);
3599b50d902SRodney W. Grimes 	}
3609b50d902SRodney W. Grimes 	if (gflag) {
3619b50d902SRodney W. Grimes 		if (sflag)
3629b50d902SRodney W. Grimes 			mrt_stats(nl[N_MRTPROTO].n_value,
3639b50d902SRodney W. Grimes 			    nl[N_MRTSTAT].n_value);
3649b50d902SRodney W. Grimes 		else
3659b50d902SRodney W. Grimes 			mroutepr(nl[N_MRTPROTO].n_value,
3669b50d902SRodney W. Grimes 			    nl[N_MRTTABLE].n_value,
3679b50d902SRodney W. Grimes 			    nl[N_VIFTABLE].n_value);
3689b50d902SRodney W. Grimes 		exit(0);
3699b50d902SRodney W. Grimes 	}
3709b50d902SRodney W. Grimes 	if (af == AF_INET || af == AF_UNSPEC) {
3719b50d902SRodney W. Grimes 		setprotoent(1);
3729b50d902SRodney W. Grimes 		setservent(1);
3739b50d902SRodney W. Grimes 		/* ugh, this is O(MN) ... why do we do this? */
3749b50d902SRodney W. Grimes 		while (p = getprotoent()) {
3759b50d902SRodney W. Grimes 			for (tp = protox; tp->pr_name; tp++)
3769b50d902SRodney W. Grimes 				if (strcmp(tp->pr_name, p->p_name) == 0)
3779b50d902SRodney W. Grimes 					break;
3789b50d902SRodney W. Grimes 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
3799b50d902SRodney W. Grimes 				continue;
3809b50d902SRodney W. Grimes 			printproto(tp, p->p_name);
3819b50d902SRodney W. Grimes 		}
3829b50d902SRodney W. Grimes 		endprotoent();
3839b50d902SRodney W. Grimes 	}
3849b50d902SRodney W. Grimes 	if (af == AF_NS || af == AF_UNSPEC)
3859b50d902SRodney W. Grimes 		for (tp = nsprotox; tp->pr_name; tp++)
3869b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
3879b50d902SRodney W. Grimes 	if (af == AF_ISO || af == AF_UNSPEC)
3889b50d902SRodney W. Grimes 		for (tp = isoprotox; tp->pr_name; tp++)
3899b50d902SRodney W. Grimes 			printproto(tp, tp->pr_name);
3909b50d902SRodney W. Grimes 	if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
3919b50d902SRodney W. Grimes 		unixpr(nl[N_UNIXSW].n_value);
3929b50d902SRodney W. Grimes 	exit(0);
3939b50d902SRodney W. Grimes }
3949b50d902SRodney W. Grimes 
3959b50d902SRodney W. Grimes /*
3969b50d902SRodney W. Grimes  * Print out protocol statistics or control blocks (per sflag).
3979b50d902SRodney W. Grimes  * If the interface was not specifically requested, and the symbol
3989b50d902SRodney W. Grimes  * is not in the namelist, ignore this one.
3999b50d902SRodney W. Grimes  */
4009b50d902SRodney W. Grimes static void
4019b50d902SRodney W. Grimes printproto(tp, name)
4029b50d902SRodney W. Grimes 	register struct protox *tp;
4039b50d902SRodney W. Grimes 	char *name;
4049b50d902SRodney W. Grimes {
4059b50d902SRodney W. Grimes 	void (*pr)();
4069b50d902SRodney W. Grimes 	u_long off;
4079b50d902SRodney W. Grimes 
4089b50d902SRodney W. Grimes 	if (sflag) {
4099b50d902SRodney W. Grimes 		pr = tp->pr_stats;
4109b50d902SRodney W. Grimes 		off = nl[tp->pr_sindex].n_value;
4119b50d902SRodney W. Grimes 	} else {
4129b50d902SRodney W. Grimes 		pr = tp->pr_cblocks;
4139b50d902SRodney W. Grimes 		off = nl[tp->pr_index].n_value;
4149b50d902SRodney W. Grimes 	}
4159b50d902SRodney W. Grimes 	if (pr != NULL && (off || af != AF_UNSPEC))
4169b50d902SRodney W. Grimes 		(*pr)(off, name);
4179b50d902SRodney W. Grimes }
4189b50d902SRodney W. Grimes 
4199b50d902SRodney W. Grimes /*
4209b50d902SRodney W. Grimes  * Read kernel memory, return 0 on success.
4219b50d902SRodney W. Grimes  */
4229b50d902SRodney W. Grimes int
4239b50d902SRodney W. Grimes kread(addr, buf, size)
4249b50d902SRodney W. Grimes 	u_long addr;
4259b50d902SRodney W. Grimes 	char *buf;
4269b50d902SRodney W. Grimes 	int size;
4279b50d902SRodney W. Grimes {
4289b50d902SRodney W. Grimes 
4299b50d902SRodney W. Grimes 	if (kvm_read(kvmd, addr, buf, size) != size) {
4309b50d902SRodney W. Grimes 		/* XXX this duplicates kvm_read's error printout */
4319b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s: kvm_read %s\n", prog,
4329b50d902SRodney W. Grimes 		    kvm_geterr(kvmd));
4339b50d902SRodney W. Grimes 		return (-1);
4349b50d902SRodney W. Grimes 	}
4359b50d902SRodney W. Grimes 	return (0);
4369b50d902SRodney W. Grimes }
4379b50d902SRodney W. Grimes 
4389b50d902SRodney W. Grimes char *
4399b50d902SRodney W. Grimes plural(n)
4409b50d902SRodney W. Grimes 	int n;
4419b50d902SRodney W. Grimes {
4429b50d902SRodney W. Grimes 	return (n != 1 ? "s" : "");
4439b50d902SRodney W. Grimes }
4449b50d902SRodney W. Grimes 
4459b50d902SRodney W. Grimes char *
4469b50d902SRodney W. Grimes plurales(n)
4479b50d902SRodney W. Grimes 	int n;
4489b50d902SRodney W. Grimes {
4499b50d902SRodney W. Grimes 	return (n != 1 ? "es" : "");
4509b50d902SRodney W. Grimes }
4519b50d902SRodney W. Grimes 
4529b50d902SRodney W. Grimes /*
4539b50d902SRodney W. Grimes  * Find the protox for the given "well-known" name.
4549b50d902SRodney W. Grimes  */
4559b50d902SRodney W. Grimes static struct protox *
4569b50d902SRodney W. Grimes knownname(name)
4579b50d902SRodney W. Grimes 	char *name;
4589b50d902SRodney W. Grimes {
4599b50d902SRodney W. Grimes 	struct protox **tpp, *tp;
4609b50d902SRodney W. Grimes 
4619b50d902SRodney W. Grimes 	for (tpp = protoprotox; *tpp; tpp++)
4629b50d902SRodney W. Grimes 		for (tp = *tpp; tp->pr_name; tp++)
4639b50d902SRodney W. Grimes 			if (strcmp(tp->pr_name, name) == 0)
4649b50d902SRodney W. Grimes 				return (tp);
4659b50d902SRodney W. Grimes 	return (NULL);
4669b50d902SRodney W. Grimes }
4679b50d902SRodney W. Grimes 
4689b50d902SRodney W. Grimes /*
4699b50d902SRodney W. Grimes  * Find the protox corresponding to name.
4709b50d902SRodney W. Grimes  */
4719b50d902SRodney W. Grimes static struct protox *
4729b50d902SRodney W. Grimes name2protox(name)
4739b50d902SRodney W. Grimes 	char *name;
4749b50d902SRodney W. Grimes {
4759b50d902SRodney W. Grimes 	struct protox *tp;
4769b50d902SRodney W. Grimes 	char **alias;			/* alias from p->aliases */
4779b50d902SRodney W. Grimes 	struct protoent *p;
4789b50d902SRodney W. Grimes 
4799b50d902SRodney W. Grimes 	/*
4809b50d902SRodney W. Grimes 	 * Try to find the name in the list of "well-known" names. If that
4819b50d902SRodney W. Grimes 	 * fails, check if name is an alias for an Internet protocol.
4829b50d902SRodney W. Grimes 	 */
4839b50d902SRodney W. Grimes 	if (tp = knownname(name))
4849b50d902SRodney W. Grimes 		return (tp);
4859b50d902SRodney W. Grimes 
4869b50d902SRodney W. Grimes 	setprotoent(1);			/* make protocol lookup cheaper */
4879b50d902SRodney W. Grimes 	while (p = getprotoent()) {
4889b50d902SRodney W. Grimes 		/* assert: name not same as p->name */
4899b50d902SRodney W. Grimes 		for (alias = p->p_aliases; *alias; alias++)
4909b50d902SRodney W. Grimes 			if (strcmp(name, *alias) == 0) {
4919b50d902SRodney W. Grimes 				endprotoent();
4929b50d902SRodney W. Grimes 				return (knownname(p->p_name));
4939b50d902SRodney W. Grimes 			}
4949b50d902SRodney W. Grimes 	}
4959b50d902SRodney W. Grimes 	endprotoent();
4969b50d902SRodney W. Grimes 	return (NULL);
4979b50d902SRodney W. Grimes }
4989b50d902SRodney W. Grimes 
4999b50d902SRodney W. Grimes static void
5009b50d902SRodney W. Grimes usage()
5019b50d902SRodney W. Grimes {
5029b50d902SRodney W. Grimes 	(void)fprintf(stderr,
5039b50d902SRodney W. Grimes "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", prog);
5049b50d902SRodney W. Grimes 	(void)fprintf(stderr,
505e1e293a5SDavid Greenman "       %s [-bdghimnrs] [-f address_family] [-M core] [-N system]\n", prog);
5069b50d902SRodney W. Grimes 	(void)fprintf(stderr,
507e1e293a5SDavid Greenman "       %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n", prog);
5089b50d902SRodney W. Grimes 	(void)fprintf(stderr,
5099b50d902SRodney W. Grimes "       %s [-M core] [-N system] [-p protocol]\n", prog);
5109b50d902SRodney W. Grimes 	exit(1);
5119b50d902SRodney W. Grimes }
512