• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            大漠落日

            while(!dead) study++;
            posts - 46, comments - 126, trackbacks - 0, articles - 0
              C++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            net-snmp庫(kù)snmp API解讀分析

            Posted on 2011-01-26 17:14 亂78糟 閱讀(10094) 評(píng)論(1)  編輯 收藏 引用 所屬分類(lèi): C語(yǔ)言派系開(kāi)源

            導(dǎo)讀

            net-snmp API分為兩種,一種叫傳統(tǒng)API(Traditional API),一種叫單個(gè)API(Single API)。早期的neet-snmp沒(méi)有考慮到多線程的問(wèn)題,所有的會(huì)話共享同一個(gè)資源,這些就是傳統(tǒng)API,后來(lái)支持多線程的就叫做單個(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ù)是唬人的,根本沒(méi)鎖
                slp->next = Sessions;//在snmp_api.c開(kāi)頭定義全局變量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,這里可以看出所有的會(huì)話共享全局的Sessions鏈表。
            snmp_res_lock為什么說(shuō)是唬人的呢?我們明明在mt_suppotr.h和m_support.c里有看到支持跨平臺(tái)的代碼啊?注意看這兩個(gè)文件里的宏編譯之類(lèi)NETSNMP_REENTRANT,可以在net-snmp-config.h里看到如下的注釋?zhuān)?br>
            /* add in recent resource lock functions (not complete) */
            /* #undef NETSNMP_REENTRANT */
            原來(lái)是還沒(méi)有完全寫(xiě)完,OK,期待后續(xù)版本不用我們來(lái)自己寫(xiě)資源鎖吧。

            snmp_send
            介紹:下面這些函數(shù)使用活動(dòng)的會(huì)話發(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來(lái)創(chuàng)建連續(xù)的包(即pdu),必要時(shí)采用會(huì)話的默認(rèn)項(xiàng)設(shè)置某些pdu數(shù)據(jù)。
            如果這個(gè)PDU有額外的響應(yīng),那就需要排列這個(gè)會(huì)話的外出請(qǐng)求并且存儲(chǔ)這些回調(diào)向量。
            通過(guò)會(huì)話向指定目標(biāo)發(fā)送pdu。
            如果成功,返回這個(gè)pdu請(qǐng)求的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è)會(huì)話指針,否則返回NULL,snmp_error同時(shí)設(shè)置為SNMPERR_BAD_SESSION。

            snmp_select_info
            介紹:
            輸入:如果輸入的timeout沒(méi)有被定義,block設(shè)為1;如果輸入的timeout被定義了,block設(shè)為0。
            輸出:如果輸出的timeout沒(méi)有被定義,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沒(méi)有定義,block為1,那么輸出后timeout值為0,block值被設(shè)為0。

            這里需要注意的是是里面調(diào)用了netsnmp_large_fd_set這個(gè)結(jié)構(gòu),它的介紹如源碼注釋所說(shuō)
            /*
             * 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)用來(lái)接收包。pud的結(jié)果被傳遞給那個(gè)會(huì)話的回調(diào)例程。如果回調(diào)例程返回成功,這個(gè)pdu和它的請(qǐng)求就被刪除掉。

            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è)擁有對(duì)外請(qǐng)求的會(huì)話是否已經(jīng)超時(shí)。如果它發(fā)現(xiàn)一個(gè)(或多個(gè)),并且那個(gè)pdu擁有多余可用的嘗試次數(shù),這個(gè)pud就構(gòu)造一個(gè)新報(bào)文并且重新發(fā)送。如果沒(méi)有多余的可用次數(shù),這個(gè)會(huì)話的回調(diào)函數(shù)就會(huì)被用來(lái)通知用戶(hù)超時(shí)了。

            Feedback

            # re: net-snmp庫(kù)snmp API解讀分析  回復(fù)  更多評(píng)論   

            2011-01-26 17:15 by 亂78糟
            暈死,編輯器丟了好多內(nèi)容,- -!
            国产婷婷成人久久Av免费高清| 久久综合色之久久综合| 合区精品久久久中文字幕一区| 久久精品中文字幕无码绿巨人| 亚洲综合久久夜AV | 久久精品成人影院| 久久国产美女免费观看精品 | 蜜臀av性久久久久蜜臀aⅴ| 久久只有这精品99| 国产精品亚洲综合久久| 人妻无码精品久久亚瑟影视| 蜜桃麻豆WWW久久囤产精品| 狠狠色丁香婷婷久久综合| 久久精品国产欧美日韩99热| 人妻无码精品久久亚瑟影视| 亚洲级αV无码毛片久久精品| 久久夜色精品国产噜噜麻豆 | 久久精品国产只有精品66| 久久亚洲精品无码播放| 18禁黄久久久AAA片| 亚洲精品无码成人片久久| 无码AV中文字幕久久专区| 久久久女人与动物群交毛片 | 久久婷婷色综合一区二区| 国产aⅴ激情无码久久| 久久精品午夜一区二区福利| AV狠狠色丁香婷婷综合久久| 久久久国产一区二区三区| 大香伊人久久精品一区二区 | 久久精品国产亚洲av影院| 久久精品9988| 狠狠色婷婷久久一区二区| 青青青青久久精品国产| 久久综合鬼色88久久精品综合自在自线噜噜 | 青青草国产成人久久91网| 久久亚洲国产成人精品无码区| 国产成人无码精品久久久性色 | 亚洲精品无码久久久久久| 99久久亚洲综合精品网站| 亚洲国产精品高清久久久| 久久精品国产亚洲5555|