PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/release/src/router/snmp/apps/snmpwalk.c

https://gitlab.com/envieidoc/advancedtomato2
C | 352 lines | 252 code | 27 blank | 73 comment | 53 complexity | 9f888b820ffc8a24c73686c192276605 MD5 | raw file
  1. /*
  2. * snmpwalk.c - send snmp GETNEXT requests to a network entity, walking a
  3. * subtree.
  4. *
  5. */
  6. /**********************************************************************
  7. Copyright 1988, 1989, 1991, 1992 by Carnegie Mellon University
  8. All Rights Reserved
  9. Permission to use, copy, modify, and distribute this software and its
  10. documentation for any purpose and without fee is hereby granted,
  11. provided that the above copyright notice appear in all copies and that
  12. both that copyright notice and this permission notice appear in
  13. supporting documentation, and that the name of CMU not be
  14. used in advertising or publicity pertaining to distribution of the
  15. software without specific, written prior permission.
  16. CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  17. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  18. CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  19. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  21. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  22. SOFTWARE.
  23. ******************************************************************/
  24. #include <net-snmp/net-snmp-config.h>
  25. #if HAVE_STDLIB_H
  26. #include <stdlib.h>
  27. #endif
  28. #if HAVE_UNISTD_H
  29. #include <unistd.h>
  30. #endif
  31. #if HAVE_STRING_H
  32. #include <string.h>
  33. #else
  34. #include <strings.h>
  35. #endif
  36. #include <sys/types.h>
  37. #if HAVE_NETINET_IN_H
  38. # include <netinet/in.h>
  39. #endif
  40. #if TIME_WITH_SYS_TIME
  41. # ifdef WIN32
  42. # include <sys/timeb.h>
  43. # else
  44. # include <sys/time.h>
  45. # endif
  46. # include <time.h>
  47. #else
  48. # if HAVE_SYS_TIME_H
  49. # include <sys/time.h>
  50. # else
  51. # include <time.h>
  52. # endif
  53. #endif
  54. #if HAVE_SYS_SELECT_H
  55. #include <sys/select.h>
  56. #endif
  57. #include <stdio.h>
  58. #if HAVE_WINSOCK_H
  59. #include <winsock.h>
  60. #endif
  61. #if HAVE_NETDB_H
  62. #include <netdb.h>
  63. #endif
  64. #if HAVE_ARPA_INET_H
  65. #include <arpa/inet.h>
  66. #endif
  67. #include <net-snmp/net-snmp-includes.h>
  68. #define NETSNMP_DS_WALK_INCLUDE_REQUESTED 1
  69. #define NETSNMP_DS_WALK_PRINT_STATISTICS 2
  70. #define NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC 3
  71. oid objid_mib[] = { 1, 3, 6, 1, 2, 1 };
  72. int numprinted = 0;
  73. void
  74. usage(void)
  75. {
  76. fprintf(stderr, "USAGE: snmpwalk ");
  77. snmp_parse_args_usage(stderr);
  78. fprintf(stderr, " [OID]\n\n");
  79. snmp_parse_args_descriptions(stderr);
  80. fprintf(stderr,
  81. " -C APPOPTS\t\tSet various application specific behaviours:\n");
  82. fprintf(stderr, "\t\t\t p: print the number of variables found\n");
  83. fprintf(stderr, "\t\t\t i: include given OID in the search range\n");
  84. fprintf(stderr,
  85. "\t\t\t c: do not check returned OIDs are increasing\n");
  86. }
  87. void
  88. snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len)
  89. {
  90. netsnmp_pdu *pdu, *response;
  91. netsnmp_variable_list *vars;
  92. int status;
  93. pdu = snmp_pdu_create(SNMP_MSG_GET);
  94. snmp_add_null_var(pdu, theoid, theoid_len);
  95. status = snmp_synch_response(ss, pdu, &response);
  96. if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
  97. for (vars = response->variables; vars; vars = vars->next_variable) {
  98. numprinted++;
  99. print_variable(vars->name, vars->name_length, vars);
  100. }
  101. }
  102. if (response) {
  103. snmp_free_pdu(response);
  104. }
  105. }
  106. static void
  107. optProc(int argc, char *const *argv, int opt)
  108. {
  109. switch (opt) {
  110. case 'C':
  111. while (*optarg) {
  112. switch (*optarg++) {
  113. case 'i':
  114. netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  115. NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  116. break;
  117. case 'p':
  118. netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  119. NETSNMP_DS_WALK_PRINT_STATISTICS);
  120. break;
  121. case 'c':
  122. netsnmp_ds_toggle_boolean(NETSNMP_DS_APPLICATION_ID,
  123. NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  124. break;
  125. default:
  126. fprintf(stderr, "Unknown flag passed to -C: %c\n",
  127. optarg[-1]);
  128. exit(1);
  129. }
  130. }
  131. break;
  132. }
  133. }
  134. int
  135. main(int argc, char *argv[])
  136. {
  137. netsnmp_session session, *ss;
  138. netsnmp_pdu *pdu, *response;
  139. netsnmp_variable_list *vars;
  140. int arg;
  141. oid name[MAX_OID_LEN];
  142. size_t name_length;
  143. oid root[MAX_OID_LEN];
  144. size_t rootlen;
  145. int count;
  146. int running;
  147. int status;
  148. int check;
  149. int exitval = 0;
  150. netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "includeRequested",
  151. NETSNMP_DS_APPLICATION_ID,
  152. NETSNMP_DS_WALK_INCLUDE_REQUESTED);
  153. netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "printStatistics",
  154. NETSNMP_DS_APPLICATION_ID,
  155. NETSNMP_DS_WALK_PRINT_STATISTICS);
  156. netsnmp_ds_register_config(ASN_BOOLEAN, "snmpwalk", "dontCheckOrdering",
  157. NETSNMP_DS_APPLICATION_ID,
  158. NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  159. /*
  160. * get the common command line arguments
  161. */
  162. switch (arg = snmp_parse_args(argc, argv, &session, "C:", optProc)) {
  163. case -2:
  164. exit(0);
  165. case -1:
  166. usage();
  167. exit(1);
  168. default:
  169. break;
  170. }
  171. /*
  172. * get the initial object and subtree
  173. */
  174. if (arg < argc) {
  175. /*
  176. * specified on the command line
  177. */
  178. rootlen = MAX_OID_LEN;
  179. if (snmp_parse_oid(argv[arg], root, &rootlen) == NULL) {
  180. snmp_perror(argv[arg]);
  181. exit(1);
  182. }
  183. } else {
  184. /*
  185. * use default value
  186. */
  187. memmove(root, objid_mib, sizeof(objid_mib));
  188. rootlen = sizeof(objid_mib) / sizeof(oid);
  189. }
  190. SOCK_STARTUP;
  191. /*
  192. * open an SNMP session
  193. */
  194. ss = snmp_open(&session);
  195. if (ss == NULL) {
  196. /*
  197. * diagnose snmp_open errors with the input netsnmp_session pointer
  198. */
  199. snmp_sess_perror("snmpwalk", &session);
  200. SOCK_CLEANUP;
  201. exit(1);
  202. }
  203. /*
  204. * get first object to start walk
  205. */
  206. memmove(name, root, rootlen * sizeof(oid));
  207. name_length = rootlen;
  208. running = 1;
  209. check =
  210. !netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID,
  211. NETSNMP_DS_WALK_DONT_CHECK_LEXICOGRAPHIC);
  212. if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_INCLUDE_REQUESTED)) {
  213. snmp_get_and_print(ss, root, rootlen);
  214. }
  215. while (running) {
  216. /*
  217. * create PDU for GETNEXT request and add object name to request
  218. */
  219. pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
  220. snmp_add_null_var(pdu, name, name_length);
  221. /*
  222. * do the request
  223. */
  224. status = snmp_synch_response(ss, pdu, &response);
  225. if (status == STAT_SUCCESS) {
  226. if (response->errstat == SNMP_ERR_NOERROR) {
  227. /*
  228. * check resulting variables
  229. */
  230. for (vars = response->variables; vars;
  231. vars = vars->next_variable) {
  232. if ((vars->name_length < rootlen)
  233. || (memcmp(root, vars->name, rootlen * sizeof(oid))
  234. != 0)) {
  235. /*
  236. * not part of this subtree
  237. */
  238. running = 0;
  239. continue;
  240. }
  241. numprinted++;
  242. print_variable(vars->name, vars->name_length, vars);
  243. if ((vars->type != SNMP_ENDOFMIBVIEW) &&
  244. (vars->type != SNMP_NOSUCHOBJECT) &&
  245. (vars->type != SNMP_NOSUCHINSTANCE)) {
  246. /*
  247. * not an exception value
  248. */
  249. if (check
  250. && snmp_oid_compare(name, name_length,
  251. vars->name,
  252. vars->name_length) >= 0) {
  253. fprintf(stderr, "Error: OID not increasing: ");
  254. fprint_objid(stderr, name, name_length);
  255. fprintf(stderr, " >= ");
  256. fprint_objid(stderr, vars->name,
  257. vars->name_length);
  258. fprintf(stderr, "\n");
  259. running = 0;
  260. exitval = 1;
  261. }
  262. memmove((char *) name, (char *) vars->name,
  263. vars->name_length * sizeof(oid));
  264. name_length = vars->name_length;
  265. } else
  266. /*
  267. * an exception value, so stop
  268. */
  269. running = 0;
  270. }
  271. } else {
  272. /*
  273. * error in response, print it
  274. */
  275. running = 0;
  276. if (response->errstat == SNMP_ERR_NOSUCHNAME) {
  277. printf("End of MIB\n");
  278. } else {
  279. fprintf(stderr, "Error in packet.\nReason: %s\n",
  280. snmp_errstring(response->errstat));
  281. if (response->errindex != 0) {
  282. fprintf(stderr, "Failed object: ");
  283. for (count = 1, vars = response->variables;
  284. vars && count != response->errindex;
  285. vars = vars->next_variable, count++)
  286. /*EMPTY*/;
  287. if (vars)
  288. fprint_objid(stderr, vars->name,
  289. vars->name_length);
  290. fprintf(stderr, "\n");
  291. }
  292. exitval = 2;
  293. }
  294. }
  295. } else if (status == STAT_TIMEOUT) {
  296. fprintf(stderr, "Timeout: No Response from %s\n",
  297. session.peername);
  298. running = 0;
  299. exitval = 1;
  300. } else { /* status == STAT_ERROR */
  301. snmp_sess_perror("snmpwalk", ss);
  302. running = 0;
  303. exitval = 1;
  304. }
  305. if (response)
  306. snmp_free_pdu(response);
  307. }
  308. if (numprinted == 0 && status == STAT_SUCCESS) {
  309. /*
  310. * no printed successful results, which may mean we were
  311. * pointed at an only existing instance. Attempt a GET, just
  312. * for get measure.
  313. */
  314. snmp_get_and_print(ss, root, rootlen);
  315. }
  316. snmp_close(ss);
  317. if (netsnmp_ds_get_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_WALK_PRINT_STATISTICS)) {
  318. printf("Variables found: %d\n", numprinted);
  319. }
  320. SOCK_CLEANUP;
  321. return exitval;
  322. }