10 #ifndef HAKA_PROTO_IPV4_IPV4_H 11 #define HAKA_PROTO_IPV4_IPV4_H 15 #include <haka/compiler.h> 18 #include <haka/lua/object.h> 19 #include <haka/container/list2.h> 25 #define SWAP_TO_IPV4(type, x) SWAP_TO_BE(type, x) 26 #define SWAP_FROM_IPV4(type, x) SWAP_FROM_BE(type, x) 27 #define IPV4_GET_BIT(type, v, i) GET_BIT(SWAP_FROM_BE(type, v), i) 28 #define IPV4_SET_BIT(type, v, i, x) SWAP_TO_BE(type, SET_BIT(SWAP_FROM_BE(type, v), i, x)) 29 #define IPV4_GET_BITS(type, v, r) GET_BITS(SWAP_FROM_BE(type, v), r) 30 #define IPV4_SET_BITS(type, v, r, x) SWAP_TO_BE(type, SET_BITS(SWAP_FROM_BE(type, v), r, x)) 32 #define IPV4_CHECK(ip, ...) if (!(ip) || !(ip)->packet) { error("invalid ipv4 packet"); return __VA_ARGS__; } 34 #define IPV4_FLAG_RB 15 35 #define IPV4_FLAG_DF 15-1 36 #define IPV4_FLAG_MF 15-2 37 #define IPV4_FLAG_BITS 16-3, 16 38 #define IPV4_FRAGMENTOFFSET_BITS 0, 16-3 39 #define IPV4_FRAGMENTOFFSET_OFFSET 3 40 #define IPV4_HDR_LEN_OFFSET 2 44 #ifdef HAKA_LITTLEENDIAN 68 struct lua_object lua_object;
69 struct list2_elem frag_list;
74 size_t reassembled_offset;
75 bool invalid_checksum:1;
81 struct ipv4 *ipv4_reassemble(
struct ipv4 *ip);
82 struct ipv4 *ipv4_create(
struct packet *packet);
83 struct packet *ipv4_forge(
struct ipv4 *ip);
84 struct ipv4_header *ipv4_header(
struct ipv4 *ip,
bool write);
85 void ipv4_release(
struct ipv4 *ip);
86 bool ipv4_verify_checksum(
struct ipv4 *ip);
87 void ipv4_compute_checksum(
struct ipv4 *ip);
88 size_t ipv4_get_payload_length(
struct ipv4 *ip);
89 const char *ipv4_get_proto_dissector(
struct ipv4 *ip);
90 void ipv4_register_proto_dissector(
uint8 proto,
const char *dissector);
91 void ipv4_action_drop(
struct ipv4 *ip);
93 struct checksum_partial {
99 extern struct checksum_partial checksum_partial_init;
101 int16 inet_checksum(
const uint8 *ptr,
size_t size);
103 void inet_checksum_partial(
struct checksum_partial *csum,
const uint8 *ptr,
size_t size);
104 void inet_checksum_vbuffer_partial(
struct checksum_partial *csum,
struct vbuffer_sub *buf);
105 int16 inet_checksum_reduce(
struct checksum_partial *csum);
108 #define IPV4_GETSET_FIELD(type, field) \ 109 INLINE type ipv4_get_##field(struct ipv4 *ip) { IPV4_CHECK(ip, 0); return SWAP_FROM_IPV4(type, ipv4_header(ip, false)->field); } \ 110 INLINE void ipv4_set_##field(struct ipv4 *ip, type v) { IPV4_CHECK(ip); \ 111 struct ipv4_header *header = ipv4_header(ip, true); if (header) { header->field = SWAP_TO_IPV4(type, v); } } 113 IPV4_GETSET_FIELD(
uint8, version);
114 IPV4_GETSET_FIELD(
uint8, tos);
115 IPV4_GETSET_FIELD(
uint16, len);
116 IPV4_GETSET_FIELD(
uint16,
id);
117 IPV4_GETSET_FIELD(
uint8, ttl);
118 IPV4_GETSET_FIELD(
uint8, proto);
119 IPV4_GETSET_FIELD(
uint16, checksum);
123 INLINE
uint8 ipv4_get_hdr_len(
struct ipv4 *ip)
126 return ipv4_header(ip,
false)->hdr_len << IPV4_HDR_LEN_OFFSET;
129 INLINE
void ipv4_set_hdr_len(
struct ipv4 *ip,
uint8 v)
132 struct ipv4_header *header = ipv4_header(ip,
true);
134 ipv4_header(ip,
true)->hdr_len = v >> IPV4_HDR_LEN_OFFSET;
137 INLINE
uint16 ipv4_get_frag_offset(
struct ipv4 *ip)
139 struct ipv4_header *header;
141 header = ipv4_header(ip,
false);
142 return (IPV4_GET_BITS(
uint16, header->fragment, IPV4_FRAGMENTOFFSET_BITS)) << IPV4_FRAGMENTOFFSET_OFFSET;
145 INLINE
void ipv4_set_frag_offset(
struct ipv4 *ip,
uint16 v)
148 struct ipv4_header *header = ipv4_header(ip,
true);
150 header->fragment = IPV4_SET_BITS(
uint16, header->fragment, IPV4_FRAGMENTOFFSET_BITS, v >> IPV4_FRAGMENTOFFSET_OFFSET);
155 struct ipv4_header *header;
157 header = ipv4_header(ip,
false);
158 return IPV4_GET_BITS(
uint16, header->fragment, IPV4_FLAG_BITS);
161 INLINE
void ipv4_set_flags(
struct ipv4 *ip,
uint16 v)
164 struct ipv4_header *header = ipv4_header(ip,
true);
166 header->fragment = IPV4_SET_BITS(
uint16, header->fragment, IPV4_FLAG_BITS, v);
169 #define IPV4_GETSET_FLAG(name, flag) \ 170 INLINE bool ipv4_get_flags_##name(struct ipv4 *ip) { \ 171 struct ipv4_header *header; \ 173 header = ipv4_header(ip, false); \ 174 return IPV4_GET_BIT(uint16, header->fragment, flag); \ 176 INLINE void ipv4_set_flags_##name(struct ipv4 *ip, bool v) { \ 178 struct ipv4_header *header = ipv4_header(ip, true); \ 180 header->fragment = IPV4_SET_BIT(uint16, header->fragment, flag, v); \ 184 IPV4_GETSET_FLAG(df, IPV4_FLAG_DF);
185 IPV4_GETSET_FLAG(mf, IPV4_FLAG_MF);
186 IPV4_GETSET_FLAG(rb, IPV4_FLAG_RB);
uint32 ipv4addr
Definition: ipv4-addr.h:25
unsigned HAKA_16BIT_TYPE uint16
Definition: types.h:28
Definition: vbuffer_stream.h:23
HAKA_16BIT_TYPE int16
Definition: types.h:23
HAKA_32BIT_TYPE int32
Definition: types.h:24
unsigned char uint8
Definition: types.h:27