vs2013winpacp的使用
来源:互联网 发布:js获取当前日期 函数 编辑:程序博客网 时间:2024/06/04 00:59
首先,从http://www.winpcap.org/下载winpcap.exe和Developer's Pack
安装WinPcap_4_1_3.exe
解压WpdPack_4_1_2.zip得到以下文件
2.配置VS2013。在VS2013中新建一个win32控制台工程。具体设置如下
项目属性——c/c++——常规——附加包含目录中添加解压文件的include目录,C:\Users\cql\Desktop\WpdPack\Include\,注意最后要加 \
链接器里面的附加库目录添加解压文件的lib文件目录,注意加 \ 。
还有,c/c++中预处理定义中加HAVE_REMOTE
3,在项目里引用lib文件
#pragma comment(lib,"wpcap")
#pragma comment(lib,"Packet")
#pragma comment(lib,"ws2_32")
4、添加头文件
#include "pcap.h"
#include <winsock.h>
#include"remote-ext.h"
代码:
typedef unsigned char u8_t;
typedef signed char s8_t;
typedef unsigned short u16_t;
typedef signed short s16_t;
typedef unsigned long u32_t;
typedef signed long s32_t;
struct ether_header
{
u8_t ether_dhost[6];
u8_t ether_shost[6];
u16_t ether_type;
};
void callback(u_char *user_data,
const struct pcap_pkthdr *packet_header,
const u_char *packet_content)
{
u_short ethernet_type;
struct ether_header *ethernet_protocol;
u_char *mac_string;
static int packet_number = 1;
printf("**************************************************\n");
printf("捕获第%d个以太网数据包\n", packet_number);
printf("捕获时间:\n");
//printf("%s", ctime((const time_t*)&packet_header->ts.tv_sec));
printf("数据包长度:\n");
printf("%d\n", packet_header->len);
printf("----------- 以太网协议 ------------\n");
ethernet_protocol = (struct ether_header*)packet_content;
printf("以太网类型:\n");
ethernet_type = ntohs(ethernet_protocol->ether_type);
printf("x\n", ethernet_type);
switch (ethernet_type)
{
case 0x0800:
printf("上层协议为IP协议\n");
break;
case 0x0806:
printf("上层协议是ARP协议\n");
break;
case 0x8035:
printf("上层协议为RARP协议\n");
break;
default:
break;
}
printf("源以太网地址: \n");
mac_string = ethernet_protocol->ether_shost;
for (int i = 0; i < 6;i++)
{
printf("%d:", mac_string[i]);
}
printf(" \n");
printf("目的以太网地址: \n");
mac_string = ethernet_protocol->ether_dhost;
for (int j = 0; j <6; j++)
{
printf("%d:", mac_string[j]);
}
printf(" \n");
printf("**************************************************\n");
packet_number++;
}
int _tmain(int argc, _TCHAR* argv[])
{
//定义指针
pcap_t *fp;
//开辟空间
char errbuf[PCAP_ERRBUF_SIZE];
int i = 0;
//定义指针
pcap_if_t *alldevs;
//定义指针
pcap_if_t *d;
//定义变量
int inum;
//实例结构体
struct bpf_program bpf_filter;
char bpf_filter_string[] = "tcp";
bpf_u_int32 net_mask;
bpf_u_int32 net_ip;
if (pcap_findalldevs(&alldevs, errbuf) == -1)
{
fprintf(stderr, "查找所有的devs错误: %s\n", errbuf);
return -1;
}
//打印所有的devs
for (d = alldevs; d; d = d->next)
{
printf("%d. %s", ++i, d->name);
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 -2;
}
printf("选择网卡 (1-%d):", i);
scanf_s("%d", &inum);
if (inum < 1 || inum > i)
{
printf("\n输入范围1-3.\n");
//释放所有接口
pcap_freealldevs(alldevs);
return -3;
}
for (d = alldevs, i = 0; i < inum - 1; d = d->next, i++); {
pcap_lookupnet(d->name, &net_ip, &net_mask, errbuf);
}
if ((fp = pcap_open_live(d->name, // 设备名称
65536, // 要捕获的数据包的一部分。在这种情况下没有关系
1, // 混杂模式(非零均值的混杂)
1000, // 读超时
errbuf // 错误缓冲
)) == NULL)
{
fprintf(stderr, "\n无法打开适配器。%s不支持网络 \n", d->name);
return -4;
}
if (pcap_compile(fp, &bpf_filter, bpf_filter_string, 0, net_mask) < 0)
{
fprintf(stderr, "\n无法编译包筛选器。检查语法 .\n");
return -5;
}
pcap_setfilter(fp, &bpf_filter);
if (pcap_datalink(fp) != DLT_EN10MB){
return -6;
}
//监听
pcap_loop(fp, -1, callback, NULL);
pcap_close(fp);
return 0;
}
运行结果:
- vs2013winpacp的使用
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- ->的使用
- +、-、*、/、%的使用
- &&的使用
- %:的使用
- Makefile中使用$$的使用
- Makefile中使用$$的使用
- Red5的使用--使用日志
- prefernce的使用具体使用
- 【工具使用】git 的使用
- 关于使用AsyncTaskLoader的使用
- 使用CABasicAnimation的基本使用
- 使用RocketMQ的客户端使用
- gulp的使用基本使用
- HDU 4366 Successor [树形转线形+线段树]【数据结构+技巧】
- Vulkan编程指南翻译 第五章 展现 第6节 清扫工作
- Mysql开发常见问题
- Python中使用json.loads解码字符串时出错:ValueError: Expecting property name: line 1 column 1 (char 1)
- Quick Sort In Haskell
- vs2013winpacp的使用
- toolbar中设置menu收起图标颜色以及popupwindow弹出位置
- 代码混淆的一些基本常识代码
- js中的变量名和函数名重名问题
- leetcode513 FindBottomLeftTreeValue Java
- linux的shell简介bash的基本功能
- 程序开发名词汇总
- 一行js代码破解百某Du云大文件下载限制
- Hadoop: File could only be replicated to 0 nodes instead of minReplication (=1). There are 1 datanod