www.pudn.com > SNMP范例源代码.zip > Ne2000.c
#include//Global variable extern xdata union netcard rxdnet; extern xdata union netcard txdnet; extern xdata union ethernet_address_type my_ethernet_address; //本机的以太网地址 extern xdata union ethernet_address_type gateway_ethernet_address; //网关的以太网地址 extern xdata union ethernet_address_type ping_ethernet_address; //用来ping的以太网地址 extern xdata union ip_address_type my_ip_address; //本机的ip地址 extern xdata union ip_address_type gateway_ip_address; //网关的ip地址 extern xdata union ip_address_type mask_ip_address; //子网掩码 extern xdata union ip_address_type SNMP_ip_address; //网管SNMP主机的ip地址 extern xdata union ip_address_type ping_ip_address; //用于ping命令 extern xdata union ip_address_type temp_ip_address; //临时变量 /*following globals comming from agent.c*/ u32 snmpAuthEnable = AGENT_AUTH_DISABLE; /* enable/disable snmp authentication */ /* space allocated for snmp hosts/communities for authentication purposes */ struct snmpComm_t authComms[AUTH_MAX_COMMS]; /* space for all communities allowed */ struct snmpHost_t authHosts[AUTH_MAX_HOSTS]; /* space for all hosts allowed */ /* space allocated for snmp hosts/communities for traps */ struct snmpComm_t trapComms[TRAP_MAX_COMMS]; /* space for all communities allowed */ struct snmpHost_t trapHosts[TRAP_MAX_HOSTS]; /* space for all hosts allowed */ u32 snmpAuthMaxComms = AUTH_MAX_COMMS; /* max communities for auth group */ u32 snmpAuthMaxHosts = AUTH_MAX_HOSTS; /* max hosts for auth group */ u32 snmpTrapMaxComms = TRAP_MAX_COMMS; /* max communities for trap group */ u32 snmpTrapMaxHosts = TRAP_MAX_HOSTS; /* max hosts for trap group */ /************************************************** SNMP proc parameters **************************************************/ xdata system_t system; xdata txfxDevice_t txfxDevice; xdata txfxModule_t modules[MAX_MOUDLE_NUM]; // insert here SNMP modules defines /* defines */ #define SNMP_IPA 0 /* Ipaddress, APPLICATION (0) */ #define SNMP_CNT 1 /* Counter, APPLICATION (1) */ #define SNMP_GGE 2 /* Gauge, APPLICATION (2) */ #define SNMP_TIT 3 /* TimeTicks APPLICATION (3) */ #define SNMP_OPQ 4 /* Opaque, APPLICATION (4) */ /* typedefs */ typedef struct snmp_cnv_s snmp_cnv_t; struct snmp_cnv_s { u32 Class; u32 Tag; i32 Syntax; }; /* globals */ i32 snmpErrStatus = SNMP_NOERROR; i32 snmpErrIndex = 0; asn1_sck_t snmpErrAsn1Status; asn1_sck_t snmpErrAsn1Index; asn1_sck_t snmpErrAsn1Resp; snmp_stat_t SnmpStat; const i8 *SnmpTrap[] = { "cold start", "warm start", "link down", "link up", "authentication failure", "neighbor loss", "enterprise specific" }; /* locals */ static snmp_cnv_t SnmpCnv[] = { {ASN1_UNI, ASN1_NUL, SNMP_NULL}, {ASN1_UNI, ASN1_INT, SNMP_INTEGER}, {ASN1_UNI, ASN1_OTS, SNMP_OCTETSTR}, {ASN1_UNI, ASN1_OTS, SNMP_DISPLAYSTR}, {ASN1_UNI, ASN1_OJI, SNMP_OBJECTID}, {ASN1_APL, SNMP_IPA, SNMP_IPADDR}, {ASN1_APL, SNMP_CNT, SNMP_COUNTER}, {ASN1_APL, SNMP_GGE, SNMP_GAUGE}, {ASN1_APL, SNMP_TIT, SNMP_TIMETICKS}, {ASN1_APL, SNMP_OPQ, SNMP_OPAQUE}, {0, 0, -1} }; /* globals */ extern snmp_stat_t SnmpStat; #define XSNMP_TOTAL_OBJECTS 100 xdata i32 Table[XSNMP_TOTAL_OBJECTS]; static idata i32 Mib_Count; static xdata mib_object_t MibRoot[] = { {{1,3,6,1,2,1,1,1},8, LEAF_sysDescr,SNMP_OCTETSTR,MIB_READ|MIB_WRITE},/*sysDescr*/ {{1,3,6,1,2,1,1,2},8, LEAF_sysObjectID,SNMP_OBJECTID,MIB_READ},/*sysObjectID*/ {{1,3,6,1,2,1,1,3},8, LEAF_sysUpTime,SNMP_TIMETICKS,MIB_READ},/*sysUpTime*/ {{1,3,6,1,2,1,1,4},8, LEAF_sysContact,SNMP_OCTETSTR,MIB_READ|MIB_WRITE},/*sysContact*/ {{1,3,6,1,2,1,1,5},8, LEAF_sysName,SNMP_OCTETSTR,MIB_READ|MIB_WRITE},/*sysName*/ {{1,3,6,1,2,1,1,6},8, LEAF_sysLocation,SNMP_OCTETSTR,MIB_READ|MIB_WRITE},/*sysLocation*/ {{1,3,6,1,2,1,1,7},8, LEAF_sysServices,SNMP_INTEGER,MIB_READ},/*sysServices*/ {{1,3,6,1,2,1,1,8},8, LEAF_sysORLastChange,SNMP_TIMETICKS,MIB_READ},/*sysORLastChange*/ /* Following belongs to snmp group*/ {{1,3,6,1,2,1,11,1},8, LEAF_snmpInPkts,SNMP_OCTETSTR,MIB_READ},/*snmpInPkts*/ {{1,3,6,1,2,1,11,3},8, LEAF_snmpInBadVersions,SNMP_COUNTER,MIB_READ},/*snmpInBadVersions*/ {{1,3,6,1,2,1,11,4},8, LEAF_snmpInBadCommunityNames,SNMP_COUNTER,MIB_READ},/*snmpInBadCommunityNames*/ {{1,3,6,1,2,1,11,5},8, LEAF_snmpInBadCommunityUses,SNMP_COUNTER,MIB_READ},/*snmpInBadCommunityUses*/ {{1,3,6,1,2,1,11,6},8, LEAF_snmpInASNParseErrs,SNMP_COUNTER,MIB_READ},/*snmpInASNParseErrs*/ {{1,3,6,1,2,1,11,6},8, LEAF_snmpEnableAuthenTraps,SNMP_INTEGER,MIB_READ},/*snmpEnableAuthenTraps*/ /* Following from privite tree*/ {{1,3,6,1,4,1,26,5,1,1},10, LEAF_txfxDevSoftwareVersion,SNMP_INTEGER,MIB_READ},/*txfxDevSoftwareVersion*/ {{1,3,6,1,4,1,26,5,1,2},10, LEAF_txfxDeviceType,SNMP_INTEGER,MIB_READ},/*txfxDeviceType*/ {{1,3,6,1,4,1,26,5,1,3},10, LEAF_txfxDeviceHardwareVersion,SNMP_INTEGER,MIB_READ},/*txfxDeviceHardwareVersion*/ {{1,3,6,1,4,1,26,5,1,4},10, LEAF_txfxDeviceModuleNum,SNMP_INTEGER,MIB_READ},/*txfxDeviceModuleNum*/ /* the following is a tablur vars*/ {{1,3,6,1,4,1,26,5,1,5,1},11, LEAF_txfxModuleIndex,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleIndex*/ {{1,3,6,1,4,1,26,5,1,5,2},11, LEAF_txfxModuleType,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleType*/ {{1,3,6,1,4,1,26,5,1,5,3},11, LEAF_txfxModuleTimeElapsed,SNMP_TIMETICKS,MIB_READ},/*LEAF_txfxModuleTimeElapsed*/ {{1,3,6,1,4,1,26,5,1,5,4},11, LEAF_txfxModuleAdminStatus,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleAdminStatus*/ {{1,3,6,1,4,1,26,5,1,5,5},11, LEAF_txfxModuleHardwareVer,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleHardwareVer*/ {{1,3,6,1,4,1,26,5,1,5,6},11, LEAF_txfxModuleSlot,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleSlot*/ {{1,3,6,1,4,1,26,5,1,5,7},11, LEAF_txfxModuleOpertionalStatus,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleOpertionalStatus*/ {{1,3,6,1,4,1,26,5,1,5,8},11, LEAF_txfxModuleOnOff,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleOnOff*/ {{1,3,6,1,4,1,26,5,1,5,9},11, LEAF_txfxModuleCapacity,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleCapacity*/ {{1,3,6,1,4,1,26,5,1,5,10},11, LEAF_txfxModuleLinkAlt,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleLinkAlt*/ {{1,3,6,1,4,1,26,5,1,5,11},11, LEAF_txfxModuleTxMode,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleTxMode*/ {{1,3,6,1,4,1,26,5,1,5,12},11, LEAF_txfxModuleState,SNMP_INTEGER,MIB_READ},/*LEAF_txfxModuleState*/ /* Following from Trap group*/ {{1,3,6,1,6,3,1,1,4,1},10, LEAF_snmpTrapOID,SNMP_OBJECTID,MIB_READ},/*snmpTrapOID*/ {{1,3,6,1,6,3,1,1,4,3},10, LEAF_snmpTrapEnterprise,SNMP_OBJECTID,MIB_READ},/*snmpTrapEnterprise*/ /* Following from Traps group*/ {{1,3,6,1,6,3,1,1,5,1},10, 0,SNMP_NULL,MIB_READ},/*coldStart*/ {{1,3,6,1,6,3,1,1,5,2},10, 0,SNMP_NULL,MIB_READ},/*warmStart*/ {{1,3,6,1,6,3,1,1,5,3},10, 0,SNMP_NULL,MIB_READ},/*linkDown*/ {{1,3,6,1,6,3,1,1,5,4},10, 0,SNMP_NULL,MIB_READ},/*linkUp*/ {{1,3,6,1,6,3,1,1,5,5},10, 0,SNMP_NULL,MIB_READ},/*authenticationFailure*/ {{1,3,6,1,6,3,1,1,5,6},10, 0,SNMP_NULL,MIB_READ}/*egpNeighorLoss*/ }; extern xdata system_t system; extern xdata txfxDevice_t txfxDevice; extern xdata txfxModule_t modules[16]; xdata u8 oldSysDescr[SNMP_SIZE_BUFCHR]; xdata u8 oldSysContact[SNMP_SIZE_BUFCHR]; xdata u8 oldSysName[SNMP_SIZE_BUFCHR]; xdata u8 oldSysLocation[SNMP_SIZE_BUFCHR]; /* forward declarations */ LOCAL i32 MibCmpObjId(ul32 * ObjId, u16 ObjIdLen1, ul32 * ObjId2, u16 ObjIdLen2); LOCAL i32 MibObjectFind(ul32 * Id, i32 IdLen, i32 * cmp); LOCAL u16 setupIdbCall(u32 object, snmp_object_t * Obj, u16 IdLen, u32 type); LOCAL u16 Request(snmp_object_t * Obj, i32 * lastindex); LOCAL u16 idbDispatch(idb_t* idbBuf); /* forward declarations */ static int SnmpSyn2TagCls(u32 * Tag, u32 * Cls, i32 Syn); static int SnmpTagCls2Syn(u32 Tag, u32 Cls, u16 * Syn); static int SnmpObjEnc(asn1_sck_t * Asn1, snmp_object_t * Obj); static int SnmpObjDec(asn1_sck_t * Asn1, snmp_object_t * Obj); static int SnmpLstEnc(asn1_sck_t * Asn1, snmp_object_t * Lst, u32 LstLen); static int SnmpLstDec(asn1_sck_t * Asn1, snmp_object_t * Lst, u32 LstSze, u32 * LstLen); static int SnmpRqsEnc(asn1_sck_t * Asn1, snmp_pdu_t * Rqs); static int SnmpRqsDec(asn1_sck_t * Asn1, snmp_pdu_t * Rqs); static int SnmpPduEnc(asn1_sck_t * Asn1, snmp_pdu_t * Pdu, snmp_object_t * Lst, u32 LstLen); static int SnmpPduDec(asn1_sck_t * Asn1, snmp_pdu_t * Pdu, snmp_object_t * Lst, u32 LstSze, u32 * LstLen); // all above from SNMP.c file //static int MsgRequest(u8 * rqsBuffer, u32 rqsLen,u8 ** rspBuffer,u32 * rspLen,ul32 addr); //void udp_request(unsigned char* pdu, unsigned int len, unsigned long ip_address,unsigned int portnumber); // insert here ASN.1 modules (asn1.c/* typedefs */ /* globals */ int asn1ErrStatus = ASN1_ERR_NOERROR; /* locals */ /******************************************************************************* * * MibRequest - Calls Request(arg1, arg2) to process SNMP request * * DESCRIPTION * MibRequest is called by Request(5 args) in agent.c and other trap * routines to process an SNMP request. * * This routine calls SET/COMMIT/UNDO in that order depending on * the success/failure of the SET/COMMIT. * * MibRequest allows for different requests per varbind list * * RETURNS * * NOMANUAL */ u16 MibRequest ( u32 listLen, snmp_object_t * list, u16 * errindex ) { u16 status, mindex, errstatus; LOCAL snmp_object_t MibRequestbackup[AGENT_LIST_SIZE]; LOCAL i32 MibRequestlastindex[AGENT_LIST_SIZE]; for (mindex = 0; mindex < AGENT_LIST_SIZE; mindex++) MibRequestlastindex[mindex] = 0; for (mindex = 0, status = SNMP_NOERROR; mindex < listLen && status == SNMP_NOERROR; mindex++) { if (list[mindex].Request != SNMP_PDU_SET) { MibRequestbackup[mindex] = list[mindex]; } else { /* prevent backup later on */ MibRequestbackup[mindex].Request = SNMP_PDU_SET; } status = Request(list + mindex, MibRequestlastindex+mindex); } errstatus = status; *errindex = mindex; /* * if it's a set, then go through list a second time and actually * do the set, if there were no errors the first time */ if (errstatus == SNMP_NOERROR) { for (mindex = 0, status = SNMP_NOERROR; mindex < listLen && status == SNMP_NOERROR; mindex++) { if (list[mindex].Request == SNMP_PDU_SET) { list[mindex].Request = SNMP_PDU_COMMIT; status = Request(list + mindex, MibRequestlastindex + mindex); } } errstatus = status; *errindex = mindex; } if (errstatus != SNMP_NOERROR) { /* * count the errors by types for reporting snmp group errors */ switch (errstatus) { case SNMP_NOSUCHNAME: SnmpStat.OutNoSuchNames++; break; case SNMP_BADVALUE: SnmpStat.OutBadValues++; break; case SNMP_READONLY: break; case SNMP_GENERROR: SnmpStat.OutGenErrs++; break; default: break; } for (mindex = 0, status = SNMP_NOERROR; mindex < *errindex && status == SNMP_NOERROR; mindex++) { /* * if a set was attempted, this can undo those sets when a set * fails */ if (list[mindex].Request == SNMP_PDU_SET || list[mindex].Request == SNMP_PDU_COMMIT) { list[mindex].Request = SNMP_PDU_UNDO; status = Request(list + mindex, MibRequestlastindex + mindex); } else { list[mindex] = MibRequestbackup[mindex]; } } } return errstatus; } /******************************************************************************* * * Request - Calls setupIdbCall() if it finds MIB object in local MIB tree * * DESCRIPTION * Request calls setupIdbCall() to communicate with IDB to process the * SNMP request for a particualr MIB object. This routine maps the SNMP * request into an IDB request. * * Request is called by MibRequest in mib.c * * RETURNS * * NOMANUAL */ LOCAL u16 Request ( snmp_object_t * Obj, i32 * lastindex ) { i32 mindex, cmp; u16 error; u16 i; if (MibRoot == NULL) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 1"); #endif return SNMP_GENERROR; } error = SNMP_NOSUCHNAME; cmp = MibCmpObjId(MibRoot[Table[*lastindex]].Id, MibRoot[Table[*lastindex]].IdLen, Obj->Id, (u16) Obj->IdLen); if (cmp == -2 && Obj->Request == SNMP_PDU_NEXT && ++(*lastindex) < Mib_Count) { cmp = MibCmpObjId(MibRoot[Table[*lastindex]].Id, MibRoot[Table[*lastindex]].IdLen, Obj->Id, (u16) Obj->IdLen); } if (cmp == -1) { mindex = *lastindex; } else { mindex = *lastindex = MibObjectFind(Obj->Id, (i32) Obj->IdLen, &cmp); } switch (Obj->Request) { case SNMP_PDU_GET: if (cmp!=-1|| MibRoot[Table[mindex]].Rqs == 0) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 2"); PrintStr("\r\n Obj->id ="); for(i=0; i IdLen; i++) { PrintLong(Obj->Id[i]);PrintChar(' ');} PrintStr("\r\n mindex ="); PrintLong(mindex); #endif return error; } if (!(MibRoot[Table[mindex]].Support & MIB_READ)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 3"); #endif return error; } error = setupIdbCall(MibRoot[Table[mindex]].Rqs, Obj, MibRoot[Table[mindex]].IdLen, MibRoot[Table[mindex]].Type); if (error == SNMP_NOERROR) { Obj->Type = MibRoot[Table[mindex]].Type; } #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 4"); #endif return error; case SNMP_PDU_SET: if (cmp != -1 || MibRoot[Table[mindex]].Rqs == 0) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 5"); #endif return error; } if (!(MibRoot[Table[mindex]].Support & MIB_WRITE)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 6"); #endif return SNMP_READONLY; } if (Obj->Type == SNMP_OBJECTID && Obj->SyntaxLen > SNMP_SIZE_BUFINT) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 7"); #endif return SNMP_BADVALUE; } if ((Obj->Type == SNMP_OCTETSTR || Obj->Type == SNMP_DISPLAYSTR || Obj->Type == SNMP_OPAQUE) && (Obj->SyntaxLen > SNMP_SIZE_BUFCHR)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 8"); #endif return SNMP_BADVALUE; } error = setupIdbCall(MibRoot[Table[mindex]].Rqs, Obj, MibRoot[Table[mindex]].IdLen, MibRoot[Table[mindex]].Type); return error; case SNMP_PDU_NEXT: switch (cmp) { case -2: return error; case -1: case 0: if (MibRoot[Table[mindex]].Support & MIB_READ && MibRoot[Table[mindex]].Rqs != 0) { error = setupIdbCall(MibRoot[Table[mindex]].Rqs, Obj, MibRoot[Table[mindex]].IdLen, MibRoot[Table[mindex]].Type); if (error == SNMP_NOERROR) { Obj->Type = MibRoot[Table[mindex]].Type; break; } } mindex++; break; default: break; } while (error == SNMP_NOSUCHNAME && mindex < (sizeof(MibRoot)/sizeof(mib_object_t))) { memcpy(Obj->Id, MibRoot[Table[mindex]].Id, MibRoot[Table[mindex]].IdLen * sizeof(l32)); Obj->IdLen = MibRoot[Table[mindex]].IdLen; if (MibRoot[Table[mindex]].Support & MIB_READ && MibRoot[Table[mindex]].Rqs != 0) { error = setupIdbCall(MibRoot[Table[mindex]].Rqs, Obj, MibRoot[Table[mindex]].IdLen, MibRoot[Table[mindex]].Type); if (error == SNMP_NOERROR) { Obj->Type = MibRoot[Table[mindex]].Type; break; } } mindex++; } return error; case SNMP_PDU_COMMIT: case SNMP_PDU_UNDO: if (cmp != -1 || MibRoot[Table[mindex]].Rqs == 0) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 9"); #endif return error; } error = setupIdbCall(MibRoot[Table[mindex]].Rqs, Obj, MibRoot[Table[mindex]].IdLen, MibRoot[Table[mindex]].Type); #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 10"); #endif return error; default: break; } #ifdef SNMP_DEBUG PrintStr("\r\n leaving mib request module 11"); #endif return SNMP_GENERROR; } /******************************************************************************* * * MibCmpObjId - Used to compare if 2 OIDs match * * DESCRIPTION * * RETURNS * * NOMANUAL */ i32 MibCmpObjId ( ul32 * ObjId1, u16 ObjIdLen1, ul32 * ObjId2, u16 ObjIdLen2 ) { while (ObjIdLen1 > 0 && ObjIdLen2 > 0 && *ObjId1 == *ObjId2) { ObjIdLen1--; ObjIdLen2--; ObjId1++; ObjId2++; } if (!ObjIdLen1 && !ObjIdLen2) { return 0; } if (!ObjIdLen1) { return -1; } if (!ObjIdLen2) { return 1; } if (*ObjId1 < *ObjId2) { return -2; } else { return 2; } } /******************************************************************************* * * MibObjectFind - Used to find OIDs in internal MIB tree * * DESCRIPTION * * RETURNS * * NOMANUAL */ i32 MibObjectFind ( ul32 * Id, i32 IdLen, i32 * cmp ) { i32 first, last, mindex; first = 0; last = Mib_Count - 1; *cmp = 2; if (last < 0) { return 0; } while (first < last) { mindex = (first + last) / 2; *cmp = MibCmpObjId(MibRoot[Table[mindex]].Id, MibRoot[Table[mindex]].IdLen, Id, (u16) IdLen); switch (*cmp) { case -2: first = mindex + 1; break; case -1: case 0: first = last = mindex; return mindex; case 1: case 2: last = mindex; break; default: break; } } mindex = first; *cmp = MibCmpObjId(MibRoot[Table[mindex]].Id, MibRoot[Table[mindex]].IdLen, Id, (u16) IdLen); return mindex; } /******************************************************************************* * * MibObjectInit - Used to init MIB objects into agent's internal MIB tree * * DESCRIPTION * * RETURNS * * NOMANUAL */ void MibObjectInit() { i32 mindex, cmp, i,j; Mib_Count =0; if (Mib_Count >= XSNMP_TOTAL_OBJECTS) { return; } for(i=0;i< sizeof(MibRoot)/sizeof(mib_object_t); i++) { Table[Mib_Count++]=i; mindex = MibObjectFind(MibRoot[i].Id, MibRoot[i].IdLen,&cmp); if (cmp == -1 && mindex != Mib_Count - 1) { Mib_Count--; continue; } for (j = Mib_Count - 1; j > mindex; j--) Table[j] = Table[j - 1]; Table[mindex] = i; } return; } /******************************************************************************* * * setupIdbCall - Interface routine to communicate with IDB * * DESCRIPTION * This routine is the interface routine between SNMP and IDB. This routine sets * up the idb_t structure and calls idbDispatch() to get to the relevant module. * Upon return from IDB, this routine gets the needed information from the * idb_t structure and uses it for responding back to the SNMP request. * * RETURNS * * NOMANUAL */ u16 setupIdbCall ( u32 object, snmp_object_t * Obj, u16 IdLen, u32 type ) { idb_t idbBuf; u32 i; u32 indexBuf[32]; u16 retval; if ((Obj->Type != SNMP_NULL) && (Obj->Type != type) && (Obj->Request == SNMP_PDU_SET)) return SNMP_BADVALUE; idbBuf.object = object; //指向内部定义的对象类型。 idbBuf.request = Obj->Request; idbBuf.indexLength = Obj->IdLen - IdLen; if ((idbBuf.request == SNMP_PDU_GET) || (idbBuf.request == SNMP_PDU_NEXT)) Obj->SyntaxLen = 0; for (i = 0; i < idbBuf.indexLength; i++) { indexBuf[i] = Obj->Id[IdLen + i]; } idbBuf.index = &(indexBuf[0]); //保存表结构中的子项。 switch (type) { case SNMP_NULL: case SNMP_COUNTER: case SNMP_GAUGE: case SNMP_TIMETICKS: idbBuf.value.LngUns = Obj->Syntax.LngUns; idbBuf.valueLength = 4; break; case SNMP_INTEGER: idbBuf.value.LngInt = Obj->Syntax.LngInt; idbBuf.valueLength = 4; break; case SNMP_OBJECTID: memcpy(idbBuf.value.BufChr, Obj->Syntax.BufChr,sizeof(Obj->Syntax.BufChr)); //idbBuf.value.BufChr = &(Obj->Syntax.BufChr[0]); idbBuf.valueLength = 128; break; case SNMP_IPADDR: memcpy(idbBuf.value.BufChr, Obj->Syntax.BufChr,sizeof(Obj->Syntax.BufChr)); // idbBuf.value.BufChr = &(Obj->Syntax.BufChr[0]); idbBuf.valueLength = 256; break; case SNMP_OPAQUE: case SNMP_OCTETSTR: memcpy(idbBuf.value.BufChr, Obj->Syntax.BufChr,sizeof(Obj->Syntax.BufChr)); //idbBuf.value.BufChr = &(Obj->Syntax.BufChr[0]); if (((idbBuf.request == SNMP_PDU_SET) || (idbBuf.request == SNMP_PDU_UNDO) || (idbBuf.request == SNMP_PDU_COMMIT)) && (Obj->SyntaxLen >= 0)) idbBuf.valueLength = Obj->SyntaxLen; else idbBuf.valueLength = 256; break; default: return SNMP_GENERROR; } if (Obj->SyntaxLen > 0) idbBuf.valueLength = Obj->SyntaxLen; retval = idbDispatch(&idbBuf); // 调用本地数据访问接口。 switch (type) { case SNMP_NULL: case SNMP_COUNTER: case SNMP_GAUGE: case SNMP_TIMETICKS: Obj->Syntax.LngUns = idbBuf.value.LngUns; Obj->SyntaxLen =idbBuf.valueLength; break; case SNMP_INTEGER: Obj->Syntax.LngInt = idbBuf.value.LngInt; Obj->SyntaxLen =idbBuf.valueLength; break; case SNMP_OBJECTID: case SNMP_IPADDR: Obj->SyntaxLen = idbBuf.valueLength; memcpy(Obj->Syntax.BufChr, idbBuf.value.BufChr,idbBuf.valueLength); break; case SNMP_OPAQUE: case SNMP_OCTETSTR: Obj->SyntaxLen = idbBuf.valueLength; memcpy(Obj->Syntax.BufChr, idbBuf.value.BufChr,idbBuf.valueLength); break; default: return SNMP_GENERROR; } Obj->IdLen += (idbBuf.indexLength - (Obj->IdLen - IdLen)); for (i = 0; i < idbBuf.indexLength; i++) { Obj->Id[IdLen + i] = indexBuf[i]; } switch (type) { case SNMP_NULL: case SNMP_COUNTER: case SNMP_GAUGE: case SNMP_TIMETICKS: Obj->Syntax.LngUns = idbBuf.value.LngUns; break; case SNMP_INTEGER: Obj->Syntax.LngInt = idbBuf.value.LngInt; break; case SNMP_IPADDR: case SNMP_OCTETSTR: case SNMP_OBJECTID: case SNMP_OPAQUE: break; default: return SNMP_GENERROR; } if ((Obj->Type != type) && (Obj->Request == SNMP_PDU_GET)) Obj->Type = type; return (idbBuf.snmpReturnCode); } u16 idbDispatch(idb_t* idbBuf) { u16 error; u32 object; u16 index; u16 i; object = idbBuf->object; if(idbBuf->indexLength>1) { if(idbBuf->indexLength!=3) return SNMP_NOSUCHNAME; index = idbBuf->index[0]; //在MibRoot中查找该数据的子项。获得新的object参数。 } if(idbBuf->request == SNMP_PDU_GET||SNMP_PDU_NEXT) { switch(object) { // system group member. case LEAF_sysDescr: /* memcpy(idbBuf->value.BufChr, system.sysDescr, sizeof(system.sysDescr));*/ for(i=0;i<4;i++)/*sizeof(system.sysDescr)*/ { idbBuf->value.BufChr[i] = 'a';/*system.sysDescr[i];*/ if(system.sysDescr[i]=='\0') break; } idbBuf->valueLength =4;/*i*/ idbBuf->snmpReturnCode = SNMP_NOERROR; #ifdef SNMP_DEBUG PrintStr("\r\n run in sysDescr section"); #endif return SNMP_NOERROR; case LEAF_sysObjectID: for(i=0;i value.BufInt[i] = system.sysObjectID[i]; if(system.sysObjectID[i]==0) break; } idbBuf->valueLength= i; idbBuf->snmpReturnCode = SNMP_NOERROR; #ifdef SNMP_DEBUG PrintStr("\r\n run in sysObjectId section"); #endif return SNMP_NOERROR; case LEAF_sysUpTime: idbBuf->value.LngUns = system.sysUpTime; idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; case LEAF_sysContact: for(i=0;i<4;i++)/*sizeof(system.sysContact)*/ { idbBuf->value.BufChr[i] ='b'; /* system.sysContact[i];*/ if(system.sysContact[i]=='\0') break; } idbBuf->valueLength =i; idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; case LEAF_sysName: for(i=0;i value.BufChr[i] = system.sysName[i]; if(system.sysName[i]=='\0') break; } idbBuf->valueLength =i; idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; case LEAF_sysLocation: for(i=0;i value.BufChr[i] = system.sysLocation[i]; if(system.sysLocation[i]=='\0') break; } idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; case LEAF_sysServices: idbBuf->value.LngUns = system.sysServices; idbBuf->valueLength = 4; idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; case LEAF_sysORLastChange: memcpy(idbBuf->value.BufChr,system.sysContact, sizeof(system.sysContact)); idbBuf->valueLength = sizeof(system.sysContact); idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; // snmp group member. case LEAF_snmpInPkts: idbBuf->value.LngInt = SnmpStat.InPkts; break; case LEAF_snmpOutPkts: idbBuf->value.LngInt = SnmpStat.OutPkts; break; case LEAF_snmpInBadVersions: idbBuf->value.LngInt = SnmpStat.InBadVersions; break; case LEAF_snmpInBadCommunityNames: idbBuf->value.LngInt = 0; break; case LEAF_snmpInBadCommunityUses: idbBuf->value.LngInt = SnmpStat.snmpInBadCommunityUses; break; case LEAF_snmpInASNParseErrs: idbBuf->value.LngInt = SnmpStat.InASNParseErrs; break; case LEAF_snmpInTooBigs: idbBuf->value.LngInt = SnmpStat.InTooBigs; break; case LEAF_snmpInNoSuchNames: idbBuf->value.LngInt = SnmpStat.InNoSuchNames; break; case LEAF_snmpInBadValues: idbBuf->value.LngInt = SnmpStat.InBadValues; break; case LEAF_snmpInReadOnlys: idbBuf->value.LngInt = SnmpStat.InReadOnlys; break; case LEAF_snmpInGenErrs: idbBuf->value.LngInt = SnmpStat.InGenErrs; break; case LEAF_snmpInTotalReqVars: idbBuf->value.LngInt = 0; break; case LEAF_snmpInTotalSetVars: idbBuf->value.LngInt = 0; break; case LEAF_snmpInGetRequests: idbBuf->value.LngInt = SnmpStat.InGetRequests; break; case LEAF_snmpInGetNexts: idbBuf->value.LngInt = SnmpStat.InGetNexts; break; case LEAF_snmpInSetRequests: idbBuf->value.LngInt = SnmpStat.InSetRequests; break; case LEAF_snmpInGetResponses: idbBuf->value.LngInt = SnmpStat.InGetResponses; break; case LEAF_snmpInTraps: idbBuf->value.LngInt = SnmpStat.InTraps; break; case LEAF_snmpOutTooBigs: idbBuf->value.LngInt = SnmpStat.OutTooBigs; break; case LEAF_snmpOutNoSuchNames: idbBuf->value.LngInt = SnmpStat.OutNoSuchNames; break; case LEAF_snmpOutBadValues: idbBuf->value.LngInt = SnmpStat.OutBadValues; break; case LEAF_snmpOutGenErrs: idbBuf->value.LngInt = SnmpStat.OutGenErrs; break; case LEAF_snmpOutGetRequests: idbBuf->value.LngInt = SnmpStat.OutGetRequests; break; case LEAF_snmpOutGetNexts: idbBuf->value.LngInt = SnmpStat.OutGetNexts; break; case LEAF_snmpOutSetRequests: idbBuf->value.LngInt = SnmpStat.OutSetRequests; break; case LEAF_snmpOutGetResponses: idbBuf->value.LngInt = SnmpStat.OutGetResponses; break; case LEAF_snmpOutTraps: idbBuf->value.LngInt = SnmpStat.OutTraps; break; case LEAF_snmpEnableAuthenTraps: idbBuf->value.LngInt = SnmpStat.snmpEnableAuthenTraps; break; case LEAF_snmpSilentDrops: idbBuf->value.LngInt = SnmpStat.snmpSilentDrops; break; case LEAF_snmpProxyDrops: idbBuf->value.LngInt = SnmpStat.snmpProxyDrops; break; // txfxMib Memembers case LEAF_txfxDevSoftwareVersion: idbBuf->value.LngUns = txfxDevice.txfxDevSoftwareVersion; break; case LEAF_txfxDeviceType: idbBuf-> value.LngUns = txfxDevice.txfxDeviceType; break; case LEAF_txfxDeviceHardwareVersion: idbBuf-> value.LngUns = txfxDevice.txfxDeviceHardwareVersion; break; case LEAF_txfxDeviceModuleNum: idbBuf-> value.LngUns = txfxDevice.txfxDeviceModuleNum; break; // entry to the txfxModuleTable case LEAF_txfxModuleType: idbBuf->value.LngUns = (l32)modules[index].txfxModuleType; idbBuf->valueLength = 4; idbBuf->snmpReturnCode = SNMP_NOERROR; break; case LEAF_txfxModuleTimeElapsed: case LEAF_txfxModuleAdminStatus: case LEAF_txfxModuleHardwareVer: case LEAF_txfxModuleSlot: case LEAF_txfxModuleOpertionalStatus: case LEAF_txfxModuleOnOff: case LEAF_txfxModuleCapacity: case LEAF_txfxModuleLinkAlt: case LEAF_txfxModuleTxMode: case LEAF_txfxModuleState: default: return SNMP_NOSUCHNAME; } idbBuf->snmpReturnCode = SNMP_NOERROR; return SNMP_NOERROR; } else if(idbBuf->request == SNMP_PDU_SET) { switch(object) { // system group member. case LEAF_sysDescr: strcpy(oldSysDescr, system.sysDescr); memcpy(system.sysDescr, idbBuf->value.BufChr, idbBuf->valueLength); return SNMP_NOERROR; case LEAF_sysContact: strcpy(oldSysContact, system.sysContact); memcpy(system.sysContact, idbBuf->value.BufChr,idbBuf->valueLength); return SNMP_NOERROR; case LEAF_sysName: strcpy(oldSysName, system.sysName); memcpy(system.sysName, idbBuf->value.BufChr,idbBuf->valueLength); return SNMP_NOERROR; case LEAF_sysLocation: strcpy(oldSysLocation, system.sysLocation); memcpy(system.sysLocation, idbBuf->value.BufChr,idbBuf->valueLength); return SNMP_NOERROR; break; default: return SNMP_READONLY; } } else if(idbBuf->request==SNMP_PDU_COMMIT||SNMP_PDU_COMMIT) { switch(object) { // system group member. case LEAF_sysDescr: if(idbBuf->request==SNMP_PDU_UNDO) { strcpy(system.sysDescr,oldSysDescr); memcpy(idbBuf->value.BufChr, system.sysDescr, sizeof(system.sysLocation)); idbBuf->valueLength = sizeof(system.sysDescr); } break; case LEAF_sysContact: if(idbBuf->request==SNMP_PDU_UNDO) { strcpy(system.sysContact,oldSysContact); memcpy(idbBuf->value.BufChr, system.sysContact, sizeof(system.sysLocation)); idbBuf->valueLength = sizeof(system.sysContact); } break; case LEAF_sysName: if(idbBuf->request==SNMP_PDU_UNDO) { strcpy(system.sysName,oldSysName); memcpy(idbBuf->value.BufChr, system.sysName, sizeof(system.sysName)); idbBuf->valueLength = sizeof(system.sysName); } break; case LEAF_sysLocation: if(idbBuf->request==SNMP_PDU_UNDO) { strcpy(system.sysLocation,oldSysLocation); memcpy(idbBuf->value.BufChr, system.sysLocation, sizeof(system.sysLocation)); idbBuf->valueLength = sizeof(system.sysLocation); } break; default: return SNMP_READONLY; } } return error; } /* forward declarations */ void Asn1Opn ( asn1_sck_t * Asn1, u8 * Buf, u32 Len, u32 Mde ) { Asn1->Begin = Buf; Asn1->End = Buf + Len; Asn1->Pointer = (Mde == ASN1_ENC) ? Buf + Len : Buf; #ifdef SNMP_DEBUG PrintStr("\r\nASN1 process begin:"); PrintStr("Len=");PrintLong(Len); #endif } void Asn1Cls ( asn1_sck_t * Asn1, u8 ** Buf, u32 * Len ) { *Buf = Asn1->Pointer; *Len = (u32) (Asn1->End - Asn1->Pointer); #ifdef SNMP_DEBUG PrintStr("\r\nASN1 process end"); #endif } int Asn1OctEnc( asn1_sck_t * Asn1, u8 Chr) { if (Asn1->Pointer <= Asn1->Begin) { #ifdef SNMP_DEBUG PrintStr("\r\nleaving ASN1OctEnc with error ASN1_ERR_ENC_FULL !"); PrintByte(Chr); PrintChar(' '); PrintChar(Chr); #endif asn1ErrStatus = ASN1_ERR_ENC_FULL; return FALSE; } *--(Asn1->Pointer) = Chr; /* #ifdef SNMP_DEBUG PrintStr("\r\nASN1 process OctEnc:"); PrintByte(Chr); #endif */ return TRUE; } int Asn1OctDec ( asn1_sck_t * Asn1, u8 * Chr ) { if (Asn1->Pointer >= Asn1->End) { asn1ErrStatus = ASN1_ERR_DEC_EMPTY; return FALSE; } *Chr = *(Asn1->Pointer)++; return TRUE; } int Asn1TagEnc ( asn1_sck_t * Asn1, u32 Tag ) { u8 Chr; Chr = (u8) (Tag & 0x7F); Tag >>= 7; if (!Asn1OctEnc(Asn1, Chr)) return FALSE; while (Tag > 0) { Chr = (u8) (Tag | 0x80); Tag >>= 7; if (!Asn1OctEnc(Asn1, Chr)) return FALSE; } return TRUE; } int Asn1TagDec ( asn1_sck_t * Asn1, u32 * Tag ) { u8 Chr; *Tag = 0; do { if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Tag <<= 7; *Tag |= Chr & 0x7F; } while ((Chr & 0x80) == 0x80); return TRUE; } int Asn1IdrEnc ( asn1_sck_t * Asn1, u32 Cls, u32 Con, u32 Tag ) { u8 Chr; if (Tag >= 0x1F) { if (!Asn1TagEnc(Asn1, Tag)) return FALSE; Tag = 0x1F; } Chr = (u8) ((Cls << 6) | (Con << 5) | (Tag)); if (!Asn1OctEnc(Asn1, Chr)) return FALSE; return TRUE; } int Asn1IdrDec ( asn1_sck_t * Asn1, u32 * Cls, u32 * Con, u32 * Tag ) { u8 Chr; u16 temp; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; temp =Chr; temp = 0x00FF& temp; *Cls = (temp & 0x00C0) >> 6; *Con = (Chr & 0x20) >> 5; *Tag = (Chr & 0x1F); if (*Tag == 0x1F) { if (!Asn1TagDec(Asn1, Tag)) return FALSE; } return TRUE; } int Asn1LenEnc ( asn1_sck_t * Asn1, u32 Def, u32 Len ) { u8 Chr, Cnt; if (!Def) { Chr = 0x80; } else { if (Len < 0x80) { Chr = (u8) Len; } else { Cnt = 0; while (Len > 0) { Chr = (u8) Len; Len >>= 8; if (!Asn1OctEnc(Asn1, Chr)) return FALSE; Cnt++; } Chr = (u8) (Cnt | 0x80); } } if (!Asn1OctEnc(Asn1, Chr)) return FALSE; return TRUE; } int Asn1LenDec ( asn1_sck_t * Asn1, u32 * Def, u32 * Len ) { u8 Chr, Cnt; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; if (Chr == 0x80) { *Def = 0; } else { *Def = 1; if (Chr < 0x80) { *Len = Chr; } else { Cnt = (u8) (Chr & 0x7F); *Len = 0; while (Cnt > 0) { if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Len <<= 8; *Len |= Chr; Cnt--; } } } return TRUE; } int Asn1HdrEnc ( asn1_sck_t * Asn1, u8 * Eoc, u32 Cls, u32 Con, u32 Tag ) { u32 Def, Len; if (Eoc == 0) { Def = 0; Len = 0; } else { Def = 1; Len = (u32) (Eoc - Asn1->Pointer); } if (!Asn1LenEnc(Asn1, Def, Len)) return FALSE; if (!Asn1IdrEnc(Asn1, Cls, Con, Tag)) return FALSE; return TRUE; } int Asn1HdrDec ( asn1_sck_t * Asn1, u8 ** Eoc, u32 * Cls, u32 * Con, u32 * Tag ) { u32 Def, Len; if (!Asn1IdrDec(Asn1, Cls, Con, Tag)) { return FALSE; } if (!Asn1LenDec(Asn1, &Def, &Len)) { return FALSE; } if (Def) *Eoc = Asn1->Pointer + Len; else *Eoc = 0; return TRUE; } int Asn1Eoc ( asn1_sck_t * Asn1, u8 * Eoc ) { if (Eoc == 0) return (Asn1->Pointer[0] == 0x00 && Asn1->Pointer[1] == 0x00); else return (Asn1->Pointer >= Eoc); } int Asn1EocEnc ( asn1_sck_t * Asn1, u8 ** Eoc ) { if (Eoc == 0) { if (!Asn1OctEnc(Asn1, 0x00)) return FALSE; if (!Asn1OctEnc(Asn1, 0x00)) return FALSE; return TRUE; } else { *Eoc = Asn1->Pointer; return TRUE; } } int Asn1EocDec ( asn1_sck_t * Asn1, u8 * Eoc ) { u8 Chr; if (Eoc == 0) { if (!Asn1OctDec(Asn1, &Chr)) return FALSE; if (Chr != 0x00) { asn1ErrStatus = ASN1_ERR_DEC_EOC_MISMATCH; return FALSE; } if (!Asn1OctDec(Asn1, &Chr)) return FALSE; if (Chr != 0x00) { asn1ErrStatus = ASN1_ERR_DEC_EOC_MISMATCH; return FALSE; } return TRUE; } else { if (Asn1->Pointer != Eoc) { asn1ErrStatus = ASN1_ERR_DEC_LENGTH_MISMATCH; return FALSE; } return TRUE; } } int Asn1NulEnc ( asn1_sck_t * Asn1, u8 ** Eoc ) { *Eoc = Asn1->Pointer; return TRUE; } int Asn1NulDec ( asn1_sck_t * Asn1, u8 * Eoc ) { Asn1->Pointer = Eoc; return TRUE; } int Asn1IntEnc ( asn1_sck_t * Asn1, u8 ** Eoc, i32 Int ) { u8 Chr, Sgn; i32 Lim; *Eoc = Asn1->Pointer; if (Int < 0) { Lim = -1; Sgn = 0x80; } else { Lim = 0; Sgn = 0x00; } do { Chr = (u8) Int; Int >>= 8; /*lint !e702 */ if (!Asn1OctEnc(Asn1, Chr)) return FALSE; } while ((Int != Lim) || (u8) (Chr & 0x80) != Sgn); return TRUE; } int Asn1IntDec ( asn1_sck_t * Asn1, u8 * Eoc, i32 * Int ) { u8 Chr; u32 Len; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int = (i32) Chr; Len = 1; while (Asn1->Pointer < Eoc) { if (++Len > sizeof(i32)) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int <<= 8; /*lint !e701 */ *Int |= Chr; } return TRUE; } int Asn1IntEncLng ( asn1_sck_t * Asn1, u8 ** Eoc, i32 Int ) { u8 Chr, Sgn; i32 Lim; *Eoc = Asn1->Pointer; #if(0) if (Int < 0) { Lim = -1; Sgn = 0x80; } else { Lim = 0; Sgn = 0x00; } if (Int == -1) { Lim = 0; Sgn = 0x80; } else #endif if (Int < 0) { Lim = 0; /* Was -1 */ Sgn = 0x80; } else { Lim = 0; Sgn = 0x00; } do { Chr = (u8) Int; Int = (i32) ((u32) Int >> 8); if (!Asn1OctEnc(Asn1, Chr)) { return FALSE; } } while ((Int != Lim) || (u8) (Chr & 0x80) != Sgn); return TRUE; } int Asn1IntDecLng ( asn1_sck_t * Asn1, u8 * Eoc, i32 * Int ) { u8 Chr; u32 Len; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int = (i8) Chr; Len = 1; while (Asn1->Pointer < Eoc) { if (++Len > sizeof(u32)) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int <<= 8; /*lint !e701 */ *Int |= Chr; } return TRUE; } int Asn1IntEncUns ( asn1_sck_t * Asn1, u8 ** Eoc, u32 Int ) { u8 Chr; /*#ifdef SNMP_DEBUG PrintStr("\r\n Entering Asn1IntEncUns"); PrintStr("\r\nInt ="); PrintLong(Int); #endif */ *Eoc = Asn1->Pointer; do { Chr = (u8) Int; Int >>= 8; if (!Asn1OctEnc(Asn1, Chr)) { #ifdef SNMP_DEBUG PrintStr("\r\n Asn1IntEncUns: leaving with Error\r\nChr ="); PrintByte(Chr); #endif return FALSE; } } while ((Int != 0) || (Chr & 0x80) != 0x00); return TRUE; } int Asn1IntDecUns ( asn1_sck_t * Asn1, u8 * Eoc, u32 * Int ) { u8 Chr; u32 Len; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int = Chr; if (Chr == 0) Len = 0; else Len = 1; while (Asn1->Pointer < Eoc) { if (++Len > sizeof(i32)) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int <<= 8; *Int |= Chr; } return TRUE; } int Asn1IntEncLngUns ( asn1_sck_t * Asn1, u8 ** Eoc, u32 Int ) { u8 Chr; *Eoc = Asn1->Pointer; do { Chr = (u8) Int; Int >>= 8; if (!Asn1OctEnc(Asn1, Chr)) return FALSE; } while ((Int != 0) || (Chr & 0x80) != 0x00); return TRUE; } int Asn1IntDecLngUns ( asn1_sck_t * Asn1, u8 * Eoc, u32 * Int ) { u8 Chr; u32 Len; if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int = Chr; if (Chr == 0) Len = 0; else Len = 1; while (Asn1->Pointer < Eoc) { if (++Len > sizeof(u32)) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Int <<= 8; *Int |= Chr; } return TRUE; } int Asn1OtsEnc ( asn1_sck_t * Asn1, u8 ** Eoc, u8 * Ots, u32 OtsLen ) { /* #ifdef SNMP_DEBUG PrintStr("\r\nEntering Asn1OtsEnc module"); #endif*/ *Eoc = Asn1->Pointer; Ots += OtsLen; while (OtsLen-- > 0) { if (!Asn1OctEnc(Asn1, *--Ots)){ return FALSE; } } return TRUE; } int Asn1OtsDec ( asn1_sck_t * Asn1, u8 * Eoc, u8 * Ots, u32 OtsSze, u32 * OtsLen ) { *OtsLen = 0; while (Asn1->Pointer < Eoc) { if (++(*OtsLen) > OtsSze) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1OctDec(Asn1, (u8 *) Ots++)) return FALSE; } return TRUE; } int Asn1SbiEnc ( asn1_sck_t * Asn1, u32 Sbi ) { u8 Chr; Chr = (u8) (Sbi & 0x7F); Sbi >>= 7; /* #ifdef SNMP_DEBUG PrintStr("\r\n entering Asn1SbiEnc module"); PrintStr("\r\n Sbi ="); PrintLong(Sbi); PrintStr("\r\n Chr ="); PrintByte(Chr); #endif */ if (!Asn1OctEnc(Asn1, Chr)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving Asn1SbiEnc module 1"); #endif return FALSE; } while (Sbi > 0) { Chr = (u8) (Sbi | 0x80); Sbi >>= 7; PrintStr("\r\n Chr ="); PrintByte(Chr); if (!Asn1OctEnc(Asn1, Chr)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving Asn1SbiEnc module 2"); #endif return FALSE; } } return TRUE; } int Asn1SbiDec ( asn1_sck_t * Asn1, u32 * Sbi ) { u8 Chr; *Sbi = 0; do { if (!Asn1OctDec(Asn1, &Chr)) return FALSE; *Sbi <<= 7; *Sbi |= Chr & 0x7F; } while ((Chr & 0x80) == 0x80); return TRUE; } int Asn1OjiEnc ( asn1_sck_t * Asn1, u8 ** Eoc, u32 * Oji, u32 OjiLen ) { u32 Sbi; u16 i; *Eoc = Asn1->Pointer; if (OjiLen < 2) { asn1ErrStatus = ASN1_ERR_ENC_BADVALUE; #ifdef SNMP_DEBUG PrintStr("\r\n leaving Asn1OjiEnc module 1"); #endif return FALSE; } Sbi = Oji[1] + Oji[0] * 40; /* #ifdef SNMP_DEBUG PrintStr("\r\nOjiLen ="); PrintLong(OjiLen); PrintStr ("\r\nOji ="); for(i=0; i Pointer < Eoc) { if (++(*OjiLen) > OjiSze) { asn1ErrStatus = ASN1_ERR_DEC_BADVALUE; return FALSE; } if (!Asn1SbiDec(Asn1, Oji++)) return FALSE; } return TRUE; } // insert here SNMP modules in order to find the faults int SnmpSyn2TagCls ( u32 * Tag, u32 * Cls, i32 Syn ) { snmp_cnv_t *Cnv; Cnv = SnmpCnv; while (Cnv->Syntax != -1) { if (Cnv->Syntax == Syn) { *Tag = Cnv->Tag; *Cls = Cnv->Class; return TRUE; } Cnv++; } snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; return FALSE; } int SnmpTagCls2Syn ( u32 Tag, u32 Cls, u16 * Syn ) { snmp_cnv_t *Cnv; Cnv = SnmpCnv; while (Cnv->Syntax != -1) { if (Cnv->Tag == Tag && Cnv->Class == Cls) { *Syn = (u16) Cnv->Syntax; return TRUE; } Cnv++; } snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; return FALSE; } int SnmpObjEnc ( asn1_sck_t * Asn1, snmp_object_t * Obj ) { u32 Cls, Tag; u8 *Eoc, *End; int i; /* #ifdef SNMP_DEBUG PrintStr("\r\n Entering the SnmpObjEnc module"); PrintStr("\r\n Obj->Request="); PrintWord(Obj->Request); PrintStr("\r\n Obj->Id="); for(i = 0; i IdLen;i++) { PrintLong(Obj->Id[i]); PrintChar(' '); } PrintStr("\r\nsize of Obj = "); PrintLong(sizeof(*Obj)); PrintStr("\r\nsize of Obj->Syntax = "); PrintLong(sizeof(Obj->Syntax)); PrintStr("\r\nObj->SyntaxLen = "); PrintLong(Obj->SyntaxLen); #endif */ if (!Asn1EocEnc(Asn1, &Eoc)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 1"); #endif return FALSE; } switch (Obj->Type) { case SNMP_INTEGER: if (!Asn1IntEncLng(Asn1, &End, Obj->Syntax.LngInt)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 2"); PrintStr("\r\nSyntax.LngInt ="); PrintLong(Obj->Syntax.LngInt); #endif return FALSE; } break; case SNMP_OCTETSTR: case SNMP_OPAQUE: if (!Asn1OtsEnc(Asn1, &End, Obj->Syntax.BufChr, Obj->SyntaxLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 3"); PrintStr("\r\n SyntaxLen ="); PrintByte(Obj->SyntaxLen); PrintStr("\r\n BufChr ="); for(i=0;i SyntaxLen;i++) { PrintByte(Obj->Syntax.BufChr[i]); } #endif return FALSE; } break; case SNMP_NULL: if (!Asn1NulEnc(Asn1, &End)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 4"); #endif return FALSE; } break; case SNMP_OBJECTID: if (!Asn1OjiEnc(Asn1, &End, (u32 *) Obj->Syntax.BufInt, Obj->SyntaxLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 5"); #endif return FALSE; } break; case SNMP_IPADDR: if (!Asn1OtsEnc(Asn1, &End, (u8 *) & Obj->Syntax.LngUns, 4)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 6"); #endif return FALSE; } break; case SNMP_COUNTER: case SNMP_GAUGE: case SNMP_TIMETICKS: if (!Asn1IntEncLngUns(Asn1, &End, Obj->Syntax.LngUns)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 7"); #endif return FALSE; } break; default: snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 8"); #endif return FALSE; } if (!SnmpSyn2TagCls(&Tag, &Cls, Obj->Type)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 10"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, Cls, ASN1_PRI, Tag)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 11"); #endif return FALSE; } if (!Asn1OjiEnc(Asn1, &End, (u32 *) Obj->Id, Obj->IdLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 12"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_OJI)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 13"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, Eoc, ASN1_UNI, ASN1_CON, ASN1_SEQ)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module 14"); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpObjEnc module successfully"); #endif return TRUE; } int SnmpObjDec ( asn1_sck_t * Asn1, snmp_object_t * Obj ) { u32 Cls, Con, Tag; u8 *Eoc, *End; if (!Asn1HdrDec(Asn1, &Eoc, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_CON || Tag != ASN1_SEQ) return FALSE; if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_OJI) return FALSE; if (!Asn1OjiDec(Asn1, End, (u32 *) Obj->Id, SNMP_SIZE_OBJECTID, &Obj->IdLen)) return FALSE; if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Con != ASN1_PRI) { snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; return FALSE; } if (!SnmpTagCls2Syn(Tag, Cls, &Obj->Type)) return FALSE; switch (Obj->Type) { case SNMP_INTEGER: if (!Asn1IntDecLng(Asn1, End, (i32 *) & Obj->Syntax.LngInt)) return FALSE; break; case SNMP_OCTETSTR: case SNMP_OPAQUE: if (!Asn1OtsDec(Asn1, End, Obj->Syntax.BufChr, SNMP_SIZE_BUFCHR, &Obj->SyntaxLen)) return FALSE; break; case SNMP_NULL: if (!Asn1NulDec(Asn1, End)) return FALSE; break; case SNMP_OBJECTID: if (!Asn1OjiDec(Asn1, End, (u32 *) Obj->Syntax.BufInt, SNMP_SIZE_BUFINT, &Obj->SyntaxLen)) return FALSE; break; case SNMP_IPADDR: if (!Asn1OtsDec(Asn1, End, (u8 *) & Obj->Syntax.LngUns, 4, &Obj->SyntaxLen)) return FALSE; if (Obj->SyntaxLen != 4) return FALSE; break; case SNMP_COUNTER: case SNMP_GAUGE: case SNMP_TIMETICKS: if (!Asn1IntDecLngUns(Asn1, End, (u32 *) & Obj->Syntax.LngUns)) return FALSE; break; default: snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; return FALSE; } if (!Asn1EocDec(Asn1, Eoc)) return FALSE; return TRUE; } int SnmpLstEnc ( asn1_sck_t * Asn1, snmp_object_t * Lst, u32 LstLen ) { u8 *Eoc; #ifdef SNMP_DEBUG PrintStr("\r\n Entering SnmpLstEnc module"); PrintLong(LstLen); #endif if (!Asn1EocEnc(Asn1, &Eoc)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpLstEnc module 1"); #endif return FALSE; } Lst += LstLen; while (LstLen-- > 0) { if (!SnmpObjEnc(Asn1, --Lst)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpLstEnc module with error in SnmpObjEnc"); #endif return FALSE; } } if (!Asn1HdrEnc(Asn1, Eoc, ASN1_UNI, ASN1_CON, ASN1_SEQ)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpLstEnc module 3"); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpLstEnc module successfully"); #endif return TRUE; } int SnmpLstDec ( asn1_sck_t * Asn1, snmp_object_t * Lst, u32 LstSze, u32 * LstLen ) { u32 Cls, Con, Tag; u8 *Eoc; if (!Asn1HdrDec(Asn1, &Eoc, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_CON || Tag != ASN1_SEQ) return FALSE; *LstLen = 0; while (!Asn1Eoc(Asn1, Eoc)) { if (++(*LstLen) > LstSze) { snmpErrStatus = SNMP_TOOBIG; SnmpStat.OutTooBigs++; return FALSE; } if (!SnmpObjDec(Asn1, Lst++)) { snmpErrIndex = (i32) * LstLen; return FALSE; } } if (!Asn1EocDec(Asn1, Eoc)) return FALSE; return TRUE; } int SnmpRqsEnc ( asn1_sck_t * Asn1, snmp_pdu_t * Rqs ) { u8 *End; #ifdef SNMP_DEBUG PrintStr("\r\n Rqs ="); PrintLong(Rqs->ErrorIndex); PrintChar(' '); PrintLong(Rqs->ErrorStatus); PrintChar(' '); PrintLong(Rqs->Id); #endif if (!Asn1IntEncUns(Asn1, &End, Rqs->ErrorIndex)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 1"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 2"); #endif return FALSE; } if (!Asn1IntEncUns(Asn1, &End, Rqs->ErrorStatus)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 3"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 4"); #endif return FALSE; } if (!Asn1IntEncLngUns(Asn1, &End, Rqs->Id)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 5"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpRqsEnc 6"); #endif return FALSE; } return TRUE; } int SnmpRqsDec ( asn1_sck_t * Asn1, snmp_pdu_t * Rqs ) { u32 Cls, Con, Tag; u8 *End; if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_INT) return FALSE; if (!Asn1IntDecLngUns(Asn1, End, (u32 *) & Rqs->Id)) return FALSE; if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_INT) return FALSE; if (!Asn1IntDecUns(Asn1, End, &Rqs->ErrorStatus)) return FALSE; memcpy(&snmpErrAsn1Status, Asn1, sizeof(asn1_sck_t)); if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_INT) return FALSE; if (!Asn1IntDecUns(Asn1, End, &Rqs->ErrorIndex)) return FALSE; /* * Should not receive any error status */ if (Rqs->ErrorStatus != SNMP_NOERROR) { switch (Rqs->ErrorStatus) { case SNMP_TOOBIG: SnmpStat.InTooBigs++; break; case SNMP_NOSUCHNAME: SnmpStat.InNoSuchNames++; break; case SNMP_BADVALUE: SnmpStat.InBadValues++; break; case SNMP_READONLY: SnmpStat.InReadOnlys++; break; default: SnmpStat.InGenErrs++; break; } return FALSE; } #if 0 if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) return FALSE; if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_INT) return FALSE; if (!Asn1IntDecUns(Asn1, End, &Rqs->ErrorIndex)) return FALSE; #endif memcpy(&snmpErrAsn1Index, Asn1, sizeof(asn1_sck_t)); return TRUE; } int SnmpPduEnc ( asn1_sck_t * Asn1, snmp_pdu_t * Pdu, snmp_object_t * Lst, u32 LstLen ) { u8 *Eoc; #ifdef SNMP_DEBUG PrintStr("\r\n Entering the SNMP PDU Enc module"); #endif if (!Asn1EocEnc(Asn1, &Eoc)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module 1"); #endif return FALSE; } if (!SnmpLstEnc(Asn1, Lst, LstLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module with errors in SnmpLstEnc"); #endif return FALSE; } switch (Pdu->Type) { case SNMP_PDU_GET: case SNMP_PDU_NEXT: case SNMP_PDU_RESPONSE: case SNMP_PDU_SET: case SNMP_PDU_TRAP: if (!SnmpRqsEnc(Asn1, Pdu)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module 3"); #endif return FALSE; } break; default: #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module 4"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, Eoc, ASN1_CTX, ASN1_CON, Pdu->Type)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module 5"); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SnmpPduEnc module successfully!"); #endif return TRUE; } int SnmpPduDec ( asn1_sck_t * Asn1, snmp_pdu_t * Pdu, snmp_object_t * Lst, u32 LstSze, u32 * LstLen ) { u32 Cls, Con; u8 *Eoc; #ifdef SNMP_DEBUG PrintStr("\r\n entering the SNMP PDU Dec module"); #endif if (!Asn1HdrDec(Asn1, &Eoc, &Cls, &Con, &Pdu->Type)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SNMP PDU Dec module 1"); #endif return FALSE; } memcpy(&snmpErrAsn1Resp, Asn1, sizeof(asn1_sck_t)); if (Cls != ASN1_CTX || Con != ASN1_CON) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SNMP PDU Dec module 2"); PrintLong(Cls);PrintChar(' '); PrintLong(Con); PrintChar('\n'); PrintLong(ASN1_CTX);PrintChar(' '); PrintLong(ASN1_CON); #endif return FALSE; } switch (Pdu->Type) { case SNMP_PDU_GET: case SNMP_PDU_NEXT: case SNMP_PDU_RESPONSE: case SNMP_PDU_SET: case SNMP_PDU_TRAP: if (!SnmpRqsDec(Asn1, Pdu)) { #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SNMP PDU Dec module 3"); #endif return FALSE; } break; default: SnmpStat.InBadTypes++; #ifdef SNMP_DEBUG PrintStr("\r\n Leaving the SNMP PDU Dec module 4"); #endif return FALSE; } if (!SnmpLstDec(Asn1, Lst, LstSze, LstLen)) { #ifdef SNMP_DEBUG PrintStr("\n Leaving the SNMP PDU Dec module 5"); #endif return FALSE; } if (!Asn1EocDec(Asn1, Eoc)) { #ifdef SNMP_DEBUG PrintStr("\n Leaving the SNMP PDU Dec module 5"); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\n Leaving the SNMP PDU Dec module successfully!"); #endif return TRUE; } int SnmpMsgEnc ( asn1_sck_t * Asn1, snmp_pdu_t * Pdu, u8 * Com, u32 ComLen, snmp_object_t * Lst, u32 LstLen ) { u8 *Eoc, *End; #ifdef SNMP_DEBUG PrintStr("\r\n entering SnmpMsgEnc module"); #endif if (!Asn1EocEnc(Asn1, &Eoc)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 1"); #endif return FALSE; } if (!SnmpPduEnc(Asn1, Pdu, Lst, LstLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 2 with errors in SnmpPduEnc"); #endif return FALSE; } if (!Asn1OtsEnc(Asn1, &End, Com, ComLen)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 3"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_OTS)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 4"); #endif return FALSE; } if (!Asn1IntEncUns(Asn1, &End, SNMP_VERSION)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 5"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 6"); #endif return FALSE; } if (!Asn1HdrEnc(Asn1, Eoc, ASN1_UNI, ASN1_CON, ASN1_SEQ)) { #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module 7"); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\r\n leaving SnmpMsgEnc module successfully!"); #endif return TRUE; } int SnmpMsgDec ( asn1_sck_t * Asn1, snmp_pdu_t * Pdu, u8 * Com, u32 ComSze, u32 * ComLen, snmp_object_t * Lst, u32 LstSze, u32 * LstLen ) { u32 Cls, Con, Tag, Ver; u8 *Eoc, *End; #ifdef SNMP_DEBUG PrintStr("\r\nEnter SnmpMsgDec routine "); #endif if (!Asn1HdrDec(Asn1, &Eoc, &Cls, &Con, &Tag)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 1 "); #endif return FALSE; } if (Cls != ASN1_UNI || Con != ASN1_CON || Tag != ASN1_SEQ) { #ifdef SNMP_DEBUG PrintStr("\r\nCls="); PrintLong(Cls); PrintStr(" Con="); PrintLong(Con); PrintStr(" Tag="); PrintLong(Tag); PrintStr("\r\nLeave SnmpMsgDec routine 2 "); #endif return FALSE; } if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 3 "); #endif return FALSE; } if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_INT) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 4 "); #endif return FALSE; } if (!Asn1IntDecUns(Asn1, End, &Ver)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 5 "); #endif return FALSE; } if (!Asn1HdrDec(Asn1, &End, &Cls, &Con, &Tag)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 6 "); #endif return FALSE; } if (Cls != ASN1_UNI || Con != ASN1_PRI || Tag != ASN1_OTS) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 7 "); #endif return FALSE; } if (!Asn1OtsDec(Asn1, End, Com, ComSze, ComLen)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 8 "); #endif return FALSE; } if (Ver != SNMP_VERSION) { SnmpStat.InBadVersions++; #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 9 "); #endif return FALSE; } if (!SnmpPduDec(Asn1, Pdu, Lst, LstSze, LstLen)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 10 "); PrintStr("\r\n there is something wrong in SnmpPduDec module"); #endif return FALSE; } if (!Asn1EocDec(Asn1, Eoc)) { #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine 11 "); #endif return FALSE; } #ifdef SNMP_DEBUG PrintStr("\r\nLeave SnmpMsgDec routine successfully "); #endif return TRUE; } int SnmpEnc ( u8 ** Snmp, u32 * SnmpLen, snmp_pdu_t * Pdu, u8 * Com, u32 ComLen, snmp_object_t * Lst, u32 LstLen ) { static asn1_sck_t SnmpEncAsn1; #ifdef SNMP_DEBUG PrintStr("\r\n entering SnmpEnc module"); #endif snmpErrStatus = SNMP_NOERROR; snmpErrIndex = 0; Asn1Opn(&SnmpEncAsn1, *Snmp, *SnmpLen, ASN1_ENC); #ifdef SNMP_DEBUG PrintStr("\r\n entering SnmpMsgEnc module"); #endif if (!SnmpMsgEnc(&SnmpEncAsn1, Pdu, Com, ComLen, Lst, LstLen)) { if (snmpErrStatus == SNMP_NOERROR) { switch (asn1ErrStatus) { case ASN1_ERR_ENC_FULL: case ASN1_ERR_NOERROR: case ASN1_ERR_DEC_EMPTY: case ASN1_ERR_DEC_EOC_MISMATCH: case ASN1_ERR_DEC_LENGTH_MISMATCH: case ASN1_ERR_DEC_BADVALUE: case ASN1_ERR_ENC_BADVALUE: default: PrintStr("\r\nSnmpEnc: GENERROR, asn1ErrStatus = "); PrintLong(asn1ErrStatus); snmpErrStatus = SNMP_GENERROR; SnmpStat.OutGenErrs++; break; } } #ifdef SNMP_DEBUG PrintStr("\r\nleaving SnmpEnc module with error"); #endif return FALSE; } Asn1Cls(&SnmpEncAsn1, Snmp, SnmpLen); SnmpStat.OutPkts++; switch (Pdu->Type) { case SNMP_PDU_GET: SnmpStat.OutGetRequests++; break; case SNMP_PDU_NEXT: SnmpStat.OutGetNexts++; break; case SNMP_PDU_RESPONSE: SnmpStat.OutGetResponses++; break; case SNMP_PDU_SET: SnmpStat.OutSetRequests++; break; case SNMP_PDU_TRAP: SnmpStat.OutTraps++; break; default: break; } #ifdef SNMP_DEBUG PrintStr("\r\n entering SnmpEnc module"); #endif return TRUE; } int SnmpDec ( u8 * Snmp, u32 SnmpLen, snmp_pdu_t * Pdu, u8 * Com, u32 ComSze, u32 * ComLen, snmp_object_t * Lst, u32 LstSze, u32 * LstLen ) { static asn1_sck_t SnmpDecAsn1; snmpErrStatus = SNMP_NOERROR; snmpErrIndex = 0; #ifdef SNMP_DEBUG PrintStr("\r\nEnter the module of SNMP DECODE ROUTINES\n"); #endif Asn1Opn(&SnmpDecAsn1, Snmp, SnmpLen, ASN1_DEC); #ifdef SNMP_DEBUG PrintStr("\r\nsnmpLen = "); PrintLong(SnmpLen); #endif if (!SnmpMsgDec(&SnmpDecAsn1, Pdu, Com, ComSze, ComLen, Lst, LstSze, LstLen)) { if (snmpErrStatus == SNMP_NOERROR) { switch (asn1ErrStatus) { case ASN1_ERR_DEC_BADVALUE: case ASN1_ERR_DEC_EOC_MISMATCH: case ASN1_ERR_DEC_LENGTH_MISMATCH: case ASN1_ERR_DEC_EMPTY: snmpErrStatus = SNMP_BADVALUE; SnmpStat.OutBadValues++; SnmpStat.InASNParseErrs++; break; case ASN1_ERR_ENC_FULL: case ASN1_ERR_NOERROR: case ASN1_ERR_ENC_BADVALUE: default: snmpErrStatus = SNMP_GENERROR; SnmpStat.OutGenErrs++; break; } } return FALSE; } Asn1Cls(&SnmpDecAsn1, &Snmp, &SnmpLen); SnmpStat.InPkts++; switch (Pdu->Type) { case SNMP_PDU_GET: SnmpStat.InGetRequests++; break; case SNMP_PDU_NEXT: SnmpStat.InGetNexts++; break; case SNMP_PDU_RESPONSE: SnmpStat.InGetResponses++; break; case SNMP_PDU_SET: SnmpStat.InSetRequests++; break; case SNMP_PDU_TRAP: SnmpStat.InTraps++; break; default: break; } #ifdef SNMP_DEBUG PrintStr("\r\nleaving SnmpDec module successfully!"); #endif return TRUE; } int SnmpErrEnc ( snmp_pdu_t * Rqs ) { u8 *End; /* asn1_sck_t *Asn1 = &snmpErrAsn1; */ #ifdef SNMP_DEBUG PrintStr("\r\nentering SnmpErrEnc module!"); #endif if (!Asn1IntEncUns(&snmpErrAsn1Index, &End, Rqs->ErrorIndex)) return FALSE; if (!Asn1HdrEnc(&snmpErrAsn1Index, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) return FALSE; if (!Asn1IntEncUns(&snmpErrAsn1Status, &End, Rqs->ErrorStatus)) return FALSE; if (!Asn1HdrEnc(&snmpErrAsn1Status, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) return FALSE; /* if(!Asn1IntEncLngUns(&snmpErrAsn1Status, &End, Rqs->Id)) * return FALSE; * if(!Asn1HdrEnc(Asn1, End, ASN1_UNI, ASN1_PRI, ASN1_INT)) * return FALSE; */ if (!Asn1HdrEnc(&snmpErrAsn1Resp, snmpErrAsn1Resp.End, ASN1_CTX, ASN1_CON, Rqs->Type)) return FALSE; #ifdef SNMP_DEBUG PrintStr("\r\nleaving SnmpErrEnc module successfully!"); #endif return TRUE; } void dword2mem ( u8 * mem, ul32 val ) { *(ul32 *) mem = val; } void bzero(u8* str, i32 len) { i32 i; for(i=0;i Type = SNMP_PDU_RESPONSE; rqs->ErrorStatus = status; rqs->ErrorIndex = aindex; #ifdef SNMP_DEBUG PrintStr("\r\nwe are now in MsgRequest Module"); PrintStr("\r\n now we have left MibRequest module with error"); PrintStr("\r\n listLen ="); PrintLong(listLen); #endif if (!SnmpErrEnc(rqs)) { return (FALSE); } memcpy(*rspBuffer, rqsBuffer, rqsLen); *rspLen = rqsLen; return (TRUE); } rqs->Type = SNMP_PDU_RESPONSE; rqs->ErrorStatus = SNMP_NOERROR; rqs->ErrorIndex = 0; #ifdef SNMP_DEBUG PrintStr("\r\n now we have left MibRequest module without error"); PrintStr("\r\n Rqs ="); PrintLong(rqs->ErrorIndex); PrintChar(' '); PrintLong(rqs->ErrorStatus); PrintChar( ' '); PrintLong(rqs->Id); PrintChar( ' '); PrintLong(rqs->Type); #endif if (!SnmpEnc(rspBuffer, rspLen, &Requestpdu, RequestComm, commLen, RequestList, listLen)) { rqs->Type = SNMP_PDU_RESPONSE; rqs->ErrorStatus = (u32) snmpErrStatus; rqs->ErrorIndex = (u32) snmpErrIndex; #ifdef SNMP_DEBUG PrintStr("\r\nleaving SnmpEnc with error"); #endif if (!SnmpErrEnc(rqs)) { return (FALSE); } memcpy(*rspBuffer, rqsBuffer, rqsLen); *rspLen = rqsLen; return (TRUE); } #ifdef SNMP_DEBUG PrintStr("\r\nleaving SnmpEnc without error"); PrintStr("\r\nleaving MsgRequest without error"); PrintStr("\r\n rspLen ="); PrintByte(*rspLen); PrintStr("\r\n rspBuffer ="); for(i=0;i<*rspLen;i++) { PrintByte((*rspBuffer)[i]); PrintChar(' '); } #endif return (TRUE); } void snmpcycle() { ;//do nothing here } void udp_recv() { int status,i; int rqsLen,rspLen; static u8 rspBuffer[AGENT_BUFFER_SIZE]; static u8 rqsBuffer[AGENT_BUFFER_SIZE]; //ul32 addr = rxdnet.ipframe.sourceip; //目标IP地址 unsigned long addr; int port = rxdnet.udpframe.sourceport; // 目的端口号 u8 * rsp = rspBuffer; MibObjectInit(); system.sysServices = 7; system.sysUpTime = 0; strcpy (system.sysDescr,"decription"); strcpy (system.sysLocation, "Enter here exact Location"); strcpy (system.sysName, "system Name"); strcpy (system.sysContact, "system contact info"); rspLen = sizeof(rspBuffer); rqsLen = rxdnet.udpframe.length-8; temp_ip_address.words[0]=rxdnet.ipframe.sourceip[0]; temp_ip_address.words[1]=rxdnet.ipframe.sourceip[1]; addr=temp_ip_address.dwords; #ifdef SNMP_DEBUG PrintStr("\r\n entering udp_recv\r\n"); #endif for(i=0;i