??? 還是按照“winpcap使用系列” http://www.smatrix.org/bbs/read.php?tid=359&fpage=4 ?一步步學習。先看文章里頭包含的頭文件。
#ifndef?WIN32
#include? < sys / socket.h >
#include? < netinet / in .h >
#else
#include? < winsock.h >
#endif
??? 接著,看下代碼。

??? 這些使用到一些控制臺程序的函數,不過對于MFC程序沒有大礙。先不說ifprint和iptos的作用,我們來看來pcap_if_t這個結構。
??? 在pcap.h中它有一個另名pcap_if,至于為什么要改名字我也不太清楚。(為了說明這個一個type?)在官方的文檔中(...\WpdPack\docs\html\structpcap__if.html)對pcap_if進行如下定義:
pcap_if *? | next |
? |
if not NULL, a pointer to the next element in the list; NULL for the last element of the list
|
char *? | name |
? |
a pointer to a string giving a name for the device to pass to
pcap_open_live()
|
char *? | description |
? |
if not NULL, a pointer to a string giving a human-readable description of the device
|
pcap_addr *? | addresses |
? |
a pointer to the first element of a list of addresses for the interface
|
u_int? | flags |
? |
PCAP_IF_ interface flags. Currently the only possible flag is
PCAP_IF_LOOPBACK
, that is set if the interface is a loopback interface.
|
??? next是對下一個設備驅動的指針,name是本驅動的名字(基本上是一些不知所云的數字),description是驅動的描述(如Realtek RTL8169/8110 Family Gigabit Ethernet NIC,這個程序員就比較清楚了),pcap_addr則是另一個pcap.h中定義的結構,最后的flags目前為0。 官方文檔對pcap_addr定義如下:
pcap_addr *? | next |
? |
if not NULL, a pointer to the next element in the list; NULL for the last element of the list
|
sockaddr *? | addr |
? |
a pointer to a struct sockaddr containing an address
|
sockaddr *? | netmask |
? |
if not NULL, a pointer to a struct sockaddr that contains the netmask corresponding to the address pointed to by addr.
|
sockaddr *? | broadaddr |
? |
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 *? | dstaddr |
? | 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 |

??? 上面的0x0600是vista的版本號,就是說當系統為XP或以下的時候用u_short的定義(其實ADDRESS_FAMILY也就是個ushort,只是換個名字)。還是回來看下它的結構,主要是后面的sa_data[14],這個參考 http://baike.baidu.com/view/2355183.html ,里面說得很詳細。下面是截圖,可以很清楚地看到前2個字節都是0,接下來的4個是有值的,后面的都是0。
??? 實際上那4個有值的字節就是32位的地址,如192.168.0.1等。
??? 現在讓我們回來看那兩個函數ifprint及iptos。在看完pcap_if_t等結構的說明后,想必你也看出來ifprint就是對pcap_if_t結構的解析,iptos就是對sockaddr的解析了吧。
??? 目前為止還沒有自己寫的東西,誰叫我還是個新手,一步步學吧。下一步是做一個可以測試在1000M網卡下WinPcap發送能力的極限能到多少的小程序,以前100M的話好像只能到60M。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
