這一次要分析的實例程序跟上一講很是相似(「打開適配器並捕獲數據包」),略微不一樣的一點是本次將pcap_loop()函數替換成了pcap_next_ex()函數。本節的重點也就是說一下這兩個函數之間的差別。咱們知道pcap_loop()函數是基於回調的原理來進行數據捕獲的,如技術文檔所說,這是一種精妙的方法,而且在某些場合下,它是一種很好的選擇。可是在處理回調有時候會並不實用,它會增長程序的複雜度,特別是在多線程的C++程序中。而對於pcap_next_ex()函數而言,能夠經過直接調用它來得到一個數據包,也只有在調用了這個函數才能收到數據包。pcap_next_ex()函數跟pcap_loop()的回調函數參數是相同的:html
int pcap_next_ex ( pcap_t * p,
struct pcap_pkthdr ** pkt_header,
const u_char ** pkt_data
)
第一個參數是網絡適配器的描述符;第二個參數是一個指向pcap_pkthdr結構體的指針;第三個參數是指向數據報數據的緩衝的指針。網絡
來看一下實例程序的代碼:多線程
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 #include <winsock2.h> 5 #include "pcap.h" 6 7 8 typedef struct sockaddr_in sockad; 9 /* 從tcptraceroute數字IP地址轉換爲字符串 */ 10 #define IPTOSBUFFERS 12 11 char *iptos(u_long in) 12 { 13 static char output[IPTOSBUFFERS][3*4+3+1]; 14 static short which; 15 u_char *p; 16 17 p = (u_char *)∈ 18 which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1); 19 sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); 20 return output[which]; 21 } 22 23 void ifprint(pcap_if_t *d,int* i) 24 { 25 pcap_addr_t *a; 26 27 printf("%d. %s", ++(*i), d->name); 28 if (d->description) 29 printf(" (%s)\n", d->description); 30 else 31 printf(" (No description available)\n"); 32 for(a=d->addresses;a;a=a->next) 33 { 34 switch(a->addr->sa_family) 35 { 36 case AF_INET: 37 if(a->addr) 38 printf("\tIPv4地址:%s\n",iptos(((sockad *)a->addr)->sin_addr.s_addr)); 39 break; 40 } 41 } 42 } 43 44 int main() 45 { 46 pcap_if_t *alldevs; 47 pcap_if_t *d; 48 int inum; 49 int i=0; 50 pcap_t *adhandle; 51 int res; 52 char errbuf[PCAP_ERRBUF_SIZE]; 53 struct tm *ltime; 54 char timestr[16]; 55 struct pcap_pkthdr *header; 56 u_char *pkt_data; 57 58 /* 獲取設備列表 */ 59 if (pcap_findalldevs(&alldevs, errbuf) == -1) 60 { 61 fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf); 62 exit(1); 63 } 64 65 /* 打印列表 */ 66 for(d=alldevs; d; d=d->next) 67 { 68 ifprint(d,&i); 69 } 70 71 if(i==0) 72 { 73 printf("\nNo interfaces found! Make sure WinPcap is installed.\n"); 74 return -1; 75 } 76 77 printf("Enter the interface number (1-%d):",i); 78 scanf("%d", &inum); 79 80 if(inum < 1 || inum > i) 81 { 82 printf("\nInterface number out of range.\n"); 83 /* Free the device list */ 84 pcap_freealldevs(alldevs); 85 return -1; 86 } 87 88 /* 跳轉到選定的適配器 */ 89 for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++); 90 91 /* Open the adapter */ 92 if ( (adhandle= pcap_open_live(d->name, // name of the device 93 65536, // portion of the packet to capture. 94 // 65536 grants that the whole packet will be captured on all the MACs. 95 1, // promiscuous mode 96 1000, // read timeout 97 errbuf // error buffer 98 ) ) == NULL) 99 { 100 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n"); 101 /* Free the device list */ 102 pcap_freealldevs(alldevs); 103 return -1; 104 } 105 106 printf("\nlistening on %s...\n", d->description); 107 108 /* At this point, we don't need any more the device list. Free it */ 109 pcap_freealldevs(alldevs); 110 111 /* 檢索包 */ 112 while((res = pcap_next_ex( adhandle, &header, &pkt_data)) >= 0){ 113 114 if(res == 0) 115 // Timeout elapsed 116 continue; 117 118 //convert the timestamp to readable format 119 ltime=localtime(&header->ts.tv_sec); 120 strftime( timestr, sizeof timestr, "%H:%M:%S", ltime); 121 122 printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len); 123 } 124 125 if(res == -1){ 126 printf("Error reading the packets: %s\n", pcap_geterr(adhandle)); 127 return -1; 128 } 129 130 return 0; 131 }
文檔上在最後簡單地比較了一下pcap_next_ex()函數和pcap_next()函數的區別,經過函數名咱們知道pcap_next_ex()函數是在pcap_next()基礎上擴展獲得的。爲何會擴展?根據文檔說明能夠知道,pcap_next()函數有一些缺陷。好比它效率很低,儘管隱藏了回調的方式,但它仍然依賴於函數pcap_dispatch();另外,它不能檢測到EOF這個狀態,那麼若是數據包是從文件中讀取過來的,那麼它就不那麼好用了。顯然,pcap_next_ex()函數在此基礎上作出了一些改進。最後咱們來看一下pcap_next_ex()函數的返回值,引用文檔中的描述:tcp
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,應該是文件)