openiwan 0.3.0

An iWAN client and protocol library
Documentation
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
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
//! Byte-exact traditional iWAN framing.
//!
//! Segment-routing envelopes are parsed by [`crate::sr`] because their first
//! bytes are not a standard iWAN header even though byte zero is
//! `SEGMENT_ROUTING`.

use crate::crypto;
use crate::{Error, Result};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::net::Ipv4Addr;
use std::str::FromStr;

pub const HEADER_LEN: usize = 8;
pub const SIGNATURE_LEN: usize = 16;
pub const CONTROL_PREFIX_LEN: usize = HEADER_LEN + SIGNATURE_LEN;
pub const PING_SESSION_ID: u16 = u16::MAX;
pub const PING_TOKEN: u32 = u32::MAX;
pub const ECHO_BODY_LEN: usize = 20;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum PacketType {
    OpenReject = 0x11,
    OpenAck = 0x12,
    Open = 0x13,
    Data = 0x14,
    EchoRequest = 0x15,
    EchoResponse = 0x16,
    Close = 0x17,
    DataEncrypted = 0x18,
    DataDup = 0x19,
    DataEncryptedDup = 0x20,
    IpFragment = 0x22,
    DataIpv6 = 0x23,
    IpFragmentIpv6 = 0x24,
    SegmentRouting = 0x28,
    PingRequest = 0x29,
    PingResponse = 0x2a,
}

impl PacketType {
    pub const ALL: [Self; 16] = [
        Self::OpenReject,
        Self::OpenAck,
        Self::Open,
        Self::Data,
        Self::EchoRequest,
        Self::EchoResponse,
        Self::Close,
        Self::DataEncrypted,
        Self::DataDup,
        Self::DataEncryptedDup,
        Self::IpFragment,
        Self::DataIpv6,
        Self::IpFragmentIpv6,
        Self::SegmentRouting,
        Self::PingRequest,
        Self::PingResponse,
    ];

    pub const fn is_control(self) -> bool {
        matches!(
            self,
            Self::OpenReject
                | Self::OpenAck
                | Self::Open
                | Self::EchoRequest
                | Self::EchoResponse
                | Self::Close
                | Self::PingRequest
                | Self::PingResponse
        )
    }

    pub const fn is_data(self) -> bool {
        !self.is_control() && !matches!(self, Self::SegmentRouting)
    }

    pub const fn is_encrypted_data(self) -> bool {
        matches!(self, Self::DataEncrypted | Self::DataEncryptedDup)
    }
}

impl TryFrom<u8> for PacketType {
    type Error = Error;

    fn try_from(value: u8) -> Result<Self> {
        Self::ALL
            .into_iter()
            .find(|kind| *kind as u8 == value)
            .ok_or(Error::UnknownPacketType(value))
    }
}

impl fmt::Display for PacketType {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::OpenReject => "OPEN_REJECT",
            Self::OpenAck => "OPEN_ACK",
            Self::Open => "OPEN",
            Self::Data => "DATA",
            Self::EchoRequest => "ECHO_REQUEST",
            Self::EchoResponse => "ECHO_RESPONSE",
            Self::Close => "CLOSE",
            Self::DataEncrypted => "DATA_ENCRYPTED",
            Self::DataDup => "DATA_DUP",
            Self::DataEncryptedDup => "DATA_ENC_DUP",
            Self::IpFragment => "IP_FRAGMENT",
            Self::DataIpv6 => "DATA_IPV6",
            Self::IpFragmentIpv6 => "IP_FRAGMENT_IPV6",
            Self::SegmentRouting => "SEGMENT_ROUTING",
            Self::PingRequest => "PING_REQUEST",
            Self::PingResponse => "PING_RESPONSE",
        })
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
#[repr(u8)]
pub enum EncryptionMethod {
    None = 0,
    #[default]
    Xor = 1,
    Aes = 2,
}

impl TryFrom<u8> for EncryptionMethod {
    type Error = Error;

