LwIP的SNMP学习笔记
关于这方面的资料网上非常少,做一下笔记。
在LwIP中,在\lwip-1.4.1\src\core\snmp目录下有SNMP相关的c文件,
在lwip-1.4.1\src\include\lwip目录下有SNMP相关的h文件,
在\lwip-1.4.1\doc目录下有一个snmp代理的说明,这是LwIP最直接的说明文档:



snmp_init(void)中会设置UDP接收的回调函数,这样,所有发往本机161端口的udp包都会由snmp_recv处理
/**
* Starts SNMP Agent.
* Allocates UDP pcb and binds it to IP_ADDR_ANY port 161.
*/
void
snmp_init(void)
{
struct snmp_msg_pstat *msg_ps;
u8_t i; snmp1_pcb = udp_new();
if (snmp1_pcb != NULL)
{
udp_recv(snmp1_pcb, snmp_recv, (void *)SNMP_IN_PORT);
udp_bind(snmp1_pcb, IP_ADDR_ANY, SNMP_IN_PORT);
}
snmp_recv函数中会调用snmp_pdu_header_check函数,检查SNMP PDU的头部是否正确
/* lwIP UDP receive callback function */
static void
snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
/* check total length, version, community, pdu type */
err_ret = snmp_pdu_header_check(p, payload_ofs, payload_len, &varbind_ofs, msg_ps);
在snmp_pdu_header_check函数中,先检测type,什么type?PDU类型?但后面判断type有效性里面又不是,为什么?
/**
* Checks and decodes incoming SNMP message header, logs header errors.
*
* @param p points to pbuf chain of SNMP message (UDP payload)
* @param ofs points to first octet of SNMP message
* @param pdu_len the length of the UDP payload
* @param ofs_ret returns the ofset of the variable bindings
* @param m_stat points to the current message request state return
* @return
* - ERR_OK SNMP header is sane and accepted
* - ERR_ARG SNMP header is either malformed or rejected
*/
static err_t
snmp_pdu_header_check(struct pbuf *p, u16_t ofs, u16_t pdu_len, u16_t *ofs_ret, struct snmp_msg_pstat *m_stat)
{
ofs_base = ofs;
snmp_asn1_dec_type(p, ofs, &type);
derr = snmp_asn1_dec_length(p, ofs+, &len_octets, &len);
if ((derr != ERR_OK) ||
(pdu_len != ( + len_octets + len)) ||
(type != (SNMP_ASN1_UNIV | SNMP_ASN1_CONSTR | SNMP_ASN1_SEQ)))
{
snmp_inc_snmpinasnparseerrs();
return ERR_ARG;
}
SNMPv1 agent for lwIP
SNMPv1 agent for lwIP Author: Christiaan Simons This is a brief introduction how to use and configure the SNMP agent.
Note the agent uses the raw-API UDP interface so you may also want to
read rawapi.txt to gain a better understanding of the SNMP message handling. 0 Agent Capabilities
==================== SNMPv1 per RFC1157
This is an old(er) standard but is still widely supported.
For SNMPv2c and v3 have a greater complexity and need many
more lines of code. IMHO this breaks the idea of "lightweight IP".
v1简单,v2和v3虽然更多功能,但需要更多代码,违背了轻量级的初衷
Note the S in SNMP stands for "Simple". Note that "Simple" is
relative. SNMP is simple compared to the complex ISO network
management protocols CMIP (Common Management Information Protocol)
and CMOT (CMip Over Tcp).
SNMP的S本意为简单,但这是相对的(与OSI的相比),其实并不简单
MIB II per RFC1213
The standard lwIP stack management information base.
This is a required MIB, so this is always enabled.
When builing lwIP without TCP, the mib-2.tcp group is omitted.
The groups EGP, CMOT and transmission are disabled by default.
MIB II,RFC1213是必须的MIB
Most mib-2 objects are not writable except:
sysName, sysLocation, sysContact, snmpEnableAuthenTraps.
Writing to or changing the ARP and IP address and route
tables is not possible.
不能写IP地址和路由表???
Note lwIP has a very limited notion of IP routing. It currently
doen't have a route table and doesn't have a notion of the U,G,H flags.
Instead lwIP uses the interface list with only one default interface
acting as a single gateway interface (G) for the default route. The agent returns a "virtual table" with the default route 0.0.0.0
for the default interface and network routes (no H) for each
network interface in the netif_list.
All routes are considered to be up (U). Loading additional MIBs
MIBs can only be added in compile-time, not in run-time.
There is no MIB compiler thus additional MIBs must be hand coded.
MIB只能通过代码实现,不能通过命令行添加
Large SNMP message support
The packet decoding and encoding routines are designed
to use pbuf-chains. Larger payloads then the minimum
SNMP requirement of 484 octets are supported if the
PBUF_POOL_SIZE and IP_REASS_BUFSIZE are set to match your
local requirement. 1 Building the Agent
==================== First of all you'll need to add the following define
to your local lwipopts.h: #define LWIP_SNMP 1 and add the source files in lwip/src/core/snmp
and some snmp headers in lwip/src/include/lwip to your makefile. Note you'll might need to adapt you network driver to update
the mib2 variables for your interface. 2 Running the Agent
=================== The following function calls must be made in your program to
actually get the SNMP agent running. Before starting the agent you should supply pointers
to non-volatile memory for sysContact, sysLocation,
and snmpEnableAuthenTraps. You can do this by calling
mib2.c文件中
snmp_set_syscontact()
snmp_set_syslocation()
snmp_set_snmpenableauthentraps() Additionally you may want to set snmp_set_sysdescr()
snmp_set_sysobjid() (if you have a private MIB)
snmp_set_sysname() Also before starting the agent you need to setup
one or more trap destinations using these calls:
msg_out.c文件中
snmp_trap_dst_enable();
snmp_trap_dst_ip_set(); In the lwIP initialisation sequence call snmp_init() just after
the call to udp_init(). Exactly every 10 msec the SNMP uptime timestamp must be updated with
snmp_inc_sysuptime(). You should call this from a timer interrupt
or a timer signal handler depending on your runtime environment. An alternative way to update the SNMP uptime timestamp is to do a call like
snmp_add_sysuptime(100) each 1000ms (which is bigger "step", but call to
a lower frequency). Another one is to not call snmp_inc_sysuptime() or
snmp_add_sysuptime(), and to define the SNMP_GET_SYSUPTIME(sysuptime) macro.
This one is undefined by default in mib2.c. SNMP_GET_SYSUPTIME is called inside
snmp_get_sysuptime(u32_t *value), and enable to change "sysuptime" value only
when it's queried (any function which need "sysuptime" have to call
snmp_get_sysuptime). 3 Private MIBs
============== If want to extend the agent with your own private MIB you'll need to
add the following define to your local lwipopts.h: #define SNMP_PRIVATE_MIB 1 You must provide the private_mib.h and associated files yourself.
Note we don't have a "MIB compiler" that generates C source from a MIB,
so you're required to do some serious coding if you enable this! Note the lwIP enterprise ID (26381) is assigned to the lwIP project,
ALL OBJECT IDENTIFIERS LIVING UNDER THIS ID ARE ASSIGNED BY THE lwIP
MAINTAINERS! If you need to create your own private MIB you'll need
to apply for your own enterprise ID with IANA: http://www.iana.org/numbers.html You can set it by passing a struct snmp_obj_id to the agent
using snmp_set_sysobjid(&my_object_id), just before snmp_init(). Note the object identifiers for thes MIB-2 and your private MIB
tree must be kept in sorted ascending (lexicographical) order.
This to ensure correct getnext operation. An example for a private MIB is part of the "minimal Unix" project:
contrib/ports/unix/proj/minimal/lwip_prvmib.c The next chapter gives a more detailed description of the
MIB-2 tree and the optional private MIB. 4 The Gory Details
================== 4.0 Object identifiers and the MIB tree. We have three distinct parts for all object identifiers: The prefix
.iso.org.dod.internet the middle part
.mgmt.mib-2.ip.ipNetToMediaTable.ipNetToMediaEntry.ipNetToMediaPhysAddress and the index part
.1.192.168.0.1 Objects located above the .internet hierarchy aren't supported.
Currently only the .mgmt sub-tree is available and
when the SNMP_PRIVATE_MIB is enabled the .private tree
becomes available too. Object identifiers from incoming requests are checked
for a matching prefix, middle part and index part
or are expanded(*) for GetNext requests with short
or inexisting names in the request.
(* we call this "expansion" but this also
resembles the "auto-completion" operation) The middle part is usually located in ROM (const)
to preserve precious RAM on small microcontrollers.
However RAM location is possible for an dynamically
changing private tree. The index part is handled by functions which in
turn use dynamically allocated index trees from RAM.
These trees are updated by e.g. the etharp code
when new entries are made or removed form the ARP cache. /** @todo more gory details */
LwIP的SNMP学习笔记的更多相关文章
- SNMP学习笔记之SNMP TRAP简介、流程以及使用Python实现接受Trap信息
0x00 SNMP TRAP简介 SNMP(Simple Network Management Protocol) trap是一种很有用,但是也容易让人难以理解的协议. 虽然名字叫做简单网络管理协议, ...
- SNMP学习笔记之SNMP 原理与实战详解
原文地址:http://freeloda.blog.51cto.com/2033581/1306743 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法 ...
- SNMP学习笔记之SNMP介绍,OID及MIB库
1.1. SNMP概览 SNMP的基本知识介绍简单网络管理协议(SNMP-Simple Network Management Protocol)是一个与网络设备交互的简单方法.该规范是由IETF ...
- snmp学习笔记
snmp5.5 client 包含头文件 #include <net-snmp/net-snmp-config.h> #include <net-snmp/net-snmp-incl ...
- SNMP学习笔记之SNMPv3的配置和认证以及TroubleShooting
0x00 增加snmp v3用户 增加用户的时候,必须要停止SNMP服务. # service snmpd stop # net-snmp-config --create-snmpv3-user -r ...
- SNMP学习笔记之SNMP树形结构介绍
Basic command of SNMP: GET: The GET operation is a request sent by the manager to the managed device ...
- SNMP学习笔记之iReasoning MIB Browser
0x00 MIB Browser iReasoning MIB浏览器是一个强大和易于使用的工具由iReasoning SNMP API提供支持. MIB浏览器是工程师管理启用SNMP的网络设备和应用程 ...
- SNMP学习笔记之Python的netsnmp和pysnmp的性能对比
0x00 概览 用python获取snmp信息有多个现成的库可以使用,其中比较常用的是netsnmp和pysnmp两个库.网上有较多的关于两个库的例子. 本文重点在于如何并发的获取snmp的数据,即同 ...
- SNMP学习笔记之SNMP的安装及Python的调用
0x00 概述 本文是介绍SNMP在Windows和Linux(Ubuntu)下的安装,以及通过Python调用其接口的文章. 0x01 开发环境 Python 3.5.1 Windows 10 64 ...
随机推荐
- php部署后错误排查流程
未使用框架的php程序不可用时,没有框架提供的调试信息,因此要按照请求的整个生命周期来调试程序, 具体错误依次排查网络,服务器,环境,代码的步骤层层深入,最终定位到错误的发生点. 1 访问程序部署的服 ...
- 哪些工具可以提升PHP开发效率
本文就我自己在开发过程中的一点经验,谈谈如何利用工具来提升开发工作的编码效率, IDE(phpstorm 收费) 一个好的IDE真的可以给开发人员节省大量的时间,我从最开始使用editplus 到su ...
- 使用wireshark 对flutter 框架APP进行抓包
引言 最近公司开发一个APP,由于原生人力不足,直接由前端使用flutter 开发的,而使用flutter框架开发的客户端 fiddler无法抓到包,所以我采用wireshark从路由层面抓包 fid ...
- Java 循环队列
传统数组实现的队列有缺陷,当多次入队出队后,队头指针会后移,当队尾指针达到数组末尾时,会提示队列已满,导致数组前部分空间被浪费.如果当队尾和队头指针到达数组末尾时能从数组[0]继续添加数据,可以提升数 ...
- D2. Optimal Subsequences (Hard Version) 主席树
题目链接:https://codeforces.com/contest/1262/problem/D2 将数组按大到小排序(相同大小的按下标由小到大排序),依次将排序后的每个数在原数组中的位置放入主席 ...
- Alibaba Nacos 服务发现组件集群部署
前面学习了单机模式下的启动,生产环境中部署nacos肯定是使用集群模式cluster保证高可用. 官方文档的集群部署推荐使用VIP+域名模式,把所有服务列表放到一个vip下面,然后挂到一个域名下面. ...
- 「扫盲」Elasticsearch
前言 只有光头才能变强. 文本已收录至我的GitHub精选文章,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家的公司用Elasticsearch多不 ...
- MySQL——DOS命令
翻开之前的笔记发现有这么一篇,于是整理了一下发出来加深记忆并分享交流,欢迎纠错,谢谢!!! 1.启动MySQL服务: net start mysql; 2.停止MySQL服务: net stop my ...
- Django HttpResponse、render、redirect
一.HttpResponse 作业:返回相应的内容 格式: return HttpResponse("Hello, World") 二.render 作业:提交网页和字符串替换 提 ...
- Java入门 - 语言基础 - 19.方法
原文地址:http://www.work100.net/training/java-method.html 更多教程:光束云 - 免费课程 方法 序号 文内章节 视频 1 概述 2 方法的定义 3 方 ...