日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ winpcap开发(4)

發布時間:2025/3/15 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ winpcap开发(4) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

捕獲數據包而沒有回調
本課程中的示例程序與以前的程序(打開適配器和捕獲數據包)完全相同,但它使用pcap_next_ex()而不是pcap_loop()。
pcap_loop()的基于回調的捕獲機制是優雅的,在某些情況下可能是一個很好的選擇。

但是,處理回調有時是不實際的 - 通常會使程序更加復雜,特別是在多線程應用程序或C ++類的情況下。
在這些情況下,pcap_next_ex()使用直接調用檢索數據包 - 只有在程序員想要的時候才使用pcap_next_ex()數據包。
該函數的參數與捕獲回調相同 - 它需要一個適配器描述符和一些將被初始化并返回給用戶的指針(一個到pcap_pkthdr結構,另一個指向具有數據包數據的緩沖區)。
在以下程序中,我們回收上一課程示例的回調代碼,并在調用pcap_next_ex()之后將其移動到main()中。

#include "pcap.h"int main() { 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;/* Retrieve the device list on the local machine */if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1){fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);exit(1);}/* Print the list */for(d=alldevs; d; d=d->next){printf("%d. %s", ++i, d->name);if (d->description)printf(" (%s)\n", d->description);elseprintf(" (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_s("%d", &inum);if(inum < 1 || inum > i){printf("\nInterface number out of range.\n");/* Free the device list */pcap_freealldevs(alldevs);return -1;}/* Jump to the selected adapter */for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);/* Open the device */if ( (adhandle= pcap_open(d->name, // name of the device65536, // portion of the packet to capture. // 65536 guarantees that the whole packet will be captured on all the link layersPCAP_OPENFLAG_PROMISCUOUS, // promiscuous mode1000, // read timeoutNULL, // authentication on the remote machineerrbuf // error buffer) ) == NULL){fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);/* Free the device list */pcap_freealldevs(alldevs);return -1;}printf("\nlistening on %s...\n", d->description);/* At this point, we don't need any more the device list. Free it */pcap_freealldevs(alldevs);/* Retrieve the packets */while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){if(res == 0)/* Timeout elapsed */continue;/* convert the timestamp to readable format */local_tv_sec = header->ts.tv_sec;localtime_s(<ime, &local_tv_sec);strftime( timestr, sizeof timestr, "%H:%M:%S", <ime);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()?因為pcap_next()有一些缺點。首先,它是低效的,因為它隱藏回調方法,但仍依賴于pcap_dispatch()。第二,它不能檢測到EOF,所以在從文件中收集數據包時不是很有用。

還要注意,pcap_next_ex()返回成功,超時時間,錯誤和EOF條件的不同值。


總結

以上是生活随笔為你收集整理的c++ winpcap开发(4)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。