trafgen: parser: Parse IPv6 address by strict match pattern
[netsniff-ng.git] / trafgen_proto.h
blobd4427e6971a89a275ef63b11871fcdade5dd70a2
1 #ifndef TRAFGEN_PROTO_H
2 #define TRAFGEN_PROTO_H
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
8 enum proto_id {
9 PROTO_NONE = 0,
10 PROTO_ETH,
11 PROTO_PAUSE,
12 PROTO_PFC,
13 PROTO_VLAN,
14 PROTO_ARP,
15 PROTO_MPLS,
16 PROTO_IP4,
17 PROTO_ICMP4,
18 PROTO_IP6,
19 PROTO_ICMP6,
20 PROTO_UDP,
21 PROTO_TCP,
22 __PROTO_MAX,
25 enum proto_layer {
26 PROTO_L0, /* invalid layer */
27 PROTO_L2,
28 PROTO_L3,
29 PROTO_L4,
32 struct proto_field;
33 struct proto_hdr;
35 struct proto_ops {
36 enum proto_id id;
37 enum proto_layer layer;
39 void (*header_init)(struct proto_hdr *hdr);
40 void (*header_finish)(struct proto_hdr *hdr);
41 void (*field_changed)(struct proto_field *field);
42 void (*packet_finish)(struct proto_hdr *hdr);
43 void (*packet_update)(struct proto_hdr *hdr);
44 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
47 struct proto_hdr {
48 const struct proto_ops *ops;
49 uint16_t pkt_offset;
50 uint32_t pkt_id;
51 uint32_t index;
52 struct proto_field *fields;
53 size_t fields_count;
54 bool is_csum_valid;
55 size_t len;
58 enum proto_field_func_t {
59 PROTO_FIELD_FUNC_INC = 1 << 0,
60 PROTO_FIELD_FUNC_MIN = 1 << 1,
61 PROTO_FIELD_FUNC_RND = 1 << 2,
64 struct proto_field_func {
65 enum proto_field_func_t type;
66 uint32_t min;
67 uint32_t max;
68 int32_t inc;
69 uint32_t val;
71 void (*update_field)(struct proto_field *field);
74 struct proto_field {
75 uint32_t id;
76 size_t len;
77 uint32_t shift;
78 uint32_t mask;
79 /* might be negative (e.g. VLAN TPID field) */
80 int16_t offset;
82 struct proto_field_func func;
83 bool is_set;
84 uint16_t pkt_offset;
85 struct proto_hdr *hdr;
88 extern void protos_init(const char *dev);
89 extern void proto_ops_register(const struct proto_ops *ops);
91 extern struct proto_hdr *proto_header_push(enum proto_id pid);
92 extern void proto_header_finish(struct proto_hdr *hdr);
93 extern void proto_packet_finish(void);
94 extern void proto_packet_update(uint32_t idx);
96 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
97 enum proto_id pid);
99 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
100 extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
101 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
103 extern void proto_header_fields_add(struct proto_hdr *hdr,
104 const struct proto_field *fields,
105 size_t count);
107 extern bool proto_hdr_field_is_set(struct proto_hdr *hdr, uint32_t fid);
108 extern void proto_hdr_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
109 const uint8_t *bytes);
110 extern void proto_hdr_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
111 extern uint8_t proto_hdr_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
112 extern void proto_hdr_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
113 extern uint16_t proto_hdr_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
114 extern void proto_hdr_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
115 extern uint32_t proto_hdr_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
117 extern void proto_hdr_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
118 const uint8_t *bytes);
119 extern void proto_hdr_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
120 uint8_t val);
121 extern void proto_hdr_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
122 uint16_t val);
123 extern void proto_hdr_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
124 uint32_t val);
126 extern void proto_hdr_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
127 extern void proto_hdr_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
129 extern void proto_hdr_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
130 uint16_t val);
131 extern void proto_hdr_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
132 uint32_t val);
134 extern void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
135 extern void proto_hdr_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
137 extern void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
138 extern void proto_hdr_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
140 extern void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
141 extern void proto_hdr_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
143 extern void proto_field_dyn_apply(struct proto_field *field);
145 extern void proto_hdr_field_func_add(struct proto_hdr *hdr, uint32_t fid,
146 struct proto_field_func *func);
148 extern struct proto_field *proto_hdr_field_by_id(struct proto_hdr *hdr, uint32_t fid);
151 extern void proto_field_set_u8(struct proto_field *field, uint8_t val);
152 extern uint8_t proto_field_get_u8(struct proto_field *field);
153 extern void proto_field_set_u16(struct proto_field *field, uint16_t val);
154 extern uint16_t proto_field_get_u16(struct proto_field *field);
155 extern void proto_field_set_u32(struct proto_field *field, uint32_t val);
156 extern uint32_t proto_field_get_u32(struct proto_field *field);
157 extern void proto_field_set_be16(struct proto_field *field, uint16_t val);
158 extern void proto_field_set_be32(struct proto_field *field, uint32_t val);
160 #endif /* TRAFGEN_PROTO_H */