Debian Codegraph @

apps/snmpnetstat/main.c

raw
1 /*
2 * $OpenBSD: main.c,v 1.52 2005/02/10 14:25:08 itojun Exp $
3 */
4 /*
5 * $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $
6 */
7
8 /*
9 * Copyright (c) 1983, 1988, 1993
10 * Regents of the University of California. All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #ifndef lint
38 char copyright[] = "@(#) Copyright (c) 1983, 1988, 1993\n\
39 Regents of the University of California. All rights reserved.\n";
40 #endif /* not lint */
41
42 #include <net-snmp/net-snmp-config.h>
43 #include <net-snmp/net-snmp-includes.h>
44 #include <net-snmp/utilities.h>
45
46 #ifdef HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49 #ifdef HAVE_NETDB_H
50 #include <netdb.h>
51 #endif
52
53 #include "main.h"
54 #include "netstat.h"
55
56 #ifdef HAVE_WINSOCK_H
57 #include "winstub.h"
58 #endif
59
60 int aflag; /* show all sockets (including servers) */
61 int bflag; /* show bytes instead of packets */
62 int dflag; /* show i/f dropped packets */
63 int gflag; /* show group (multicast) routing or stats */
64 int iflag; /* show interfaces */
65 int lflag; /* show routing table with use and ref */
66 int Lflag; /* Legacy mibs */
67 int mflag; /* show memory stats */
68 int nflag; /* show addresses numerically */
69 int oflag; /* Open/Net-BSD style octet output */
70 int pflag; /* show given protocol */
71 int qflag; /* only display non-zero values for output */
72 int rflag; /* show routing tables (or routing stats) */
73 int Sflag; /* show source address in routing table */
74 int sflag; /* show protocol statistics */
75 int tflag; /* show i/f watchdog timers */
76 int vflag; /* be verbose */
77
78
79 int interval; /* repeat interval for i/f stats */
80 char *intrface; /* desired i/f for stats, or NULL for all i/fs */
81 int af; /* address family */
82 int max_getbulk = 32; /* specifies the max-repeaters value to use with GETBULK requests */
83
84 char *progname = NULL;
85 const char *pname;
86 /*
87 * struct nlist nl[] - Omitted
88 */
89
90 typedef void (stringfun) (const char *);
91
92 struct protox {
93 /*
94 * pr_index/pr_sindex - Omitted
95 */
96 int pr_wanted; /* 1 if wanted, 0 otherwise */
97 stringfun *pr_cblocks; /* control blocks printing routine */
98 stringfun *pr_stats; /* statistics printing routine */
99 const char *pr_name; /* well-known name */
100 };
101
102 struct protox protox[] = {
103 {1, tcpprotopr, tcp_stats, "tcp"},
104 {1, udpprotopr, udp_stats, "udp"},
105
106 {1, (stringfun *) 0, ip_stats, "ip"}, /* protopr Omitted */
107 {1, (stringfun *) 0, icmp_stats, "icmp"},
108 /*
109 * igmp/ah/esp/ipencap/etherip/ipcomp/carp/pfsync/pim - Omitted
110 */
111 {0, (stringfun *) 0, (stringfun *) 0, NULL}
112 };
113
114 struct protox ip6protox[] = {
115 {1, tcp6protopr, (stringfun *) 0, "tcp6"},
116 {1, udp6protopr, (stringfun *) 0, "udp6"},
117
118 {1, (stringfun *) 0, ip6_stats, "ip6"}, /* ip6protopr Omitted */
119 {1, (stringfun *) 0, icmp6_stats, "icmp6"},
120 /*
121 * pim6/rip6 - Omitted
122 */
123 {0, (stringfun *) 0, (stringfun *) 0, NULL}
124 };
125
126 struct protox ipxprotox[] = {
127 {1, tcpxprotopr, tcp_stats, "tcp"},
128 {1, udpxprotopr, udp_stats, "udp"},
129 {1, (stringfun *) 0, ipx_stats, "ip"}, /* ip6protopr Omitted */
130 {1, (stringfun *) 0, ipx_stats, "ip6"}, /* ip6protopr Omitted */
131 {1, (stringfun *) 0, icmpx_stats, "icmp"},
132 {1, (stringfun *) 0, icmpx_stats, "icmp6"},
133 {0, (stringfun *) 0, (stringfun *) 0, NULL}
134 };
135
136 /*
137 * {ipx,ns,atalk}protox Omitted
138 */
139
140 struct protox *protoprotox[] = {
141 protox, ip6protox, NULL
142 };
143
144 static void printproto(struct protox *, const char *);
145 static void usage(void);
146 static struct protox *name2protox(const char *);
147 static struct protox *knownname(const char *);
148
149 netsnmp_session *ss;
150 struct protox *tp = NULL; /* for printing cblocks & stats */
151
152 static void
153 optProc(int argc, char *const *argv, int opt)
154 {
155 switch (opt) {
156 case 'C':
157 while (*optarg) {
158 switch (*optarg++) {
159 case 'a':
160 aflag = 1;
161 break;
162 case 'b':
163 bflag = 1;
164 break;
165 case 'd':
166 dflag = 1;
167 break;
168 case 'f':
169 if (!*optarg)
170 optarg = argv[optind++];
171 if (strcmp(optarg, "inet") == 0)
172 af = AF_INET;
173 else if (strcmp(optarg, "inet6") == 0)
174 af = AF_INET6;
175 /*
176 * else if (strcmp(optarg, "local") == 0)
177 * af = AF_LOCAL;
178 * else if (strcmp(optarg, "unix") == 0)
179 * af = AF_UNIX;
180 * else if (strcmp(optarg, "ipx") == 0)
181 * af = AF_IPX;
182 * else if (strcmp(optarg, "ns") == 0)
183 * af = AF_NS;
184 * else if (strcmp(optarg, "encap") == 0)
185 * af = PF_KEY;
186 * else if (strcmp(optarg, "atalk") == 0)
187 * af = AF_APPLETALK;
188 */
189 else {
190 (void) fprintf(stderr,
191 "%s: %s: unknown address family\n",
192 progname, optarg);
193 exit(1);
194 }
195 return;
196 case 'g':
197 gflag = 1;
198 break;
199 case 'I':
200 iflag = 1;
201 if (!*optarg)
202 optarg = argv[optind++];
203 intrface = optarg;
204 return;
205 case 'i':
206 iflag = 1;
207 break;
208 case 'L':
209 Lflag = 1;
210 break;
211 /*
212 * case 'L': FreeBSD: Display listen queue lengths
213 * NetBSD: Suppress link-level routes
214 */
215 /*
216 * case 'l': OpenBSD: Wider IPv6 display
217 * Linux: Listening sockets only
218 * lflag = 1;
219 * break;
220 * case 'M': *BSD: Memory image
221 * Linux: Masqueraded connections
222 * memf = optarg;
223 * break;
224 */
225 case 'm':
226 mflag = 1;
227 break;
228 /*
229 * case 'N': *BSD: Kernel image
230 * nlistf = optarg;
231 * break;
232 */
233 case 'n':
234 nflag = 1;
235 break;
236 case 'o':
237 oflag = 1;
238 break;
239 /*
240 * case 'P': NetBSD:
241 * OpenBSD: dump PCB block
242 */
243 case 'p':
244 if (!*optarg)
245 optarg = argv[optind++];
246 if ((tp = name2protox(optarg)) == NULL) {
247 (void) fprintf(stderr,
248 "%s: %s: unknown protocol\n",
249 progname, optarg);
250 exit(1);
251 }
252 pflag = 1;
253 pname = tp->pr_name;
254 return;
255 /*
256 * case 'q': NetBSD: IRQ information
257 * OpenBSD: Suppress inactive I/Fs
258 * qflag = 1;
259 * break;
260 */
261 case 'r':
262 rflag = 1;
263 break;
264 case 'R':
265 if (optind < argc) {
266 if (argv[optind]) {
267 max_getbulk = atoi(argv[optind]);
268 if (max_getbulk == 0) {
269 usage();
270 fprintf(stderr, "Bad -CR option: %s\n",
271 argv[optind]);
272 exit(1);
273 }
274 }
275 } else {
276 usage();
277 fprintf(stderr, "Bad -CR option: no argument given\n");
278 exit(1);
279 }
280 optind++;
281 break;
282 case 'S': /* FreeBSD:
283 * NetBSD: Semi-numeric display
284 * OpenBSD: Show route source selector */
285 Sflag = 1;
286 break;
287 case 's':
288 ++sflag;
289 break;
290 /*
291 * case 't': FreeBSD:
292 * OpenBSD: Display watchdog timers
293 * tflag = 1;
294 * break;
295 * case 'u': OpenBSD: unix sockets only
296 * af = AF_UNIX;
297 * break;
298 */
299 case 'v':
300 vflag = 1;
301 break;
302 case 'w':
303 if (!*optarg)
304 optarg = argv[optind++];
305 interval = atoi(optarg);
306 iflag = 1;
307 return;
308 case '?':
309 default:
310 usage();
311 exit(1);
312 }
313 }
314 break; /* End of '-Cx' switch */
315
316 /*
317 * Backward compatability for the main display modes
318 * (where this doesn't clash with standard SNMP flags)
319 */
320 case 'i':
321 iflag = 1;
322 break;
323 case 'R':
324 rflag = 1; /* -r sets the retry count */
325 break;
326 case 's':
327 ++sflag;
328 break;
329 }
330 }
331
332 int
333 main(int argc, char *argv[])
334 {
335 netsnmp_session session;
336 struct protoent *p;
337 char *cp;
338 int exit_code = 1;
339
340 SOCK_STARTUP;
341
342 af = AF_UNSPEC;
343 cp = strrchr(argv[0], '/');
344 if (cp)
345 progname = cp + 1;
346 else
347 progname = argv[0];
348
349 switch (snmp_parse_args(argc, argv, &session, "C:iRs", optProc)) {
350 case NETSNMP_PARSE_ARGS_ERROR:
351 goto out;
352 case NETSNMP_PARSE_ARGS_SUCCESS_EXIT:
353 exit_code = 0;
354 goto out;
355 case NETSNMP_PARSE_ARGS_ERROR_USAGE:
356 usage();
357 goto out;
358 default:
359 break;
360 }
361
362 /*
363 * Check argc vs optind ??
364 */
365 argv += optind;
366 argc -= optind;
367
368 /*
369 * Open an SNMP session.
370 */
371 ss = snmp_open(&session);
372 if (ss == NULL) {
373 /*
374 * diagnose snmp_open errors with the input netsnmp_session pointer
375 */
376 snmp_sess_perror("snmpnetstat", &session);
377 goto out;
378 }
379
380 /*
381 * Omitted:
382 * Privilege handling
383 * "Backward Compatibility"
384 * Kernel namelis handling
385 */
386
387 #if 0
388 if (mflag) {
389 /*
390 * mbpr(nl[N_MBSTAT].n_value, nl[N_MBPOOL].n_value,
391 * nl[N_MCLPOOL].n_value);
392 */
393 exit_code = 0;
394 goto out;
395 }
396 if (pflag) {
397 printproto(tp, tp->pr_name);
398 exit_code = 0;
399 goto out;
400 }
401 #endif
402 /*
403 * Keep file descriptors open to avoid overhead
404 * of open/close on each call to get* routines.
405 */
406 sethostent(1);
407 setnetent(1);
408 if (iflag) {
409 intpr(interval);
410 exit_code = 0;
411 goto out;
412 }
413 if (rflag) {
414 /*
415 * if (sflag)
416 * rt_stats();
417 * else
418 */
419 if (Lflag || routexpr(af) == 0) {
420 if (route4pr(af) == 0 && af == AF_INET)
421 routepr();
422 route6pr(af);
423 }
424 exit_code = 0;
425 goto out;
426 }
427 /*
428 * if (gflag) {
429 * if (sflag) {
430 * if (af == AF_INET || af == AF_UNSPEC)
431 * mrt_stats(nl[N_MRTPROTO].n_value,
432 * nl[N_MRTSTAT].n_value);
433 * #ifdef NETSNMP_ENABLE_IPV6
434 * if (af == AF_INET6 || af == AF_UNSPEC)
435 * mrt6_stats(nl[N_MRT6PROTO].n_value,
436 * nl[N_MRT6STAT].n_value);
437 * #endif
438 * }
439 * else {
440 * if (af == AF_INET || af == AF_UNSPEC)
441 * mroutepr(nl[N_MRTPROTO].n_value,
442 * nl[N_MFCHASHTBL].n_value,
443 * nl[N_MFCHASH].n_value,
444 * nl[N_VIFTABLE].n_value);
445 * #ifdef NETSNMP_ENABLE_IPV6
446 * if (af == AF_INET6 || af == AF_UNSPEC)
447 * mroute6pr(nl[N_MRT6PROTO].n_value,
448 * nl[N_MF6CTABLE].n_value,
449 * nl[N_MIF6TABLE].n_value);
450 * #endif
451 * }
452 * exit_code = 0;
453 * goto out;
454 * }
455 */
456 setservent(1);
457 if (Lflag) {
458 switch (af) {
459 case AF_UNSPEC:
460 setprotoent(1);
461 /*
462 * ugh, this is O(MN) ... why do we do this?
463 */
464 while ((p = getprotoent())) {
465 for (tp = protox; tp->pr_name; tp++)
466 if (strcmp(tp->pr_name, p->p_name) == 0)
467 if (tp->pr_name && tp->pr_wanted)
468 printproto(tp, p->p_name);
469 }
470 endprotoent();
471 break;
472 case AF_INET:
473 for (tp = protox; tp->pr_name; tp++)
474 printproto(tp, tp->pr_name);
475 /*
476 * FALL THROUGH
477 */
478 case AF_INET6:
479 for (tp = ip6protox; tp->pr_name; tp++)
480 printproto(tp, tp->pr_name);
481 }
482 } else {
483 for (tp = ipxprotox; tp->pr_name; tp++)
484 if (!pname || strcmp(pname, tp->pr_name) == 0)
485 printproto(tp, pname);
486 }
487 /*
488 * if (af == AF_IPX || af == AF_UNSPEC)
489 * for (tp = ipxprotox; tp->pr_name; tp++)
490 * printproto(tp, tp->pr_name);
491 * if (af == AF_NS || af == AF_UNSPEC)
492 * for (tp = nsprotox; tp->pr_name; tp++)
493 * printproto(tp, tp->pr_name);
494 * if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
495 * unixpr(nl[N_UNIXSW].n_value);
496 * if (af == AF_APPLETALK || af == AF_UNSPEC)
497 * for (tp = atalkprotox; tp->pr_name; tp++)
498 * printproto(tp, tp->pr_name);
499 */
500
501 exit_code = 0;
502
503 out:
504 netsnmp_cleanup_session(&session);
505 SOCK_CLEANUP;
506 return exit_code;
507 }
508
509 /*
510 * Print out protocol statistics or control blocks (per sflag).
511 * Namelist checks - Omitted
512 */
513 static void
514 printproto(struct protox *tp, const char *name)
515 {
516 void (*pr)(const char *);
517
518 if (sflag) {
519 pr = tp->pr_stats;
520 } else {
521 pr = tp->pr_cblocks;
522 }
523 if (pr != NULL)
524 (*pr) (name);
525 }
526
527 /*
528 * Read kernel memory - Omitted
529 */
530
531 const char *
532 plural(int n)
533 {
534 return (n != 1 ? "s" : "");
535 }
536
537 /*
538 * Find the protox for the given "well-known" name.
539 */
540 static struct protox *
541 knownname(const char *name)
542 {
543 struct protox **tpp, *tp;
544
545 for (tpp = protoprotox; *tpp; tpp++)
546 for (tp = *tpp; tp->pr_name; tp++)
547 if (strcmp(tp->pr_name, name) == 0)
548 return (tp);
549 return (NULL);
550 }
551
552 /*
553 * Find the protox corresponding to name.
554 */
555 static struct protox *
556 name2protox(const char *name)
557 {
558 struct protox *tp;
559 char **alias; /* alias from p->aliases */
560 struct protoent *p;
561
562 /*
563 * Try to find the name in the list of "well-known" names. If that
564 * fails, check if name is an alias for an Internet protocol.
565 */
566 if ((tp = knownname(name)))
567 return (tp);
568
569 setprotoent(1); /* make protocol lookup cheaper */
570 while ((p = getprotoent())) {
571 /*
572 * netsnmp_assert: name not same as p->name
573 */
574 for (alias = p->p_aliases; *alias; alias++)
575 if (strcmp(name, *alias) == 0) {
576 endprotoent();
577 return (knownname(p->p_name));
578 }
579 }
580 endprotoent();
581 return (NULL);
582 }
583
584 static void
585 usage(void)
586 {
587 (void) fprintf(stderr,
588 "usage: %s [snmp_opts] [-Canv] [-Cf address_family]\n",
589 progname);
590 (void) fprintf(stderr,
591 " %s [snmp_opts] [-Cibodnv] [-CI interface] [-Cw wait]\n",
592 progname);
593 (void) fprintf(stderr,
594 " %s [snmp_opts] [-Cs[s]] [-Cp protocol]\n",
595 progname);
596 (void) fprintf(stderr,
597 " %s [snmp_opts] [-Crnv] [-Cf address_family]\n",
598 progname);
599 }