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
/*!
Default iterator implementations that skip errors and return successfully parsed items.
*/
use crate::error::ParserError;
use crate::models::*;
use crate::parser::iters::{record_matches_filters, write_mrt_core_dump};
use crate::parser::BgpkitParser;
use crate::{Elementor, Filterable};
use log::{error, warn};
use std::io::Read;
/*********
MrtRecord Iterator
**********/
pub struct RecordIterator<R> {
pub parser: BgpkitParser<R>,
pub count: u64,
elementor: Elementor,
}
impl<R> RecordIterator<R> {
pub(crate) fn new(parser: BgpkitParser<R>) -> Self {
RecordIterator {
parser,
count: 0,
elementor: Elementor::new(),
}
}
}
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;
loop {
return match self.parser.next_record() {
Ok(v) => {
if record_matches_filters(&v, &self.parser.filters, &mut self.elementor) {
Some(v)
} else {
continue;
}
}
Err(e) => {
match e.error {
ParserError::TruncatedMsg(err_str) | ParserError::Unsupported(err_str) => {
if self.parser.options.show_warnings {
warn!("parser warn: {}", err_str);
}
write_mrt_core_dump(self.parser.core_dump, e.bytes);
continue;
}
ParserError::ParseError(err_str) => {
error!("parser error: {}", err_str);
write_mrt_core_dump(self.parser.core_dump, e.bytes);
if self.parser.core_dump {
None
} else {
continue;
}
}
ParserError::EofExpected => {
// normal end of file
None
}
ParserError::IoError(err) | ParserError::EofError(err) => {
// when reaching IO error, stop iterating
error!("{:?}", err);
write_mrt_core_dump(self.parser.core_dump, e.bytes);
None
}
#[cfg(feature = "oneio")]
ParserError::OneIoError(_) => None,
ParserError::FilterError(_) => {
// this should not happen at this stage
None
}
// Labeled NLRI parsing errors - treat as malformed and skip
ParserError::InvalidLabeledNlriLength
| ParserError::TruncatedLabeledNlri
| ParserError::TruncatedPrefix
| ParserError::MaxLabelStackDepthExceeded
| ParserError::PeerMaxLabelsExceeded
| ParserError::InvalidPrefix => {
if self.parser.options.show_warnings {
warn!("parser warn: labeled NLRI parsing error: {:?}", e.error);
}
continue;
}
}
}
};
}
}
}
/*********
BgpElem Iterator
**********/
pub struct ElemIterator<R> {
cache_elems: Vec<BgpElem>,
record_iter: RecordIterator<R>,
elementor: Elementor,
count: u64,
}
impl<R> ElemIterator<R> {
pub(crate) fn new(parser: BgpkitParser<R>) -> Self {
ElemIterator {
record_iter: RecordIterator::new(parser),
count: 0,
cache_elems: vec![],
elementor: Elementor::new(),
}
}
}
impl<R: Read> Iterator for ElemIterator<R> {
type Item = BgpElem;
fn next(&mut self) -> Option<BgpElem> {
self.count += 1;
loop {
// 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;
}
if self.cache_elems.is_empty() {
// refill cache elems
loop {
match self.record_iter.next() {
None => {
// no more records
return None;
}
Some(r) => {
let mut elems = self.elementor.record_to_elems(r);
if elems.is_empty() {
// somehow this record does not contain any elems, continue to parse next record
continue;
} else {
elems.reverse();
self.cache_elems = elems;
break;
}
}
}
}
// when reaching here, the `self.cache_elems` has been refilled with some more elems
}
// popping cached elems. note that the original elems order is preseved by reversing the
// vector before putting it on to cache_elems.
let elem = self.cache_elems.pop()?;
if elem.match_filters(&self.record_iter.parser.filters) {
return Some(elem);
}
}
}
}