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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
use std::fmt;
use nom::simple_errors::Context;
use nom::{be_u16, be_u32, rest, Err, ErrorKind, IResult};
use packet::writer::{Result as WriterResult, Writer};
use util::{hexdump, shorthash};
const HEARTBEAT_INFO_TYPE: u16 = 1;
pub const IPV4ADDRESS_TYPE: u16 = 5;
pub const IPV6ADDRESS_TYPE: u16 = 6;
const STATE_COOKIE_TYPE: u16 = 7;
const UNRECOGNIZED_PARAMETER_TYPE: u16 = 8;
const COOKIE_PRESERVATIVE_TYPE: u16 = 9;
const HOST_NAME_ADDRESS_TYPE: u16 = 11;
const SUPPORTED_ADDRESS_TYPES_TYPE: u16 = 12;
const ECNCAPABLE_TYPE: u16 = 32768;
const FORWARD_TSN_SUPPORTED_TYPE: u16 = 49152;
#[derive(Clone, Debug, PartialEq)]
pub enum Parameter {
HeartbeatInfo(Vec<u8>),
IPv4Address(IPv4Address),
IPv6Address(IPv6Address),
StateCookie(Vec<u8>),
UnrecognizedParameter(Vec<u8>),
CookiePreservative(u32),
HostNameAddress(String),
SupportedAddressTypes(Vec<u16>),
ECNCapable,
ForwardTSNSupported,
Unknown(u16, Vec<u8>),
}
const IPV4ADDRESS_SIZE: usize = 4;
const IPV6ADDRESS_SIZE: usize = 16;
#[derive(Clone, PartialEq)]
pub struct IPv4Address {
pub value: u32,
}
impl IPv4Address {
pub fn from_bytes(bytes: &[u8; 4]) -> IPv4Address {
IPv4Address {
value: ((bytes[0] as u32) << 24)
| ((bytes[1] as u32) << 16)
| ((bytes[2] as u32) << 8)
| ((bytes[3] as u32) << 0),
}
}
}
impl fmt::Display for IPv4Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}.{}.{}.{}",
(self.value >> 24) & 0xFF,
(self.value >> 16) & 0xFF,
(self.value >> 8) & 0xFF,
(self.value >> 0) & 0xFF
)
}
}
impl fmt::Debug for IPv4Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Display).fmt(f)
}
}
#[derive(Clone, PartialEq)]
pub struct IPv6Address {
pub value: [u8; 16],
}
impl IPv6Address {
pub fn from_bytes(bytes: &[u8; 16]) -> IPv6Address {
IPv6Address { value: *bytes }
}
}
impl fmt::Display for IPv6Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}:{:x}",
(self.value[0] as u16) << 8 | self.value[1] as u16,
(self.value[2] as u16) << 8 | self.value[3] as u16,
(self.value[4] as u16) << 8 | self.value[5] as u16,
(self.value[6] as u16) << 8 | self.value[7] as u16,
(self.value[8] as u16) << 8 | self.value[9] as u16,
(self.value[10] as u16) << 8 | self.value[11] as u16,
(self.value[12] as u16) << 8 | self.value[13] as u16,
(self.value[14] as u16) << 8 | self.value[15] as u16
)
}
}
impl fmt::Debug for IPv6Address {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(self as &fmt::Display).fmt(f)
}
}
impl fmt::Display for Parameter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Parameter::HeartbeatInfo(ref data) => {
try!(writeln!(f, "HeartbeatInfo"));
hexdump(f, "\t\t", data.as_slice())
}
Parameter::IPv4Address(ref addr) => write!(f, "IPv4Address {}", addr),
Parameter::IPv6Address(ref addr) => write!(f, "IPv6Address {}", addr),
Parameter::StateCookie(ref data) => {
write!(f, "StateCookie {} ({} bytes)", shorthash(data), data.len())
}
Parameter::UnrecognizedParameter(ref data) => write!(f, "Unrecognized {:?}", data),
Parameter::CookiePreservative(ref data) => write!(f, "CookiePreservative {:?}", data),
Parameter::HostNameAddress(ref hostname) => {
write!(f, "HostNameAddress \"{}\"", hostname)
}
Parameter::SupportedAddressTypes(ref types) => {
write!(f, "SupportedAddressTypes {:?}", types)
}
Parameter::ECNCapable => write!(f, "ECNCapable"),
Parameter::ForwardTSNSupported => write!(f, "ForwardTSNSupported"),
Parameter::Unknown(ptype, ref data) => {
try!(writeln!(f, "Unknown{{type: {} (0x{:#04x})}}", ptype, ptype));
hexdump(f, "\t", data.as_slice())
}
}
}
}
named!(heartbeatinfo_parameter<&[u8], Parameter>, do_parse!(
data: rest >>
( Parameter::HeartbeatInfo(data.to_vec()) )
));
named!(ipv4address_parameter<&[u8], Parameter>, do_parse!(
data: take!(IPV4ADDRESS_SIZE) >>
(Parameter::IPv4Address(
IPv4Address::from_bytes(&[ data[0], data[1], data[2], data[3] ])
))
));
named!(ipv6address_parameter<&[u8], Parameter>, do_parse!(
data: take!(IPV6ADDRESS_SIZE) >>
(
Parameter::IPv6Address(
IPv6Address::from_bytes(
&[
data[0], data[1], data[2], data[3],
data[4], data[5], data[6], data[7],
data[8], data[9], data[10], data[11],
data[12], data[13], data[14], data[15]
]
)
)
)
));
named!(statecookie_parameter<&[u8], Parameter>, do_parse!(
data: rest >>
( Parameter::StateCookie(data.to_vec()) )
));
named!(unrecognizedparameter_parameter<&[u8], Parameter>, do_parse!(
data: rest >>
( Parameter::UnrecognizedParameter(data.to_vec()) )
));
named!(cookiepreservative_parameter<&[u8], Parameter>, do_parse!(
lifespan_increment: be_u32 >>
( Parameter::CookiePreservative(lifespan_increment) )
));
const HOSTNAME_MIN: usize = 1;
const HOSTNAME_MAX: usize = 256;
fn parse_hostname(input: &[u8]) -> IResult<&[u8], String> {
let length = input.len();
if length < (HOSTNAME_MIN + 1) || length > (HOSTNAME_MAX + 1) {
return Err(Err::Error(Context::Code(input, ErrorKind::Custom(0))));
}
if length > input.len() {
return Err(Err::Error(Context::Code(input, ErrorKind::Custom(0))));
}
if input[length - 1] != 0 {
return Err(Err::Error(Context::Code(input, ErrorKind::Custom(0))));
}
let mut s: String = String::with_capacity(length - 1);
for octet in &input[..(length - 1)] {
s.push(*octet as char);
}
Ok((&input[length..], s))
}
named!(hostnameaddress_parameter<&[u8], Parameter>, do_parse!(
hostname: call!(parse_hostname) >>
( Parameter::HostNameAddress(hostname) )
));
named!(supportedaddresstypes_parameter<&[u8], Parameter>, do_parse!(
types: many0!(complete!(be_u16)) >>
( Parameter::SupportedAddressTypes(types) )
));
named!(ecncapable_parameter<&[u8], Parameter>, do_parse!(
( Parameter::ECNCapable )
));
named!(forwardtsnsupported_parameter<&[u8], Parameter>, do_parse!(
( Parameter::ForwardTSNSupported )
));
#[inline]
fn parameter_dispatch(tag: u16, value: &[u8]) -> IResult<&[u8], Parameter> {
match tag {
HEARTBEAT_INFO_TYPE => heartbeatinfo_parameter(value),
IPV4ADDRESS_TYPE => ipv4address_parameter(value),
IPV6ADDRESS_TYPE => ipv6address_parameter(value),
STATE_COOKIE_TYPE => statecookie_parameter(value),
UNRECOGNIZED_PARAMETER_TYPE => unrecognizedparameter_parameter(value),
COOKIE_PRESERVATIVE_TYPE => cookiepreservative_parameter(value),
HOST_NAME_ADDRESS_TYPE => hostnameaddress_parameter(value),
SUPPORTED_ADDRESS_TYPES_TYPE => supportedaddresstypes_parameter(value),
ECNCAPABLE_TYPE => ecncapable_parameter(value),
FORWARD_TSN_SUPPORTED_TYPE => forwardtsnsupported_parameter(value),
_ => Ok((&value[0..0], Parameter::Unknown(tag, value.to_owned()))),
}
}
named!(pub parse_parameter<&[u8], Parameter>, do_parse!(
parameter: parse_tlv!(parameter_dispatch) >> ( parameter )
));
pub fn write_parameter(writer: &mut Writer, parameter: &Parameter) -> WriterResult<()> {
match parameter {
&Parameter::HeartbeatInfo(ref v) => {
writer.open_tlv(HEARTBEAT_INFO_TYPE)?;
writer.write_bytes(v)?;
writer.close_tlv()?;
}
&Parameter::IPv4Address(ref addr) => {
writer.open_tlv(IPV4ADDRESS_TYPE)?;
writer.write_be32(addr.value)?;
writer.close_tlv()?;
}
&Parameter::IPv6Address(ref addr) => {
writer.open_tlv(IPV6ADDRESS_TYPE)?;
writer.write_bytes(&addr.value)?;
writer.close_tlv()?;
}
&Parameter::StateCookie(ref v) => {
writer.open_tlv(STATE_COOKIE_TYPE)?;
writer.write_bytes(v)?;
writer.close_tlv()?;
}
&Parameter::UnrecognizedParameter(ref v) => {
writer.open_tlv(UNRECOGNIZED_PARAMETER_TYPE)?;
writer.write_bytes(v)?;
writer.close_tlv()?;
}
&Parameter::CookiePreservative(v) => {
writer.open_tlv(COOKIE_PRESERVATIVE_TYPE)?;
writer.write_be32(v)?;
writer.close_tlv()?;
}
&Parameter::HostNameAddress(ref name) => {
writer.open_tlv(HOST_NAME_ADDRESS_TYPE)?;
writer.write_bytes(name.as_bytes())?;
writer.write_be8(0)?;
writer.close_tlv()?;
}
&Parameter::SupportedAddressTypes(ref types) => {
writer.open_tlv(SUPPORTED_ADDRESS_TYPES_TYPE)?;
for t in types {
writer.write_be16(*t)?;
}
writer.close_tlv()?;
}
&Parameter::ECNCapable => {
writer.open_tlv(ECNCAPABLE_TYPE)?;
writer.close_tlv()?;
}
&Parameter::ForwardTSNSupported => {
writer.open_tlv(FORWARD_TSN_SUPPORTED_TYPE)?;
writer.close_tlv()?;
}
&Parameter::Unknown(ptype, ref v) => {
writer.open_tlv(ptype)?;
writer.write_bytes(v)?;
writer.close_tlv()?;
}
};
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use packet::writer::*;
use util::tests::*;
const MAX_TRAILING_BYTES: usize = 5;
fn serialize(parameter: &Parameter) -> Vec<u8> {
let mut writer = WriterOwned::new();
write_parameter(&mut writer, parameter).unwrap();
writer.to_owned()
}
fn test_parameter(parameter: Parameter, expected_bytes: &[u8]) {
let buffer = serialize(¶meter);
assert_eq!(buffer, expected_bytes);
let (remainder, parsed_parameter) = parse_parameter(&buffer).unwrap();
assert!(remainder.is_empty());
assert_eq!(&parsed_parameter, ¶meter);
test_parameter_serdes(¶meter);
}
fn test_parameter_serdes(original_parameter: &Parameter) {
for trailing_bytes in 0..MAX_TRAILING_BYTES {
let mut buffer = serialize(original_parameter);
for _ in 0..trailing_bytes {
buffer.push(0xa0);
}
let (remainder, parameter) = parse_parameter(&buffer).unwrap();
assert_eq!(remainder.len(), trailing_bytes);
assert_eq!(¶meter, original_parameter);
}
}
const HEARTBEAT_INFO_PAYLOAD_BYTES: &[u8] = &[
0x02, 0x00, 0x07, 0xe4, 0x0a, 0x0a, 0x0a, 0x65,
0x70, 0x3a, 0x52, 0xd7, 0x00, 0x88, 0xff, 0xff,
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0x02, 0x99, 0x24, 0x00,
0x01, 0x00, 0x00, 0x00, 0xf3, 0x83, 0xe1, 0x85,
0xa0, 0xa6, 0x76, 0xca,
];
fn heartbeat_info() -> Parameter {
Parameter::HeartbeatInfo(HEARTBEAT_INFO_PAYLOAD_BYTES.to_vec())
}
const HEARTBEAT_INFO_BYTES: &[u8] = &[
0x00, 0x01, 0x00, 0x30, 0x02, 0x00, 0x07, 0xe4,
0x0a, 0x0a, 0x0a, 0x65, 0x70, 0x3a, 0x52, 0xd7,
0x00, 0x88, 0xff, 0xff, 0x20, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x02, 0x99, 0x24, 0x00, 0x01, 0x00, 0x00, 0x00,
0xf3, 0x83, 0xe1, 0x85, 0xa0, 0xa6, 0x76, 0xca,
];
#[test]
fn test_heartbeat_info() {
test_parameter(heartbeat_info(), HEARTBEAT_INFO_BYTES);
}
const TEST_HEARTBEAT_INFO_PAYLOAD_MAX_LENGTH: usize = 4;
#[test]
fn test_heartbeat_info_full() {
for payload in u8_test_lists(TEST_HEARTBEAT_INFO_PAYLOAD_MAX_LENGTH) {
test_parameter_serdes(&Parameter::HeartbeatInfo(payload));
}
}
fn ipv4_address() -> Parameter {
Parameter::IPv4Address(IPv4Address::from_bytes(&[127, 0, 0, 1]))
}
const IPV4_ADDRESS_BYTES: &[u8] = &[
0x00, 0x05, 0x00, 0x08, 0x7f, 0x00, 0x00, 0x01,
];
#[test]
fn test_ipv4_address() {
test_parameter(ipv4_address(), IPV4_ADDRESS_BYTES);
}
#[test]
fn test_ipv4_address_full() {
for &address in U32_TEST_VALUES {
let parameter = Parameter::IPv4Address(IPv4Address { value: address });
test_parameter_serdes(¶meter);
}
}
fn ipv6_address() -> Parameter {
Parameter::IPv6Address(IPv6Address::from_bytes(&[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x27, 0xff, 0xfe, 0x19,
0xf7, 0x37,
]))
}
const IPV6_ADDRESS_BYTES: &[u8] = &[
0x00, 0x06, 0x00, 0x14, 0xfe, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x27, 0xff,
0xfe, 0x19, 0xf7, 0x37,
];
#[test]
fn test_ipv6_address() {
test_parameter(ipv6_address(), IPV6_ADDRESS_BYTES);
}
const STATE_COOKIE_PAYLOAD_BYTES: &[u8] = &[
0x9a, 0x92, 0x48, 0xf6, 0x4e, 0xd2, 0x77, 0x10,
0xac, 0x44, 0x33, 0x6c, 0x52, 0x4a, 0xb5, 0x40,
0x4c, 0x78, 0xb5, 0x5f, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xec, 0x96, 0xb8, 0x6a,
0xf5, 0xae, 0x26, 0x6a, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xbd, 0x90, 0x8a, 0xae,
0x7e, 0x11, 0x68, 0x14, 0x0a, 0x00, 0x0a, 0x00,
0x6b, 0xcb, 0x3c, 0xd0, 0x02, 0x00, 0x9a, 0xe4,
0x7f, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xe4, 0x07, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x02, 0x00, 0x24, 0x8f, 0x6d, 0x98, 0xb9,
0x3b, 0x0e, 0x63, 0x5c, 0x2e, 0x5e, 0x34, 0xf1,
0x57, 0x7c, 0x89, 0x79, 0xd3, 0x17, 0x3c, 0x2f,
0xe9, 0xed, 0x82, 0xd3, 0x03, 0x41, 0xb5, 0x25,
0xe2, 0xb0, 0x7b, 0x35, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x44, 0x6a, 0x26, 0xae, 0xf5,
0x00, 0x01, 0xa0, 0x00, 0x00, 0x0a, 0xff, 0xff,
0x97, 0xb6, 0xcb, 0xfc, 0x00, 0x05, 0x00, 0x08,
0x7f, 0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x08,
0x0a, 0x00, 0x02, 0x0f, 0x00, 0x05, 0x00, 0x08,
0x0a, 0x0a, 0x0a, 0x65, 0x00, 0x05, 0x00, 0x08,
0xac, 0x11, 0x00, 0x01, 0x00, 0x0c, 0x00, 0x06,
0x00, 0x05, 0x00, 0x00, 0x80, 0x00, 0x00, 0x04,
0xc0, 0x00, 0x00, 0x04, 0x00, 0x05, 0x00, 0x08,
0x7f, 0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x08,
0x0a, 0x00, 0x02, 0x0f, 0x00, 0x05, 0x00, 0x08,
0x0a, 0x0a, 0x0a, 0x65, 0x00, 0x05, 0x00, 0x08,
0xac, 0x11, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
];
fn state_cookie() -> Parameter {
Parameter::StateCookie(STATE_COOKIE_PAYLOAD_BYTES.to_vec())
}
const STATE_COOKIE_BYTES: &[u8] = &[
0x00, 0x07, 0x01, 0x28, 0x9a, 0x92, 0x48, 0xf6,
0x4e, 0xd2, 0x77, 0x10, 0xac, 0x44, 0x33, 0x6c,
0x52, 0x4a, 0xb5, 0x40, 0x4c, 0x78, 0xb5, 0x5f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xec, 0x96, 0xb8, 0x6a, 0xf5, 0xae, 0x26, 0x6a,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xbd, 0x90, 0x8a, 0xae, 0x7e, 0x11, 0x68, 0x14,
0x0a, 0x00, 0x0a, 0x00, 0x6b, 0xcb, 0x3c, 0xd0,
0x02, 0x00, 0x9a, 0xe4, 0x7f, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xe4, 0x07, 0x01, 0x00,
0x00, 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x24,
0x8f, 0x6d, 0x98, 0xb9, 0x3b, 0x0e, 0x63, 0x5c,
0x2e, 0x5e, 0x34, 0xf1, 0x57, 0x7c, 0x89, 0x79,
0xd3, 0x17, 0x3c, 0x2f, 0xe9, 0xed, 0x82, 0xd3,
0x03, 0x41, 0xb5, 0x25, 0xe2, 0xb0, 0x7b, 0x35,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x20, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x44,
0x6a, 0x26, 0xae, 0xf5, 0x00, 0x01, 0xa0, 0x00,
0x00, 0x0a, 0xff, 0xff, 0x97, 0xb6, 0xcb, 0xfc,
0x00, 0x05, 0x00, 0x08, 0x7f, 0x00, 0x00, 0x01,
0x00, 0x05, 0x00, 0x08, 0x0a, 0x00, 0x02, 0x0f,
0x00, 0x05, 0x00, 0x08, 0x0a, 0x0a, 0x0a, 0x65,
0x00, 0x05, 0x00, 0x08, 0xac, 0x11, 0x00, 0x01,
0x00, 0x0c, 0x00, 0x06, 0x00, 0x05, 0x00, 0x00,
0x80, 0x00, 0x00, 0x04, 0xc0, 0x00, 0x00, 0x04,
0x00, 0x05, 0x00, 0x08, 0x7f, 0x00, 0x00, 0x01,
0x00, 0x05, 0x00, 0x08, 0x0a, 0x00, 0x02, 0x0f,
0x00, 0x05, 0x00, 0x08, 0x0a, 0x0a, 0x0a, 0x65,
0x00, 0x05, 0x00, 0x08, 0xac, 0x11, 0x00, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
#[test]
fn test_state_cookie() {
test_parameter(state_cookie(), STATE_COOKIE_BYTES);
}
fn unrecognized_parameter() -> Parameter {
Parameter::UnrecognizedParameter(serialize(&Parameter::Unknown(
0xfffe,
vec![0x00, 0x08, 0x00, 0x01, 0x02, 0x03],
)))
}
const UNRECOGNIZED_PARAMETER_BYTES: &[u8] = &[
0x00, 0x08, 0x00, 0x10, 0xff, 0xfe, 0x00, 0x0a,
0x00, 0x08, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00,
];
fn unrecognized_parameter_tests() -> Vec<Parameter> {
vec![
Parameter::Unknown(0xfffe, vec![0xff, 0xfe, 0x00, 0x08, 0x00, 0x01, 0x02, 0x03]),
Parameter::Unknown(0xffff, vec![0xff, 0xff, 0x00, 0x04]),
Parameter::IPv4Address(IPv4Address::from_bytes(&[127, 0, 0, 1])),
Parameter::IPv6Address(IPv6Address::from_bytes(&[
0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x5e, 0x60, 0xff, 0xfe, 0xeb,
0x23, 0x75,
])),
]
}
#[test]
fn test_unrecognized_parameter() {
test_parameter(unrecognized_parameter(), UNRECOGNIZED_PARAMETER_BYTES);
}
#[test]
fn test_unrecognized_parameter_full() {
for parameter in unrecognized_parameter_tests() {
let parameter = serialize(¶meter);
test_parameter_serdes(&Parameter::UnrecognizedParameter(parameter));
}
}
fn cookie_preservative_parameter() -> Parameter {
Parameter::CookiePreservative(0xfffe0201)
}
const COOKIE_PRESERVATIVE_BYTES: &[u8] = &[
0x00, 0x09, 0x00, 0x08, 0xff, 0xfe, 0x02, 0x01,
];
#[test]
fn test_cookie_preservative_parameter() {
test_parameter(cookie_preservative_parameter(), COOKIE_PRESERVATIVE_BYTES);
}
#[test]
fn test_cookie_preservative_parameter_full() {
for &lifespan_increment in U32_TEST_VALUES {
test_parameter_serdes(&Parameter::CookiePreservative(lifespan_increment));
}
}
fn host_name_address_parameter() -> Parameter {
Parameter::HostNameAddress("example.com".to_owned())
}
const HOST_NAME_ADDRESS_BYTES: &[u8] = &[
0x00, 0x0b, 0x00, 0x10, 0x65, 0x78, 0x61, 0x6d,
0x70, 0x6c, 0x65, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
];
const HOST_NAME_ADDRESS_TEST_VALUES: &[&str] = &[
"x",
"example.com",
"3.141592653589793238462643383279502884197169399375105820974944592.com",
];
#[test]
fn test_host_name_address_parameter() {
test_parameter(host_name_address_parameter(), HOST_NAME_ADDRESS_BYTES);
}
#[test]
fn test_host_name_address_parameter_full() {
for hostname in HOST_NAME_ADDRESS_TEST_VALUES {
test_parameter_serdes(&Parameter::HostNameAddress(hostname.to_string()));
}
}
fn supported_address_types() -> Parameter {
Parameter::SupportedAddressTypes(vec![5, 6])
}
const SUPPORTED_ADDRESS_TYPES_BYTES: &[u8] = &[
0x00, 0x0c, 0x00, 0x08, 0x00, 0x05, 0x00, 0x06,
];
const SUPPORTED_ADDRESS_TYPES_TEST_VALUES: &[&[u16]] = &[
&[],
&[IPV4ADDRESS_TYPE],
&[IPV6ADDRESS_TYPE],
&[IPV4ADDRESS_TYPE, IPV6ADDRESS_TYPE],
];
#[test]
fn test_supported_address_types() {
test_parameter(supported_address_types(), SUPPORTED_ADDRESS_TYPES_BYTES);
}
#[test]
fn test_supported_address_types_full() {
for address_types in SUPPORTED_ADDRESS_TYPES_TEST_VALUES {
test_parameter_serdes(&Parameter::SupportedAddressTypes(address_types.to_vec()));
}
}
fn ecn_capable() -> Parameter {
Parameter::ECNCapable
}
const ECN_CAPABLE_BYTES: &[u8] = &[
0x80, 0x00, 0x00, 0x04,
];
#[test]
fn test_ecn_capable() {
test_parameter(ecn_capable(), ECN_CAPABLE_BYTES);
}
fn forward_tsn_supported() -> Parameter {
Parameter::ForwardTSNSupported
}
const FORWARD_TSN_SUPPORTED_BYTES: &[u8] = &[
0xc0, 0x00, 0x00, 0x04,
];
#[test]
fn test_forward_tsn_supported() {
test_parameter(forward_tsn_supported(), FORWARD_TSN_SUPPORTED_BYTES);
}
}