Les Forums

Les Forums

Les forums sont fermés. Ils restent présent pour consultation et archivage.
Vous pouvez désormais poser vos questions directement dans les commentaires en bas de chaque page du site.
Alors n'hésitez pas à participer

Création de trap SNMP V3 C++

Bonjour à tous 🙂 ,

Je réalise un programme en c++ capable d'envoyer des traps SNMP V3. J'utilise LoriotPro V5 pour les recevoir. Quand je ne met pas de sécurité ça marche bien mais quand je veux réaliser l'authentification dans mon programme me signale que le security name est inconnue.
Voici mon code:
[code:1:cfedba7448]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;
}[/code:1:cfedba7448]

Je ne trouve pas pourquoi? 😥
a priori je comparerai un paquet capturé via wireshark avec le paquet que tu attendais, histoire de vérifier que tout est ok 🙂