Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.32
      1 /*	$NetBSD: main.c,v 1.32 2001/02/19 23:03:50 cgd 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.32 2001/02/19 23:03:50 cgd 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 	{ "" },
    183 };
    184 
    185 struct protox {
    186 	u_char	pr_index;		/* index into nlist of cb head */
    187 	u_char	pr_sindex;		/* index into nlist of stat block */
    188 	u_char	pr_wanted;		/* 1 if wanted, 0 otherwise */
    189 	void	(*pr_cblocks)		/* control blocks printing routine */
    190 			__P((u_long, char *));
    191 	void	(*pr_stats)		/* statistics printing routine */
    192 			__P((u_long, char *));
    193 	void	(*pr_istats)
    194 			__P((char *));	/* per/if statistics printing routine */
    195 	void	(*pr_dump)		/* PCB state dump routine */
    196 			__P((u_long));
    197 	char	*pr_name;		/* well-known name */
    198 } protox[] = {
    199 	{ N_TCBTABLE,	N_TCPSTAT,	1,	protopr,
    200 	  tcp_stats,	NULL,		tcp_dump,	"tcp" },
    201 	{ N_UDBTABLE,	N_UDPSTAT,	1,	protopr,
    202 	  udp_stats,	NULL,		0,	"udp" },
    203 	{ -1,		N_IPSTAT,	1,	0,
    204 	  ip_stats,	NULL,		0,	"ip" },
    205 	{ -1,		N_ICMPSTAT,	1,	0,
    206 	  icmp_stats,	NULL,		0,	"icmp" },
    207 	{ -1,		N_IGMPSTAT,	1,	0,
    208 	  igmp_stats,	NULL,		0,	"igmp" },
    209 #ifdef IPSEC
    210 	{ -1,		N_IPSECSTAT,	1,	0,
    211 	  ipsec_stats,	NULL,		0,	"ipsec" },
    212 #endif
    213 	{ -1,		-1,		0,	0,
    214 	  0,		NULL,		0,	0 }
    215 };
    216 
    217 #ifdef INET6
    218 struct protox ip6protox[] = {
    219 	{ -1,		N_IP6STAT,	1,	0,
    220 	  ip6_stats,	ip6_ifstats,	0,	"ip6" },
    221 	{ -1,		N_ICMP6STAT,	1,	0,
    222 	  icmp6_stats,	icmp6_ifstats,	0,	"icmp6" },
    223 #ifdef TCP6
    224 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    225 	  tcp6_stats,	NULL,		tcp6_dump,	"tcp6" },
    226 #else
    227 	{ N_TCB6,	N_TCP6STAT,	1,	ip6protopr,
    228 	  tcp_stats,	NULL,		tcp_dump,	"tcp6" },
    229 #endif
    230 	{ N_UDB6,	N_UDP6STAT,	1,	ip6protopr,
    231 	  udp6_stats,	NULL,		0,	"udp6" },
    232 #ifdef IPSEC
    233 	{ -1,		N_IPSEC6STAT,	1,	0,
    234 	  ipsec_stats,	NULL,		0,	"ipsec6" },
    235 #endif
    236 	{ -1,		N_PIM6STAT,	1,	0,
    237 	  pim6_stats,	NULL,		0,	"pim6" },
    238 	{ -1,		-1,		0,	0,
    239 	  0,		NULL,		0,	0 }
    240 };
    241 #endif
    242 
    243 struct protox arpprotox[] = {
    244 	{ -1,		N_ARPSTAT,	1,	0,
    245 	  arp_stats,	NULL,		0,	"arp" },
    246 	{ -1,		-1,		0,	0,
    247 	  0,		NULL,		0,	0 }
    248 };
    249 
    250 #ifdef IPSEC
    251 struct protox pfkeyprotox[] = {
    252 	{ -1,		N_PFKEYSTAT,	1,	0,
    253 	  pfkey_stats,	NULL,		0,	"pfkey" },
    254 	{ -1,		-1,		0,	0,
    255 	  0,		NULL,		0,	0 }
    256 };
    257 #endif
    258 
    259 #ifndef SMALL
    260 struct protox atalkprotox[] = {
    261 	{ N_DDPCB,	N_DDPSTAT,	1,	atalkprotopr,
    262 	  ddp_stats,	NULL,		0,	"ddp" },
    263 	{ -1,		-1,		0,	0,
    264 	  0,		NULL,		0 }
    265 };
    266 
    267 struct protox nsprotox[] = {
    268 	{ N_IDP,	N_IDPSTAT,	1,	nsprotopr,
    269 	  idp_stats,	NULL,		0,	"idp" },
    270 	{ N_IDP,	N_SPPSTAT,	1,	nsprotopr,
    271 	  spp_stats,	NULL,		0,	"spp" },
    272 	{ -1,		N_NSERR,	1,	0,
    273 	  nserr_stats,	NULL,		0,	"ns_err" },
    274 	{ -1,		-1,		0,	0,
    275 	  0,		NULL,		0 }
    276 };
    277 
    278 struct protox isoprotox[] = {
    279 	{ ISO_TP,	N_TPSTAT,	1,	iso_protopr,
    280 	  tp_stats,	NULL,		0,	"tp" },
    281 	{ N_CLTP,	N_CLTPSTAT,	1,	iso_protopr,
    282 	  cltp_stats,	NULL,		0,	"cltp" },
    283 	{ -1,		N_CLNPSTAT,	1,	 0,
    284 	  clnp_stats,	NULL,		0,	"clnp"},
    285 	{ -1,		N_ESISSTAT,	1,	 0,
    286 	  esis_stats,	NULL,		0,	"esis"},
    287 	{ -1,		-1,		0,	0,
    288 	  0,		NULL,		0,	0 }
    289 };
    290 #endif
    291 
    292 struct protox *protoprotox[] = { protox,
    293 #ifdef INET6
    294 				 ip6protox,
    295 #endif
    296 				 arpprotox,
    297 #ifdef IPSEC
    298 				 pfkeyprotox,
    299 #endif
    300 #ifndef SMALL
    301 				 atalkprotox, nsprotox, isoprotox,
    302 #endif
    303 				 NULL };
    304 
    305 int main __P((int, char *[]));
    306 static void printproto __P((struct protox *, char *));
    307 static void usage __P((void));
    308 static struct protox *name2protox __P((char *));
    309 static struct protox *knownname __P((char *));
    310 
    311 kvm_t *kvmd;
    312 
    313 int
    314 main(argc, argv)
    315 	int argc;
    316 	char *argv[];
    317 {
    318 	struct protoent *p;
    319 	struct protox *tp;	/* for printing cblocks & stats */
    320 	int ch;
    321 	char *nlistf = NULL, *memf = NULL;
    322 	char buf[_POSIX2_LINE_MAX], *cp;
    323 	u_long pcbaddr;
    324 	gid_t egid = getegid();
    325 
    326 	(void)setegid(getgid());
    327 	tp = NULL;
    328 	af = AF_UNSPEC;
    329 	pcbaddr = 0;
    330 
    331 	while ((ch = getopt(argc, argv, "Aabdf:ghI:LliM:mN:nP:p:rstuvw:")) != -1)
    332 		switch(ch) {
    333 		case 'A':
    334 			Aflag = 1;
    335 			break;
    336 		case 'a':
    337 			aflag = 1;
    338 			break;
    339 		case 'b':
    340 			bflag = 1;
    341 			break;
    342 		case 'd':
    343 			dflag = 1;
    344 			break;
    345 		case 'f':
    346 			if (strcmp(optarg, "ns") == 0)
    347 				af = AF_NS;
    348 			else if (strcmp(optarg, "inet") == 0)
    349 				af = AF_INET;
    350 			else if (strcmp(optarg, "inet6") == 0)
    351 				af = AF_INET6;
    352 			else if (strcmp(optarg, "arp") == 0)
    353 				af = AF_ARP;
    354 			else if (strcmp(optarg, "pfkey") == 0)
    355 				af = PF_KEY;
    356 			else if (strcmp(optarg, "unix") == 0
    357 			    || strcmp(optarg, "local") == 0)
    358 				af = AF_LOCAL;
    359 			else if (strcmp(optarg, "iso") == 0)
    360 				af = AF_ISO;
    361 			else if (strcmp(optarg, "atalk") == 0)
    362 				af = AF_APPLETALK;
    363 			else
    364 				errx(1, "%s: unknown address family",
    365 				    optarg);
    366 			break;
    367 #ifndef SMALL
    368 		case 'g':
    369 			gflag = 1;
    370 			break;
    371 #endif
    372 		case 'I':
    373 			iflag = 1;
    374 			interface = optarg;
    375 			break;
    376 		case 'i':
    377 			iflag = 1;
    378 			break;
    379 		case 'L':
    380 			Lflag = 1;
    381 			break;
    382 		case 'l':
    383 			lflag = 1;
    384 			break;
    385 		case 'M':
    386 			memf = optarg;
    387 			break;
    388 		case 'm':
    389 			mflag = 1;
    390 			break;
    391 		case 'N':
    392 			nlistf = optarg;
    393 			break;
    394 		case 'n':
    395 			nflag = 1;
    396 			break;
    397 		case 'P':
    398 			pcbaddr = strtoul(optarg, &cp, 16);
    399 			if (*cp != '\0' || errno == ERANGE)
    400 				errx(1, "invalid PCB address %s",
    401 				    optarg);
    402 			Pflag = 1;
    403 			break;
    404 		case 'p':
    405 			if ((tp = name2protox(optarg)) == NULL)
    406 				errx(1, "%s: unknown or uninstrumented protocol",
    407 				    optarg);
    408 			pflag = 1;
    409 			break;
    410 		case 'r':
    411 			rflag = 1;
    412 			break;
    413 		case 's':
    414 			++sflag;
    415 			break;
    416 		case 't':
    417 			tflag = 1;
    418 			break;
    419 		case 'u':
    420 			af = AF_LOCAL;
    421 			break;
    422 		case 'v':
    423 			vflag = 1;
    424 			break;
    425 		case 'w':
    426 			interval = atoi(optarg);
    427 			iflag = 1;
    428 			break;
    429 		case '?':
    430 		default:
    431 			usage();
    432 		}
    433 	argv += optind;
    434 	argc -= optind;
    435 
    436 #define	BACKWARD_COMPATIBILITY
    437 #ifdef	BACKWARD_COMPATIBILITY
    438 	if (*argv) {
    439 		if (isdigit(**argv)) {
    440 			interval = atoi(*argv);
    441 			if (interval <= 0)
    442 				usage();
    443 			++argv;
    444 			iflag = 1;
    445 		}
    446 		if (*argv) {
    447 			nlistf = *argv;
    448 			if (*++argv)
    449 				memf = *argv;
    450 		}
    451 	}
    452 #endif
    453 
    454 	/*
    455 	 * Discard setgid privileges.  If not the running kernel, we toss
    456 	 * them away totally so that bad guys can't print interesting stuff
    457 	 * from kernel memory, otherwise switch back to kmem for the
    458 	 * duration of the kvm_openfiles() call.
    459 	 */
    460 	if (nlistf != NULL || memf != NULL || Pflag)
    461 		(void)setgid(getgid());
    462 	else
    463 		(void)setegid(egid);
    464 
    465 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    466 	    buf)) == NULL)
    467 		errx(1, "%s", buf);
    468 
    469 	/* do this now anyway */
    470 	if (nlistf == NULL && memf == NULL)
    471 		(void)setgid(getgid());
    472 
    473 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    474 		if (nlistf)
    475 			errx(1, "%s: no namelist", nlistf);
    476 		else
    477 			errx(1, "no namelist");
    478 	}
    479 	if (mflag) {
    480 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    481 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
    482 		    nl[N_MCLPOOL].n_value);
    483 		exit(0);
    484 	}
    485 	if (Pflag) {
    486 		if (tp == NULL) {
    487 			/* Default to TCP. */
    488 			tp = name2protox("tcp");
    489 		}
    490 		if (tp->pr_dump)
    491 			(*tp->pr_dump)(pcbaddr);
    492 		else
    493 			printf("%s: no PCB dump routine\n", tp->pr_name);
    494 		exit(0);
    495 	}
    496 	if (pflag) {
    497 		if (iflag && tp->pr_istats)
    498 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
    499 		else if (tp->pr_stats)
    500 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    501 				tp->pr_name);
    502 		else
    503 			printf("%s: no stats routine\n", tp->pr_name);
    504 		exit(0);
    505 	}
    506 	/*
    507 	 * Keep file descriptors open to avoid overhead
    508 	 * of open/close on each call to get* routines.
    509 	 */
    510 	sethostent(1);
    511 	setnetent(1);
    512 	if (iflag) {
    513 		if (af != AF_UNSPEC)
    514 			goto protostat;
    515 
    516 		intpr(interval, nl[N_IFNET].n_value, NULL);
    517 		exit(0);
    518 	}
    519 	if (rflag) {
    520 		if (sflag)
    521 			rt_stats(nl[N_RTSTAT].n_value);
    522 		else
    523 			routepr(nl[N_RTREE].n_value);
    524 		exit(0);
    525 	}
    526 #ifndef SMALL
    527 	if (gflag) {
    528 		if (sflag) {
    529 			if (af == AF_INET || af == AF_UNSPEC)
    530 				mrt_stats(nl[N_MRTPROTO].n_value,
    531 					  nl[N_MRTSTAT].n_value);
    532 #ifdef INET6
    533 			if (af == AF_INET6 || af == AF_UNSPEC)
    534 				mrt6_stats(nl[N_MRT6PROTO].n_value,
    535 					   nl[N_MRT6STAT].n_value);
    536 #endif
    537 		}
    538 		else {
    539 			if (af == AF_INET || af == AF_UNSPEC)
    540 				mroutepr(nl[N_MRTPROTO].n_value,
    541 					 nl[N_MFCHASHTBL].n_value,
    542 					 nl[N_MFCHASH].n_value,
    543 					 nl[N_VIFTABLE].n_value);
    544 #ifdef INET6
    545 			if (af == AF_INET6 || af == AF_UNSPEC)
    546 				mroute6pr(nl[N_MRT6PROTO].n_value,
    547 					  nl[N_MF6CTABLE].n_value,
    548 					  nl[N_MIF6TABLE].n_value);
    549 #endif
    550 		}
    551 		exit(0);
    552 	}
    553 #endif
    554   protostat:
    555 	if (af == AF_INET || af == AF_UNSPEC) {
    556 		setprotoent(1);
    557 		setservent(1);
    558 		/* ugh, this is O(MN) ... why do we do this? */
    559 		while ((p = getprotoent()) != NULL) {
    560 			for (tp = protox; tp->pr_name; tp++)
    561 				if (strcmp(tp->pr_name, p->p_name) == 0)
    562 					break;
    563 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    564 				continue;
    565 			printproto(tp, p->p_name);
    566 			tp->pr_wanted = 0;
    567 		}
    568 		endprotoent();
    569 		for (tp = protox; tp->pr_name; tp++)
    570 			if (tp->pr_wanted)
    571 				printproto(tp, tp->pr_name);
    572 	}
    573 #ifdef INET6
    574 	if (af == AF_INET6 || af == AF_UNSPEC)
    575 		for (tp = ip6protox; tp->pr_name; tp++)
    576 			printproto(tp, tp->pr_name);
    577 #endif
    578 	if (af == AF_ARP || af == AF_UNSPEC)
    579 		for (tp = arpprotox; tp->pr_name; tp++)
    580 			printproto(tp, tp->pr_name);
    581 #ifdef IPSEC
    582 	if (af == PF_KEY || af == AF_UNSPEC)
    583 		for (tp = pfkeyprotox; tp->pr_name; tp++)
    584 			printproto(tp, tp->pr_name);
    585 #endif
    586 #ifndef SMALL
    587 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    588 		for (tp = atalkprotox; tp->pr_name; tp++)
    589 			printproto(tp, tp->pr_name);
    590 	if (af == AF_NS || af == AF_UNSPEC)
    591 		for (tp = nsprotox; tp->pr_name; tp++)
    592 			printproto(tp, tp->pr_name);
    593 	if (af == AF_ISO || af == AF_UNSPEC)
    594 		for (tp = isoprotox; tp->pr_name; tp++)
    595 			printproto(tp, tp->pr_name);
    596 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
    597 		unixpr(nl[N_UNIXSW].n_value);
    598 #endif
    599 	exit(0);
    600 }
    601 
    602 /*
    603  * Print out protocol statistics or control blocks (per sflag).
    604  * If the interface was not specifically requested, and the symbol
    605  * is not in the namelist, ignore this one.
    606  */
    607 static void
    608 printproto(tp, name)
    609 	struct protox *tp;
    610 	char *name;
    611 {
    612 	void (*pr) __P((u_long, char *));
    613 	u_long off;
    614 
    615 	if (sflag) {
    616 		if (iflag) {
    617 			if (tp->pr_istats)
    618 				intpr(interval, nl[N_IFNET].n_value,
    619 				      tp->pr_istats);
    620 			return;
    621 		}
    622 		else {
    623 			pr = tp->pr_stats;
    624 			off = nl[tp->pr_sindex].n_value;
    625 		}
    626 	} else {
    627 		pr = tp->pr_cblocks;
    628 		off = nl[tp->pr_index].n_value;
    629 	}
    630 	if (pr != NULL && (off || af != AF_UNSPEC))
    631 		(*pr)(off, name);
    632 }
    633 
    634 /*
    635  * Read kernel memory, return 0 on success.
    636  */
    637 int
    638 kread(addr, buf, size)
    639 	u_long addr;
    640 	char *buf;
    641 	int size;
    642 {
    643 
    644 	if (kvm_read(kvmd, addr, buf, size) != size) {
    645 		warnx("%s\n", kvm_geterr(kvmd));
    646 		return (-1);
    647 	}
    648 	return (0);
    649 }
    650 
    651 char *
    652 plural(n)
    653 	int n;
    654 {
    655 
    656 	return (n != 1 ? "s" : "");
    657 }
    658 
    659 char *
    660 plurales(n)
    661 	int n;
    662 {
    663 
    664 	return (n != 1 ? "es" : "");
    665 }
    666 
    667 /*
    668  * Find the protox for the given "well-known" name.
    669  */
    670 static struct protox *
    671 knownname(name)
    672 	char *name;
    673 {
    674 	struct protox **tpp, *tp;
    675 
    676 	for (tpp = protoprotox; *tpp; tpp++)
    677 		for (tp = *tpp; tp->pr_name; tp++)
    678 			if (strcmp(tp->pr_name, name) == 0)
    679 				return (tp);
    680 	return (NULL);
    681 }
    682 
    683 /*
    684  * Find the protox corresponding to name.
    685  */
    686 static struct protox *
    687 name2protox(name)
    688 	char *name;
    689 {
    690 	struct protox *tp;
    691 	char **alias;			/* alias from p->aliases */
    692 	struct protoent *p;
    693 
    694 	/*
    695 	 * Try to find the name in the list of "well-known" names. If that
    696 	 * fails, check if name is an alias for an Internet protocol.
    697 	 */
    698 	if ((tp = knownname(name)) != NULL)
    699 		return (tp);
    700 
    701 	setprotoent(1);			/* make protocol lookup cheaper */
    702 	while ((p = getprotoent()) != NULL) {
    703 		/* assert: name not same as p->name */
    704 		for (alias = p->p_aliases; *alias; alias++)
    705 			if (strcmp(name, *alias) == 0) {
    706 				endprotoent();
    707 				return (knownname(p->p_name));
    708 			}
    709 	}
    710 	endprotoent();
    711 	return (NULL);
    712 }
    713 
    714 static void
    715 usage()
    716 {
    717 	const char *progname = getprogname();
    718 
    719 	(void)fprintf(stderr,
    720 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
    721 	(void)fprintf(stderr,
    722 "       %s [-gimnrsv] [-f address_family] [-M core] [-N system]\n",
    723 	progname);
    724 	(void)fprintf(stderr,
    725 "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
    726 	(void)fprintf(stderr,
    727 "       %s [-M core] [-N system] [-p protocol]\n", progname);
    728 	(void)fprintf(stderr,
    729 "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", progname);
    730 	exit(1);
    731 }
    732