免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
net-snmp庫snmp API解讀分析


導(dǎo)讀

net-snmp API分為兩種,一種叫傳統(tǒng)API(Traditional API),一種叫單個(gè)API(Single API)。早期的neet-snmp沒有考慮到多線程的問題,所有的會話共享同一個(gè)資源,這些就是傳統(tǒng)API,后來支持多線程的就叫做單個(gè)API。詳細(xì)的內(nèi)容在源碼根目錄下的README.thread文件里有詳細(xì)介紹,這里貼出一部分關(guān)鍵內(nèi)容。

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代碼,詳細(xì)可以點(diǎn)擊這里
2)只列出了若干個(gè)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);  //調(diào)用singleAPI創(chuàng)建
    if (!slp) {
        
return NULL;
    }

    snmp_res_lock(MT_LIBRARY_ID, MT_LIB_SESSION); 
//這個(gè)函數(shù)是唬人的,根本沒鎖
    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是傳統(tǒng)API,這里可以看出所有的會話共享全局的Sessions鏈表。
snmp_res_lock為什么說是唬人的呢?我們明明在mt_suppotr.h和m_support.c里有看到支持跨平臺的代碼?。孔⒁饪催@兩個(gè)文件里的宏編譯之類NETSNMP_REENTRANT,可以在net-snmp-config.h里看到如下的注釋:
/* add in recent resource lock functions (not complete) */
/* #undef NETSNMP_REENTRANT */
原來是還沒有完全寫完,OK,期待后續(xù)版本不用我們來自己寫資源鎖吧。

snmp_send
介紹:下面這些函數(shù)使用活動的會話發(fā)送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
調(diào)用snmp_build來創(chuàng)建連續(xù)的包(即pdu),必要時(shí)采用會話的默認(rèn)項(xiàng)設(shè)置某些pdu數(shù)據(jù)。
如果這個(gè)PDU有額外的響應(yīng),那就需要排列這個(gè)會話的外出請求并且存儲這些回調(diào)向量。
通過會話向指定目標(biāo)發(fā)送pdu。
如果成功,返回這個(gè)pdu請求的id,并且這個(gè)pdu被釋放。如果失敗,返回0,調(diào)用者必須調(diào)用snmmp_free_pdu釋放資源。

snmp_send調(diào)用snmp_asyn_send,后者又調(diào)用snmp_sess_asyn_send,callback和cb_data參數(shù)都為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函數(shù)在全局變量Sessions里查找當(dāng)前這個(gè)session,如果存在返回這個(gè)會話指針,否則返回NULL,snmp_error同時(shí)設(shè)置為SNMPERR_BAD_SESSION。

snmp_select_info
介紹:
輸入:如果輸入的timeout沒有被定義,block設(shè)為1;如果輸入的timeout被定義了,block設(shè)為0。
輸出:如果輸出的timeout沒有被定義,block被視為1;如果輸出的timeout被定義了,block被設(shè)為0。
上面的輸入輸出指定是參數(shù)timeout和block。
該函數(shù)的返回值為可以操作的socket數(shù)量,并且這些socket已經(jīng)被選到了fdset里,供后續(xù)的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();
因?yàn)檩斎雝imeout沒有定義,block為1,那么輸出后timeout值為0,block值被設(shè)為0。

這里需要注意的是是里面調(diào)用了netsnmp_large_fd_set這個(gè)結(jié)構(gòu),它的介紹如源碼注釋所說
/*
 * 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
介紹:校驗(yàn)看看fd里面的集合是否屬于snmp。每個(gè)socket的fd集合都有一個(gè)從它讀取的包,同時(shí)snmp_parse被調(diào)用來接收包。pud的結(jié)果被傳遞給那個(gè)會話的回調(diào)例程。如果回調(diào)例程返回成功,這個(gè)pdu和它的請求就被刪除掉。

snmp_timeout
介紹:當(dāng)snmp_select_info里設(shè)定的超時(shí)期滿的時(shí)候,這個(gè)函數(shù)應(yīng)當(dāng)被調(diào)用,但是它是冪等(idempotent)的,所以snmp_timeout能夠被檢驗(yàn)(大概一個(gè)cpu時(shí)間)。snmp_timeout檢驗(yàn)查看每一個(gè)擁有對外請求的會話是否已經(jīng)超時(shí)。如果它發(fā)現(xiàn)一個(gè)(或多個(gè)),并且那個(gè)pdu擁有多余可用的嘗試次數(shù),這個(gè)pud就構(gòu)造一個(gè)新報(bào)文并且重新發(fā)送。如果沒有多余的可用次數(shù),這個(gè)會話的回調(diào)函數(shù)就會被用來通知用戶超時(shí)了。
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
python snmp 自動化3-修改python的netsnmp庫
淺談 Linux 系統(tǒng)中的 SNMP Trap
DL框架之TensorFlow:深度學(xué)習(xí)框架TensorFlow Core(低級別TensorFlow API)的簡介、安裝、使用方法之詳細(xì)攻略
【原】Netsnmp調(diào)試方法
cacti windows版的剛剛找到,不敢獨(dú)享先分出來看看誰部署的快!
net-snmp安裝手記
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服