Annotation of src/usr.bin/netstat/main.c, Revision 1.67
1.67 ! claudio 1: /* $OpenBSD: main.c,v 1.66 2007/06/04 12:20:24 henning Exp $ */
1.2 deraadt 2: /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1983, 1988, 1993
6: * Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.36 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
34: char copyright[] =
35: "@(#) Copyright (c) 1983, 1988, 1993\n\
36: Regents of the University of California. All rights reserved.\n";
37: #endif /* not lint */
38:
39: #ifndef lint
40: #if 0
41: static char sccsid[] = "from: @(#)main.c 8.4 (Berkeley) 3/1/94";
42: #else
1.67 ! claudio 43: static char *rcsid = "$OpenBSD: main.c,v 1.66 2007/06/04 12:20:24 henning Exp $";
1.1 deraadt 44: #endif
45: #endif /* not lint */
46:
47: #include <sys/param.h>
48: #include <sys/file.h>
49: #include <sys/protosw.h>
50: #include <sys/socket.h>
51:
52: #include <netinet/in.h>
53:
54: #include <ctype.h>
1.61 djm 55: #include <err.h>
1.1 deraadt 56: #include <errno.h>
57: #include <kvm.h>
58: #include <limits.h>
59: #include <netdb.h>
60: #include <nlist.h>
61: #include <paths.h>
62: #include <stdio.h>
63: #include <stdlib.h>
64: #include <string.h>
65: #include <unistd.h>
66: #include "netstat.h"
67:
68: struct nlist nl[] = {
1.55 deraadt 69: #define N_MBSTAT 0
1.1 deraadt 70: { "_mbstat" },
1.55 deraadt 71: #define N_IPSTAT 1
1.1 deraadt 72: { "_ipstat" },
1.55 deraadt 73: #define N_TCBTABLE 2
1.1 deraadt 74: { "_tcbtable" },
1.55 deraadt 75: #define N_TCPSTAT 3
1.1 deraadt 76: { "_tcpstat" },
1.55 deraadt 77: #define N_UDBTABLE 4
1.1 deraadt 78: { "_udbtable" },
1.55 deraadt 79: #define N_UDPSTAT 5
1.1 deraadt 80: { "_udpstat" },
1.55 deraadt 81: #define N_IFNET 6
1.1 deraadt 82: { "_ifnet" },
1.55 deraadt 83: #define N_ICMPSTAT 7
1.1 deraadt 84: { "_icmpstat" },
1.55 deraadt 85: #define N_RTSTAT 8
1.1 deraadt 86: { "_rtstat" },
1.55 deraadt 87: #define N_UNIXSW 9
1.1 deraadt 88: { "_unixsw" },
1.58 henning 89: #define N_RTREE 10
1.1 deraadt 90: { "_rt_tables"},
1.58 henning 91: #define N_FILE 11
1.1 deraadt 92: { "_file" },
1.58 henning 93: #define N_IGMPSTAT 12
1.1 deraadt 94: { "_igmpstat" },
1.58 henning 95: #define N_MRTPROTO 13
1.1 deraadt 96: { "_ip_mrtproto" },
1.58 henning 97: #define N_MRTSTAT 14
1.1 deraadt 98: { "_mrtstat" },
1.58 henning 99: #define N_MFCHASHTBL 15
1.1 deraadt 100: { "_mfchashtbl" },
1.58 henning 101: #define N_MFCHASH 16
1.1 deraadt 102: { "_mfchash" },
1.58 henning 103: #define N_VIFTABLE 17
1.1 deraadt 104: { "_viftable" },
1.67 ! claudio 105: #define N_AHSTAT 18
1.8 angelos 106: { "_ahstat"},
1.67 ! claudio 107: #define N_ESPSTAT 19
1.8 angelos 108: { "_espstat"},
1.67 ! claudio 109: #define N_IP4STAT 20
1.21 angelos 110: { "_ipipstat"},
1.67 ! claudio 111: #define N_DDPSTAT 21
1.12 denny 112: { "_ddpstat"},
1.67 ! claudio 113: #define N_DDPCB 22
1.12 denny 114: { "_ddpcb"},
1.67 ! claudio 115: #define N_ETHERIPSTAT 23
1.18 angelos 116: { "_etheripstat"},
1.67 ! claudio 117: #define N_IP6STAT 24
1.19 itojun 118: { "_ip6stat" },
1.67 ! claudio 119: #define N_ICMP6STAT 25
1.19 itojun 120: { "_icmp6stat" },
1.67 ! claudio 121: #define N_PIM6STAT 26
1.19 itojun 122: { "_pim6stat" },
1.67 ! claudio 123: #define N_MRT6PROTO 27
1.19 itojun 124: { "_ip6_mrtproto" },
1.67 ! claudio 125: #define N_MRT6STAT 28
1.19 itojun 126: { "_mrt6stat" },
1.67 ! claudio 127: #define N_MF6CTABLE 29
1.19 itojun 128: { "_mf6ctable" },
1.67 ! claudio 129: #define N_MIF6TABLE 30
1.19 itojun 130: { "_mif6table" },
1.67 ! claudio 131: #define N_MBPOOL 31
1.25 provos 132: { "_mbpool" },
1.67 ! claudio 133: #define N_MCLPOOL 32
1.25 provos 134: { "_mclpool" },
1.67 ! claudio 135: #define N_IPCOMPSTAT 33
1.26 jjbg 136: { "_ipcompstat" },
1.67 ! claudio 137: #define N_RIP6STAT 34
1.32 itojun 138: { "_rip6stat" },
1.67 ! claudio 139: #define N_CARPSTAT 35
1.39 mcbride 140: { "_carpstats" },
1.67 ! claudio 141: #define N_RAWIPTABLE 36
1.40 markus 142: { "_rawcbtable" },
1.67 ! claudio 143: #define N_RAWIP6TABLE 37
1.40 markus 144: { "_rawin6pcbtable" },
1.67 ! claudio 145: #define N_PFSYNCSTAT 38
1.43 mcbride 146: { "_pfsyncstats" },
1.67 ! claudio 147: #define N_PIMSTAT 39
1.50 mcbride 148: { "_pimstat" },
1.67 ! claudio 149: #define N_AF2RTAFIDX 40
1.63 claudio 150: { "_af2rtafidx" },
1.67 ! claudio 151: #define N_RTBLIDMAX 41
1.63 claudio 152: { "_rtbl_id_max" },
1.67 ! claudio 153: #define N_RTMASK 42
1.63 claudio 154: { "_mask_rnhead" },
1.55 deraadt 155: { ""}
1.1 deraadt 156: };
157:
158: struct protox {
1.38 deraadt 159: u_char pr_index; /* index into nlist of cb head */
160: u_char pr_sindex; /* index into nlist of stat block */
161: u_char pr_wanted; /* 1 if wanted, 0 otherwise */
162: void (*pr_cblocks)(u_long, char *); /* control blocks printing routine */
163: void (*pr_stats)(u_long, char *); /* statistics printing routine */
1.59 markus 164: void (*pr_dump)(u_long); /* pcb printing routine */
1.38 deraadt 165: char *pr_name; /* well-known name */
1.1 deraadt 166: } protox[] = {
167: { N_TCBTABLE, N_TCPSTAT, 1, protopr,
1.59 markus 168: tcp_stats, tcp_dump, "tcp" },
1.1 deraadt 169: { N_UDBTABLE, N_UDPSTAT, 1, protopr,
1.59 markus 170: udp_stats, 0, "udp" },
1.40 markus 171: { N_RAWIPTABLE, N_IPSTAT, 1, protopr,
1.59 markus 172: ip_stats, 0, "ip" },
1.1 deraadt 173: { -1, N_ICMPSTAT, 1, 0,
1.59 markus 174: icmp_stats, 0, "icmp" },
1.1 deraadt 175: { -1, N_IGMPSTAT, 1, 0,
1.59 markus 176: igmp_stats, 0, "igmp" },
1.8 angelos 177: { -1, N_AHSTAT, 1, 0,
1.59 markus 178: ah_stats, 0, "ah" },
1.8 angelos 179: { -1, N_ESPSTAT, 1, 0,
1.59 markus 180: esp_stats, 0, "esp" },
1.8 angelos 181: { -1, N_IP4STAT, 1, 0,
1.59 markus 182: ipip_stats, 0, "ipencap" },
1.18 angelos 183: { -1, N_ETHERIPSTAT, 1, 0,
1.59 markus 184: etherip_stats,0, "etherip" },
1.29 mickey 185: { -1, N_IPCOMPSTAT, 1, 0,
1.59 markus 186: ipcomp_stats, 0, "ipcomp" },
1.44 deraadt 187: { -1, N_CARPSTAT, 1, 0,
1.59 markus 188: carp_stats, 0, "carp" },
1.44 deraadt 189: { -1, N_PFSYNCSTAT, 1, 0,
1.59 markus 190: pfsync_stats, 0, "pfsync" },
1.50 mcbride 191: { -1, N_PIMSTAT, 1, 0,
1.59 markus 192: pim_stats, 0, "pim" },
1.1 deraadt 193: { -1, -1, 0, 0,
1.59 markus 194: 0, 0, 0 }
1.1 deraadt 195: };
196:
1.19 itojun 197: #ifdef INET6
198: struct protox ip6protox[] = {
1.22 itojun 199: { N_TCBTABLE, N_TCPSTAT, 1, ip6protopr,
1.59 markus 200: 0, tcp_dump, "tcp" },
1.22 itojun 201: { N_UDBTABLE, N_UDPSTAT, 1, ip6protopr,
1.59 markus 202: 0, 0, "udp" },
1.40 markus 203: { N_RAWIP6TABLE,N_IP6STAT, 1, ip6protopr,
1.59 markus 204: ip6_stats, 0, "ip6" },
1.19 itojun 205: { -1, N_ICMP6STAT, 1, 0,
1.59 markus 206: icmp6_stats, 0, "icmp6" },
1.19 itojun 207: { -1, N_PIM6STAT, 1, 0,
1.59 markus 208: pim6_stats, 0, "pim6" },
1.32 itojun 209: { -1, N_RIP6STAT, 1, 0,
1.59 markus 210: rip6_stats, 0, "rip6" },
1.19 itojun 211: { -1, -1, 0, 0,
1.59 markus 212: 0, 0, 0 }
1.19 itojun 213: };
214: #endif
215:
1.12 denny 216: struct protox atalkprotox[] = {
217: { N_DDPCB, N_DDPSTAT, 1, atalkprotopr,
1.59 markus 218: ddp_stats, 0, "ddp" },
1.12 denny 219: { -1, -1, 0, 0,
1.59 markus 220: 0, 0, 0 }
1.12 denny 221: };
222:
1.19 itojun 223: #ifndef INET6
1.34 deraadt 224: struct protox *protoprotox[] = {
1.66 henning 225: protox, atalkprotox, NULL
1.34 deraadt 226: };
1.19 itojun 227: #else
1.34 deraadt 228: struct protox *protoprotox[] = {
1.66 henning 229: protox, ip6protox, atalkprotox, NULL
1.34 deraadt 230: };
1.19 itojun 231: #endif
1.1 deraadt 232:
1.30 millert 233: static void printproto(struct protox *, char *);
234: static void usage(void);
235: static struct protox *name2protox(char *);
236: static struct protox *knownname(char *);
1.1 deraadt 237:
238: kvm_t *kvmd;
239:
240: int
1.34 deraadt 241: main(int argc, char *argv[])
1.1 deraadt 242: {
243: extern char *optarg;
244: extern int optind;
1.28 mpech 245: struct protoent *p;
246: struct protox *tp = NULL; /* for printing cblocks & stats */
1.1 deraadt 247: int ch;
1.59 markus 248: char *nlistf = NULL, *memf = NULL, *ep;
1.1 deraadt 249: char buf[_POSIX2_LINE_MAX];
1.61 djm 250: gid_t gid;
1.59 markus 251: u_long pcbaddr = 0;
1.1 deraadt 252:
253: af = AF_UNSPEC;
254:
1.64 pyr 255: while ((ch = getopt(argc, argv, "AabdFf:gI:ilM:mN:np:P:qrstuvW:w:")) != -1)
1.31 deraadt 256: switch (ch) {
1.1 deraadt 257: case 'A':
258: Aflag = 1;
259: break;
260: case 'a':
261: aflag = 1;
1.24 camield 262: break;
263: case 'b':
264: bflag = 1;
1.1 deraadt 265: break;
266: case 'd':
267: dflag = 1;
1.64 pyr 268: break;
269: case 'F':
270: Fflag = 1;
1.1 deraadt 271: break;
272: case 'f':
1.4 mickey 273: if (strcmp(optarg, "inet") == 0)
1.1 deraadt 274: af = AF_INET;
1.19 itojun 275: else if (strcmp(optarg, "inet6") == 0)
276: af = AF_INET6;
1.7 kstailey 277: else if (strcmp(optarg, "local") == 0)
278: af = AF_LOCAL;
1.1 deraadt 279: else if (strcmp(optarg, "unix") == 0)
280: af = AF_UNIX;
1.10 angelos 281: else if (strcmp(optarg, "encap") == 0)
1.16 angelos 282: af = PF_KEY;
1.12 denny 283: else if (strcmp(optarg, "atalk") == 0)
284: af = AF_APPLETALK;
1.63 claudio 285: else if (strcmp(optarg, "mask") == 0)
286: af = 0xff;
1.1 deraadt 287: else {
288: (void)fprintf(stderr,
289: "%s: %s: unknown address family\n",
1.2 deraadt 290: __progname, optarg);
1.1 deraadt 291: exit(1);
292: }
293: break;
294: case 'g':
295: gflag = 1;
296: break;
1.2 deraadt 297: case 'I':
1.1 deraadt 298: iflag = 1;
1.2 deraadt 299: interface = optarg;
1.1 deraadt 300: break;
301: case 'i':
302: iflag = 1;
303: break;
1.19 itojun 304: case 'l':
305: lflag = 1;
306: break;
1.1 deraadt 307: case 'M':
308: memf = optarg;
309: break;
310: case 'm':
311: mflag = 1;
312: break;
313: case 'N':
314: nlistf = optarg;
315: break;
316: case 'n':
317: nflag = 1;
318: break;
319: case 'p':
320: if ((tp = name2protox(optarg)) == NULL) {
321: (void)fprintf(stderr,
1.42 jmc 322: "%s: %s: unknown protocol\n",
1.2 deraadt 323: __progname, optarg);
1.1 deraadt 324: exit(1);
325: }
326: pflag = 1;
1.27 brian 327: break;
1.59 markus 328: case 'P':
329: errno = 0;
330: pcbaddr = strtoul(optarg, &ep, 16);
331: if (optarg[0] == '\0' || *ep != '\0' ||
332: errno == ERANGE) {
333: (void)fprintf(stderr,
334: "%s: %s: invalid PCB address\n",
335: __progname, optarg);
336: exit(1);
337: }
338: Pflag = 1;
339: break;
1.27 brian 340: case 'q':
341: qflag = 1;
1.1 deraadt 342: break;
343: case 'r':
344: rflag = 1;
1.46 cedric 345: break;
1.1 deraadt 346: case 's':
347: ++sflag;
348: break;
349: case 't':
350: tflag = 1;
351: break;
352: case 'u':
353: af = AF_UNIX;
1.13 peter 354: break;
355: case 'v':
356: vflag = 1;
1.1 deraadt 357: break;
1.56 reyk 358: case 'W':
359: Wflag = 1;
360: interface = optarg;
361: break;
1.1 deraadt 362: case 'w':
363: interval = atoi(optarg);
364: iflag = 1;
365: break;
366: case '?':
367: default:
368: usage();
369: }
370: argv += optind;
371: argc -= optind;
372:
1.33 deraadt 373: /*
1.56 reyk 374: * Show per-interface statistics which don't need access to
375: * kernel memory (they're using IOCTLs)
376: */
377: if (Wflag) {
378: if (interface == NULL)
379: usage();
380: net80211_ifstats(interface);
381: exit(0);
382: }
383:
384: /*
1.33 deraadt 385: * Discard setgid privileges if not the running kernel so that bad
386: * guys can't print interesting stuff from kernel memory.
1.59 markus 387: * Dumping PCB info is also restricted.
1.33 deraadt 388: */
1.61 djm 389: gid = getgid();
390: if (nlistf != NULL || memf != NULL || Pflag)
391: if (setresgid(gid, gid, gid) == -1)
392: err(1, "setresgid");
1.62 claudio 393: if (nlistf == NULL && memf == NULL && rflag && !Aflag) {
394: /* printing the routing table no longer needs kvm */
395: if (setresgid(gid, gid, gid) == -1)
396: err(1, "setresgid");
397: if (sflag)
398: rt_stats(1, 0);
399: else
400: p_rttables(af);
401: exit(0);
402: }
1.33 deraadt 403: if ((kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
404: buf)) == NULL) {
405: fprintf(stderr, "%s: kvm_open: %s\n", __progname, buf);
406: exit(1);
407: }
1.61 djm 408:
409: if (nlistf == NULL && memf == NULL && !Pflag)
410: if (setresgid(gid, gid, gid) == -1)
411: err(1, "setresgid");
1.33 deraadt 412:
1.1 deraadt 413: #define BACKWARD_COMPATIBILITY
414: #ifdef BACKWARD_COMPATIBILITY
415: if (*argv) {
416: if (isdigit(**argv)) {
417: interval = atoi(*argv);
418: if (interval <= 0)
419: usage();
420: ++argv;
421: iflag = 1;
422: }
423: if (*argv) {
424: nlistf = *argv;
425: if (*++argv)
426: memf = *argv;
427: }
428: }
429: #endif
1.14 deraadt 430:
1.1 deraadt 431: if (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0) {
432: if (nlistf)
1.2 deraadt 433: fprintf(stderr, "%s: %s: no namelist\n", __progname,
434: nlistf);
1.1 deraadt 435: else
1.2 deraadt 436: fprintf(stderr, "%s: no namelist\n", __progname);
1.1 deraadt 437: exit(1);
438: }
439: if (mflag) {
1.25 provos 440: mbpr(nl[N_MBSTAT].n_value, nl[N_MBPOOL].n_value,
441: nl[N_MCLPOOL].n_value);
1.1 deraadt 442: exit(0);
443: }
444: if (pflag) {
1.45 markus 445: printproto(tp, tp->pr_name);
1.59 markus 446: exit(0);
447: }
448: if (Pflag) {
449: if (tp == NULL && (tp = name2protox("tcp")) == NULL) {
450: (void)fprintf(stderr,
451: "%s: %s: unknown protocol\n",
452: __progname, "tcp");
453: exit(1);
454: }
455: if (tp->pr_dump)
456: (tp->pr_dump)(pcbaddr);
1.1 deraadt 457: exit(0);
458: }
459: /*
460: * Keep file descriptors open to avoid overhead
461: * of open/close on each call to get* routines.
462: */
463: sethostent(1);
464: setnetent(1);
1.56 reyk 465:
1.1 deraadt 466: if (iflag) {
467: intpr(interval, nl[N_IFNET].n_value);
468: exit(0);
469: }
470: if (rflag) {
471: if (sflag)
1.62 claudio 472: rt_stats(0, nl[N_RTSTAT].n_value);
1.1 deraadt 473: else
1.63 claudio 474: routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value,
475: nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value);
1.1 deraadt 476: exit(0);
477: }
478: if (gflag) {
1.19 itojun 479: if (sflag) {
480: if (af == AF_INET || af == AF_UNSPEC)
481: mrt_stats(nl[N_MRTPROTO].n_value,
482: nl[N_MRTSTAT].n_value);
483: #ifdef INET6
484: if (af == AF_INET6 || af == AF_UNSPEC)
485: mrt6_stats(nl[N_MRT6PROTO].n_value,
486: nl[N_MRT6STAT].n_value);
487: #endif
488: }
489: else {
490: if (af == AF_INET || af == AF_UNSPEC)
491: mroutepr(nl[N_MRTPROTO].n_value,
492: nl[N_MFCHASHTBL].n_value,
493: nl[N_MFCHASH].n_value,
494: nl[N_VIFTABLE].n_value);
495: #ifdef INET6
496: if (af == AF_INET6 || af == AF_UNSPEC)
497: mroute6pr(nl[N_MRT6PROTO].n_value,
498: nl[N_MF6CTABLE].n_value,
499: nl[N_MIF6TABLE].n_value);
500: #endif
501: }
1.1 deraadt 502: exit(0);
503: }
504: if (af == AF_INET || af == AF_UNSPEC) {
505: setprotoent(1);
506: setservent(1);
507: /* ugh, this is O(MN) ... why do we do this? */
1.11 millert 508: while ((p = getprotoent())) {
1.1 deraadt 509: for (tp = protox; tp->pr_name; tp++)
510: if (strcmp(tp->pr_name, p->p_name) == 0)
511: break;
512: if (tp->pr_name == 0 || tp->pr_wanted == 0)
513: continue;
514: printproto(tp, p->p_name);
515: }
516: endprotoent();
517: }
1.19 itojun 518: #ifdef INET6
519: if (af == AF_INET6 || af == AF_UNSPEC)
520: for (tp = ip6protox; tp->pr_name; tp++)
521: printproto(tp, tp->pr_name);
522: #endif
1.1 deraadt 523: if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
524: unixpr(nl[N_UNIXSW].n_value);
1.12 denny 525: if (af == AF_APPLETALK || af == AF_UNSPEC)
526: for (tp = atalkprotox; tp->pr_name; tp++)
527: printproto(tp, tp->pr_name);
1.1 deraadt 528: exit(0);
529: }
530:
531: /*
532: * Print out protocol statistics or control blocks (per sflag).
533: * If the interface was not specifically requested, and the symbol
534: * is not in the namelist, ignore this one.
535: */
536: static void
1.34 deraadt 537: printproto(struct protox *tp, char *name)
1.1 deraadt 538: {
1.38 deraadt 539: void (*pr)(u_long, char *);
1.35 dhartmei 540: u_char i;
1.1 deraadt 541:
542: if (sflag) {
543: pr = tp->pr_stats;
1.35 dhartmei 544: i = tp->pr_sindex;
1.1 deraadt 545: } else {
546: pr = tp->pr_cblocks;
1.35 dhartmei 547: i = tp->pr_index;
1.1 deraadt 548: }
1.35 dhartmei 549: if (pr != NULL && i < sizeof(nl) / sizeof(nl[0]) &&
550: (nl[i].n_value || af != AF_UNSPEC))
551: (*pr)(nl[i].n_value, name);
1.1 deraadt 552: }
553:
554: /*
555: * Read kernel memory, return 0 on success.
556: */
557: int
1.53 jaredy 558: kread(u_long addr, void *buf, int size)
1.1 deraadt 559: {
560:
561: if (kvm_read(kvmd, addr, buf, size) != size) {
1.2 deraadt 562: (void)fprintf(stderr, "%s: %s\n", __progname,
1.1 deraadt 563: kvm_geterr(kvmd));
564: return (-1);
565: }
566: return (0);
567: }
568:
569: char *
1.34 deraadt 570: plural(int n)
1.1 deraadt 571: {
572: return (n != 1 ? "s" : "");
573: }
574:
575: char *
1.34 deraadt 576: plurales(int n)
1.1 deraadt 577: {
578: return (n != 1 ? "es" : "");
579: }
580:
581: /*
582: * Find the protox for the given "well-known" name.
583: */
584: static struct protox *
1.34 deraadt 585: knownname(char *name)
1.1 deraadt 586: {
587: struct protox **tpp, *tp;
588:
589: for (tpp = protoprotox; *tpp; tpp++)
590: for (tp = *tpp; tp->pr_name; tp++)
591: if (strcmp(tp->pr_name, name) == 0)
592: return (tp);
593: return (NULL);
594: }
595:
596: /*
597: * Find the protox corresponding to name.
598: */
599: static struct protox *
1.34 deraadt 600: name2protox(char *name)
1.1 deraadt 601: {
602: struct protox *tp;
603: char **alias; /* alias from p->aliases */
604: struct protoent *p;
605:
606: /*
607: * Try to find the name in the list of "well-known" names. If that
608: * fails, check if name is an alias for an Internet protocol.
609: */
1.11 millert 610: if ((tp = knownname(name)))
1.1 deraadt 611: return (tp);
612:
613: setprotoent(1); /* make protocol lookup cheaper */
1.11 millert 614: while ((p = getprotoent())) {
1.1 deraadt 615: /* assert: name not same as p->name */
616: for (alias = p->p_aliases; *alias; alias++)
617: if (strcmp(name, *alias) == 0) {
618: endprotoent();
619: return (knownname(p->p_name));
620: }
621: }
622: endprotoent();
623: return (NULL);
624: }
625:
626: static void
1.34 deraadt 627: usage(void)
1.1 deraadt 628: {
629: (void)fprintf(stderr,
1.60 jaredy 630: "usage: %s [-Aan] [-f address_family] [-M core] [-N system]\n"
1.65 jmc 631: " %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n"
1.60 jaredy 632: " %s [-bdn] [-I interface] [-M core] [-N system] [-w wait]\n"
633: " %s [-M core] [-N system] -P pcbaddr\n"
634: " %s [-s] [-M core] [-N system] [-p protocol]\n"
635: " %s [-a] [-f address_family] [-i | -I interface]\n"
636: " %s [-W interface]\n",
637: __progname, __progname, __progname, __progname,
638: __progname, __progname, __progname);
1.1 deraadt 639: exit(1);
640: }