您的位置:首页 > 其它

Winpcap学习第一天

2006-06-08 15:18 495 查看
#include <pcap.h>
#include <remote-ext.h>

int main() {
pcap_if_t *alldevs;
pcap_if_t *d;
int i = 0;
char errbuf[PCAP_ERRBUF_SIZE];

/* Retrieve the device list from the local machine*/
if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL /* auth is not needed */, &alldevs, errbuf) == -1)
{
printf("Error in pcap_findalldevs_ex: %s/n", errbuf);
exit(1);
}

/* Print the list */
for (d = alldevs; d != NULL; d = d->next)
{
/* Print the device's name */
printf("%d. %s", ++ i, d->name);

/* Print the device's dscription */
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 0;
}

/* We don't need any more the device list. Free it */
pcap_freealldevs(alldevs);

return 1;
}

编译的时候又遇到问题——“无法打开pcap.h”。又查看开发手册才找到解决方法:

1.安装Winpcap驱动。下载地址:http://www.winpcap.org/install/bin/WinPcap_3_1.exe

2.将Winpcap的Include,Lib目录添加进VC6.0的环境变量中;

3. 针对每一个项目,先用VC打开项目,然后在"Project->Settings",标签栏出选择"C/C++",在"Preprocessor definitions"的输入框里添加"WPCAP",再选择"Link",在"Object/library modules"的输入框里添加"wpcap.lib Packet.lib"。

再编译时终于OK了。之后,阅读代码并查看开发手册学到了下面的东西:

pcap_if是一个结构体,具体点它是一个链表的结点,他的定义如下:

struct pcap_if {

struct pcap_if *next;

char *name;

char *description;

struct pcap_addr *addresses;

u_int flags;

}

另外,在pcap.h中有一句“typedef struct pcap_if pcap_if_t;”,所以也可以用pcap_if_t代替pcap_if。

int pcap_findalldevs_ex(char * source,

struct pcap_rmtauth * auth,

pcap_if_t ** alldevs,

char * errbuf

)

这个函数是’pcap_findalldevs()’的一个超集。’pcap_findalldevs()’比较老,他只允许列出本地机器上的设备。然而,’pcap_findalldevs_ex()’除了可以列出本地及其上的设备,还可以列出远程机器上的设备。此外,它还能列出所有可用的pcap文件到指定的文件夹。’pcap_findalldevs_ex()’是平台无关的,然而它以来于标准的’pcap_findalldevs()’来获得本地机器的地址。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: