WinpCap安装:
(一) 首先安装winpcap驱动,可以到winpcap官方网站上下载:http://www.winpcap.org/install/default.htm
安装winpcap驱动后:1. C:\WINDOWS\system32目录下自动生成: wpcap.dll,packet.dll
2. C:\WINDOWS\system32\drivers下自动生成:npf.sys
(二)winpcap-4.0.3配置环境:
在项目->XX属性,选择配置属性:1. c/c++->常规\附加包含目录:D:\WpdPack-4.0.3\Include
->预处理器\预处理器定义:WPCAP
2. 链接器->常规\附加库目录:D:\WpdPack-4.0.3\Lib
->输入\附加依赖项: Packet.lib wpcap.lib ws2_32.lib
网络截包:
获取截获数据包数据段长度
#include <stdlib.h>
#include <stdio.h>
#define WIN32 1
#include "pcap.h"
#include <windows.h>
#pragma comment (lib,"wpcap.lib")
#pragma comment(lib, "ws2_32.lib")
CRITICAL_SECTION cs;
typedef struct mac_hdr
{
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char d;
unsigned char e;
unsigned char f;
unsigned char a1;
unsigned char b2;
unsigned char c3;
unsigned char d4;
unsigned char e5;
unsigned char f6;
short type;
}MAC_HEADER;
typedef struct tcp_hdr //定义TCP首部
{
USHORT th_sport; //16位源端口
USHORT th_dport; //16位目的端口
ULONG th_seq; //32位序列号
ULONG th_ack; //32位确认号
UCHAR th_lenres; //4位首部长度/6位保留字