bgpkit-parser 0.25.0-beta.1

MRT/BGP/BMP data processing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
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
#![forbid(unsafe_code)]
/*!
Borrowed element views over the records this path serves: `TABLE_DUMP_V2` `RIB_AFI` entries and
`BGP4MP_AS4` updates.

[`BgpkitParser::next_record_elems`](crate::BgpkitParser::next_record_elems) reads one record body
into a reusable buffer owned by the parser and lends a [`RecordElems`] batch over its RIB entries.
Each [`ElemRef`] yielded by [`RecordElems::iter`] borrows directly from the record body and the
active peer index table. Its accessors decode on demand. An update's next hop is decoded once for
the whole record; a dump entry's `MP_REACH_NLRI` next hop is decoded per call and allocates, and
materialising an element allocates its AS path and communities.
*/

mod element;
mod update;
mod view;

pub use element::ElemRef;
pub use view::*;

use update::{fill_update_entries, parse_peer_index_vec, update_record_elems};
use view::{mp_reach_next_hop, scan_attr_block, AttrBlockScan};

use crate::error::ParserError;
use crate::models::{
    get_deprecated_attr_type, Afi, AsPath, AsPathSegment, Asn, AsnLength, AttrFlags, AttrRaw,
    AttrType, Bgp4MpType, BgpIdentifier, CommonHeader, ElemType, ExtendedCommunity, LargeCommunity,
    MetaCommunity, NetworkPrefix, Origin, Peer, Safi,
};
use crate::parser::iters::handle_record_parse_error;
use crate::parser::mrt::frame::{assemble_error_bytes, FrameError, Framer};
use crate::parser::mrt::mrt_header::HeaderBuf;
use crate::parser::{BgpkitParser, ElemError, ReadUtils};
use crate::BgpElem;
use bytes::Bytes;
use ipnet::IpNet;
use log::{error, warn};
use std::cell::Cell;
use std::io::Read;
use std::net::IpAddr;
use zerocopy::big_endian::U32 as BeU32;
use zerocopy::FromBytes;

const AS_PATH_AS_SET: u8 = 1;
const AS_PATH_AS_SEQUENCE: u8 = 2;
const AS_PATH_CONFED_SEQUENCE: u8 = 3;
const AS_PATH_CONFED_SET: u8 = 4;

use crate::parser::mrt::frame::{FLAG_ADD_PATH, FLAG_AFI_IPV6, FLAG_SAFI_MULTICAST};
/// Set for an element of a `BGP4MP` update. A dump's entry decodes `NEXT_HOP` with the record's
/// family; an update's `NEXT_HOP` is always the IPv4 address RFC 4271 defines.
const FLAG_UPDATE: u8 = 1 << 3;

// One `u8` carries the frame bits and the update bit, so they must not overlap.
const _: () = assert!(FLAG_UPDATE & (FLAG_AFI_IPV6 | FLAG_SAFI_MULTICAST | FLAG_ADD_PATH) == 0);

const SLOT_UNSCANNED: u64 = u64::MAX;
const SLOT_ABSENT: u64 = u64::MAX - 1;
const SLOT_MERGED: u64 = u64::MAX - 2;

/// Reusable parser-owned state backing [`BgpkitParser::next_record_elems`].
///
/// All buffers start empty (`0` heap capacity) so parsers that never call `next_record_elems`
/// pay no allocation overhead.
#[derive(Default)]
pub(crate) struct ElemViewState {
    /// Reusable buffer holding the wire body of the active MRT record.
    ///
    /// The buffer keeps its allocation across records; the active record body is
    /// `&body_buf[..active_len]`.
    pub(crate) body_buf: Vec<u8>,
    /// Shared framing: common header plus the body read into `body_buf`.
    pub(crate) framer: Framer,
    /// Active `TABLE_DUMP_V2` `PeerIndexTable` indexed directly by wire `peer_index`.
    peers: Option<Vec<Peer>>,
    /// NLRI of the `BGP4MP` update the current [`RecordElems`] batch covers.
    pub(crate) update_entries: Vec<UpdateEntry>,
    /// Byte range of that update's attribute block inside `body_buf`, filled by the walk that also
    /// produced `update_entries` so no record is decoded twice.
    pub(crate) update_attrs: (usize, usize),
    /// The next hop that update's elements share, decoded by the same walk.
    pub(crate) update_next_hop: Option<IpAddr>,
    /// Elements belonging to records this view does not serve, counted by the owned path so a
    /// caller that only counts gets the same total as `into_elem_iter`.
    pub(crate) unserved_elems: usize,
    /// Set once EOF or a non-recoverable framing/core-dump error ends iteration.
    finished: bool,
}

/// Count the elements this parser reads, without materialising them.
///
/// A served record is counted entry by entry over the borrowed batch, which decodes no attribute.
/// A record the view does not serve is parsed by the owned path and counted from its elements, so
/// the total matches `into_elem_iter().count()`.
pub(crate) fn count_elems<R: Read>(parser: &mut BgpkitParser<R>) -> Option<usize> {
    // A text dump carries elements this path never frames.
    if parser.text_dump_iter.is_some() {
        return None;
    }
    let mut total = 0usize;
    while let Some(batch) = next_record_elems_impl(parser) {
        total += batch.iter().count();
    }
    Some(total + parser.elem_view_state.unserved_elems)
}

/// Whether a `GEO_PEER_TABLE` body decodes, decided by the owned parser so the RFC 6397 layout has
/// one owner.
fn validate_geo_peer_table(data: &[u8]) -> Result<(), ParserError> {
    crate::parser::mrt::messages::table_dump_v2::geo_peer_table::parse_geo_peer_table(
        &mut bytes::Bytes::copy_from_slice(data),
    )
    .map(|_| ())
}

