Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80287
  • 博文数量: 26
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2014-11-27 23:29
文章分类

全部博文(26)

文章存档

2017年(9)

2016年(13)

2014年(4)

我的朋友

分类: C#/.net

2016-12-15 19:10:14

net-snmp API分为两种,一种叫传统API(Traditional API),一种叫单个API(Single API)。早期的neet-snmp没有考虑到多线程的问题,所有的会话共享同一个资源,这些就是传统API,后来支持多线程的就叫做单个API。详细的内容在源码根目录下的README.thread文件里有详细介绍,这里贴出一部分关键内容。

The functions in the following table are functionally equivalent,
with the exception of these behaviors:
- The Traditional API manages many sessions
- The Traditional API passes a struct snmp_session pointer,
       and touches the Sessions list
- The Single API manages only one session
- The Single API passes an opaque pointer, and does not use Sessions list
 
  Traditional        Single                    Comment
  ===========        ==============            =======
  snmp_sess_init     snmp_sess_init            Call before either open
  snmp_open          snmp_sess_open            Single not on Sessions list
                     snmp_sess_session         Exposes snmp_session pointer
  snmp_send          snmp_sess_send            Send one APDU
  snmp_async_send    snmp_sess_async_send      Send one APDU with callback
  snmp_select_info   snmp_sess_select_info     Which session(s) have input
  snmp_read          snmp_sess_read            Read APDUs
  snmp_timeout       snmp_sess_timeout         Check for timeout
  snmp_close         snmp_sess_close           Single not on Sessions list
 snmp_synch_response snmp_sess_synch_response  Send/receive one APDU
  snmp_error         snmp_sess_error           Get library,system errno

注:
1)分析采用的示例代码源自net-snmp官方教程中一片异步APP代码,详细可以
2)只列出了若干个API,更多的可以查看源码
3)这里分析的net-snmp源码版本为5.6.1

正文


if (!(hs->sess = snmp_open(&sess))) {
    snmp_perror(
"snmp_open");
    
continue;
}
上面是snmp_open使用的演示代码,下面看看snmp_open里具体做了什么事情
netsnmp_session *
snmp_open(netsnmp_session 
*session)
{
    
struct session_list *slp;
    slp 
= (struct session_list *) snmp_sess_open(session);  //调用singleAPI创建
    if (!slp) {
        
return NULL;
    }

    snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION); 
//这个函数是唬人的,根本没锁
    slp->next = Sessions;//在snmp_api.c开头定义全局变量struct session_list *Sessions = NULL;   /* MT_LIB_SESSION */
    Sessions = slp;  //添加到共享的Sessions链上
    snmp_res_unlock(MT_LIBRARY_ID, MT_LIB_SESSION);//同样是唬人的

    
return (slp->session);
}
snmp_open是传统API,这里可以看出所有的会话共享全局的Sessions链表。
snmp_res_lock为什么说是唬人的呢?我们明明在mt_suppotr.h和m_support.c里有看到支持跨平台的代码啊?注意看这两个文件里的宏编译之类NETSNMP_REENTRANT,可以在net-snmp-config.h里看到如下的注释:
/* add in recent resource lock functions (not complete) */
/* #undef NETSNMP_REENTRANT */
原来是还没有完全写完,OK,期待后续版本不用我们来自己写资源锁吧。

snmp_send
介绍:下面这些函数使用活动的会话发送PDUs
 * snmp_send             - traditional API, no callback
 * snmp_async_send       - traditional API, with callback
 * snmp_sess_send        - single session API, no callback
 * snmp_sess_async_send  - single session API, with callback
调用snmp_build来创建连续的包(即pdu),必要时采用会话的默认项设置某些pdu数据。
如果这个PDU有额外的响应,那就需要排列这个会话的外出请求并且存储这些回调向量。
通过会话向指定目标发送pdu。
如果成功,返回这个pdu请求的id,并且这个pdu被释放。如果失败,返回0,调用者必须调用snmmp_free_pdu释放资源。

snmp_send调用snmp_asyn_send,后者又调用snmp_sess_asyn_send,callback和cb_data参数都为NULL。
int snmp_async_send(netsnmp_session * session,
                netsnmp_pdu 
*pdu, snmp_callback callback, void *cb_data)
{
    
void           *sessp = snmp_sess_pointer(session);
    
return snmp_sess_async_send(sessp, pdu, callback, cb_data);
}
snmp_sess_pointer函数在全局变量Sessions里查找当前这个session,如果存在返回这个会话指针,否则返回NULL,snmp_error同时设置为SNMPERR_BAD_SESSION。

snmp_select_info
介绍:
输入:如果输入的timeout没有被定义,block设为1;如果输入的timeout被定义了,block设为0。
输出:如果输出的timeout没有被定义,block被视为1;如果输出的timeout被定义了,block被设为0。
上面的输入输出指定是参数timeout和block。
该函数的返回值为可以操作的socket数量,并且这些socket已经被选到了fdset里,供后续的select操作。

示例代码如下
        int fds = 0, block = 1;
        fd_set fdset;
        
struct timeval timeout;
        FD_ZERO(
&fdset);
        snmp_select_info(
&fds, &fdset, &timeout, &block);
        fds 
= select(fds, &fdset, NULL, NULL, block ? NULL : &timeout);
        
if (fds < 0) {
            perror(
"select failed");
            exit(
1);
        }
        
if (fds) snmp_read(&fdset);
        
else snmp_timeout();
因为输入timeout没有定义,block为1,那么输出后timeout值为0,block值被设为0。

这里需要注意的是是里面调用了netsnmp_large_fd_set这个结构,它的介绍如源码注释所说
/*
 * Structure for holding a set of file descriptors, similar to fd_set.
 *
 * This structure however can hold so-called large file descriptors
 * (>= FD_SETSIZE or 1024) on Unix systems or more than FD_SETSIZE (64)
 * sockets on Windows systems.
 *
 * It is safe to allocate this structure on the stack.
 *
 * This structure must be initialized by calling netsnmp_large_fd_set_init()
 * and must be cleaned up via netsnmp_large_fd_set_cleanup(). If this last
 * function is not called this may result in a memory leak.
 *
 * The members of this structure are:
 * lfs_setsize: maximum set size.
 * lsf_setptr:  points to lfs_set if lfs_setsize <= FD_SETSIZE, and otherwise
 *              to dynamically allocated memory.
 * lfs_set:     file descriptor / socket set data if lfs_setsize <= FD_SETSIZE.
 
*/
typedef 
struct netsnmp_large_fd_set_s {
    unsigned        lfs_setsize;
    fd_set         
*lfs_setptr;
    fd_set          lfs_set;
} netsnmp_large_fd_set;

snmp_read
介绍:校验看看fd里面的集合是否属于snmp。每个socket的fd集合都有一个从它读取的包,同时snmp_parse被调用来接收包。pud的结果被传递给那个会话的回调例程。如果回调例程返回成功,这个pdu和它的请求就被删除掉。

snmp_timeout
介绍:当snmp_select_info里设定的超时期满的时候,这个函数应当被调用,但是它是幂等(idempotent)的,所以snmp_timeout能够被检验(大概一个cpu时间)。snmp_timeout检验查看每一个拥有对外请求的会话是否已经超时。如果它发现一个(或多个),并且那个pdu拥有多余可用的尝试次数,这个pud就构造一个新报文并且重新发送。如果没有多余的可用次数,这个会话的回调函数就会被用来通知用户超时了。
阅读(2494) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~