还是按照“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。