summaryrefslogtreecommitdiff
path: root/common/include/packet_parser.h
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2023-08-09 18:47:16 +0800
committerluwenpeng <[email protected]>2023-08-10 18:31:38 +0800
commite34aa3f5e23d7fa0b95944269c499d5c1e7c23aa (patch)
treeaf0565991e01741c850d9479850fc58df6f9b509 /common/include/packet_parser.h
parent1063574ca0d3fea91f26b8a6bd76a2d021efd822 (diff)
TSG-16531 PacketAdapter适配容器环境,使用mrzcpd收包,通过RAW Socket注RST包v2.0.0-20230810
Diffstat (limited to 'common/include/packet_parser.h')
-rw-r--r--common/include/packet_parser.h85
1 files changed, 85 insertions, 0 deletions
diff --git a/common/include/packet_parser.h b/common/include/packet_parser.h
new file mode 100644
index 0000000..58f475c
--- /dev/null
+++ b/common/include/packet_parser.h
@@ -0,0 +1,85 @@
+#ifndef _PACKET_PARSER_H
+#define _PACKET_PARSER_H
+
+#ifdef __cpluscplus
+extern "C"
+{
+#endif
+
+#include <stdint.h>
+
+enum layer_type
+{
+ // 数据链路层
+ LAYER_TYPE_ETHER = 1 << 0,
+ LAYER_TYPE_PPP = 1 << 1,
+ LAYER_TYPE_HDLC = 1 << 2,
+ LAYER_TYPE_L2 = (LAYER_TYPE_ETHER | LAYER_TYPE_PPP | LAYER_TYPE_HDLC),
+
+ // 数据链路层 -- 隧道
+ LAYER_TYPE_VLAN = 1 << 3,
+ LAYER_TYPE_PPPOE = 1 << 4,
+ LAYER_TYPE_MPLS = 1 << 5,
+ LAYER_TYPE_L2_TUN = (LAYER_TYPE_VLAN | LAYER_TYPE_PPPOE | LAYER_TYPE_MPLS),
+
+ // 网络层
+ LAYER_TYPE_IPV4 = 1 << 6,
+ LAYER_TYPE_IPV6 = 1 << 7,
+ LAYER_TYPE_L3 = (LAYER_TYPE_IPV4 | LAYER_TYPE_IPV6),
+
+ // 网络层 -- 隧道
+
+ // 传输层
+ LAYER_TYPE_UDP = 1 << 8,
+ LAYER_TYPE_TCP = 1 << 9,
+ LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP),
+
+ // 传输层 -- 隧道
+ LAYER_TYPE_G_VXLAN = 1 << 10,
+ LAYER_TYPE_GTPV1_U = 1 << 11,
+
+ // ALL
+ LAYER_TYPE_ALL = (LAYER_TYPE_L2 | LAYER_TYPE_L2_TUN | LAYER_TYPE_L3 | LAYER_TYPE_L4 | LAYER_TYPE_G_VXLAN | LAYER_TYPE_GTPV1_U),
+
+ // UNKNOWN
+ LAYER_TYPE_UNKNOWN,
+};
+
+struct layer_record
+{
+ enum layer_type type;
+ uint16_t hdr_offset;
+ uint16_t hdr_len;
+ uint16_t pld_len;
+};
+
+struct parser_result
+{
+ struct layer_record layers[16];
+ uint16_t used;
+ uint16_t size;
+};
+
+struct packet_parser
+{
+ struct parser_result result;
+
+ const void *packet_data;
+ uint16_t packet_len;
+ uint64_t packet_id;
+};
+
+void packet_parser_init(struct packet_parser *handler);
+const void *packet_parser_parse(struct packet_parser *handler, const void *packet_data, uint16_t packet_len, uint64_t packet_id); // return most inner payload
+const struct layer_record *packet_parser_get_most_inner(struct packet_parser *handler, enum layer_type type);
+const struct layer_record *packet_parser_get_most_outer(struct packet_parser *handler, enum layer_type type);
+
+// return 4: IPv4
+// return 6: IPv6
+uint8_t gtp_next_proto(const char *data);
+
+#ifdef __cpluscplus
+}
+#endif
+
+#endif