/// Frame the next record, returning owned framing metadata and leaving the body in
/// `elem_view_state.body_buf`, so no borrow outlives the call.
pub(crate) fn frame_next<R: Read>(
    parser: &mut BgpkitParser<R>,
) -> Result<(CommonHeader, HeaderBuf, usize), FrameError> {
    let state = &mut parser.elem_view_state;
    let frame = state.framer.next(&mut parser.reader, &mut state.body_buf)?;
    Ok((frame.header, frame.raw_header, frame.body.len()))
}

/// Frame the next record, consuming the peer index and geo peer tables into the state.
///
/// Every path that reads frames starts this way: frame a record, report a framing failure, and let
/// the two table record kinds fill the state without reaching the caller. `None` means end of input.
pub(crate) fn frame_body_record<R: Read>(
    parser: &mut BgpkitParser<R>,
) -> Option<(CommonHeader, HeaderBuf, usize)> {
    use crate::parser::mrt::frame::{classify, RecordKind};

    loop {
        let (header, raw_header, body_len) = match frame_next(parser) {
            Ok(framed) => framed,
            Err(failure) => {
                if matches!(failure.error, ParserError::EofExpected) {
                    parser.elem_view_state.finished = true;
                    return None;
                }
                if !handle_record_parse_error(parser, failure.error, failure.bytes) {
                    parser.elem_view_state.finished = true;
                    return None;
                }
                continue;
            }
        };
        let body = &parser.elem_view_state.body_buf[..body_len];
        match classify(&header, body) {
            RecordKind::PeerIndex => match parse_peer_index_vec(body) {
                Ok(peers) => parser.elem_view_state.peers = Some(peers),
                Err(err) => {
                    let raw = parser
                        .core_dump
                        .then(|| assemble_error_bytes(raw_header.as_slice(), body));
                    if !handle_record_parse_error(parser, err, raw) {
                        parser.elem_view_state.finished = true;
                        return None;
                    }
                }
            },
            RecordKind::GeoPeer => {
                let body = &parser.elem_view_state.body_buf[..body_len];
                if let Err(err) = validate_geo_peer_table(body) {
                    let raw = parser
                        .core_dump
                        .then(|| assemble_error_bytes(raw_header.as_slice(), body));
                    if !handle_record_parse_error(parser, err, raw) {
                        parser.elem_view_state.finished = true;
                        return None;
                    }
                }
            }
            _ => return Some((header, raw_header, body_len)),
        }
    }
}

/// Whether a filter set proves this record yields no matching element.
///
/// Both record paths ask the same question of the same bytes: the header names the prefix for a dump
/// record and the peer and time for an update, and `attr_scan` decides an origin predicate. A record
/// this cannot decide is not excluded.
pub(crate) fn record_excluded_by_filters<R: Read>(
    parser: &BgpkitParser<R>,
    header: &CommonHeader,
    body_len: usize,
) -> bool {
    use crate::parser::mrt::frame::{classify, RecordKind};

    let body = &parser.elem_view_state.body_buf[..body_len];
    match classify(header, body) {
        RecordKind::Rib {
            prefix,
            flags,
            entries_offset,
            ..
        } => {
            crate::parser::filter::rib_record_excluded(&parser.filters, &prefix)
                || crate::parser::attr_scan::rib_record_excluded(
                    &parser.filters,
                    body,
                    entries_offset,
                    flags,
                )
        }
        RecordKind::Bgp4Mp {
            peer_ip,
            peer_asn,
            timestamp,
        } => {
            crate::parser::filter::peer_excludes_record(&parser.filters, &peer_ip, &peer_asn)
                || crate::parser::filter::timestamp_excludes_record(&parser.filters, timestamp)
                || crate::parser::attr_scan::bgp4mp_record_excluded(
                    &parser.filters,
                    body,
                    header.entry_subtype,
                )
        }
        RecordKind::PeerIndex | RecordKind::GeoPeer | RecordKind::Other => false,
    }
}

/// What the borrowed element engine produced for the next record.
pub(crate) enum NextBatch<'a> {
    /// A RIB record's elements, borrowed from the parser's record buffer.
    Rib(RecordElems<'a>),
    /// A record this engine does not serve. Its body is still in the parser's buffer, untouched.
    Other {
        header: CommonHeader,
        raw_header: crate::parser::mrt::mrt_header::HeaderBuf,
        body_len: usize,
    },
    /// End of input.
    End,
}

/// Advance to the next record and report whether the borrowed element engine can serve it.
///
/// Unlike [`next_record_elems_impl`], this stops at the first record the engine cannot serve
/// instead of skipping it, so a caller can hand that record to the owned path.
pub(crate) fn next_batch<R: Read>(parser: &mut BgpkitParser<R>) -> NextBatch<'_> {
    use crate::parser::mrt::frame::{classify, RecordKind};

    if parser.text_dump_iter.is_some() || parser.elem_view_state.finished {
        return NextBatch::End;
    }

    loop {
        let (header, raw_header, body_len) = match frame_body_record(parser) {
            Some(framed) => framed,
            None => return NextBatch::End,
        };
        // The same predicates the owned path applies, so a filtered run keeps the skips it had
        // before it moved onto the framed path.
        if record_excluded_by_filters(parser, &header, body_len) {
            continue;
        }
        match classify(&header, &parser.elem_view_state.body_buf[..body_len]) {
            RecordKind::Rib {
                prefix,
                entry_count,
                flags,
                entries_offset,
            } => {
                if parser.elem_view_state.peers.is_none() {
                    error!("{}", ElemError::MissingPeerTable);
                    continue;
                }
                let state = &mut parser.elem_view_state;
                let peers = state.peers.as_deref().expect("checked peers is_some");
                let entries_bytes = &state.body_buf[entries_offset..body_len];
                return NextBatch::Rib(RecordElems::rib(
                    header,
                    entries_bytes,
                    peers,
                    prefix.prefix,
                    entry_count,
                    flags,
                ));
            }
            // Hand the record back untouched: the caller owns the fallback and its error handling.
            RecordKind::Bgp4Mp { .. } | RecordKind::Other => {
                return NextBatch::Other {
                    header,
                    raw_header,
                    body_len,
                }
            }
            // `frame_body_record` consumes both table kinds before returning.
            RecordKind::PeerIndex | RecordKind::GeoPeer => continue,
        }
    }
}

