• <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糟 閱讀(10090) 評論(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糟
            暈死,編輯器丟了好多內容,- -!
            久久国产精品视频| 精品久久久久久久国产潘金莲| 伊人久久大香线蕉av不变影院| 久久亚洲精品国产精品婷婷| 久久精品中文字幕一区| 精品久久久久中文字幕日本| 九九久久99综合一区二区| 国产精品综合久久第一页| 久久夜色精品国产亚洲| 97超级碰碰碰久久久久| 一级A毛片免费观看久久精品| 久久综合香蕉国产蜜臀AV| 久久精品国产黑森林| 久久亚洲私人国产精品| 欧美无乱码久久久免费午夜一区二区三区中文字幕 | 国产激情久久久久影院老熟女| 日韩一区二区三区视频久久| 大伊人青草狠狠久久| 久久人人爽人人爽人人爽| 99久久精品九九亚洲精品| 久久ww精品w免费人成| 久久人人爽人人爽人人片AV高清 | 国产精品久久久久无码av| 一本久久综合亚洲鲁鲁五月天| 久久精品国产99国产电影网| 亚洲色欲久久久综合网东京热| 国产成人精品久久一区二区三区av| 亚洲精品无码久久久久久| 无码乱码观看精品久久| 日本三级久久网| 久久最新精品国产| 99国产欧美久久久精品蜜芽| 亚洲国产欧洲综合997久久| 国产精品久久久久久久app| 久久国产免费直播| 久久夜色撩人精品国产小说| 成人国内精品久久久久影院VR| 久久美女网站免费| 国内精品久久久久久久涩爱| yellow中文字幕久久网| 理论片午午伦夜理片久久 |