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