pub(crate) fn next_record_elems_impl<R: Read>(
    parser: &mut BgpkitParser<R>,
) -> Option<RecordElems<'_>> {
    use crate::parser::mrt::frame::{bgp4mp_is_update, classify, RecordKind};

    if parser.text_dump_iter.is_some() || parser.elem_view_state.finished {
        return None;
    }
    if !parser.filters.is_empty() {
        parser.options.warn_view_filters_once();
        return None;
    }

    enum Framed {
        Rib {
            header: CommonHeader,
            prefix: IpNet,
            entry_count: u16,
            flags: u8,
            entries_offset: usize,
            body_len: usize,
        },
        Update {
            header: CommonHeader,
            peer_ip: IpAddr,
            peer_asn: Asn,
            timestamp: f64,
            body_len: usize,
        },
    }

    let framed = loop {
        let (common_header, raw_header, body_len) = frame_body_record(parser)?;
        let kind = classify(&common_header, &parser.elem_view_state.body_buf[..body_len]);
        match kind {
            RecordKind::Rib {
                prefix,
                entry_count,
                flags,
                entries_offset,
            } => {
                if parser.elem_view_state.peers.is_none() {
                    error!("{}", ElemError::MissingPeerTable);
                    continue;
                }
                break Framed::Rib {
                    header: common_header,
                    prefix: prefix.prefix,
                    entry_count,
                    flags,
                    entries_offset,
                    body_len,
                };
            }
            RecordKind::Bgp4Mp {
                peer_ip,
                peer_asn,
                timestamp,
            } => {
                // Support is decided from the header. The walk then runs here, once, and its result
                // is kept in the state so the batch can be built without decoding again.
                let subtype = common_header.entry_subtype;
                let as4 = matches!(
                    Bgp4MpType::try_from(subtype),
                    Ok(Bgp4MpType::MessageAs4)
                        | Ok(Bgp4MpType::MessageAs4Local)
                        | Ok(Bgp4MpType::MessageAs4Addpath)
                        | Ok(Bgp4MpType::MessageLocalAs4Addpath)
                );
                let would_yield =
                    bgp4mp_is_update(subtype, &parser.elem_view_state.body_buf[..body_len]);
                let walked = if as4 && would_yield {
                    let state = &mut parser.elem_view_state;
                    let body = &state.body_buf[..body_len];
                    let outcome = fill_update_entries(body, subtype, &mut state.update_entries);
                    outcome.map(|(attrs, next_hop)| {
                        let offset = attrs.as_ptr() as usize - state.body_buf.as_ptr() as usize;
                        state.update_attrs = (offset, attrs.len());
                        state.update_next_hop = next_hop;
                    })
                } else {
                    None
                };
                if walked.is_none() {
                    // A record the view cannot read is either a parse error or a record kind it
                    // does not serve. The owned parser answers which: its error goes through the
                    // same handler, and only a record it parses cleanly is a scope limit.
                    match parser.parse_framed_record(common_header, raw_header.as_slice(), body_len)
                    {
                        Ok(record) if as4 && would_yield => {
                            parser.elem_view_state.unserved_elems +=
                                crate::Elementor::default().record_to_elems(record).len();
                            // The owned path parses it, so the failure is in this walk; say so
                            // rather than drop the record in silence.
                            error!(
                                "next_record_elems could not read a BGP4MP update the parser accepts; use into_elem_iter for this file"
                            );
                        }
                        Ok(record) if !as4 && would_yield => {
                            parser
                                .options
                                .warn_view_scope_once("BGP4MP record with 2-byte AS numbers");
                            parser.elem_view_state.unserved_elems +=
                                crate::Elementor::default().record_to_elems(record).len();
                        }
                        Ok(_) => {}
                        Err(failure) => {
                            if !handle_record_parse_error(parser, failure.error, failure.bytes) {
                                parser.elem_view_state.finished = true;
                                return None;
                            }
                        }
                    }
                    continue;
                }
                break Framed::Update {
                    header: common_header,
                    peer_ip,
                    peer_asn,
                    timestamp,
                    body_len,
                };
            }
            // `frame_body_record` consumes both table kinds before returning.
            RecordKind::PeerIndex | RecordKind::GeoPeer => continue,
            RecordKind::Other => {
                match parser.parse_framed_record(common_header, raw_header.as_slice(), body_len) {
                    Ok(record) => {
                        parser.options.warn_view_scope_once("other");
                        parser.elem_view_state.unserved_elems +=
                            crate::Elementor::default().record_to_elems(record).len();
                    }
                    Err(failure) => {
                        if !handle_record_parse_error(parser, failure.error, failure.bytes) {
                            parser.elem_view_state.finished = true;
                            return None;
                        }
                    }
                }
                continue;
            }
        }
    };

    match framed {
        Framed::Rib {
            header,
            prefix,
            entry_count,
            flags,
            entries_offset,
            body_len,
        } => {
            let state = &mut parser.elem_view_state;
            let peers = state
                .peers
                .as_deref()
                .expect("checked peers is_some before breaking");
            let entries_bytes = &state.body_buf[entries_offset..body_len];
            Some(RecordElems::rib(
                header,
                entries_bytes,
                peers,
                prefix,
                entry_count,
                flags,
            ))
        }
        Framed::Update {
            header,
            peer_ip,
            peer_asn,
            timestamp,
            body_len,
        } => {
            let record = update_record_elems(
                &mut parser.elem_view_state,
                body_len,
                header,
                peer_ip,
                peer_asn,
                timestamp,
            );
            if record.is_none() {
                // The header said this record yields elements but the walk could not read it, so it
                // is reported once instead of dropped in silence.
                error!(
                    "next_record_elems could not read a BGP4MP update the parser accepts; use into_elem_iter for this file"
                );
            }
            record
        }
    }
}

