1.什么是winpcap:
winpcap(windows packet capture)是windows平台下一个免费,公共的网络访问系统。开发winpcap这个项目的目的在于为win32应用程序提供访问网络底层的能力。
文档手册:http://www.ferrisxu.com/WinPcap/html/index.html
2.功能:
1> 捕获原始数据包,包括在共享网络上各主机发送/接收的以及相互之间交换的数据
包; 2> 在数据包发往 应用程序 之前,按照自定义的规则将某些特殊的数据包过滤掉; 3> 在网络上发送原始的数据包; 4> 收集网络通信过程中的统计信息。3.环境配置:
步骤1:安装驱动程序。下载WinPcap Driver和DLL并安装,安装后重启机器。
步骤2:下载wpdpack(Developer’s pack)。解压后会看到其中包含了docs、Include、lib、Examples等文件夹。
步骤3:在VC中设定Include目录及Library目录。具体做法:打开VC后,Tools->Option->Directories,在include files中添加……\wpdpack\Include目录(步骤2中得到的);在Library files中添加……\wpdpack\Lib目录。
步骤4:Project->settings->Link,在Object/library modules中加上wpcap.lib。
4.实验:
打开vc++,新建一个win32 console application;
新建一个cpp文件;
代码:
#include "pcap.h"
void main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int i=0;
char errbuf[PCAP_ERRBUF_SIZE];
/* 这个API用来获得网卡 的列表 */
if (pcap_findalldevs(&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("\n\t\tDescription: (%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);
}
运行结果:
5.代码说明:
代码完成的功能是获取设备的列表:
int pcap_findalldevs_ex | ( | char * | source, | |
struct pcap_rmtauth * | auth, | |||
pcap_if_t ** | alldevs, | |||
char * | errbuf | |||
) |
Create a list of network devices that can be opened with pcap_open().
more details:http://www.ferrisxu.com/WinPcap/html/index.html
gg