trafgen: ipv4: Do not use user-provided 'ihl' field to calculate csum
[netsniff-ng-new.git] / trafgen_proto.h
blob72cd9f7bbb5bbe1ba86eab6c8a2f57cb8e20bf4f
1 #ifndef TRAFGEN_PROTO_H
2 #define TRAFGEN_PROTO_H
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
8 struct proto_ctx {
9 const char *dev;
12 enum proto_id {
13 PROTO_NONE,
14 PROTO_ETH,
15 PROTO_VLAN,
16 PROTO_ARP,
17 PROTO_MPLS,
18 PROTO_IP4,
19 PROTO_ICMP4,
20 PROTO_IP6,
21 PROTO_ICMP6,
22 PROTO_UDP,
23 PROTO_TCP,
26 enum proto_layer {
27 PROTO_L0, /* invalid layer */
28 PROTO_L2,
29 PROTO_L3,
30 PROTO_L4,
33 struct proto_field {
34 uint32_t id;
35 size_t len;
36 uint32_t shift;
37 uint32_t mask;
38 /* might be negative (e.g. VLAN TPID field) */
39 int16_t offset;
41 bool is_set;
42 uint16_t pkt_offset;
45 struct proto_hdr {
46 enum proto_id id;
47 enum proto_layer layer;
49 struct proto_hdr *next;
50 struct proto_ctx *ctx;
51 uint16_t pkt_offset;
52 struct proto_field *fields;
53 size_t fields_count;
54 size_t len;
56 void (*header_init)(struct proto_hdr *hdr);
57 void (*header_finish)(struct proto_hdr *hdr);
58 void (*packet_finish)(struct proto_hdr *hdr);
59 void (*set_next_proto)(struct proto_hdr *hdr, enum proto_id pid);
62 extern void protos_init(const char *dev);
63 extern void proto_header_register(struct proto_hdr *hdr);
65 extern struct proto_hdr *proto_header_init(enum proto_id pid);
66 extern void proto_header_finish(struct proto_hdr *hdr);
67 extern void proto_packet_finish(void);
68 extern struct proto_hdr *proto_lower_default_add(struct proto_hdr *hdr,
69 enum proto_id pid);
71 extern struct proto_hdr *proto_lower_header(struct proto_hdr *hdr);
72 extern uint8_t *proto_header_ptr(struct proto_hdr *hdr);
74 extern void proto_header_fields_add(struct proto_hdr *hdr,
75 const struct proto_field *fields,
76 size_t count);
78 extern bool proto_field_is_set(struct proto_hdr *hdr, uint32_t fid);
79 extern void proto_field_set_bytes(struct proto_hdr *hdr, uint32_t fid,
80 uint8_t *bytes);
81 extern void proto_field_set_u8(struct proto_hdr *hdr, uint32_t fid, uint8_t val);
82 extern uint8_t proto_field_get_u8(struct proto_hdr *hdr, uint32_t fid);
83 extern void proto_field_set_u16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
84 extern uint16_t proto_field_get_u16(struct proto_hdr *hdr, uint32_t fid);
85 extern void proto_field_set_u32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
86 extern uint32_t proto_field_get_u32(struct proto_hdr *hdr, uint32_t fid);
88 extern void proto_field_set_default_bytes(struct proto_hdr *hdr, uint32_t fid,
89 uint8_t *bytes);
90 extern void proto_field_set_default_u8(struct proto_hdr *hdr, uint32_t fid,
91 uint8_t val);
92 extern void proto_field_set_default_u16(struct proto_hdr *hdr, uint32_t fid,
93 uint16_t val);
94 extern void proto_field_set_default_u32(struct proto_hdr *hdr, uint32_t fid,
95 uint32_t val);
97 extern void proto_field_set_be16(struct proto_hdr *hdr, uint32_t fid, uint16_t val);
98 extern void proto_field_set_be32(struct proto_hdr *hdr, uint32_t fid, uint32_t val);
100 extern void proto_field_set_default_be16(struct proto_hdr *hdr, uint32_t fid,
101 uint16_t val);
102 extern void proto_field_set_default_be32(struct proto_hdr *hdr, uint32_t fid,
103 uint32_t val);
105 extern void proto_field_set_dev_mac(struct proto_hdr *hdr, uint32_t fid);
106 extern void proto_field_set_default_dev_mac(struct proto_hdr *hdr, uint32_t fid);
108 extern void proto_field_set_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
109 extern void proto_field_set_default_dev_ipv4(struct proto_hdr *hdr, uint32_t fid);
111 extern void proto_field_set_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
112 extern void proto_field_set_default_dev_ipv6(struct proto_hdr *hdr, uint32_t fid);
114 #endif /* TRAFGEN_PROTO_H */