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