/// A borrowed batch of elements from one MRT record.
///
/// Holds a mutable borrow of the [`BgpkitParser`] (its reusable record buffer, and the active peer
/// table for a dump); drop it before advancing the parser to the next record.
pub struct RecordElems<'a> {
    header: CommonHeader,
    source: RecordSource<'a>,
}

/// Where a [`RecordElems`] batch takes its elements from.
enum RecordSource<'a> {
    /// `TABLE_DUMP_V2` `RIB_AFI`: one prefix, and entries that name a peer and carry attributes.
    Rib {
        entries_bytes: &'a [u8],
        peers: &'a [Peer],
        prefix: IpNet,
        entry_count: u16,
        flags: u8,
    },
    /// `BGP4MP` update: one attribute block, the NLRI this record announced or withdrew, and the
    /// next hop every element of it shares.
    Update {
        entries: &'a [UpdateEntry],
        attrs: &'a [u8],
        next_hop: Option<IpAddr>,
        peer_ip: IpAddr,
        peer_asn: Asn,
        timestamp: f64,
    },
}

impl<'a> RecordElems<'a> {
    #[inline]
    pub(crate) fn rib(
        header: CommonHeader,
        entries_bytes: &'a [u8],
        peers: &'a [Peer],
        prefix: IpNet,
        entry_count: u16,
        flags: u8,
    ) -> Self {
        RecordElems {
            header,
            source: RecordSource::Rib {
                entries_bytes,
                peers,
                prefix,
                entry_count,
                flags,
            },
        }
    }

    #[inline]
    pub(crate) fn update(
        header: CommonHeader,
        entries: &'a [UpdateEntry],
        attrs: &'a [u8],
        peer_ip: IpAddr,
        peer_asn: Asn,
        timestamp: f64,
        next_hop: Option<IpAddr>,
    ) -> Self {
        RecordElems {
            header,
            source: RecordSource::Update {
                entries,
                attrs,
                peer_ip,
                peer_asn,
                timestamp,
                next_hop,
            },
        }
    }

    /// Returns the MRT [`CommonHeader`] of this record.
    #[inline]
    pub fn header(&self) -> &CommonHeader {
        &self.header
    }

    /// The number of entries the record declares: the RIB entry count, or the number of NLRI this
    /// update announced and withdrew.
    ///
    /// This is what the header says, not a promise about how many elements the iterator yields: an
    /// entry naming a peer outside the table, or one the record cannot decode, produces fewer.
    /// Never size a buffer from it without a ceiling, since the count comes from the input.
    #[inline]
    pub fn len(&self) -> usize {
        match &self.source {
            RecordSource::Rib { entry_count, .. } => *entry_count as usize,
            RecordSource::Update { entries, .. } => entries.len(),
        }
    }

    /// Returns `true` when the record declares no elements.
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns an iterator yielding borrowed [`ElemRef`] views over the record's elements.
    #[inline]
    pub fn iter(&self) -> ElemRefIter<'a> {
        let source = match &self.source {
            RecordSource::Rib {
                entries_bytes,
                peers,
                prefix,
                entry_count,
                flags,
            } => IterSource::Rib {
                data: entries_bytes,
                peers,
                prefix: *prefix,
                remaining: *entry_count,
                flags: *flags,
            },
            RecordSource::Update {
                entries,
                attrs,
                peer_ip,
                peer_asn,
                timestamp,
                next_hop,
            } => IterSource::Update {
                entries,
                attrs,
                peer_ip: *peer_ip,
                peer_asn: *peer_asn,
                timestamp: *timestamp,
                next: 0,
                next_hop: *next_hop,
            },
        };
        ElemRefIter { source }
    }
}

impl<'a> IntoIterator for &RecordElems<'a> {
    type Item = ElemRef<'a>;
    type IntoIter = ElemRefIter<'a>;

    #[inline]
    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// One element of a `BGP4MP` update, as the record declared it.
#[derive(Clone, Copy, Debug)]
pub(crate) struct UpdateEntry {
    prefix: NetworkPrefix,
    elem_type: ElemType,
    flags: u8,
}

/// Iterator over borrowed [`ElemRef`]s within a single [`RecordElems`] record.
pub struct ElemRefIter<'a> {
    source: IterSource<'a>,
}

enum IterSource<'a> {
    Rib {
        data: &'a [u8],
        peers: &'a [Peer],
        prefix: IpNet,
        remaining: u16,
        flags: u8,
    },
    Update {
        entries: &'a [UpdateEntry],
        attrs: &'a [u8],
        peer_ip: IpAddr,
        peer_asn: Asn,
        timestamp: f64,
        next: usize,
        next_hop: Option<IpAddr>,
    },
}

