Add packet parser

This commit is contained in:
luwenpeng
2023-12-15 18:57:13 +08:00
parent 7653d646d3
commit dda0cdf104
5 changed files with 4303 additions and 1 deletions

View File

@@ -1,4 +1,5 @@
add_subdirectory(timestamp) add_subdirectory(timestamp)
add_subdirectory(tuple) add_subdirectory(tuple)
add_subdirectory(packet)
add_subdirectory(session) add_subdirectory(session)
add_subdirectory(stellar) add_subdirectory(stellar)

20
src/packet/CMakeLists.txt Normal file
View File

@@ -0,0 +1,20 @@
###############################################################################
# packet
###############################################################################
add_library(packet packet.cpp)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/src/packet)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/src/tuple)
target_include_directories(packet PUBLIC ${CMAKE_SOURCE_DIR}/deps/uthash)
target_link_libraries(packet tuple)
###############################################################################
# gtest
###############################################################################
add_executable(gtest_packet gtest_packet.cpp)
target_include_directories(gtest_packet PUBLIC ${CMAKE_CURRENT_LIST_DIR})
target_link_libraries(gtest_packet packet gtest)
include(GoogleTest)
gtest_discover_tests(gtest_packet)

2706
src/packet/gtest_packet.cpp Normal file

File diff suppressed because it is too large Load Diff

1474
src/packet/packet.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -6,7 +6,108 @@ extern "C"
{ {
#endif #endif
struct packet; #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 " 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;
};
// 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 #ifdef __cpluscplus
} }