PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/release/src/router/snmp/apps/snmpnetstat/main.c

https://gitlab.com/envieidoc/advancedtomato2
C | 475 lines | 365 code | 51 blank | 59 comment | 68 complexity | a4e961e427b503339e32187ae5a41daf MD5 | raw file
  1. /******************************************************************
  2. Copyright 1989, 1991, 1992 by Carnegie Mellon University
  3. All Rights Reserved
  4. Permission to use, copy, modify, and distribute this software and its
  5. documentation for any purpose and without fee is hereby granted,
  6. provided that the above copyright notice appear in all copies and that
  7. both that copyright notice and this permission notice appear in
  8. supporting documentation, and that the name of CMU not be
  9. used in advertising or publicity pertaining to distribution of the
  10. software without specific, written prior permission.
  11. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  12. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  13. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  14. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  15. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  16. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  17. SOFTWARE.
  18. ******************************************************************/
  19. /*
  20. * Copyright (c) 1983,1988 Regents of the University of California.
  21. * All rights reserved.
  22. *
  23. * Redistribution and use in source and binary forms are permitted
  24. * provided that this notice is preserved and that due credit is given
  25. * to the University of California at Berkeley. The name of the University
  26. * may not be used to endorse or promote products derived from this
  27. * software without specific prior written permission. This software
  28. * is provided ``as is'' without express or implied warranty.
  29. */
  30. #include <net-snmp/net-snmp-config.h>
  31. #ifndef lint
  32. char copyright[] =
  33. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  34. All rights reserved.\n";
  35. #endif /* not lint */
  36. #if HAVE_STDLIB_H
  37. #include <stdlib.h>
  38. #endif
  39. #if HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. #if HAVE_STRING_H
  43. #include <string.h>
  44. #else
  45. #include <strings.h>
  46. #endif
  47. #include <net-snmp/utilities.h>
  48. #include <sys/types.h>
  49. #if HAVE_SYS_PARAM_H
  50. #include <sys/param.h>
  51. #endif
  52. #if TIME_WITH_SYS_TIME
  53. # ifdef WIN32
  54. # include <sys/timeb.h>
  55. # else
  56. # include <sys/time.h>
  57. # endif
  58. # include <time.h>
  59. #else
  60. # if HAVE_SYS_TIME_H
  61. # include <sys/time.h>
  62. # else
  63. # include <time.h>
  64. # endif
  65. #endif
  66. #if HAVE_SYS_SELECT_H
  67. #include <sys/select.h>
  68. #endif
  69. #if HAVE_WINSOCK_H
  70. #include <winsock.h>
  71. #include "winstub.h"
  72. #endif
  73. #if HAVE_SYS_SOCKET_H
  74. #include <sys/socket.h>
  75. #endif
  76. #if HAVE_NETDB_H
  77. #include <netdb.h>
  78. #endif
  79. #include <ctype.h>
  80. #include <errno.h>
  81. #include <stdio.h>
  82. #if HAVE_NETINET_IN_H
  83. #include <netinet/in.h>
  84. #endif
  85. #if HAVE_ARPA_INET_H
  86. #include <arpa/inet.h>
  87. #endif
  88. #include <net-snmp/net-snmp-includes.h>
  89. #include "netstat.h"
  90. #define NULLPROTOX ((struct protox *) 0)
  91. struct protox {
  92. u_char pr_wanted; /* 1 if wanted, 0 otherwise */
  93. void (*pr_cblocks) (const char *); /* control blocks printing routine */
  94. void (*pr_stats) (void); /* statistics printing routine */
  95. const char *pr_name; /* well-known name */
  96. } protox[] = {
  97. {
  98. 0, protopr, tcp_stats, "tcp"}, {
  99. 0, protopr, udp_stats, "udp"},
  100. #ifdef INET6
  101. {
  102. 0, protopr6, tcp_stats, "tcp6"}, {
  103. 0, protopr6, udp_stats, "udp6"},
  104. #endif
  105. {
  106. 0, 0, ip_stats, "ip"}, {
  107. 0, 0, icmp_stats, "icmp"}, {
  108. 0, 0, 0, 0}
  109. };
  110. int aflag;
  111. int iflag;
  112. int oflag;
  113. int nflag;
  114. int rflag;
  115. int sflag;
  116. int interval;
  117. char *intrface;
  118. netsnmp_session *Session;
  119. int print_errors = 0;
  120. static struct protoent *getprotoent46(void);
  121. void
  122. usage(void)
  123. {
  124. fprintf(stderr,
  125. "Usage: snmpnetstat [options...] hostname [ community ] [interval]\n");
  126. fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version());
  127. fprintf(stderr, " -v [1 | 2c ] SNMP version\n");
  128. fprintf(stderr, " -V display version number\n");
  129. fprintf(stderr, " -p port specify agent port number\n");
  130. fprintf(stderr, " -c community specify community name\n");
  131. fprintf(stderr, " -t timeout SNMP packet timeout (seconds)\n");
  132. fprintf(stderr,
  133. " -i show interfaces with packet counters\n");
  134. fprintf(stderr,
  135. " -o show interfaces with octet counters\n");
  136. fprintf(stderr, " -r show routing table\n");
  137. fprintf(stderr, " -s show general statistics\n");
  138. fprintf(stderr, " -n show IP addresses, not names\n");
  139. fprintf(stderr, " -a show sockets in LISTEN mode too\n");
  140. fprintf(stderr,
  141. " -P proto show only details for this protocol\n");
  142. fprintf(stderr, " -I interface show only this interface\n");
  143. fprintf(stderr, " -d dump packets\n");
  144. fprintf(stderr, " -Ddebugspec \n");
  145. }
  146. int
  147. main(int argc, char *argv[])
  148. {
  149. char *hostname = NULL;
  150. struct protoent *p;
  151. struct protox *tp = NULL; /* for printing cblocks & stats */
  152. int allprotos = 1;
  153. char *community = NULL;
  154. char *argp;
  155. netsnmp_session session;
  156. int dest_port = SNMP_PORT;
  157. int timeout = SNMP_DEFAULT_TIMEOUT;
  158. int version = SNMP_DEFAULT_VERSION;
  159. int arg;
  160. init_mib();
  161. /*
  162. * Usage: snmpnetstatwalk -v 1 [-q] hostname community ... or:
  163. * Usage: snmpnetstat [-v 2 ] [-q] hostname noAuth ...
  164. */
  165. while ((arg = getopt(argc, argv, "VhdqD:p:t:c:v:aionrsP:I:")) != EOF) {
  166. switch (arg) {
  167. case 'V':
  168. fprintf(stderr, "NET-SNMP version: %s\n",
  169. netsnmp_get_version());
  170. exit(0);
  171. break;
  172. case 'h':
  173. usage();
  174. exit(0);
  175. case 'd':
  176. snmp_set_dump_packet(1);
  177. break;
  178. case 'q':
  179. snmp_set_quick_print(1);
  180. break;
  181. case 'D':
  182. debug_register_tokens(optarg);
  183. snmp_set_do_debugging(1);
  184. break;
  185. case 'p':
  186. dest_port = atoi(optarg);
  187. break;
  188. case 't':
  189. timeout = atoi(optarg);
  190. timeout *= 1000000;
  191. break;
  192. case 'c':
  193. community = optarg;
  194. break;
  195. case 'v':
  196. argp = optarg;
  197. if (!strcasecmp(argp, "1"))
  198. version = SNMP_VERSION_1;
  199. else if (!strcasecmp(argp, "2c"))
  200. version = SNMP_VERSION_2c;
  201. else {
  202. fprintf(stderr, "Invalid version: %s\n", argp);
  203. usage();
  204. exit(1);
  205. }
  206. break;
  207. case 'a':
  208. aflag++;
  209. break;
  210. case 'i':
  211. iflag++;
  212. break;
  213. case 'o':
  214. oflag++;
  215. break;
  216. case 'n':
  217. nflag++;
  218. break;
  219. case 'r':
  220. rflag++;
  221. break;
  222. case 's':
  223. sflag++;
  224. break;
  225. case 'P':
  226. if ((tp = name2protox(optarg)) == NULLPROTOX) {
  227. fprintf(stderr, "%s: unknown or uninstrumented protocol\n",
  228. optarg);
  229. exit(1);
  230. }
  231. allprotos = 0;
  232. tp->pr_wanted = 1;
  233. break;
  234. case 'I':
  235. iflag++;
  236. intrface = optarg;
  237. break;
  238. default:
  239. exit(1);
  240. break;
  241. }
  242. continue;
  243. }
  244. init_snmp("snmpapp");
  245. if (version == SNMP_DEFAULT_VERSION) {
  246. version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_SNMPVERSION);
  247. }
  248. if (hostname == NULL && optind < argc) {
  249. hostname = argv[optind++];
  250. }
  251. if ((version == SNMP_VERSION_1 || version == SNMP_VERSION_2c)
  252. && community == NULL && optind < argc) {
  253. community = argv[optind++];
  254. fprintf(stderr,
  255. "Warning: positional community parameter is deprecated. Use -c\n");
  256. }
  257. if (optind < argc && isdigit(argv[optind][0])) {
  258. interval = atoi(argv[optind++]);
  259. if (interval <= 0) {
  260. usage();
  261. exit(1);
  262. }
  263. iflag++;
  264. }
  265. if (optind < argc) {
  266. usage();
  267. exit(1);
  268. }
  269. if (!hostname) {
  270. fprintf(stderr, "Missing host name.\n");
  271. exit(1);
  272. }
  273. snmp_sess_init(&session);
  274. session.peername = hostname;
  275. session.remote_port = dest_port;
  276. session.timeout = timeout;
  277. if (version == SNMP_VERSION_1 || version == SNMP_VERSION_2c) {
  278. if (!community
  279. && !(community =
  280. netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_COMMUNITY))) {
  281. fprintf(stderr, "Missing community name.\n");
  282. exit(1);
  283. }
  284. session.version = version;
  285. session.community = (u_char *) community;
  286. session.community_len = strlen((char *) community);
  287. }
  288. SOCK_STARTUP;
  289. /*
  290. * open an SNMP session
  291. */
  292. Session = snmp_open(&session);
  293. if (Session == NULL) {
  294. /*
  295. * diagnose snmp_open errors with the input netsnmp_session pointer
  296. */
  297. snmp_sess_perror("snmpnetstat", &session);
  298. SOCK_CLEANUP;
  299. exit(1);
  300. }
  301. /*
  302. * Keep file descriptors open to avoid overhead
  303. * of open/close on each call to get* routines.
  304. */
  305. sethostent(1);
  306. setnetent(1);
  307. setprotoent(1);
  308. setservent(1);
  309. if (iflag) {
  310. intpr(interval);
  311. }
  312. if (oflag) {
  313. intpro(interval);
  314. }
  315. if (rflag) {
  316. if (sflag)
  317. rt_stats();
  318. else
  319. routepr();
  320. }
  321. if (iflag || rflag || oflag);
  322. else {
  323. while ((p = getprotoent46())) {
  324. for (tp = protox; tp->pr_name; tp++) {
  325. if (strcmp(tp->pr_name, p->p_name) == 0)
  326. break;
  327. }
  328. if (tp->pr_name == 0 || (tp->pr_wanted == 0 && allprotos == 0))
  329. continue;
  330. if (sflag) {
  331. if (tp->pr_stats)
  332. (*tp->pr_stats) ();
  333. } else if (tp->pr_cblocks)
  334. (*tp->pr_cblocks) (tp->pr_name);
  335. }
  336. } /* ! iflag, rflag, oflag */
  337. endprotoent();
  338. endservent();
  339. endnetent();
  340. endhostent();
  341. snmp_close(Session);
  342. SOCK_CLEANUP;
  343. return 0;
  344. }
  345. const char *
  346. plural(int n)
  347. {
  348. return (n != 1 ? "s" : "");
  349. }
  350. /*
  351. * Find the protox for the given "well-known" name.
  352. */
  353. struct protox *
  354. knownname(const char *name)
  355. {
  356. struct protox *tp;
  357. for (tp = protox; tp->pr_name; tp++)
  358. if (strcmp(tp->pr_name, name) == 0)
  359. return (tp);
  360. return (NULLPROTOX);
  361. }
  362. /*
  363. * Find the protox corresponding to name.
  364. */
  365. struct protox *
  366. name2protox(const char *name)
  367. {
  368. struct protox *tp;
  369. char **alias; /* alias from p->aliases */
  370. struct protoent *p;
  371. /*
  372. * Try to find the name in the list of "well-known" names. If that
  373. * fails, check if name is an alias for an Internet protocol.
  374. */
  375. if ((tp = knownname(name)))
  376. return (tp);
  377. setprotoent(1); /* make protocol lookup cheaper */
  378. while ((p = getprotoent46())) {
  379. /*
  380. * assert: name not same as p->name
  381. */
  382. for (alias = p->p_aliases; *alias; alias++)
  383. if (strcasecmp(name, *alias) == 0) {
  384. endprotoent();
  385. return (knownname(p->p_name));
  386. }
  387. }
  388. endprotoent();
  389. return (NULLPROTOX);
  390. }
  391. static struct protoent *
  392. getprotoent46(void)
  393. {
  394. #ifdef INET6
  395. static enum { NONE, V4, V6 } state = NONE;
  396. static struct protoent v;
  397. struct protoent *p;
  398. int l;
  399. switch (state) {
  400. case NONE:
  401. p = getprotoent();
  402. if (!p)
  403. return p;
  404. memcpy(&v, p, sizeof(v));
  405. state = V4;
  406. break;
  407. case V4:
  408. strcat(v.p_name, "4");
  409. state = V6;
  410. break;
  411. case V6:
  412. l = strlen(v.p_name);
  413. v.p_name[l - 1] = '6';
  414. state = NONE;
  415. break;
  416. }
  417. return &v;
  418. #else
  419. return getprotoent();
  420. #endif
  421. }