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

            net-snmp庫snmp API解讀分析

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

            導讀

            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就構造一個新報文并且重新發送。如果沒有多余的可用次數,這個會話的回調函數就會被用來通知用戶超時了。

            Feedback

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

            2011-01-26 17:15 by 亂78糟
            暈死,編輯器丟了好多內容,- -!
            久久精品一区二区三区不卡| 99久久成人国产精品免费| 久久久久亚洲精品中文字幕| 99久久精品国产综合一区| 亚洲乱码日产精品a级毛片久久| 久久精品无码专区免费青青| 色综合久久88色综合天天| 一级A毛片免费观看久久精品| 国产福利电影一区二区三区久久老子无码午夜伦不 | 欧美丰满熟妇BBB久久久| 久久精品人人做人人爽97| 国産精品久久久久久久| 99久久精品午夜一区二区| 久久久久无码精品| 91久久成人免费| 久久精品中文字幕久久| 精品久久8x国产免费观看| 亚洲人成伊人成综合网久久久 | 99久久综合狠狠综合久久| 夜夜亚洲天天久久| 久久露脸国产精品| 99久久精品国产一区二区| 久久天天躁夜夜躁狠狠| 久久精品国产99久久无毒不卡| 精品多毛少妇人妻AV免费久久 | 国产99精品久久| 热久久这里只有精品| 97精品国产97久久久久久免费 | 97精品久久天干天天天按摩| 精品无码久久久久久国产| 久久99精品久久久久久9蜜桃 | 国产成人精品久久免费动漫 | 久久久精品人妻一区二区三区蜜桃| 国内精品九九久久久精品| 777久久精品一区二区三区无码| 色播久久人人爽人人爽人人片aV| 久久精品国产日本波多野结衣| 久久精品视频网| 国内精品久久久久影院优| 综合网日日天干夜夜久久| 人妻无码精品久久亚瑟影视|