feature: support packet dump to pcap

This commit is contained in:
luwenpeng
2024-07-01 15:51:36 +08:00
parent ec38d90241
commit 761329e530
5 changed files with 123 additions and 138 deletions

View File

@@ -10,6 +10,7 @@
#include "packet_def.h"
#include "packet_layer.h"
#include "packet_parse.h"
#include "packet_dump.h"
#include "packet_utils.h"
#define MAX_BUFF_SIZE 2048
@@ -47,60 +48,6 @@ static void str_buff_push(struct str_buff *buff, const char *str)
buff->used += len;
}
static void dump_pcap(const char *file, const char *data, uint16_t len)
{
struct pcap_pkt_hdr
{
unsigned int tv_sec; // time stamp
unsigned int tv_usec; // time stamp
unsigned int caplen; // length of portion present
unsigned int len; // length this packet (off wire)
} pcap_pkt_hdr = {0};
struct pcap_file_hdr
{
unsigned int magic;
unsigned short version_major;
unsigned short version_minor;
unsigned int thiszone; // gmt to local correction
unsigned int sigfigs; // accuracy of timestamps
unsigned int snaplen; // max length saved portion of each pkt
unsigned int linktype; // data link type (LINKTYPE_*)
} pcap_file_hdr = {
.magic = 0xA1B2C3D4,
.version_major = 0x0002,
.version_minor = 0x0004,
.thiszone = 0,
.sigfigs = 0,
.snaplen = 0xFFFF,
.linktype = 1};
if (file == NULL || data == NULL || len == 0)
{
return;
}
FILE *fp = fopen(file, "w+");
if (fp == NULL)
{
return;
}
struct timeval ts = {0};
gettimeofday(&ts, NULL);
pcap_pkt_hdr.tv_sec = ts.tv_sec;
pcap_pkt_hdr.tv_usec = ts.tv_usec;
pcap_pkt_hdr.caplen = len;
pcap_pkt_hdr.len = len;
fwrite(&pcap_file_hdr, sizeof(struct pcap_file_hdr), 1, fp);
fwrite(&pcap_pkt_hdr, sizeof(struct pcap_pkt_hdr), 1, fp);
fwrite(data, 1, len, fp);
fflush(fp);
fclose(fp);
}
static void tshark_format(const struct runtime *rte, const struct packet *pkt)
{
/*
@@ -341,7 +288,7 @@ static void craft_compare(const struct runtime *rte, const struct packet *raw_pk
error_out:
char file[256] = {0};
snprintf(file, sizeof(file), "craft%lu.pcap", rte->pcap_count);
dump_pcap(file, new_pkt_data, new_pkt_len);
packet_dump(file, new_pkt_data, new_pkt_len);
packet_free(new_pkt);
}