impl<'a> Iterator for ElemRefIter<'a> {
    type Item = ElemRef<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.source {
            IterSource::Rib {
                data,
                peers,
                prefix,
                remaining,
                flags,
            } => {
                if *remaining == 0 {
                    return None;
                }
                let with_path_id = (*flags & FLAG_ADD_PATH) != 0;
                let header_len = if with_path_id { 12 } else { 8 };
                if data.len() < header_len {
                    warn!("early break due to error Truncated message: truncated msg while parsing RIB AFI entries");
                    *remaining = 0;
                    return None;
                }
                let peer_index = u16::from_be_bytes([data[0], data[1]]);
                let originated_time = u32::from_be_bytes([data[2], data[3], data[4], data[5]]);
                // With add-path, bytes 6..10 carry this entry's path identifier: two entries of one
                // prefix differ only by it, so the element keeps it.
                // With add-path, bytes 6..10 hold the path id; the element keeps the record's prefix.
                let attr_len =
                    u16::from_be_bytes([data[header_len - 2], data[header_len - 1]]) as usize;
                let end = header_len + attr_len;
                if data.len() < end {
                    warn!(
                        "early break due to error Truncated message: not enough bytes to read. remaining: {}, required: {} while parsing RIB AFI entries",
                        data.len() - header_len,
                        attr_len
                    );
                    *remaining = 0;
                    return None;
                }
                let attrs = &data[header_len..end];
                *data = &data[end..];
                *remaining -= 1;
                let Some(peer) = peers.get(peer_index as usize) else {
                    error!("peer ID {} not found in peer_index table", peer_index);
                    *remaining = 0;
                    return None;
                };
                // One walk decides both questions: whether the record survives (an unreadable header
                // fails it, as it does on the owned path) and where its attributes end (a value that
                // runs past the block stops the decode there, which is what the owned walker does
                // with the attributes it already read).
                let (usable_len, truncated) = match scan_attr_block(attrs) {
                    AttrBlockScan::Usable {
                        usable_len,
                        truncated,
                    } => (
                        usable_len,
                        truncated.map(|(code, declared, available)| {
                            (code, declared as u16, available as u32)
                        }),
                    ),
                    AttrBlockScan::Unreadable => {
                        *remaining = 0;
                        return None;
                    }
                };
                Some(ElemRef::new(
                    *prefix,
                    peer,
                    originated_time,
                    *flags,
                    &attrs[..usable_len],
                    truncated,
                ))
            }
            IterSource::Update {
                entries,
                attrs,
                peer_ip,
                peer_asn,
                timestamp,
                next,
                next_hop,
            } => {
                let entry = entries.get(*next)?;
                *next += 1;
                // A withdrawal carries no attributes, exactly as the owned path leaves them unset.
                let attr_bytes: &[u8] = match entry.elem_type {
                    ElemType::ANNOUNCE => attrs,
                    ElemType::WITHDRAW => &[],
                };
                Some(ElemRef::new_update(
                    entry, *peer_ip, *peer_asn, *timestamp, attr_bytes, *next_hop,
                ))
            }
        }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = match &self.source {
            IterSource::Rib { remaining, .. } => *remaining as usize,
            IterSource::Update { entries, next, .. } => entries.len().saturating_sub(*next),
        };
        (0, Some(remaining))
    }
}

