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
/*!
Default iterator implementations that skip errors and return successfully parsed items.
*/
use crate::models::*;
use crate::parser::iters::{handle_record_parse_error, record_matches_filters};
use crate::parser::mrt::mrt_elem::PendingElems;
use crate::parser::BgpkitParser;
use crate::{Elementor, Filterable};
use std::io::Read;
#[cfg(test)]
mod tests;
/*********
MrtRecord Iterator
**********/
pub struct RecordIterator<R> {
pub parser: BgpkitParser<R>,
pub count: u64,
elementor: Elementor,
/// Whether the parser's filters are applied once per record.
///
/// Elem iterators test every elem they build, so a record-level pass costs
/// a full elem conversion per record (plus a record clone) that is thrown
/// away. They construct this iterator with the pass disabled.
prefilter: bool,
}
impl<R> RecordIterator<R> {
pub(crate) fn new(parser: BgpkitParser<R>) -> Self {
RecordIterator {
parser,
count: 0,
elementor: Elementor::new(),
prefilter: true,
}
}
/// Skip the record-level filter pass for an iterator whose consumer filters
/// the elems it builds anyway.
pub(crate) fn without_prefilter(mut self) -> Self {
self.prefilter = false;
self
}
}
impl<R: Read> Iterator for RecordIterator<R> {
type Item = MrtRecord;
fn next(&mut self) -> Option<MrtRecord> {
// Text-dump parsers have no MRT-record representation; short-circuit
// instead of spinning forever on Unsupported errors from next_record().
if self.parser.text_dump_iter.is_some() {
return None;
}
self.count += 1;
// A record whose single RIB prefix cannot satisfy a prefix filter yields no matching
// elements, so it can be skipped before its body is parsed.
// A record skipped here never raises its parse error, and core-dump mode stops at the
// first one, so pushdown is off while dumping.
let record_pushdown = !self.parser.core_dump
&& crate::parser::filter::has_record_level_constraint(&self.parser.filters);
loop {
let next_record = if record_pushdown {
self.parser.next_record_prefiltered()
} else {
self.parser.next_record()
};
return match next_record {
Ok(v) => {
if !self.prefilter
|| record_matches_filters(&v, &self.parser.filters, &mut self.elementor)
{
Some(v)
} else {
continue;
}
}
Err(e) => {
if handle_record_parse_error(&mut self.parser, e.error, e.bytes) {
continue;
}
None
}
};
}
}
}
/*********
BgpElem Iterator
**********/
/// What one framed record produced for the element iterator.
enum Refill {
/// `batch` now holds elements to drain.
Batch,
/// The record went to the owned path, which filled `pending`.
Owned,
/// End of input, or an error the parser cannot recover from.
End,
}
pub struct ElemIterator<R> {
pending: PendingElems,
/// Rejects an entry before its element is built, when a predicate can decide it from attributes.
screen: Option<std::sync::Arc<crate::parser::attr_scan::EntryScreen>>,
/// Elements materialised from one borrowed RIB record, drained before anything else.
batch: std::vec::IntoIter<BgpElem>,
/// Whether records are dispatched per frame. A core dump needs the owned iterator to read the
/// stream itself, because it has to hand back the bytes around a parse error.
framed: bool,
record_iter: RecordIterator<R>,
elementor: Elementor,
count: u64,
}
impl<R> ElemIterator<R> {
pub(crate) fn new(parser: BgpkitParser<R>) -> Self {
// The framed path reads records through the parser's own buffer; the element loop applies
// the screen and the filters to what it produces.
let framed = !parser.core_dump;
let screen =
crate::parser::attr_scan::EntryScreen::new(&parser.filters).map(std::sync::Arc::new);
ElemIterator {
screen,
record_iter: RecordIterator::new(parser).without_prefilter(),
count: 0,
pending: PendingElems::Empty,
batch: Vec::new().into_iter(),
framed,
elementor: Elementor::new(),
}
}
}
impl<R: Read> ElemIterator<R> {
/// Read one framed record and route it: RIB dump records are materialised from the borrowed
/// view, everything else is parsed by the owned path.
fn refill(&mut self) -> Refill {
use crate::parser::iters::elem_ref::NextBatch;
match self.record_iter.parser.next_elem_batch() {
NextBatch::Rib(record) => {
// The declared entry count is attacker-controlled; cap the reservation so a
// malformed record cannot ask for a large allocation per record.
let mut elems = Vec::with_capacity(record.len().min(1024));
let screen = self.screen.clone();
for elem in record.iter() {
// An entry the screen can reject from its bytes never becomes an element.
if let Some(screen) = screen.as_deref() {
if !screen.admits_bytes(elem.attrs_bytes()) {
continue;
}
}
elems.push(elem.to_bgp_elem());
}
// The loop above is the last use of `record`, so its borrow of the parser's
// buffers has ended and the filters can be read here.
let filters = &self.record_iter.parser.filters;
if !filters.is_empty() {
elems.retain(|elem| elem.match_filters(filters));
}
self.batch = elems.into_iter();
Refill::Batch
}
NextBatch::Other {
header,
raw_header,
body_len,
} => {
match self.record_iter.parser.parse_framed_record(
header,
raw_header.as_slice(),
body_len,
) {
Ok(record) => {
self.pending = self.elementor.ingest(record);
Refill::Owned
}
Err(failure) => {
if handle_record_parse_error(
&mut self.record_iter.parser,
failure.error,
failure.bytes,
) {
Refill::Owned
} else {
Refill::End
}
}
}
}
NextBatch::End => Refill::End,
}
}
}
impl<R: Read> Iterator for ElemIterator<R> {
type Item = BgpElem;
fn next(&mut self) -> Option<BgpElem> {
self.count += 1;
// Fast path: drain streaming text-dump elems directly, with filter support.
if let Some(iter) = &mut self.record_iter.parser.text_dump_iter {
for elem in iter.by_ref() {
if elem.match_filters(&self.record_iter.parser.filters) {
return Some(elem);
}
}
return None;
}
loop {
// Anything already produced comes out first: materialised RIB elements, then the owned
// iterator's pending elements.
if let Some(elem) = self.batch.next() {
return Some(elem);
}
while let Some(elem) = self.pending.next_elem(self.elementor.peer_table.as_ref()) {
if elem.match_filters(&self.record_iter.parser.filters) {
return Some(elem);
}
}
if self.framed {
match self.refill() {
Refill::Batch | Refill::Owned => continue,
Refill::End => return None,
}
}
let record = self.record_iter.next()?;
// This branch is core-dump mode: it builds every element and lets the filter decide.
self.pending = self.elementor.ingest(record);
}
}
}