crafter 0.3.2

Packet-level network interaction for Rust tools and agents.
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
//! RIPng — RIP for IPv6 (RFC 2080).
//!
//! RIPng is the IPv6 variant of the Routing Information Protocol. It runs over
//! UDP port 521 and uses the link-local multicast group `ff02::9` for periodic
//! and triggered responses. Following the project's explicit v4/v6 layer-naming
//! convention (mirroring `Icmpv4`/`Icmpv6`), RIPng is modeled as its own
//! `Ripng` layer rather than being folded into the IPv4 `Rip` layer.
//!
//! Types and constants are populated in later steps; this module starts as an
//! empty scaffold so the rest of the crate has a stable home to build against.

pub mod constants;
pub mod rte;

pub use constants::*;
pub use rte::RipngRte;

use core::any::Any;
use core::ops::Div;

use crate::error::{CrafterError, Result};
use crate::field::Field;
use crate::packet::{IntoPacket, Layer, LayerContext, Packet};
use crate::protocols::rip::message::RipCommand;

macro_rules! impl_layer_object {
    ($type:ty) => {
        fn clone_layer(&self) -> Box<dyn Layer> {
            Box::new(self.clone())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }

        fn as_any_mut(&mut self) -> &mut dyn Any {
            self
        }

        fn into_any(self: Box<Self>) -> Box<dyn Any> {
            self
        }
    };
}

macro_rules! impl_layer_div {
    ($type:ty) => {
        impl<R> Div<R> for $type
        where
            R: IntoPacket,
        {
            type Output = Packet;

            fn div(self, rhs: R) -> Self::Output {
                Packet::from_layer(self).concat(rhs)
            }
        }
    };
}

/// A RIPng message over IPv6/UDP 521 (RFC 2080 §2).
///
/// A `Ripng` is the 4-octet RIPng header (command, version, 2-octet reserved)
/// followed by zero or more fixed 20-octet route table entries
/// ([`RipngRte`]). RIPng is the IPv6 variant of RIP; following the project's
/// explicit v4/v6 layer-naming convention (mirroring `Icmpv4`/`Icmpv6`), it is
/// modeled as its own `Ripng` layer rather than folded into the IPv4 [`Rip`]
/// layer. The IPv4 [`RipCommand`] enum is reused for the command octet, since
/// RIPng shares the Request/Response codepoints (RFC 2080 §2.1).
///
/// [`Rip`]: crate::protocols::rip::Rip
///
/// Header fields are held in [`Field`] wrappers so a later `compile()` step can
/// fill defaults (version, reserved) only when the caller left a field unset and
/// leave caller-set values — including deliberately wrong ones — untouched. The
/// builders mark touched fields caller-set via `set_user`.
///
/// The `command`/`rtes` builders use `with_`-prefixed names so they do not
/// collide with the same-named [`Ripng::command`]/[`Ripng::rtes`] accessors
/// (Rust rejects two inherent methods with the same name).
#[derive(Debug, Clone)]
pub struct Ripng {
    /// RIPng command octet (RFC 2080 §2.1); modeled as a raw code so unknown
    /// commands round-trip. Read it as a typed [`RipCommand`] via
    /// [`Ripng::command`].
    pub command: Field<u8>,
    /// RIPng version octet (RFC 2080 §2).
    pub version: Field<u8>,
    /// Reserved 2-octet header field, must be zero (RFC 2080 §2).
    pub reserved: Field<u16>,
    /// Route table entries that follow the header (RFC 2080 §2.1).
    pub rtes: Vec<RipngRte>,
}

impl Ripng {
    /// Create a RIPng message with library defaults.
    ///
    /// The command defaults to [`RipCommand::Response`], the version to
    /// [`RIPNG_VERSION_1`], the reserved field to `0`, and the RTE list is
    /// empty. None of these defaults are marked caller-set, so a later
    /// `compile()` step may overwrite them.
    pub fn new() -> Self {
        Self {
            command: Field::defaulted(RIPNG_COMMAND_RESPONSE),
            version: Field::defaulted(RIPNG_VERSION_1),
            reserved: Field::defaulted(0),
            rtes: Vec::new(),
        }
    }

    /// Build a RIPng Request message (RFC 2080 §2.1).
    ///
    /// Sets the command to [`RipCommand::Request`] and the version to
    /// [`RIPNG_VERSION_1`]; both are marked caller-set.
    pub fn request() -> Self {
        Self::new()
            .with_command(RipCommand::Request)
            .version(RIPNG_VERSION_1)
    }

