wincap中文版翻譯
(一)得到網絡驅動列表
用PCAP寫應用程序的第一件事往往就是要獲得本地的網卡列表。PCAP提供了pcap_findalldevs()這個函數來實現此功能,這個API返回一個
pcap_if結構的連表,連表的每項內容都含有全面的網卡信息:尤其是字段名字和含有名字的描述以及有關驅動器的易讀信息。
得到網絡驅動列表的程序如下:
程序代碼: [ 復制代碼到剪貼板 ]
#include "pcap.h"
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* 這個API用來獲得網卡 的列表 */
if (pcap_findalldevs(&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;
}
/* We don‘t need any more the device list. Free it */
pcap_freealldevs(alldevs);
}
[code]有關這段程序的一些說明:
首先pcap_findalldevs()同其他的libpcap函數一樣有一個errbuf參數,當有異常情況發生時,這個參數會被PCAP填充為某個特定的錯誤字串。
再次,UNIX也同樣提供pcap_findalldevs()這個函數,但是請注意并非所有的系統都支持libpcap提供的網絡程序接口。所以我門要想寫出合適
的程序就必須考慮到這些情況(系統不能夠返回一些字段的描述信息),在這種情況下我門應該給出類似"No description available"這樣的
提示。
最后結束時別忘了用pcap_freealldevs()釋放掉內存資源。
[code]#include "pcap.h"
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* Retrieve the device list */
if (pcap_findalldevs(&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);
else printf(" (No description available)\n");
}
if(i==0)
{
printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
return;
}
/* We don‘t need any more the device list. Free it */
pcap_freealldevs(alldevs);
}
二)獲得已安裝網絡驅動器的高級信息
在第一章中演示了如何獲得已存在適配器的靜態信息。實際上WinPcap同樣也提供其他的高級信息,特別是 pcap_findalldevs()這個函數返回
的每個 pcap_if結構體都同樣包含一個pcap_addr結構的列表,他包含:
一個地址列表,一個掩碼列表,一個廣播地址列表和一個目的地址列表。
下面的例子通過一個ifprint()函數打印出了pcap_if結構的的所有字段信息,該程序對每一個pcap_findalldevs()所返回的pcap_if結構循環調
用ifprint()來顯示詳細的字段信息。
程序代碼: [ 復制代碼到剪貼板 ]
#include "pcap.h"
#ifndef WIN32
#include <sys/socket.h>
#include <netinet/in.h>
#else
#include <winsock.h>
#endif
void ifprint(pcap_if_t *d);
char *iptos(u_long in);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[PCAP_ERRBUF_SIZE+1];
/* 獲得網卡的列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
/* 循環調用ifprint() 來顯示pcap_if結構的信息*/
for(d=alldevs;d;d=d->next)
{
ifprint(d);
}
return 1;
}
/* Print all the available information on the given interface */
void ifprint(pcap_if_t *d)
{
pcap_addr_t *a;
/* Name */
printf("%s\n",d->name);
/* Description */
if (d->description)
printf("\tDescription: %s\n",d->description);
/* Loopback Address*/
printf("\tLoopback: %s\n",(d->flags & PCAP_IF_LOOPBACK)?"yes":"no");
/* IP addresses */
for(a=d->addresses;a;a=a->next) {
printf("\tAddress Family: #%d\n",a->addr->sa_family);
/*關于 sockaddr_in 結構請參考其他的網絡編程書*/
switch(a->addr->sa_family)
{
case AF_INET:
printf("\tAddress Family Name: AF_INET\n");//打印網絡地址類型
if (a->addr)//打印IP地址
printf("\tAddress: %s\n",iptos(((struct sockaddr_in *)a->addr)->sin_addr.s_addr));
if (a->netmask)//打印掩碼
printf("\tNetmask: %s\n",iptos(((struct sockaddr_in *)a->netmask)->sin_addr.s_addr));
if (a->broadaddr)//打印廣播地址
printf("\tBroadcast Address: %s\n",iptos(((struct sockaddr_in *)a->broadaddr)->sin_addr.s_addr));
if (a->dstaddr)//目的地址
printf("\tDestination Address: %s\n",iptos(((struct sockaddr_in *)a->dstaddr)->sin_addr.s_addr));
break;
default:
printf("\tAddress Family Name: Unknown\n");
break;
}
}
printf("\n");
}
/* 將一個unsigned long 型的IP轉換為字符串類型的IP */
#define IPTOSBUFFERS 12
char *iptos(u_long in)
{
static char output[IPTOSBUFFERS][3*4+3+1];
static short which;
u_char *p;
p = (u_char *)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
三)打開網卡捕獲數據包
現在我門已經知道了如何去獲得網卡的信息現在就讓我們開始真正的工作:打開網卡并捕獲數據流。在這一節
里我們將寫一個打印流經網絡的每個數據包信息的程序。打開網卡的功能是通過pcap_open_live()來實現的它
有三個參數snaplen promisc to_ms。
snaplen用于指定所捕獲包的特定部分,在一些系統上(象xBSD and Win32等)驅動只給出所捕獲數據包的一部分而不是全部,這樣就減少了拷
貝數據的數量從而提高了包捕獲的效率。
promisc指明網卡處于混雜模式,在正常情況下網卡只接受去往它的包而去往其他主機的數據包則被忽略。相反當網卡處于混雜 模式時他將接
收所有的流經它的數據包:這就意味著在共享介質的情況下我門可以捕獲
到其它主機的數據包。大部分的包捕獲程序都將混雜模式設為默認,所有我們在下面的例子里也將網卡設為混雜模式。to_ms 參數指定讀數據
的超時控制,超時以毫秒計算。當在超時時間內網卡上沒有數據到來時對網卡的讀操作將返回(如pcap_dispatch() or pcap_next_ex()等函數
)。還有,如果網卡處于統計模式下(請查看“統計和收集網絡數據流一節”)to_ms還定義了統計的時間間隔。如果該參數為0那么意味著沒
有超時控制,對網卡的讀操作在沒有數據到來是將永遠堵塞。如果為-1那么對網卡的讀操作將立即返回不管有沒有數據可讀。
程序代碼: [ 復制代碼到剪貼板 ]
#include "pcap.h"
/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
/* 獲得網卡的列表 */
if (pcap_findalldevs(&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");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* 找到要選擇的網卡結構 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打開選擇的網卡 */
if ( (adhandle= pcap_open_live(d->name, // 設備名稱
65536, // portion of the packet to capture.
// 65536 grants that the whole packet will be captured on all the MACs.
1, // 混雜模式
1000, // 讀超時為1秒
errbuf // error buffer
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
/* 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);
/* 開始捕獲包 */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* 對每一個到來的數據包調用該函數 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
/* 將時間戳轉變為易讀的標準格式*/
ltime=localtime(&header->ts.tv_sec);
strftime( timestr, sizeof timestr, "%H:%M:%S", ltime);
printf("%s,%.6d len:%d\n", timestr, header->ts.tv_usec, header->len);
}
一旦網卡被打開,舊可以調用pcap_dispatch() 或pcap_loop()進行數據的捕獲,這兩個函數的功能十分相似不同的是pcap_ dispatch()可以不
被阻塞,而pcap_loop()在沒有數據流到達時將阻塞。在這個簡單的例子里用pcap_loop()就足夠了,而在一些復雜的程序里往往用
pcap_dispatch()。
Once the adapter is opened, the capture can be started with pcap_dispatch() or pcap_loop(). These
two functions are very similar, the difference is that pcap_ dispatch() is granted to return when
the expires while pcap_loop() doesn‘t return until cnt packets have been captured, so it can
block for an arbitrary period on a few utilized network. pcap_loop() is enough for the purpose of
this sample, while pcap_dispatch() is normally used in more complex program.
這兩個函數都有返回的參數,一個指向某個函數(該函數用來接受數據如該程序中的packet_handler)的指針,libpcap調用該函數對每個從網
上到來的數據包進行處理和接收數據包。另一個參數是帶有時間戳和包長等信息的頭部,最后一個是含有所有協議頭部數據報的實際數據。注
意MAC的冗余校驗碼一般不出現,因為當一個楨到達并被確認后網卡就把它刪除了,同樣需要注意的是大多數網卡會丟掉冗余碼出錯的數據包,
所以WinPcap一般不能夠捕獲這些出錯的數據報。
剛才的例子里從pcap_pkthdr中提取出了每個數據報的時間戳和長度并在顯示器上打印出了他們。