wincap中文版翻譯
(一)得到網(wǎng)絡驅動列表
用PCAP寫應用程序的第一件事往往就是要獲得本地的網(wǎng)卡列表。PCAP提供了pcap_findalldevs()這個函數(shù)來實現(xiàn)此功能,這個API返回一個
pcap_if結構的連表,連表的每項內容都含有全面的網(wǎng)卡信息:尤其是字段名字和含有名字的描述以及有關驅動器的易讀信息。
得到網(wǎng)絡驅動列表的程序如下:
程序代碼: [ 復制代碼到剪貼板 ]
#include "pcap.h"
main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* 這個API用來獲得網(wǎng)卡 的列表 */
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函數(shù)一樣有一個errbuf參數(shù),當有異常情況發(fā)生時,這個參數(shù)會被PCAP填充為某個特定的錯誤字串。
再次,UNIX也同樣提供pcap_findalldevs()這個函數(shù),但是請注意并非所有的系統(tǒng)都支持libpcap提供的網(wǎng)絡程序接口。所以我門要想寫出合適
的程序就必須考慮到這些情況(系統(tǒng)不能夠返回一些字段的描述信息),在這種情況下我門應該給出類似"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);
}
二)獲得已安裝網(wǎng)絡驅動器的高級信息
在第一章中演示了如何獲得已存在適配器的靜態(tài)信息。實際上WinPcap同樣也提供其他的高級信息,特別是 pcap_findalldevs()這個函數(shù)返回
的每個 pcap_if結構體都同樣包含一個pcap_addr結構的列表,他包含:
一個地址列表,一個掩碼列表,一個廣播地址列表和一個目的地址列表。
下面的例子通過一個ifprint()函數(shù)打印出了pcap_if結構的的所有字段信息,該程序對每一個pcap_findalldevs()所返回的pcap_if結構循環(huán)調
用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];
/* 獲得網(wǎng)卡的列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n",errbuf);
exit(1);
}
/* 循環(huán)調用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 結構請參考其他的網(wǎng)絡編程書*/
switch(a->addr->sa_family)
{
case AF_INET:
printf("\tAddress Family Name: AF_INET\n");//打印網(wǎng)絡地址類型
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];
}
三)打開網(wǎng)卡捕獲數(shù)據(jù)包
現(xiàn)在我門已經(jīng)知道了如何去獲得網(wǎng)卡的信息現(xiàn)在就讓我們開始真正的工作:打開網(wǎng)卡并捕獲數(shù)據(jù)流。在這一節(jié)
里我們將寫一個打印流經(jīng)網(wǎng)絡的每個數(shù)據(jù)包信息的程序。打開網(wǎng)卡的功能是通過pcap_open_live()來實現(xiàn)的它
有三個參數(shù)snaplen promisc to_ms。
snaplen用于指定所捕獲包的特定部分,在一些系統(tǒng)上(象xBSD and Win32等)驅動只給出所捕獲數(shù)據(jù)包的一部分而不是全部,這樣就減少了拷
貝數(shù)據(jù)的數(shù)量從而提高了包捕獲的效率。
promisc指明網(wǎng)卡處于混雜模式,在正常情況下網(wǎng)卡只接受去往它的包而去往其他主機的數(shù)據(jù)包則被忽略。相反當網(wǎng)卡處于混雜 模式時他將接
收所有的流經(jīng)它的數(shù)據(jù)包:這就意味著在共享介質的情況下我門可以捕獲
到其它主機的數(shù)據(jù)包。大部分的包捕獲程序都將混雜模式設為默認,所有我們在下面的例子里也將網(wǎng)卡設為混雜模式。to_ms 參數(shù)指定讀數(shù)據(jù)
的超時控制,超時以毫秒計算。當在超時時間內網(wǎng)卡上沒有數(shù)據(jù)到來時對網(wǎng)卡的讀操作將返回(如pcap_dispatch() or pcap_next_ex()等函數(shù)
)。還有,如果網(wǎng)卡處于統(tǒng)計模式下(請查看“統(tǒng)計和收集網(wǎng)絡數(shù)據(jù)流一節(jié)”)to_ms還定義了統(tǒng)計的時間間隔。如果該參數(shù)為0那么意味著沒
有超時控制,對網(wǎng)卡的讀操作在沒有數(shù)據(jù)到來是將永遠堵塞。如果為-1那么對網(wǎng)卡的讀操作將立即返回不管有沒有數(shù)據(jù)可讀。
程序代碼: [ 復制代碼到剪貼板 ]
#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];
/* 獲得網(wǎng)卡的列表 */
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 打印網(wǎng)卡信息 */
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); //輸入要選擇打開的網(wǎng)卡號
if(inum < 1 || inum > i) //判斷號的合法性
{
printf("\nInterface number out of range.\n");
/* Free the device list */
pcap_freealldevs(alldevs);
return -1;
}
/* 找到要選擇的網(wǎng)卡結構 */
for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);
/* 打開選擇的網(wǎng)卡 */
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;
}
/* 對每一個到來的數(shù)據(jù)包調用該函數(shù) */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
struct tm *ltime;
char timestr[16];
/* 將時間戳轉變?yōu)橐鬃x的標準格式*/
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);
}
一旦網(wǎng)卡被打開,舊可以調用pcap_dispatch() 或pcap_loop()進行數(shù)據(jù)的捕獲,這兩個函數(shù)的功能十分相似不同的是pcap_ dispatch()可以不
被阻塞,而pcap_loop()在沒有數(shù)據(jù)流到達時將阻塞。在這個簡單的例子里用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.
這兩個函數(shù)都有返回的參數(shù),一個指向某個函數(shù)(該函數(shù)用來接受數(shù)據(jù)如該程序中的packet_handler)的指針,libpcap調用該函數(shù)對每個從網(wǎng)
上到來的數(shù)據(jù)包進行處理和接收數(shù)據(jù)包。另一個參數(shù)是帶有時間戳和包長等信息的頭部,最后一個是含有所有協(xié)議頭部數(shù)據(jù)報的實際數(shù)據(jù)。注
意MAC的冗余校驗碼一般不出現(xiàn),因為當一個楨到達并被確認后網(wǎng)卡就把它刪除了,同樣需要注意的是大多數(shù)網(wǎng)卡會丟掉冗余碼出錯的數(shù)據(jù)包,
所以WinPcap一般不能夠捕獲這些出錯的數(shù)據(jù)報。
剛才的例子里從pcap_pkthdr中提取出了每個數(shù)據(jù)報的時間戳和長度并在顯示器上打印出了他們。