Visual Studio 2022 配置 winpcap 环境
这个可以同样地应用到 npcap 中。可以说,npcap 的配置和 winpcap 完全一致。
另,说明,虽然本篇博客写的是 winpcap 的配置,但是本人在实验中使用的其实还是 npcap,另外,我也建议你使用 npcap,因为那是针对 win10 和 win11 升级的,winpcap 的很多功能在 win10 和 win11 中无法使用,比如有些适配器它根本识别不了。诸君,慎重。
之所以这篇博客没有改成 npcap 的配置教程,单纯是因为我懒。
安装软件
首先,到 winpcap 官网下载 winpcap 这个软件。注意,这个是软件,下面我们还要下载一个开发包。
然后,安装即可,注意,在安装的过程中有一个选项要勾选上:
安装开发包
即开发者所需要用到的 api。
来着开发包下载网页,然后解压到一个没有中文路径的目录即可:
测试
上面的目录解压后,有一个官方示例:
这个我们使用 VS 打开工程的话,IDE 会自动给我们升级项目,在这样的情况下,项目是可以正常运行的。
配置
下面我们来手动配置这个环境。
打开项目配置,
然后将刚刚解压的目录中的 Include 路径和 Lib 路径分别添加到包含目录和库目录中,
然后设置预处理器,即添加 WIN32;WPCAP;HAVE_REMOTE,
然后是链接器,添加这个附加库目录,注意这个附加库目录是 Lib 目录下的 x64 子目录,
然后添加两个附加依赖项,即 wpcap.lib;Packet.lib,
然后在写代码时,我们需要添加经典的预定义:
#ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
#define _CRT_SECURE_NO_WARNINGS
#endif
之后就可以愉快地写代码了。
下面是测试:
#ifdef _MSC_VER
/*
* we do not want the warnings about the old deprecated and unsecure CRT functions
* since these examples can be compiled under *nix as well
*/
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "pcap.h"
// 函数原型
void ifprint(pcap_if_t* d);
char* iptos(u_long in);
int main()
{
pcap_if_t* alldevs;
pcap_if_t* d;
char errbuf[PCAP_ERRBUF_SIZE + 1];
char source[PCAP_ERRBUF_SIZE + 1];
printf("Enter the device you want to list:\n"
"rpcap:// ==> lists interfaces in the local machine\n"
"rpcap://hostname:port ==> lists interfaces in a remote machine\n"
" (rpcapd daemon must be up and running\n"
" and it must accept 'null' authentication)\n"
"file://foldername ==> lists all pcap files in the give folder\n\n"
"Enter your choice: ");
fgets(source, PCAP_ERRBUF_SIZE, stdin);
source[PCAP_ERRBUF_SIZE] = '\0';
/* 获得接口列表 */
if (pcap_findalldevs_ex(source, NULL, &alldevs, errbuf) == -1)
{
fprintf(stderr, "Error in pcap_findalldevs: %s\n", errbuf);
exit(1);
}
/* 扫描列表并打印每一项 */
for (d = alldevs; d; d = d->next)
{
ifprint(d);
}
pcap_freealldevs(alldevs);
getchar();
return 1;
}
/* 打印所有可用信息 */
void ifprint(pcap_if_t* d)
{
pcap_addr_t* a;
/* 设备名(Name) */
printf("%s\n", d->name);
/* 设备描述(Description) */
if (d->description)
printf("\tDescription: %s\n", d->description);
/* Loopback Address*/
printf("\tLoopback: %s\n", (d->flags & PCAP_IF_LOOPBACK) ? "yes" : "no");
/* IP addresses */
for (a = d->addresses; a; a = a->next) {
printf("\tAddress Family: #%d\n", a->addr->sa_family);
switch (a->addr->sa_family)
{
case AF_INET:
printf("\tAddress Family Name: AF_INET\n");
if (a->addr)
printf("\tAddress: %s\n", iptos(((struct sockaddr_in*)a->addr)->sin_addr.s_addr));
if (a->netmask)
printf("\tNetmask: %s\n", iptos(((struct sockaddr_in*)a->netmask)->sin_addr.s_addr));
if (a->broadaddr)
printf("\tBroadcast Address: %s\n", iptos(((struct sockaddr_in*)a->broadaddr)->sin_addr.s_addr));
if (a->dstaddr)
printf("\tDestination Address: %s\n", iptos(((struct sockaddr_in*)a->dstaddr)->sin_addr.s_addr));
break;
default:
printf("\tAddress Family Name: Unknown\n");
break;
}
}
printf("\n");
}
/* 将数字类型的IP地址转换成字符串类型的 */
#define IPTOSBUFFERS 12
char* iptos(u_long in)
{
static char output[IPTOSBUFFERS][3 * 4 + 3 + 1];
static short which;
u_char* p;
p = (u_char*)∈
which = (which + 1 == IPTOSBUFFERS ? 0 : which + 1);
sprintf(output[which], "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
return output[which];
}
运行结果:
其它可能会出现的问题
如果出现如下的错误:
Error LNK2019 unresolved external symbol __imp_getnameinfo referenced in function ip6tos
那么,解决的方法很简单,只需要在链接器的输入的附加依赖中。
至于为什么要添加这个库,只需要稍微到网上了解一下这个库就行。
参考:
Visual Studio 2022 配置 winpcap 环境
http://fanyfull.github.io/2022/05/28/Visual-Studio-2022-配置-winpcap-环境/