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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
use super::super::*;
use std::net::Ipv6Addr;
use std::slice::from_raw_parts;
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct Ipv6Header {
pub traffic_class: u8,
pub flow_label: u32,
pub payload_length: u16,
pub next_header: u8,
pub hop_limit: u8,
pub source: [u8;16],
pub destination: [u8;16]
}
impl SerializedSize for Ipv6Header {
const SERIALIZED_SIZE:usize = 40;
}
impl Ipv6Header {
#[deprecated(
since = "0.10.1",
note = "Renamed to `Ipv6Header::from_slice`"
)]
#[inline]
pub fn read_from_slice(slice: &[u8]) -> Result<(Ipv6Header, &[u8]), ReadError> {
Ipv6Header::from_slice(slice)
}
#[inline]
pub fn from_slice(slice: &[u8]) -> Result<(Ipv6Header, &[u8]), ReadError> {
Ok((
Ipv6HeaderSlice::from_slice(slice)?.to_header(),
&slice[Ipv6Header::SERIALIZED_SIZE..]
))
}
pub fn read<T: io::Read + io::Seek + Sized>(reader: &mut T) -> Result<Ipv6Header, ReadError> {
let mut value : [u8;1] = [0;1];
reader.read_exact(&mut value)?;
let version = value[0] >> 4;
if 6 != version {
return Err(ReadError::Ipv6UnexpectedVersion(version));
}
match Ipv6Header::read_without_version(reader, value[0] & 0xf) {
Ok(value) => Ok(value),
Err(err) => Err(ReadError::IoError(err))
}
}
pub fn read_without_version<T: io::Read + io::Seek + Sized>(reader: &mut T, version_rest: u8) -> Result<Ipv6Header, io::Error> {
let mut buffer : [u8;8+32-1] = [0;8+32-1];
reader.read_exact(&mut buffer[..])?;
Ok(Ipv6Header{
traffic_class: (version_rest << 4) | (buffer[0] >> 4),
flow_label: u32::from_be_bytes([0, buffer[0] & 0xf, buffer[1], buffer[2]]),
payload_length: u16::from_be_bytes([buffer[3], buffer[4]]),
next_header: buffer[5],
hop_limit: buffer[6],
source: [
buffer[7], buffer[8], buffer[9], buffer[10],
buffer[11], buffer[12], buffer[13], buffer[14],
buffer[15], buffer[16], buffer[17], buffer[18],
buffer[19], buffer[20], buffer[21], buffer[22],
],
destination: [
buffer[23], buffer[24], buffer[25], buffer[26],
buffer[27], buffer[28], buffer[29], buffer[30],
buffer[31], buffer[32], buffer[33], buffer[34],
buffer[35], buffer[36], buffer[37], buffer[38],
]
})
}
pub fn skip_header_extension_in_slice(slice: &[u8], next_header: u8) -> Result<(u8, &[u8]), ReadError> {
use crate::ip_number::*;
if slice.len() >= 2 {
let len = match next_header {
IPV6_FRAG => 8,
AUTH => (usize::from(slice[1]) + 2)*4,
IPV6_HOP_BY_HOP | IPV6_ROUTE | IPV6_DEST_OPTIONS | MOBILITY | HIP | SHIM6 => {
(usize::from(slice[1]) + 1)*8
},
_ => return Ok((next_header, slice))
};
if slice.len() < len {
Err(ReadError::UnexpectedEndOfSlice(len))
} else {
Ok((slice[0], &slice[len..]))
}
} else {
Err(ReadError::UnexpectedEndOfSlice(2))
}
}
pub fn is_skippable_header_extension(ip_protocol_number: u8) -> bool {
use crate::ip_number::*;
matches!(ip_protocol_number, IPV6_HOP_BY_HOP | IPV6_ROUTE | IPV6_FRAG | AUTH | IPV6_DEST_OPTIONS | MOBILITY | HIP | SHIM6)
}
pub fn skip_all_header_extensions_in_slice(slice: &[u8], next_header: u8) -> Result<(u8, &[u8]), ReadError> {
let mut next_header = next_header;
let mut rest = slice;
for _i in 0..IPV6_MAX_NUM_HEADER_EXTENSIONS {
let (n_id, n_rest) = Ipv6Header::skip_header_extension_in_slice(rest, next_header)?;
if n_rest.len() == rest.len() {
return Ok((next_header, rest))
} else {
next_header = n_id;
rest = n_rest;
}
}
if Ipv6Header::is_skippable_header_extension(next_header) {
Err(ReadError::Ipv6TooManyHeaderExtensions)
} else {
Ok((next_header, rest))
}
}
pub fn skip_header_extension<T: io::Read + io::Seek + Sized>(reader: &mut T, next_header: u8) -> Result<u8, io::Error> {
use crate::ip_number::*;
let (next_header, rest_length) = match next_header {
IPV6_FRAG => {
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
(buf[0], 7)
},
AUTH => {
let mut buf = [0; 2];
reader.read_exact(&mut buf)?;
(buf[0], i64::from(buf[1])*4 + 6)
},
IPV6_HOP_BY_HOP | IPV6_ROUTE | IPV6_DEST_OPTIONS | MOBILITY | HIP | SHIM6 => {
let mut buf = [0; 2];
reader.read_exact(&mut buf)?;
(buf[0],i64::from(buf[1])*8 + 6)
},
_ => return Ok(next_header)
};
reader.seek(io::SeekFrom::Current(rest_length - 1))?;
{
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
}
Ok(next_header)
}
pub fn skip_all_header_extensions<T: io::Read + io::Seek + Sized>(reader: &mut T, next_header: u8) -> Result<u8, ReadError> {
let mut next_header = next_header;
for _i in 0..IPV6_MAX_NUM_HEADER_EXTENSIONS {
if Ipv6Header::is_skippable_header_extension(next_header)
{
next_header = Ipv6Header::skip_header_extension(reader, next_header)?;
} else {
return Ok(next_header);
}
}
if Ipv6Header::is_skippable_header_extension(next_header) {
Err(ReadError::Ipv6TooManyHeaderExtensions)
} else {
Ok(next_header)
}
}
pub fn write<T: io::Write + Sized>(&self, writer: &mut T) -> Result<(), WriteError> {
use crate::ErrorField::*;
fn max_check_u32(value: u32, max: u32, field: ErrorField) -> Result<(), WriteError> {
if value <= max {
Ok(())
} else {
Err(
WriteError::ValueError(
ValueError::U32TooLarge{
value,
max,
field
}
)
)
}
}
max_check_u32(self.flow_label, 0xfffff, Ipv6FlowLabel)?;
let flow_label_be = self.flow_label.to_be_bytes();
let payload_len_be = self.payload_length.to_be_bytes();
let header_raw = [
(6 << 4) | (self.traffic_class >> 4),
(self.traffic_class << 4) | flow_label_be[1],
flow_label_be[2],
flow_label_be[3],
payload_len_be[0],
payload_len_be[1],
self.next_header,
self.hop_limit,
self.source[0], self.source[1], self.source[2], self.source[3],
self.source[4], self.source[5], self.source[6], self.source[7],
self.source[8], self.source[9], self.source[10], self.source[11],
self.source[12], self.source[13], self.source[14], self.source[15],
self.destination[0], self.destination[1], self.destination[2], self.destination[3],
self.destination[4], self.destination[5], self.destination[6], self.destination[7],
self.destination[8], self.destination[9], self.destination[10], self.destination[11],
self.destination[12], self.destination[13], self.destination[14], self.destination[15],
];
writer.write_all(&header_raw)?;
Ok(())
}
#[inline]
pub fn header_len(&self) -> usize {
Ipv6Header::SERIALIZED_SIZE
}
pub fn set_payload_length(&mut self, size: usize) -> Result<(), ValueError> {
const MAX_PAYLOAD_LENGTH: usize = std::u16::MAX as usize;
if MAX_PAYLOAD_LENGTH < size {
return Err(ValueError::Ipv6PayloadLengthTooLarge(size));
}
self.payload_length = size as u16;
Ok(())
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Ipv6HeaderSlice<'a> {
slice: &'a [u8]
}
impl<'a> Ipv6HeaderSlice<'a, > {
pub fn from_slice(slice: &'a[u8]) -> Result<Ipv6HeaderSlice<'a>, ReadError> {
use crate::ReadError::*;
if slice.len() < Ipv6Header::SERIALIZED_SIZE {
return Err(UnexpectedEndOfSlice(Ipv6Header::SERIALIZED_SIZE));
}
let version = unsafe {
slice.get_unchecked(0) >> 4
};
if 6 != version {
return Err(Ipv6UnexpectedVersion(version));
}
Ok(Ipv6HeaderSlice {
slice: unsafe {
from_raw_parts(
slice.as_ptr(),
Ipv6Header::SERIALIZED_SIZE
)
}
})
}
#[inline]
pub fn slice(&self) -> &'a [u8] {
self.slice
}
#[inline]
pub fn version(&self) -> u8 {
unsafe {
*self.slice.get_unchecked(0) >> 4
}
}
#[inline]
pub fn traffic_class(&self) -> u8 {
unsafe {
(self.slice.get_unchecked(0) << 4) |
(self.slice.get_unchecked(1) >> 4)
}
}
#[inline]
pub fn flow_label(&self) -> u32 {
u32::from_be_bytes(
unsafe {
[
0,
*self.slice.get_unchecked(1) & 0xf,
*self.slice.get_unchecked(2),
*self.slice.get_unchecked(3)
]
}
)
}
#[inline]
pub fn payload_length(&self) -> u16 {
unsafe {
get_unchecked_be_u16(self.slice.as_ptr().add(4))
}
}
#[inline]
pub fn next_header(&self) -> u8 {
unsafe {
*self.slice.get_unchecked(6)
}
}
#[inline]
pub fn hop_limit(&self) -> u8 {
unsafe {
*self.slice.get_unchecked(7)
}
}
#[inline]
pub fn source(&self) -> [u8;16] {
unsafe {
get_unchecked_16_byte_array(self.slice.as_ptr().add(8))
}
}
pub fn source_addr(&self) -> Ipv6Addr {
Ipv6Addr::from(self.source())
}
#[inline]
pub fn destination(&self) -> [u8;16] {
unsafe {
get_unchecked_16_byte_array(self.slice.as_ptr().add(24))
}
}
pub fn destination_addr(&self) -> Ipv6Addr {
Ipv6Addr::from(self.destination())
}
pub fn to_header(&self) -> Ipv6Header {
Ipv6Header {
traffic_class: self.traffic_class(),
flow_label: self.flow_label(),
payload_length: self.payload_length(),
next_header: self.next_header(),
hop_limit: self.hop_limit(),
source: self.source(),
destination: self.destination()
}
}
}