bgpkit-commons 0.14.2

A library for common BGP-related data and functions.
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
//! Streaming parser for IRR bulk dump files.
//!
//! Reads gzipped RPSL text from a URL (via oneio), splits it into
//! per-object chunks at blank lines, parses each with `rpsl::parse_object`,
//! and extracts typed [`IrrObject`]s via [`crate::irr::extract`].
//!
//! For `WholeDb` dumps, objects whose first attribute doesn't match any
//! supported type are skipped cheaply (a string comparison on the first
//! token of each object).

use std::io::{BufRead, BufReader, Read};

use tracing::{info, warn};

use crate::irr::sources::{DumpFormat, IrrDumpUrl, IrrSource, Transport, source_by_name};
use crate::irr::types::IrrObjectType;
use crate::irr::types::{IrrAttribute, IrrObject, IrrRecord};
use crate::{BgpkitCommonsError, Result};

/// Statistics from a single dump-file parse pass.
#[derive(Debug, Clone, Default)]
pub struct ParseStats {
    /// Total RPSL objects encountered (including skipped ones).
    pub total_objects: usize,
    /// Objects successfully extracted as typed IRR objects.
    pub extracted: usize,
    /// Objects skipped (unsupported type or parse error).
    pub skipped: usize,
    /// Objects that failed extraction with an error.
    pub errors: usize,
}

/// Streaming iterator over source-faithful RPSL records.
pub struct IrrRecordIter<R: Read> {
    reader: BufReader<R>,
    format: DumpFormat,
    line: Vec<u8>,
    object_lines: Vec<String>,
    finished: bool,
}

/// Parse source-faithful RPSL records from a caller-provided reader.
pub fn parse_reader<R: Read>(reader: R, format: DumpFormat) -> IrrRecordIter<R> {
    IrrRecordIter {
        reader: BufReader::new(reader),
        format,
        line: Vec::new(),
        object_lines: Vec::new(),
        finished: false,
    }
}

impl<R: Read> IrrRecordIter<R> {
    /// Return the publication format associated with this record stream.
    pub fn format(&self) -> DumpFormat {
        self.format
    }
}

impl<R: Read> Iterator for IrrRecordIter<R> {
    type Item = Result<IrrRecord>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.finished {
            return None;
        }

        loop {
            self.line.clear();
            match self.reader.read_until(b'\n', &mut self.line) {
                Err(error) => {
                    self.finished = true;
                    return Some(Err(error.into()));
                }
                Ok(0) => {
                    self.finished = true;
                    if self.object_lines.is_empty() {
                        return None;
                    }
                    return Some(parse_record_lines(std::mem::take(&mut self.object_lines)));
                }
                Ok(_) => {}
            }

            let line = String::from_utf8_lossy(&self.line);
            let line = line.trim_end_matches(['\n', '\r']);
            // ARIN (and some other registries) terminate their whole-DB dumps
            // with a bare `EOF` marker line.
            if line.trim().is_empty()
                || line.starts_with('#')
                || line.starts_with('%')
                || line.trim() == "EOF"
            {
                if self.object_lines.is_empty() {
                    continue;
                }
                return Some(parse_record_lines(std::mem::take(&mut self.object_lines)));
            }
            self.object_lines.push(line.to_string());
        }
    }
}

fn parse_record_lines(lines: Vec<String>) -> Result<IrrRecord> {
    let mut attributes: Vec<IrrAttribute> = Vec::new();
    for line in lines {
        if line.starts_with(char::is_whitespace) {
            let Some(attribute) = attributes.last_mut() else {
                return Err(BgpkitCommonsError::invalid_format(
                    "RPSL object",
                    line,
                    "continuation line without an attribute",
                ));
            };
            attribute.value.push('\n');
            attribute.value.push_str(line.trim());
            continue;
        }
        if line.starts_with('+') || line.starts_with('-') {
            // RIPE dumps contain decorative banner/separator lines starting
            // with '+' or '-'. When an attribute is open, treat the line as a
            // continuation (some dumps wrap key material this way), stripping
            // the decorative marker; otherwise skip it as decoration between
            // objects.
            if let Some(attribute) = attributes.last_mut() {
                attribute.value.push('\n');
                attribute
                    .value
                    .push_str(line.trim().trim_start_matches(['+', '-']).trim());
            }
            continue;
        }

        let Some((name, value)) = line.split_once(':') else {
            return Err(BgpkitCommonsError::invalid_format(
                "RPSL object",
                line,
                "attribute line is missing ':'",
            ));
        };
        let name = name.trim();
        if name.is_empty() {
            return Err(BgpkitCommonsError::invalid_format(
                "RPSL object",
                line,
                "attribute name is empty",
            ));
        }
        attributes.push(IrrAttribute {
            name: name.to_string(),
            value: value.trim().to_string(),
        });
    }

    let object_type = attributes
        .first()
        .map(|attribute| attribute.name.clone())
        .ok_or_else(|| {
            BgpkitCommonsError::invalid_format("RPSL object", "", "object has no attributes")
        })?;
    Ok(IrrRecord {
        object_type,
        attributes,
    })
}

