• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            net-snmp庫snmp API解讀分析

            Posted on 2011-01-26 17:14 亂78糟 閱讀(10094) 評論(1)  編輯 收藏 引用 所屬分類: C語言派系開源

            導(dǎo)讀

            net-snmp API分為兩種,一種叫傳統(tǒng)API(Traditional API),一種叫單個API(Single API)。早期的neet-snmp沒有考慮到多線程的問題,所有的會話共享同一個資源,這些就是傳統(tǒng)API,后來支持多線程的就叫做單個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)只列出了若干個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); 
            //這個函數(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里有看到支持跨平臺的代碼???注意看這兩個文件里的宏編譯之類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),必要時采用會話的默認(rèn)項(xiàng)設(shè)置某些pdu數(shù)據(jù)。
            如果這個PDU有額外的響應(yīng),那就需要排列這個會話的外出請求并且存儲這些回調(diào)向量。
            通過會話向指定目標(biāo)發(fā)送pdu。
            如果成功,返回這個pdu請求的id,并且這個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)前這個session,如果存在返回這個會話指針,否則返回NULL,snmp_error同時設(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這個結(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。每個socket的fd集合都有一個從它讀取的包,同時snmp_parse被調(diào)用來接收包。pud的結(jié)果被傳遞給那個會話的回調(diào)例程。如果回調(diào)例程返回成功,這個pdu和它的請求就被刪除掉。

            snmp_timeout
            介紹:當(dāng)snmp_select_info里設(shè)定的超時期滿的時候,這個函數(shù)應(yīng)當(dāng)被調(diào)用,但是它是冪等(idempotent)的,所以snmp_timeout能夠被檢驗(yàn)(大概一個cpu時間)。snmp_timeout檢驗(yàn)查看每一個擁有對外請求的會話是否已經(jīng)超時。如果它發(fā)現(xiàn)一個(或多個),并且那個pdu擁有多余可用的嘗試次數(shù),這個pud就構(gòu)造一個新報文并且重新發(fā)送。如果沒有多余的可用次數(shù),這個會話的回調(diào)函數(shù)就會被用來通知用戶超時了。

            Feedback

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

            2011-01-26 17:15 by 亂78糟
            暈死,編輯器丟了好多內(nèi)容,- -!
            久久综合久久鬼色| 狼狼综合久久久久综合网| 7国产欧美日韩综合天堂中文久久久久 | 久久久久久久久无码精品亚洲日韩| 久久夜色精品国产噜噜麻豆| 久久99国产精一区二区三区| 国产免费福利体检区久久| 无码任你躁久久久久久老妇App| www性久久久com| 亚洲国产小视频精品久久久三级| 99精品久久精品一区二区| 青青草原综合久久大伊人导航| 久久精品国产亚洲AV无码娇色 | 99re这里只有精品热久久| 亚洲国产成人久久综合区| 99久久免费国产精精品| 成人久久免费网站| 91久久精品国产免费直播| 日产精品久久久一区二区| 青春久久| 久久精品嫩草影院| 久久精品国产网红主播| 久久无码高潮喷水| 久久亚洲精品无码观看不卡| 久久综合九色综合97_久久久| 久久久久亚洲AV无码专区首JN| 99久久国产主播综合精品| AV无码久久久久不卡网站下载| 久久成人国产精品免费软件| 久久综合久久性久99毛片| 国内精品伊人久久久久网站| 久久精品国产亚洲一区二区| 成人国内精品久久久久影院| 久久综合给合久久国产免费| 精品人妻伦九区久久AAA片69| 亚洲精品乱码久久久久久不卡| 久久久久99精品成人片| 久久综合九色综合久99| 久久亚洲高清综合| 欧美日韩精品久久久免费观看| 久久伊人五月丁香狠狠色|