1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
| int _stdcall trapMib()
{
#define strcasecmp stricmp
#define COLDSTART "1.3.6.1.2.1.11"
#define PAYLOADID "1.3.6.1.2.1.11"
#define PAYLOAD "Trap Test"
#define ENTERPRISE "1.3.6.1.2.1.11"
int result=0;
Snmp::socket_startup(); // Initialize socket subsystem
//---------[ make a GenAddress and Oid object to retrieve ]---------------
UdpAddress address("192.168.0.11"); // make a SNMP++ Generic address
if ( !address.valid())
{ // check validity of address
result=-10;
}
Oid oid( COLDSTART); // default is ColdStart
if(!oid.valid())
{ // check validity of user oid
result=-20;
}
//---------[ determine options to use ]-----------------------------------
snmp_version version=version3; // default is v3
u_short port=162; // snmp port
OctetStr community("public"); // community name
Oid ent(ENTERPRISE); // default enterprise
#ifdef SNMPV3
OctetStr privPassword("traidis");
OctetStr authPassword("christophe");
OctetStr securityName("secureUser");
OctetStr userName("secureUser");
int securityModel = SecurityModel_USM;
//int securityLevel = SecurityLevel_noAuthNoPriv;
int securityLevel = SecurityLevel_authPriv;
OctetStr contextName("other");
OctetStr contextEngineID("secureUser");
// long authProtocol = SNMPv3_usmNoAuthProtocol;
// long privProtocol = SNMPv3_usmNoPrivProtocol;
long authProtocol = SNMP_AUTHPROTOCOL_HMACSHA;
long privProtocol = SNMP_PRIVPROTOCOL_AES256;
v3MP *v3_MP;
#endif
//----------[ create a SNMP++ session ]-----------------------------------
int status;
// bind to any port and use IPv6 if needed
Snmp snmp(status, 0, (address.get_ip_version() == Address::version_ipv6));
if(status != SNMP_CLASS_SUCCESS)
{
result=-30;
}
//---------[ init SnmpV3 ]--------------------------------------------
#ifdef SNMPV3
if(version == version3)
{
char *engineId = "secureUser";
char *filename = "snmpv3_boot_counter";
unsigned int snmpEngineBoots = 0;
int status;
status = getBootCounter(filename, engineId, snmpEngineBoots);
if ((status != SNMPv3_OK) && (status < SNMPv3_FILEOPEN_ERROR))
{
result = -40;
}
snmpEngineBoots++;
status = saveBootCounter(filename, engineId, snmpEngineBoots);
if (status != SNMPv3_OK)
{
result = -50;
}
int construct_status;
v3_MP = new v3MP(engineId, snmpEngineBoots, construct_status);
USM *usm = v3_MP->get_usm();
if(usm->add_usm_user(userName, securityName, authProtocol, privProtocol,
authPassword, privPassword)== SNMPv3_USM_ERROR)
{
result = -60;
}
}
else
{
// MUST create a dummy v3MP object if _SNMPv3 is enabled!
int construct_status;
v3_MP = new v3MP("dummy", 0, construct_status);
}
#endif
//--------[ build up SNMP++ object needed ]-------------------------------
Pdu pdu; // construct a Pdu object
Vb vb; // variable binding object to use
vb.set_oid(PAYLOADID); // example oid for trap payload
vb.set_value(PAYLOAD); // example string for payload
pdu += vb; // append the vb to the pdu
pdu.set_notify_id( oid); // set the id of the trap
pdu.set_notify_enterprise( ent); // set up the enterprise of the trap
address.set_port(port);
CTarget ctarget(address); // make a target using the address
#ifdef SNMPV3
UTarget utarget(address);
if (version == version3)
{
utarget.set_version( version); // set the SNMP version SNMPV1 or V2 or V3
utarget.set_security_model( securityModel);
utarget.set_security_name( securityName);
pdu.set_security_level( securityLevel);
pdu.set_context_name (contextName);
pdu.set_context_engine_id(contextEngineID);
}
else
{
#endif
ctarget.set_version( version); // set the SNMP version SNMPV1 or V2
ctarget.set_readcommunity( community); // set the read community name
#ifdef SNMPV3
}
#endif
//-------[ Send the trap ]------------------------------------------------
SnmpTarget *target;
#ifdef SNMPV3
if (version == version3)
target = &utarget;
else
#endif
target = &ctarget;
status = snmp.trap( pdu,*target);
if(status != SNMP_CLASS_SUCCESS)
{
result=-70;
MessageBox(NULL,snmp.error_msg(status),"Error: ", MB_OK);
}
Snmp::socket_cleanup(); // Shut down socket subsystem
return result;
} |
Partager