Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / netstat / main.c
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
35 char 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
42 static char sccsid[] = "@(#)main.c      8.4 (Berkeley) 3/1/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD: src/usr.bin/netstat/main.c,v 1.34.2.12 2001/09/17 15:17:46 ru Exp $";
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
71 static 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 #define N_RTTRASH       42
157         { "_rttrash" },
158         { "" },
159 };
160
161 struct protox {
162         u_char  pr_index;               /* index into nlist of cb head */
163         u_char  pr_sindex;              /* index into nlist of stat block */
164         u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
165         void    (*pr_cblocks)(u_long, char *, int);
166                                         /* control blocks printing routine */
167         void    (*pr_stats)(u_long, char *, int);
168                                         /* statistics printing routine */
169         void    (*pr_istats)(char *);   /* per/if statistics printing routine */
170         char    *pr_name;               /* well-known name */
171         int     pr_usesysctl;           /* true if we use sysctl, not kvm */
172 } protox[] = {
173         { -1,           -1,             1,      protopr,
174           tcp_stats,    NULL,           "tcp",  IPPROTO_TCP },
175         { -1,           -1,             1,      protopr,
176           udp_stats,    NULL,           "udp",  IPPROTO_UDP },
177         { -1,           -1,             1,      protopr,
178           NULL,         NULL,           "divert",IPPROTO_DIVERT },
179         { -1,           -1,             1,      protopr,
180           ip_stats,     NULL,           "ip",   IPPROTO_RAW },
181         { -1,           -1,             1,      protopr,
182           icmp_stats,   NULL,           "icmp", IPPROTO_ICMP },
183         { -1,           -1,             1,      protopr,
184           igmp_stats,   NULL,           "igmp", IPPROTO_IGMP },
185 #ifdef IPSEC
186         { -1,           N_IPSECSTAT,    1,      0,
187           ipsec_stats,  NULL,           "ipsec",        0},
188 #endif
189         { -1,           -1,             1,      0,
190           bdg_stats,    NULL,           "bdg",  1 /* bridging... */ },
191         { -1,           -1,             0,      0,
192           0,            NULL,           0 }
193 };
194
195 #ifdef INET6
196 struct protox ip6protox[] = {
197         { -1,           -1,             1,      protopr,
198           tcp_stats,    NULL,           "tcp",  IPPROTO_TCP },
199         { -1,           -1,             1,      protopr,
200           udp_stats,    NULL,           "udp",  IPPROTO_UDP },
201         { -1,           N_IP6STAT,      1,      protopr,
202           ip6_stats,    ip6_ifstats,    "ip6",  IPPROTO_RAW },
203         { -1,           N_ICMP6STAT,    1,      protopr,
204           icmp6_stats,  icmp6_ifstats,  "icmp6",IPPROTO_ICMPV6 },
205 #ifdef IPSEC
206         { -1,           N_IPSEC6STAT,   1,      0,
207           ipsec_stats,  NULL,           "ipsec6",0 },
208 #endif
209 #ifdef notyet
210         { -1,           N_PIM6STAT,     1,      0,
211           pim6_stats,   NULL,           "pim6", 0 },
212 #endif
213         { -1,           -1,             1,      0,
214           rip6_stats,   NULL,           "rip6", 0 },
215         { -1,           -1,             1,      0,
216           bdg_stats,    NULL,           "bdg",  1 /* bridging... */ },
217         { -1,           -1,             0,      0,
218           0,            NULL,           0,      0 }
219 };
220 #endif /*INET6*/
221
222 #ifdef IPSEC
223 struct protox pfkeyprotox[] = {
224         { -1,           N_PFKEYSTAT,    1,      0,
225           pfkey_stats,  NULL,           "pfkey", 0 },
226         { -1,           -1,             0,      0,
227           0,            NULL,           0,      0 }
228 };
229 #endif
230
231 struct protox atalkprotox[] = {
232         { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
233           ddp_stats,    NULL,           "ddp" },
234         { -1,           -1,             0,      0,
235           0,            NULL,           0 }
236 };
237
238 struct protox netgraphprotox[] = {
239         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
240           NULL,         NULL,           "ctrl" },
241         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
242           NULL,         NULL,           "data" },
243         { -1,           NULL,           0,      0,
244           0,            NULL,           0 }
245 };
246
247 struct protox ipxprotox[] = {
248         { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
249           ipx_stats,    NULL,           "ipx",  0 },
250         { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
251           spx_stats,    NULL,           "spx",  0 },
252         { -1,           -1,             0,      0,
253           0,            NULL,           0,      0 }
254 };
255
256 #ifdef NS
257 struct protox nsprotox[] = {
258         { N_IDP,        N_IDPSTAT,      1,      nsprotopr,
259           idp_stats,    NULL,           "idp" },
260         { N_IDP,        N_SPPSTAT,      1,      nsprotopr,
261           spp_stats,    NULL,           "spp" },
262         { -1,           N_NSERR,        1,      0,
263           nserr_stats,  NULL,           "ns_err" },
264         { -1,           -1,             0,      0,
265           0,            NULL,           0 }
266 };
267 #endif
268
269 #ifdef ISO
270 struct protox isoprotox[] = {
271         { ISO_TP,       N_TPSTAT,       1,      iso_protopr,
272           tp_stats,     NULL,           "tp" },
273         { N_CLTP,       N_CLTPSTAT,     1,      iso_protopr,
274           cltp_stats,   NULL,           "cltp" },
275         { -1,           N_CLNPSTAT,     1,       0,
276           clnp_stats,   NULL,           "clnp"},
277         { -1,           N_ESISSTAT,     1,       0,
278           esis_stats,   NULL,           "esis"},
279         { -1,           -1,             0,      0,
280           0,            NULL,           0 }
281 };
282 #endif
283
284 struct protox *protoprotox[] = {
285                                          protox,
286 #ifdef INET6
287                                          ip6protox,
288 #endif
289 #ifdef IPSEC
290                                          pfkeyprotox,
291 #endif
292                                          ipxprotox, atalkprotox,
293 #ifdef NS
294                                          nsprotox, 
295 #endif
296 #ifdef ISO
297                                          isoprotox, 
298 #endif
299                                          NULL };
300
301 static void printproto (struct protox *, char *);
302 static void usage (void);
303 static struct protox *name2protox (char *);
304 static struct protox *knownname (char *);
305
306 static kvm_t *kvmd;
307 static char *nlistf = NULL, *memf = NULL;
308
309 int     Aflag;          /* show addresses of protocol control block */
310 int     aflag;          /* show all sockets (including servers) */
311 int     bflag;          /* show i/f total bytes in/out */
312 int     dflag;          /* show i/f dropped packets */
313 int     gflag;          /* show group (multicast) routing or stats */
314 int     iflag;          /* show interfaces */
315 int     Lflag;          /* show size of listen queues */
316 int     mflag;          /* show memory stats */
317 int     numeric_addr;   /* show addresses numerically */
318 int     numeric_port;   /* show ports numerically */
319 static int pflag;       /* show given protocol */
320 int     rflag;          /* show routing tables (or routing stats) */
321 int     sflag;          /* show protocol statistics */
322 int     tflag;          /* show i/f watchdog timers */
323 int     Wflag;          /* wide display */
324 int     zflag;          /* zero stats */
325
326 int     interval;       /* repeat interval for i/f stats */
327
328 char    *interface;     /* desired i/f for stats, or NULL for all i/fs */
329 int     unit;           /* unit number for above */
330
331 int     af;             /* address family */
332
333 int
334 main(argc, argv)
335         int argc;
336         char *argv[];
337 {
338         register struct protox *tp = NULL;  /* for printing cblocks & stats */
339         int ch;
340
341         af = AF_UNSPEC;
342
343         while ((ch = getopt(argc, argv, "Aabdf:gI:iLlM:mN:np:rSstuWw:z")) != -1)
344                 switch(ch) {
345                 case 'A':
346                         Aflag = 1;
347                         break;
348                 case 'a':
349                         aflag = 1;
350                         break;
351                 case 'b':
352                         bflag = 1;
353                         break;
354                 case 'd':
355                         dflag = 1;
356                         break;
357                 case 'f':
358 #ifdef NS
359                         if (strcmp(optarg, "ns") == 0)
360                                 af = AF_NS;
361                         else
362 #endif
363                         if (strcmp(optarg, "ipx") == 0)
364                                 af = AF_IPX;
365                         else if (strcmp(optarg, "inet") == 0)
366                                 af = AF_INET;
367 #ifdef INET6
368                         else if (strcmp(optarg, "inet6") == 0)
369                                 af = AF_INET6;
370 #endif /*INET6*/
371 #ifdef INET6
372                         else if (strcmp(optarg, "pfkey") == 0)
373                                 af = PF_KEY;
374 #endif /*INET6*/
375                         else if (strcmp(optarg, "unix") == 0)
376                                 af = AF_UNIX;
377                         else if (strcmp(optarg, "atalk") == 0)
378                                 af = AF_APPLETALK;
379                         else if (strcmp(optarg, "ng") == 0
380                             || strcmp(optarg, "netgraph") == 0)
381                                 af = AF_NETGRAPH;
382 #ifdef ISO
383                         else if (strcmp(optarg, "iso") == 0)
384                                 af = AF_ISO;
385 #endif
386                         else if (strcmp(optarg, "link") == 0)
387                                 af = AF_LINK;
388                         else {
389                                 errx(1, "%s: unknown address family", optarg);
390                         }
391                         break;
392                 case 'g':
393                         gflag = 1;
394                         break;
395                 case 'I': {
396                         char *cp;
397
398                         iflag = 1;
399                         for (cp = interface = optarg; isalpha(*cp); cp++)
400                                 continue;
401                         unit = atoi(cp);
402                         break;
403                 }
404                 case 'i':
405                         iflag = 1;
406                         break;
407                 case 'L':
408                         Lflag = 1;
409                         break;
410                 case 'M':
411                         memf = optarg;
412                         break;
413                 case 'm':
414                         mflag = 1;
415                         break;
416                 case 'N':
417                         nlistf = optarg;
418                         break;
419                 case 'n':
420                         numeric_addr = numeric_port = 1;
421                         break;
422                 case 'p':
423                         if ((tp = name2protox(optarg)) == NULL) {
424                                 errx(1, 
425                                      "%s: unknown or uninstrumented protocol",
426                                      optarg);
427                         }
428                         pflag = 1;
429                         break;
430                 case 'r':
431                         rflag = 1;
432                         break;
433                 case 's':
434                         ++sflag;
435                         break;
436                 case 'S':
437                         numeric_addr = 1;
438                         break;
439                 case 't':
440                         tflag = 1;
441                         break;
442                 case 'u':
443                         af = AF_UNIX;
444                         break;
445                 case 'W':
446                 case 'l':
447                         Wflag = 1;
448                         break;
449                 case 'w':
450                         interval = atoi(optarg);
451                         iflag = 1;
452                         break;
453                 case 'z':
454                         zflag = 1;
455                         break;
456                 case '?':
457                 default:
458                         usage();
459                 }
460         argv += optind;
461         argc -= optind;
462
463 #define BACKWARD_COMPATIBILITY
464 #ifdef  BACKWARD_COMPATIBILITY
465         if (*argv) {
466                 if (isdigit(**argv)) {
467                         interval = atoi(*argv);
468                         if (interval <= 0)
469                                 usage();
470                         ++argv;
471                         iflag = 1;
472                 }
473                 if (*argv) {
474                         nlistf = *argv;
475                         if (*++argv)
476                                 memf = *argv;
477                 }
478         }
479 #endif
480
481         /*
482          * Discard setgid privileges if not the running kernel so that bad
483          * guys can't print interesting stuff from kernel memory.
484          */
485         if (nlistf != NULL || memf != NULL)
486                 setgid(getgid());
487
488         if (mflag) {
489                 if (memf != NULL) {
490                         if (kread(0, 0, 0) == 0)
491                                 mbpr(nl[N_MBSTAT].n_value,
492                                     nl[N_MBTYPES].n_value,
493                                     nl[N_NMBCLUSTERS].n_value,
494                                     nl[N_NMBUFS].n_value);
495                 } else
496                         mbpr(0, 0, 0, 0);
497                 exit(0);
498         }
499 #if 0
500         /*
501          * Keep file descriptors open to avoid overhead
502          * of open/close on each call to get* routines.
503          */
504         sethostent(1);
505         setnetent(1);
506 #else
507         /*
508          * This does not make sense any more with DNS being default over
509          * the files.  Doing a setXXXXent(1) causes a tcp connection to be
510          * used for the queries, which is slower.
511          */
512 #endif
513         if (iflag && !sflag) {
514                 kread(0, 0, 0);
515                 intpr(interval, nl[N_IFNET].n_value, NULL);
516                 exit(0);
517         }
518         if (rflag) {
519                 kread(0, 0, 0);
520                 if (sflag)
521                         rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
522                 else
523                         routepr(nl[N_RTREE].n_value);
524                 exit(0);
525         }
526         if (gflag) {
527                 kread(0, 0, 0);
528                 if (sflag) {
529                         if (af == AF_INET || af == AF_UNSPEC)
530                                 mrt_stats(nl[N_MRTSTAT].n_value);
531 #ifdef INET6
532                         if (af == AF_INET6 || af == AF_UNSPEC)
533                                 mrt6_stats(nl[N_MRT6STAT].n_value);
534 #endif
535                 } else {
536                         if (af == AF_INET || af == AF_UNSPEC)
537                                 mroutepr(nl[N_MFCTABLE].n_value,
538                                          nl[N_VIFTABLE].n_value);
539 #ifdef INET6
540                         if (af == AF_INET6 || af == AF_UNSPEC)
541                                 mroute6pr(nl[N_MF6CTABLE].n_value,
542                                           nl[N_MIF6TABLE].n_value);
543 #endif
544                 }
545                 exit(0);
546         }
547
548         kread(0, 0, 0);
549         if (tp) {
550                 printproto(tp, tp->pr_name);
551                 exit(0);
552         }
553         if (af == AF_INET || af == AF_UNSPEC)
554                 for (tp = protox; tp->pr_name; tp++)
555                         printproto(tp, tp->pr_name);
556 #ifdef INET6
557         if (af == AF_INET6 || af == AF_UNSPEC)
558                 for (tp = ip6protox; tp->pr_name; tp++)
559                         printproto(tp, tp->pr_name);
560 #endif /*INET6*/
561 #ifdef IPSEC
562         if (af == PF_KEY || af == AF_UNSPEC)
563                 for (tp = pfkeyprotox; tp->pr_name; tp++)
564                         printproto(tp, tp->pr_name);
565 #endif /*IPSEC*/
566         if (af == AF_IPX || af == AF_UNSPEC) {
567                 kread(0, 0, 0);
568                 for (tp = ipxprotox; tp->pr_name; tp++)
569                         printproto(tp, tp->pr_name);
570         }
571         if (af == AF_APPLETALK || af == AF_UNSPEC)
572                 for (tp = atalkprotox; tp->pr_name; tp++)
573                         printproto(tp, tp->pr_name);
574         if (af == AF_NETGRAPH || af == AF_UNSPEC)
575                 for (tp = netgraphprotox; tp->pr_name; tp++)
576                         printproto(tp, tp->pr_name);
577 #ifdef NS
578         if (af == AF_NS || af == AF_UNSPEC)
579                 for (tp = nsprotox; tp->pr_name; tp++)
580                         printproto(tp, tp->pr_name);
581 #endif
582 #ifdef ISO
583         if (af == AF_ISO || af == AF_UNSPEC)
584                 for (tp = isoprotox; tp->pr_name; tp++)
585                         printproto(tp, tp->pr_name);
586 #endif
587         if ((af == AF_UNIX || af == AF_UNSPEC) && !Lflag && !sflag)
588                 unixpr();
589         exit(0);
590 }
591
592 /*
593  * Print out protocol statistics or control blocks (per sflag).
594  * If the interface was not specifically requested, and the symbol
595  * is not in the namelist, ignore this one.
596  */
597 static void
598 printproto(tp, name)
599         register struct protox *tp;
600         char *name;
601 {
602         void (*pr)(u_long, char *, int);
603         u_long off;
604
605         if (sflag) {
606                 if (iflag) {
607                         if (tp->pr_istats)
608                                 intpr(interval, nl[N_IFNET].n_value,
609                                       tp->pr_istats);
610                         else if (pflag)
611                                 printf("%s: no per-interface stats routine\n",
612                                     tp->pr_name);
613                         return;
614                 }
615                 else {
616                         pr = tp->pr_stats;
617                         if (!pr) {
618                                 if (pflag)
619                                         printf("%s: no stats routine\n",
620                                             tp->pr_name);
621                                 return;
622                         }
623                         off = tp->pr_usesysctl ? tp->pr_usesysctl 
624                                 : nl[tp->pr_sindex].n_value;
625                 }
626         } else {
627                 pr = tp->pr_cblocks;
628                 if (!pr) {
629                         if (pflag)
630                                 printf("%s: no PCB routine\n", tp->pr_name);
631                         return;
632                 }
633                 off = tp->pr_usesysctl ? tp->pr_usesysctl
634                         : nl[tp->pr_index].n_value;
635         }
636         if (pr != NULL && (off || af != AF_UNSPEC))
637                 (*pr)(off, name, af);
638 }
639
640 /*
641  * Read kernel memory, return 0 on success.
642  */
643 int
644 kread(u_long addr, char *buf, int size)
645 {
646         if (kvmd == 0) {
647                 /*
648                  * XXX.
649                  */
650                 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
651                 if (kvmd != NULL) {
652                         if (kvm_nlist(kvmd, nl) < 0) {
653                                 if(nlistf)
654                                         errx(1, "%s: kvm_nlist: %s", nlistf,
655                                              kvm_geterr(kvmd));
656                                 else
657                                         errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
658                         }
659
660                         if (nl[0].n_type == 0) {
661                                 if(nlistf)
662                                         errx(1, "%s: no namelist", nlistf);
663                                 else
664                                         errx(1, "no namelist");
665                         }
666                 } else {
667                         warnx("kvm not available");
668                         return(-1);
669                 }
670         }
671         if (!buf)
672                 return (0);
673         if (kvm_read(kvmd, addr, buf, size) != size) {
674                 warnx("%s", kvm_geterr(kvmd));
675                 return (-1);
676         }
677         return (0);
678 }
679
680 char *
681 plural(int n)
682 {
683         return (n != 1 ? "s" : "");
684 }
685
686 char *
687 plurales(int n)
688 {
689         return (n != 1 ? "es" : "");
690 }
691
692 /*
693  * Find the protox for the given "well-known" name.
694  */
695 static struct protox *
696 knownname(char *name)
697 {
698         struct protox **tpp, *tp;
699
700         for (tpp = protoprotox; *tpp; tpp++)
701                 for (tp = *tpp; tp->pr_name; tp++)
702                         if (strcmp(tp->pr_name, name) == 0)
703                                 return (tp);
704         return (NULL);
705 }
706
707 /*
708  * Find the protox corresponding to name.
709  */
710 static struct protox *
711 name2protox(char *name)
712 {
713         struct protox *tp;
714         char **alias;                   /* alias from p->aliases */
715         struct protoent *p;
716
717         /*
718          * Try to find the name in the list of "well-known" names. If that
719          * fails, check if name is an alias for an Internet protocol.
720          */
721         if ((tp = knownname(name)) != NULL)
722                 return (tp);
723
724         setprotoent(1);                 /* make protocol lookup cheaper */
725         while ((p = getprotoent()) != NULL) {
726                 /* assert: name not same as p->name */
727                 for (alias = p->p_aliases; *alias; alias++)
728                         if (strcmp(name, *alias) == 0) {
729                                 endprotoent();
730                                 return (knownname(p->p_name));
731                         }
732         }
733         endprotoent();
734         return (NULL);
735 }
736
737 static void
738 usage(void)
739 {
740         (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
741 "usage: netstat [-AaLnSW] [-f protocol_family | -p protocol]\n"
742 "               [-M core] [-N system]",
743 "       netstat -i | -I interface [-abdnt] [-f address_family]\n"
744 "               [-M core] [-N system]",
745 "       netstat -w wait [-I interface] [-d] [-M core] [-N system]",
746 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol] [-M core]",
747 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
748 "               [-M core] [-N system]",
749 "       netstat -m [-M core] [-N system]",
750 "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
751 "       netstat -rs [-s] [-M core] [-N system]",
752 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
753 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
754         exit(1);
755 }