-
Notifications
You must be signed in to change notification settings - Fork 11
/
net-headers.h
325 lines (267 loc) · 7.11 KB
/
net-headers.h
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* This file is part of fernmelder.
*
* (C) 2014 by Sebastian Krahmer, sebastian [dot] krahmer [at] gmail [dot] com
*
* fernmelder is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* fernmelder is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with fernmelder. If not, see <http://www.gnu.org/licenses/>.
*/
/* some of the header definitions have been taken from various other
* open-sourced include files
*/
#ifndef __net_headers__
#define __net_headers__
#include <sys/types.h>
#include <bits/endian.h>
#include <stdint.h>
#include <netinet/in.h>
namespace net_headers {
struct tap_header {
uint16_t flags, proto;
} __attribute__((packed));
struct icmphdr {
uint8_t type;
uint8_t code;
uint16_t sum;
union {
struct {
uint16_t id;
uint16_t sequence;
} echo;
uint32_t gateway;
struct {
uint16_t unused;
uint16_t mtu;
} frag;
} un;
};
enum eth_types_t : uint16_t {
ETH_P_IP = 0x0800
};
enum icmp_type : uint8_t {
ICMP_ECHO_REPLY = 0,
ICMP_ECHO_REQUEST = 8,
ICMP6_ECHO_REQUEST = 128,
ICMP6_ECHO_REPLY = 129
};
class udphdr {
public:
uint16_t source;
uint16_t dest;
uint16_t len;
uint16_t check;
udphdr() : source(0), dest(0), len(0), check(0) { }
};
/*
* The pseudo-header is used to calculate checksums over UDP
* and TCP packets.
*/
struct pseudohdr {
uint32_t saddr;
uint32_t daddr;
uint8_t zero;
uint8_t proto;
uint16_t len;
};
struct pseudohdr6 {
in6_addr saddr, daddr;
uint32_t len;
uint8_t zero[3];
uint8_t proto;
};
enum tcp_flags_t : uint8_t {
TH_FIN = 0x01,
TH_SYN = 0x02,
TH_RST = 0x04,
TH_PUSH = 0x08,
TH_ACK = 0x10,
TH_URG = 0x20
};
class tcphdr
{
public:
uint16_t th_sport;
uint16_t th_dport;
uint32_t th_seq;
uint32_t th_ack;
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t th_x2:4; // unused
uint8_t th_off:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t th_off:4;
uint8_t th_x2:4;
#endif
uint8_t th_flags;
uint16_t th_win;
uint16_t th_sum;
uint16_t th_urg;
};
enum tcp_opt_t : uint8_t {
TCPOPT_EOL = 0,
TCPOPT_NOP = 1,
TCPOPT_MAXSEG = 2,
TCPOLEN_MAXSEG = 4,
TCPOPT_WINDOW = 3,
TCPOLEN_WINDOW = 3,
TCPOPT_SACK_PERMITTED = 4, // Experimental
TCPOLEN_SACK_PERMITTED= 2,
TCPOPT_SACK = 5, // Experimental
TCPOPT_TIMESTAMP = 8,
TCPOLEN_TIMESTAMP = 10,
TCPOLEN_TSTAMP_APPA = (TCPOLEN_TIMESTAMP+2), // appendix A
TCPOPT_QSR = 27,
TCPOLEN_QSR = 8
};
class iphdr
{
public:
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint32_t ihl:4;
uint32_t version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint32_t version:4;
uint32_t ihl:4;
#else
# error "Please fix <bits/endian.h>"
#endif
uint8_t tos;
uint16_t tot_len;
uint16_t id;
uint16_t frag_off;
uint8_t ttl;
uint8_t protocol;
uint16_t check;
uint32_t saddr;
uint32_t daddr;
iphdr() : ihl(5), version(4), tos(0), tot_len(0), id(0), frag_off(0),
ttl(64), protocol(IPPROTO_IP), check(0), saddr(0), daddr(0) { }
};
enum ip_flags_t : uint16_t {
IP_RF = 0x8000,
IP_DF = 0x4000,
IP_MF = 0x2000,
IP_OFFMASK = 0x1fff
};
struct ip6_hdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t priority:4,
version:4;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version:4,
priority:4;
#else
#error "Please fix <asm/byteorder.h>"
#endif
uint8_t flow_lbl[3];
uint16_t payload_len;
uint8_t nexthdr;
uint8_t hop_limit;
struct in6_addr saddr;
struct in6_addr daddr;
};
struct ip6_opt {
uint8_t ip6o_type;
uint8_t ip6o_len;
};
struct icmp6_hdr {
uint8_t icmp6_type; // type field
uint8_t icmp6_code; // code field
uint16_t icmp6_cksum; // checksum field
union {
uint32_t icmp6_un_data32[1]; // type-specific field
uint16_t icmp6_un_data16[2]; // type-specific field
uint8_t icmp6_un_data8[4]; // type-specific field
} icmp6_dataun;
};
class dnshdr {
public:
uint16_t id;
#if __BYTE_ORDER == __BIG_ENDIAN
/* fields in third byte */
uint16_t qr: 1; /* response flag */
uint16_t opcode: 4; /* purpose of message */
uint16_t aa: 1; /* authoritive answer */
uint16_t tc: 1; /* truncated message */
uint16_t rd: 1; /* recursion desired */
/* fields in fourth byte */
uint16_t ra: 1; /* recursion available */
uint16_t unused :1; /* unused bits (MBZ as of 4.9.3a3) */
uint16_t ad: 1; /* authentic data from named */
uint16_t cd: 1; /* checking disabled by resolver */
uint16_t rcode :4; /* response code */
#endif
#if __BYTE_ORDER == __LITTLE_ENDIAN || __BYTE_ORDER == __PDP_ENDIAN
/* fields in third byte */
uint16_t rd :1; /* recursion desired */
uint16_t tc :1; /* truncated message */
uint16_t aa :1; /* authoritive answer */
uint16_t opcode :4; /* purpose of message */
uint16_t qr :1; /* response flag */
/* fields in fourth byte */
uint16_t rcode :4; /* response code */
uint16_t cd: 1; /* checking disabled by resolver */
uint16_t ad: 1; /* authentic data from named */
uint16_t unused :1; /* unused bits (MBZ as of 4.9.3a3) */
uint16_t ra :1; /* recursion available */
#endif
/*
union {
u_int16_t flags;
u_int16_t QR:1;
u_int16_t opcode:4;
u_int16_t AA:1;
u_int16_t TC:1;
u_int16_t RD:1;
u_int16_t RA:1;
u_int16_t zero:3;
u_int16_t rcode:4;
} u;
*/
uint16_t q_count;
uint16_t a_count;
uint16_t rra_count;
uint16_t ad_count;
dnshdr() : id (0),
q_count(0), a_count(0), rra_count(0), ad_count(0)
{
qr = 0; opcode = 0; aa = 0; tc = 0; rd = 0; ra = 0; ad = 0; cd = 0;
rcode = 0; unused = 0;
}
private: dnshdr(const dnshdr &) {};
};
enum dns_type : uint16_t {
A = 1,
NS = 2,
CNAME = 5,
SOA = 6,
PTR = 12,
HINFO = 13,
MX = 15,
TXT = 16,
AAAA = 28,
SRV = 33,
DNAME = 39,
OPT = 41,
DNSKEY = 48,
EUI64 = 109,
};
// an IPv4 RR
struct dns_rr {
// name here
uint16_t type, _class;
uint32_t ttl;
uint16_t len;
// rdata
} __attribute__((packed));
} // namespace
#endif