impl std::fmt::Display for ParseStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{} extracted, {} skipped, {} errors (of {} total)",
            self.extracted, self.skipped, self.errors, self.total_objects
        )
    }
}

/// Parse a single dump file and call `handler` for each extracted object.
///
/// The handler receives each [`IrrObject`] and can collect, filter, or
/// process them as needed. This is the core building block for all
/// IRR data loading.
///
/// # Arguments
///
/// * `dump_url` — The dump file descriptor (URL, transport, format).
/// * `handler` — A closure called for each successfully extracted object.
///
/// # Errors
///
/// Returns an error if the file cannot be opened or read.
pub fn parse_dump<F>(dump_url: &IrrDumpUrl, mut handler: F) -> Result<ParseStats>
where
    F: FnMut(IrrObject),
{
    info!("parsing IRR dump: {}", dump_url.url);
    let reader = fetch_dump_url(dump_url)?;
    parse_dump_from_reader(reader, dump_url.format, &mut handler)
}

/// Reader returned by [`fetch`], including the selected dump metadata.
pub struct IrrReader {
    /// Canonical catalog URL and publication format selected for this reader.
    pub dump_url: IrrDumpUrl,
    reader: Box<dyn Read>,
}

impl Read for IrrReader {
    fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
        self.reader.read(buffer)
    }
}

/// Validate a catalog source and open its dump for an object type.
pub fn fetch(source: &IrrSource, object_type: IrrObjectType) -> Result<IrrReader> {
    let source = source_by_name(source.name).ok_or_else(|| {
        BgpkitCommonsError::invalid_format("IRR source", source.name, "unknown registry name")
    })?;
    let dump_url = source
        .dump_urls(object_type)
        .into_iter()
        .next()
        .ok_or_else(|| {
            BgpkitCommonsError::invalid_format(
                "IRR dump",
                source.name,
                format!("no dump for {}", object_type.key_attr()),
            )
        })?;
    let reader = fetch_dump_url(&dump_url)?;
    Ok(IrrReader { dump_url, reader })
}

fn fetch_dump_url(dump_url: &IrrDumpUrl) -> Result<Box<dyn Read>> {
    match dump_url.transport {
        // IRR dumps are large (whole databases can be hundreds of MB) and are
        // parsed incrementally over minutes; the resumable HTTP reader
        // reconnects with Range requests if the connection drops mid-transfer
        // (oneio >= 0.26).
        Transport::Https => Ok(oneio::get_resumable_http_reader(&dump_url.url)?),
        // FTP sources (e.g. RADB) use the plain reader: the resumable reader
        // is HTTP-only and would fail building an HTTP request for an FTP URL.
        Transport::Ftp => Ok(oneio::get_reader(&dump_url.url)?),
    }
}