/// Whether a 4-byte-ASN `AS_PATH` / `AS4_PATH` attribute payload is well formed.
///
/// The owned segment parser decides this: a segment type it rejects, or a count that runs past the
/// payload, fails there exactly as it must here.
fn validate_as_path_wire(wire: &[u8]) -> bool {
    crate::parser::bgp::attributes::validate_as_path_slice(wire, &AsnLength::Bits32)
}

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

    #[test]
    fn test_bytes_as_be_u32_words_alignment_and_length_check() {
        // Unaligned subslice (offset 1) with valid multiple-of-4 length decodes big-endian u32s.
        let backing = [0xAA, 0x00, 0x00, 0xFD, 0xE8, 0xFF, 0xFF, 0xFF, 0x01];
        let unaligned = &backing[1..9];
        let words = bytes_as_be_u32_words(unaligned);
        assert_eq!(words.len(), 2);
        assert_eq!(words[0].get(), 65_000);
        // NO_EXPORT, written out because the wire value belongs to the attribute module.
        assert_eq!(words[1].get(), 0xFFFF_FF01);

        // Non-multiple-of-4 length is rejected cleanly as empty rather than panicking.
        assert!(bytes_as_be_u32_words(&backing[1..8]).is_empty());
        assert!(bytes_as_be_u32_words(&[]).is_empty());
    }
}

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

    /// A real TABLE_DUMP_V2 slice: one PEER_INDEX_TABLE followed by 399 RIB records.
    const RIB_FIXTURE: &str = "tests/fixtures/routeviews/bview.slice.gz";

    /// The borrowed view serves updates as well as dumps, and must produce the same elements the
    /// owned record path produces for the same file.
    #[test]
    fn borrowed_update_elems_match_owned_elems() {
        // A 15-minute RIPE update file: BGP4MP messages with 4-byte AS numbers.
        let fixture = "tests/fixtures/ripe/rrc15/2010.02/updates.20100227.1600.gz";
        // The borrowed view serves the AS4 message subtypes; the fixture also carries 2-byte ASN
        // records from 2010, which stay on the owned path and are excluded from the comparison.
        let (owned, legacy_records): (Vec<_>, usize) = {
            let mut elementor = crate::parser::mrt::mrt_elem::Elementor::new();
            let mut owned = Vec::new();
            let mut legacy = 0;
            for record in BgpkitParser::new(fixture)
                .expect("open fixture")
                .into_record_iter()
            {
                if !matches!(record.common_header.entry_subtype, 4 | 5 | 9 | 10) {
                    legacy += 1;
                    continue;
                }
                owned.extend(elementor.record_to_elems(record));
            }
            (owned, legacy)
        };
        assert!(!owned.is_empty(), "fixture produced no elems");
        assert!(
            legacy_records > 0,
            "fixture should exercise the 2-byte skip"
        );

        let mut parser = BgpkitParser::new(fixture).expect("open fixture");
        let mut borrowed = Vec::new();
        while let Some(record) = parser.next_record_elems() {
            assert!(!record.is_empty(), "an update record declared no elements");
            for elem in record.iter() {
                borrowed.push(elem.to_bgp_elem());
            }
        }
        assert_eq!(owned.len(), borrowed.len(), "element count differs");
        for (index, (owned_elem, borrowed_elem)) in owned.iter().zip(&borrowed).enumerate() {
            assert_eq!(owned_elem, borrowed_elem, "element {index} differs");
        }
    }

    /// Every field of a materialised element must equal what the owned record path produces for
    /// the same entry. The reference is the record iterator plus `Elementor`, not `into_elem_iter`,
    /// which now goes through this same borrowed engine.
    #[test]
    fn borrowed_elems_match_owned_elems() {
        let owned: Vec<_> = {
            let mut elementor = crate::parser::mrt::mrt_elem::Elementor::new();
            BgpkitParser::new(RIB_FIXTURE)
                .expect("open fixture")
                .into_record_iter()
                .flat_map(|record| elementor.record_to_elems(record))
                .collect()
        };
        assert!(!owned.is_empty(), "fixture produced no elems");

        let mut parser = BgpkitParser::new(RIB_FIXTURE).expect("open fixture");
        let mut borrowed = Vec::new();
        while let Some(record) = parser.next_record_elems() {
            for elem in record.iter() {
                borrowed.push(elem.to_bgp_elem());
            }
        }

        assert_eq!(owned.len(), borrowed.len(), "element count differs");
        for (index, (owned_elem, borrowed_elem)) in owned.iter().zip(borrowed.iter()).enumerate() {
            assert_eq!(owned_elem, borrowed_elem, "elem {index} differs");
        }
    }
}

#[cfg(test)]
mod wire_parity_tests {
    use crate::models::*;
    use std::io::Cursor;
    use std::str::FromStr;

    /// Wrap a BGP4MP body in an MRT common header.
    fn mrt(subtype: Bgp4MpType, body: Vec<u8>) -> Vec<u8> {
        let mut wire = Vec::new();
        wire.extend_from_slice(&1_600_000_000u32.to_be_bytes());
        wire.extend_from_slice(&(EntryType::BGP4MP as u16).to_be_bytes());
        wire.extend_from_slice(&(subtype as u16).to_be_bytes());
        wire.extend_from_slice(&(body.len() as u32).to_be_bytes());
        wire.extend_from_slice(&body);
        wire
    }

    /// A BGP4MP message body: envelope, then a BGP message of the given type.
    fn bgp4mp_body(asn_len: AsnLength, afi: Afi, msg_type: u8, payload: &[u8]) -> Vec<u8> {
        let mut body = Vec::new();
        match asn_len {
            AsnLength::Bits16 => {
                body.extend_from_slice(&64496u16.to_be_bytes());
                body.extend_from_slice(&64497u16.to_be_bytes());
            }
            AsnLength::Bits32 => {
                body.extend_from_slice(&64496u32.to_be_bytes());
                body.extend_from_slice(&64497u32.to_be_bytes());
            }
        }
        body.extend_from_slice(&0u16.to_be_bytes());
        body.extend_from_slice(&u16::from(afi).to_be_bytes());
        match afi {
            Afi::Ipv4 => {
                body.extend_from_slice(&[192, 0, 2, 1]);
                body.extend_from_slice(&[192, 0, 2, 2]);
            }
            Afi::Ipv6 => {
                body.extend_from_slice(&[
                    0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
                ]);
                body.extend_from_slice(&[
                    0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
                ]);
            }
            other => panic!("test does not build BGP4MP records for {other:?}"),
        }
        let mut message = vec![0xFF; 16];
        message.extend_from_slice(&((19 + payload.len()) as u16).to_be_bytes());
        message.push(msg_type);
        message.extend_from_slice(payload);
        body.extend_from_slice(&message);
        body
    }

    /// An UPDATE payload: withdrawn NLRI, attribute block, announced NLRI.
    fn update_payload(withdrawn: &[u8], attributes: &[u8], announced: &[u8]) -> Vec<u8> {
        let mut payload = Vec::new();
        payload.extend_from_slice(&(withdrawn.len() as u16).to_be_bytes());
        payload.extend_from_slice(withdrawn);
        payload.extend_from_slice(&(attributes.len() as u16).to_be_bytes());
        payload.extend_from_slice(attributes);
        payload.extend_from_slice(announced);
        payload
    }

    /// One path attribute with a one-octet length. Optional attributes need the optional bit, or
    /// the parser treats the flag byte as a validation finding and drops the attribute.
    fn attr(code: u8, body: &[u8]) -> Vec<u8> {
        let optional = matches!(code, 14 | 15);
        let flags = if optional { 0x80 } else { 0x40 };
        let mut out = vec![flags, code, body.len() as u8];
        out.extend_from_slice(body);
        out
    }

