2023-09-20 17:02:08 +08:00
|
|
|
#include "net_task_thread.h"
|
|
|
|
|
|
|
|
void *net_task(void *arg)
|
|
|
|
{
|
|
|
|
printf("net_task\n");
|
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
char *dev1 = "ens33"; // 第一个网卡
|
|
|
|
char *dev2 = "ens38"; // 第二个网卡
|
2023-09-20 17:02:08 +08:00
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
// 打开网卡设备
|
|
|
|
open_device(&device1, dev1);
|
|
|
|
open_device(&device2, dev2);
|
2023-09-20 17:02:08 +08:00
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
// 开始捕获数据包:
|
|
|
|
// pcap_loop: 网卡设备、捕获的数据包个数、回调函数、传递给回调函数的参数
|
|
|
|
pcap_loop(device1, -1, process_packet, NULL); // -1 表示无限循环
|
|
|
|
pcap_loop(device2, -1, process_packet, NULL); // -1 表示无限循环
|
2023-09-20 17:02:08 +08:00
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2023-09-20 17:02:08 +08:00
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
// 打开网卡设备
|
|
|
|
void open_device(pcap_t **device, char *dev_name)
|
|
|
|
{
|
|
|
|
// 打开网卡设备: 设备名、最大字节数、混杂模式、超时时间、错误信息缓冲区
|
|
|
|
*device = pcap_open_live(dev_name, MAX_BYTE, 1, 512, errbuf);
|
|
|
|
if (*device == NULL)
|
2023-09-20 17:02:08 +08:00
|
|
|
{
|
2023-09-21 09:51:03 +08:00
|
|
|
perror("pcap_open_live");
|
|
|
|
exit(-1);
|
2023-09-20 17:02:08 +08:00
|
|
|
}
|
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
// 处理数据包
|
|
|
|
libnet_context1 = libnet_init(LIBNET_LINK, dev_name, errbuf); // 初始化 libnet
|
|
|
|
if (libnet_context1 == NULL)
|
2023-09-20 17:02:08 +08:00
|
|
|
{
|
2023-09-21 09:51:03 +08:00
|
|
|
perror("libnet_init");
|
|
|
|
exit(-1);
|
2023-09-20 17:02:08 +08:00
|
|
|
}
|
2023-09-21 09:51:03 +08:00
|
|
|
}
|
2023-09-20 17:02:08 +08:00
|
|
|
|
2023-09-21 09:51:03 +08:00
|
|
|
// 处理数据包
|
|
|
|
void process_packet(u_char *arg, const struct pcap_pkthdr *pkthdr, const u_char *packet)
|
|
|
|
{
|
|
|
|
printf("process_packet\n");
|
2023-09-20 17:02:08 +08:00
|
|
|
}
|