/// Parse a dump file from any reader (for testing or custom I/O).
pub fn parse_dump_from_reader<R: std::io::Read, F>(
    reader: R,
    format: DumpFormat,
    handler: &mut F,
) -> Result<ParseStats>
where
    F: FnMut(IrrObject),
{
    let mut stats = ParseStats::default();
    for record in parse_reader(reader, format) {
        stats.total_objects += 1;
        match record.and_then(|record| record.to_typed()) {
            Ok(Some(typed)) => {
                stats.extracted += 1;
                handler(typed);
            }
            Ok(None) => stats.skipped += 1,
            Err(error) => {
                stats.errors += 1;
                warn!("IRR parse error: {error}");
            }
        }
    }

    info!("IRR dump parsed: {}", stats);
    Ok(stats)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::irr::types::IrrObject;
    use std::io::Cursor;

    const SAMPLE_RIPE_AUTNUM: &str = "\
aut-num:        AS13335
as-name:        CLOUDFLARENET
descr:          Cloudflare, Inc.
org:            ORG-CF165-RIPE
mnt-by:         CLOUDFLARE-MNT
source:         RIPE

aut-num:        AS15169
as-name:        GOOGLE
descr:          Google LLC
source:         RIPE

route:          1.1.1.0/24
origin:         AS13335
descr:          Cloudflare
source:         RIPE
";

    #[test]
    fn test_parse_aut_num_and_route() {
        let mut objects = Vec::new();
        let stats = parse_dump_from_reader(
            Cursor::new(SAMPLE_RIPE_AUTNUM),
            DumpFormat::SplitFiles,
            &mut |obj| objects.push(obj),
        )
        .unwrap();

        assert_eq!(stats.total_objects, 3);
        assert_eq!(stats.extracted, 3);
        assert_eq!(stats.skipped, 0);
        assert_eq!(stats.errors, 0);

        // First: aut-num AS13335
        match &objects[0] {
            IrrObject::AutNum(a) => {
                assert_eq!(a.asn, 13335);
                assert_eq!(a.as_name, "CLOUDFLARENET");
                assert_eq!(a.source, "RIPE");
                assert!(a.descr.iter().any(|d| d.contains("Cloudflare")));
            }
            other => panic!("expected AutNum, got {other:?}"),
        }

        // Second: aut-num AS15169
        match &objects[1] {
            IrrObject::AutNum(a) => {
                assert_eq!(a.asn, 15169);
                assert_eq!(a.as_name, "GOOGLE");
            }
            other => panic!("expected AutNum, got {other:?}"),
        }

        // Third: route 1.1.1.0/24
        match &objects[2] {
            IrrObject::Route(r) => {
                assert_eq!(r.prefix.to_string(), "1.1.1.0/24");
                assert_eq!(r.origin, 13335);
            }
            other => panic!("expected Route, got {other:?}"),
        }
    }

    const SAMPLE_ASSET: &str = "\
as-set:         AS-EXAMPLE
descr:          Example AS set
members:        AS64496, AS64497
members:        AS-EXAMPLE-CLIENTS
members:        AS64500
source:         RIPE
";

    #[test]
    fn test_parse_as_set() {
        let mut objects = Vec::new();
        parse_dump_from_reader(
            Cursor::new(SAMPLE_ASSET),
            DumpFormat::SplitFiles,
            &mut |obj| objects.push(obj),
        )
        .unwrap();

        assert_eq!(objects.len(), 1);
        match &objects[0] {
            IrrObject::AsSet(s) => {
                assert_eq!(s.name, "AS-EXAMPLE");
                assert_eq!(s.members, vec![64496, 64497, 64500]);
                assert_eq!(s.set_members, vec!["AS-EXAMPLE-CLIENTS"]);
            }
            other => panic!("expected AsSet, got {other:?}"),
        }
    }

    const SAMPLE_WITH_COMMENTS: &str = "\
# This is a comment
# Another comment

aut-num:        AS3356
as-name:        LEVEL3
source:         RIPE
";

    #[test]
    fn test_skip_comments() {
        let mut objects = Vec::new();
        let stats = parse_dump_from_reader(
            Cursor::new(SAMPLE_WITH_COMMENTS),
            DumpFormat::SplitFiles,
            &mut |obj| objects.push(obj),
        )
        .unwrap();

        assert_eq!(objects.len(), 1);
        assert_eq!(stats.extracted, 1);
    }

    const SAMPLE_SKIP_UNKNOWN: &str = "\
person:         John Doe
nic-hdl:        JD-RIPE
source:         RIPE

aut-num:        AS1299
as-name:        TWELVE99
source:         RIPE
";

    #[test]
    fn test_skip_unknown_types() {
        let mut objects = Vec::new();
        let stats = parse_dump_from_reader(
            Cursor::new(SAMPLE_SKIP_UNKNOWN),
            DumpFormat::WholeDb,
            &mut |obj| objects.push(obj),
        )
        .unwrap();

        assert_eq!(objects.len(), 1);
        assert_eq!(stats.total_objects, 2);
        assert_eq!(stats.extracted, 1);
        assert_eq!(stats.skipped, 1);
    }
}