    fn try_from(value: u8) -> Result<Self> {
        match value {
            0 => Ok(Self::None),
            1 => Ok(Self::Xor),
            2 => Ok(Self::Aes),
            other => Err(Error::UnknownEncryptionMethod(other)),
        }
    }
}

impl FromStr for EncryptionMethod {
    type Err = Error;

    fn from_str(value: &str) -> Result<Self> {
        match value.to_ascii_lowercase().as_str() {
            "none" | "0" => Ok(Self::None),
            "xor" | "1" => Ok(Self::Xor),
            "aes" | "aes128" | "2" => Ok(Self::Aes),
            _ => Err(Error::InvalidConfig(format!(
                "unknown encryption method {value:?}"
            ))),
        }
    }
}

impl fmt::Display for EncryptionMethod {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(match self {
            Self::None => "none",
            Self::Xor => "xor",
            Self::Aes => "aes",
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PacketHeader {
    pub packet_type: PacketType,
    pub encryption: EncryptionMethod,
    pub session_id: u16,
    pub token: u32,
}

impl PacketHeader {
    pub const fn new(
        packet_type: PacketType,
        encryption: EncryptionMethod,
        session_id: u16,
        token: u32,
    ) -> Self {
        Self {
            packet_type,
            encryption,
            session_id,
            token,
        }
    }

    pub fn encode(self) -> [u8; HEADER_LEN] {
        let mut output = [0_u8; HEADER_LEN];
        output[0] = self.packet_type as u8;
        output[1] = self.encryption as u8;
        output[2..4].copy_from_slice(&self.session_id.to_be_bytes());
        output[4..8].copy_from_slice(&self.token.to_be_bytes());
        output
    }

    pub fn decode(input: &[u8]) -> Result<Self> {
        if input.len() < HEADER_LEN {
            return Err(Error::PacketTooShort {
                minimum: HEADER_LEN,
                actual: input.len(),
            });
        }
        if input[0] == PacketType::SegmentRouting as u8 {
            return Err(Error::InvalidSegmentRouting(
                "SR envelope must be parsed as an SR header",
            ));
        }
        Self::decode_inner(input)
    }

    /// Decode a standard-format header embedded inside an SR envelope.
    pub fn decode_inner(input: &[u8]) -> Result<Self> {
        if input.len() < HEADER_LEN {
            return Err(Error::PacketTooShort {
                minimum: HEADER_LEN,
                actual: input.len(),
            });
        }
        Ok(Self {
            packet_type: PacketType::try_from(input[0])?,
            encryption: EncryptionMethod::try_from(input[1])?,
            session_id: u16::from_be_bytes([input[2], input[3]]),
            token: u32::from_be_bytes([input[4], input[5], input[6], input[7]]),
        })
    }
}

/// Protocol TLV registry.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum TlvType {
    Username = 0x01,
    Password = 0x02,
    Mtu = 0x03,
    Ip = 0x04,
    Dns = 0x05,
    Gateway = 0x06,
    Netmask = 0x07,
    Encrypt = 0x08,
    DupPacket = 0x09,
    Link = 0x0a,
    AuthVerify = 0x0f,
    ErrorMessage = 0x10,
}

impl TlvType {
    pub const ALL: [Self; 12] = [
        Self::Username,
        Self::Password,
        Self::Mtu,
        Self::Ip,
        Self::Dns,
        Self::Gateway,
        Self::Netmask,
        Self::Encrypt,
        Self::DupPacket,
        Self::Link,
        Self::AuthVerify,
        Self::ErrorMessage,
    ];

    pub const fn name(self) -> &'static str {
        match self {
            Self::Username => "USERNAME",
            Self::Password => "PASSWORD",
            Self::Mtu => "MTU",
            Self::Ip => "IP",
            Self::Dns => "DNS",
            Self::Gateway => "GATEWAY",
            Self::Netmask => "NETMASK",
            Self::Encrypt => "ENCRYPT",
            Self::DupPacket => "DUP_PKT",
            Self::Link => "LINK",
            Self::AuthVerify => "AUTH_VERIFY",
            Self::ErrorMessage => "ERR_MSG",
        }
    }
}

impl TryFrom<u8> for TlvType {
    type Error = Error;

