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