用 pcap_next_ex() 函數代替 _5_ 中的 pcap_loop() 函數;
?
pcap_loop() 函數是 基于回調的原理來進行數據捕獲 ,這是一種精妙的方法,并且在某些場合中,它是一種很好的選擇。 然而,處理回調有時候并不實用 -- 它會增加程序的復雜度,特別是在擁有多線程的C++程序中。
可以通過直接調用 pcap_next_ex() 函數來獲得一個數據包 -- 只有當編程人員使用了 pcap_next_ex() 函數才能收到數據包。
這個函數的參數和捕獲回調函數的參數是一樣的 -- 它包含 一個網絡適配器的描述符 和 兩個可以初始化和返回給用戶的指針 (一個指向 pcap_pkthdr 結構體,另一個指向數據報數據的緩沖)。
在下面的程序中,我們會再次用到上一講中的有關回調方面的代碼,只是我們將它放入了main()函數,之后調用 pcap_next_ex() 函數。
?
int pcap_next_ex ( pcap_t * p, struct pcap_pkthdr ** pkt_header, const u_char ** pkt_data )
Read a packet from an interface or from an offline capture.
This function is used to retrieve the next available packet, bypassing the callback method traditionally provided by libpcap.
pcap_next_ex fills the pkt_header and pkt_data parameters (see pcap_handler() ) with the pointers to the header and to the data of the next captured packet.
The return value can be:
- 1 if the packet has been read without problems
- 0 if the timeout set with pcap_open_live() has elapsed. In this case pkt_header and pkt_data don't point to a valid packet
- -1 if an error occurred
- -2 if EOF was reached reading from an offline capture
?
?
typedef void (*) pcap_handler(u_char *user, const struct pcap_pkthdr *pkt_header, const u_char *pkt_data)
Prototype of the callback function that receives the packets.
When pcap_dispatch() or pcap_loop() are called by the user, the packets are passed to the application by means of this callback. user is a user-defined parameter that contains the state of the capture session, it corresponds to the user parameter of pcap_dispatch() and pcap_loop() . pkt_header is the header associated by the capture driver to the packet. It is NOT a protocol header. pkt_data points to the data of the packet, including the protocol headers.?
?

1 #include " pcap.h " 2 #pragma comment(lib, "wpcap.lib") 3 #pragma comment(lib, "Packet.lib") 4 #pragma comment(lib, "wsock32.lib") 5 6 7 #include " pcap.h " 8 9 10 main() 11 { 12 pcap_if_t * alldevs; 13 pcap_if_t * d; 14 int inum; 15 int i= 0 ; 16 pcap_t * adhandle; 17 int res; 18 char errbuf[PCAP_ERRBUF_SIZE]; 19 struct tm * ltime; 20 char timestr[ 16 ]; 21 struct pcap_pkthdr * header; 22 const u_char * pkt_data; 23 time_t local_tv_sec; 24 25 26 /* 獲取本機設備列表 */ 27 if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == - 1 ) 28 { 29 fprintf(stderr, " Error in pcap_findalldevs: %s\n " , errbuf); 30 exit( 1 ); 31 } 32 33 /* 打印列表 */ 34 for (d=alldevs; d; d=d-> next) 35 { 36 printf( " %d. %s " , ++i, d-> name); 37 if (d-> description) 38 printf( " (%s)\n " , d-> description); 39 else 40 printf( " (No description available)\n " ); 41 } 42 43 if (i== 0 ) 44 { 45 printf( " \nNo interfaces found! Make sure WinPcap is installed.\n " ); 46 return - 1 ; 47 } 48 49 printf( " Enter the interface number (1-%d): " ,i); 50 scanf( " %d " , & inum); 51 52 if (inum < 1 || inum > i) 53 { 54 printf( " \nInterface number out of range.\n " ); 55 /* 釋放設備列表 */ 56 pcap_freealldevs(alldevs); 57 return - 1 ; 58 } 59 60 /* 跳轉到已選中的適配器 */ 61 for (d=alldevs, i= 0 ; i< inum- 1 ;d=d->next, i++ ); 62 63 /* 打開設備 */ 64 if ( (adhandle= pcap_open(d->name, // 設備名 65 65536 , // 要捕捉的數據包的部分 66 // 65535保證能捕獲到不同數據鏈路層上的每個數據包的全部內容 67 PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式 68 1000 , // 讀取超時時間 69 NULL, // 遠程機器驗證 70 errbuf // 錯誤緩沖池 71 ) ) == NULL) 72 { 73 fprintf(stderr, " \nUnable to open the adapter. %s is not supported by WinPcap\n " , d-> name); 74 /* 釋放設列表 */ 75 pcap_freealldevs(alldevs); 76 return - 1 ; 77 } 78 79 printf( " \nlistening on %s...\n " , d-> description); 80 81 /* 釋放設備列表 */ 82 pcap_freealldevs(alldevs); 83 84 /* 獲取數據包 */ 85 while ((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0 ){ 86 87 if (res == 0 ) 88 /* 超時時間到 */ 89 continue ; 90 91 /* 將時間戳轉換成可識別的格式 */ 92 local_tv_sec = header-> ts.tv_sec; 93 ltime=localtime(& local_tv_sec); 94 strftime( timestr, sizeof timestr, " %H:%M:%S " , ltime); 95 96 printf( " %s,%.6d len:%d\n " , timestr, header->ts.tv_usec, header-> len); 97 } 98 99 if (res == - 1 ){ 100 printf( " Error reading the packets: %s\n " , pcap_geterr(adhandle)); 101 return - 1 ; 102 } 103 104 return 0 ; 105 }
? ·結果:
?
為什么我們要用 pcap_next_ex() 代替以前的 pcap_next() ? 因為 pcap_next() 有一些不好的地方。首先,它效率低下,盡管它隱藏了回調的方式,但它依然依賴于函數 pcap_dispatch() 。第二,它不能檢測到文件末尾這個狀態(EOF),因此,如果數據包是從文件讀取來的,那么它就不那么有用了。
值得注意的是, pcap_next_ex() 在成功,超時,出錯或EOF的情況下,會返回不同的值。
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