    fn try_from(value: u8) -> Result<Self> {
        Self::ALL
            .into_iter()
            .find(|kind| *kind as u8 == value)
            .ok_or_else(|| Error::InvalidTlv {
                offset: 0,
                reason: format!("unknown type 0x{value:02x}"),
            })
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Tlv {
    pub kind: TlvType,
    pub value: Vec<u8>,
}

impl Tlv {
    pub fn new(kind: TlvType, value: impl Into<Vec<u8>>) -> Result<Self> {
        let value = value.into();
        if value.len() > 253 {
            return Err(Error::TlvTooLarge {
                kind: kind.name(),
                length: value.len(),
            });
        }
        Ok(Self { kind, value })
    }

    pub fn encode(&self, output: &mut Vec<u8>) -> Result<()> {
        let length = u8::try_from(self.value.len() + 2).map_err(|_| Error::TlvTooLarge {
            kind: self.kind.name(),
            length: self.value.len(),
        })?;
        output.push(self.kind as u8);
        output.push(length);
        output.extend_from_slice(&self.value);
        Ok(())
    }

    /// Parse while at least three bytes remain and ignore a final one- or
    /// two-byte suffix, as required by the protocol.
    pub fn parse_all(input: &[u8]) -> Result<Vec<Self>> {
        let mut attributes = Vec::new();
        let mut offset = 0;
        while input.len() - offset >= 3 {
            let kind = TlvType::try_from(input[offset]).map_err(|_| Error::InvalidTlv {
                offset,
                reason: format!("unknown type 0x{:02x}", input[offset]),
            })?;
            let length = usize::from(input[offset + 1]);
            if length < 2 {
                return Err(Error::InvalidTlv {
                    offset,
                    reason: format!("invalid total length {length}"),
                });
            }
            if offset + length > input.len() {
                return Err(Error::InvalidTlv {
                    offset,
                    reason: format!(
                        "declared length {length} exceeds {} remaining bytes",
                        input.len() - offset
                    ),
                });
            }
            attributes.push(Self {
                kind,
                value: input[offset + 2..offset + length].to_vec(),
            });
            offset += length;
        }
        Ok(attributes)
    }

    /// Strict parser used when recognizing a structured `OPEN_REJECT` suffix.
    pub fn parse_complete(input: &[u8]) -> Result<Vec<Self>> {
        let attributes = Self::parse_all(input)?;
        let parsed_len = attributes.iter().map(|tlv| tlv.value.len() + 2).sum();
        if parsed_len != input.len() {
            return Err(Error::InvalidTlv {
                offset: parsed_len,
                reason: "trailing bytes do not form a complete TLV".into(),
            });
        }
        Ok(attributes)
    }

    pub fn mtu(mtu: u16) -> Self {
        Self {
            kind: TlvType::Mtu,
            value: mtu.to_be_bytes().to_vec(),
        }
    }

    pub fn username(username: &str) -> Result<Self> {
        Self::new(TlvType::Username, crypto::java_us_ascii(username))
    }

    pub fn encrypted_password(password: [u8; 16]) -> Self {
        Self {
            kind: TlvType::Password,
            value: password.to_vec(),
        }
    }

    pub fn encryption(method: EncryptionMethod) -> Self {
        Self {
            kind: TlvType::Encrypt,
            value: vec![method as u8],
        }
    }

    pub fn link(link: u32) -> Self {
        Self {
            kind: TlvType::Link,
            value: link.to_be_bytes().to_vec(),
        }
    }

    pub fn auth_verify(nonce: u32) -> Self {
        Self {
            kind: TlvType::AuthVerify,
            value: nonce.to_be_bytes().to_vec(),
        }
    }

    /// Decode a protocol integer, whose defined widths are 1, 2, and 4 bytes.
    pub fn as_integer(&self) -> Result<u32> {
        match self.value.as_slice() {
            [value] => Ok(u32::from(*value)),
            [a, b] => Ok(u32::from(u16::from_be_bytes([*a, *b]))),
            [a, b, c, d] => Ok(u32::from_be_bytes([*a, *b, *c, *d])),
            _ => Err(Error::InvalidTlvValue(self.kind.name())),
        }
    }

    pub fn first_u32(&self) -> Result<u32> {
        let bytes = self
            .value
            .get(..4)
            .ok_or(Error::InvalidTlvValue(self.kind.name()))?;
        Ok(u32::from_be_bytes(
            bytes.try_into().expect("slice length checked"),
        ))
    }

    pub fn first_ipv4(&self) -> Result<Ipv4Addr> {
        let bytes = self
            .value
            .get(..4)
            .ok_or(Error::InvalidTlvValue(self.kind.name()))?;
        Ok(Ipv4Addr::from(
            <[u8; 4]>::try_from(bytes).expect("slice length checked"),
        ))
    }
}

#[derive(Debug, Clone)]
pub struct DecodedPacket {
    pub header: PacketHeader,
    pub signature: Option<[u8; SIGNATURE_LEN]>,
    pub body: Vec<u8>,
}

pub fn calculate_signature(header: PacketHeader) -> [u8; SIGNATURE_LEN] {
    let mut input = [0_u8; HEADER_LEN + 2];
    input[..HEADER_LEN].copy_from_slice(&header.encode());
    input[HEADER_LEN..].copy_from_slice(b"mw");
    crypto::md5(&input)
}

pub fn encode_control(header: PacketHeader, body: &[u8]) -> Vec<u8> {
    debug_assert!(header.packet_type.is_control());
    let mut output = Vec::with_capacity(CONTROL_PREFIX_LEN + body.len());
    output.extend_from_slice(&header.encode());
    output.extend_from_slice(&calculate_signature(header));
    output.extend_from_slice(body);
    output
}

pub fn encode_data(header: PacketHeader, body: &[u8]) -> Vec<u8> {
    debug_assert!(header.packet_type.is_data());
    let mut output = Vec::with_capacity(HEADER_LEN + body.len());
    output.extend_from_slice(&header.encode());
    output.extend_from_slice(body);
    output
}

pub fn decode_packet(input: &[u8]) -> Result<DecodedPacket> {
    let header = PacketHeader::decode(input)?;
    if header.packet_type.is_control() {
        // Authentication probes use a raw 8-byte CLOSE packet.
        if header.packet_type == PacketType::Close && input.len() == HEADER_LEN {
            return Ok(DecodedPacket {
                header,
                signature: None,
                body: Vec::new(),
            });
        }
        if input.len() < CONTROL_PREFIX_LEN {
            return Err(Error::PacketTooShort {
                minimum: CONTROL_PREFIX_LEN,
                actual: input.len(),
            });
        }
        let signature: [u8; SIGNATURE_LEN] = input[HEADER_LEN..CONTROL_PREFIX_LEN]
            .try_into()
            .expect("slice length checked");
        if signature != calculate_signature(header) {
            return Err(Error::InvalidSignature);
        }
        Ok(DecodedPacket {
            header,
            signature: Some(signature),
            body: input[CONTROL_PREFIX_LEN..].to_vec(),
        })
    } else {
        Ok(DecodedPacket {
            header,
            signature: None,
            body: input[HEADER_LEN..].to_vec(),
        })
    }
}

pub fn build_open(
    username: &str,
    password: &str,
    mtu: u16,
    encryption: EncryptionMethod,
    first_hop_link: Option<u32>,
    auth_verify: Option<u32>,
) -> Result<Vec<u8>> {
    let mut attributes = vec![
        Tlv::mtu(mtu),
        Tlv::username(username)?,
        Tlv::encrypted_password(crypto::encrypt_password(password, username)),
        Tlv::encryption(encryption),
    ];
    if let Some(link) = first_hop_link {
        attributes.push(Tlv::link(link));
    }
    if let Some(nonce) = auth_verify.filter(|nonce| *nonce != 0) {
        attributes.push(Tlv::auth_verify(nonce));
    }

    let mut body = Vec::new();
    for attribute in &attributes {
        attribute.encode(&mut body)?;
    }
    Ok(encode_control(
        PacketHeader::new(PacketType::Open, encryption, 0, 0),
        &body,
    ))
}

pub fn build_close(header: PacketHeader) -> Vec<u8> {
    encode_control(
        PacketHeader::new(
            PacketType::Close,
            header.encryption,
            header.session_id,
            header.token,
        ),
        &[],
    )
}

/// Build the header-only `CLOSE` used for a temporary OPEN probe.
pub fn build_probe_close(header: PacketHeader) -> [u8; HEADER_LEN] {
    PacketHeader::new(
        PacketType::Close,
        header.encryption,
        header.session_id,
        header.token,
    )
    .encode()
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EchoDelayStats {
    pub current_micros: u32,
    pub minimum_micros: u32,
    pub maximum_micros: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EchoBody {
    pub tick_micros: u64,
    pub delay_stats: EchoDelayStats,
}

impl EchoBody {
    pub const fn new(tick_micros: u64, delay_stats: EchoDelayStats) -> Self {
        Self {
            tick_micros,
            delay_stats,
        }
    }

    pub fn encode(self) -> [u8; ECHO_BODY_LEN] {
        let mut body = [0_u8; ECHO_BODY_LEN];
        body[0..8].copy_from_slice(&self.tick_micros.to_le_bytes());
        body[8..12].copy_from_slice(&self.delay_stats.current_micros.to_le_bytes());
        body[12..16].copy_from_slice(&self.delay_stats.minimum_micros.to_le_bytes());
        body[16..20].copy_from_slice(&self.delay_stats.maximum_micros.to_le_bytes());
        body
    }

    pub fn decode(body: &[u8]) -> Result<Self> {
        if body.len() < ECHO_BODY_LEN {
            return Err(Error::PacketTooShort {
                minimum: ECHO_BODY_LEN,
                actual: body.len(),
            });
        }
        Ok(Self {
            tick_micros: u64::from_le_bytes(body[0..8].try_into().expect("length checked")),
            delay_stats: EchoDelayStats {
                current_micros: u32::from_le_bytes(body[8..12].try_into().expect("length checked")),
                minimum_micros: u32::from_le_bytes(
                    body[12..16].try_into().expect("length checked"),
                ),
                maximum_micros: u32::from_le_bytes(
                    body[16..20].try_into().expect("length checked"),
                ),
            },
        })
    }
}

pub fn build_echo_request(header: PacketHeader, echo: EchoBody) -> Vec<u8> {
    encode_control(
        PacketHeader::new(
            PacketType::EchoRequest,
            header.encryption,
            header.session_id,
            header.token,
        ),
        &echo.encode(),
    )
}

pub fn build_echo_response(request: PacketHeader, request_body: &[u8]) -> Result<Vec<u8>> {
    let body = EchoBody::decode(request_body)?.encode();
    Ok(encode_control(
        PacketHeader::new(
            PacketType::EchoResponse,
            request.encryption,
            request.session_id,
            request.token,
        ),
        &body,
    ))
}

pub fn build_ping_request() -> Vec<u8> {
    encode_control(
        PacketHeader::new(
            PacketType::PingRequest,
            EncryptionMethod::None,
            PING_SESSION_ID,
            PING_TOKEN,
        ),
        &[],
    )
}

pub fn build_ping_response() -> Vec<u8> {
    encode_control(
        PacketHeader::new(
            PacketType::PingResponse,
            EncryptionMethod::None,
            PING_SESSION_ID,
            PING_TOKEN,
        ),
        &[],
    )
}

pub fn find_tlv(attributes: &[Tlv], kind: TlvType) -> Option<&Tlv> {
    attributes.iter().find(|attribute| attribute.kind == kind)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn decode_hex(value: &str) -> Vec<u8> {
        let compact: String = value.chars().filter(|c| !c.is_ascii_whitespace()).collect();
        (0..compact.len())
            .step_by(2)
            .map(|offset| u8::from_str_radix(&compact[offset..offset + 2], 16).unwrap())
            .collect()
    }

    #[test]
    fn packet_registry_matches_protocol() {
        assert_eq!(
            PacketType::ALL.map(|kind| kind as u8),
            [
                0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x22, 0x23, 0x24, 0x28,
                0x29, 0x2a
            ]
        );
    }

    #[test]
    fn ping_matches_specification_vector() {
        assert_eq!(
            build_ping_request(),
            decode_hex("2900ffffffffffff0a6dd17cf9e6d40493ee1f0e6b4dc521")
        );
    }

    #[test]
    fn signed_close_matches_specification_vector() {
        let header = PacketHeader::new(
            PacketType::Close,
            EncryptionMethod::Xor,
            0x1234,
            0x89ab_cdef,
        );
        assert_eq!(
            build_close(header),
            decode_hex("1701123489abcdefc1ee1606aa425b8bd62cafd88e89f052")
        );
        assert_eq!(
            build_probe_close(header).as_slice(),
            decode_hex("1701123489abcdef")
        );
        assert!(decode_packet(&header.encode()).is_ok());
    }

    #[test]
    fn open_matches_specification_vector() {
        assert_eq!(
            build_open("alice", "secret", 1400, EncryptionMethod::Xor, None, None).unwrap(),
            decode_hex(
                "13010000000000003601680de3a6b3dd336ac557c87b501b
                 030405780107616c6963650212567e3c4f58d08532529f8191
                 a0ae0d9d080301"
            )
        );
    }

    #[test]
    fn tlv_parser_accepts_trailing_suffix() {
        let mut bytes = Vec::new();
        Tlv::mtu(1400).encode(&mut bytes).unwrap();
        bytes.extend_from_slice(&[0xaa, 0xbb]);
        assert_eq!(Tlv::parse_all(&bytes).unwrap(), [Tlv::mtu(1400)]);
        assert!(Tlv::parse_complete(&bytes).is_err());
    }

    #[test]
    fn integer_and_prefix_decoders_accept_defined_widths() {
        for (bytes, expected) in [
            (vec![2], 2),
            (vec![1, 2], 0x0102),
            (vec![1, 2, 3, 4], 0x0102_0304),
        ] {
            let tlv = Tlv::new(TlvType::Mtu, bytes).unwrap();
            assert_eq!(tlv.as_integer().unwrap(), expected);
        }
        let ip = Tlv::new(TlvType::Ip, [192, 0, 2, 1, 99]).unwrap();
        assert_eq!(ip.first_ipv4().unwrap(), Ipv4Addr::new(192, 0, 2, 1));
    }

    #[test]
    fn echo_body_is_twenty_bytes_and_little_endian() {
        let echo = EchoBody::new(
            0x0102_0304_0506_0708,
            EchoDelayStats {
                current_micros: 0x1112_1314,
                minimum_micros: 0x2122_2324,
                maximum_micros: 0x3132_3334,
            },
        );
        assert_eq!(
            echo.encode(),
            [
                8, 7, 6, 5, 4, 3, 2, 1, 0x14, 0x13, 0x12, 0x11, 0x24, 0x23, 0x22, 0x21, 0x34, 0x33,
                0x32, 0x31
            ]
        );
        assert_eq!(EchoBody::decode(&echo.encode()).unwrap(), echo);
    }
}