summaryrefslogtreecommitdiff
path: root/common/include/packet.h
diff options
context:
space:
mode:
Diffstat (limited to 'common/include/packet.h')
-rw-r--r--common/include/packet.h123
1 files changed, 123 insertions, 0 deletions
diff --git a/common/include/packet.h b/common/include/packet.h
new file mode 100644
index 0000000..9269beb
--- /dev/null
+++ b/common/include/packet.h
@@ -0,0 +1,123 @@
+#ifndef _PACKET_H
+#define _PACKET_H
+
+#ifdef __cpluscplus
+extern "C"
+{
+#endif
+
+#include <stdint.h>
+#include <stdio.h>
+#include "tuple.h"
+
+#define PACKET_MAX_LAYERS 16
+// #define PACKET_LOG_ERROR(format, ...) void(0)
+#ifndef PACKET_LOG_ERROR
+#define PACKET_LOG_ERROR(format, ...) \
+ fprintf(stderr, "ERROR (packet), " format "\n", ##__VA_ARGS__);
+#endif
+// #define PACKET_LOG_DEBUG(format, ...) void(0)
+#ifndef PACKET_LOG_DEBUG
+#define PACKET_LOG_DEBUG(format, ...) \
+ fprintf(stderr, "DEBUG (packet), " format "\n", ##__VA_ARGS__);
+#endif
+
+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_GRE = 1 << 8,
+ LAYER_TYPE_L3_TUN = (LAYER_TYPE_GRE),
+
+ // 传输层
+ LAYER_TYPE_UDP = 1 << 9,
+ LAYER_TYPE_TCP = 1 << 10,
+ LAYER_TYPE_L4 = (LAYER_TYPE_UDP | LAYER_TYPE_TCP),
+
+ // 传输层 -- 隧道
+ LAYER_TYPE_VXLAN = 1 << 11,
+ LAYER_TYPE_GTPV1_U = 1 << 12,
+
+ // ALL
+ LAYER_TYPE_ALL = (LAYER_TYPE_L2 | LAYER_TYPE_L2_TUN | LAYER_TYPE_L3 | LAYER_TYPE_L3_TUN | LAYER_TYPE_L4 | LAYER_TYPE_VXLAN | LAYER_TYPE_GTPV1_U),
+};
+
+enum ldbc_method
+{
+ LDBC_METHOD_HASH_INT_IP = 1,
+ LDBC_METHOD_HASH_EXT_IP = 2,
+ LDBC_METHOD_HASH_INT_IP_AND_EXT_IP = 3,
+ LDBC_METHOD_HASH_INNERMOST_INT_IP = 4,
+ LDBC_METHOD_HASH_INNERMOST_EXT_IP = 5,
+};
+
+struct layer_record
+{
+ enum layer_type type;
+ const char *hdr_ptr; // header pointer
+ const char *pld_ptr; // payload pointer
+ uint16_t hdr_offset; // header offset from data_ptr
+ uint16_t hdr_len; // header length
+ uint16_t pld_len; // payload length
+};
+
+struct packet
+{
+ struct layer_record layers[PACKET_MAX_LAYERS];
+ int8_t layers_used;
+ int8_t layers_size;
+
+ const char *data_ptr;
+ uint16_t data_len;
+ uint64_t zone_id;
+
+ const void *user_data;
+};
+
+// return innermost payload
+const char *packet_parse(struct packet *handler, const char *data, uint16_t len);
+void packet_print(const struct packet *handler);
+
+// return 0 : found
+// return -1 : not found
+int packet_get_innermost_tuple2(const struct packet *handler, struct tuple2 *tuple);
+int packet_get_outermost_tuple2(const struct packet *handler, struct tuple2 *tuple);
+
+// return 0 : found
+// return -1 : not found
+int packet_get_innermost_tuple4(const struct packet *handler, struct tuple4 *tuple);
+int packet_get_outermost_tuple4(const struct packet *handler, struct tuple4 *tuple);
+
+// return 0 : found
+// return -1 : not found
+int packet_get_innermost_tuple6(const struct packet *handler, struct tuple6 *tuple);
+int packet_get_outermost_tuple6(const struct packet *handler, struct tuple6 *tuple);
+
+const struct layer_record *packet_get_innermost_layer(const struct packet *handler, enum layer_type type);
+const struct layer_record *packet_get_outermost_layer(const struct packet *handler, enum layer_type type);
+
+// direction 1: E2I
+// direction 0: I2E
+uint64_t packet_get_hash(const struct packet *handler, enum ldbc_method method, int direction);
+
+#ifdef __cpluscplus
+}
+#endif
+
+#endif