snmp.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 1,767 行 · 第 1/5 页
CPP
1,767 行
0); // callback data for async only
};
//------------------------[ get next async ]-----------------------------
int Snmp::get_next( Pdu &pdu, // pdu to use
SnmpTarget &target, // destination target
const snmp_callback callback, // callback to use
const void *callback_data) // callback data
{
if ( ! callback )
return SNMP_CLASS_INVALID_CALLBACK;
pdu.set_type( sNMP_PDU_GETNEXT);
return snmp_engine( pdu, // pdu to get next
0, // max repeaters
0, // non repeaters
target, // target
callback, // callback
callback_data); // callback data
};
//-------------------------[ set ]---------------------------------------
int Snmp::set( Pdu &pdu, // pdu to use
SnmpTarget &target) // target address
{
pdu.set_type( sNMP_PDU_SET);
return snmp_engine( pdu, // pdu to set
0, // max repeaters
0, // non repeaters
target, // target
NULL, // callback for async only
0); // callback data
};
//------------------------[ set async ]----------------------------------
int Snmp::set( Pdu &pdu, // pdu to use
SnmpTarget &target, // destination target
const snmp_callback callback, // callback to use
const void * callback_data) // callback data
{
if ( ! callback )
return SNMP_CLASS_INVALID_CALLBACK;
pdu.set_type( sNMP_PDU_SET);
return snmp_engine( pdu, // pdu to set
0, // max repeaters
0, // non repeaters
target, // target
callback, // callback function
callback_data); // callback data
};
//-----------------------[ get bulk ]------------------------------------
int Snmp::get_bulk( Pdu &pdu, // pdu to use
SnmpTarget &target, // destination target
const int non_repeaters, // number of non repeaters
const int max_reps) // maximum number of repetitions
{
pdu.set_type( sNMP_PDU_GETBULK);
return snmp_engine( pdu, // pdu to use
non_repeaters, // # of non repeaters
max_reps, // max repititions
target, // target
NULL, // callback for async only
0); // callback data
};
//-----------------------[ get bulk async ]------------------------------
int Snmp::get_bulk( Pdu &pdu, // pdu to use
SnmpTarget &target, // destination target
const int non_repeaters, // number of non repeaters
const int max_reps, // maximum number of repetitions
const snmp_callback callback, // callback to use
const void *callback_data) // callback data
{
if ( ! callback )
return SNMP_CLASS_INVALID_CALLBACK;
pdu.set_type( sNMP_PDU_GETBULK);
return snmp_engine( pdu, // pdu to use
non_repeaters, // # of non repeaters
max_reps, // max repititions
target, // target
callback, // callback function
callback_data); // callback data
};
//----------------------[ blocking inform, V2 only]------------------------
int Snmp::inform( Pdu &pdu, // pdu to send
SnmpTarget &target) // destination target
{
pdu.set_type( sNMP_PDU_INFORM);
return snmp_engine( pdu, // get pdu
0, // max repeaters
0, // non repeaters
target, // target
NULL, // callback for async only
0); // callback data
};
//----------------------[ asynch inform, V2 only]------------------------
int Snmp::inform( Pdu &pdu, // pdu to send
SnmpTarget &target, // destination target
const snmp_callback callback, // callback function
const void * callback_data) // callback data
{
if ( ! callback )
return SNMP_CLASS_INVALID_CALLBACK;
pdu.set_type( sNMP_PDU_INFORM_ASYNC);
return snmp_engine( pdu, // pdu to set
0, // max repeaters
0, // non repeaters
target, // target
callback, // callback function
callback_data); // callback data
};
//---------------------[ send a trap ]------------------------------------------
int Snmp::trap( Pdu &pdu, // pdu to send
SnmpTarget &target) // destination target
{
int status;
long int request_id; // unique request id
SNMPDEBUG("++ SNMP++, Send a Trap\n");
//---------[ make sure pdu is valid ]--------------------------
if ( !pdu.valid())
return SNMP_CLASS_INVALID_PDU;
pdu.set_type( sNMP_PDU_TRAP);
SNMP_WAIT;
//-----[ get a request id to use ]-------------------------
request_id = pdu_container->get_request_id();
SNMP_SIGNAL;
//-----[ send it out ]----------------------------------------------
status = sendMsg(target, pdu,0,0,request_id);
// verify the send status
if (status != SNMP_CLASS_SUCCESS) {
SNMPDEBUG("-- SNMP++, Failure Sending Trap Pdu\n");
return status; // fail on send message
}
SNMP_WAIT;
pdu_container->traps_sent();
SNMP_SIGNAL;
return SNMP_CLASS_SUCCESS;
};
//------------[ convert SNMP++ VB to WinSNMP smiVALUE ]----------------
int convertVbToSmival( Vb &tempvb, smiVALUE *smival )
{
smival->syntax = tempvb.get_syntax();
switch ( smival->syntax ) {
// case sNMP_SYNTAX_INT32:
case sNMP_SYNTAX_INT:
tempvb.get_value(smival->value.sNumber);
break;
// case sNMP_SYNTAX_UINT32:
case sNMP_SYNTAX_GAUGE32:
case sNMP_SYNTAX_CNTR32:
case sNMP_SYNTAX_TIMETICKS:
tempvb.get_value(smival->value.uNumber);
break;
// case Counter64
case sNMP_SYNTAX_CNTR64:
{
Counter64 c64;
tempvb.get_value(c64);
smival->value.hNumber.hipart = c64.high();
smival->value.hNumber.lopart = c64.low();
}
break;
case sNMP_SYNTAX_BITS:
case sNMP_SYNTAX_OCTETS:
case sNMP_SYNTAX_IPADDR:
{
OctetStr os;
tempvb.get_value(os);
smival->value.string.ptr = NULL;
smival->value.string.len = os.len();
if ( smival->value.string.len > 0 )
{
smival->value.string.ptr = (SmiLPBYTE) new unsigned char [smival->value.string.len];
if ( smival->value.string.ptr )
{
for (int i=0; i<(int) smival->value.string.len ; i++)
smival->value.string.ptr[i] = os[i];
}
else
{
smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival
return SNMP_CLASS_RESOURCE_UNAVAIL;
}
}
}
break;
case sNMP_SYNTAX_OID:
{
Oid oid;
tempvb.get_value(oid);
smival->value.oid.ptr = NULL;
smival->value.oid.len = oid.len();
if ( smival->value.oid.len > 0 )
{
smival->value.oid.ptr = (SmiLPUINT32) new unsigned long [ smival->value.oid.len];
if ( smival->value.oid.ptr )
{
for (int i=0; i<(int)smival->value.oid.len ; i++)
smival->value.oid.ptr[i] = oid[i];
}
else
{
smival->syntax = sNMP_SYNTAX_NULL; // invalidate the smival
return SNMP_CLASS_RESOURCE_UNAVAIL;
}
}
}
break;
default:
return SNMP_CLASS_INTERNAL_ERROR;
}
return SNMP_CLASS_SUCCESS;
}
//------------[ free WinSNMP smiVALUE descriptors ]---------------------
void freeSmivalDescriptor( smiVALUE *smival )
{
switch ( smival->syntax ) {
case SNMP_SYNTAX_OCTETS:
case SNMP_SYNTAX_OPAQUE:
case SNMP_SYNTAX_IPADDR:
case SNMP_SYNTAX_NSAPADDR: // obsoleted in SNMPv2 Draft Std
case SNMP_SYNTAX_BITS: // obsoleted in SNMPv2 Draft Std
delete [] smival->value.string.ptr;
break;
case SNMP_SYNTAX_OID:
delete [] smival->value.oid.ptr;
break;
}
smival->syntax = SNMP_SYNTAX_NULL;
}
//----------------------[ load_varbindlist ]----------------------------
// load the WinSNMP varbind list with the Snmp++ Vb objects
int load_varbindlist(HSNMP_SESSION session_handle,
HSNMP_VBL hvbl, // handle to WinSNMP vbl
Pdu &pdu) // pdu to use
{
int z; // looping variable
Oid current_oid; // current oid object
smiOID smioid; // winsnmp oid variable
smiVALUE smival; // winsnmp smi value
SNMPAPI_STATUS Wstatus; // return status for snmp calls
Vb tempvb; // temp vb to use
int vb_count; // count of vbs in pdu
unsigned short action; // type of pdu
int rc; // snmp++ result code
TimeTicks timestamp; // timestamp for traps
SNMPDEBUG("++ SNMP++, In load var bind list \n");
vb_count = pdu.get_vb_count();
action = pdu.get_type();
//------------------------------------------------
// if its a trap then build up trap timestamp and id
if ( action == sNMP_PDU_TRAP) {
// timestamp
tempvb.set_oid( (Oid) "1.3.6.1.2.1.1.3.0");
pdu.get_notify_timestamp( timestamp);
if ( timestamp <= 0)
timestamp = ( long) GetTickCount();
tempvb.set_value( timestamp);
Wstatus = SnmpStrToOid( tempvb.get_printable_oid(),&smioid);
if ( Wstatus == SNMPAPI_FAILURE)
{
Wstatus = SnmpGetLastError(NULL);
if (SNMPAPI_ALLOC_ERROR == Wstatus)
return SNMP_CLASS_RESOURCE_UNAVAIL;
else
if (SNMPAPI_OID_INVALID == Wstatus)
return SNMP_CLASS_INVALID_OID;
else
return SNMP_CLASS_INTERNAL_ERROR;
}
rc = convertVbToSmival(tempvb, &smival);
if ( rc != SNMP_CLASS_SUCCESS )
{
SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE) &smioid);
return rc;
}
Wstatus = SnmpSetVb( hvbl,0,&smioid,&smival);
if ( Wstatus == SNMPAPI_FAILURE)
{
Wstatus = SnmpGetLastError(NULL);
SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE) &smioid);
freeSmivalDescriptor( &smival);
if (SNMPAPI_ALLOC_ERROR == Wstatus)
return SNMP_CLASS_RESOURCE_UNAVAIL;
else
return SNMP_CLASS_INTERNAL_ERROR;
}
freeSmivalDescriptor(&smival);
// free the smioid descriptor
SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE) &smioid);
// trap id
tempvb.set_oid( (Oid) "1.3.6.1.6.3.1.1.4.1.0");
pdu.get_notify_id( current_oid);
tempvb.set_value( current_oid);
Wstatus = SnmpStrToOid( tempvb.get_printable_oid(),&smioid);
if ( Wstatus == SNMPAPI_FAILURE)
{
Wstatus = SnmpGetLastError(NULL);
if (SNMPAPI_ALLOC_ERROR == Wstatus)
return SNMP_CLASS_RESOURCE_UNAVAIL;
else
if (SNMPAPI_OID_INVALID == Wstatus)
return SNMP_CLASS_INVALID_OID;
else
return SNMP_CLASS_INTERNAL_ERROR;
}
rc = convertVbToSmival(tempvb, &smival);
if ( rc != SNMP_CLASS_SUCCESS )
{
SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE) &smioid);
return rc;
}
Wstatus = SnmpSetVb( hvbl,0,&smioid,&smival);
if ( Wstatus == SNMPAPI_FAILURE)
{
Wstatus = SnmpGetLastError(NULL);
SnmpFreeDescriptor(SNMP_SYNTAX_OID, (smiLPOPAQUE) &smioid);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?