我在扩展snmp时,自己编写的SnmpExtensionInit函数能被调用,但是在用snmpget获取自己扩展的节点时,无法调用预期的SnmpExtensionQuery函数。猜测是在SnmpExtensionInit函数中对象没能注册成功。下面列出部分代码:
#include
#include
#include
#include "testmib.h"
#pragma comment(lib,"Snmpapi.lib")
//#define OID_SIZEOF( Oid ) ( sizeof Oid / sizeof(UINT) )
// template MIB entry
struct MIB_ENTRY
{
AsnObjectIdentifier asnOid;
void * pStorageValue;
CHAR * szStorageName;
BYTE chType;
UINT unAccess;
MIB_ENTRY* pMibNext;
};
UINT g_unMyOIDPrefix[] = {1, 3, 6, 1, 4, 1, 15};
UINT g_unAgeOid[] = {1,1,1};
AsnObjectIdentifier MIB_OidPrefix = { OID_SIZEOF(g_unMyOIDPrefix), g_unMyOIDPrefix};
DWORD g_dwStartTime = 0;
int g_age = 0;
struct MIB_ENTRY g_MyMibTable[] = {
{
{OID_SIZEOF(g_unAgeOid),g_unAgeOid},
&g_age,
"Age",
ASN_INTEGER,
SNMP_ACCESS_READ_WRITE,
NULL
}
};
UINT g_unMyMibCount = (sizeof(g_MyMibTable) / sizeof(MIB_ENTRY));
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
// TerminateThread(g_hTrapGenThread,0);
// CloseHandle(g_hTrapGenThread);
// CloseHandle(g_hSimulateTrap);
break;
}
return TRUE;
}
// When exported funtion will be called during DLL loading and initialization
BOOL SNMP_FUNC_TYPE SnmpExtensionInit(DWORD dwUptimeReference,HANDLE *phSubagentTrapEvent, AsnObjectIdentifier *pFirstSupportedRegion)
{
*phSubagentTrapEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // creaet this event for the trap
SnmpUtilOidCpy(pFirstSupportedRegion, &MIB_OidPrefix);
//*pFirstSupportedRegion = MIB_OidPrefix;
// *phSubagentTrapEvent = g_hSimulateTrap; // by assigning it pass it to the SNMP service
// So when ever you set this event service will call
// SnmpExtensionTrap exported function
// hard coded initialization
g_age = 0;
g_dwStartTime = GetTickCount();
return SNMPAPI_NOERROR;
}
// this export is to query the MIB table and fields
BOOL SNMP_FUNC_TYPE SnmpExtensionQuery(BYTE bPduType, SnmpVarBindList *pVarBindList, AsnInteger32 *pErrorStatus, AsnInteger32 *pErrorIndex)
{
int nRet = 0;
UINT i = 0;
//AsnObjectName tt;
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
*pErrorIndex = 0;
for
(
;
i < pVarBindList->len;
++i)
{
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
// what type of request we are getting?
switch(bPduType)
{
case SNMP_PDU_GET:// // gets the variable value passed variable in pVarBindList
pVarBindList->list[i].value.asnType = ASN_INTEGER;
pVarBindList->list[i].value.asnValue.number = g_age;
if(*pErrorStatus != SNMP_ERRORSTATUS_NOERROR)
*pErrorIndex++;
break;
case SNMP_PDU_GETNEXT: // gets the next variable related to the passed variable in pVarBindList
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
break;
case SNMP_PDU_SET: // sets a variable
g_age = pVarBindList->list[i].value.asnValue.counter;
*pErrorStatus = SNMP_ERRORSTATUS_NOERROR;
//*pErrorIndex++;
break;
default:
*pErrorStatus = SNMP_ERRORSTATUS_NOSUCHNAME;
*pErrorIndex++;
}
}
return SNMPAPI_NOERROR;
}