rename packet_get_layers_number to packet_get_layer_count

This commit is contained in:
luwenpeng
2024-06-13 10:11:30 +08:00
parent 01958b56c5
commit 020c8303c6
5 changed files with 8 additions and 113 deletions

View File

@@ -81,7 +81,7 @@ enum packet_direction packet_get_direction(const struct packet *pkt);
uint64_t packet_get_session_id(const struct packet *pkt);
void packet_prepend_sid_list(struct packet *pkt, const struct sid_list *list);
int8_t packet_get_layers_number(const struct packet *pkt);
int8_t packet_get_layer_count(const struct packet *pkt);
const struct packet_layer *packet_get_layer(const struct packet *pkt, int8_t idx);
const char *packet_get_data(const struct packet *pkt);
@@ -93,27 +93,6 @@ uint16_t packet_get_payload_len(const struct packet *pkt);
void packet_set_action(struct packet *pkt, enum packet_action action);
enum packet_action packet_get_action(const struct packet *pkt);
/*
******************************************************************************
* Example: foreach layer in packet
******************************************************************************
*
* // inorder
* int8_t layers = packet_get_layers_number(pkt);
* for (int8_t i = 0; i < layers; i++)
* {
* const struct packet_layer *layer = packet_get_layer(pkt, i);
* printf("layer[%d]: type=%d, hdr_offset=%d, hdr_len=%d, pld_len=%d\n", i, layer->type, layer->hdr_offset, layer->hdr_len, layer->pld_len);
* }
*
* // reverse
* for (int8_t i = layers - 1; i >= 0; i--)
* {
* const struct packet_layer *layer = packet_get_layer(pkt, i);
* printf("layer[%d]: type=%d, hdr_offset=%d, hdr_len=%d, pld_len=%d\n", i, layer->type, layer->hdr_offset, layer->hdr_len, layer->pld_len);
* }
*/
struct address
{
uint8_t family; // AF_INET or AF_INET6
@@ -129,7 +108,7 @@ static inline int packet_get_addr(const struct packet *pkt, struct address *src_
const struct ip *ip4_hdr = NULL;
const struct ip6_hdr *ip6_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
int8_t num = packet_get_layer_count(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
@@ -173,7 +152,7 @@ static inline int packet_get_port(const struct packet *pkt, uint16_t *src_port,
const struct tcphdr *tcp_hdr = NULL;
const struct udphdr *udp_hdr = NULL;
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
int8_t num = packet_get_layer_count(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
@@ -196,86 +175,6 @@ static inline int packet_get_port(const struct packet *pkt, uint16_t *src_port,
return -1;
}
static inline int packet_get_ip_hdr(const struct packet *pkt, struct ip *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type == LAYER_PROTO_IPV4)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip));
}
return 0;
}
}
return -1;
}
static inline int packet_get_ip6_hdr(const struct packet *pkt, struct ip6_hdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type == LAYER_PROTO_IPV6)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct ip6_hdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_tcp_hdr(const struct packet *pkt, struct tcphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type == LAYER_PROTO_TCP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct tcphdr));
}
return 0;
}
}
return -1;
}
static inline int packet_get_udp_hdr(const struct packet *pkt, struct udphdr *hdr)
{
const struct packet_layer *layer = NULL;
int8_t num = packet_get_layers_number(pkt);
for (int8_t i = num - 1; i >= 0; i--)
{
layer = packet_get_layer(pkt, i);
if (layer->type == LAYER_PROTO_UDP)
{
if (hdr != NULL)
{
memcpy(hdr, layer->hdr_ptr, sizeof(struct udphdr));
}
return 0;
}
}
return -1;
}
#ifdef __cplusplus
}
#endif