61 lines
2.0 KiB
C
61 lines
2.0 KiB
C
#pragma once
|
||
|
||
#ifdef __cplusplus
|
||
extern "C"
|
||
{
|
||
#endif
|
||
|
||
#include "layer.h"
|
||
|
||
enum tunnel_type
|
||
{
|
||
TUNNEL_IPV4 = 1, // contain layers: IPv4, (next inner layer must be IPv4 / IPv6)
|
||
TUNNEL_IPV6 = 2, // contain layers: IPv6, (next inner layer must be IPv4 / IPv6)
|
||
|
||
TUNNEL_GRE = 3, // contain layers: IPv4 + GRE
|
||
// contain layers: IPv6 + GRE
|
||
|
||
TUNNEL_GTP = 4, // contain layers: IPv4 + UDP + GTP
|
||
// contain layers: IPv6 + UDP + GTP
|
||
|
||
TUNNEL_VXLAN = 5, // contain layers: IPv4 + UDP + VXLAN
|
||
// contain layers: IPv6 + UDP + VXLAN
|
||
|
||
TUNNEL_L2TP = 6, // contain layers: IPv4 + UDP + L2TP
|
||
// contain layers: IPv6 + UDP + L2TP
|
||
|
||
TUNNEL_TEREDO = 7, // contain layers: IPv4 + UDP, (next inner layer must be IPv6)
|
||
};
|
||
|
||
#define MAX_LAYERS_PER_TUNNEL 3
|
||
struct tunnel
|
||
{
|
||
enum tunnel_type type;
|
||
|
||
int layer_count;
|
||
struct layer layers[MAX_LAYERS_PER_TUNNEL];
|
||
};
|
||
|
||
int packet_get_tunnel_count(const struct packet *pkt);
|
||
// return 0: success
|
||
// 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) \
|
||
{ \
|
||
int num = MIN(packet_get_tunnel_count(pkt), (sizeof(tunnels) / sizeof(tunnels[0]))); \
|
||
for (int i = 0; i < num && packet_get_tunnel_by_idx(pkt, i, &tunnels[i]) == 0; i++) \
|
||
/* void */; \
|
||
return num; \
|
||
}
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|