bgpkit-parser 0.16.0

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
/*!
parser module maintains the main logic for processing BGP and MRT messages.
*/
use std::io::Read;

#[macro_use]
pub mod utils;
pub mod bgp;
pub mod bmp;
pub mod filter;
pub mod iters;
pub mod mrt;
pub mod rpki;

#[cfg(feature = "rislive")]
pub mod rislive;

pub(crate) use self::utils::*;

use crate::models::MrtRecord;
pub use mrt::mrt_elem::{BgpUpdateElemIter, ElemError, Elementor, RecordElemIter};
#[cfg(feature = "oneio")]
use oneio::{get_cache_reader, get_reader};

pub use crate::error::{ParserError, ParserErrorWithBytes};
pub use bmp::{parse_bmp_msg, parse_openbmp_header, parse_openbmp_msg};
pub use filter::*;
pub use iters::*;
pub use mrt::*;

#[cfg(feature = "rislive")]
pub use rislive::parse_ris_live_message;

pub struct BgpkitParser<R> {
    reader: R,
    core_dump: bool,
    filters: Vec<Filter>,
    options: ParserOptions,
}

pub(crate) struct ParserOptions {
    show_warnings: bool,
}
impl Default for ParserOptions {
    fn default() -> Self {
        ParserOptions {
            show_warnings: true,
        }
    }
}

#[cfg(feature = "oneio")]
impl BgpkitParser<Box<dyn Read + Send>> {
    /// Creating a new parser from a object that implements [Read] trait.
    pub fn new(path: &str) -> Result<Self, ParserErrorWithBytes> {
        let reader = get_reader(path)?;
        Ok(BgpkitParser {
            reader,
            core_dump: false,
            filters: vec![],
            options: ParserOptions::default(),
        })
    }

    /// Creating a new parser that also caches the remote content to a local cache directory.
    ///
    /// The cache file name is generated by the following format: `cache-<crc32 of file name>-<file name>`.
    /// For example, the remote file `http://archive.routeviews.org/route-views.chile/bgpdata/2023.03/RIBS/rib.20230326.0600.bz2`
    /// will be cached as `cache-682cb1eb-rib.20230326.0600.bz2` in the cache directory.
    pub fn new_cached(path: &str, cache_dir: &str) -> Result<Self, ParserErrorWithBytes> {
        let file_name = path.rsplit('/').next().unwrap().to_string();
        let new_file_name = format!(
            "cache-{}",
            add_suffix_to_filename(file_name.as_str(), crc32(path).as_str())
        );
        let reader = get_cache_reader(path, cache_dir, Some(new_file_name), false)?;
        Ok(BgpkitParser {
            reader,
            core_dump: false,
            filters: vec![],
            options: ParserOptions::default(),
        })
    }
}

#[cfg(feature = "oneio")]
fn add_suffix_to_filename(filename: &str, suffix: &str) -> String {
    let mut parts: Vec<&str> = filename.split('.').collect(); // Split filename by dots
    if parts.len() > 1 {
        let last_part = parts.pop().unwrap(); // Remove the last part (suffix) from the parts vector
        let new_last_part = format!("{suffix}.{last_part}"); // Add the suffix to the last part
        parts.push(&new_last_part); // Add the updated last part back to the parts vector
        parts.join(".") // Join the parts back into a filename string with dots
    } else {
        // If the filename does not have any dots, simply append the suffix to the end
        format!("{filename}.{suffix}")
    }
}

impl<R: Read> BgpkitParser<R> {
    /// Creating a new parser from an object that implements [Read] trait.
    pub fn from_reader(reader: R) -> Self {
        BgpkitParser {
            reader,
            core_dump: false,
            filters: vec![],
            options: ParserOptions::default(),
        }
    }

    /// This is used in for loop `for item in parser{}`
    pub fn next_record(&mut self) -> Result<MrtRecord, ParserErrorWithBytes> {
        parse_mrt_record(&mut self.reader)
    }
}

impl<R> BgpkitParser<R> {
    pub fn enable_core_dump(self) -> Self {
        BgpkitParser {
            reader: self.reader,
            core_dump: true,
            filters: self.filters,
            options: self.options,
        }
    }

    pub fn disable_warnings(self) -> Self {
        let mut options = self.options;
        options.show_warnings = false;
        BgpkitParser {
            reader: self.reader,
            core_dump: self.core_dump,
            filters: self.filters,
            options,
        }
    }