    /// Build a RIPng Response message (RFC 2080 §2.1).
    ///
    /// Sets the command to [`RipCommand::Response`] and the version to
    /// [`RIPNG_VERSION_1`]; both are marked caller-set.
    pub fn response() -> Self {
        Self::new()
            .with_command(RipCommand::Response)
            .version(RIPNG_VERSION_1)
    }

    /// Set the command from a typed [`RipCommand`] (caller-set).
    ///
    /// Stores the command's wire code via [`RipCommand::code`].
    pub fn with_command(mut self, command: RipCommand) -> Self {
        self.command.set_user(command.code());
        self
    }

    /// Set the command from a raw wire code (caller-set).
    ///
    /// Use this to emit an unrecognized command octet verbatim.
    pub fn command_code(mut self, code: u8) -> Self {
        self.command.set_user(code);
        self
    }

    /// Set the version octet (caller-set).
    pub fn version(mut self, version: u8) -> Self {
        self.version.set_user(version);
        self
    }

    /// Set the reserved header field (caller-set).
    ///
    /// The reserved field must be zero on the wire (RFC 2080 §2); this builder
    /// exists so generated tools can emit a deliberately non-zero value.
    pub fn reserved(mut self, reserved: u16) -> Self {
        self.reserved.set_user(reserved);
        self
    }

    /// Append a single route table entry.
    pub fn rte(mut self, rte: RipngRte) -> Self {
        self.rtes.push(rte);
        self
    }

    /// Replace the route table entries with the given list.
    pub fn with_rtes(mut self, rtes: impl Into<Vec<RipngRte>>) -> Self {
        self.rtes = rtes.into();
        self
    }

    /// Effective command wire code (caller-set or default).
    pub fn command_value(&self) -> u8 {
        self.command
            .value()
            .copied()
            .unwrap_or(RIPNG_COMMAND_RESPONSE)
    }

    /// Effective command as a typed [`RipCommand`] (caller-set or default).
    pub fn command(&self) -> RipCommand {
        RipCommand::from_code(self.command_value())
    }

    /// Effective version octet (caller-set or default).
    pub fn version_value(&self) -> u8 {
        self.version.value().copied().unwrap_or(RIPNG_VERSION_1)
    }

    /// Effective reserved header field (caller-set or default).
    pub fn reserved_value(&self) -> u16 {
        self.reserved.value().copied().unwrap_or(0)
    }

    /// The route table entries that follow the header.
    pub fn rtes(&self) -> &[RipngRte] {
        &self.rtes
    }
}

impl Default for Ripng {
    fn default() -> Self {
        Self::new()
    }
}

impl Layer for Ripng {
    fn name(&self) -> &'static str {
        "Ripng"
    }

    fn encoded_len(&self) -> usize {
        // RFC 2080 §2: 4-octet header plus 20 octets per route table entry.
        RIPNG_HEADER_LEN + self.rtes.len() * RIPNG_RTE_LEN
    }

    fn compile(&self, _ctx: &LayerContext<'_>, out: &mut Vec<u8>) -> Result<()> {
        // RFC 2080 §2: 4-octet header (command, version, 2-octet reserved)
        // followed by each route table entry's 20 octets. Effective values are
        // used as-is, so caller-set command/version/reserved (including
        // deliberately wrong ones) serialize exactly as set.
        out.reserve(self.encoded_len());
        out.push(self.command_value());
        out.push(self.version_value());
        out.extend_from_slice(&self.reserved_value().to_be_bytes());

        for rte in &self.rtes {
            rte.encode(out);
        }
        Ok(())
    }

    fn summary(&self) -> String {
        // Compact one-line summary: command name, version, and RTE count, e.g.
        // "Ripng v1 Response (2 RTEs)". Uses effective values so a decoded or
        // caller-set message reads correctly.
        let count = self.rtes.len();
        let plural = if count == 1 { "RTE" } else { "RTEs" };
        format!(
            "Ripng v{} {} ({} {})",
            self.version_value(),
            self.command().name(),
            count,
            plural
        )
    }

    fn inspection_fields(&self) -> Vec<(&'static str, String)> {
        // Surface the header fields and RTE count so decoded messages are
        // readable via show()/summary() without log-fishing.
        vec![
            ("command", self.command().name().to_string()),
            ("version", self.version_value().to_string()),
            ("reserved", self.reserved_value().to_string()),
            ("rtes", self.rtes.len().to_string()),
        ]
    }

    impl_layer_object!(Ripng);
}

impl_layer_div!(Ripng);

/// Decode a UDP payload into a [`Ripng`] layer (RFC 2080 §2).
///
/// A RIPng message is the 4-octet header (command, version, 2-octet reserved)
/// followed by a whole number of 20-octet route table entries. The header
/// command, version, and reserved fields are marked caller-set (`set_user`) so
/// the decoded layer re-`compile()`s byte-for-byte, and each RTE is parsed with
/// `RipngRte::decode`.
///
/// Decoding never panics on a short or partial buffer. A body shorter than
/// [`RIPNG_HEADER_LEN`] yields the crate's structured
/// [`CrafterError::buffer_too_short`] with context `"RIPng header"`; a trailing
/// run of bytes that is not a whole multiple of [`RIPNG_RTE_LEN`] yields the
/// same structured error for the partial RTE rather than dropping bytes.
///
/// The RFC 2080 §2.1 25-RTE limit is a generation guideline, not a decode-time
/// rejection: every present RTE is decoded.
pub fn decode(bytes: &[u8]) -> Result<Ripng> {
    if bytes.len() < RIPNG_HEADER_LEN {
        return Err(CrafterError::buffer_too_short(
            "RIPng header",
            RIPNG_HEADER_LEN,
            bytes.len(),
        ));
    }

    let command = bytes[0];
    let version = bytes[1];
    let reserved = u16::from_be_bytes([bytes[2], bytes[3]]);

    let mut ripng = Ripng::new();
    ripng.command.set_user(command);
    ripng.version.set_user(version);
    ripng.reserved.set_user(reserved);

    let mut rest = &bytes[RIPNG_HEADER_LEN..];
    while !rest.is_empty() {
        let rte = RipngRte::decode(rest)?;
        ripng.rtes.push(rte);
        rest = &rest[RIPNG_RTE_LEN..];
    }

    Ok(ripng)
}

/// Append a decoded RIPng message to an existing packet stack.
///
/// Mirrors the RIP UDP-application decode entry: [`decode`] parses the UDP
/// payload into a [`Ripng`] layer, which is then pushed onto the packet. Used by
/// the protocol registry's conservative UDP/521 binding.
pub(crate) fn append_ripng_packet(packet: Packet, bytes: &[u8]) -> Result<Packet> {
    Ok(packet.push(decode(bytes)?))
}

/// Return true when bytes have enough RIPng structure to bind on UDP/521.
///
/// The check is deliberately conservative so unrelated traffic on port 521
/// falls through to `Raw` rather than misdecoding as `Ripng`: the payload must
/// be at least the 4-octet header ([`RIPNG_HEADER_LEN`]), the command must be a
/// known RIPng command (Request/Response 1/2), the version must be 1, and the
/// bytes after the header must be a whole multiple of the 20-octet RTE length
/// ([`RIPNG_RTE_LEN`]).
pub(crate) fn looks_like_ripng_payload(bytes: &[u8]) -> bool {
    if bytes.len() < RIPNG_HEADER_LEN {
        return false;
    }
    let command = bytes[0];
    let version = bytes[1];
    matches!(command, RIPNG_COMMAND_REQUEST | RIPNG_COMMAND_RESPONSE)
        && version == RIPNG_VERSION_1
        && (bytes.len() - RIPNG_HEADER_LEN) % RIPNG_RTE_LEN == 0
}

/// Build a RIPng whole-table-request message (RFC 2080 §2.4.1).
///
/// RFC 2080 §2.4.1 specifies that a request for the peer's complete routing
/// table is a Request message carrying exactly one RTE whose prefix is `::`,
/// prefix length is `0`, route tag is `0`, and metric is infinity (16) — the
/// [`RipngRte::whole_table_request`] sentinel.
///
/// This convenience assembles the request stack with the project's layer
/// composition idiom and returns a typed [`Packet`]:
///
/// - an [`Ipv6`](crate::protocols::ip::v6::Ipv6) layer whose source is `source`
///   and whose destination is the well-known RIPng multicast group
///   [`RIPNG_MULTICAST`] (`ff02::9`),
/// - a [`Udp`](crate::protocols::transport::Udp) datagram with source and
///   destination port [`RIPNG_UDP_PORT`] (521), and
/// - a [`Ripng::request`] message carrying the single whole-table sentinel RTE.
///
/// Lengths and checksums are left for [`Packet::compile`] to fill. Callers
/// supply a documentation-range source address (`2001:db8::/32`); the
/// destination is fixed to the RIPng multicast group.
pub fn ripng_whole_table_request(source: std::net::Ipv6Addr) -> Packet {
    use crate::protocols::ip::v6::Ipv6;
    use crate::protocols::transport::Udp;

    Ipv6::new().src(source).dst(RIPNG_MULTICAST)
        / Udp::new().sport(RIPNG_UDP_PORT).dport(RIPNG_UDP_PORT)
        / Ripng::request().rte(RipngRte::whole_table_request())
}

#[cfg(test)]
mod ripng_decode_roundtrips_response {
    use super::*;
    use crate::packet::LayerContext;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_decode_roundtrips_response() {
        // A two-RTE Response message.
        let first = RipngRte::route(
            "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix"),
            64,
            1,
        );
        let second = RipngRte::route(
            "2001:db8:1::".parse::<Ipv6Addr>().expect("valid prefix"),
            48,
            3,
        )
        .route_tag(0x1234);
        let ripng = Ripng::response().with_rtes(vec![first.clone(), second.clone()]);

        // Compile the layer to its wire bytes via a single-layer packet context.
        let packet = Packet::from_layer(ripng.clone());
        let ctx = LayerContext::new(&packet, 0);
        let mut bytes = Vec::new();
        ripng.compile(&ctx, &mut bytes).expect("ripng compiles");

        // Decode reproduces equal command, version, and RTE field values.
        let decoded = decode(&bytes).expect("two-RTE response decodes");
        assert_eq!(decoded.command(), RipCommand::Response);
        assert_eq!(decoded.command_value(), ripng.command_value());
        assert_eq!(decoded.version_value(), ripng.version_value());
        assert_eq!(decoded.rtes().len(), 2);

        // Compare RTEs via their effective values (derived PartialEq on Field is
        // user-vs-default sensitive; decode marks all fields caller-set).
        for (got, want) in decoded.rtes().iter().zip([&first, &second]) {
            assert_eq!(got.prefix_value(), want.prefix_value());
            assert_eq!(got.route_tag_value(), want.route_tag_value());
            assert_eq!(got.prefix_len_value(), want.prefix_len_value());
            assert_eq!(got.metric_value(), want.metric_value());
        }

        // The decoded layer re-compiles to byte-identical wire form.
        let repacket = Packet::from_layer(decoded.clone());
        let rectx = LayerContext::new(&repacket, 0);
        let mut recompiled = Vec::new();
        decoded
            .compile(&rectx, &mut recompiled)
            .expect("decoded ripng recompiles");
        assert_eq!(recompiled, bytes);
    }

    #[test]
    fn short_header_returns_structured_error_without_panic() {
        // 3 octets is fewer than the fixed 4-octet header (RFC 2080 §2).
        let short = [0u8; 3];
        let err = decode(&short).expect_err("3 octets is too short");
        match err {
            CrafterError::BufferTooShort {
                context,
                required,
                available,
            } => {
                assert!(
                    context.contains("RIPng"),
                    "context should mention RIPng, got {context:?}"
                );
                assert_eq!(required, RIPNG_HEADER_LEN);
                assert_eq!(available, short.len());
            }
            other => panic!("expected BufferTooShort, got {other:?}"),
        }
    }

    #[test]
    fn partial_trailing_rte_returns_structured_error() {
        // Valid 4-octet header plus 12 octets: not a whole multiple of 20.
        let mut bytes = vec![0x02, 0x01, 0x00, 0x00];
        bytes.extend_from_slice(&[0u8; 12]);
        let err = decode(&bytes).expect_err("partial RTE is too short");
        match err {
            CrafterError::BufferTooShort {
                required,
                available,
                ..
            } => {
                assert_eq!(required, RIPNG_RTE_LEN);
                assert_eq!(available, 12);
            }
            other => panic!("expected BufferTooShort, got {other:?}"),
        }
    }
}

#[cfg(test)]
mod ripng_summary_mentions_command_and_count {
    use super::*;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_summary_mentions_command_and_count() {
        // A Response message carrying three RTEs.
        let rtes = vec![
            RipngRte::route(
                "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix"),
                64,
                1,
            ),
            RipngRte::route(
                "2001:db8:1::".parse::<Ipv6Addr>().expect("valid prefix"),
                48,
                2,
            ),
            RipngRte::route(
                "2001:db8:2::".parse::<Ipv6Addr>().expect("valid prefix"),
                56,
                3,
            ),
        ];
        let summary = Ripng::response().with_rtes(rtes).summary();

        // The one-line summary names the command, the version, and the RTE count.
        assert!(
            summary.contains("Response"),
            "summary should name the command, got {summary:?}"
        );
        assert!(
            summary.contains("v1"),
            "summary should name the version, got {summary:?}"
        );
        assert!(
            summary.contains('3'),
            "summary should report the RTE count, got {summary:?}"
        );
    }
}

#[cfg(test)]
mod ripng_layer_builder {
    use super::*;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_layer_builder_sets_header_and_rtes() {
        // response() reports a typed Response command and version 1.
        let response = Ripng::response();
        assert_eq!(response.command(), RipCommand::Response);
        assert_eq!(response.command_value(), RIPNG_COMMAND_RESPONSE);
        assert_eq!(response.version_value(), RIPNG_VERSION_1);
        assert!(response.rtes().is_empty());

        // request() reports a typed Request command and version 1.
        let request = Ripng::request();
        assert_eq!(request.command(), RipCommand::Request);
        assert_eq!(request.command_value(), RIPNG_COMMAND_REQUEST);
        assert_eq!(request.version_value(), RIPNG_VERSION_1);

        // new() defaults to a Response command, version 1, reserved 0, no RTEs.
        let default = Ripng::new();
        assert_eq!(default.command(), RipCommand::Response);
        assert_eq!(default.version_value(), RIPNG_VERSION_1);
        assert_eq!(default.reserved_value(), 0);
        assert!(default.rtes().is_empty());

        // .rte(..) appends RTEs in order.
        let prefix = "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix");
        let route = RipngRte::route(prefix, 32, 3);
        let with_one = Ripng::response().rte(route.clone());
        assert_eq!(with_one.rtes().len(), 1);
        assert_eq!(with_one.rtes()[0], route);

        // with_rtes(..) replaces the RTE list.
        let second = RipngRte::route(
            "2001:db8:1::".parse::<Ipv6Addr>().expect("valid prefix"),
            48,
            5,
        );
        let with_two = Ripng::response().with_rtes(vec![route.clone(), second.clone()]);
        assert_eq!(with_two.rtes().len(), 2);
        assert_eq!(with_two.rtes()[1], second);
    }
}

#[cfg(test)]
mod ripng_layer_compiles {
    use super::*;
    use crate::packet::LayerContext;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_layer_compiles_header_and_rtes() {
        let prefix = "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix");
        let ripng = Ripng::response().rte(RipngRte::route(prefix, 64, 1));

        // Compile the layer in isolation via a single-layer packet context.
        let packet = Packet::from_layer(ripng.clone());
        let ctx = LayerContext::new(&packet, 0);
        let mut out = Vec::new();
        ripng.compile(&ctx, &mut out).expect("ripng compiles");

        // RFC 2080 §2 header: command=Response(2), version=1, reserved=0.
        assert_eq!(&out[..RIPNG_HEADER_LEN], &[0x02, 0x01, 0x00, 0x00]);
        // Header plus one 20-octet RTE.
        assert_eq!(out.len(), RIPNG_HEADER_LEN + RIPNG_RTE_LEN);
        assert_eq!(ripng.encoded_len(), RIPNG_HEADER_LEN + RIPNG_RTE_LEN);
    }
}

#[cfg(test)]
mod ripng_layer_div {
    use super::*;
    use crate::protocols::ip::v6::Ipv6;
    use crate::protocols::transport::Udp;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_layer_div_composes_into_packet() {
        let prefix = "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix");
        let ripng = Ripng::response().rte(RipngRte::route(prefix, 64, 1));

        // The `/` operator composes an IPv6/UDP stack and a Ripng layer into a
        // Packet whose layer stack includes the Ripng layer.
        let packet = Ipv6::new()
            .src("2001:db8::1".parse::<Ipv6Addr>().expect("valid source"))
            .dst(RIPNG_MULTICAST)
            / Udp::new().sport(RIPNG_UDP_PORT).dport(RIPNG_UDP_PORT)
            / ripng;
        assert!(packet.layer::<Ripng>().is_some());
    }
}

#[cfg(test)]
mod ripng_udp_binding {
    use super::*;
    use crate::packet::{NetworkLayer, Raw};
    use crate::protocols::ip::v6::Ipv6;
    use crate::protocols::transport::Udp;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_decodes_from_udp_521() {
        // An IPv6/UDP(521)/Ripng Response with one route table entry.
        let prefix = "2001:db8::".parse::<Ipv6Addr>().expect("valid prefix");
        let ripng = Ripng::response().rte(RipngRte::route(prefix, 64, 1));
        let packet = Ipv6::new()
            .src("2001:db8::1".parse::<Ipv6Addr>().expect("valid source"))
            .dst(RIPNG_MULTICAST)
            / Udp::new().sport(RIPNG_UDP_PORT).dport(RIPNG_UDP_PORT)
            / ripng;

        let compiled = packet.compile().expect("ripng stack compiles");

        // The conservative UDP/521 binding routes the payload to the Ripng decoder.
        let decoded = Packet::decode_from_l3(NetworkLayer::Ipv6, compiled.as_bytes())
            .expect("ipv6/udp/ripng decodes");

        let decoded_ripng = decoded
            .layer::<Ripng>()
            .expect("decoded packet includes a Ripng layer");
        assert_eq!(decoded_ripng.command(), RipCommand::Response);
        assert_eq!(decoded_ripng.rtes().len(), 1);
        assert!(decoded.layer::<Raw>().is_none());
    }

    #[test]
    fn ripng_non_ripng_udp_521_stays_raw() {
        // A UDP/521 datagram whose payload fails looks_like_ripng_payload: an
        // unknown command (0xFF) with a non-multiple-of-20 trailing length, so it
        // must fall through to Raw rather than decode as Ripng.
        let payload = vec![0xFFu8, 0xFF, 0x00, 0x00, 0x01, 0x02, 0x03];
        assert!(
            !looks_like_ripng_payload(&payload),
            "fixture payload must not look like RIPng"
        );

        let packet = Ipv6::new()
            .src("2001:db8::1".parse::<Ipv6Addr>().expect("valid source"))
            .dst("2001:db8::2".parse::<Ipv6Addr>().expect("valid dest"))
            / Udp::new().sport(RIPNG_UDP_PORT).dport(RIPNG_UDP_PORT)
            / Raw::from_bytes(&payload);

        let compiled = packet.compile().expect("udp/raw stack compiles");
        let decoded = Packet::decode_from_l3(NetworkLayer::Ipv6, compiled.as_bytes())
            .expect("ipv6/udp/raw decodes");

        assert!(
            decoded.layer::<Ripng>().is_none(),
            "non-RIPng port-521 payload must not decode as Ripng"
        );
        assert!(
            decoded.layer::<Raw>().is_some(),
            "non-RIPng port-521 payload must remain Raw"
        );
    }
}

#[cfg(test)]
mod ripng_whole_table_request_builds {
    use super::*;
    use crate::packet::NetworkLayer;
    use crate::protocols::ip::v6::Ipv6;
    use std::net::Ipv6Addr;

    #[test]
    fn ripng_whole_table_request_builds() {
        let source = "2001:db8::1".parse::<Ipv6Addr>().expect("valid source");
        let packet = ripng_whole_table_request(source);

        // The stack compiles and decodes back to a Ripng layer.
        let compiled = packet.compile().expect("ripng request stack compiles");
        let decoded = Packet::decode_from_l3(NetworkLayer::Ipv6, compiled.as_bytes())
            .expect("ipv6/udp/ripng request decodes");

        // The IPv6 layer targets the well-known RIPng multicast group ff02::9.
        let ipv6 = decoded
            .layer::<Ipv6>()
            .expect("decoded packet includes an Ipv6 layer");
        assert_eq!(ipv6.destination(), RIPNG_MULTICAST);
        assert_eq!(
            RIPNG_MULTICAST,
            "ff02::9".parse::<Ipv6Addr>().expect("ff02::9")
        );

        // The Ripng layer is a Request carrying exactly one whole-table sentinel
        // RTE (RFC 2080 §2.4.1).
        let ripng = decoded
            .layer::<Ripng>()
            .expect("decoded packet includes a Ripng layer");
        assert_eq!(ripng.command(), RipCommand::Request);
        assert_eq!(ripng.rtes().len(), 1);
        assert!(
            ripng.rtes()[0].is_whole_table_request(),
            "the single RTE must be the whole-table-request sentinel"
        );
    }
}