summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2024-06-17 11:41:21 +0800
committerluwenpeng <[email protected]>2024-06-17 17:42:30 +0800
commit327d6e7b14f32e6471a946dcec1cb2966abcad43 (patch)
treecc8972263250249b34689ba99c7444fda44d430d /include
parentde4c15f43c1fee821723bd629d46193dacd7735b (diff)
Support struct tunnel
Diffstat (limited to 'include')
-rw-r--r--include/CMakeLists.txt1
-rw-r--r--include/stellar/layer.h15
-rw-r--r--include/stellar/tunnel.h67
3 files changed, 74 insertions, 9 deletions
diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt
index e231241..2e5b316 100644
--- a/include/CMakeLists.txt
+++ b/include/CMakeLists.txt
@@ -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)
diff --git a/include/stellar/layer.h b/include/stellar/layer.h
index fce1315..f77e021 100644
--- a/include/stellar/layer.h
+++ b/include/stellar/layer.h
@@ -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
diff --git a/include/stellar/tunnel.h b/include/stellar/tunnel.h
new file mode 100644
index 0000000..b1ef626
--- /dev/null
+++ b/include/stellar/tunnel.h
@@ -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