Home | History | Annotate | Line # | Download | only in netstat
main.c revision 1.33
      1 /*	$NetBSD: main.c,v 1.33 2001/05/28 04:22:56 assar 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.33 2001/05/28 04:22:56 assar 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:rsStuvw:")) != -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 			numeric_addr = numeric_port = 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 'S':
    417 			numeric_addr = 1;
    418 			break;
    419 		case 't':
    420 			tflag = 1;
    421 			break;
    422 		case 'u':
    423 			af = AF_LOCAL;
    424 			break;
    425 		case 'v':
    426 			vflag = 1;
    427 			break;
    428 		case 'w':
    429 			interval = atoi(optarg);
    430 			iflag = 1;
    431 			break;
    432 		case '?':
    433 		default:
    434 			usage();
    435 		}
    436 	argv += optind;
    437 	argc -= optind;
    438 
    439 #define	BACKWARD_COMPATIBILITY
    440 #ifdef	BACKWARD_COMPATIBILITY
    441 	if (*argv) {
    442 		if (isdigit(**argv)) {
    443 			interval = atoi(*argv);
    444 			if (interval <= 0)
    445 				usage();
    446 			++argv;
    447 			iflag = 1;
    448 		}
    449 		if (*argv) {
    450 			nlistf = *argv;
    451 			if (*++argv)
    452 				memf = *argv;
    453 		}
    454 	}
    455 #endif
    456 
    457 	/*
    458 	 * Discard setgid privileges.  If not the running kernel, we toss
    459 	 * them away totally so that bad guys can't print interesting stuff
    460 	 * from kernel memory, otherwise switch back to kmem for the
    461 	 * duration of the kvm_openfiles() call.
    462 	 */
    463 	if (nlistf != NULL || memf != NULL || Pflag)
    464 		(void)setgid(getgid());
    465 	else
    466 		(void)setegid(egid);
    467 
    468 	if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
    469 	    buf)) == NULL)
    470 		errx(1, "%s", buf);
    471 
    472 	/* do this now anyway */
    473 	if (nlistf == NULL && memf == NULL)
    474 		(void)setgid(getgid());
    475 
    476 	if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
    477 		if (nlistf)
    478 			errx(1, "%s: no namelist", nlistf);
    479 		else
    480 			errx(1, "no namelist");
    481 	}
    482 	if (mflag) {
    483 		mbpr(nl[N_MBSTAT].n_value,  nl[N_MSIZE].n_value,
    484 		    nl[N_MCLBYTES].n_value, nl[N_MBPOOL].n_value,
    485 		    nl[N_MCLPOOL].n_value);
    486 		exit(0);
    487 	}
    488 	if (Pflag) {
    489 		if (tp == NULL) {
    490 			/* Default to TCP. */
    491 			tp = name2protox("tcp");
    492 		}
    493 		if (tp->pr_dump)
    494 			(*tp->pr_dump)(pcbaddr);
    495 		else
    496 			printf("%s: no PCB dump routine\n", tp->pr_name);
    497 		exit(0);
    498 	}
    499 	if (pflag) {
    500 		if (iflag && tp->pr_istats)
    501 			intpr(interval, nl[N_IFNET].n_value, tp->pr_istats);
    502 		else if (tp->pr_stats)
    503 			(*tp->pr_stats)(nl[tp->pr_sindex].n_value,
    504 				tp->pr_name);
    505 		else
    506 			printf("%s: no stats routine\n", tp->pr_name);
    507 		exit(0);
    508 	}
    509 	/*
    510 	 * Keep file descriptors open to avoid overhead
    511 	 * of open/close on each call to get* routines.
    512 	 */
    513 	sethostent(1);
    514 	setnetent(1);
    515 	if (iflag) {
    516 		if (af != AF_UNSPEC)
    517 			goto protostat;
    518 
    519 		intpr(interval, nl[N_IFNET].n_value, NULL);
    520 		exit(0);
    521 	}
    522 	if (rflag) {
    523 		if (sflag)
    524 			rt_stats(nl[N_RTSTAT].n_value);
    525 		else
    526 			routepr(nl[N_RTREE].n_value);
    527 		exit(0);
    528 	}
    529 #ifndef SMALL
    530 	if (gflag) {
    531 		if (sflag) {
    532 			if (af == AF_INET || af == AF_UNSPEC)
    533 				mrt_stats(nl[N_MRTPROTO].n_value,
    534 					  nl[N_MRTSTAT].n_value);
    535 #ifdef INET6
    536 			if (af == AF_INET6 || af == AF_UNSPEC)
    537 				mrt6_stats(nl[N_MRT6PROTO].n_value,
    538 					   nl[N_MRT6STAT].n_value);
    539 #endif
    540 		}
    541 		else {
    542 			if (af == AF_INET || af == AF_UNSPEC)
    543 				mroutepr(nl[N_MRTPROTO].n_value,
    544 					 nl[N_MFCHASHTBL].n_value,
    545 					 nl[N_MFCHASH].n_value,
    546 					 nl[N_VIFTABLE].n_value);
    547 #ifdef INET6
    548 			if (af == AF_INET6 || af == AF_UNSPEC)
    549 				mroute6pr(nl[N_MRT6PROTO].n_value,
    550 					  nl[N_MF6CTABLE].n_value,
    551 					  nl[N_MIF6TABLE].n_value);
    552 #endif
    553 		}
    554 		exit(0);
    555 	}
    556 #endif
    557   protostat:
    558 	if (af == AF_INET || af == AF_UNSPEC) {
    559 		setprotoent(1);
    560 		setservent(1);
    561 		/* ugh, this is O(MN) ... why do we do this? */
    562 		while ((p = getprotoent()) != NULL) {
    563 			for (tp = protox; tp->pr_name; tp++)
    564 				if (strcmp(tp->pr_name, p->p_name) == 0)
    565 					break;
    566 			if (tp->pr_name == 0 || tp->pr_wanted == 0)
    567 				continue;
    568 			printproto(tp, p->p_name);
    569 			tp->pr_wanted = 0;
    570 		}
    571 		endprotoent();
    572 		for (tp = protox; tp->pr_name; tp++)
    573 			if (tp->pr_wanted)
    574 				printproto(tp, tp->pr_name);
    575 	}
    576 #ifdef INET6
    577 	if (af == AF_INET6 || af == AF_UNSPEC)
    578 		for (tp = ip6protox; tp->pr_name; tp++)
    579 			printproto(tp, tp->pr_name);
    580 #endif
    581 	if (af == AF_ARP || af == AF_UNSPEC)
    582 		for (tp = arpprotox; tp->pr_name; tp++)
    583 			printproto(tp, tp->pr_name);
    584 #ifdef IPSEC
    585 	if (af == PF_KEY || af == AF_UNSPEC)
    586 		for (tp = pfkeyprotox; tp->pr_name; tp++)
    587 			printproto(tp, tp->pr_name);
    588 #endif
    589 #ifndef SMALL
    590 	if (af == AF_APPLETALK || af == AF_UNSPEC)
    591 		for (tp = atalkprotox; tp->pr_name; tp++)
    592 			printproto(tp, tp->pr_name);
    593 	if (af == AF_NS || af == AF_UNSPEC)
    594 		for (tp = nsprotox; tp->pr_name; tp++)
    595 			printproto(tp, tp->pr_name);
    596 	if (af == AF_ISO || af == AF_UNSPEC)
    597 		for (tp = isoprotox; tp->pr_name; tp++)
    598 			printproto(tp, tp->pr_name);
    599 	if ((af == AF_LOCAL || af == AF_UNSPEC) && !sflag)
    600 		unixpr(nl[N_UNIXSW].n_value);
    601 #endif
    602 	exit(0);
    603 }
    604 
    605 /*
    606  * Print out protocol statistics or control blocks (per sflag).
    607  * If the interface was not specifically requested, and the symbol
    608  * is not in the namelist, ignore this one.
    609  */
    610 static void
    611 printproto(tp, name)
    612 	struct protox *tp;
    613 	char *name;
    614 {
    615 	void (*pr) __P((u_long, char *));
    616 	u_long off;
    617 
    618 	if (sflag) {
    619 		if (iflag) {
    620 			if (tp->pr_istats)
    621 				intpr(interval, nl[N_IFNET].n_value,
    622 				      tp->pr_istats);
    623 			return;
    624 		}
    625 		else {
    626 			pr = tp->pr_stats;
    627 			off = nl[tp->pr_sindex].n_value;
    628 		}
    629 	} else {
    630 		pr = tp->pr_cblocks;
    631 		off = nl[tp->pr_index].n_value;
    632 	}
    633 	if (pr != NULL && (off || af != AF_UNSPEC))
    634 		(*pr)(off, name);
    635 }
    636 
    637 /*
    638  * Read kernel memory, return 0 on success.
    639  */
    640 int
    641 kread(addr, buf, size)
    642 	u_long addr;
    643 	char *buf;
    644 	int size;
    645 {
    646 
    647 	if (kvm_read(kvmd, addr, buf, size) != size) {
    648 		warnx("%s\n", kvm_geterr(kvmd));
    649 		return (-1);
    650 	}
    651 	return (0);
    652 }
    653 
    654 char *
    655 plural(n)
    656 	int n;
    657 {
    658 
    659 	return (n != 1 ? "s" : "");
    660 }
    661 
    662 char *
    663 plurales(n)
    664 	int n;
    665 {
    666 
    667 	return (n != 1 ? "es" : "");
    668 }
    669 
    670 /*
    671  * Find the protox for the given "well-known" name.
    672  */
    673 static struct protox *
    674 knownname(name)
    675 	char *name;
    676 {
    677 	struct protox **tpp, *tp;
    678 
    679 	for (tpp = protoprotox; *tpp; tpp++)
    680 		for (tp = *tpp; tp->pr_name; tp++)
    681 			if (strcmp(tp->pr_name, name) == 0)
    682 				return (tp);
    683 	return (NULL);
    684 }
    685 
    686 /*
    687  * Find the protox corresponding to name.
    688  */
    689 static struct protox *
    690 name2protox(name)
    691 	char *name;
    692 {
    693 	struct protox *tp;
    694 	char **alias;			/* alias from p->aliases */
    695 	struct protoent *p;
    696 
    697 	/*
    698 	 * Try to find the name in the list of "well-known" names. If that
    699 	 * fails, check if name is an alias for an Internet protocol.
    700 	 */
    701 	if ((tp = knownname(name)) != NULL)
    702 		return (tp);
    703 
    704 	setprotoent(1);			/* make protocol lookup cheaper */
    705 	while ((p = getprotoent()) != NULL) {
    706 		/* assert: name not same as p->name */
    707 		for (alias = p->p_aliases; *alias; alias++)
    708 			if (strcmp(name, *alias) == 0) {
    709 				endprotoent();
    710 				return (knownname(p->p_name));
    711 			}
    712 	}
    713 	endprotoent();
    714 	return (NULL);
    715 }
    716 
    717 static void
    718 usage()
    719 {
    720 	const char *progname = getprogname();
    721 
    722 	(void)fprintf(stderr,
    723 "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n", progname);
    724 	(void)fprintf(stderr,
    725 "       %s [-gimnrsSv] [-f address_family] [-M core] [-N system]\n",
    726 	progname);
    727 	(void)fprintf(stderr,
    728 "       %s [-n] [-I interface] [-M core] [-N system] [-w wait]\n", progname);
    729 	(void)fprintf(stderr,
    730 "       %s [-M core] [-N system] [-p protocol]\n", progname);
    731 	(void)fprintf(stderr,
    732 "       %s [-M core] [-N system] [-p protocol] -P pcbaddr\n", progname);
    733 	exit(1);
    734 }
    735