refactor: update packet get layer/tunnel API

This commit is contained in:
luwenpeng
2024-08-21 11:55:06 +08:00
parent 1aece166cd
commit a0fb57e843
7 changed files with 151 additions and 171 deletions

View File

@@ -33,7 +33,7 @@ struct tunnel
enum tunnel_type type;
int layer_count;
struct layer layers[MAX_LAYERS_PER_TUNNEL];
const struct layer *layers[MAX_LAYERS_PER_TUNNEL];
};
int packet_get_tunnel_count(const struct packet *pkt);
@@ -41,23 +41,6 @@ int packet_get_tunnel_count(const struct packet *pkt);
// return -1: failed
int packet_get_tunnel_by_idx(const struct packet *pkt, int idx, struct tunnel *out);
#define PACKET_FOREACH_TUNNEL_INORDER(pkt, tunnel) \
for (int i = 0; i < packet_get_tunnel_count(pkt) && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i++)
#define PACKET_FOREACH_TUNNEL_REVERSE(pkt, tunnel) \
for (int i = packet_get_tunnel_count(pkt) - 1; i >= 0 && packet_get_tunnel_by_idx(pkt, i, &tunnel) == 0; i--)
#define PACKET_GETALL_TUNNELS(pkt, tunnels) \
({ \
memset(tunnels, 0, sizeof(tunnels)); \
int size = sizeof(tunnels) / sizeof(tunnels[0]); \
int count = packet_get_tunnel_count(pkt); \
int num = count > size ? size : count; \
for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \
/* void */; \
num; \
})
#ifdef __cplusplus
}
#endif