RocDove 2014-12-04 09:32 采纳率: 0%
浏览 1021

如何扩张windows自带的snmp

我在扩展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;

}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 18:43
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?