main.c revision 78284
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 78284 2001-06-15 18:25:38Z ru $";
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 __P((struct protox *, char *));
300static void usage __P((void));
301static struct protox *name2protox __P((char *));
302static struct protox *knownname __P((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 0
495	/*
496	 * Keep file descriptors open to avoid overhead
497	 * of open/close on each call to get* routines.
498	 */
499	sethostent(1);
500	setnetent(1);
501#else
502	/*
503	 * This does not make sense any more with DNS being default over
504	 * the files.  Doing a setXXXXent(1) causes a tcp connection to be
505	 * used for the queries, which is slower.
506	 */
507#endif
508	if (iflag && !sflag) {
509		kread(0, 0, 0);
510		intpr(interval, nl[N_IFNET].n_value, NULL);
511		exit(0);
512	}
513	if (rflag) {
514		kread(0, 0, 0);
515		if (sflag)
516			rt_stats(nl[N_RTSTAT].n_value);
517		else
518			routepr(nl[N_RTREE].n_value);
519		exit(0);
520	}
521	if (gflag) {
522		kread(0, 0, 0);
523		if (sflag) {
524			if (af == AF_INET || af == AF_UNSPEC)
525				mrt_stats(nl[N_MRTSTAT].n_value);
526#ifdef INET6
527			if (af == AF_INET6 || af == AF_UNSPEC)
528				mrt6_stats(nl[N_MRT6STAT].n_value);
529#endif
530		} else {
531			if (af == AF_INET || af == AF_UNSPEC)
532				mroutepr(nl[N_MFCTABLE].n_value,
533					 nl[N_VIFTABLE].n_value);
534#ifdef INET6
535			if (af == AF_INET6 || af == AF_UNSPEC)
536				mroute6pr(nl[N_MF6CTABLE].n_value,
537					  nl[N_MIF6TABLE].n_value);
538#endif
539		}
540		exit(0);
541	}
542
543	kread(0, 0, 0);
544	if (tp) {
545		printproto(tp, tp->pr_name);
546		exit(0);
547	}
548	if (af == AF_INET || af == AF_UNSPEC)
549		for (tp = protox; tp->pr_name; tp++)
550			printproto(tp, tp->pr_name);
551#ifdef INET6
552	if (af == AF_INET6 || af == AF_UNSPEC)
553		for (tp = ip6protox; tp->pr_name; tp++)
554			printproto(tp, tp->pr_name);
555#endif /*INET6*/
556#ifdef IPSEC
557	if (af == PF_KEY || af == AF_UNSPEC)
558		for (tp = pfkeyprotox; tp->pr_name; tp++)
559			printproto(tp, tp->pr_name);
560#endif /*IPSEC*/
561	if (af == AF_IPX || af == AF_UNSPEC) {
562		kread(0, 0, 0);
563		for (tp = ipxprotox; tp->pr_name; tp++)
564			printproto(tp, tp->pr_name);
565	}
566	if (af == AF_APPLETALK || af == AF_UNSPEC)
567		for (tp = atalkprotox; tp->pr_name; tp++)
568			printproto(tp, tp->pr_name);
569	if (af == AF_NETGRAPH || af == AF_UNSPEC)
570		for (tp = netgraphprotox; tp->pr_name; tp++)
571			printproto(tp, tp->pr_name);
572#ifdef NS
573	if (af == AF_NS || af == AF_UNSPEC)
574		for (tp = nsprotox; tp->pr_name; tp++)
575			printproto(tp, tp->pr_name);
576#endif
577#ifdef ISO
578	if (af == AF_ISO || af == AF_UNSPEC)
579		for (tp = isoprotox; tp->pr_name; tp++)
580			printproto(tp, tp->pr_name);
581#endif
582	if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
583		unixpr();
584	exit(0);
585}
586
587/*
588 * Print out protocol statistics or control blocks (per sflag).
589 * If the interface was not specifically requested, and the symbol
590 * is not in the namelist, ignore this one.
591 */
592static void
593printproto(tp, name)
594	register struct protox *tp;
595	char *name;
596{
597	void (*pr)(u_long, char *, int);
598	u_long off;
599
600	if (sflag) {
601		if (iflag) {
602			if (tp->pr_istats)
603				intpr(interval, nl[N_IFNET].n_value,
604				      tp->pr_istats);
605			else if (pflag)
606				printf("%s: no per-interface stats routine\n",
607				    tp->pr_name);
608			return;
609		}
610		else {
611			pr = tp->pr_stats;
612			if (!pr) {
613				if (pflag)
614					printf("%s: no stats routine\n",
615					    tp->pr_name);
616				return;
617			}
618			off = tp->pr_usesysctl ? tp->pr_usesysctl
619				: nl[tp->pr_sindex].n_value;
620		}
621	} else {
622		pr = tp->pr_cblocks;
623		if (!pr) {
624			if (pflag)
625				printf("%s: no PCB routine\n", tp->pr_name);
626			return;
627		}
628		off = tp->pr_usesysctl ? tp->pr_usesysctl
629			: nl[tp->pr_index].n_value;
630	}
631	if (pr != NULL && (off || af != AF_UNSPEC))
632		(*pr)(off, name, af);
633}
634
635/*
636 * Read kernel memory, return 0 on success.
637 */
638int
639kread(u_long addr, char *buf, int size)
640{
641	if (kvmd == 0) {
642		/*
643		 * XXX.
644		 */
645		kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
646		if (kvmd != NULL) {
647			if (kvm_nlist(kvmd, nl) < 0) {
648				if(nlistf)
649					errx(1, "%s: kvm_nlist: %s", nlistf,
650					     kvm_geterr(kvmd));
651				else
652					errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
653			}
654
655			if (nl[0].n_type == 0) {
656				if(nlistf)
657					errx(1, "%s: no namelist", nlistf);
658				else
659					errx(1, "no namelist");
660			}
661		} else {
662			warnx("kvm not available");
663			return(-1);
664		}
665	}
666	if (!buf)
667		return (0);
668	if (kvm_read(kvmd, addr, buf, size) != size) {
669		warnx("%s", kvm_geterr(kvmd));
670		return (-1);
671	}
672	return (0);
673}
674
675char *
676plural(int n)
677{
678	return (n != 1 ? "s" : "");
679}
680
681char *
682plurales(int n)
683{
684	return (n != 1 ? "es" : "");
685}
686
687/*
688 * Find the protox for the given "well-known" name.
689 */
690static struct protox *
691knownname(char *name)
692{
693	struct protox **tpp, *tp;
694
695	for (tpp = protoprotox; *tpp; tpp++)
696		for (tp = *tpp; tp->pr_name; tp++)
697			if (strcmp(tp->pr_name, name) == 0)
698				return (tp);
699	return (NULL);
700}
701
702/*
703 * Find the protox corresponding to name.
704 */
705static struct protox *
706name2protox(char *name)
707{
708	struct protox *tp;
709	char **alias;			/* alias from p->aliases */
710	struct protoent *p;
711
712	/*
713	 * Try to find the name in the list of "well-known" names. If that
714	 * fails, check if name is an alias for an Internet protocol.
715	 */
716	if ((tp = knownname(name)) != NULL)
717		return (tp);
718
719	setprotoent(1);			/* make protocol lookup cheaper */
720	while ((p = getprotoent()) != NULL) {
721		/* assert: name not same as p->name */
722		for (alias = p->p_aliases; *alias; alias++)
723			if (strcmp(name, *alias) == 0) {
724				endprotoent();
725				return (knownname(p->p_name));
726			}
727	}
728	endprotoent();
729	return (NULL);
730}
731
732static void
733usage(void)
734{
735	(void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
736"usage: netstat [-AaLlnW] [-f address_family] [-M core] [-N system]",
737"       netstat [-abdgilnrsS] [-f address_family] [-M core] [-N system]",
738"       netstat [-bdn] [-I interface] [-M core] [-N system] [-w wait]",
739"       netstat -m [-M core] [-N system]",
740"       netstat [-M core] [-N system] [-p protocol]");
741	exit(1);
742}
743