Net-snmp总结(六)-net-snmp源码分析
来源:互联网 发布:mars安卓视频教程源码 编辑:程序博客网 时间:2024/06/05 23:41
一、net-snmp源码分析
Net-snmp代码的核心,就是初始化函数时,向代理(snmpd)注册了回调处理函数,当(snmpd)接收到一个snmp请求包时,它会先对包进行校验,如果校验不通过,会返回相应的错误。如果通过后,它会解析请求包,并把请求包的内容转换成请求结构(netsnmp_agent_request_info【包含请求包的pdu信息】,netsnmp_request_info【包含请求包的vb信息】)。匹配到相关的oid时,就调用相应的注册处理函数并传入请求结构给处理函数,处理函数只需要根据结构中的内容进行相应的业务处理就可以了。
下面用之前添加的testGet代码来做简要说明。
/** Initializes the testGet module */voidinit_testGet(void){ const oid GetTime_oid[] = { 1, 3, 6, 1, 4, 1, 77695, 1 }; DEBUGMSGTL(("testGet", "Initializing\n")); netsnmp_register_scalar(netsnmp_create_handler_registration ("GetTime", handle_GetTime, GetTime_oid, OID_LENGTH(GetTime_oid), HANDLER_CAN_RONLY));}inthandle_GetTime(netsnmp_mib_handler *handler, netsnmp_handler_registration *reginfo, netsnmp_agent_request_info *reqinfo, netsnmp_request_info *requests){ time_t t; switch (reqinfo->mode) { case MODE_GET: time(&t); char szTime[100]; snprintf(szTime,100,"%s",ctime(&t)); snmp_set_var_typed_value(requests->requestvb, ASN_OCTET_STR, /* * XXX: a pointer to the scalar's data */ szTime, /* * XXX: the length of the data in bytes */ strlen(szTime)); break; default: /* * we should never get here, so this is a really bad error */ snmp_log(LOG_ERR, "unknown mode (%d) in handle_GetTime\n", reqinfo->mode); return SNMP_ERR_GENERR; } return SNMP_ERR_NOERROR;}
初始函数调用netsnmp_create_handler_registration向snmpd注册GetTime_oid的处理函数为handle_GetTime,当snmpd调用handle_GetTime时,便会把相应的snmp包的信息从这几个参数中传进来。其中reqinfo 就是snmp请求包被snmpd解析后得到的结构,包含了请求包的会话和pdu信息;requests主要包含了 VB 信息。每次处理一个SNMP请求,就调用一次注册函数。
主要的几个结构体如下
netsnmp_agent_request_info
/** @struct netsnmp_agent_request_info_s * The agent transaction request structure */ typedef struct netsnmp_agent_request_info_s { int mode; /** pdu contains authinfo, eg */ /* netsnmp_pdu *pdu; */ struct netsnmp_agent_session_s *asp; /* may not be needed */ /* * can be used to pass information on a per-pdu basis from a * helper to the later handlers */ netsnmp_data_list *agent_data; } netsnmp_agent_request_info;
netsnmp_request_info
/** @typedef struct netsnmp_request_info_s netsnmp_request_info * Typedefs the netsnmp_request_info_s struct into * netsnmp_request_info*/ /** @struct netsnmp_request_info_s * The netsnmp request info structure. */ typedef struct netsnmp_request_info_s { /** * variable bindings */ netsnmp_variable_list *requestvb; /** * can be used to pass information on a per-request basis from a * helper to the later handlers */ netsnmp_data_list *parent_data; /* * pointer to the agent_request_info for this request */ struct netsnmp_agent_request_info_s *agent_req_info; /** don't free, reference to (struct tree)->end */ oid *range_end; size_t range_end_len; /* * flags */ int delegated; int processed; int inclusive; int status; /** index in original pdu */ int index; /** get-bulk */ int repeat; int orig_repeat; netsnmp_variable_list *requestvb_start; /* internal use */ struct netsnmp_request_info_s *next; struct netsnmp_request_info_s *prev; struct netsnmp_subtree_s *subtree; } netsnmp_request_info;
二、net-snmp程序逻辑
snmpd代理完成两个功能:
1、接收网管发过来的snmp包,并对接收到的snmp包进行解析,校验后,找到并调用相应的处理函数进行处理。
2、调用注册了的告警函数,向网管发送告警信息。
Net-snmp代码流程图如下图所示
阅读全文
0 0
- Net-snmp总结(六)-net-snmp源码分析
- net-snmp的片段源码分析
- net-snmp example分析
- 移植NET-SNMP总结
- net-snmp库snmp API解读分析
- Net-snmp总结(一)-snmp的基本概念
- net-snmp
- net-snmp移植项目总结
- net-snmp移植项目总结
- Net-snmp总结(二)-net-snmp配置编译安装
- net-snmp配置文件snmp.conf
- NET-SNMP学习--SNMP get
- 总结:用net-snmp开发snmp代理客户端
- 总结:用net-snmp开发snmp代理客户端
- [net-snmp] Net-SNMP学习笔记
- net-snmp代码分析之fd_event_manager.c
- net-snmp代码分析之fd_event_manager.c
- 移植NET-SNMP(ARM 9)总结
- 三步搭建Python可视化开发环境
- 完美解决微信video视频隐藏控件和内联播放问题
- SpringBoot开发Junit单元测试方法
- 为什么要使用 BigDecimal
- 简单实现手机号验证码注册功能
- Net-snmp总结(六)-net-snmp源码分析
- 树状数组 详解
- Projection of Bumpy Surfaces凹凸曲面投影
- 图像处理的变分法研究(1)——11月第一阶段进展报告
- django gunicorn启动配置
- 谷歌插件安装
- bzoj 2301(1101) [HAOI2011]Problem b 莫比乌斯反演+分段优化
- java中queue的使用
- AS gradle 中Failed to resolve:com.android.support:appcompat-v7:报错处理