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
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

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

报告相同问题?

悬赏问题

  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体