snmpmsg.cpp
来自「HP公司的SNMP++的Win32版本源码」· C++ 代码 · 共 606 行 · 第 1/2 页
CPP
606 行
raw_pdu->trap_type = 6; // enterprise specific
// last oid subid is the specific value
// if 2nd to last subid is "0", remove it
// enterprise is always the notify oid prefix
raw_pdu->specific_type = (int) trapid[(int) (trapid.len()-1)];
trapid.trim(1);
if ( trapid[(int)(trapid.len()-1)] == 0 )
trapid.trim(1);
enterprise = trapid;
}
if ( raw_pdu->trap_type !=6)
pdu.get_notify_enterprise( enterprise);
if ( enterprise.len() >0) {
// note!!
// these are hooks into an SNMP++ oid
// and therefor the raw_pdu enterprise
// should not free them. null them out!!
SmiLPOID rawOid;
rawOid = enterprise.oidval();
raw_pdu->enterprise = rawOid->ptr;
raw_pdu->enterprise_length = (int) rawOid->len;
}
// timestamp
TimeTicks timestamp;
pdu.get_notify_timestamp( timestamp);
raw_pdu->time = ( unsigned long) timestamp;
}
// if its a v2 trap then we need to make a few adjustments
// vb #1 is the timestamp
// vb #2 is the id, represented as an Oid
if ( raw_pdu->command == sNMP_PDU_TRAP)
{
Pdu temppdu;
Vb tempvb;
// vb #1 is the timestamp
TimeTicks timestamp;
tempvb.set_oid("1.3.6.1.2.1.1.3.0"); // sysuptime
pdu.get_notify_timestamp( timestamp);
tempvb.set_value ( timestamp);
temppdu += tempvb;
// vb #2 is the id
Oid trapid;
tempvb.set_oid("1.3.6.1.6.3.1.1.5");
pdu.get_notify_id( trapid);
tempvb.set_value( trapid);
temppdu += tempvb;
// append the remaining vbs
for ( int z=0;z<pdu.get_vb_count();z++) {
pdu.get_vb( tempvb,z);
temppdu += tempvb;
}
pdu = temppdu; // reassign the pdu to the temp one
}
// load up the payload
// for all Vbs in list, add them to the pdu
int vb_count;
Vb tempvb;
Oid tempoid;
SmiLPOID smioid;
SmiVALUE smival;
vb_count = pdu.get_vb_count();
for (int z=0;z<vb_count;z++) {
pdu.get_vb( tempvb,z);
tempvb.get_oid( tempoid);
smioid = tempoid.oidval();
// clear the value portion, in case its
// not already been done so by the app
// writer
// only do it in the case its a get,next or bulk
if ((raw_pdu->command == sNMP_PDU_GET) ||
(raw_pdu->command == sNMP_PDU_GETNEXT) ||
(raw_pdu->command == sNMP_PDU_GETBULK))
tempvb.set_null();
status = convertVbToSmival( tempvb,
&smival );
if ( status != SNMP_CLASS_SUCCESS)
return status;
// add the vb to the raw pdu
snmp_add_var( raw_pdu,
smioid->ptr,
(int) smioid->len,
&smival);
freeSmivalDescriptor( &smival);
}
// ASN1 encode the pdu
status = snmp_build( raw_pdu,
databuff,
(int *) &bufflen,
version,
community.data(),
(int) community.len());
if ( status != 0) {
valid_flag = FALSE;
return SNMP_ERROR_WRONG_ENCODING;
}
valid_flag = TRUE;
// prevention of SNMP++ Enterprise Oid death
if ( enterprise.len() >0) {
raw_pdu->enterprise = 0;
raw_pdu->enterprise_length=0;
}
snmp_free_pdu( raw_pdu);
return SNMP_CLASS_SUCCESS;
};
// load up a SnmpMessage
int SnmpMessage::load( unsigned char *data,
unsigned long len)
{
bufflen = SNMP_MSG_LENGTH;
valid_flag = FALSE;
if ( len <= SNMP_MSG_LENGTH) {
memcpy( (unsigned char *) databuff,
(unsigned char *) data,
(unsigned int) len);
bufflen = len;
valid_flag = TRUE;
}
else
return SNMP_ERROR_WRONG_LENGTH;
return SNMP_CLASS_SUCCESS;
};
// unload the data into SNMP++ objects
int SnmpMessage::unload( Pdu &pdu, // Pdu object
OctetStr &community, // community object
snmp_version &version) // SNMP version #
{
unsigned char community_name[255];
unsigned long community_len;
Pdu tmppdu;
pdu = tmppdu;
if ( !valid_flag)
return SNMP_CLASS_INVALID;
snmp_pdu *raw_pdu;
raw_pdu = snmp_pdu_create(0);
int status = snmp_parse( raw_pdu,
databuff,
community_name,
community_len,
version,
(int) bufflen);
if ( status != 0)
return SNMP_CLASS_INTERNAL_ERROR;
// load up the SNMP++ variables
community.set_data( community_name,
community_len);
set_request_id( &pdu,raw_pdu->reqid);
set_error_status( &pdu,(int) raw_pdu->errstat);
set_error_index( &pdu,(int) raw_pdu->errindex);
pdu.set_type( raw_pdu->command);
// deal with trap parameters
TimeTicks timestamp;
timestamp = raw_pdu->time;
pdu.set_notify_timestamp( timestamp);
// vbs
Vb tempvb;
Oid tempoid;
struct variable_list *vp;
for(vp = raw_pdu->variables; vp; vp = vp->next_variable) {
// extract the oid portion
tempoid.set_data( (unsigned long *)vp->name,
( unsigned int) vp->name_length);
tempvb.set_oid( tempoid);
// extract the value portion
switch(vp->type){
// octet string
case sNMP_SYNTAX_OCTETS:
case sNMP_SYNTAX_OPAQUE:
{
OctetStr octets( (unsigned char *) vp->val.string,
(unsigned long) vp->val_len);
tempvb.set_value( octets);
}
break;
// object id
case sNMP_SYNTAX_OID:
{
Oid oid( (unsigned long*) vp->val.objid,
(int) vp->val_len);
tempvb.set_value( oid);
}
break;
// timeticks
case sNMP_SYNTAX_TIMETICKS:
{
TimeTicks timeticks( (unsigned long) *(vp->val.integer));
tempvb.set_value( timeticks);
}
break;
// 32 bit counter
case sNMP_SYNTAX_CNTR32:
{
Counter32 counter32( (unsigned long) *(vp->val.integer));
tempvb.set_value( counter32);
}
break;
// 32 bit gauge
case sNMP_SYNTAX_GAUGE32:
{
Gauge32 gauge32( (unsigned long) *(vp->val.integer));
tempvb.set_value( gauge32);
}
break;
// ip address
case sNMP_SYNTAX_IPADDR:
{
char buffer[20];
sprintf( buffer,"%d.%d.%d.%d",
vp->val.string[0],
vp->val.string[1],
vp->val.string[2],
vp->val.string[3]);
IpAddress ipaddress( buffer);
tempvb.set_value( ipaddress);
}
break;
// 32 bit integer
case sNMP_SYNTAX_INT:
{
SnmpInt32 int32( (long) *(vp->val.integer));
tempvb.set_value( int32);
}
break;
// 32 bit unsigned integer
case sNMP_SYNTAX_UINT32:
{
SnmpUInt32 uint32( (unsigned long) *(vp->val.integer));
tempvb.set_value( uint32);
}
break;
// v2 counter 64's
case sNMP_SYNTAX_CNTR64:
break;
case sNMP_SYNTAX_NULL:
tempvb.set_null();
break;
// v2 vb exceptions
case sNMP_SYNTAX_NOSUCHOBJECT:
case sNMP_SYNTAX_NOSUCHINSTANCE:
case sNMP_SYNTAX_ENDOFMIBVIEW:
set_exception_status( &tempvb, vp->type);
break;
default:
tempvb.set_null();
} // end switch
// append the vb to the pdu
pdu += tempvb;
}
snmp_free_pdu( raw_pdu);
return SNMP_CLASS_SUCCESS;
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?