0

我正在使用 WinSnmp 创建一个 C++ 应用程序来管理 Windows 上的 SNMP 代理。它工作得很好,但我发送的所有“响应”消息都在 V1 (0) 中,尽管我设置了 SNMPAPI_UNTRANSLATED_V2 转换模式并且 GET 请求在 V2 中。我不明白为什么,也不知道如何强制版本V2。

我的代码:

#include "stdafx.h"
#include <Winsnmp.h>

#pragma comment(lib, "Wsnmp32.lib")


SNMPAPI_STATUS ReceiveMessage( HSNMP_SESSION hSession )
{
    SNMPAPI_STATUS status;

    HSNMP_ENTITY srcEntity, destEntity;
    HSNMP_CONTEXT context;
    HSNMP_PDU pdu;

    status = SnmpRecvMsg(hSession, &srcEntity, &destEntity, &context, &pdu);
    if (status == SNMPAPI_SUCCESS) {
        smiINT pduType, errorStatus, errorIndex;
        smiINT32 requestId;
        HSNMP_VBL vbl = NULL;
        if (pdu) {
            if (SnmpGetPduData(pdu, &pduType, &requestId, &errorStatus, &errorIndex, &vbl) == SNMPAPI_SUCCESS) {

                if (pduType == SNMP_PDU_GET) {
                    HSNMP_PDU responsePdu = SnmpCreatePdu(hSession, SNMP_PDU_RESPONSE, requestId, SNMP_ERROR_NOERROR, 0, vbl);

                    if (SnmpSendMsg(hSession, destEntity, srcEntity, context, responsePdu) != SNMPAPI_SUCCESS) {
                        printf("error");
                    }
                    SnmpFreePdu(responsePdu);
                }

                SnmpFreeVbl(vbl);
            }
            SnmpFreePdu(pdu);
        }
        SnmpFreeEntity(srcEntity);
        SnmpFreeEntity(destEntity);
        SnmpFreeContext(context);
    }

    return status;
}

static SNMPAPI_STATUS CALLBACK snmpCallback
(IN HSNMP_SESSION hSession,
 IN HWND hWnd,
 IN UINT wMsg,
 IN WPARAM wParam,
 IN LPARAM lParam,
 IN LPVOID lpClientData)
{
    SNMPAPI_STATUS res;
    if (wParam == 0) {
        res = ReceiveMessage(hSession);
    } else if (wParam == SNMPAPI_TL_TIMEOUT) {
        res = SNMPAPI_SUCCESS;
    }

    return res;
}

int _tmain(int argc, _TCHAR* argv[])
{
    smiUINT32 majVersion, minorVersion, level, translateMode, retransmitMode;
    if (SnmpStartupEx(&majVersion, &minorVersion, &level, &translateMode, &retransmitMode) == SNMPAPI_SUCCESS) {

        if (SnmpSetTranslateMode(SNMPAPI_UNTRANSLATED_V2) != SNMPAPI_SUCCESS) {
            printf("Unable to set translate mode");
        }
        HSNMP_SESSION hSession = SnmpCreateSession(NULL, 0, snmpCallback, NULL);
        SnmpGetTranslateMode(&translateMode);
        if (hSession) {
            HSNMP_ENTITY localEntity = SnmpStrToEntity(hSession, "0.0.0.0");
            SnmpListen(localEntity, SNMPAPI_ON);

            while(true) {
                Sleep(1000);
            }
        }
    }

    return 0;
}

接收和发送的 UDP 数据包的内容(使用 Wireshark):

No.     Time           Source                Destination           Protocol Length Info
    126 2.592412000    10.247.29.212         10.247.29.112         SNMP     90     get-request 1.3.6.1.4.1.4865.2.100.1.800001.1.1.0

Internet Protocol Version 4, Src: 10.247.29.212 (10.247.29.212), Dst:
10.247.29.112 (10.247.29.112) User Datagram Protocol, Src Port: rapidbase (1953), Dst Port: snmp (161) Simple Network Management Protocol
    version: v2c (1)
    community: public
    data: get-request (0)
        get-request
            request-id: 19
            error-status: noError (0)
            error-index: 0
            variable-bindings: 1 item
                1.3.6.1.4.1.4865.2.100.1.800001.1.1.0: Value (Null)
                    Object Name: 1.3.6.1.4.1.4865.2.100.1.800001.1.1.0 (iso.3.6.1.4.1.4865.2.100.1.800001.1.1.0)
                    Value (Null)

No.     Time           Source                Destination           Protocol Length Info
    127 2.595141000    10.247.29.112         10.247.29.212         SNMP     90     get-response 1.3.6.1.4.1.4865.2.100.1.800001.1.1.0

Internet Protocol Version 4, Src: 10.247.29.112 (10.247.29.112), Dst:
10.247.29.212 (10.247.29.212) User Datagram Protocol, Src Port: 54034 (54034), Dst Port: rapidbase (1953) Simple Network Management Protocol
    version: version-1 (0)
    community: public
    data: get-response (2)
        get-response
            request-id: 19
            error-status: noError (0)
            error-index: 0
            variable-bindings: 1 item
                1.3.6.1.4.1.4865.2.100.1.800001.1.1.0: Value (Null)
                    Object Name: 1.3.6.1.4.1.4865.2.100.1.800001.1.1.0 (iso.3.6.1.4.1.4865.2.100.1.800001.1.1.0)
                    Value (Null)

如果有人有想法。谢谢 !

4

0 回答 0