Support struct tunnel

This commit is contained in:
luwenpeng
2024-06-17 11:41:21 +08:00
parent de4c15f43c
commit 327d6e7b14
9 changed files with 205 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
install(FILES stellar/utils.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/layer.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/tunnel.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/packet.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/session.h DESTINATION include/stellar/ COMPONENT LIBRARIES)
install(FILES stellar/stellar.h DESTINATION include/stellar/ COMPONENT LIBRARIES)

View File

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

67
include/stellar/tunnel.h Normal file
View File

@@ -0,0 +1,67 @@
#ifndef _TUNNEL_H
#define _TUNNEL_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include "layer.h"
enum tunnel_type
{
// none tunnel
TUNNEL_NONE = 0, // contain layers: IPv4 + TCP
// contain layers: IPv4 + UDP
// contain layers: IPv4
// contain layers: IPv6 + TCP
// contain layers: IPv6 + UDP
// contain layers: IPv6
// GRE tunnel
TUNNEL_GRE, // contain layers: IPv4 + GRE
// contain layers: IPv6 + GRE
// GTP tunnel
TUNNEL_GTP, // contain layers: IPv4 + UDP + GTP
// contain layers: IPv6 + UDP + GTP
// IP tunnel
TUNNEL_IPV4, // contain layers: IPv4, (next inner layer must be IPv4 / IPv6)
TUNNEL_IPV6, // contain layers: IPv6, (next inner layer must be IPv4 / IPv6)
};
#define MAX_LAYERS_PER_TUNNEL 3
struct tunnel
{
enum tunnel_type type;
int used;
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(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(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(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(pkt, i, &tunnels[i]) == 0; i++) \
/* void */; \
return num; \
}
#ifdef __cplusplus
}
#endif
#endif