SNMP协议交互学习-获取udp的udpindatagrams

MIB的组织结构,如下左图,对于udp来说1.3.6.1.2.1.7,组织如下右图,包括4个标量和1个表格

udp节点在LwIP中的定义如下:

const s32_t udp_ids[5] = { 1, 2, 3, 4, 5 };
struct mib_node* const udp_nodes[5] = {
  (struct mib_node*)&udp_scalar, (struct mib_node*)&udp_scalar,
  (struct mib_node*)&udp_scalar, (struct mib_node*)&udp_scalar,
  (struct mib_node*)&udptable
};
const struct mib_array_node udp = {
  &noleafs_get_object_def,
  &noleafs_get_value,
  &noleafs_set_test,
  &noleafs_set_value,
  MIB_NODE_AR,
  5,
  udp_ids,
  udp_nodes
};

SNMPv1的报文格式如下:

版本、PDU类型、请求标识、差错状态、差错索引均为4个字节(这个理解是不对的!!!所有的字段都是TLV格式)

共同体一般为6个字符“public”

/* public (non-static) constants */
/** SNMP v1 == 0 */
const s32_t snmp_version = 0;
/** default SNMP community string */
const char snmp_publiccommunity[7] = "public";

后面的名称、值,不知道该如何写?

 比如,如果想获取udp的udpindatagrams变量,那么,该遍历的MIB编码为1.3.6.1.2.1.7.1,那么,SNMP报文中的名称、值该如何填呢?

 从net-snmp那本书上看到了一个例子,在lwip上测试了一下可以正常操作。

串口终端发送(串口上跑SLIP协议):SNMP get-request,获取udp的udpindatagrams,1.3.6.1.2.1.7.1.0

C0 45 00 00 45 00 08 00 00 FF 11 00 00 DB DC A8 65 05 DB DC A8 65 0A 07 F8 00 A1 00 31 00 00 30 27 02 01 00 04 06 70 75 62 6C 69 63 A0 1A 02 02 00 BD 02 01 00 02 01 00 30 0E 30 0C 06 08 2B 06 01 02 01 07 01 00 05 00 C0

30 27是序列,02 01 00是整型(type=2)版本号,04 06 70 75 62 6C 69 63是字符型(type=4)public,A0 1A是上下文(type=A0)表示get-request,02 02 00 BD是整型请求标识,后面是差错状态和差错索引,30 0E 30 0C是序列(序列怎么理解,怎么有两个???),06 08 2B 06 01 02 01 07 01 00是Object ID(type=6),前两个1.3需要用1个字节表示,用1*40+3=43表示,最后的05 00是NULL空值。

串口终端返回的:SNMP,get-response,回复udp的udpindatagrams

C0 45 00 00 46 00 01 00 00 FF 11 00 00 DB DC A8 65 0A DB DC A8 65 05 00 A1 07 F8 00 32 00 00 30 28 02 01 00 04 06 70 75 62 6C 69 63 A2 1B 02 02 00 BD 02 01 00 02 01 00 30 0F 30 0D 06 08 2B 06 01 02 01 07 01 00 41 01 03 C0

最后,返回的值是41 01 03,41是Counter类型,03表示udpindatagrams=03

SNMP使用的TLV中type类型如下表:

 

以太网发送及回复的内容:

参考资料:

http://blog.csdn.net/jiangtaohu123/article/details/7404920

原文地址:https://www.cnblogs.com/yanhc/p/7487145.html