feature: support crafting L3 packets with L3 payload

This commit is contained in:
luwenpeng
2024-08-12 10:49:53 +08:00
parent 12975e2da7
commit 8054b9c032
7 changed files with 306 additions and 219 deletions

View File

@@ -30,7 +30,6 @@ struct runtime
uint64_t pcap_count;
int tshark_format;
int print_verbose;
int craft_compare;
};
static void str_buff_push(struct str_buff *buff, const char *str)
@@ -245,54 +244,6 @@ static void tshark_format(const struct runtime *rte, const struct packet *pkt)
buff_udp_dst.data);
}
static void craft_compare(const struct runtime *rte, const struct packet *raw_pkt)
{
struct layer layers[PACKET_MAX_LAYERS];
int layer_count = PACKET_GETALL_LAYERS(raw_pkt, layers);
struct packet *new_pkt = craft_packet_from_scratch(layers, layer_count, packet_get_payload(raw_pkt), packet_get_payload_len(raw_pkt));
if (new_pkt == NULL)
{
PRINT_RED("craft compare: failed (craft error)");
return;
}
if (rte->print_verbose)
{
packet_print(new_pkt);
}
if (rte->tshark_format)
{
tshark_format(rte, new_pkt);
}
const char *raw_pkt_data = packet_get_raw_data(raw_pkt);
const char *new_pkt_data = packet_get_raw_data(new_pkt);
uint16_t raw_pkt_len = packet_get_raw_len(raw_pkt);
uint16_t new_pkt_len = packet_get_raw_len(new_pkt);
if (raw_pkt_len != new_pkt_len)
{
PRINT_RED("craft compare: failed (length mismatch)");
goto error_out;
}
if (memcmp(raw_pkt_data, new_pkt_data, raw_pkt_len) != 0)
{
PRINT_RED("craft compare: failed (data mismatch)");
goto error_out;
}
PRINT_GREEN("craft compare: success");
error_out:
char file[256] = {0};
snprintf(file, sizeof(file), "craft%lu.pcap", rte->pcap_count);
packet_dump_pcap(new_pkt, file);
packet_free(new_pkt);
}
static void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
struct runtime *rte = (struct runtime *)user;
@@ -312,11 +263,6 @@ static void packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_ch
{
tshark_format(rte, &pkt);
}
if (rte->craft_compare)
{
craft_compare(rte, &pkt);
}
}
static void usage(char *cmd)
@@ -326,7 +272,6 @@ static void usage(char *cmd)
printf(" -f <pcap file> pcap file\n");
printf(" -t print tshark format\n");
printf(" -v print verbose info\n");
printf(" -c compare recrafted packet\n");
printf(" -h print help\n");
printf("\n");
}
@@ -335,7 +280,7 @@ int main(int argc, char **argv)
{
int opt = 0;
struct runtime rte = {0};
while ((opt = getopt(argc, argv, "f:tvch")) != -1)
while ((opt = getopt(argc, argv, "f:tvh")) != -1)
{
switch (opt)
{
@@ -348,9 +293,6 @@ int main(int argc, char **argv)
case 'v':
rte.print_verbose = 1;
break;
case 'c':
rte.craft_compare = 1;
break;
case 'h':
default:
usage(argv[0]);