#ifndef _PACKET_PARSER_H #define _PACKET_PARSER_H #ifdef __cpluscplus extern "C" { #endif #include 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