用 pcap_next_ex() 函數(shù)代替 _5_ 中的 pcap_loop() 函數(shù);
?
pcap_loop() 函數(shù)是 基于回調(diào)的原理來(lái)進(jìn)行數(shù)據(jù)捕獲 ,這是一種精妙的方法,并且在某些場(chǎng)合中,它是一種很好的選擇。 然而,處理回調(diào)有時(shí)候并不實(shí)用 -- 它會(huì)增加程序的復(fù)雜度,特別是在擁有多線程的C++程序中。
可以通過(guò)直接調(diào)用 pcap_next_ex() 函數(shù)來(lái)獲得一個(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ù)。
?
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 /* 獲取本機(jī)設(shè)備列表 */ 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 /* 釋放設(shè)備列表 */ 56 pcap_freealldevs(alldevs); 57 return - 1 ; 58 } 59 60 /* 跳轉(zhuǎn)到已選中的適配器 */ 61 for (d=alldevs, i= 0 ; i< inum- 1 ;d=d->next, i++ ); 62 63 /* 打開(kāi)設(shè)備 */ 64 if ( (adhandle= pcap_open(d->name, // 設(shè)備名 65 65536 , // 要捕捉的數(shù)據(jù)包的部分 66 // 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容 67 PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式 68 1000 , // 讀取超時(shí)時(shí)間 69 NULL, // 遠(yuǎn)程機(jī)器驗(yàn)證 70 errbuf // 錯(cuò)誤緩沖池 71 ) ) == NULL) 72 { 73 fprintf(stderr, " \nUnable to open the adapter. %s is not supported by WinPcap\n " , d-> name); 74 /* 釋放設(shè)列表 */ 75 pcap_freealldevs(alldevs); 76 return - 1 ; 77 } 78 79 printf( " \nlistening on %s...\n " , d-> description); 80 81 /* 釋放設(shè)備列表 */ 82 pcap_freealldevs(alldevs); 83 84 /* 獲取數(shù)據(jù)包 */ 85 while ((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0 ){ 86 87 if (res == 0 ) 88 /* 超時(shí)時(shí)間到 */ 89 continue ; 90 91 /* 將時(shí)間戳轉(zhuǎn)換成可識(shí)別的格式 */ 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 }
? ·結(jié)果:
?
為什么我們要用 pcap_next_ex() 代替以前的 pcap_next() ? 因?yàn)? pcap_next() 有一些不好的地方。首先,它效率低下,盡管它隱藏了回調(diào)的方式,但它依然依賴于函數(shù) pcap_dispatch() 。第二,它不能檢測(cè)到文件末尾這個(gè)狀態(tài)(EOF),因此,如果數(shù)據(jù)包是從文件讀取來(lái)的,那么它就不那么有用了。
值得注意的是, pcap_next_ex() 在成功,超時(shí),出錯(cuò)或EOF的情況下,會(huì)返回不同的值。
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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