    /// Add a filter to the parser by specifying filter type and value as strings.
    ///
    /// This method parses the filter type and value strings to create a [`Filter`] and adds it
    /// to the parser's filter list. For the full list of available filter types and their
    /// formats, see the [`Filter`] struct documentation.
    ///
    /// # Available Filter Types
    ///
    /// - `origin_asn` - Origin AS number (e.g., "12345")
    /// - `origin_asns` - Multiple origin AS numbers, comma-separated (e.g., "12345,67890")
    /// - `prefix` - Exact prefix match (e.g., "192.168.1.0/24")
    /// - `prefix_super` - Match prefix and super-prefixes
    /// - `prefix_sub` - Match prefix and sub-prefixes
    /// - `prefix_super_sub` - Match prefix, super-prefixes, and sub-prefixes
    /// - `prefixes` - Multiple prefixes (e.g., "1.1.1.0/24,8.8.8.0/24")
    /// - `peer_ip` - Peer IP address (e.g., "192.168.1.1")
    /// - `peer_ips` - Multiple peer IPs (e.g., "192.168.1.1,192.168.1.2")
    /// - `peer_asn` - Peer AS number (e.g., "12345")
    /// - `peer_asns` - Multiple peer AS numbers (e.g., "12345,67890")
    /// - `type` - Message type: "a"/"announce" or "w"/"withdraw"
    /// - `ts_start` - Start timestamp (unix timestamp or RFC3339)
    /// - `ts_end` - End timestamp (unix timestamp or RFC3339)
    /// - `as_path` - AS path regex pattern
    /// - `community` - Community regex pattern
    /// - `ip_version` - IP version: "4"/"ipv4" or "6"/"ipv6"
    ///
    /// # Negative Filters
    ///
    /// Most filters support negation by prefixing the value with `!`. For example:
    /// - `origin_asn=!13335` matches elements where origin AS is NOT 13335
    /// - `prefix=!10.0.0.0/8` matches elements where prefix is NOT 10.0.0.0/8
    ///
    /// # Example
    ///
    /// ```no_run
    /// use bgpkit_parser::BgpkitParser;
    ///
    /// let parser = BgpkitParser::new("https://spaces.bgpkit.org/parser/update-example.gz")
    ///     .unwrap()
    ///     .add_filter("peer_ip", "185.1.8.65")
    ///     .unwrap()
    ///     .add_filter("type", "w")
    ///     .unwrap();
    ///
    /// for elem in parser {
    ///     println!("{}", elem);
    /// }
    /// ```
    pub fn add_filter(
        self,
        filter_type: &str,
        filter_value: &str,
    ) -> Result<Self, ParserErrorWithBytes> {
        let mut filters = self.filters;
        filters.push(Filter::new(filter_type, filter_value)?);
        Ok(BgpkitParser {
            reader: self.reader,
            core_dump: self.core_dump,
            filters,
            options: self.options,
        })
    }

    /// Add multiple filters to the parser.
    ///
    /// This method extends the existing filters with the provided slice of filters.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use bgpkit_parser::BgpkitParser;
    /// use bgpkit_parser::parser::Filter;
    ///
    /// let filters = vec![
    ///     Filter::new("peer_ip", "185.1.8.65").unwrap(),
    ///     Filter::new("type", "w").unwrap(),
    /// ];
    ///
    /// let parser = BgpkitParser::new("https://spaces.bgpkit.org/parser/update-example.gz")
    ///     .unwrap()
    ///     .add_filters(&filters);
    /// ```
    pub fn add_filters(mut self, filters: &[Filter]) -> Self {
        self.filters.extend(filters.iter().cloned());
        self
    }

    /// Set filters directly, replacing any existing filters.
    ///
    /// This method allows passing a pre-built `Vec<Filter>` directly to the parser,
    /// bypassing the need to parse filter strings. This is useful when you want to
    /// build filter specifications independently and reuse them across multiple parsers.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use bgpkit_parser::BgpkitParser;
    /// use bgpkit_parser::parser::Filter;
    ///
    /// // Build filters independently
    /// let filters = vec![
    ///     Filter::new("peer_ip", "185.1.8.65").unwrap(),
    ///     Filter::new("type", "w").unwrap(),
    /// ];
    ///
    /// // Apply to multiple parsers (no manual clone needed)
    /// let parser1 = BgpkitParser::new("https://spaces.bgpkit.org/parser/update-example.gz")
    ///     .unwrap()
    ///     .with_filters(&filters);
    ///
    /// let parser2 = BgpkitParser::new("https://spaces.bgpkit.org/parser/update-example.gz")
    ///     .unwrap()
    ///     .with_filters(&filters);
    /// ```
    pub fn with_filters(mut self, filters: &[Filter]) -> Self {
        self.filters = filters.to_vec();
        self
    }
}

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

    #[test]
    fn test_new_with_reader() {
        // bzip2 reader for a compressed file
        let reader = oneio::get_reader("http://archive.routeviews.org/route-views.ny/bgpdata/2023.02/UPDATES/updates.20230215.0630.bz2").unwrap();
        assert_eq!(
            12683,
            BgpkitParser::from_reader(reader).into_elem_iter().count()
        );

        // remote reader for an uncompressed updates file
        let reader = oneio::get_reader("https://spaces.bgpkit.org/parser/update-example").unwrap();
        assert_eq!(
            8160,
            BgpkitParser::from_reader(reader).into_elem_iter().count()
        );
    }

    #[test]
    fn test_new_cached_with_reader() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";
        let parser = BgpkitParser::new_cached(url, "/tmp/bgpkit-parser-tests")
            .unwrap()
            .enable_core_dump()
            .disable_warnings();
        let count = parser.into_elem_iter().count();
        assert_eq!(8160, count);
        let parser = BgpkitParser::new_cached(url, "/tmp/bgpkit-parser-tests").unwrap();
        let count = parser.into_elem_iter().count();
        assert_eq!(8160, count);
    }

    #[test]
    fn test_add_suffix_to_filename() {
        // Test with a filename that has dots
        let filename = "example.txt";
        let suffix = "suffix";
        let result = add_suffix_to_filename(filename, suffix);
        assert_eq!(result, "example.suffix.txt");

        // Test with a filename that has multiple dots
        let filename = "example.tar.gz";
        let suffix = "suffix";
        let result = add_suffix_to_filename(filename, suffix);
        assert_eq!(result, "example.tar.suffix.gz");

        // Test with a filename that has no dots
        let filename = "example";
        let suffix = "suffix";
        let result = add_suffix_to_filename(filename, suffix);
        assert_eq!(result, "example.suffix");

        // Test with an empty filename
        let filename = "";
        let suffix = "suffix";
        let result = add_suffix_to_filename(filename, suffix);
        assert_eq!(result, ".suffix");

        // Test with an empty suffix
        let filename = "example.txt";
        let suffix = "";
        let result = add_suffix_to_filename(filename, suffix);
        assert_eq!(result, "example..txt");
    }

    #[test]
    fn test_with_filters() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";

        // Build filters independently
        let filters = vec![
            Filter::new("peer_ip", "185.1.8.65").unwrap(),
            Filter::new("type", "w").unwrap(),
        ];

        // Test with_filters - sets filters directly
        let parser = BgpkitParser::new(url).unwrap().with_filters(&filters);
        let count = parser.into_elem_iter().count();

        // peer 185.1.8.65 has 3393 total, 132 withdrawals
        assert_eq!(count, 132);

        // Test that with_filters replaces existing filters
        let filters1 = vec![Filter::new("peer_ip", "185.1.8.65").unwrap()];
        let filters2 = vec![Filter::new("peer_ip", "185.1.8.50").unwrap()];

        let parser = BgpkitParser::new(url)
            .unwrap()
            .with_filters(&filters1)
            .with_filters(&filters2); // Should replace filters1
        let count = parser.into_elem_iter().count();

        // peer 185.1.8.50 has 1563 elements
        assert_eq!(count, 1563);
    }

    #[test]
    fn test_add_filters() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";

        // Build filters independently
        let filters = vec![
            Filter::new("peer_ip", "185.1.8.65").unwrap(),
            Filter::new("type", "w").unwrap(),
        ];

        // Test add_filters - extends existing filters
        let parser = BgpkitParser::new(url).unwrap().add_filters(&filters);
        let count = parser.into_elem_iter().count();

        // peer 185.1.8.65 has 3393 total, 132 withdrawals
        assert_eq!(count, 132);

        // Test combining add_filter and add_filters
        let parser = BgpkitParser::new(url)
            .unwrap()
            .add_filter("peer_ip", "185.1.8.65")
            .unwrap()
            .add_filters(&[Filter::new("type", "w").unwrap()]);
        let count = parser.into_elem_iter().count();
        assert_eq!(count, 132);
    }

    #[test]
    fn test_with_filters_empty() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";

        // Test with empty filters - should return all elements
        let parser = BgpkitParser::new(url).unwrap().with_filters(&[]);
        let count = parser.into_elem_iter().count();

        // Total elements in the file
        assert_eq!(count, 8160);
    }

    #[test]
    fn test_add_filters_empty() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";

        // Test adding empty filters - should not change behavior
        let parser = BgpkitParser::new(url)
            .unwrap()
            .add_filter("peer_ip", "185.1.8.65")
            .unwrap()
            .add_filters(&[]);
        let count = parser.into_elem_iter().count();

        // peer 185.1.8.65 has 3393 elements
        assert_eq!(count, 3393);
    }

    #[test]
    fn test_with_filters_reuse() {
        let url = "https://spaces.bgpkit.org/parser/update-example.gz";

        // Build filters once
        let filters = vec![
            Filter::new("peer_ip", "185.1.8.65").unwrap(),
            Filter::new("type", "w").unwrap(),
        ];

        // Apply to multiple parsers (simulating reuse pattern - no clone needed)
        let parser1 = BgpkitParser::new(url).unwrap().with_filters(&filters);
        let count1 = parser1.into_elem_iter().count();

        let parser2 = BgpkitParser::new(url).unwrap().with_filters(&filters);
        let count2 = parser2.into_elem_iter().count();

        // Both should have same count: 132 withdrawals from peer 185.1.8.65
        assert_eq!(count1, 132);
        assert_eq!(count2, 132);
    }
}