trafgen: l7: Add DNS header generation API
[netsniff-ng.git] / trafgen_proto.h
blobf3d07ba14e3b02cc265bb35299f1371d17a61f9c
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_DNS,
23 __PROTO_MAX,
26 enum proto_layer {
27 PROTO_L0, /* invalid layer */
28 PROTO_L2,
29 PROTO_L3,
30 PROTO_L4,
31 PROTO_L7,
34 struct proto_field;
35 struct proto_hdr;
37 struct proto_ops {
38 enum proto_id id;
39 enum proto_layer layer;
41 void (*header_init)(struct proto_hdr *hdr);
42 void (*header_finish)(struct proto_hdr *hdr);
43 void (*push_sub_header)(struct proto_hdr *hdr, struct proto_hdr *sub_hdr);
44 void (*field_changed)(struct proto_field *field);
45 void (*packet_finish)(struct proto_hdr *hdr);
46 void (*packet_update)(struct proto_hdr *hdr);
47 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
50 struct proto_hdr {
51 const struct proto_ops *ops;
52 struct proto_hdr *parent;
53 struct proto_hdr **sub_headers;
54 uint32_t sub_headers_count;
55 uint16_t pkt_offset;
56 uint32_t pkt_id;
57 uint32_t index;
58 struct proto_field *fields;
59 size_t fields_count;
60 bool is_csum_valid;
61 uint32_t id;
62 size_t len;
65 enum proto_field_func_t {
66 PROTO_FIELD_FUNC_INC = 1 << 0,
67 PROTO_FIELD_FUNC_MIN = 1 << 1,
68 PROTO_FIELD_FUNC_RND = 1 << 2,
71 struct proto_field_func {
72 enum proto_field_func_t type;
73 uint32_t min;
74 uint32_t max;
75 int32_t inc;
76 uint16_t offset;
77 uint32_t val;
78 size_t len;
80 void (*update_field)(struct proto_field *field);
83 struct proto_field {
84 uint32_t id;
85 size_t len;
86 uint32_t shift;
87 uint32_t mask;
88 /* might be negative (e.g. VLAN TPID field) */
89 int16_t offset;
91 struct proto_field_func func;
92 bool is_set;
93 uint16_t pkt_offset;
94 struct proto_hdr *hdr;
97 extern void protos_init(const char *dev);
98 extern void proto_ops_register(const struct proto_ops *ops);
100 extern struct proto_hdr *proto_header_push(enum proto_id pid);
101 extern void proto_header_finish(struct proto_hdr *hdr);
102 extern void proto_packet_finish(void);
103 extern void proto_packet_update(uint32_t idx);
105 extern struct proto_hdr *proto_hdr_push_sub_header(struct proto_hdr *hdr, int id);
106 extern void proto_hdr_move_sub_header(struct proto_hdr *hdr, struct proto_hdr *from,
107 struct proto_hdr *to);
109 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
110 enum proto_id pid);
112 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
113 extern struct proto_hdr *proto_upper_header(struct proto_hdr *hdr);
114 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
116 extern void proto_header_fields_add(struct proto_hdr *hdr,
117 const struct proto_field *fields,
118 size_t count);
120 extern bool proto_hdr_field_is_set(struct proto_hdr *hdr, uint32_t fid);
121 extern void proto_hdr_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
122 const uint8_t *bytes, size_t len);
123 extern void proto_hdr_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
124 extern uint8_t proto_hdr_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
125 extern void proto_hdr_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
126 extern uint16_t proto_hdr_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
127 extern void proto_hdr_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
128 extern uint32_t proto_hdr_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
130 extern void proto_hdr_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
131 const uint8_t *bytes, size_t len);
132 extern void proto_hdr_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
133 uint8_t val);
134 extern void proto_hdr_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
135 uint16_t val);
136 extern void proto_hdr_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
137 uint32_t val);
139 extern void proto_hdr_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
140 extern void proto_hdr_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
142 extern void proto_hdr_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
143 uint16_t val);
144 extern void proto_hdr_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
145 uint32_t val);
147 extern void proto_hdr_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
148 extern void proto_hdr_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
150 extern void proto_hdr_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
151 extern void proto_hdr_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
153 extern void proto_hdr_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
154 extern void proto_hdr_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
156 extern void proto_hdr_field_set_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
157 extern void proto_hdr_field_set_default_string(struct proto_hdr *hdr, uint32_t fid, const char *str);
159 extern void proto_field_dyn_apply(struct proto_field *field);
161 extern struct proto_field *proto_hdr_field_by_id(struct proto_hdr *hdr, uint32_t fid);
164 extern void proto_field_set_u8(struct proto_field *field, uint8_t val);
165 extern uint8_t proto_field_get_u8(struct proto_field *field);
166 extern void proto_field_set_u16(struct proto_field *field, uint16_t val);
167 extern uint16_t proto_field_get_u16(struct proto_field *field);
168 extern void proto_field_set_u32(struct proto_field *field, uint32_t val);
169 extern uint32_t proto_field_get_u32(struct proto_field *field);
170 extern void proto_field_set_be16(struct proto_field *field, uint16_t val);
171 extern void proto_field_set_be32(struct proto_field *field, uint32_t val);
172 extern void proto_field_set_bytes(struct proto_field *field, const uint8_t *bytes, size_t len);
173 extern void proto_field_set_string(struct proto_field *field, const char *str);
174 extern void proto_field_set_default_string(struct proto_field *field, const char *str);
176 extern void proto_field_func_add(struct proto_field *field,
177 struct proto_field_func *func);
179 extern const char *proto_dev_get(void);
181 #endif /* TRAFGEN_PROTO_H */