support craft packet from scratch

This commit is contained in:
luwenpeng
2024-06-28 16:08:40 +08:00
parent da9fb7cd11
commit 3b376a3e46
11 changed files with 571 additions and 318 deletions

View File

@@ -79,12 +79,15 @@ int packet_get_layer_by_idx(const struct packet *pkt, int idx, struct layer *out
for (int i = packet_get_layer_count(pkt) - 1; i >= 0 && packet_get_layer_by_idx(pkt, i, &layer) == 0; i--)
#define PACKET_GETALL_LAYERS(pkt, layers) \
{ \
int num = MIN(packet_get_layer_count(pkt), (sizeof(layers) / sizeof(layers[0]))); \
({ \
memset(layers, 0, sizeof(layers)); \
int size = sizeof(layers) / sizeof(layers[0]); \
int count = packet_get_layer_count(pkt); \
int num = count > size ? size : count; \
for (int i = 0; i < num && packet_get_layer_by_idx(pkt, i, &layers[i]) == 0; i++) \
/* void */; \
return num; \
}
num; \
})
#ifdef __cplusplus
}

View File

@@ -38,6 +38,7 @@ uint16_t packet_get_payload_len(const struct packet *pkt);
struct packet *imitate_tcp_packet(const struct packet *origin_pkt, uint32_t tcp_seq, uint32_t tcp_ack, uint8_t tcp_flags, const char *tcp_payload, uint16_t tcp_payload_len);
struct packet *imitate_udp_packet(const struct packet *origin_pkt, const char *udp_payload, uint16_t udp_payload_len);
struct packet *craft_packet_from_scratch(const struct layer larers[], uint16_t layer_count, const char *payload, uint16_t payload_len);
#ifdef __cplusplus
}