(备份记录自用)
1.前提:在vscode上可以运行.c/.cpp文件
实例:安装mingw64
2.下载winpcap开发包:
官网地址:WinPcap · Developer Resources
3.配置vscode:
编写c_cpp_properties.json、launch.json、tasks.json:
3.1 c_cpp_properties.json:
{
"configurations": [
{
"name": "Win64",
"includePath": [
"${workspaceRoot}",
"D:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\",
"D:\\software\\WpdPack\\Include\\"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"browse": {
"path": [
"${workspaceRoot}",
"D:\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\8.1.0\\include\\",
"D:\\software\\WpdPack\\Include\\"
],
"limitSymbolsToIncludedHeaders": true
},
"compilerPath": "D:\\mingw64\\bin\\g++.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
要在两处配置好mingw64里include、WpdPack里Include文件的路径。
3.2 launch.json:
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "build and run",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "build"
}
]
}
3.3 tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build",
"command": "D:\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-LD:\\software\\\\WpdPack\\Lib\\x64",
"-ID:\\software\\WpdPack\\Include",
"-finput-charset=UTF-8",
"-fexec-charset=GBK",
"-lwsock32",
"-lwpcap"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: D:\\mingw64\\bin\\gcc.exe"
},
]
}
要配置好mingw64里gcc.exe、WpdPack里x64和Include文件的路径;
''label''设置为''build'',与launch.json中''preLaunchTask''相同。
4.例程测试:
4.1例程文件:pcap.cpp
#include <stdio.h>
#include <stdlib.h>
#include <winsock2.h>
#include <pcap.h>
#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"wpcap.lib")
int main() {
pcap_if_t *alldevs;
pcap_if_t *p;
pcap_t *devHandle;//适配器句柄
struct pcap_pkthdr *packetHeader;//与数据包捕获有关的一个Header
const unsigned char *packetData;//数据包内容的指针
char errbuf[PCAP_ERRBUF_SIZE];
if (pcap_findalldevs(&alldevs, errbuf) == -1) {//检索所有适配器
fprintf(stderr, "error:%s\n", errbuf);
return 1;
}
if (alldevs == NULL) {
printf("no devs\n");
return 1;
}
int i = 0;
for (p = alldevs; p; p = p->next) {
printf("%d:%s\n", ++i, p->name);
printf("%s\n", p->description);
}
int choose;
printf("Input the index of the chosen adapter:\n");
scanf("%d", &choose);
if (choose < 1 || choose > i) {
pcap_freealldevs(alldevs);//释放适配器列表
return 1;
}
p = alldevs;
for (i = 0; i < choose; i++)
p = p->next;
char err[PCAP_ERRBUF_SIZE];
devHandle = pcap_open_live(p->name, 65535, 1, 1000, err);//获取捕获网络数据包的数据包捕获描述符 现用pcap_open
if (devHandle == NULL) {
fprintf(stderr, "\nUnable to open the adapter\n", p->name);
pcap_freealldevs(alldevs);
return 1;
}
printf("\nCapture session started on adapter %s\n", p->name);
pcap_freealldevs(alldevs);
int ret;
while ((ret = pcap_next_ex(devHandle, &packetHeader, &packetData)) >= 0) {//开始抓包,pcap_dispatch(...)阻塞,pcap_loop(...)非阻塞
if (ret == 0)
continue;
printf("length of packet:%d\n", packetHeader->len);
//printf("addr:%s\n",inet_ntoa(((sockaddr_in *)(p->addresses->addr))->sin_addr));
//printf("net mask:%s\n",inet_ntoa(((sockaddr_in *)(p->addresses->netmask))->sin_addr));
//printf("broad addr:%s\n",inet_ntoa(((sockaddr_in *)(p->addresses->broadaddr))->sin_addr));
//printf("dest addr:%s\n",inet_ntoa(((sockaddr_in *)(p->addresses->dstaddr))->sin_addr));
}
if (ret == -1) {
printf("Error reading the packet:%s\n", pcap_geterr(devHandle));
return 1;
}
return 0;
}
4.2运行代码:
way1:
点击运行任务 task:
点击build:
(该build即为tasks.json中的"label": "build")
way2:
组合键Ctrl+Shift+B
4.3运行结果:
成功生成pap.exe文件。