溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

不用回調(diào)方法捕獲數(shù)據(jù)包

發(fā)布時(shí)間:2020-07-12 05:01:21 來源:網(wǎng)絡(luò) 閱讀:330 作者:1406404014 欄目:網(wǎng)絡(luò)安全

這次將用 pcap_next_ex() 函數(shù)代替上一次的 pcap_loop()函數(shù)。

pcap_loop()函數(shù)是基于回調(diào)的原理來進(jìn)行數(shù)據(jù)捕獲,這是一種精妙的方法,并且在某些場合中,它是一種很好的選擇。 然而,處理回調(diào)有時(shí)候并不實(shí)用 -- 它會(huì)增加程序的復(fù)雜度,特別是在擁有多線程的C++程序中。

可以通過直接調(diào)用pcap_next_ex() 函數(shù)來獲得一個(gè)數(shù)據(jù)包 -- 只有當(dāng)編程人員使用了 pcap_next_ex() 函數(shù)才能收到數(shù)據(jù)包。

這個(gè)函數(shù)的參數(shù)和捕獲回調(diào)函數(shù)的參數(shù)是一樣的 -- 它包含一個(gè)網(wǎng)絡(luò)適配器的描述符和兩個(gè)可以初始化和返回給用戶的指針 (一個(gè)指向 pcap_pkthdr 結(jié)構(gòu)體,另一個(gè)指向數(shù)據(jù)報(bào)數(shù)據(jù)的緩沖)。

在下面的程序中,會(huì)再次用到上一次中的有關(guān)回調(diào)方面的代碼,只是將它放入了main()函數(shù),之后調(diào)用pcap_next_ex()函數(shù)。


實(shí)現(xiàn)代碼:

// 6404002.cpp : 定義控制臺應(yīng)用程序的入口點(diǎn)。
#include "stdafx.h"
#include "pcap.h"


int _tmain(int argc, _TCHAR* argv[])
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
int res;
char errbuf[PCAP_ERRBUF_SIZE];
struct tm *ltime;
char timestr[16];
struct pcap_pkthdr *header;
const u_char *pkt_data;
time_t local_tv_sec;
    
    
    /* 獲取本機(jī)設(shè)備列表 */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* 打印列表 */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }
    
    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
    
    printf("Enter the interface number (1-%d):",i);
    scanf("%d", &inum);
    
    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* 釋放設(shè)備列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
    /* 跳轉(zhuǎn)到已選中的適配器 */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
    
    /* 打開設(shè)備 */
    if ( (adhandle= pcap_open(d->name,          // 設(shè)備名
                              65536,            // 要捕捉的數(shù)據(jù)包的部分 
                                                // 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部內(nèi)容
                              PCAP_OPENFLAG_PROMISCUOUS,    // 混雜模式
                              1000,             // 讀取超時(shí)時(shí)間
                              NULL,             // 遠(yuǎn)程機(jī)器驗(yàn)證
                              errbuf            // 錯(cuò)誤緩沖池
                              ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
        /* 釋放設(shè)列表 */
        pcap_freealldevs(alldevs);
        return -1;
    }
    
    printf("\nlistening on %s...\n", d->description);
    
    /* 釋放設(shè)備列表 */
    pcap_freealldevs(alldevs);
    
    /* 獲取數(shù)據(jù)包 */
    while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){
        
        if(res == 0)
            /* 超時(shí)時(shí)間到 */
            continue;
        
        /* 將時(shí)間戳轉(zhuǎn)換成可識別的格式 */
        local_tv_sec = header->ts.tv_sec;
        ltime=localtime(&local_tv_sec);
        strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
        
        printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
    }
    
    if(res == -1){
        printf("Error reading the packets: %s\n", pcap_geterr(adhandle));
        return -1;
    }
    
    return 0;

}


要用 pcap_next_ex() 代替以前的 pcap_next()是 因?yàn)?nbsp;pcap_next() 有一些不好的地方。首先,它效率低下,盡管它隱藏了回調(diào)的方式,但它依然依賴于函數(shù) pcap_dispatch()。第二,它不能檢測到文件末尾這個(gè)狀態(tài)(EOF),因此,如果數(shù)據(jù)包是從文件讀取來的,那么它就不那么有用了。

值得注意的是, pcap_next_ex() 在成功,超時(shí),出錯(cuò)或EOF的情況下,會(huì)返回不同的值。



注意事項(xiàng):

項(xiàng)目-->**屬性(alt+F7)
配置屬性-->清單工具-->輸入和輸出-->嵌入清單-->否


項(xiàng)目-->**屬性(alt+F7)
配置屬性-->C/C++-->常規(guī)-->附加包含目錄-->

項(xiàng)目-->**屬性(alt+F7)
配置屬性-->鏈接器-->常規(guī)-->附加庫目錄-->

項(xiàng)目-->**屬性(alt+F7)
配置屬性-->鏈接器-->輸入-->附加依賴項(xiàng)-->補(bǔ)充“;Packet.lib;wpcap.lib”

項(xiàng)目-->**屬性(alt+F7)
配置屬性-->C/C++-->預(yù)處理器-->預(yù)處理器定義-->補(bǔ)充“;HAVE_REMOTE”

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI