1

我必须构建一个 SNMP 项目来与各种设备进行通信。遗憾的是,除了用于 SNMP 的 windows dll(mgmtapi.dll、snmpapi.dll、wsnmp32.dll)之外,我不能使用任何库。我已经为这些库和这些库的功能所需的所有结构制作了包装器,但是在循环之后:

  • SnmpMgrOpen(初始化和获取会话)。
  • SnmpVarBindList 创建(与 SnmpMgrStrToOid、SnmpUtilMemAlloc 和其他编组)创建 GET 请求。
  • SnmpMgrRequest 发出请求并填充 SnmpVarBindList 结构。
  • SnmpMgrClose 关闭会话。

当我想使用 SnmpVarBindList 查看 SnmpVarBind 内部的内容时

Marshal.PtrToStringAnsi(varBind.value.asnValue.text.stream, (int)varBind.value.asnValue.text.length + 1)

我收到错误消息:尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

从头文件 snmp.h 翻译的结构是:

[StructLayout(LayoutKind.Sequential)]
public struct AsnOctetString
{
    public uint length;
    public int dynamic;
    public IntPtr stream;
}

[StructLayout(LayoutKind.Sequential)]
public struct AsnObjectIdentifier
{
    public uint idLength;
    public IntPtr ids;
}

[StructLayout(LayoutKind.Explicit)]
public struct AsnUnion
{
    [FieldOffset(0)]
    public int number;
    [FieldOffset(0)]
    public uint unsigned32;
    [FieldOffset(0)]
    public ulong counter64;
    [FieldOffset(0)]
    public AsnOctetString text;
    [FieldOffset(0)]
    public AsnOctetString bits;
    [FieldOffset(0)]
    public AsnObjectIdentifier objectID;
    [FieldOffset(0)]
    public AsnOctetString sequence;
    [FieldOffset(0)]
    public AsnOctetString address;
    [FieldOffset(0)]
    public uint counter;
    [FieldOffset(0)]
    public uint gauge;
    [FieldOffset(0)]
    public uint ticks;
    [FieldOffset(0)]
    public AsnOctetString arbitrary;
}

[StructLayout(LayoutKind.Sequential)]
public struct AsnAny
{
    public byte asnType;
    public AsnUnion asnValue;
}

[StructLayout(LayoutKind.Sequential)]
public struct SnmpVarBind
{
    public AsnObjectIdentifier name;
    public AsnAny value;
}

[StructLayout(LayoutKind.Sequential)]
public struct SnmpVarBindList
{
    public IntPtr list;
    public uint len;
}

有什么想法吗?我可以放置您想要/需要的代码。

4

1 回答 1

1

我知道这个答案很晚,但我最近遇到了 SNMP API 的编组问题。我曾假设这些结构是按字节边界打包的,但这是错误的。如果您查看Snmp.h,您将看到#include <pshpack4.h>哪个打开了 4 字节边界上的打包。这意味着场偏移是不同的。这就是我的AsnAny样子:

[StructLayout(LayoutKind.Explicit)]
    private struct AsnAny
    {
        [FieldOffset(0)]
        public readonly byte Type;

        [FieldOffset(4)]
        public readonly Int32 Number;

        [FieldOffset(4)]
        public readonly UInt32 Unsigned32;

        [FieldOffset(4)]
        public readonly UInt64 Counter64;

        [FieldOffset(4)]
        public readonly AsnOctetString String;

        [FieldOffset(4)]
        public readonly AsnOctetString Bits;

        [FieldOffset(4)]
        public readonly AsnObjectIdentifier Object;

        [FieldOffset(4)]
        public readonly AsnOctetString Sequence;

        [FieldOffset(4)]
        public readonly AsnOctetString IPAddress;

        [FieldOffset(4)]
        public readonly UInt32 Counter32;

        [FieldOffset(4)]
        public readonly UInt32 Gauge32;

        [FieldOffset(4)]
        public readonly UInt32 Ticks;

        [FieldOffset(4)]
        public readonly AsnOctetString Arbitrary;
    }
于 2014-09-25T14:15:56.560 回答