這一次要分析的實例程序跟上一講非常類似(“打開適配器並捕獲數據包”),略微不同的一點是本次將pcap_loop()函數替換成了pcap_next_ex()函數。本節的重點也就是說一下這兩個函數之間的差異。我們知道pcap_loop()函數是基於回調的原理來進行數據捕獲的,如技術文檔所說,這是一種精妙的方法,並且在某些場合下,它是一種很好的選擇。但是在處理回調有時候會並不實用,它會增加程序的復雜度,特別是在多線程的C++程序中。而對於pcap_next_ex()函數而言,可以通過直接調用它來獲得一個數據包,也只有在調用了這個函數才能收到數據包。pcap_next_ex()函數跟pcap_loop()的回調函數參數是相同的:
int pcap_next_ex ( pcap_t * p,
struct pcap_pkthdr ** pkt_header,
const u_char ** pkt_data
)
第一個參數是網絡適配器的描述符;第二個參數是一個指向pcap_pkthdr結構體的指針;第三個參數是指向數據報數據的緩沖的指針。
來看一下實例程序的代碼:
#define HAVE_REMOTE
#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;
/* 獲取本機設備列表 */
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");
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳轉到已選中的適配器 */
for(d=alldevs, i=0; i< inum-1 ; d=d->next, i++);
/* 打開設備 */
if ( (adhandle= pcap_open(d->name, // 設備名
65536, // 要捕捉的數據包的部分
// 65535保證能捕獲到不同數據鏈路層上的每個數據包的全部內容
PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式
1000, // 讀取超時時間
NULL, // 遠程機器驗證
errbuf // 錯誤緩沖池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 釋放設列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* 釋放設備列表 */
pcap_freealldevs(alldevs);
/* 獲取數據包 */
while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0)
{
if(res == 0)
/* 超時時間到 */
continue;
/* 將時間戳轉換成可識別的格式 */
local_tv_sec = header->ts.tv_sec;
ltime=localtime(&local_tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s,%.6ld 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_ex()函數是在pcap_next()基礎上擴展得到的。為什么會擴展?根據文檔說明可以知道,pcap_next()函數有一些缺陷。比如它效率很低,盡管隱藏了回調的方式,但它仍然依賴於函數pcap_dispatch();另外,它不能檢測到EOF這個狀態,那么如果數據包是從文件中讀取過來的,那么它就不那么好用了。顯然,pcap_next_ex()函數在此基礎上做出了一些改進。最后我們來看一下pcap_next_ex()函數的返回值,引用文檔中的描述:
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
- (pcap_open_live()設置的超時時間超時,在這種情況下pkt_header和pkt_data指向一個非法的數據)
- -1 if an error occurred (出錯)
- -2 if EOF was reached reading from an offline capture (讀取到EOF,應該是文件)
