打开适配器并捕获数据包
? ? ? 由前兩節(jié)的鋪墊,我們已經(jīng)知道如何獲取本地適配器的信息了,接下來我們來學(xué)習(xí)一下如何打開適配器并捕獲數(shù)據(jù)包,聽起來相當(dāng)誘惑,那么咱們立刻進(jìn)入主題吧!在貼源碼之前先介紹一個(gè)將要用到的很重要的函數(shù)--pcap_open(),下面是pcap_open()在remote-ex.h中的聲明:
pcap_t *pcap_open(const char *source, int snaplen, int flags, int read_timeout, struct pcap_rmtauth *auth, char *errbuf);? ? ? 第一個(gè)參數(shù)不用多說,它表示的是設(shè)備的名稱。在獲取適配器鏈表后,通過返回?cái)?shù)據(jù)域name即可知道設(shè)備的名稱。
? ? ? 第二個(gè)參數(shù)制定要捕獲數(shù)據(jù)包中的哪些部分。技術(shù)文檔中介紹說,在一些操作系統(tǒng)中,驅(qū)動(dòng)可以被配置成只捕獲數(shù)據(jù)包的初始化部分,它的好處就是可以減少應(yīng)用程序間復(fù)制數(shù)量的量,從而提高捕獲效率。下面將要給出的實(shí)例程序中設(shè)為65535。我們知道對(duì)于使用以太網(wǎng)的局域網(wǎng)來說,最大傳輸單元為1500字節(jié),那么設(shè)為65535則能保證收到完整的數(shù)據(jù)包。
? ? ? 第三個(gè)參數(shù)是最重要的一個(gè)值,它用來指示適配器是否需要設(shè)置成混雜模式,這里引用一下技術(shù)文檔中對(duì)設(shè)置混雜模式的說明:
一般情況下,適配器只接收發(fā)給它自己的數(shù)據(jù)包, 而那些在其他機(jī)器之間通訊的數(shù)據(jù)包,將會(huì)被丟棄。 相反,如果適配器是混雜模式,那么不管這個(gè)數(shù)據(jù)包是不是發(fā)給我的,我都會(huì)去捕獲。也就是說,我會(huì)去捕獲所有的數(shù)據(jù)包。 這意味著在一個(gè)共享媒介(比如總線型以太網(wǎng)),WinPcap能捕獲其他主機(jī)的所有的數(shù)據(jù)包。 大多數(shù)用于數(shù)據(jù)捕獲的應(yīng)用程序都會(huì)將適配器設(shè)置成混雜模式,所以,我們也會(huì)在下面的范例中,使用混雜模式。
? ? ? 它的意思就是說設(shè)置混雜模式可以捕獲到所有經(jīng)過適配器的數(shù)據(jù)包,不論是不是發(fā)給機(jī)器本身的。PCAP_OPENFLAG_PROMISCUOUS這個(gè)值就是設(shè)置成混雜模式的意思。
? ? ? 第四個(gè)參數(shù)表示的是讀取數(shù)據(jù)的超時(shí)時(shí)間,單位是毫秒。意思就是說會(huì)在read_timeout時(shí)間內(nèi)對(duì)適配器的讀取操作進(jìn)行響應(yīng),不管有沒有讀到數(shù)據(jù)。這里有兩個(gè)特殊的值需要說明一下,如果將時(shí)間設(shè)置為0意味著沒有超時(shí),那么如果沒有數(shù)據(jù)到達(dá)的話,讀操作就永遠(yuǎn)不會(huì)返回;如果設(shè)置為-1則恰恰相反,不論有沒有讀到數(shù)據(jù)都會(huì)立即返回。
? ? ? 第五個(gè)參數(shù)之前提到過,它表示的是連接遠(yuǎn)程用戶的驗(yàn)證信息,由于我們不需要連接到遠(yuǎn)程用戶,這里置為NULL。
? ? ? 第六個(gè)參數(shù)是錯(cuò)誤信息緩沖,如果該函數(shù)在調(diào)用過程中出錯(cuò)則會(huì)將出錯(cuò)信息保存在緩沖中。
? ? ? 函數(shù)的返回值是一個(gè)pcap_t的指針類型,查看聲明處我們可以發(fā)現(xiàn)pcap_t實(shí)際上是pcap結(jié)構(gòu)體,而文檔上說明它是一個(gè)已打開的捕捉實(shí)例的描述符。這個(gè)結(jié)構(gòu)體對(duì)用戶來說是不透明的(我們查看pcap的聲明處是找不到的),它通過wpcap.dll提供的函數(shù),維護(hù)了它的內(nèi)容。
? ? ? 下面我們來看一下這個(gè)程序的代碼!
#define HAVE_REMOTE#include <pcap.h>
/* packet handler 函數(shù)原型 */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);
int main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
/* 獲取本機(jī)設(shè)備列表 */
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");
/* 釋放設(shè)備列表 */
pcap_freealldevs(alldevs);
return -1;
}
/* 跳轉(zhuǎn)到選中的適配器 */
for(d=alldevs, i=0; i< inum-1 ; d=d->next, i++);
/* 打開設(shè)備 */
if ( (adhandle= pcap_open(d->name, // 設(shè)備名
65535, // 65535保證能捕獲到不同數(shù)據(jù)鏈路層上的每個(gè)數(shù)據(jù)包的全部?jī)?nèi)容
PCAP_OPENFLAG_PROMISCUOUS, // 混雜模式
1000, // 讀取超時(shí)時(shí)間
NULL, // 遠(yuǎn)程機(jī)器驗(yàn)證
errbuf // 錯(cuò)誤緩沖池
) ) == NULL)
{
fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", d->name);
/* 釋放設(shè)備列表 */
pcap_freealldevs(alldevs);
return -1;
}
printf("\nlistening on %s...\n", d->description);
/* 釋放設(shè)備列表 */
pcap_freealldevs(alldevs);
/* 開始捕獲 */
pcap_loop(adhandle, 0, packet_handler, NULL);
return 0;
}
/* 每次捕獲到數(shù)據(jù)包時(shí),libpcap都會(huì)自動(dòng)調(diào)用這個(gè)回調(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];
time_t local_tv_sec;
/* 將時(shí)間戳轉(zhuǎn)換成可識(shí)別的格式 */
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);
}
? ? ? 打開設(shè)備后我們就可以進(jìn)行捕獲了,這里我們用到的函數(shù)是pcap_loop(),下面是pcap_loop()函數(shù)的聲明:
int pcap_loop ( pcap_t * p,int cnt,
pcap_handler callback,
u_char * user
)
? ? ? 引用一下文檔里面對(duì)于pcap_loop()函數(shù)的介紹:
Collect a group of packets.
pcap_loop() is similar to pcap_dispatch() except it keeps reading packets until cnt packets are processed or an error occurs. It does not return when live read timeouts occur. Rather, specifying a non-zero read timeout to pcap_open_live() and then calling pcap_dispatch() allows the reception and processing of any packets that arrive when the timeout occurs. A negative cnt causes pcap_loop() to loop forever (or at least until an error occurs). -1 is returned on an error; 0 is returned if cnt is exhausted; -2 is returned if the loop terminated due to a call to pcap_breakloop() before any packets were processed. If your application uses pcap_breakloop(), make sure that you explicitly check for -1 and -2, rather than just checking for a return value < 0.
? ? ? 大致就是說pcap_loop()函數(shù)是用來捕獲一組數(shù)據(jù)分組的。pcap_loop()函數(shù)跟pcap_dispath()函數(shù)很類似,唯一不同之處就是pcap_loop()會(huì)一直讀數(shù)據(jù)直到cnt數(shù)據(jù)被處理或者出現(xiàn)錯(cuò)誤。不論讀取時(shí)間是否超時(shí)它都不返回。當(dāng)然有一種特殊情況,就是在調(diào)用pcap_open_live()函數(shù)時(shí)指定非0的讀取超時(shí)時(shí)間,調(diào)用pcap_dispath()函數(shù)可以在超時(shí)發(fā)生時(shí)對(duì)讀到的數(shù)據(jù)進(jìn)行接收和處理。將cnt設(shè)為正數(shù)可以使pcap_loop()一直循環(huán)(或至少到錯(cuò)誤發(fā)生之前)。返回-1則出錯(cuò);返回0則說明cnt被耗盡;返回-2則是由于在數(shù)據(jù)被處理之前調(diào)用pcap_breakloop()來中斷循環(huán)。如果你的應(yīng)用程序使用了pcap_breakloop()函數(shù),你需要對(duì)返回值-1和-2進(jìn)行詳細(xì)的檢查,而不是只檢查<0的情況。(如果翻譯有誤還請(qǐng)指正!!!)
? ? ? 文檔里說得很詳細(xì),看得也是一頭霧水,也不知道究竟在說些什么。簡(jiǎn)單來講就是說,pcap_loop()只有當(dāng)cnt數(shù)據(jù)包被捕獲時(shí)才會(huì)返回,即它會(huì)在一小段時(shí)間內(nèi)阻塞網(wǎng)絡(luò)的利用。pcap_loop()函數(shù)的第三個(gè)參數(shù)很重要,它是一個(gè)回調(diào)函數(shù)的函數(shù)指針,即pcap_loop()函數(shù)返回時(shí)會(huì)自動(dòng)調(diào)用這個(gè)回調(diào)函數(shù)。這個(gè)回調(diào)函數(shù)的參數(shù)類型是規(guī)定好的:
typedef void (*pcap_handler)(u_char *, const struct pcap_pkthdr *,const u_char *);
? ? ? 這個(gè)程序當(dāng)中我們只用到了第二個(gè)參數(shù),將每一個(gè)數(shù)據(jù)包的時(shí)間戳和長(zhǎng)度從它的首部當(dāng)中解析出來,并打印在屏幕上。細(xì)節(jié)的東西就不贅述了,看一下運(yùn)行的結(jié)果:
? ? ? 輸入1并按回車,開始捕獲數(shù)據(jù)并打印:
轉(zhuǎn)載于:https://www.cnblogs.com/blacksword/archive/2012/03/10/2389153.html
總結(jié)
以上是生活随笔為你收集整理的打开适配器并捕获数据包的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL基础--层次化查询(START B
- 下一篇: Adapter (适配器模式)