原創(chuàng)|其它|編輯:郝浩|2009-08-28 10:21:13.000|閱讀 897 次
概述:由于是Win32平臺(tái)的開發(fā),#ifndef WIN32...#else那段都是沒用的。真正要使用到的是#include
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
還是按照“winpcap使用系列” 一步步學(xué)習(xí)。先看文章里頭包含的頭文件。
由于是Win32平臺(tái)的開發(fā),#ifndef WIN32...#else那段都是沒用的。真正要使用到的是#include<winsock.h>。首先聲名一下,我做出來的測試程序是MFC的窗口程序。而MFC程序中的自動(dòng)生成的stdafx.h文件中有這么一句 #include <afxwin.h> // MFC 核心組件和標(biāo)準(zhǔn)組件,而在afxwin.h里頭有就winsock2.h的聲名。事實(shí)上winsock與winsock2是干同樣活的,不過是新舊之分,所以假如你在程序中加上#include<winsock.h>的話就必然會(huì)出現(xiàn)函數(shù)同名的錯(cuò)誤(winsock與winsock2很多同名的函數(shù))。因此在我的程序中只須聲名#include "pcap.h"。
接著,看下代碼。
這些使用到一些控制臺(tái)程序的函數(shù),不過對(duì)于MFC程序沒有大礙。先不說ifprint和iptos的作用,我們來看來pcap_if_t這個(gè)結(jié)構(gòu)。
在pcap.h中它有一個(gè)另名pcap_if,至于為什么要改名字我也不太清楚。(為了說明這個(gè)一個(gè)type?)在官方的文檔中(...\WpdPack\docs\html\structpcap__if.html)對(duì)pcap_if進(jìn)行如下定義:
* | |
if not NULL, a pointer to the next element in the list; NULL for the last element of the list | |
char * | |
a pointer to a string giving a name for the device to pass to | |
char * | |
if not NULL, a pointer to a string giving a human-readable description of the device | |
* | |
a pointer to the first element of a list of addresses for the interface | |
u_int | |
PCAP_IF_ interface flags. Currently the only possible flag is PCAP_IF_LOOPBACK, that is set if the interface is a loopback interface. |
next是對(duì)下一個(gè)設(shè)備驅(qū)動(dòng)的指針,name是本驅(qū)動(dòng)的名字(基本上是一些不知所云的數(shù)字),description是驅(qū)動(dòng)的描述(如Realtek RTL8169/8110 Family Gigabit Ethernet NIC,這個(gè)程序員就比較清楚了),pcap_addr則是另一個(gè)pcap.h中定義的結(jié)構(gòu),最后的flags目前為0。 官方文檔對(duì)pcap_addr定義如下:
* | |
if not NULL, a pointer to the next element in the list; NULL for the last element of the list | |
sockaddr * | |
a pointer to a struct sockaddr containing an address | |
sockaddr * | |
if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address pointed to by addr. | |
sockaddr * | |
if not NULL, a pointer to a struct sockaddr that contains the broadcast address corre sponding to the address pointed to by addr; may be null if the interface doesn't support broadcasts | |
sockaddr * | |
if not NULL, a pointer to a struct sockaddr that contains the destination address corre sponding to the address pointed to by addr; may be null if the interface isn't a point- to-point interface |
其中,addr為IP地址,netmask為子網(wǎng)掩碼(以上兩個(gè)都為平時(shí)常用的網(wǎng)絡(luò)連接設(shè)置),broadaddr為廣播地址(說明參照百度百科),dstaddr為目標(biāo)地址(具體使用不太清楚,什么叫點(diǎn)對(duì)點(diǎn)接口?什么情況下會(huì)有這個(gè)接口?哪位高人給我指點(diǎn)一下!)。以上4部分都是sockaddr這個(gè)結(jié)構(gòu),它的定義在ws2def.h中。
上面的0x0600是vista的版本號(hào),就是說當(dāng)系統(tǒng)為XP或以下的時(shí)候用u_short的定義(其實(shí)ADDRESS_FAMILY也就是個(gè)ushort,只是換個(gè)名字)。還是回來看下它的結(jié)構(gòu),主要是后面的sa_data[14],這個(gè)參考,里面說得很詳細(xì)。下面是截圖,可以很清楚地看到前2個(gè)字節(jié)都是0,接下來的4個(gè)是有值的,后面的都是0。
實(shí)際上那4個(gè)有值的字節(jié)就是32位的地址,如192.168.0.1等。
現(xiàn)在讓我們回來看那兩個(gè)函數(shù)ifprint及iptos。在看完pcap_if_t等結(jié)構(gòu)的說明后,想必你也看出來ifprint就是對(duì)pcap_if_t結(jié)構(gòu)的解析,iptos就是對(duì)sockaddr的解析了吧。
目前為止還沒有自己寫的東西,誰叫我還是個(gè)新手,一步步學(xué)吧。下一步是做一個(gè)可以測試在1000M網(wǎng)卡下WinPcap發(fā)送能力的極限能到多少的小程序,以前100M的話好像只能到60M。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:博客園