receive_trap.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*_############################################################################
  2. _##
  3. _## receive_trap.cpp
  4. _##
  5. _## SNMP++v3.2.23
  6. _## -----------------------------------------------
  7. _## Copyright (c) 2001-2007 Jochen Katz, Frank Fock
  8. _##
  9. _## This software is based on SNMP++2.6 from Hewlett Packard:
  10. _##
  11. _## Copyright (c) 1996
  12. _## Hewlett-Packard Company
  13. _##
  14. _## ATTENTION: USE OF THIS SOFTWARE IS SUBJECT TO THE FOLLOWING TERMS.
  15. _## Permission to use, copy, modify, distribute and/or sell this software
  16. _## and/or its documentation is hereby granted without fee. User agrees
  17. _## to display the above copyright notice and this license notice in all
  18. _## copies of the software and any documentation of the software. User
  19. _## agrees to assume all liability for the use of the software;
  20. _## Hewlett-Packard and Jochen Katz make no representations about the
  21. _## suitability of this software for any purpose. It is provided
  22. _## "AS-IS" without warranty of any kind, either express or implied. User
  23. _## hereby grants a royalty-free license to any and all derivatives based
  24. _## upon this software code base.
  25. _##
  26. _## Stuttgart, Germany, Sun Nov 11 15:10:59 CET 2007
  27. _##
  28. _##########################################################################*/
  29. char receivetrap_cpp_version[]="@(#) SNMP++ $Id: receive_trap.cpp 236 2006-03-10 22:11:35Z katz $";
  30. #include "snmp_pp/snmp_pp.h"
  31. #include "snmp_pp/collect.h"
  32. #include "snmp_pp/notifyqueue.h"
  33. #include <stdlib.h>
  34. #include <stdio.h>
  35. #ifdef SNMP_PP_NAMESPACE
  36. using namespace Snmp_pp;
  37. #endif
  38. #if (__GNUC__ > 2)
  39. #include <iostream>
  40. using std::cerr;
  41. using std::cout;
  42. using std::endl;
  43. using std::flush;
  44. #else
  45. #include <iostream.h>
  46. #endif
  47. void callback( int reason, Snmp *snmp, Pdu &pdu, SnmpTarget &target, void *cd)
  48. {
  49. Vb nextVb;
  50. int pdu_error;
  51. GenAddress addr;
  52. target.get_address(addr);
  53. UdpAddress from(addr);
  54. cout << "reason: " << reason << endl
  55. << "msg: " << snmp->error_msg(reason) << endl
  56. << "from: " << from.get_printable() << endl;
  57. pdu_error = pdu.get_error_status();
  58. if (pdu_error){
  59. cout << "Response contains error: "
  60. << snmp->error_msg(pdu_error)<< endl;
  61. }
  62. Oid id;
  63. pdu.get_notify_id(id);
  64. cout << "ID: " << id.get_printable() << endl;
  65. cout << "Type:" << pdu.get_type() << endl;
  66. for (int i=0; i<pdu.get_vb_count(); i++)
  67. {
  68. pdu.get_vb(nextVb, i);
  69. cout << "Oid: " << nextVb.get_printable_oid() << endl
  70. << "Val: " << nextVb.get_printable_value() << endl;
  71. }
  72. if (pdu.get_type() == sNMP_PDU_INFORM) {
  73. cout << "pdu type: " << pdu.get_type() << endl;
  74. cout << "sending response to inform: " << endl;
  75. nextVb.set_value("This is the response.");
  76. pdu.set_vb(nextVb, 0);
  77. snmp->response(pdu, target);
  78. }
  79. cout << endl;
  80. }
  81. int main(int argc, char **argv)
  82. {
  83. int trap_port;
  84. if (argc < 2)
  85. trap_port = 10162; // no need to be root
  86. else
  87. trap_port = atoi(argv[1]);
  88. //----------[ create a SNMP++ session ]-----------------------------------
  89. int status;
  90. Snmp::socket_startup(); // Initialize socket subsystem
  91. Snmp snmp(status); // check construction status
  92. if ( status != SNMP_CLASS_SUCCESS)
  93. {
  94. cout << "SNMP++ Session Create Fail, " << snmp.error_msg(status) << "\n";
  95. return 1;
  96. }
  97. //---------[ init SnmpV3 ]--------------------------------------------
  98. #ifdef _SNMPv3
  99. const unsigned char engineId[] =
  100. { 0x80, 0x00, 0x13, 0x70, 0xC0, 0xA8, 0x01, 0x0D };
  101. OctetStr engineID(engineId, 8);
  102. char *filename = "snmpv3_boot_counter";
  103. unsigned int snmpEngineBoots = 0;
  104. v3MP *v3_MP;
  105. status = getBootCounter(filename, engineID, snmpEngineBoots);
  106. if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
  107. {
  108. cout << "Error loading snmpEngineBoots counter: " << status << endl;
  109. return 1;
  110. }
  111. snmpEngineBoots++;
  112. status = saveBootCounter(filename, engineID, snmpEngineBoots);
  113. if (status != SNMPv3_OK)
  114. {
  115. cout << "Error saving snmpEngineBoots counter: " << status << endl;
  116. return 1;
  117. }
  118. int construct_status;
  119. v3_MP = new v3MP(engineID, snmpEngineBoots, construct_status);
  120. USM *usm = v3_MP->get_usm();
  121. // users at UCD
  122. usm->add_usm_user("SHADESUser",
  123. SNMPv3_usmHMACSHAAuthProtocol, SNMPv3_usmDESPrivProtocol,
  124. "The UCD Demo Password", "The UCD Demo Password");
  125. usm->add_usm_user("SHAMD5User",
  126. SNMPv3_usmHMACMD5AuthProtocol, SNMPv3_usmDESPrivProtocol,
  127. "The UCD Demo Password", "The UCD Demo Password");
  128. usm->add_usm_user("noAuthUser",
  129. SNMPv3_usmHMACMD5AuthProtocol, SNMPv3_usmDESPrivProtocol,
  130. "Password", "Password");
  131. // testusers
  132. usm->add_usm_user("unsecureUser",
  133. SNMPv3_usmNoAuthProtocol, SNMPv3_usmNoPrivProtocol,
  134. "", "");
  135. usm->add_usm_user("MD5",
  136. SNMPv3_usmHMACMD5AuthProtocol, SNMPv3_usmNoPrivProtocol,
  137. "MD5UserAuthPassword", "");
  138. usm->add_usm_user("SHA",
  139. SNMPv3_usmHMACSHAAuthProtocol, SNMPv3_usmNoPrivProtocol,
  140. "SHAUserAuthPassword", "");
  141. usm->add_usm_user("MD5DES",
  142. SNMPv3_usmHMACMD5AuthProtocol, SNMPv3_usmDESPrivProtocol,
  143. "MD5DESUserAuthPassword", "MD5DESUserPrivPassword");
  144. usm->add_usm_user("SHADES",
  145. SNMPv3_usmHMACSHAAuthProtocol, SNMPv3_usmDESPrivProtocol,
  146. "SHADESUserAuthPassword", "SHADESUserPrivPassword");
  147. usm->add_usm_user("MD5IDEA",
  148. SNMPv3_usmHMACMD5AuthProtocol, SNMPv3_usmIDEAPrivProtocol,
  149. "MD5IDEAUserAuthPassword", "MD5IDEAUserPrivPassword");
  150. usm->add_usm_user("SHAIDEA",
  151. SNMPv3_usmHMACSHAAuthProtocol, SNMPv3_usmIDEAPrivProtocol,
  152. "SHAIDEAUserAuthPassword", "SHAIDEAUserPrivPassword");
  153. #endif
  154. OidCollection oidc;
  155. TargetCollection targetc;
  156. cout << "Trying to register for traps on port " << trap_port << "." << endl;
  157. snmp.notify_set_listen_port(trap_port);
  158. status = snmp.notify_register(oidc, targetc, callback, NULL);
  159. if (status != SNMP_CLASS_SUCCESS)
  160. {
  161. cout << "Error register for notify (" << status << "): "
  162. << snmp.error_msg(status) << endl;
  163. exit(1);
  164. }
  165. else
  166. cout << "Waiting for traps/informs..." << endl;
  167. snmp.start_poll_thread(1000);
  168. cout << "press return to stop\n";
  169. getc(stdin);
  170. snmp.stop_poll_thread();
  171. Snmp::socket_cleanup(); // Shut down socket subsystem
  172. }