CSnmptrap.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913
  1. #include "CSnmptrap.h"
  2. #include "TSysTime.h"
  3. #include "CSysFormat.h"
  4. oid CSnmptrap::objid_enterprise[] = { 1, 3, 6, 1, 4, 1, 3, 1, 1 };
  5. oid CSnmptrap::objid_sysuptime[] = { 1, 3, 6, 1, 2, 1, 1, 3, 0 };
  6. oid CSnmptrap::objid_snmptrap[] = { 1, 3, 6, 1, 6, 3, 1, 1, 4, 1, 0 };
  7. CSnmptrap::CSnmptrap()
  8. {
  9. inform = 0;
  10. }
  11. CSnmptrap::~CSnmptrap()
  12. {
  13. }
  14. std::string CSnmptrap::FindString(std::string a, std::string b)
  15. {
  16. if (a.size()>b.size())
  17. swap(a, b); //确保前面的一个字符串短;
  18. std::string str_m;
  19. for (int i = 0; i<a.size(); i++)
  20. {
  21. for (int j = i; j<a.size(); j++)
  22. {
  23. std::string temp = a.substr(i, j - i + 1);
  24. if (int(b.find(temp))<0)
  25. break;
  26. else if (str_m.size()<temp.size())
  27. str_m = temp;
  28. }
  29. }
  30. return str_m;
  31. }
  32. char* CSnmptrap::CreateBuff(std::string src)
  33. {
  34. char* m_pResult = NULL;
  35. m_pResult = (char*)malloc(src.size() + 1);
  36. memset(m_pResult, 0, src.size() + 1);
  37. memcpy(m_pResult, src.c_str(), src.size());
  38. return m_pResult;
  39. }
  40. bool CSnmptrap::SendTrap2(int status, std::string ip, std::string comunnity, std::string alarmType, std::string moduleIdentity, const char* custom, int needTime)
  41. {
  42. if (ip == "")
  43. return false;
  44. char* m_pIp = CreateBuff(ip);
  45. char* m_pComunnity = CreateBuff(comunnity);
  46. char* m_Iidentity = CreateBuff(moduleIdentity);
  47. std::string times = "[" + TSysTimeHelper::GetLocalTimeString() + "]";
  48. if (needTime)
  49. OutputDebugStringA(fmt::format("Send Trap Times:{}",times).c_str());
  50. if (0 == status) {
  51. std::string m_sbuffer = alarmType + " Cleared";
  52. if (needTime) {
  53. m_sbuffer = times + m_sbuffer;
  54. }
  55. char* m_pAlarm = CreateBuff(m_sbuffer);
  56. if (m_pAlarm) {
  57. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", m_Iidentity, "SNMPv2-MIB::sysLocation.0","s",m_pAlarm };
  58. trapmain(11, argv2,custom);
  59. free(m_pAlarm);
  60. m_pAlarm = NULL;
  61. }
  62. }
  63. else {
  64. std::string buf = alarmType;
  65. if (needTime) {
  66. buf = times + buf;
  67. }
  68. char* m_pAlarm = CreateBuff(buf);
  69. if (m_pAlarm) {
  70. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", m_Iidentity, "SNMPv2-MIB::sysLocation.0","s",m_pAlarm };
  71. trapmain(11, argv2,custom);
  72. free(m_pAlarm);
  73. m_pAlarm = NULL;
  74. }
  75. }
  76. free(m_pIp);
  77. free(m_pComunnity);
  78. free(m_Iidentity);
  79. m_pIp = NULL;
  80. m_pComunnity = NULL;
  81. m_Iidentity = NULL;
  82. return true;
  83. }
  84. //status: 0/正常 1/触发报警
  85. void CSnmptrap::SendTrapH3c(int status, std::string ip, std::string comunnity, std::string alarmType)
  86. {
  87. //char* argv2[] = {"netsnmp_ALL.exe","-v", "2c", "-c", "public", "192.168.31.181", "", "1.3.6.1.4.1.2345", "HH3C-REDUNDANCY-POWER-MIB::hh3credundancyPowerStatus","i","2"};
  88. //char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", "public", "192.168.31.181", "", "1.3.6.1.4.1.2345", "SNMPv2-MIB::sysLocation.0","s","just666" };
  89. //去除\n回车,否则影响整形数据
  90. //int m_nindex = buff.find('\n');
  91. //if (m_nindex != buff.npos) {
  92. // buff = buff.substr(0, m_nindex);
  93. //}
  94. //char* m_pbuf = (char*)malloc(buff.size() + 1);
  95. //memset(m_pbuf,0, buff.size() + 1);
  96. //memcpy(m_pbuf, buff.c_str(), buff.size());
  97. char* m_pIp = CreateBuff(ip);
  98. char* m_pComunnity = CreateBuff(comunnity);
  99. if (0 == status) {
  100. std::string m_sbuffer = alarmType + " Cleared";
  101. char* m_pAlarm = CreateBuff(m_sbuffer);
  102. if (m_pAlarm) {
  103. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", "1.3.6.1.4.1.2345", "SNMPv2-MIB::sysLocation.0","s",m_pAlarm };
  104. trapmain(11, argv2,"0.0.0.0");
  105. free(m_pAlarm);
  106. m_pAlarm = NULL;
  107. }
  108. }
  109. else {
  110. char* m_pAlarm = CreateBuff(alarmType);
  111. if (m_pAlarm) {
  112. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", "1.3.6.1.4.1.2345", "SNMPv2-MIB::sysLocation.0","s",m_pAlarm };
  113. trapmain(11, argv2,"0.0.0.0");
  114. free(m_pAlarm);
  115. m_pAlarm = NULL;
  116. }
  117. }
  118. free(m_pIp);
  119. free(m_pComunnity);
  120. m_pIp = NULL;
  121. m_pComunnity = NULL;
  122. }
  123. //status: 0/正常 1/触发报警
  124. void CSnmptrap::SendTrap(int status,std::string ip, std::string comunnity,std::string alarmType, std::string moduleIdentity, std::string power, const char* custom, int needTime)
  125. {
  126. //char* argv2[] = {"netsnmp_ALL.exe","-v", "2c", "-c", "public", "192.168.31.181", "", "1.3.6.1.4.1.2345", "HH3C-REDUNDANCY-POWER-MIB::hh3credundancyPowerStatus","i","2"};
  127. //char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", "public", "192.168.31.181", "", "1.3.6.1.4.1.2345", "SNMPv2-MIB::sysLocation.0","s","just666" };
  128. if (ip == "")
  129. return;
  130. char* m_pIp = CreateBuff(ip);
  131. char* m_pComunnity = CreateBuff(comunnity);
  132. char* m_Iidentity = CreateBuff(moduleIdentity);
  133. char* m_pPower = CreateBuff(power);
  134. std::string times = "[" + TSysTimeHelper::GetLocalTimeString() + "]";
  135. if (needTime)
  136. OutputDebugStringA(fmt::format("Send Trap Times:{}", times).c_str());
  137. if (0 == status) {
  138. std::string m_sbuffer = alarmType + " Cleared";
  139. if (needTime) {
  140. m_sbuffer = times + m_sbuffer;
  141. }
  142. char* m_pAlarm = CreateBuff(m_sbuffer);
  143. if (m_pAlarm) {
  144. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", m_Iidentity, m_pPower,"s",m_pAlarm };
  145. trapmain(11, argv2,custom);
  146. free(m_pAlarm);
  147. m_pAlarm = NULL;
  148. }
  149. }
  150. else {
  151. std::string buf = alarmType;
  152. if (needTime) {
  153. buf = times + buf;
  154. }
  155. char* m_pAlarm = CreateBuff(buf);
  156. if (m_pAlarm) {
  157. char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", m_pComunnity, m_pIp, "", m_Iidentity, m_pPower,"s",m_pAlarm };
  158. trapmain(11, argv2,custom);
  159. free(m_pAlarm);
  160. m_pAlarm = NULL;
  161. }
  162. }
  163. free(m_pIp);
  164. free(m_pComunnity);
  165. free(m_Iidentity);
  166. free(m_pPower);
  167. m_pIp = NULL;
  168. m_pComunnity = NULL;
  169. m_pPower = NULL;
  170. m_Iidentity = NULL;
  171. }
  172. int CSnmptrap::snmp_input1(int operation,netsnmp_session * session,int reqid, netsnmp_pdu *pdu, void *magic)
  173. {
  174. printf("session.callback\n");
  175. return 1;
  176. }
  177. void CSnmptrap::optProc(int argc, char *const *argv, int opt)
  178. {
  179. switch (opt) {
  180. case 'C':
  181. while (*optarg) {
  182. switch (*optarg++) {
  183. case 'i':
  184. //inform = 1;
  185. break;
  186. default:
  187. fprintf(stderr,
  188. "Unknown flag passed to -C: %c\n", optarg[-1]);
  189. exit(1);
  190. }
  191. }
  192. break;
  193. }
  194. }
  195. int CSnmptrap::netsnmp_parse_args1(int argc,char **argv,netsnmp_session * session, const char *localOpts,void(*proc) (int, char *const *, int),int flags)
  196. {
  197. int arg, ret, sp = 0;
  198. char *cp;
  199. char *Apsz = NULL;
  200. char *Xpsz = NULL;
  201. char *Cpsz = NULL;
  202. char Opts[512];
  203. int zero_sensitive = !(flags & NETSNMP_PARSE_ARGS_NOZERO);
  204. char *backup_NETSNMP_DS_LIB_OUTPUT_PRECISION = NULL;
  205. /*
  206. * initialize session to default values
  207. */
  208. snmp_sess_init(session);
  209. strcpy(Opts, "Y:VhHm:M:O:I:P:D:dv:r:t:c:Z:e:E:n:u:l:x:X:a:A:p:T:-:3:L:s:");
  210. if (localOpts) {
  211. if (strlen(localOpts) + strlen(Opts) >= sizeof(Opts)) {
  212. snmp_log(LOG_ERR, "Too many localOpts in snmp_parse_args()\n");
  213. return -1;
  214. }
  215. strcat(Opts, localOpts);
  216. }
  217. /*
  218. * get the options
  219. */
  220. DEBUGMSGTL(("snmp_parse_args", "starting: %d/%d\n", optind, argc));
  221. for (arg = 0; arg < argc; arg++) {
  222. DEBUGMSGTL(("snmp_parse_args", " arg %d = %s\n", arg, argv[arg]));
  223. }
  224. optind = 1;
  225. while ((arg = getopt(argc, argv, Opts)) != EOF) {
  226. DEBUGMSGTL(("snmp_parse_args", "handling (#%d): %c (optarg %s) (sp %d)\n",
  227. optind, arg, optarg, sp));
  228. switch (arg) {
  229. case '-':
  230. if (strcasecmp(optarg, "help") == 0) {
  231. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  232. goto out;
  233. }
  234. if (strcasecmp(optarg, "version") == 0) {
  235. fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version());
  236. ret = NETSNMP_PARSE_ARGS_SUCCESS_EXIT;
  237. goto out;
  238. }
  239. handle_long_opt(optarg);
  240. break;
  241. case 'V':
  242. fprintf(stderr, "NET-SNMP version: %s\n", netsnmp_get_version());
  243. ret = NETSNMP_PARSE_ARGS_SUCCESS_EXIT;
  244. goto out;
  245. case 'h':
  246. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  247. goto out;
  248. case 'H':
  249. init_snmp(NETSNMP_APPLICATION_CONFIG_TYPE);
  250. fprintf(stderr, "Configuration directives understood:\n");
  251. read_config_print_usage(" ");
  252. ret = NETSNMP_PARSE_ARGS_SUCCESS_EXIT;
  253. goto out;
  254. case 'Y':
  255. netsnmp_config_remember(optarg);
  256. break;
  257. case 'm':
  258. setenv("MIBS", optarg, 1);
  259. break;
  260. case 'M':
  261. netsnmp_get_mib_directory(); /* prepare the default directories */
  262. netsnmp_set_mib_directory(optarg);
  263. break;
  264. case 'O':
  265. cp = snmp_out_options(optarg, argc, argv);
  266. if (cp != NULL) {
  267. fprintf(stderr, "Unknown output option passed to -O: %c.\n",
  268. *cp);
  269. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  270. goto out;
  271. }
  272. break;
  273. case 'I':
  274. cp = snmp_in_options(optarg, argc, argv);
  275. if (cp != NULL) {
  276. fprintf(stderr, "Unknown input option passed to -I: %c.\n",
  277. *cp);
  278. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  279. goto out;
  280. }
  281. break;
  282. case 'P':
  283. cp = snmp_mib_toggle_options(optarg);
  284. if (cp != NULL) {
  285. fprintf(stderr,
  286. "Unknown parsing option passed to -P: %c.\n", *cp);
  287. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  288. goto out;
  289. }
  290. break;
  291. case 'D':
  292. debug_register_tokens(optarg);
  293. snmp_set_do_debugging(1);
  294. break;
  295. case 'd':
  296. netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID,
  297. NETSNMP_DS_LIB_DUMP_PACKET, 1);
  298. break;
  299. case 's':
  300. session->localname = strdup(optarg);
  301. break;
  302. case 'v':
  303. session->version = -1;
  304. if (!strcmp(optarg, "1")) {
  305. session->version = SNMP_VERSION_1;
  306. }
  307. if (!strcasecmp(optarg, "2c")) {
  308. session->version = SNMP_VERSION_2c;
  309. }
  310. if (!strcasecmp(optarg, "3")) {
  311. session->version = SNMP_VERSION_3;
  312. }
  313. if (session->version == -1) {
  314. fprintf(stderr,
  315. "Invalid version specified after -v flag: %s\n",
  316. optarg);
  317. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  318. goto out;
  319. }
  320. break;
  321. case 'p':
  322. fprintf(stderr, "Warning: -p option is no longer used - ");
  323. fprintf(stderr, "specify the remote host as HOST:PORT\n");
  324. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  325. goto out;
  326. case 'T':
  327. {
  328. char leftside[SNMP_MAXBUF_MEDIUM], rightside[SNMP_MAXBUF_MEDIUM];
  329. char *tmpcp, *tmpopt;
  330. /* ensure we have a proper argument */
  331. tmpopt = strdup(optarg);
  332. tmpcp = strchr(tmpopt, '=');
  333. if (!tmpcp) {
  334. fprintf(stderr, "-T expects a NAME=VALUE pair.\n");
  335. free(tmpopt);
  336. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  337. goto out;
  338. }
  339. *tmpcp++ = '\0';
  340. /* create the transport config container if this is the first */
  341. if (!session->transport_configuration) {
  342. netsnmp_container_init_list();
  343. session->transport_configuration =
  344. netsnmp_container_find("transport_configuration:fifo");
  345. if (!session->transport_configuration) {
  346. fprintf(stderr, "failed to initialize the transport configuration container\n");
  347. free(tmpopt);
  348. ret = NETSNMP_PARSE_ARGS_ERROR;
  349. goto out;
  350. }
  351. session->transport_configuration->compare =
  352. (netsnmp_container_compare*)
  353. netsnmp_transport_config_compare;
  354. }
  355. /* set the config */
  356. strlcpy(leftside, tmpopt, sizeof(leftside));
  357. strlcpy(rightside, tmpcp, sizeof(rightside));
  358. CONTAINER_INSERT(session->transport_configuration,
  359. netsnmp_transport_create_config(leftside,
  360. rightside));
  361. free(tmpopt);
  362. }
  363. break;
  364. case 't':
  365. session->timeout = (long)(atof(optarg) * 1000000L);
  366. if (session->timeout <= 0) {
  367. fprintf(stderr, "Invalid timeout in seconds after -t flag.\n");
  368. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  369. goto out;
  370. }
  371. break;
  372. case 'r':
  373. session->retries = atoi(optarg);
  374. if (session->retries < 0 || !isdigit((unsigned char)(optarg[0]))) {
  375. fprintf(stderr, "Invalid number of retries after -r flag.\n");
  376. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  377. goto out;
  378. }
  379. break;
  380. case 'c':
  381. if (zero_sensitive) {
  382. SNMP_FREE(Cpsz); /* free any previous value */
  383. if ((Cpsz = strdup(optarg)) != NULL) {
  384. //memcpy(optarg,0, sizeof(optarg));
  385. //memset(optarg, '\0', strlen(optarg));
  386. }
  387. else {
  388. fprintf(stderr, "malloc failure processing -c flag.\n");
  389. ret = NETSNMP_PARSE_ARGS_ERROR;
  390. goto out;
  391. }
  392. }
  393. else {
  394. Cpsz = strdup(optarg);
  395. }
  396. break;
  397. case '3':
  398. if (snmpv3_parse_args(optarg, session, &Apsz, &Xpsz, argc, argv,
  399. flags) < 0) {
  400. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  401. goto out;
  402. }
  403. break;
  404. case 'L':
  405. if (snmp_log_options(optarg, argc, argv) < 0) {
  406. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  407. goto out;
  408. }
  409. break;
  410. #define SNMPV3_CMD_OPTIONS
  411. #ifdef SNMPV3_CMD_OPTIONS
  412. case 'Z':
  413. case 'e':
  414. case 'E':
  415. case 'n':
  416. case 'l':
  417. case 'u':
  418. #ifdef NETSNMP_SECMOD_USM
  419. case 'a':
  420. case 'x':
  421. case 'A':
  422. case 'X':
  423. #endif /* NETSNMP_SECMOD_USM */
  424. if (snmpv3_parse_arg(arg, optarg, session, &Apsz, &Xpsz, argc,
  425. argv, flags) < 0) {
  426. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  427. goto out;
  428. }
  429. break;
  430. #endif /* SNMPV3_CMD_OPTIONS */
  431. case '?':
  432. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  433. goto out;
  434. default:
  435. proc(argc, argv, arg);
  436. break;
  437. }
  438. }
  439. DEBUGMSGTL(("snmp_parse_args", "finished: %d/%d\n", optind, argc));
  440. /*
  441. * save command line parameters which should have precedence above config file settings
  442. * (There ought to be a more scalable approach than this....)
  443. */
  444. if (netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION)) {
  445. backup_NETSNMP_DS_LIB_OUTPUT_PRECISION =
  446. strdup(netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION));
  447. }
  448. /*
  449. * read in MIB database and initialize the snmp library, read the config file
  450. */
  451. init_snmp(NETSNMP_APPLICATION_CONFIG_TYPE);
  452. /*
  453. * restore command line parameters which should have precedence above config file settings
  454. */
  455. if (backup_NETSNMP_DS_LIB_OUTPUT_PRECISION) {
  456. netsnmp_ds_set_string(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_OUTPUT_PRECISION, backup_NETSNMP_DS_LIB_OUTPUT_PRECISION);
  457. free(backup_NETSNMP_DS_LIB_OUTPUT_PRECISION);
  458. }
  459. /*
  460. * session default version
  461. */
  462. if (session->version == SNMP_DEFAULT_VERSION) {
  463. /*
  464. * run time default version
  465. */
  466. session->version = netsnmp_ds_get_int(NETSNMP_DS_LIBRARY_ID,
  467. NETSNMP_DS_LIB_SNMPVERSION);
  468. /*
  469. * compile time default version
  470. */
  471. if (!session->version) {
  472. switch (NETSNMP_DEFAULT_SNMP_VERSION) {
  473. case 1:
  474. session->version = SNMP_VERSION_1;
  475. break;
  476. case 2:
  477. session->version = SNMP_VERSION_2c;
  478. break;
  479. case 3:
  480. session->version = SNMP_VERSION_3;
  481. break;
  482. default:
  483. snmp_log(LOG_ERR, "Can't determine a valid SNMP version for the session\n");
  484. return(NETSNMP_PARSE_ARGS_ERROR);
  485. }
  486. }
  487. else {
  488. if (session->version == NETSNMP_DS_SNMP_VERSION_1) /* bogus value. version 1 actually = 0 */
  489. session->version = SNMP_VERSION_1;
  490. }
  491. }
  492. #ifdef NETSNMP_SECMOD_USM
  493. /* XXX: this should ideally be moved to snmpusm.c somehow */
  494. /*
  495. * make master key from pass phrases
  496. */
  497. if (Apsz) {
  498. session->securityAuthKeyLen = USM_AUTH_KU_LEN;
  499. if (session->securityAuthProto == NULL) {
  500. /*
  501. * get .conf set default
  502. */
  503. const oid *def =
  504. get_default_authtype(&session->securityAuthProtoLen);
  505. session->securityAuthProto =
  506. snmp_duplicate_objid(def, session->securityAuthProtoLen);
  507. }
  508. if (session->securityAuthProto == NULL) {
  509. session->securityAuthProto =
  510. snmp_duplicate_objid(SNMP_DEFAULT_AUTH_PROTO,
  511. SNMP_DEFAULT_AUTH_PROTOLEN);
  512. session->securityAuthProtoLen = SNMP_DEFAULT_AUTH_PROTOLEN;
  513. }
  514. if (generate_Ku(session->securityAuthProto,
  515. session->securityAuthProtoLen,
  516. (u_char *)Apsz, strlen(Apsz),
  517. session->securityAuthKey,
  518. &session->securityAuthKeyLen) != SNMPERR_SUCCESS) {
  519. snmp_perror(argv[0]);
  520. fprintf(stderr,
  521. "Error generating a key (Ku) from the supplied authentication pass phrase. \n");
  522. ret = NETSNMP_PARSE_ARGS_ERROR;
  523. goto out;
  524. }
  525. free(Apsz);
  526. Apsz = NULL;
  527. }
  528. if (Xpsz) {
  529. session->securityPrivKeyLen = USM_PRIV_KU_LEN;
  530. if (session->securityPrivProto == NULL) {
  531. /*
  532. * get .conf set default
  533. */
  534. const oid *def =
  535. get_default_privtype(&session->securityPrivProtoLen);
  536. session->securityPrivProto =
  537. snmp_duplicate_objid(def, session->securityPrivProtoLen);
  538. }
  539. if (session->securityPrivProto == NULL) {
  540. session->securityPrivProto =
  541. snmp_duplicate_objid(SNMP_DEFAULT_PRIV_PROTO,
  542. SNMP_DEFAULT_PRIV_PROTOLEN);
  543. session->securityPrivProtoLen = SNMP_DEFAULT_PRIV_PROTOLEN;
  544. }
  545. if (generate_Ku(session->securityAuthProto,
  546. session->securityAuthProtoLen,
  547. (u_char *)Xpsz, strlen(Xpsz),
  548. session->securityPrivKey,
  549. &session->securityPrivKeyLen) != SNMPERR_SUCCESS) {
  550. snmp_perror(argv[0]);
  551. fprintf(stderr,
  552. "Error generating a key (Ku) from the supplied privacy pass phrase. \n");
  553. ret = NETSNMP_PARSE_ARGS_ERROR;
  554. goto out;
  555. }
  556. free(Xpsz);
  557. Xpsz = NULL;
  558. }
  559. #endif /* NETSNMP_SECMOD_USM */
  560. /*
  561. * get the hostname
  562. */
  563. if (optind == argc) {
  564. fprintf(stderr, "No hostname specified.\n");
  565. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  566. goto out;
  567. }
  568. session->peername = argv[optind++]; /* hostname */
  569. #if !defined(NETSNMP_DISABLE_SNMPV1) || !defined(NETSNMP_DISABLE_SNMPV2C)
  570. /*
  571. * If v1 or v2c, check community has been set, either by a -c option above,
  572. * or via a default token somewhere.
  573. * If neither, it will be taken from the incoming request PDU.
  574. */
  575. if (session->version == SNMP_VERSION_1 || session->version == SNMP_VERSION_2c)
  576. {
  577. if (Cpsz == NULL) {
  578. Cpsz = netsnmp_ds_get_string(NETSNMP_DS_LIBRARY_ID,
  579. NETSNMP_DS_LIB_COMMUNITY);
  580. if (Cpsz == NULL) {
  581. if (netsnmp_ds_get_boolean(NETSNMP_DS_LIBRARY_ID,
  582. NETSNMP_DS_LIB_IGNORE_NO_COMMUNITY)) {
  583. DEBUGMSGTL(("snmp_parse_args",
  584. "ignoring that the community string is not present\n"));
  585. session->community = NULL;
  586. session->community_len = 0;
  587. }
  588. else {
  589. fprintf(stderr, "No community name specified.\n");
  590. ret = NETSNMP_PARSE_ARGS_ERROR_USAGE;
  591. goto out;
  592. }
  593. }
  594. else {
  595. Cpsz = NULL;
  596. }
  597. }
  598. else {
  599. session->community = (unsigned char *)Cpsz;
  600. session->community_len = strlen(Cpsz);
  601. Cpsz = NULL;
  602. }
  603. }
  604. #endif /* support for community based SNMP */
  605. ret = optind;
  606. out:
  607. free(Apsz);
  608. free(Xpsz);
  609. free(Cpsz);
  610. return ret;
  611. }
  612. int CSnmptrap::snmp_parse_args1(int argc, char **argv, netsnmp_session * session, const char *localOpts, void(*proc) (int, char *const *, int))
  613. {
  614. return netsnmp_parse_args1(argc, argv, session, localOpts, proc, 0);
  615. }
  616. int CSnmptrap::trapmain(int argc, char *argv[],const char* custom)
  617. {
  618. netsnmp_session session, *ss;
  619. netsnmp_pdu *pdu, *response;
  620. oid name[MAX_OID_LEN];
  621. size_t name_length;
  622. int arg;
  623. int status;
  624. char *trap = NULL;
  625. int exitval = 1;
  626. #ifndef NETSNMP_DISABLE_SNMPV1
  627. char *specific = NULL, *description = NULL, *agent = NULL;
  628. in_addr_t *pdu_in_addr_t;
  629. #endif
  630. SOCK_STARTUP;
  631. //char* argv2[] = { "netsnmp_ALL.exe","-v", "2c", "-c", "public", "192.168.31.8", "", "1.3.6.1.4.1.2345", "HH3C-REDUNDANCY-POWER-MIB::hh3credundancyPowerStatus","i","2" };
  632. //argv = argv2;
  633. /** parse args (also initializes session) */
  634. arg = snmp_parse_args1(argc, argv, &session, "C:", optProc);
  635. session.callback = snmp_input1;
  636. session.callback_magic = NULL;
  637. /*
  638. * setup the local engineID which may be for either or both of the
  639. * contextEngineID and/or the securityEngineID.
  640. */
  641. setup_engineID(NULL, NULL);
  642. /* if we don't have a contextEngineID set via command line
  643. arguments, use our internal engineID as the context. */
  644. if (session.contextEngineIDLen == 0 ||
  645. session.contextEngineID == NULL) {
  646. session.contextEngineID =
  647. snmpv3_generate_engineID(&session.contextEngineIDLen);
  648. }
  649. if (session.version == SNMP_VERSION_3 && !inform) {
  650. /*
  651. * for traps, we use ourselves as the authoritative engine
  652. * which is really stupid since command line apps don't have a
  653. * notion of a persistent engine. Hence, our boots and time
  654. * values are probably always really wacked with respect to what
  655. * a manager would like to see.
  656. *
  657. * The following should be enough to:
  658. *
  659. * 1) prevent the library from doing discovery for engineid & time.
  660. * 2) use our engineid instead of the remote engineid for
  661. * authoritative & privacy related operations.
  662. * 3) The remote engine must be configured with users for our engineID.
  663. *
  664. * -- Wes
  665. */
  666. /*
  667. * pick our own engineID
  668. */
  669. if (session.securityEngineIDLen == 0 ||
  670. session.securityEngineID == NULL) {
  671. session.securityEngineID =
  672. snmpv3_generate_engineID(&session.securityEngineIDLen);
  673. }
  674. /*
  675. * set boots and time, which will cause problems if this
  676. * machine ever reboots and a remote trap receiver has cached our
  677. * boots and time... I'll cause a not-in-time-window report to
  678. * be sent back to this machine.
  679. */
  680. if (session.engineBoots == 0)
  681. session.engineBoots = 1;
  682. if (session.engineTime == 0) /* not really correct, */
  683. session.engineTime = get_uptime(); /* but it'll work. Sort of. */
  684. set_enginetime(session.securityEngineID, session.securityEngineIDLen,
  685. session.engineBoots, session.engineTime, TRUE);
  686. }
  687. ss = snmp_add(&session,
  688. netsnmp_transport_open_client("snmptrap", session.peername,custom),
  689. NULL, NULL);
  690. if (ss == NULL) {
  691. /*
  692. * diagnose netsnmp_transport_open_client and snmp_add errors with
  693. * the input netsnmp_session pointer
  694. */
  695. snmp_sess_perror("snmptrap", &session);
  696. goto out;
  697. }
  698. #ifndef NETSNMP_DISABLE_SNMPV1
  699. if (session.version == SNMP_VERSION_1) {
  700. if (inform) {
  701. fprintf(stderr, "Cannot send INFORM as SNMPv1 PDU\n");
  702. goto out;
  703. }
  704. pdu = snmp_pdu_create(SNMP_MSG_TRAP);
  705. if (!pdu) {
  706. fprintf(stderr, "Failed to create trap PDU\n");
  707. goto out;
  708. }
  709. pdu_in_addr_t = (in_addr_t *)pdu->agent_addr;
  710. if (arg == argc) {
  711. fprintf(stderr, "No enterprise oid\n");
  712. goto out;
  713. }
  714. if (argv[arg][0] == 0) {
  715. pdu->enterprise = (oid *)malloc(sizeof(objid_enterprise));
  716. memcpy(pdu->enterprise, objid_enterprise,
  717. sizeof(objid_enterprise));
  718. pdu->enterprise_length =
  719. sizeof(objid_enterprise) / sizeof(oid);
  720. }
  721. else {
  722. name_length = MAX_OID_LEN;
  723. if (!snmp_parse_oid(argv[arg], name, &name_length)) {
  724. snmp_perror(argv[arg]);
  725. goto out;
  726. }
  727. pdu->enterprise = (oid *)malloc(name_length * sizeof(oid));
  728. memcpy(pdu->enterprise, name, name_length * sizeof(oid));
  729. pdu->enterprise_length = name_length;
  730. }
  731. if (++arg >= argc) {
  732. fprintf(stderr, "Missing agent parameter\n");
  733. goto out;
  734. }
  735. agent = argv[arg];
  736. if (agent != NULL && strlen(agent) != 0) {
  737. int ret = netsnmp_gethostbyname_v4(agent, pdu_in_addr_t);
  738. if (ret < 0) {
  739. fprintf(stderr, "unknown host: %s\n", agent);
  740. goto out;
  741. }
  742. }
  743. else {
  744. *pdu_in_addr_t = get_myaddr();
  745. }
  746. if (++arg == argc) {
  747. fprintf(stderr, "Missing generic-trap parameter\n");
  748. // usage();
  749. goto out;
  750. }
  751. trap = argv[arg];
  752. pdu->trap_type = atoi(trap);
  753. if (++arg == argc) {
  754. fprintf(stderr, "Missing specific-trap parameter\n");
  755. goto out;
  756. }
  757. specific = argv[arg];
  758. pdu->specific_type = atoi(specific);
  759. if (++arg == argc) {
  760. fprintf(stderr, "Missing uptime parameter\n");
  761. goto out;
  762. }
  763. description = argv[arg];
  764. if (description == NULL || *description == 0)
  765. pdu->time = get_uptime();
  766. else
  767. pdu->time = atol(description);
  768. }
  769. else
  770. #endif
  771. {
  772. long sysuptime;
  773. char csysuptime[20];
  774. pdu = snmp_pdu_create(inform ? SNMP_MSG_INFORM : SNMP_MSG_TRAP2);
  775. if (!pdu) {
  776. fprintf(stderr, "Failed to create notification PDU\n");
  777. goto out;
  778. }
  779. if (arg == argc) {
  780. fprintf(stderr, "Missing up-time parameter\n");
  781. goto out;
  782. }
  783. trap = argv[arg];
  784. if (*trap == 0) {
  785. sysuptime = get_uptime();
  786. sprintf(csysuptime, "%ld", sysuptime);
  787. trap = csysuptime;
  788. }
  789. snmp_add_var(pdu, objid_sysuptime,
  790. sizeof(objid_sysuptime) / sizeof(oid), 't', trap);
  791. if (++arg == argc) {
  792. fprintf(stderr, "Missing trap-oid parameter\n");
  793. goto out;
  794. }
  795. if (snmp_add_var
  796. (pdu, objid_snmptrap, sizeof(objid_snmptrap) / sizeof(oid),
  797. 'o', argv[arg]) != 0) {
  798. snmp_perror(argv[arg]);
  799. goto out;
  800. }
  801. }
  802. arg++;
  803. while (arg < argc) {
  804. arg += 3;
  805. if (arg > argc) {
  806. fprintf(stderr, "%s: Missing type/value for variable\n",
  807. argv[arg - 3]);
  808. goto out;
  809. }
  810. name_length = MAX_OID_LEN;
  811. if (!snmp_parse_oid(argv[arg - 3], name, &name_length)) {
  812. snmp_perror(argv[arg - 3]);
  813. goto out;
  814. }
  815. if (snmp_add_var(pdu, name, name_length, argv[arg - 2][0],argv[arg - 1]) != 0) {
  816. snmp_perror(argv[arg - 3]);
  817. goto out;
  818. }
  819. }
  820. if (inform)
  821. status = snmp_synch_response(ss, pdu, &response);
  822. else
  823. status = snmp_send(ss, pdu) == 0;
  824. if (status) {
  825. snmp_sess_perror(inform ? "snmpinform" : "snmptrap", ss);
  826. if (!inform)
  827. snmp_free_pdu(pdu);
  828. goto close_session;
  829. }
  830. else if (inform)
  831. snmp_free_pdu(response);
  832. exitval = 0;
  833. close_session:
  834. snmp_close(ss);
  835. //snmp_shutdown(NETSNMP_APPLICATION_CONFIG_TYPE);
  836. out:
  837. SOCK_CLEANUP;
  838. return exitval;
  839. }