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