📄 snmp_process.c
字号:
#include "snmptype.h"
#include "snmpdef.h"
#include "snmpfunc.h"
#include "snmp_ol100cr.h"
SNMP_PKT_T gSnmpPkt;
_INT16 mib2_SystemScalar_Handler(_UINT8 request,void *ptr);
#if 0
_INT16 mib2_IfScalar_Handler(_UINT8 request,void *ptr);
_INT16 mib2_IfTable_Handler(_UINT8 request,void *ptr);
#endif
SNMP_OID_TABLE_T g_mib_table[]= {
{SNMP_SCALAR,{1,3,6,1,2,1,1}, 8 , 1 , 7 , mib2_SystemScalar_Handler},
{SNMP_SCALAR ,{1,3,6,1,4,1,3807,1,100,1} , 11, 1 ,10, ol100crLocDeviceInfo_Handler},
{SNMP_TABLE , {1,3,6,1,4,1,3807,1,100,2,1,1} , 13, 1,12,ol100crLocPortTableEntry_Handler},
{SNMP_TABLE , {1,3,6,1,4,1,3807,1,100,2,2,1} , 13, 1,7,ol100crLocPortInfoEntry_Handler},
{SNMP_TABLE , {1,3,6,1,4,1,3807,1,100,2,3,1} , 13, 1,31,ol100crLocPortStasticEntry_Handler},
#if 0
{SNMP_SCALAR,{1,3,6,1,2,1,2}, 8 , 1 , 1 , mib2_IfScalar_Handler},
{SNMP_TABLE, {1,3,6,1,2,1,2,2,1}, 10 , 1 , 22, mib2_IfTable_Handler},
#endif
{SNMP_TERMINATOR}
};
#if 1
_UINT32 Uint32Get(VB_T *vbp)
{
return (_UINT32)(vbp->value_u.v_counter);
}
_INT32 Int32Get(VB_T *vbp)
{
return (_INT32)(vbp->value_u.v_number);
}
_UINT8 * octetGet(VB_T *vbp)
{
return (_UINT8 *)(vbp->value_u.v_string);
}
_UINT32 * oidGet(VB_T *vbp)
{
return (_UINT32 *)(vbp->value_u.v_object.component_list);
}
void Uint32Set(VB_T *vbp,_UINT32 value)
{
vbp->vb_data_flags_n_type = VT_UNSIGNED32;
vbp->value_u.v_counter=value;
}
void GaugeSet(VB_T *vbp,_UINT32 value)
{
vbp->vb_data_flags_n_type = VT_GAUGE;
vbp->value_u.v_counter=value;
}
void CounterSet(VB_T *vbp,_UINT32 value)
{
vbp->vb_data_flags_n_type = VT_COUNTER;
vbp->value_u.v_counter=value;
}
void Counter64Set(VB_T *vbp,_UINT32 high,_UINT32 low)
{
vbp->vb_data_flags_n_type = VT_COUNTER64;
vbp->value_u.v_counter64.high=high;
vbp->value_u.v_counter64.low=low;
}
#endif
void Int32Set(VB_T *vbp,_INT32 value)
{
vbp->vb_data_flags_n_type = VT_NUMBER;
vbp->value_u.v_number=value;
}
void TimetickSet(VB_T *vbp,_UINT32 value)
{
vbp->vb_data_flags_n_type = VT_TIMETICKS;
vbp->value_u.v_counter=value;
}
void oidSet(VB_T *vbp,_UINT32 *pOid,_UINT16 length)
{
_UINT16 i;
vbp->vb_data_flags_n_type = VT_OBJECT;
for(i=0; i<length; i++)
vbp->value_u.v_object.component_list[i] = pOid[i];
vbp->value_u.v_object.num_components = length;
}
void octetSet(VB_T *vbp,_UINT8 *pValue,_INT16 length)
{
if(length>MAX_OCTET_LENGTH)
length=MAX_OCTET_LENGTH;
memset(vbp->value_u.v_string,0,sizeof(vbp->value_u.v_string));
memcpy(vbp->value_u.v_string,pValue,length);
vbp->vb_data_length=length;
vbp->vb_data_flags_n_type = VT_STRING;
}
_INT16 mib2_SystemScalar_Handler(_UINT8 request,void *ptr)
{
VB_T *vbp=(VB_T *)ptr;
_UINT32 valOid[]={1,3,6,1,4,1,3807,1,100};
switch(*(vbp->pLeafId))
{
default:
return -1;
break;
case 1:/*sysDescr*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
octetSet(vbp,"F-engine",strlen("F-engine"));
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 2:/*sysObjectId*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
oidSet(vbp,valOid,sizeof(valOid)/sizeof(_UINT32));
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 3:/*sysuptime*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
TimetickSet(vbp,0);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 4:/*sysContact*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
octetSet(vbp,"www.fhn.com.cn",strlen("www.fhn.com.cn"));
break;
case SET_REQUEST_PDU:
break;
}
break;
case 5:/*sysName*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
octetSet(vbp," ",strlen(""));
break;
case SET_REQUEST_PDU:
break;
}
break;
case 6:/*sysLocaltion*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
octetSet(vbp," ",strlen(" "));
break;
case SET_REQUEST_PDU:
break;
}
break;
case 7:/*sysServices*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,0);
break;
case SET_REQUEST_PDU:
break;
}
break;
}
return NO_ERROR;
}
#if 0
_INT32 mib2_IfScalar_Handler(_UINT8 request,void *ptr)
{
VB_T *vbp=(VB_T *)ptr;
switch(*(vbp->pLeafId))
{
default:
return GEN_ERR;
break;
case 1:/*sysServices*/
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,3);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
}
return NO_ERROR;
}
_INT32 mib2_IfTable_Handler(_UINT8 request,void *ptr)
{
VB_T *vbp=(VB_T *)ptr;
_UINT8 string[16];
_UINT32 valOid[]={1,3,6,1,2,1,2,2,1,1,0};
if(request==GET_NEXT_REQUEST_PDU)
{
if(vbp->vb_obj_id.num_components<=vbp->defined_prefix_len)
{
/*get first index*/
vbp->pIndex[0]=1;
}
else
{
/*get next index*/
if(vbp->pIndex[0]<3)
{
vbp->pIndex[0]++;
}
else
{
/*get fist index*/
(*(vbp->pLeafId))++;
vbp->pIndex[0]=1;
}
}
vbp->vb_obj_id.num_components=vbp->defined_prefix_len+vbp->defined_index_cnt;
}
switch(*(vbp->pLeafId))
{
default:
return GEN_ERR;
break;
case 1:/*ifIndex */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,vbp->pIndex[0]);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 2:/*ifDescr */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
sprintf(string,"ifDescr%u",(INT32)vbp->pIndex[0]);
octetSet(vbp,string,strlen(string));
break;
case SET_REQUEST_PDU:
break;
}
break;
case 3:/*ifType */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,6);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 4:/*ifMtu */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,1500);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 5:/*ifSpeed */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
GaugeSet(vbp,100);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 6:/*ifPhysAddress */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
memset(string,1,sizeof(string));
octetSet(vbp,string,6);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 7:/*ifAdminStatus */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,1);
break;
case SET_REQUEST_PDU:
break;
}
break;
case 8:/*ifOperStatus */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,1);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 9:/*ifLastChange */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
TimetickSet(vbp,0);
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 10:/*ifInOctets */
case 11:/*ifInUcastPkts */
case 12:/*ifInNUcastPkts */
case 13:/*ifInDiscards */
case 14:/*ifInErrors */
case 15:/*ifInUnknownProtos */
case 16:/*ifOutOctets */
case 17:/*ifOutUcastPkts */
case 18:/*ifOutNUcastPkts */
case 19:/*ifOutDiscards */
case 20:/*ifOutErrors */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
CounterSet(vbp,*(vbp->pLeafId));
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 21:/*ifOutQLen */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
Int32Set(vbp,*(vbp->pLeafId));
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
case 22:/*ifSpecific */
switch(request)
{
case GET_NEXT_REQUEST_PDU:
case GET_REQUEST_PDU:
valOid[sizeof(valOid)/sizeof(_UINT32)-1]=vbp->pIndex[0];
oidSet(vbp,valOid,sizeof(valOid)/sizeof(_UINT32));
break;
case SET_REQUEST_PDU:
return NOT_WRITABLE;
}
break;
}
return NO_ERROR;
}
#endif
void Send_SNMP_Error_Packet(SNMP_PKT_T * rp, _INT16 ecode, _INT16 eindex)
{
_UINT16 bufsize;
rp->pdu.error_status = ecode;
rp->pdu.error_index = ++eindex;
rp->pdu_type = GET_RESPONSE_PDU;
bufsize = SNMP_Bufsize_For_Packet(rp);
(void)snmpAgentOutput(rp,bufsize);
return;
}
void Send_SNMP_Packet(SNMP_PKT_T * rp)
{
_UINT16 bufsize;
if(rp->pdu.error_status != NO_ERROR)
{
/* if we had an error we send it */
Send_SNMP_Error_Packet(rp, rp->pdu.error_status,rp->pdu.error_index);
return;
}
bufsize = SNMP_Bufsize_For_Packet(rp);
rp->pdu_type = GET_RESPONSE_PDU;
(void)snmpAgentOutput(rp,bufsize);
return;
}
/*
Compare two object identifiers with different lengths.
* Returns -1, 0, or +1 depending whether o1 < o2, o1 == o2, or o1 > o2
*/
_INT16 oidcmp(_UINT16 n1, _UINT32 *idp1,_UINT16 n2,_UINT32 * idp2,_INT16 *matchLen)
{
(*matchLen)=0;
while((n1 > 0) && (n2 > 0))
{
if(*idp1 != *idp2)
return (*idp1 < *idp2) ? -1 : 1;
idp1++;
idp2++;
n1--;
n2--;
(*matchLen)++;
}
if(n1 == n2)
return 0; /* OIDs are the same */
else if(n1 > n2)
return 1;
else
return -1;
}
_INT16 process_packet_oid(_UINT8 pdu_type,VB_T *vbp)
{
_INT16 i=0;
_INT16 matchIndex=0;
_INT16 bestMatchLen=-1;
_INT16 matchCmp=0;
_INT16 err_code=NO_ERROR;
_INT16 cmp=0;
_INT16 matchLen=0;
/*find the best match*/
while(g_mib_table[i].type!=SNMP_TERMINATOR)
{
/*first find the best match tabale*/
cmp=oidcmp(vbp->vb_obj_id.num_components,
vbp->vb_obj_id.component_list,
g_mib_table[i].prefixLen-1,
g_mib_table[i].prefixSubOid,&matchLen);
if((cmp<=0) || (matchLen==(g_mib_table[i].prefixLen-1)))
{
/* find the match*/
if(matchLen>bestMatchLen)
{
matchIndex=i;
bestMatchLen=matchLen;
matchCmp=cmp;
}
else
{
break;
}
}
i++;
}
if(bestMatchLen<0)
{
vbp->vb_data_flags_n_type = VT_ENDOFMIB;
return err_code;
}
i=matchIndex;
cmp=matchCmp;
matchLen=bestMatchLen;
while(g_mib_table[i].type!=SNMP_TERMINATOR)
{
if(matchIndex!=i)
{
cmp=oidcmp(vbp->vb_obj_id.num_components,
vbp->vb_obj_id.component_list,
g_mib_table[i].prefixLen-1,
g_mib_table[i].prefixSubOid,&matchLen);
}
if((cmp<=0) || (matchLen==(g_mib_table[i].prefixLen-1)))
{
if(cmp<0)
{
vbp->vb_obj_id.num_components=g_mib_table[i].prefixLen-1;
(void)memcpy((_UINT8 *)vbp->vb_obj_id.component_list,(_UINT8 *)g_mib_table[i].prefixSubOid,sizeof(_UINT32)*g_mib_table[i].prefixLen);
}
vbp->defined_prefix_len = g_mib_table[i].prefixLen;
vbp->defined_index_cnt = g_mib_table[i].idxCnt;
vbp->pLeafId=(_UINT32 *)(&(vbp->vb_obj_id.component_list[vbp->defined_prefix_len-1]));
vbp->pIndex=(_UINT32 *)(&(vbp->vb_obj_id.component_list[vbp->defined_prefix_len]));
if(pdu_type==GET_NEXT_REQUEST_PDU)
{
if(g_mib_table[i].type==SNMP_SCALAR)
{
if(vbp->vb_obj_id.num_components<vbp->defined_prefix_len)
{
(*vbp->pLeafId)=1;
}
else if(vbp->vb_obj_id.num_components==vbp->defined_prefix_len+vbp->defined_index_cnt)
{
(*vbp->pLeafId)+=1;
}
vbp->pIndex[0]=0;
vbp->vb_obj_id.num_components=vbp->defined_prefix_len+vbp->defined_index_cnt;
}
else
{
if(vbp->vb_obj_id.num_components<vbp->defined_prefix_len)
{
(*vbp->pLeafId)=1;
vbp->vb_obj_id.num_components=vbp->defined_prefix_len;
}
/*
else if(vbp->vb_obj_id.num_components==vbp->defined_prefix_len+vbp->defined_index_cnt)
{
vbp->vb_obj_id.component_list[vbp->defined_prefix_len-1]++;
}
vbp->vb_obj_id.component_list[vbp->defined_prefix_len]=0;
*/
}
}
else
{
if(vbp->vb_obj_id.num_components!=(vbp->defined_prefix_len+vbp->defined_index_cnt))
{
err_code = NO_SUCH_NAME;
return err_code;
}
}
if((*vbp->pLeafId)>g_mib_table[i].leafNum)
{
i++;
continue;
}
vbp->process = g_mib_table[i].pObjHandler;
if(vbp->process)
err_code=(*vbp->process)(pdu_type,vbp);
if(err_code!=NO_ERROR)
{
if(pdu_type==GET_NEXT_REQUEST_PDU)
{
i++;
continue;
}
}
return err_code;
}
i++;
}
err_code=NO_ERROR;
vbp->vb_data_flags_n_type = VT_ENDOFMIB;
return err_code;
}
void Process_Rcvd_SNMP_Packet(_UINT8 *ebuffp,_UINT16 pktl,SNMPADDR_T *peeraddr,SNMPADDR_T *myaddr,_INT32 *sock_cooike)
{
_INT16 i=0;
SNMP_PKT_T *rp=(SNMP_PKT_T *)(&gSnmpPkt);
VB_T *vbp=0;
(void)memset((_UINT8 *)rp,0,sizeof(SNMP_PKT_T));
#if (PLATFORM==LINUX) ||(PLATFORM==UCLINUX) ||(PLATFORM==VXWORKS)
if(peeraddr!=0)
(void)memcpy((_UINT8 *)(&(rp->peerAddr)),(_UINT8 *)peeraddr,sizeof(SNMPADDR_T));
if(myaddr!=0)
(void)memcpy((_UINT8 *)(&(rp->myAddr)),(_UINT8 *)myaddr,sizeof(SNMPADDR_T));
if(sock_cooike!=0)
rp->sock_cooike = *sock_cooike;
#endif
if(SNMP_Decode_Packet_WER(ebuffp,pktl,rp)==NO_ERROR)
{
if(rp->pdu_type==GET_BULK_REQUEST_PDU)
rp->pdu_type=GET_NEXT_REQUEST_PDU;
for(i = 0; i < rp->pdu.vb_count; i++)
{
vbp=(VB_T *)(&(rp->pdu.vb_obj[i]));
rp->pdu.error_status=process_packet_oid(rp->pdu_type,vbp);
if(rp->pdu.error_status!=NO_ERROR)
{
rp->pdu.error_index=i;
break;
}
}
}
(void)Send_SNMP_Packet(rp);
return;
}
_UINT8 SNMP_Process_Finish(SNMP_PKT_T * rp,_UINT8 *ebuffp,_UINT16 need)
{
if(SNMP_Encode_Pkt_With_Siz(rp,ebuffp,need))
return 0;
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -