crafter 0.3.0

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
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
//! OSPFv3 link-state advertisement header (RFC 5340 §A.4.2).
//!
//! The OSPFv3 LSA header is 20 octets, like the OSPFv2 header (RFC 2328
//! §A.4.1), but lays its fields out differently: the LS type is a full 16-bit
//! field (carrying scope and the U-bit, RFC 5340 §A.4.2.1) and there is no
//! separate 8-bit Options octet in the header — OSPFv3 carries Options inside
//! the LSA bodies instead:
//!
//! ```text
//!  0                   1                   2                   3
//!  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! |            LS age             |           LS type             |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! |                       Link State ID                           |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! |                     Advertising Router                        |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! |                     LS sequence number                        |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! |         LS checksum           |             length            |
//! +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
//! ```
//!
//! As in OSPFv2 the LS checksum is the Fletcher-16 checksum (RFC 905 Annex B)
//! computed over the LSA from octet 2 (the first octet of the LS type, with the
//! LS age excluded) to the end, with the LS checksum field zeroed. The checksum
//! field still sits at octet 16. The Database Description (RFC 5340 §A.3.3) and
//! Link State Acknowledgment (RFC 5340 §A.3.6) bodies carry bare 20-octet
//! [`Ospfv3LsaHeader`] records with no body. Like the OSPFv2 header,
//! [`Ospfv3LsaHeader`] uses [`Field`] members so `compile()` honors any value
//! the caller pinned while auto-filling the length and checksum.

use core::net::Ipv4Addr;

use crate::checksum::fletcher16_checkbytes;
use crate::field::Field;
use crate::{CrafterError, Result};

/// OSPFv3 LSA header length, in octets. RFC 5340 §A.4.2: LS age(2), LS type(2),
/// Link State ID(4), Advertising Router(4), LS sequence number(4), LS
/// checksum(2), length(2).
pub const OSPFV3_LSA_HEADER_LEN: usize = 20;

/// Default LS sequence number (the initial sequence number `InitialSequenceNumber`,
/// RFC 2328 §12.1.6, reused by OSPFv3 per RFC 5340 §A.4.2).
const OSPFV3_LSA_INITIAL_SEQUENCE_NUMBER: u32 = 0x8000_0001;

/// Offset of the LS checksum field within the OSPFv3 LSA, in octets (RFC 5340
/// §A.4.2).
const OSPFV3_LSA_CHECKSUM_OFFSET: usize = 16;

/// Offset where the Fletcher-protected region begins within the OSPFv3 LSA, in
/// octets: octet 2 (the first octet of the LS type), with the LS age (octets
/// 0..2) excluded (RFC 5340 §A.4.2, mirroring RFC 2328 §12.1.7).
const OSPFV3_LSA_CHECKSUM_START: usize = 2;

/// The 20-octet OSPFv3 LSA header (RFC 5340 §A.4.2).
///
/// Shared by the OSPFv3 Database Description and Link State Acknowledgment
/// bodies (which carry bare LSA headers). Unlike the OSPFv2 header
/// ([`OspfLsaHeader`](crate::protocols::ospf::lsa::OspfLsaHeader)) the LS type
/// is a full 16-bit field and there is no 8-bit Options octet in the header.
/// Each field is a [`Field`] so `compile()` fills the `length` and `ls_checksum`
/// the caller left unset while preserving anything set explicitly, including
/// wrong-on-purpose values.
#[derive(Debug, Clone)]
pub struct Ospfv3LsaHeader {
    /// LS age, in seconds (RFC 5340 §A.4.2); defaults to 0.
    ls_age: Field<u16>,
    /// LS type, a full 16-bit field carrying the LSA scope and U-bit (RFC 5340
    /// §A.4.2.1); left unset until a caller or typed body sets it.
    ls_type: Field<u16>,
    /// Link State ID (RFC 5340 §A.4.2); defaults to the unspecified address.
    link_state_id: Field<Ipv4Addr>,
    /// Advertising Router (RFC 5340 §A.4.2); defaults to the unspecified address.
    advertising_router: Field<Ipv4Addr>,
    /// LS sequence number (RFC 5340 §A.4.2); defaults to the initial sequence
    /// number `0x80000001`.
    ls_sequence_number: Field<u32>,
    /// LS checksum, the Fletcher-16 checksum (RFC 5340 §A.4.2); auto-filled.
    ls_checksum: Field<u16>,
    /// LSA length, in octets, including the 20-octet header (RFC 5340 §A.4.2);
    /// auto-filled to cover the header plus the body.
    length: Field<u16>,
}

impl Ospfv3LsaHeader {
    /// Build a new OSPFv3 LSA header with RFC defaults: LS age 0, the LS sequence
    /// number set to the initial sequence number `0x80000001`, the LS type, Link
    /// State ID, and Advertising Router left unset, and the LS checksum and
    /// length unset so `encode_with_body()` fills them.
    pub fn new() -> Self {
        Self {
            ls_age: Field::defaulted(0),
            ls_type: Field::unset(),
            link_state_id: Field::unset(),
            advertising_router: Field::unset(),
            ls_sequence_number: Field::defaulted(OSPFV3_LSA_INITIAL_SEQUENCE_NUMBER),
            ls_checksum: Field::unset(),
            length: Field::unset(),
        }
    }

    /// Construct an OSPFv3 LSA header from decoded wire fields, marking every
    /// field as caller-supplied so re-compilation preserves the decoded values
    /// byte-for-byte.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn from_decoded_parts(
        ls_age: u16,
        ls_type: u16,
        link_state_id: Ipv4Addr,
        advertising_router: Ipv4Addr,
        ls_sequence_number: u32,
        ls_checksum: u16,
        length: u16,
    ) -> Self {
        Self {
            ls_age: Field::user(ls_age),
            ls_type: Field::user(ls_type),
            link_state_id: Field::user(link_state_id),
            advertising_router: Field::user(advertising_router),
            ls_sequence_number: Field::user(ls_sequence_number),
            ls_checksum: Field::user(ls_checksum),
            length: Field::user(length),
        }
    }

    /// Set the LS age field, in seconds.
    pub fn ls_age(mut self, ls_age: u16) -> Self {
        self.ls_age.set_user(ls_age);
        self
    }

    /// Set the 16-bit LS type field (RFC 5340 §A.4.2.1).
    pub fn ls_type(mut self, ls_type: u16) -> Self {
        self.ls_type.set_user(ls_type);
        self
    }

    /// Set the Link State ID field.
    pub fn link_state_id(mut self, link_state_id: impl Into<Ipv4Addr>) -> Self {
        self.link_state_id.set_user(link_state_id.into());
        self
    }

    /// Set the Advertising Router field.
    pub fn advertising_router(mut self, advertising_router: impl Into<Ipv4Addr>) -> Self {
        self.advertising_router.set_user(advertising_router.into());
        self
    }

    /// Set the LS sequence number field.
    pub fn ls_sequence_number(mut self, ls_sequence_number: u32) -> Self {
        self.ls_sequence_number.set_user(ls_sequence_number);
        self
    }

    /// Force the LS checksum field.
    ///
    /// This preserves malformed-on-purpose LSAs whose checksum is not the
    /// computed Fletcher-16 checksum.
    pub fn ls_checksum(mut self, ls_checksum: u16) -> Self {
        self.ls_checksum.set_user(ls_checksum);
        self
    }

    /// Force the LSA length field.
    ///
    /// This preserves malformed-on-purpose LSAs whose declared length differs
    /// from the emitted byte count.
    pub fn length(mut self, length: u16) -> Self {
        self.length.set_user(length);
        self
    }

    /// The effective LS age (the caller value, else 0).
    pub fn ls_age_value(&self) -> u16 {
        self.ls_age.value().copied().unwrap_or(0)
    }

    /// The effective 16-bit LS type (the caller value, else 0).
    pub fn ls_type_value(&self) -> u16 {
        self.ls_type.value().copied().unwrap_or(0)
    }

    /// The effective Link State ID (the caller value, else the unspecified
    /// address).
    pub fn link_state_id_value(&self) -> Ipv4Addr {
        self.link_state_id
            .value()
            .copied()
            .unwrap_or(Ipv4Addr::UNSPECIFIED)
    }

    /// The effective Advertising Router (the caller value, else the unspecified
    /// address).
    pub fn advertising_router_value(&self) -> Ipv4Addr {
        self.advertising_router
            .value()
            .copied()
            .unwrap_or(Ipv4Addr::UNSPECIFIED)
    }

    /// The effective LS sequence number (the caller value, else the initial
    /// sequence number `0x80000001`).
    pub fn ls_sequence_number_value(&self) -> u32 {
        self.ls_sequence_number
            .value()
            .copied()
            .unwrap_or(OSPFV3_LSA_INITIAL_SEQUENCE_NUMBER)
    }

    /// The pinned LS checksum, if the caller set it.
    pub fn ls_checksum_value(&self) -> Option<u16> {
        self.ls_checksum.value().copied()
    }

    /// The pinned LSA length, if the caller set it.
    pub fn length_value(&self) -> Option<u16> {
        self.length.value().copied()
    }

    /// A one-line summary of the OSPFv3 LSA header for `summary()` /
    /// `inspection_fields()`, like
    /// `LSAv3(type=0x2001, id=192.0.2.1, adv=192.0.2.1, seq=0x80000001, age=0, len=...)`.
    ///
    /// The length shows the pinned value when the caller set one and `auto`
    /// otherwise (the effective length depends on the LSA body, which the header
    /// alone does not hold).
    pub fn summary(&self) -> String {
        let length = match self.length_value() {
            Some(length) => length.to_string(),
            None => "auto".to_string(),
        };
        format!(
            "LSAv3(type=0x{:04x}, id={}, adv={}, seq=0x{:08x}, age={}, len={})",
            self.ls_type_value(),
            self.link_state_id_value(),
            self.advertising_router_value(),
            self.ls_sequence_number_value(),
            self.ls_age_value(),
            length,
        )
    }

    /// Append the 20-octet OSPFv3 LSA header followed by `body` to `out`.
    ///
    /// The `length` field is filled with `20 + body.len()` unless the caller
    /// pinned it, and the LS checksum is filled with the Fletcher-16 checksum
    /// (RFC 905 Annex B) over the LSA from octet 2 to the end (LS age excluded),
    /// with the checksum field zeroed, unless the caller pinned it.
    pub fn encode_with_body(&self, body: &[u8], out: &mut Vec<u8>) {
        let start = out.len();

        // LS age (octets 0..2).
        out.extend_from_slice(&self.ls_age_value().to_be_bytes());
        // LS type, a full 16-bit field (octets 2..4).
        out.extend_from_slice(&self.ls_type_value().to_be_bytes());
        // Link State ID (octets 4..8) and Advertising Router (octets 8..12).
        out.extend_from_slice(&self.link_state_id_value().octets());
        out.extend_from_slice(&self.advertising_router_value().octets());
        // LS sequence number (octets 12..16).
        out.extend_from_slice(&self.ls_sequence_number_value().to_be_bytes());

        // LS checksum placeholder (octets 16..18): zeroed so the auto-fill below
        // can compute over the LSA-with-hole; a pinned checksum is written
        // verbatim.
        let pinned_checksum = self.ls_checksum.value().copied();
        out.extend_from_slice(&pinned_checksum.unwrap_or(0).to_be_bytes());

        // Length (octets 18..20): the caller value, else the 20-octet header
        // plus the body.
        let length = self
            .length
            .value()
            .copied()
            .unwrap_or((OSPFV3_LSA_HEADER_LEN + body.len()) as u16);
        out.extend_from_slice(&length.to_be_bytes());

        // LSA body follows the 20-octet header.
        out.extend_from_slice(body);

        // Auto-fill the LS checksum unless the caller pinned it. The checksum is
        // the Fletcher-16 checksum over the LSA from octet 2 (the first octet of
        // the LS type, LS age excluded) to the end, with the checksum field
        // zeroed (already zeroed by the placeholder above). The checksum field
        // sits at octet 16 of the full LSA, i.e. offset 14 within the octet-2
        // protected slice.
        if pinned_checksum.is_none() {
            let protected = &out[start + OSPFV3_LSA_CHECKSUM_START..];
            let checkbytes = fletcher16_checkbytes(
                protected,
                OSPFV3_LSA_CHECKSUM_OFFSET - OSPFV3_LSA_CHECKSUM_START,
            );
            out[start + OSPFV3_LSA_CHECKSUM_OFFSET..start + OSPFV3_LSA_CHECKSUM_OFFSET + 2]
                .copy_from_slice(&checkbytes);
        }
    }

    /// Decode a 20-octet OSPFv3 LSA header from the front of `bytes`.
    ///
    /// Returns the parsed [`Ospfv3LsaHeader`] (with every field marked
    /// caller-supplied so the LSA round-trips byte-for-byte) and the declared
    /// LSA length read from the header, which spans the header plus the body.
    /// A buffer shorter than 20 octets yields a structured
    /// [`buffer_too_short`](CrafterError::buffer_too_short) error rather than a
    /// panic.
    pub fn decode(bytes: &[u8]) -> Result<(Ospfv3LsaHeader, usize)> {
        if bytes.len() < OSPFV3_LSA_HEADER_LEN {
            return Err(CrafterError::buffer_too_short(
                "ospfv3 lsa header",
                OSPFV3_LSA_HEADER_LEN,
                bytes.len(),
            ));
        }

        let ls_age = u16::from_be_bytes([bytes[0], bytes[1]]);
        let ls_type = u16::from_be_bytes([bytes[2], bytes[3]]);
        let link_state_id = Ipv4Addr::new(bytes[4], bytes[5], bytes[6], bytes[7]);
        let advertising_router = Ipv4Addr::new(bytes[8], bytes[9], bytes[10], bytes[11]);
        let ls_sequence_number = u32::from_be_bytes([bytes[12], bytes[13], bytes[14], bytes[15]]);
        let ls_checksum = u16::from_be_bytes([bytes[16], bytes[17]]);
        let length = u16::from_be_bytes([bytes[18], bytes[19]]);

        let header = Ospfv3LsaHeader::from_decoded_parts(
            ls_age,
            ls_type,
            link_state_id,
            advertising_router,
            ls_sequence_number,
            ls_checksum,
            length,
        );

        Ok((header, length as usize))
    }
}

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

/// Decode a trailing list of bare 20-octet OSPFv3 LSA headers.
///
/// The OSPFv3 Database Description (RFC 5340 §A.3.3) and Link State
/// Acknowledgment (RFC 5340 §A.3.6) bodies carry a sequence of LSA headers with
/// no body, so every entry is exactly [`OSPFV3_LSA_HEADER_LEN`] octets
/// regardless of the declared `length` field. This loops while `bytes` is
/// non-empty, decoding one header and advancing the cursor by 20 octets each
/// iteration, mirroring [`decode_lsa_headers`](crate::protocols::ospf::lsa). A
/// remainder shorter than 20 octets yields a structured
/// [`buffer_too_short`](CrafterError::buffer_too_short) error rather than a
/// panic.
///
/// The typed OSPFv3 Database Description and Link State Acknowledgment decoders
/// (`crate::protocols::ospf::v3::decode`) consume this parser.
pub(crate) fn decode_ospfv3_lsa_headers(mut bytes: &[u8]) -> Result<Vec<Ospfv3LsaHeader>> {
    let mut headers = Vec::new();
    while !bytes.is_empty() {
        if bytes.len() < OSPFV3_LSA_HEADER_LEN {
            return Err(CrafterError::buffer_too_short(
                "ospfv3 lsa header",
                OSPFV3_LSA_HEADER_LEN,
                bytes.len(),
            ));
        }
        let (header, _declared_len) = Ospfv3LsaHeader::decode(bytes)?;
        headers.push(header);
        bytes = &bytes[OSPFV3_LSA_HEADER_LEN..];
    }
    Ok(headers)
}

/// Encode a list of bare OSPFv3 LSA headers, each as a header-only 20-octet
/// record.
///
/// Each header is emitted with an empty body via
/// [`Ospfv3LsaHeader::encode_with_body`], so every entry is exactly
/// [`OSPFV3_LSA_HEADER_LEN`] octets. Shared by the OSPFv3 Database Description
/// and Link State Acknowledgment encoders, which carry only LSA headers
/// (RFC 5340 §A.3.3, §A.3.6).
pub(crate) fn encode_ospfv3_lsa_headers(headers: &[Ospfv3LsaHeader], out: &mut Vec<u8>) {
    for header in headers {
        header.encode_with_body(&[], out);
    }
}

/// Mask selecting the low 24 bits of an OSPFv3 LSA-body Options field (RFC 5340
/// §A.2): the field is encoded as three octets on the wire, like the Options in
/// the Database Description (RFC 5340 §A.3.3).
const OSPFV3_LSA_OPTIONS_MASK: u32 = 0x00ff_ffff;

/// The on-wire length of a single OSPFv3 Router-LSA interface description, in
/// octets: Type(1) + Reserved(1) + Metric(2) + Interface ID(4) + Neighbor
/// Interface ID(4) + Neighbor Router ID(4). RFC 5340 §A.4.3.
const OSPFV3_ROUTER_LSA_INTERFACE_LEN: usize = 16;

/// The fixed (pre-interface-list) length of the OSPFv3 Router-LSA body, in
/// octets: flags(1) + Options(3). RFC 5340 §A.4.3.
const OSPFV3_ROUTER_LSA_FIXED_LEN: usize = 4;

/// The fixed (pre-router-list) length of the OSPFv3 Network-LSA body, in octets:
/// Reserved(1) + Options(3). RFC 5340 §A.4.4.
const OSPFV3_NETWORK_LSA_FIXED_LEN: usize = 4;

/// The on-wire length of a single OSPFv3 Network-LSA attached Router ID, in
/// octets (RFC 5340 §A.4.4).
const OSPFV3_NETWORK_LSA_ROUTER_LEN: usize = 4;

/// A single OSPFv3 Router-LSA interface description (RFC 5340 §A.4.3).
///
/// Each description is the 16-octet record naming one of the originating
/// router's adjacencies: a link Type, a Reserved octet, the Metric (cost), the
/// Interface ID, the Neighbor Interface ID, and the Neighbor Router ID. Unlike
/// the OSPFv2 Router-LSA link descriptions (RFC 2328 §A.4.2), OSPFv3 carries no
/// per-TOS entries and identifies links by interface IDs rather than addresses.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Ospfv3RouterInterface {
    /// The interface (link) Type (RFC 5340 §A.4.3): point-to-point, transit,
    /// reserved, or virtual link.
    if_type: u8,
    /// The Reserved octet (RFC 5340 §A.4.3); emitted as zero by default but
    /// settable so a malformed-on-purpose record can carry a non-zero value.
    reserved: u8,
    /// The Metric (cost) of using this interface (RFC 5340 §A.4.3).
    metric: u16,
    /// The Interface ID the originating router assigned to this link (RFC 5340
    /// §A.4.3).
    interface_id: u32,
    /// The Neighbor Interface ID (RFC 5340 §A.4.3).
    neighbor_interface_id: u32,
    /// The Neighbor Router ID (RFC 5340 §A.4.3).
    neighbor_router_id: Ipv4Addr,
}

impl Ospfv3RouterInterface {
    /// Build an OSPFv3 Router-LSA interface description from its fields. The
    /// Reserved octet is emitted as zero; use [`reserved`](Self::reserved) to
    /// pin a non-zero value for a malformed-on-purpose record.
    pub fn new(
        if_type: u8,
        metric: u16,
        interface_id: u32,
        neighbor_interface_id: u32,
        neighbor_router_id: impl Into<Ipv4Addr>,
    ) -> Self {
        Self {
            if_type,
            reserved: 0,
            metric,
            interface_id,
            neighbor_interface_id,
            neighbor_router_id: neighbor_router_id.into(),
        }
    }

    /// Set the Reserved octet (RFC 5340 §A.4.3). Normally 0; settable so a
    /// malformed-on-purpose record can carry a non-zero value.
    pub fn reserved(mut self, reserved: u8) -> Self {
        self.reserved = reserved;
        self
    }

    /// The interface (link) Type (RFC 5340 §A.4.3).
    pub fn if_type_value(&self) -> u8 {
        self.if_type
    }

    /// The Reserved octet (RFC 5340 §A.4.3).
    pub fn reserved_value(&self) -> u8 {
        self.reserved
    }

    /// The Metric (cost) of using this interface (RFC 5340 §A.4.3).
    pub fn metric_value(&self) -> u16 {
        self.metric
    }

    /// The Interface ID (RFC 5340 §A.4.3).
    pub fn interface_id_value(&self) -> u32 {
        self.interface_id
    }

    /// The Neighbor Interface ID (RFC 5340 §A.4.3).
    pub fn neighbor_interface_id_value(&self) -> u32 {
        self.neighbor_interface_id
    }

    /// The Neighbor Router ID (RFC 5340 §A.4.3).
    pub fn neighbor_router_id_value(&self) -> Ipv4Addr {
        self.neighbor_router_id
    }

    /// Append this interface description as its 16 big-endian octets — Type(1),
    /// Reserved(1), Metric(2), Interface ID(4), Neighbor Interface ID(4),
    /// Neighbor Router ID(4) — to `out` (RFC 5340 §A.4.3).
    fn encode(&self, out: &mut Vec<u8>) {
        out.push(self.if_type);
        out.push(self.reserved);
        out.extend_from_slice(&self.metric.to_be_bytes());
        out.extend_from_slice(&self.interface_id.to_be_bytes());
        out.extend_from_slice(&self.neighbor_interface_id.to_be_bytes());
        out.extend_from_slice(&self.neighbor_router_id.octets());
    }
}

/// OSPFv3 Router-LSA body (RFC 5340 §A.4.3).
///
/// The Router-LSA (LS type function code 1, i.e. LS type `0x2001` with the
/// area scope and U-bit) describes the originating router's interfaces to an
/// area. It follows the 20-octet [`Ospfv3LsaHeader`] and is a 4-octet fixed
/// prefix — a router-description flags octet then the 24-bit Options — followed
/// by zero or more 16-octet interface descriptions:
///
/// ```text
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |    0    |Nt|x|V|E|B|            Options                        |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |     Type      |       0       |          Metric              |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                       Interface ID                           |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                    Neighbor Interface ID                     |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                    Neighbor Router ID                        |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                              ...                            ...
/// ```
///
/// Like the other LSA bodies, [`Ospfv3RouterLsa`] rides inside an [`Ospfv3Lsa`]
/// as an [`Ospfv3LsaBody::Router`] variant, and `Ospfv3Lsa::encode` auto-fills
/// the enclosing LSA `length` and the Fletcher-16 checksum over the header plus
/// this body. The flags and 24-bit Options use [`Field`] members so `compile()`
/// honors any value the caller pinned.
#[derive(Debug, Clone)]
pub struct Ospfv3RouterLsa {
    /// The router-description flags octet (RFC 5340 §A.4.3); defaults to 0.
    flags: Field<u8>,
    /// Optional capabilities (RFC 5340 §A.2), 24-bit; defaults to 0. Only the low
    /// 24 bits are emitted on the wire.
    options: Field<u32>,
    /// The interface descriptions, in order (RFC 5340 §A.4.3).
    interfaces: Vec<Ospfv3RouterInterface>,
}

impl Ospfv3RouterLsa {
    /// Build an OSPFv3 Router-LSA body with the flags and Options defaulted to 0
    /// and an empty interface-description list.
    pub fn new() -> Self {
        Self {
            flags: Field::defaulted(0),
            options: Field::defaulted(0),
            interfaces: Vec::new(),
        }
    }

    /// Set the router-description flags octet (RFC 5340 §A.4.3).
    pub fn flags(mut self, flags: u8) -> Self {
        self.flags.set_user(flags);
        self
    }

    /// Set the Options field (RFC 5340 §A.2 capability bits, 24-bit). Only the low
    /// 24 bits are emitted on the wire.
    pub fn options(mut self, options: u32) -> Self {
        self.options.set_user(options);
        self
    }

    /// Append a single interface description to the Router-LSA.
    pub fn interface(mut self, interface: Ospfv3RouterInterface) -> Self {
        self.interfaces.push(interface);
        self
    }

    /// Append several interface descriptions to the Router-LSA.
    pub fn interfaces<I>(mut self, interfaces: I) -> Self
    where
        I: IntoIterator<Item = Ospfv3RouterInterface>,
    {
        self.interfaces.extend(interfaces);
        self
    }

    /// The effective router-description flags octet (the caller value, else 0).
    pub fn flags_value(&self) -> u8 {
        self.flags.value().copied().unwrap_or(0)
    }

    /// The effective Options field, masked to the low 24 bits emitted on the wire
    /// (the caller value, else 0).
    pub fn options_value(&self) -> u32 {
        self.options.value().copied().unwrap_or(0) & OSPFV3_LSA_OPTIONS_MASK
    }

    /// The interface descriptions, in order (RFC 5340 §A.4.3).
    pub fn interfaces_value(&self) -> &[Ospfv3RouterInterface] {
        &self.interfaces
    }

    /// The on-wire length of this Router-LSA body, in octets: the 4-octet fixed
    /// prefix plus 16 octets per interface description.
    pub(crate) fn encoded_len(&self) -> usize {
        OSPFV3_ROUTER_LSA_FIXED_LEN + self.interfaces.len() * OSPFV3_ROUTER_LSA_INTERFACE_LEN
    }

    /// Append the RFC 5340 §A.4.3 OSPFv3 Router-LSA body to `out`: the flags octet
    /// and the 24-bit Options packed into three octets, then each 16-octet
    /// interface description.
    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        // flags(1), then the 24-bit Options as three big-endian octets.
        out.push(self.flags_value());
        let options = self.options_value();
        out.push(((options >> 16) & 0xff) as u8);
        out.push(((options >> 8) & 0xff) as u8);
        out.push((options & 0xff) as u8);
        for interface in &self.interfaces {
            interface.encode(out);
        }
    }
}

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

/// OSPFv3 Network-LSA body (RFC 5340 §A.4.4).
///
/// The Network-LSA (LS type function code 2, i.e. LS type `0x2002`) is
/// originated for each transit network by its designated router. It follows the
/// 20-octet [`Ospfv3LsaHeader`] and is a 4-octet fixed prefix — a Reserved octet
/// then the 24-bit Options — followed by the Router IDs of each router attached
/// to the network (including the designated router itself):
///
/// ```text
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |       0       |                  Options                      |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                        Attached Router                        |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                              ...                              |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// ```
///
/// Unlike the OSPFv2 Network-LSA (RFC 2328 §A.4.3) there is no network-mask
/// field — OSPFv3 addressing lives in separate Intra-Area-Prefix-LSAs. Like the
/// other LSA bodies, [`Ospfv3NetworkLsa`] rides inside an [`Ospfv3Lsa`] as an
/// [`Ospfv3LsaBody::Network`] variant, and `Ospfv3Lsa::encode` auto-fills the
/// enclosing LSA `length` and the Fletcher-16 checksum. The Reserved octet and
/// 24-bit Options use [`Field`] members so `compile()` honors any pinned value.
#[derive(Debug, Clone)]
pub struct Ospfv3NetworkLsa {
    /// The Reserved octet (RFC 5340 §A.4.4); defaults to 0. Settable so a
    /// malformed-on-purpose body can carry a non-zero value.
    reserved: Field<u8>,
    /// Optional capabilities (RFC 5340 §A.2), 24-bit; defaults to 0. Only the low
    /// 24 bits are emitted on the wire.
    options: Field<u32>,
    /// The Router IDs of each attached router, in order (RFC 5340 §A.4.4).
    attached_routers: Vec<Ipv4Addr>,
}

impl Ospfv3NetworkLsa {
    /// Build an OSPFv3 Network-LSA body with the Reserved octet and Options
    /// defaulted to 0 and an empty attached-router list.
    pub fn new() -> Self {
        Self {
            reserved: Field::defaulted(0),
            options: Field::defaulted(0),
            attached_routers: Vec::new(),
        }
    }

    /// Set the Reserved octet (RFC 5340 §A.4.4). Normally 0; settable so a
    /// malformed-on-purpose body can carry a non-zero value.
    pub fn reserved(mut self, reserved: u8) -> Self {
        self.reserved.set_user(reserved);
        self
    }

    /// Set the Options field (RFC 5340 §A.2 capability bits, 24-bit). Only the low
    /// 24 bits are emitted on the wire.
    pub fn options(mut self, options: u32) -> Self {
        self.options.set_user(options);
        self
    }

    /// Append a single attached-router Router ID to the Network-LSA.
    pub fn attached_router(mut self, router_id: impl Into<Ipv4Addr>) -> Self {
        self.attached_routers.push(router_id.into());
        self
    }

    /// Append several attached-router Router IDs to the Network-LSA.
    pub fn attached_routers<I>(mut self, routers: I) -> Self
    where
        I: IntoIterator<Item = Ipv4Addr>,
    {
        self.attached_routers.extend(routers);
        self
    }

    /// The effective Reserved octet (the caller value, else 0).
    pub fn reserved_value(&self) -> u8 {
        self.reserved.value().copied().unwrap_or(0)
    }

    /// The effective Options field, masked to the low 24 bits emitted on the wire
    /// (the caller value, else 0).
    pub fn options_value(&self) -> u32 {
        self.options.value().copied().unwrap_or(0) & OSPFV3_LSA_OPTIONS_MASK
    }

    /// The attached-router Router IDs, in order (RFC 5340 §A.4.4).
    pub fn attached_routers_value(&self) -> &[Ipv4Addr] {
        &self.attached_routers
    }

    /// The on-wire length of this Network-LSA body, in octets: the 4-octet fixed
    /// prefix plus 4 octets per attached router.
    pub(crate) fn encoded_len(&self) -> usize {
        OSPFV3_NETWORK_LSA_FIXED_LEN + self.attached_routers.len() * OSPFV3_NETWORK_LSA_ROUTER_LEN
    }

    /// Append the RFC 5340 §A.4.4 OSPFv3 Network-LSA body to `out`: the Reserved
    /// octet and the 24-bit Options packed into three octets, then each attached
    /// Router ID (4 octets each).
    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        // Reserved(1), then the 24-bit Options as three big-endian octets.
        out.push(self.reserved_value());
        let options = self.options_value();
        out.push(((options >> 16) & 0xff) as u8);
        out.push(((options >> 8) & 0xff) as u8);
        out.push((options & 0xff) as u8);
        for router in &self.attached_routers {
            out.extend_from_slice(&router.octets());
        }
    }
}

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

/// The type-specific body of an OSPFv3 LSA, following the 20-octet
/// [`Ospfv3LsaHeader`] (RFC 5340 §A.4).
///
/// This block models the typed [`Ospfv3LsaBody::Router`] (RFC 5340 §A.4.3) and
/// [`Ospfv3LsaBody::Network`] (RFC 5340 §A.4.4) bodies, plus the
/// [`Ospfv3LsaBody::Raw`] variant, which preserves the LSA body bytes verbatim
/// for an LSA type the builder does not (yet) model so they round-trip
/// byte-for-byte (mirroring the OSPFv2
/// [`OspfLsaBody`](crate::protocols::ospf::lsa::OspfLsaBody) family). Further
/// typed v3 LSA bodies are added by later steps.
#[derive(Debug, Clone)]
pub enum Ospfv3LsaBody {
    /// An LSA body the container does not (yet) model, preserved verbatim. The
    /// bytes are everything after the 20-octet LSA header.
    Raw(Vec<u8>),
    /// A Router-LSA body (LS type function code 1), the originating router's
    /// interface descriptions to an area (RFC 5340 §A.4.3).
    Router(Ospfv3RouterLsa),
    /// A Network-LSA body (LS type function code 2), the Router IDs of each
    /// router attached to a transit network (RFC 5340 §A.4.4).
    Network(Ospfv3NetworkLsa),
}

impl Ospfv3LsaBody {
    /// The on-wire length of this LSA body, in octets (the bytes after the
    /// 20-octet header).
    pub(crate) fn encoded_len(&self) -> usize {
        match self {
            Ospfv3LsaBody::Raw(body) => body.len(),
            Ospfv3LsaBody::Router(router) => router.encoded_len(),
            Ospfv3LsaBody::Network(network) => network.encoded_len(),
        }
    }

    /// Append this LSA body's bytes to `out`. The typed variants serialize their
    /// fields per the RFC; the [`Ospfv3LsaBody::Raw`] variant writes its bytes
    /// verbatim.
    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        match self {
            Ospfv3LsaBody::Raw(body) => out.extend_from_slice(body),
            Ospfv3LsaBody::Router(router) => router.encode(out),
            Ospfv3LsaBody::Network(network) => network.encode(out),
        }
    }
}

/// A complete OSPFv3 link-state advertisement: the 20-octet [`Ospfv3LsaHeader`]
/// (RFC 5340 §A.4.2) immediately followed by its type-specific
/// [`Ospfv3LsaBody`].
///
/// The header's `length` field spans the header plus the body, and the LS
/// checksum is the Fletcher-16 checksum over the LSA from octet 2 (LS age
/// excluded) to the end. `Ospfv3Lsa::encode` auto-fills both over header +
/// body unless the caller pinned them — exactly as the OSPFv2
/// [`OspfLsa`](crate::protocols::ospf::lsa::OspfLsa) does — so an LSA built with
/// the crate is protocol-correct by default while deliberately malformed
/// length/checksum values survive untouched.
#[derive(Debug, Clone)]
pub struct Ospfv3Lsa {
    /// The 20-octet OSPFv3 LSA header (RFC 5340 §A.4.2).
    pub header: Ospfv3LsaHeader,
    /// The type-specific LSA body following the header.
    pub body: Ospfv3LsaBody,
}

impl Ospfv3Lsa {
    /// Build an OSPFv3 LSA from its header and body.
    pub fn new(header: Ospfv3LsaHeader, body: Ospfv3LsaBody) -> Self {
        Self { header, body }
    }

    /// The on-wire length of this LSA, in octets: the 20-octet header plus the
    /// body.
    pub(crate) fn encoded_len(&self) -> usize {
        OSPFV3_LSA_HEADER_LEN + self.body.encoded_len()
    }

    /// Append the complete OSPFv3 LSA (header + body) to `out`.
    ///
    /// The body is serialized to a scratch buffer first, then the header is
    /// emitted via [`Ospfv3LsaHeader::encode_with_body`] so the `length` field is
    /// filled with `20 + body.len()` and the LS Fletcher checksum is filled over
    /// the LSA from octet 2 (LS age excluded) — each unless the caller pinned it.
    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        let mut body_bytes = Vec::with_capacity(self.body.encoded_len());
        self.body.encode(&mut body_bytes);
        self.header.encode_with_body(&body_bytes, out);
    }
}

/// The on-wire length of the OSPFv3 Link State Update `# LSAs` count field, in
/// octets (RFC 5340 §A.3.5).
const OSPFV3_LSU_COUNT_LEN: usize = 4;

/// OSPFv3 Link State Update packet body (RFC 5340 §A.3.5).
///
/// The Link State Update body follows the 16-octet OSPFv3 common header (RFC
/// 5340 §A.3.1) and carries the flooded LSAs themselves. It begins with a
/// 4-octet count of advertisements and is then the concatenation of that many
/// complete OSPFv3 LSAs:
///
/// ```text
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                            # LSAs                             |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                             LSAs                              |
/// +-                                                            +-+
/// |                              ...                             ...
/// ```
///
/// Each LSA is a 20-octet [`Ospfv3LsaHeader`] (RFC 5340 §A.4.2) immediately
/// followed by its type-specific body; the header's `length` field covers the
/// header plus the body. The `# LSAs` count auto-fills from the number of
/// carried LSAs unless the caller pinned it, so a deliberately wrong count
/// survives `compile()`. Like the other v3 bodies, [`Ospfv3LinkStateUpdate`]
/// rides inside the [`Ospfv3`](crate::protocols::ospf::Ospfv3) layer as an
/// [`Ospfv3Body::LinkStateUpdate`](crate::protocols::ospf::Ospfv3Body::LinkStateUpdate)
/// variant; its `encode()`/`encoded_len()` are what the layer's `compile()`
/// routes to.
#[derive(Debug, Clone)]
pub struct Ospfv3LinkStateUpdate {
    /// The `# LSAs` count (RFC 5340 §A.3.5). Left unset so `encode()` fills it
    /// with the number of carried LSAs; a pinned value (including a deliberately
    /// wrong one) survives untouched.
    num_lsas: Field<u32>,
    /// The carried LSAs, in order.
    lsas: Vec<Ospfv3Lsa>,
}

impl Ospfv3LinkStateUpdate {
    /// Build an OSPFv3 Link State Update body with an empty LSA list and an unset
    /// count.
    pub fn new() -> Self {
        Self {
            num_lsas: Field::unset(),
            lsas: Vec::new(),
        }
    }

    /// Append a single LSA to the update's LSA list.
    pub fn lsa(mut self, lsa: Ospfv3Lsa) -> Self {
        self.lsas.push(lsa);
        self
    }

    /// Append several LSAs to the update's LSA list.
    pub fn lsas<I>(mut self, lsas: I) -> Self
    where
        I: IntoIterator<Item = Ospfv3Lsa>,
    {
        self.lsas.extend(lsas);
        self
    }

    /// Force the `# LSAs` count field (RFC 5340 §A.3.5).
    ///
    /// This preserves malformed-on-purpose updates whose declared count differs
    /// from the number of carried LSAs.
    pub fn num_lsas(mut self, num_lsas: u32) -> Self {
        self.num_lsas.set_user(num_lsas);
        self
    }

    /// The carried LSAs, in order.
    pub fn lsas_value(&self) -> &[Ospfv3Lsa] {
        &self.lsas
    }

    /// The effective `# LSAs` count: the caller value, else the number of carried
    /// LSAs.
    pub fn num_lsas_value(&self) -> u32 {
        self.num_lsas
            .value()
            .copied()
            .unwrap_or(self.lsas.len() as u32)
    }

    /// The on-wire length of this Link State Update body, in octets: the 4-octet
    /// count plus the total size of every carried LSA (each 20-octet header plus
    /// its body).
    pub(crate) fn encoded_len(&self) -> usize {
        OSPFV3_LSU_COUNT_LEN + self.lsas.iter().map(Ospfv3Lsa::encoded_len).sum::<usize>()
    }

    /// Append the RFC 5340 §A.3.5 OSPFv3 Link State Update body to `out`: the
    /// `# LSAs` count (the caller value, else the number of carried LSAs), then
    /// each LSA (header + body) with its length and LS checksum auto-filled.
    pub(crate) fn encode(&self, out: &mut Vec<u8>) {
        out.extend_from_slice(&self.num_lsas_value().to_be_bytes());
        for lsa in &self.lsas {
            lsa.encode(out);
        }
    }
}

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

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

    /// An OSPFv3 LSA header built over a small body round-trips through
    /// `encode_with_body` / `decode`: the auto-filled length covers the 20-octet
    /// header plus the body, the Fletcher checksum validates, the 16-bit LS type
    /// occupies octets 2..4, and the decoded fields equal the built ones.
    #[test]
    fn ospfv3_lsa_header_round_trips_with_auto_length_and_checksum() {
        let body = [0xde, 0xad, 0xbe, 0xef, 0x01, 0x02];
        // 0x2001 is the OSPFv3 Router-LSA function code with the U-bit and the
        // area scope bits set (RFC 5340 §A.4.2.1); here it just exercises a full
        // 16-bit LS type value.
        let header = Ospfv3LsaHeader::new()
            .ls_age(0)
            .ls_type(0x2001)
            .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
            .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
            .ls_sequence_number(0x8000_0001);

        let mut out = Vec::new();
        header.encode_with_body(&body, &mut out);

        // The emitted LSA is the 20-octet header plus the body.
        assert_eq!(out.len(), OSPFV3_LSA_HEADER_LEN + body.len());

        // The 16-bit LS type occupies octets 2..4 (no 8-bit Options octet, unlike
        // OSPFv2).
        assert_eq!(&out[2..4], &0x2001u16.to_be_bytes());

        // The auto-filled length field (octets 18..20) covers header + body.
        let expected_len = (OSPFV3_LSA_HEADER_LEN + body.len()) as u16;
        assert_eq!(&out[18..20], &expected_len.to_be_bytes());

        // The Fletcher-16 checksum over the whole LSA validates.
        assert!(
            fletcher16_valid(&out),
            "auto-filled Fletcher checksum should validate over the OSPFv3 LSA"
        );

        // Decoding returns the parsed header and the declared LSA length.
        let (decoded, declared_len) =
            Ospfv3LsaHeader::decode(&out).expect("OSPFv3 LSA header decodes");
        assert_eq!(declared_len, OSPFV3_LSA_HEADER_LEN + body.len());

        // The decoded fields equal the built ones.
        assert_eq!(decoded.ls_age_value(), 0);
        assert_eq!(decoded.ls_type_value(), 0x2001);
        assert_eq!(decoded.link_state_id_value(), Ipv4Addr::new(192, 0, 2, 1));
        assert_eq!(
            decoded.advertising_router_value(),
            Ipv4Addr::new(192, 0, 2, 1)
        );
        assert_eq!(decoded.ls_sequence_number_value(), 0x8000_0001);
        assert_eq!(decoded.length_value(), Some(expected_len));

        // Re-encoding the decoded header (its checksum is now user-set) with the
        // same body reproduces the original LSA bytes.
        let mut reencoded = Vec::new();
        decoded.encode_with_body(&body, &mut reencoded);
        assert_eq!(reencoded, out);
    }

    /// A caller-set LS checksum survives `encode_with_body` untouched, so
    /// malformed-on-purpose OSPFv3 LSAs keep their wrong checksum.
    #[test]
    fn ospfv3_lsa_header_user_checksum_survives_encode() {
        let body = [0x00, 0x01, 0x02, 0x03];
        let header = Ospfv3LsaHeader::new()
            .ls_type(0x2002)
            .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
            .advertising_router(Ipv4Addr::new(192, 0, 2, 2))
            .ls_checksum(0xBEEF);

        let mut out = Vec::new();
        header.encode_with_body(&body, &mut out);

        // The pinned checksum is written verbatim, not recomputed.
        assert_eq!(&out[16..18], &0xBEEFu16.to_be_bytes());

        // The length still auto-fills to cover the header plus the body.
        assert_eq!(
            &out[18..20],
            &((OSPFV3_LSA_HEADER_LEN + body.len()) as u16).to_be_bytes()
        );
    }

    /// A buffer shorter than the 20-octet OSPFv3 LSA header yields a structured
    /// buffer-too-short error rather than a panic.
    #[test]
    fn ospfv3_lsa_header_decode_rejects_short_buffer() {
        let short = [0u8; OSPFV3_LSA_HEADER_LEN - 1];
        let err = Ospfv3LsaHeader::decode(&short).expect_err("a short buffer must error");
        match err {
            CrafterError::BufferTooShort {
                context,
                required,
                available,
            } => {
                assert_eq!(context, "ospfv3 lsa header");
                assert_eq!(required, OSPFV3_LSA_HEADER_LEN);
                assert_eq!(available, OSPFV3_LSA_HEADER_LEN - 1);
            }
            other => panic!("expected BufferTooShort, got {other:?}"),
        }
    }

    /// A list of three bare OSPFv3 LSA headers encodes to 60 octets and
    /// round-trips through `encode_ospfv3_lsa_headers` / `decode_ospfv3_lsa_headers`,
    /// and a 50-octet buffer (two full headers plus a 10-octet partial third)
    /// yields a structured buffer-too-short error rather than a panic.
    #[test]
    fn ospfv3_lsa_headers_list_round_trips_and_rejects_partial_trailer() {
        let headers = [
            Ospfv3LsaHeader::new()
                .ls_type(0x2001)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0001),
            Ospfv3LsaHeader::new()
                .ls_type(0x2002)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 2))
                .advertising_router(Ipv4Addr::new(198, 51, 100, 7))
                .ls_sequence_number(0x8000_0002),
            Ospfv3LsaHeader::new()
                .ls_type(0x2003)
                .link_state_id(Ipv4Addr::new(198, 51, 100, 0))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0003),
        ];

        let mut out = Vec::new();
        encode_ospfv3_lsa_headers(&headers, &mut out);

        // Three header-only records, 20 octets each.
        assert_eq!(out.len(), 3 * OSPFV3_LSA_HEADER_LEN);

        let decoded = decode_ospfv3_lsa_headers(&out).expect("OSPFv3 LSA header list decodes");
        assert_eq!(decoded.len(), 3);

        // Every field round-trips for each header.
        for (original, parsed) in headers.iter().zip(decoded.iter()) {
            assert_eq!(parsed.ls_age_value(), original.ls_age_value());
            assert_eq!(parsed.ls_type_value(), original.ls_type_value());
            assert_eq!(parsed.link_state_id_value(), original.link_state_id_value());
            assert_eq!(
                parsed.advertising_router_value(),
                original.advertising_router_value()
            );
            assert_eq!(
                parsed.ls_sequence_number_value(),
                original.ls_sequence_number_value()
            );
            // The declared length of a header-only record is 20 octets.
            assert_eq!(parsed.length_value(), Some(OSPFV3_LSA_HEADER_LEN as u16));
        }

        // A 50-octet buffer (two full headers plus a 10-octet partial third)
        // surfaces a structured error on the trailing remainder.
        let partial = &out[..2 * OSPFV3_LSA_HEADER_LEN + 10];
        assert_eq!(partial.len(), 50);
        let err =
            decode_ospfv3_lsa_headers(partial).expect_err("a partial trailing header must error");
        match err {
            CrafterError::BufferTooShort {
                context,
                required,
                available,
            } => {
                assert_eq!(context, "ospfv3 lsa header");
                assert_eq!(required, OSPFV3_LSA_HEADER_LEN);
                assert_eq!(available, 10);
            }
            other => panic!("expected BufferTooShort, got {other:?}"),
        }
    }

    /// An OSPFv3 Router-LSA body built with one 16-octet interface description
    /// encodes to the exact RFC 5340 §A.4.3 layout — the 4-octet fixed prefix
    /// (flags + 24-bit Options) then Type(1), Reserved(1), Metric(2), Interface
    /// ID(4), Neighbor Interface ID(4), Neighbor Router ID(4) — and, wrapped in an
    /// [`Ospfv3Lsa`], auto-fills the LSA length and a valid Fletcher checksum.
    #[test]
    fn ospfv3_router_lsa_one_interface_layout_and_checksum() {
        // 0x000013 exercises the 24-bit Options packing; the high octet of the
        // u32 (0xff) must be masked off on the wire.
        let router =
            Ospfv3RouterLsa::new()
                .options(0x00ff_0013)
                .interface(Ospfv3RouterInterface::new(
                    1, // Type: point-to-point
                    10,
                    0x0000_0005,
                    0x0000_0006,
                    Ipv4Addr::new(192, 0, 2, 2),
                ));

        assert_eq!(router.options_value(), 0x00ff_0013);
        assert_eq!(router.interfaces_value().len(), 1);

        let mut body = Vec::new();
        router.encode(&mut body);
        assert_eq!(body.len(), router.encoded_len());
        // 4-octet fixed prefix plus one 16-octet interface description.
        assert_eq!(body.len(), 20);

        // Hand-checked RFC 5340 §A.4.3 fixed prefix (flags + 24-bit Options).
        assert_eq!(body[0], 0x00); // flags
        assert_eq!(&body[1..4], &[0xff, 0x00, 0x13]); // Options (24-bit)

        // The 16-octet interface description.
        assert_eq!(body[4], 1); // Type
        assert_eq!(body[5], 0); // Reserved
        assert_eq!(&body[6..8], &10u16.to_be_bytes()); // Metric
        assert_eq!(&body[8..12], &0x0000_0005u32.to_be_bytes()); // Interface ID
        assert_eq!(&body[12..16], &0x0000_0006u32.to_be_bytes()); // Neighbor Interface ID
        assert_eq!(&body[16..20], &Ipv4Addr::new(192, 0, 2, 2).octets()); // Neighbor Router ID

        // Wrap the Router-LSA in an Ospfv3Lsa: the LSA length auto-fills to cover
        // the 20-octet header plus the body, and the Fletcher checksum validates.
        let lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2001)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0001),
            Ospfv3LsaBody::Router(router),
        );
        assert_eq!(lsa.encoded_len(), OSPFV3_LSA_HEADER_LEN + body.len());

        let mut lsa_bytes = Vec::new();
        lsa.encode(&mut lsa_bytes);
        assert_eq!(lsa_bytes.len(), OSPFV3_LSA_HEADER_LEN + body.len());
        // The LSA length field (octets 18..20) covers header + body.
        assert_eq!(
            &lsa_bytes[18..20],
            &((OSPFV3_LSA_HEADER_LEN + body.len()) as u16).to_be_bytes()
        );
        // The body follows the 20-octet header verbatim.
        assert_eq!(&lsa_bytes[OSPFV3_LSA_HEADER_LEN..], body.as_slice());
        assert!(
            fletcher16_valid(&lsa_bytes),
            "auto-filled Fletcher checksum should validate over the OSPFv3 Router-LSA"
        );
    }

    /// An OSPFv3 Network-LSA body built with two attached routers encodes to the
    /// exact RFC 5340 §A.4.4 layout — the 4-octet fixed prefix (Reserved +
    /// 24-bit Options) then each 4-octet attached Router ID — and, wrapped in an
    /// [`Ospfv3Lsa`], auto-fills the LSA length and a valid Fletcher checksum.
    #[test]
    fn ospfv3_network_lsa_two_attached_routers_layout_and_checksum() {
        let network = Ospfv3NetworkLsa::new()
            .options(0x0000_0013)
            .attached_router(Ipv4Addr::new(192, 0, 2, 1))
            .attached_router(Ipv4Addr::new(192, 0, 2, 2));

        assert_eq!(network.options_value(), 0x0000_0013);
        assert_eq!(network.attached_routers_value().len(), 2);

        let mut body = Vec::new();
        network.encode(&mut body);
        assert_eq!(body.len(), network.encoded_len());
        // 4-octet fixed prefix plus two 4-octet attached routers.
        assert_eq!(body.len(), 12);

        // Hand-checked RFC 5340 §A.4.4 fixed prefix (Reserved + 24-bit Options);
        // unlike OSPFv2 there is no network-mask field.
        assert_eq!(body[0], 0x00); // Reserved
        assert_eq!(&body[1..4], &[0x00, 0x00, 0x13]); // Options (24-bit)
        assert_eq!(&body[4..8], &Ipv4Addr::new(192, 0, 2, 1).octets());
        assert_eq!(&body[8..12], &Ipv4Addr::new(192, 0, 2, 2).octets());

        let lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2002)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0002),
            Ospfv3LsaBody::Network(network),
        );
        assert_eq!(lsa.encoded_len(), OSPFV3_LSA_HEADER_LEN + body.len());

        let mut lsa_bytes = Vec::new();
        lsa.encode(&mut lsa_bytes);
        assert_eq!(
            &lsa_bytes[18..20],
            &((OSPFV3_LSA_HEADER_LEN + body.len()) as u16).to_be_bytes()
        );
        assert_eq!(&lsa_bytes[OSPFV3_LSA_HEADER_LEN..], body.as_slice());
        assert!(
            fletcher16_valid(&lsa_bytes),
            "auto-filled Fletcher checksum should validate over the OSPFv3 Network-LSA"
        );
    }

    /// An OSPFv3 Link State Update body carrying a Router-LSA and a Network-LSA
    /// reports two LSAs, sizes each LSA's length and Fletcher checksum correctly,
    /// and matches the standalone-encoded LSA bytes after the 4-octet count.
    #[test]
    fn ospfv3_link_state_update_body_carries_router_and_network_lsas() {
        let router_lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2001)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0001),
            Ospfv3LsaBody::Router(Ospfv3RouterLsa::new().interface(Ospfv3RouterInterface::new(
                1,
                10,
                0x0000_0005,
                0x0000_0006,
                Ipv4Addr::new(192, 0, 2, 2),
            ))),
        );
        let network_lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2002)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0002),
            Ospfv3LsaBody::Network(
                Ospfv3NetworkLsa::new()
                    .attached_router(Ipv4Addr::new(192, 0, 2, 1))
                    .attached_router(Ipv4Addr::new(192, 0, 2, 2)),
            ),
        );

        // Standalone-encode each LSA for comparison.
        let mut router_bytes = Vec::new();
        router_lsa.encode(&mut router_bytes);
        let mut network_bytes = Vec::new();
        network_lsa.encode(&mut network_bytes);

        let lsu = Ospfv3LinkStateUpdate::new()
            .lsa(router_lsa)
            .lsa(network_lsa);
        assert_eq!(lsu.lsas_value().len(), 2);
        assert_eq!(lsu.num_lsas_value(), 2);

        let mut body = Vec::new();
        lsu.encode(&mut body);
        assert_eq!(body.len(), lsu.encoded_len());

        // # LSAs field (octets 0..4) reports two LSAs.
        assert_eq!(&body[0..4], &2u32.to_be_bytes());

        // The two LSAs follow the count, each matching its standalone encoding.
        assert_eq!(
            &body[OSPFV3_LSU_COUNT_LEN..OSPFV3_LSU_COUNT_LEN + router_bytes.len()],
            router_bytes.as_slice()
        );
        assert_eq!(
            &body[OSPFV3_LSU_COUNT_LEN + router_bytes.len()..],
            network_bytes.as_slice()
        );

        // Each carried LSA carries a valid Fletcher-16 checksum.
        assert!(fletcher16_valid(&router_bytes));
        assert!(fletcher16_valid(&network_bytes));
    }

    /// A caller-set `# LSAs` count survives `encode()` untouched, so a
    /// malformed-on-purpose OSPFv3 update keeps its wrong count.
    #[test]
    fn ospfv3_link_state_update_user_count_survives_encode() {
        let lsu = Ospfv3LinkStateUpdate::new()
            .lsa(Ospfv3Lsa::new(
                Ospfv3LsaHeader::new()
                    .ls_type(0x2001)
                    .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                    .advertising_router(Ipv4Addr::new(192, 0, 2, 1)),
                Ospfv3LsaBody::Raw(vec![0x00, 0x01]),
            ))
            .num_lsas(5);
        assert_eq!(lsu.num_lsas_value(), 5);

        let mut body = Vec::new();
        lsu.encode(&mut body);
        assert_eq!(&body[0..4], &5u32.to_be_bytes());
    }

    /// An `Ipv6 / Ospfv3::link_state_update()` packet carrying an OSPFv3
    /// Router-LSA and a Network-LSA compiles with auto-filled per-LSA lengths and
    /// Fletcher checksums, decodes back through the default registry over IPv6
    /// next-header 89, and round-trips byte-for-byte. Uses `2001:db8::/32`
    /// documentation addresses. The decode path preserves the LSU body verbatim
    /// in `Ospfv3Body::Unknown` (typed LSU decode lands in a later step), so the
    /// round-trip holds via byte preservation.
    #[test]
    fn ospfv3_link_state_update_over_ipv6_round_trips() {
        use crate::packet::{NetworkLayer, Packet};
        use crate::protocols::ip::v6::Ipv6;
        use crate::protocols::ospf::{Ospfv3, OSPFV3_HEADER_LEN, OSPFV3_TYPE_LINK_STATE_UPDATE};
        use core::net::Ipv6Addr;

        let router_lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2001)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0001),
            Ospfv3LsaBody::Router(Ospfv3RouterLsa::new().interface(Ospfv3RouterInterface::new(
                1,
                10,
                0x0000_0005,
                0x0000_0006,
                Ipv4Addr::new(192, 0, 2, 2),
            ))),
        );
        let network_lsa = Ospfv3Lsa::new(
            Ospfv3LsaHeader::new()
                .ls_type(0x2002)
                .link_state_id(Ipv4Addr::new(192, 0, 2, 1))
                .advertising_router(Ipv4Addr::new(192, 0, 2, 1))
                .ls_sequence_number(0x8000_0002),
            Ospfv3LsaBody::Network(
                Ospfv3NetworkLsa::new()
                    .attached_router(Ipv4Addr::new(192, 0, 2, 1))
                    .attached_router(Ipv4Addr::new(192, 0, 2, 2)),
            ),
        );

        let lsu = Ospfv3LinkStateUpdate::new()
            .lsa(router_lsa)
            .lsa(network_lsa);
        let mut body = Vec::new();
        lsu.encode(&mut body);

        let src: Ipv6Addr = "2001:db8::1".parse().unwrap();
        let dst: Ipv6Addr = "2001:db8::2".parse().unwrap();

        let ospfv3 = Ospfv3::link_state_update()
            .router_id([192, 0, 2, 1])
            .area_id([0, 0, 0, 0])
            .with_link_state_update(|u| *u = lsu);

        let bytes = (Ipv6::new().src(src).dst(dst) / ospfv3)
            .compile()
            .expect("Ipv6 / Ospfv3 LSU compiles");

        // The OSPFv3 LSU body bytes follow the 16-octet common header verbatim.
        let body_start = bytes.as_bytes().len() - body.len();
        assert_eq!(&bytes.as_bytes()[body_start..], body.as_slice());

        let decoded = Packet::decode_from_l3(NetworkLayer::Ipv6, bytes.as_bytes())
            .expect("the default registry decodes the OSPFv3 LSU over IPv6");
        let layer = decoded
            .layer::<Ospfv3>()
            .expect("the decoded packet exposes a typed Ospfv3 layer");
        assert_eq!(layer.packet_type_value(), OSPFV3_TYPE_LINK_STATE_UPDATE);

        // The decode path preserves the body verbatim after the 16-octet header.
        let ospfv3_start = bytes.as_bytes().len() - (OSPFV3_HEADER_LEN + body.len());
        assert_eq!(
            &bytes.as_bytes()[ospfv3_start + OSPFV3_HEADER_LEN..],
            body.as_slice()
        );

        // The decoded packet re-compiles byte-for-byte.
        let recompiled = decoded
            .compile()
            .expect("the decoded OSPFv3 LSU re-compiles");
        assert_eq!(recompiled.as_bytes(), bytes.as_bytes());
    }
}