    fn origin_igp() -> Vec<u8> {
        attr(1, &[0])
    }

    fn next_hop() -> Vec<u8> {
        attr(3, &[192, 0, 2, 254])
    }

    /// AS_SEQUENCE of length zero followed by a real one: the owned renderer writes a separator for
    /// the empty segment, so a view that skipped it would compare positions differently.
    fn as_path_with_empty_segment() -> Vec<u8> {
        let mut body = vec![2u8, 0];
        body.extend_from_slice(&[2u8, 2]);
        body.extend_from_slice(&3356u32.to_be_bytes());
        body.extend_from_slice(&174u32.to_be_bytes());
        attr(2, &body)
    }

    fn as_path(asns: &[u32], width: AsnLength) -> Vec<u8> {
        let mut body = vec![2u8, asns.len() as u8];
        for asn in asns {
            match width {
                AsnLength::Bits16 => body.extend_from_slice(&(*asn as u16).to_be_bytes()),
                AsnLength::Bits32 => body.extend_from_slice(&asn.to_be_bytes()),
            }
        }
        attr(2, &body)
    }

    /// MP_UNREACH_NLRI for AFI 1, SAFI 4 (labelled unicast): one NLRI whose first 24 of 48 bits are
    /// the compatibility field, leaving 203.0.113.0/24.
    fn labelled_withdrawal_attr() -> Vec<u8> {
        let mut body = Vec::new();
        body.extend_from_slice(&1u16.to_be_bytes());
        body.push(4);
        body.push(48);
        body.extend_from_slice(&[0x00, 0x01, 0x02]);
        body.extend_from_slice(&[203, 0, 113]);
        attr(15, &body)
    }

    /// A one-octet NLRI encoding of an IPv4 prefix.
    fn nlri(prefix: &str) -> Vec<u8> {
        let network = NetworkPrefix::from_str(prefix).expect("parse prefix");
        let bits = network.prefix.prefix_len();
        let mut out = vec![bits];
        match network.prefix {
            ipnet::IpNet::V4(v4) => {
                let octets = v4.addr().octets();
                out.extend_from_slice(&octets[..usize::from(bits).div_ceil(8)]);
            }
            ipnet::IpNet::V6(_) => panic!("test builds IPv4 NLRI here"),
        }
        out
    }

    fn owned_elems(wire: &[u8]) -> Vec<BgpElem> {
        crate::BgpkitParser::from_reader(Cursor::new(wire.to_vec()))
            .into_elem_iter()
            .collect()
    }

    fn borrowed_elems(wire: &[u8]) -> Vec<BgpElem> {
        let mut parser = crate::BgpkitParser::from_reader(Cursor::new(wire.to_vec()));
        let mut elems = Vec::new();
        while let Some(record) = parser.next_record_elems() {
            for elem in &record {
                elems.push(elem.to_bgp_elem());
            }
        }
        elems
    }

    /// Wrap a body in an MRT header of an explicit entry type and subtype.
    fn mrt_typed(entry: EntryType, subtype: u16, body: Vec<u8>) -> Vec<u8> {
        let mut wire = Vec::new();
        wire.extend_from_slice(&1_600_000_000u32.to_be_bytes());
        wire.extend_from_slice(&(entry as u16).to_be_bytes());
        wire.extend_from_slice(&subtype.to_be_bytes());
        wire.extend_from_slice(&(body.len() as u32).to_be_bytes());
        wire.extend_from_slice(&body);
        wire
    }

    /// A `PEER_INDEX_TABLE` record with one peer, then one `RIB_AFI` record for the prefix, mirroring
    /// what a dump carries.
    fn rib_record(
        rib_subtype: u16,
        prefix_len: u8,
        prefix_bytes: &[u8],
        attributes: &[u8],
    ) -> Vec<u8> {
        let mut peer_table = Vec::new();
        peer_table.extend_from_slice(&[192, 0, 2, 10]); // collector BGP id
        peer_table.extend_from_slice(&0u16.to_be_bytes()); // empty view name
        peer_table.extend_from_slice(&1u16.to_be_bytes()); // one peer
        peer_table.push(0); // peer type
        peer_table.extend_from_slice(&[192, 0, 2, 20]); // peer BGP id
        peer_table.extend_from_slice(&[192, 0, 2, 30]); // peer address
        peer_table.extend_from_slice(&64496u32.to_be_bytes()); // peer ASN
        let mut wire = mrt_typed(
            EntryType::TABLE_DUMP_V2,
            TableDumpV2Type::PeerIndexTable as u16,
            peer_table,
        );

        let mut body = Vec::new();
        body.extend_from_slice(&1u32.to_be_bytes()); // sequence number
        body.push(prefix_len);
        body.extend_from_slice(prefix_bytes);
        body.extend_from_slice(&1u16.to_be_bytes()); // one entry
        body.extend_from_slice(&0u16.to_be_bytes()); // peer index
        body.extend_from_slice(&1_600_000_000u32.to_be_bytes()); // originated time
        body.extend_from_slice(&(attributes.len() as u16).to_be_bytes());
        body.extend_from_slice(attributes);
        wire.extend_from_slice(&mrt_typed(EntryType::TABLE_DUMP_V2, rib_subtype, body));
        wire
    }

    /// Both paths must produce the same elements for a shape that is legal but rare in real traffic.
    fn assert_same(wire: &[u8]) -> Vec<BgpElem> {
        let owned = owned_elems(wire);
        let borrowed = borrowed_elems(wire);
        assert_eq!(owned, borrowed, "owned and borrowed elements differ");
        owned
    }

