summaryrefslogtreecommitdiff
path: root/common/include/packet_parser.h
blob: 58f475c8b6154c3fc9e37d6bb5e7715f1998c7c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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