Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.36
      1 /*	$NetBSD: main.c,v 1.36 2002/07/02 21:34:18 soren Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1988, 1993
      5  *	Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1983, 1988, 1993\n\
     39 	Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "from: @(#)main.c	8.4 (Berkeley) 3/1/94";
     45 #else
     46 __RCSID("$NetBSD: main.c,v 1.36 2002/07/02 21:34:18 soren Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/file.h>
     52 #include <sys/protosw.h>
     53 #include <sys/socket.h>
     54 
     55 #include <netinet/in.h>
     56 
     57 #include <ctype.h>
     58 #include <err.h>
     59 #include <errno.h>
     60 #include <kvm.h>
     61 #include <limits.h>
     62 #include <netdb.h>
     63 #include <nlist.h>
     64 #include <paths.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 #include "netstat.h"
     70 
     71 struct nlist nl[] = {
     72 #define	N_MBSTAT	0
     73 	{ "_mbstat" },
     74 #define	N_IPSTAT	1
     75 	{ "_ipstat" },
     76 #define	N_TCBTABLE	2
     77 	{ "_tcbtable" },
     78 #define	N_TCPSTAT	3
     79 	{ "_tcpstat" },
     80 #define	N_UDBTABLE	4
     81 	{ "_udbtable" },
     82 #define	N_UDPSTAT	5
     83 	{ "_udpstat" },
     84 #define	N_IFNET		6
     85 	{ "_ifnet" },
     86 #define	N_IMP		7
     87 	{ "_imp_softc" },
     88 #define	N_ICMPSTAT	8
     89 	{ "_icmpstat" },
     90 #define	N_RTSTAT	9
     91 	{ "_rtstat" },
     92 #define	N_UNIXSW	10
     93 	{ "_unixsw" },
     94 #define N_IDP		11
     95 	{ "_nspcb"},
     96 #define N_IDPSTAT	12
     97 	{ "_idpstat"},
     98 #define N_SPPSTAT	13
     99 	{ "_spp_istat"},
    100 #define N_NSERR		14
    101 	{ "_ns_errstat"},
    102 #define	N_CLNPSTAT	15
    103 	{ "_clnp_stat"},
    104 #define	IN_NOTUSED	16
    105 	{ "_tp_inpcb" },
    106 #define	ISO_TP		17
    107 	{ "_tp_refinfo" },
    108 #define	N_TPSTAT	18
    109 	{ "_tp_stat" },
    110 #define	N_ESISSTAT	19
    111 	{ "_esis_stat"},
    112 #define N_NIMP		20
    113 	{ "_nimp"},
    114 #define N_RTREE		21
    115 	{ "_rt_tables"},
    116 #define N_CLTP		22
    117 	{ "_cltb"},
    118 #define N_CLTPSTAT	23
    119 	{ "_cltpstat"},
    120 #define	N_NFILE		24
    121 	{ "_nfile" },
    122 #define	N_FILE		25
    123 	{ "_file" },
    124 #define N_IGMPSTAT	26
    125 	{ "_igmpstat" },
    126 #define N_MRTPROTO	27
    127 	{ "_ip_mrtproto" },
    128 #define N_MRTSTAT	28
    129 	{ "_mrtstat" },
    130 #define N_MFCHASHTBL	29
    131 	{ "_mfchashtbl" },
    132 #define	N_MFCHASH	30
    133 	{ "_mfchash" },
    134 #define N_VIFTABLE	31
    135 	{ "_viftable" },
    136 #define N_MSIZE		32
    137 	{ "_msize" },
    138 #define N_MCLBYTES	33
    139 	{ "_mclbytes" },
    140 #define N_DDPSTAT	34
    141 	{ "_ddpstat"},
    142 #define N_DDPCB		35
    143 	{ "_ddpcb"},
    144 #define N_MBPOOL	36
    145 	{ "_mbpool" },
    146 #define N_MCLPOOL	37
    147 	{ "_mclpool" },
    148 #define N_DIVPCB	38
    149 	{ "_divcb"},
    150 #define N_DIVSTAT	39
    151 	{ "_divstat"},
    152 #define N_IP6STAT	40
    153 	{ "_ip6stat" },
    154 #define N_TCB6		41
    155 	{ "_tcb6" },
    156 #define N_TCP6STAT	42
    157 	{ "_tcp6stat" },
    158 #define N_UDB6		43
    159 	{ "_udb6" },
    160 #define N_UDP6STAT	44
    161 	{ "_udp6stat" },
    162 #define N_ICMP6STAT	45
    163 	{ "_icmp6stat" },
    164 #define N_IPSECSTAT	46
    165 	{ "_ipsecstat" },
    166 #define N_IPSEC6STAT	47
    167 	{ "_ipsec6stat" },
    168 #define N_PIM6STAT	48
    169 	{ "_pim6stat" },
    170 #define N_MRT6PROTO	49
    171 	{ "_ip6_mrtproto" },
    172 #define N_MRT6STAT	50
    173 	{ "_mrt6stat" },
    174 #define N_MF6CTABLE	51
    175 	{ "_mf6ctable" },
    176 #define N_MIF6TABLE	52
    177 	{ "_mif6table" },
    178 #define N_PFKEYSTAT	53
    179 	{ "_pfkeystat" },
    180 #define N_ARPSTAT	54
    181 	{ "_arpstat" },
    182 #define N_RIP6STAT	55
    183 	{ "_rip6stat" },
    184 	{ "" },
    185 };
    186 
    187 struct protox {
    188 	u_char	pr_index;		/* index into nlist of cb head */
    189 	u_char	pr_sindex;		/* index into nlist of stat block */
    190 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    191 	void	(*pr_cblocks)		/* control blocks printing routine */
    192 			__P((u_long, char *));
    193 	void	(*pr_stats)		/* statistics printing routine */
    194 			__P((u_long, char *));
    195 	void	(*pr_istats)
    196 			__P((char *));	/* per/if statistics printing routine */
    197 	void	(*pr_dump)		/* PCB state dump routine */
    198 			__P((u_long));
    199 	char	*pr_name;		/* well-known name */
    200 } protox[] = {
    201 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
    202 	  tcp_stats,	NULL,		tcp_dump,	"tcp" },
    203 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
    204 	  udp_stats,	NULL,		0,	"udp" },
    205 	{ -1,		N_IPSTAT,	1,	0,
    206 	  ip_stats,	NULL,		0,	"ip" },
    207 	{ -1,		N_ICMPSTAT,	1,	0,
    208 	  icmp_stats,	NULL,		0,	"icmp" },
    209 	{ -1,		N_IGMPSTAT,	1,	0,
    210 	  igmp_stats,	NULL,		0,	"igmp" },
    211 #ifdef IPSEC
    212 	{ -1,		N_IPSECSTAT,	1,	0,
    213 	  ipsec_stats,	NULL,		0,	"ipsec" },
    214 #endif
    215 	{ -1,		-1,		0,	0,
    216 	  0,		NULL,		0,	0 }
    217 };
    218 
    219 #ifdef INET6
    220 struct protox ip6protox[] = {
    221 	{ -1,		N_IP6STAT,	1,	0,
    222 	  ip6_stats,	ip6_ifstats,	0,	"ip6" },
    223 	{ -1,		N_ICMP6STAT,	1,	0,
    224 	  icmp6_stats,	icmp6_ifstats,	0,	"icmp6" },
    225 #ifdef TCP6
    226 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    227 	  tcp6_stats,	NULL,		tcp6_dump,	"tcp6" },
    228 #else
    229 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    230 	  tcp_stats,	NULL,		tcp_dump,	"tcp6" },
    231 #endif
    232 	{ N_UDB6,	N_UDP6STAT,	1,	ip6protopr,
    233 	  udp6_stats,	NULL,		0,	"udp6" },
    234 #ifdef IPSEC
    235 	{ -1,		N_IPSEC6STAT,	1,	0,
    236 	  ipsec_stats,	NULL,		0,	"ipsec6" },
    237 #endif
    238 	{ -1,		N_PIM6STAT,	1,	0,
    239 	  pim6_stats,	NULL,		0,	"pim6" },
    240 	{ -1,		N_RIP6STAT,	1,	0,
    241 	  rip6_stats,	NULL,		0,	"rip6" },
    242 	{ -1,		-1,		0,	0,
    243 	  0,		NULL,		0,	0 }
    244 };
    245 #endif
    246 
    247 struct protox arpprotox[] = {
    248 	{ -1,		N_ARPSTAT,	1,	0,
    249 	  arp_stats,	NULL,		0,	"arp" },
    250 	{ -1,		-1,		0,	0,
    251 	  0,		NULL,		0,	0 }
    252 };
    253 
    254 #ifdef IPSEC
    255 struct protox pfkeyprotox[] = {
    256 	{ -1,		N_PFKEYSTAT,	1,	0,
    257 	  pfkey_stats,	NULL,		0,	"pfkey" },
    258 	{ -1,		-1,		0,	0,
    259 	  0,		NULL,		0,	0 }
    260 };
    261 #endif
    262 
    263 #ifndef SMALL
    264 struct protox atalkprotox[] = {
    265 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    266 	  ddp_stats,	NULL,		0,	"ddp" },
    267 	{ -1,		-1,		0,	0,
    268 	  0,		NULL,		0 }
    269 };
    270 
    271 struct protox nsprotox[] = {
    272 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    273 	  idp_stats,	NULL,		0,	"idp" },
    274 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    275 	  spp_stats,	NULL,		0,	"spp" },
    276 	{ -1,		N_NSERR,	1,	0,
    277 	  nserr_stats,	NULL,		0,	"ns_err" },
    278 	{ -1,		-1,		0,	0,
    279 	  0,		NULL,		0 }
    280 };
    281 
    282 struct protox isoprotox[] = {
    283 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    284 	  tp_stats,	NULL,		0,	"tp" },
    285 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    286 	  cltp_stats,	NULL,		0,	"cltp" },
    287 	{ -1,		N_CLNPSTAT,	1,	 0,
    288 	  clnp_stats,	NULL,		0,	"clnp"},
    289 	{ -1,		N_ESISSTAT,	1,	 0,
    290 	  esis_stats,	NULL,		0,	"esis"},
    291 	{ -1,		-1,		0,	0,
    292 	  0,		NULL,		0,	0 }
    293 };
    294 #endif
    295 
    296 struct protox *protoprotox[] = { protox,
    297 #ifdef INET6
    298 				 ip6protox,
    299 #endif
    300 				 arpprotox,
    301 #ifdef IPSEC
    302 				 pfkeyprotox,
    303 #endif
    304 #ifndef SMALL
    305 				 atalkprotox, nsprotox, isoprotox,
    306 #endif
    307 				 NULL };
    308 
    309 int main __P((int, char *[]));
    310 static void printproto __P((struct protox *, char *));
    311 static void usage __P((void));
    312 static struct protox *name2protox __P((char *));
    313 static struct protox *knownname __P((char *));
    314 
    315 kvm_t *kvmd;
    316 
    317 int
    318 main(argc, argv)
    319 	int argc;
    320 	char *argv[];
    321 {
    322 	struct protoent *p;
    323 	struct protox *tp;	/* for printing cblocks & stats */
    324 	int ch;
    325 	char *nlistf = NULL, *memf = NULL;
    326 	char buf[_POSIX2_LINE_MAX], *cp;
    327 	u_long pcbaddr;
    328 	gid_t egid = getegid();
    329 
    330 	(void)setegid(getgid());
    331 	tp = NULL;
    332 	af = AF_UNSPEC;
    333 	pcbaddr = 0;
    334 
    335 	while ((ch = getopt(argc, argv, "Aabdf:ghI:LliM:mN:nP:p:rsStuvw:")) != -1)
    336 		switch(ch) {
    337 		case 'A':
    338 			Aflag = 1;
    339 			break;
    340 		case 'a':
    341 			aflag = 1;
    342 			break;
    343 		case 'b':
    344 			bflag = 1;
    345 			break;
    346 		case 'd':
    347 			dflag = 1;
    348 			break;
    349 		case 'f':
    350 			if (strcmp(optarg, "ns") == 0)
    351 				af = AF_NS;
    352 			else if (strcmp(optarg, "inet") == 0)
    353 				af = AF_INET;
    354 			else if (strcmp(optarg, "inet6") == 0)
    355 				af = AF_INET6;
    356 			else if (strcmp(optarg, "arp") == 0)
    357 				af = AF_ARP;
    358 			else if (strcmp(optarg, "pfkey") == 0)
    359 				af = PF_KEY;
    360 			else if (strcmp(optarg, "unix") == 0
    361 			    || strcmp(optarg, "local") == 0)
    362 				af = AF_LOCAL;
    363 			else if (strcmp(optarg, "iso") == 0)
    364 				af = AF_ISO;
    365 			else if (strcmp(optarg, "atalk") == 0)
    366 				af = AF_APPLETALK;
    367 			else
    368 				errx(1, "%s: unknown address family",
    369 				    optarg);
    370 			break;
    371 #ifndef SMALL
    372 		case 'g':
    373 			gflag = 1;
    374 			break;
    375 #endif
    376 		case 'I':
    377 			iflag = 1;
    378 			interface = optarg;
    379 			break;
    380 		case 'i':
    381 			iflag = 1;
    382 			break;
    383 		case 'L':
    384 			Lflag = 1;
    385 			break;
    386 		case 'l':
    387 			lflag = 1;
    388 			break;
    389 		case 'M':
    390 			memf = optarg;
    391 			break;
    392 		case 'm':
    393 			mflag = 1;
    394 			break;
    395 		case 'N':
    396 			nlistf = optarg;
    397 			break;
    398 		case 'n':
    399 			numeric_addr = numeric_port = 1;
    400 			break;
    401 		case 'P':
    402 			pcbaddr = strtoul(optarg, &cp, 16);
    403 			if (*cp != '\0' || errno == ERANGE)
    404 				errx(1, "invalid PCB address %s",
    405 				    optarg);
    406 			Pflag = 1;
    407 			break;
    408 		case 'p':
    409 			if ((tp = name2protox(optarg)) == NULL)
    410 				errx(1, "%s: unknown or uninstrumented protocol",
    411 				    optarg);
    412 			pflag = 1;
    413 			break;
    414 		case 'r':
    415 			rflag = 1;
    416 			break;
    417 		case 's':
    418 			++sflag;
    419 			break;
    420 		case 'S':
    421 			numeric_addr = 1;
    422 			break;
    423 		case 't':
    424 			tflag = 1;
    425 			break;
    426 		case 'u':
    427 			af = AF_LOCAL;
    428 			break;
    429 		case 'v':
    430 			vflag = 1;
    431 			break;
    432 		case 'w':
    433 			interval = atoi(optarg);
    434 			iflag = 1;
    435 			break;
    436 		case '?':
    437 		default:
    438 			usage();
    439 		}
    440 	argv += optind;
    441 	argc -= optind;
    442 
    443 #define	BACKWARD_COMPATIBILITY
    444 #ifdef	BACKWARD_COMPATIBILITY
    445 	if (*argv) {
    446 		if (isdigit(**argv)) {
    447 			interval = atoi(*argv);
    448 			if (interval <= 0)
    449 				usage();
    450 			++argv;
    451 			iflag = 1;
    452 		}
    453 		if (*argv) {
    454 			nlistf = *argv;
    455 			if (*++argv)
    456 				memf = *argv;
    457 		}
    458 	}
    459 #endif
    460 
    461 	/*
    462 	 * Discard setgid privileges.  If not the running kernel, we toss
    463 	 * them away totally so that bad guys can't print interesting stuff
    464 	 * from kernel memory, otherwise switch back to kmem for the
    465 	 * duration of the kvm_openfiles() call.
    466 	 */
    467 	if (nlistf != NULL || memf != NULL || Pflag)
    468 		(void)setgid(getgid());
    469 	else
    470 		(void)setegid(egid);
    471 
    472 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    473 	    buf)) == NULL)
    474 		errx(1, "%s", buf);
    475 
    476 	/* do this now anyway */
    477 	if (nlistf == NULL && memf == NULL)
    478 		(void)setgid(getgid());
    479 
    480 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    481 		if (nlistf)
    482 			errx(1, "%s: no namelist", nlistf);
    483 		else
    484 			errx(1, "no namelist");
    485 	}
    486 	if (mflag) {
    487 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    488 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
    489 		    nl[N_MCLPOOL].n_value);
    490 		exit(0);
    491 	}
    492 	if (Pflag) {
    493 		if (tp == NULL) {
    494 			/* Default to TCP. */
    495 			tp = name2protox("tcp");
    496 		}
    497 		if (tp->pr_dump)
    498 			(*tp->pr_dump)(pcbaddr);
    499 		else
    500 			printf("%s: no PCB dump routine\n", tp->pr_name);
    501 		exit(0);
    502 	}
    503 	if (pflag) {
    504 		if (iflag && tp->pr_istats)
    505 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
    506 		else if (tp->pr_stats)
    507 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    508 				tp->pr_name);
    509 		else
    510 			printf("%s: no stats routine\n", tp->pr_name);
    511 		exit(0);
    512 	}
    513 	/*
    514 	 * Keep file descriptors open to avoid overhead
    515 	 * of open/close on each call to get* routines.
    516 	 */
    517 	sethostent(1);
    518 	setnetent(1);
    519 	if (iflag) {
    520 		if (af != AF_UNSPEC)
    521 			goto protostat;
    522 
    523 		intpr(interval, nl[N_IFNET].n_value, NULL);
    524 		exit(0);
    525 	}
    526 	if (rflag) {
    527 		if (sflag)
    528 			rt_stats(nl[N_RTSTAT].n_value);
    529 		else
    530 			routepr(nl[N_RTREE].n_value);
    531 		exit(0);
    532 	}
    533 #ifndef SMALL
    534 	if (gflag) {
    535 		if (sflag) {
    536 			if (af == AF_INET || af == AF_UNSPEC)
    537 				mrt_stats(nl[N_MRTPROTO].n_value,
    538 					  nl[N_MRTSTAT].n_value);
    539 #ifdef INET6
    540 			if (af == AF_INET6 || af == AF_UNSPEC)
    541 				mrt6_stats(nl[N_MRT6PROTO].n_value,
    542 					   nl[N_MRT6STAT].n_value);
    543 #endif
    544 		}
    545 		else {
    546 			if (af == AF_INET || af == AF_UNSPEC)
    547 				mroutepr(nl[N_MRTPROTO].n_value,
    548 					 nl[N_MFCHASHTBL].n_value,
    549 					 nl[N_MFCHASH].n_value,
    550 					 nl[N_VIFTABLE].n_value);
    551 #ifdef INET6
    552 			if (af == AF_INET6 || af == AF_UNSPEC)
    553 				mroute6pr(nl[N_MRT6PROTO].n_value,
    554 					  nl[N_MF6CTABLE].n_value,
    555 					  nl[N_MIF6TABLE].n_value);
    556 #endif
    557 		}
    558 		exit(0);
    559 	}
    560 #endif
    561   protostat:
    562 	if (af == AF_INET || af == AF_UNSPEC) {
    563 		setprotoent(1);
    564 		setservent(1);
    565 		/* ugh, this is O(MN) ... why do we do this? */
    566 		while ((p = getprotoent()) != NULL) {
    567 			for (tp = protox; tp->pr_name; tp++)
    568 				if (strcmp(tp->pr_name, p->p_name) == 0)
    569 					break;
    570 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    571 				continue;
    572 			printproto(tp, p->p_name);
    573 			tp->pr_wanted = 0;
    574 		}
    575 		endprotoent();
    576 		for (tp = protox; tp->pr_name; tp++)
    577 			if (tp->pr_wanted)
    578 				printproto(tp, tp->pr_name);
    579 	}
    580 #ifdef INET6
    581 	if (af == AF_INET6 || af == AF_UNSPEC)
    582 		for (tp = ip6protox; tp->pr_name; tp++)
    583 			printproto(tp, tp->pr_name);
    584 #endif
    585 	if (af == AF_ARP || af == AF_UNSPEC)
    586 		for (tp = arpprotox; tp->pr_name; tp++)
    587 			printproto(tp, tp->pr_name);
    588 #ifdef IPSEC
    589 	if (af == PF_KEY || af == AF_UNSPEC)
    590 		for (tp = pfkeyprotox; tp->pr_name; tp++)
    591 			printproto(tp, tp->pr_name);
    592 #endif
    593 #ifndef SMALL
    594 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    595 		for (tp = atalkprotox; tp->pr_name; tp++)
    596 			printproto(tp, tp->pr_name);
    597 	if (af == AF_NS || af == AF_UNSPEC)
    598 		for (tp = nsprotox; tp->pr_name; tp++)
    599 			printproto(tp, tp->pr_name);
    600 	if (af == AF_ISO || af == AF_UNSPEC)
    601 		for (tp = isoprotox; tp->pr_name; tp++)
    602 			printproto(tp, tp->pr_name);
    603 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
    604 		unixpr(nl[N_UNIXSW].n_value);
    605 #endif
    606 	exit(0);
    607 }
    608 
    609 /*
    610  * Print out protocol statistics or control blocks (per sflag).
    611  * If the interface was not specifically requested, and the symbol
    612  * is not in the namelist, ignore this one.
    613  */
    614 static void
    615 printproto(tp, name)
    616 	struct protox *tp;
    617 	char *name;
    618 {
    619 	void (*pr) __P((u_long, char *));
    620 	u_long off;
    621 
    622 	if (sflag) {
    623 		if (iflag) {
    624 			if (tp->pr_istats)
    625 				intpr(interval, nl[N_IFNET].n_value,
    626 				      tp->pr_istats);
    627 			return;
    628 		}
    629 		else {
    630 			pr = tp->pr_stats;
    631 			off = nl[tp->pr_sindex].n_value;
    632 		}
    633 	} else {
    634 		pr = tp->pr_cblocks;
    635 		off = nl[tp->pr_index].n_value;
    636 	}
    637 	if (pr != NULL && (off || af != AF_UNSPEC))
    638 		(*pr)(off, name);
    639 }
    640 
    641 /*
    642  * Read kernel memory, return 0 on success.
    643  */
    644 int
    645 kread(addr, buf, size)
    646 	u_long addr;
    647 	char *buf;
    648 	int size;
    649 {
    650 
    651 	if (kvm_read(kvmd, addr, buf, size) != size) {
    652 		warnx("%s", kvm_geterr(kvmd));
    653 		return (-1);
    654 	}
    655 	return (0);
    656 }
    657 
    658 char *
    659 plural(n)
    660 	int n;
    661 {
    662 
    663 	return (n != 1 ? "s" : "");
    664 }
    665 
    666 char *
    667 plurales(n)
    668 	int n;
    669 {
    670 
    671 	return (n != 1 ? "es" : "");
    672 }
    673 
    674 /*
    675  * Find the protox for the given "well-known" name.
    676  */
    677 static struct protox *
    678 knownname(name)
    679 	char *name;
    680 {
    681 	struct protox **tpp, *tp;
    682 
    683 	for (tpp = protoprotox; *tpp; tpp++)
    684 		for (tp = *tpp; tp->pr_name; tp++)
    685 			if (strcmp(tp->pr_name, name) == 0)
    686 				return (tp);
    687 	return (NULL);
    688 }
    689 
    690 /*
    691  * Find the protox corresponding to name.
    692  */
    693 static struct protox *
    694 name2protox(name)
    695 	char *name;
    696 {
    697 	struct protox *tp;
    698 	char **alias;			/* alias from p->aliases */
    699 	struct protoent *p;
    700 
    701 	/*
    702 	 * Try to find the name in the list of "well-known" names. If that
    703 	 * fails, check if name is an alias for an Internet protocol.
    704 	 */
    705 	if ((tp = knownname(name)) != NULL)
    706 		return (tp);
    707 
    708 	setprotoent(1);			/* make protocol lookup cheaper */
    709 	while ((p = getprotoent()) != NULL) {
    710 		/* assert: name not same as p->name */
    711 		for (alias = p->p_aliases; *alias; alias++)
    712 			if (strcmp(name, *alias) == 0) {
    713 				endprotoent();
    714 				return (knownname(p->p_name));
    715 			}
    716 	}
    717 	endprotoent();
    718 	return (NULL);
    719 }
    720 
    721 static void
    722 usage()
    723 {
    724 	const char *progname = getprogname();
    725 
    726 	(void)fprintf(stderr,
    727 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
    728 	(void)fprintf(stderr,
    729 "       %s [-bdgiLmnrsSv] [-f address_family] [-M core] [-N system]\n",
    730 	progname);
    731 	(void)fprintf(stderr,
    732 "       %s [-dn] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
    733 	(void)fprintf(stderr,
    734 "       %s [-p protocol] [-M core] [-N system]\n", progname);
    735 	(void)fprintf(stderr,
    736 "       %s [-p protocol] [-M core] [-N system] -P pcbaddr\n", progname);
    737 	(void)fprintf(stderr,
    738 "       %s [-p protocol] [-i] [-I Interface] \n", progname);
    739 	(void)fprintf(stderr,
    740 "       %s [-s] [-f address_family] [-i] [-I Interface]\n", progname);
    741 	exit(1);
    742 }
    743