    #[test]
    fn empty_as_path_segment() {
        let attributes: Vec<u8> = [origin_igp(), as_path_with_empty_segment(), next_hop()].concat();
        let wire = mrt(
            Bgp4MpType::MessageAs4,
            bgp4mp_body(
                AsnLength::Bits32,
                Afi::Ipv4,
                2,
                &update_payload(&[], &attributes, &nlri("198.51.100.0/24")),
            ),
        );
        let elems = assert_same(&wire);
        assert_eq!(elems.len(), 1);
        let rendered = elems[0].as_path.as_ref().map(|path| path.to_string());
        assert!(
            rendered
                .as_deref()
                .is_some_and(|path| path.contains(" 3356 174")),
            "as_path was {rendered:?}"
        );
    }

    #[test]
    fn labelled_withdrawal() {
        let attributes: Vec<u8> = [
            origin_igp(),
            as_path(&[3356, 174], AsnLength::Bits32),
            labelled_withdrawal_attr(),
        ]
        .concat();
        let wire = mrt(
            Bgp4MpType::MessageAs4,
            bgp4mp_body(
                AsnLength::Bits32,
                Afi::Ipv4,
                2,
                &update_payload(&[], &attributes, &[]),
            ),
        );
        let elems = assert_same(&wire);
        assert_eq!(elems.len(), 1);
        assert_eq!(elems[0].prefix.prefix.to_string(), "203.0.113.0/24");
        assert_eq!(elems[0].elem_type, ElemType::WITHDRAW);
    }

    /// A dump's `NEXT_HOP` is encoded with the record's family, so an IPv6 entry carries sixteen
    /// bytes. Reading it as IPv4 would report the first four octets as the next hop.
    #[test]
    fn ipv6_dump_entry_reads_a_sixteen_byte_next_hop() {
        let next_hop = [0x20u8, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1];
        let attributes: Vec<u8> = [
            origin_igp(),
            as_path(&[3356, 174], AsnLength::Bits32),
            attr(3, &next_hop),
        ]
        .concat();
        // RIB_IPV6_UNICAST, 2001:db8::/32
        let wire = rib_record(4, 32, &[0x20, 0x01, 0x0d, 0xb8], &attributes);
        let elems = assert_same(&wire);
        assert_eq!(elems.len(), 1);
        assert_eq!(elems[0].next_hop, Some("2001:db8::1".parse().unwrap()));
    }

    /// The prefix accessor must carry the ADD-PATH identifier the record declared.
    #[test]
    fn add_path_identifier_survives_the_prefix_accessor() {
        let attributes: Vec<u8> = [
            origin_igp(),
            as_path(&[3356, 174], AsnLength::Bits32),
            next_hop(),
        ]
        .concat();
        let mut announced = 7u32.to_be_bytes().to_vec();
        announced.extend_from_slice(&nlri("203.0.113.0/24"));
        let wire = mrt(
            Bgp4MpType::MessageAs4Addpath,
            bgp4mp_body(
                AsnLength::Bits32,
                Afi::Ipv4,
                2,
                &update_payload(&[], &attributes, &announced),
            ),
        );
        let elems = assert_same(&wire);
        assert_eq!(elems.len(), 1);
        assert_eq!(elems[0].prefix.path_id, Some(7));

        let mut parser = crate::BgpkitParser::from_reader(Cursor::new(wire));
        let mut seen = 0;
        while let Some(record) = parser.next_record_elems() {
            for elem in &record {
                assert_eq!(elem.prefix().path_id, Some(7));
                seen += 1;
            }
        }
        assert_eq!(seen, 1);
    }

    /// A 2-byte-ASN BGP4MP record is outside the borrowed view's scope: it produces no elements
    /// there while the owned path parses it. This pins the documented boundary.
    #[test]
    fn two_byte_asn_record_is_not_served_by_the_view() {
        let attributes: Vec<u8> = [
            origin_igp(),
            as_path(&[3356, 174], AsnLength::Bits16),
            next_hop(),
        ]
        .concat();
        let wire = mrt(
            Bgp4MpType::Message,
            bgp4mp_body(
                AsnLength::Bits16,
                Afi::Ipv4,
                2,
                &update_payload(&[], &attributes, &nlri("198.51.100.0/24")),
            ),
        );
        assert_eq!(owned_elems(&wire).len(), 1);
        assert!(borrowed_elems(&wire).is_empty());
    }

    /// An IPv6 announcement in a record that also carries an IPv4 NEXT_HOP: the owned path prefers
    /// the attribute, so the view must read it as IPv4 rather than with the entry's family.
    #[test]
    fn ipv6_announcement_with_ipv4_next_hop_attribute() {
        let mut mp_reach = Vec::new();
        mp_reach.extend_from_slice(&2u16.to_be_bytes());
        mp_reach.push(1);
        mp_reach.push(16);
        mp_reach.extend_from_slice(&[
            0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xfe,
        ]);
        mp_reach.push(0);
        mp_reach.push(32);
        mp_reach.extend_from_slice(&[0x20, 0x01, 0x0d, 0xb8]);
        let attributes: Vec<u8> = [
            origin_igp(),
            as_path(&[3356, 174], AsnLength::Bits32),
            next_hop(),
            attr(14, &mp_reach),
        ]
        .concat();
        let wire = mrt(
            Bgp4MpType::MessageAs4,
            bgp4mp_body(
                AsnLength::Bits32,
                Afi::Ipv4,
                2,
                &update_payload(&[], &attributes, &[]),
            ),
        );
        let elems = assert_same(&wire);
        assert_eq!(elems.len(), 1);
        assert_eq!(elems[0].next_hop, Some("192.0.2.254".parse().unwrap()));
    }
}