originet 1.1.0

Map IP addresses to origin ASNs with CAIDA's RouteViews prefix-to-AS snapshots, fetched on the fly or read from a local file
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
//! Result records and the output formats.

use std::fmt::Write as _;
use std::io::Write;

use anstyle::{AnsiColor, Style};
use serde::Serialize;

use crate::caida::Family;

/// Where a table came from.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Source {
    /// Downloaded from (or cached from) CAIDA.
    Caida,
    /// A file the user supplied.
    Local,
}

/// The snapshot behind one address family's answers.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SnapshotInfo {
    /// `ipv4` or `ipv6`.
    pub family: Family,
    /// `caida` or `local`.
    pub source: Source,
    /// The snapshot date (`YYYY-MM-DD`) when it is known from the file name.
    pub date: Option<String>,
    /// File name of the snapshot.
    pub file: String,
    /// Download URL (CAIDA) or local path.
    pub location: String,
    /// Number of prefixes loaded.
    pub prefixes: usize,
}

/// One answer. Flat on purpose so every format carries the same columns.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct Record {
    /// The address, normalized (`8.8.8.8/32` and `::ffff:8.8.8.8` become `8.8.8.8`); an
    /// invalid input is kept as typed.
    pub ip: String,
    /// The longest matching prefix, or null when none covers the address.
    pub prefix: Option<String>,
    /// The origin ASN: the first ASN of a multi-origin or AS-set field.
    pub asn: Option<u32>,
    /// All origins in CAIDA's notation (`A`, `A_B` for multi-origin, `A,B` for an AS set).
    pub origins: Option<String>,
    /// Date of the snapshot that answered, when known.
    pub snapshot: Option<String>,
    /// Why the input could not be looked up, for invalid lines.
    pub error: Option<String>,
}

/// Counts over all records.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize)]
pub struct Summary {
    /// Records in total.
    pub total: usize,
    /// Addresses with a matching prefix.
    pub found: usize,
    /// Valid addresses without a matching prefix.
    pub not_found: usize,
    /// Inputs that were not IP addresses.
    pub invalid: usize,
}

impl Summary {
    /// Tallies the records.
    pub fn of(records: &[Record]) -> Self {
        let mut summary = Self::default();
        for record in records {
            summary.total += 1;
            if record.error.is_some() {
                summary.invalid += 1;
            } else if record.asn.is_some() {
                summary.found += 1;
            } else {
                summary.not_found += 1;
            }
        }
        summary
    }
}

/// The complete output of a run.
#[derive(Debug, Clone, Serialize)]
pub struct Report {
    /// Name and version of this tool.
    pub tool: Tool,
    /// When the report was produced (RFC 3339, UTC).
    pub generated_at: String,
    /// The snapshots used, one per address family that was needed.
    pub snapshots: Vec<SnapshotInfo>,
    /// Totals.
    pub summary: Summary,
    /// One record per input, in input order.
    pub results: Vec<Record>,
}

/// Tool identification embedded in the JSON report.
#[derive(Debug, Clone, Serialize)]
pub struct Tool {
    /// `originet`.
    pub name: &'static str,
    /// Crate version.
    pub version: &'static str,
}

/// The output formats.
#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum Format {
    /// Aligned columns for reading in a terminal, results only; `asn` lists every origin.
    Table,
    /// One JSON document with metadata, summary and results.
    Json,
    /// One JSON object per line, results only.
    Jsonl,
    /// Comma-separated values with a header, results only.
    Csv,
    /// Tab-separated values with a header, results only.
    Tsv,
    /// Apache Parquet, results only.
    #[cfg(feature = "parquet")]
    Parquet,
}

impl Format {
    /// Whether the format is binary (and should not be written to a terminal).
    pub fn is_binary(self) -> bool {
        #[cfg(feature = "parquet")]
        {
            self == Format::Parquet
        }
        #[cfg(not(feature = "parquet"))]
        {
            false
        }
    }
}

/// Writes the report in the chosen format.
pub fn write(format: Format, report: &Report, writer: &mut dyn Write) -> anyhow::Result<()> {
    match format {
        Format::Table => write_table(report, writer, &Theme::plain())?,
        Format::Json => {
            serde_json::to_writer_pretty(&mut *writer, report)?;
            writer.write_all(b"\n")?;
        }
        Format::Jsonl => {
            for record in &report.results {
                serde_json::to_writer(&mut *writer, record)?;
                writer.write_all(b"\n")?;
            }
        }
        Format::Csv => write_delimited(b',', report, writer)?,
        Format::Tsv => write_delimited(b'\t', report, writer)?,
        #[cfg(feature = "parquet")]
        Format::Parquet => parquet_output::write(report, writer)?,
    }
    writer.flush()?;
    Ok(())
}

/// How the `table` format is styled. [`Theme::plain`] renders no escape codes at all;
/// [`Theme::colored`] is what the binary uses on a terminal.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Theme {
    /// The header line.
    pub header: Style,
    /// The `ip` of a valid input.
    pub ip: Style,
    /// The matched `prefix`.
    pub prefix: Style,
    /// The `asn` column when one AS originates the prefix.
    pub asn: Style,
    /// The `asn` column when several ASes originate the prefix (a MOAS) or the origin
    /// is an AS set.
    pub multiple_origins: Style,
    /// The `snapshot` date.
    pub snapshot: Style,
    /// The `-` of a missing value.
    pub missing: Style,
    /// An invalid input: its `ip` as typed and the `error`.
    pub error: Style,
}

impl Theme {
    /// No styling.
    pub fn plain() -> Self {
        Self::default()
    }

    /// Bold header, cyan prefix, bold green ASN, yellow multi-origin lists, dim
    /// snapshot dates and dashes, red invalid inputs.
    pub fn colored() -> Self {
        Self {
            header: Style::new().bold(),
            ip: Style::new(),
            prefix: AnsiColor::Cyan.on_default(),
            asn: AnsiColor::Green.on_default().bold(),
            multiple_origins: AnsiColor::Yellow.on_default(),
            snapshot: Style::new().dimmed(),
            missing: Style::new().dimmed(),
            error: AnsiColor::Red.on_default(),
        }
    }
}

/// CAIDA's origin field as people read it: `15169` stays; a multi-origin prefix
/// `149511_138421` becomes `149511, 138421`; an AS set `8035,13979` becomes
/// `{8035, 13979}`, BGP's own notation for a set. Also says whether there was more
/// than one origin.
pub fn display_origins(raw: &str) -> (String, bool) {
    let parts: Vec<String> = raw
        .split('_')
        .map(|part| {
            if part.contains(',') {
                format!("{{{}}}", part.replace(',', ", "))
            } else {
                part.to_owned()
            }
        })
        .collect();
    let multiple = parts.len() > 1 || raw.contains(',');
    (parts.join(", "), multiple)
}

/// The `table` format with a [`Theme`]: `ip`, `prefix`, `asn`, `snapshot` and `error`,
/// padded to the widest value of each. The `asn` column folds `origins` in: it shows
/// every origin (see [`display_origins`]) rather than only the first, so the table has
/// five columns where the other formats have six. A missing value is `-`, except in the
/// `error` column, which is empty unless the input was invalid; `asn` is right-aligned,
/// the last column is not padded. Padding is never inside a styled span.
pub fn write_table(report: &Report, writer: &mut dyn Write, theme: &Theme) -> anyhow::Result<()> {
    const HEADER: [&str; 5] = ["ip", "prefix", "asn", "snapshot", "error"];
    const ASN: usize = 2;
    let present = |value: Option<&str>, style: Style| match value {
        Some(value) => (value.to_owned(), style),
        None => ("-".to_owned(), theme.missing),
    };
    let rows: Vec<[(String, Style); 5]> = report
        .results
        .iter()
        .map(|record| {
            let invalid = record.error.is_some();
            let asn = match &record.origins {
                Some(origins) => {
                    let (text, multiple) = display_origins(origins);
                    (
                        text,
                        if multiple {
                            theme.multiple_origins
                        } else {
                            theme.asn
                        },
                    )
                }
                None => ("-".to_owned(), theme.missing),
            };
            [
                (record.ip.clone(), if invalid { theme.error } else { theme.ip }),
                present(record.prefix.as_deref(), theme.prefix),
                asn,
                present(record.snapshot.as_deref(), theme.snapshot),
                (record.error.clone().unwrap_or_default(), theme.error),
            ]
        })
        .collect();
    let mut widths = HEADER.map(str::len);
    for row in &rows {
        for (width, (cell, _)) in widths.iter_mut().zip(row) {
            *width = (*width).max(cell.chars().count());
        }
    }
    let header = HEADER.map(|name| (name.to_owned(), theme.header));
    for row in std::iter::once(&header).chain(&rows) {
        let mut line = String::new();
        for (index, ((cell, style), width)) in row.iter().zip(widths).enumerate() {
            if index > 0 {
                line.push_str("  ");
            }
            let padding = width - cell.chars().count();
            if index == ASN {
                line.extend(std::iter::repeat_n(' ', padding));
            }
            if !cell.is_empty() {
                write!(line, "{style}{cell}{style:#}")?;
            }
            if index != ASN && index + 1 < row.len() {
                line.extend(std::iter::repeat_n(' ', padding));
            }
        }
        writeln!(writer, "{}", line.trim_end())?;
    }
    Ok(())
}

fn write_delimited(delimiter: u8, report: &Report, writer: &mut dyn Write) -> anyhow::Result<()> {
    let mut csv = csv::WriterBuilder::new().delimiter(delimiter).from_writer(writer);
    for record in &report.results {
        csv.serialize(record)?;
    }
    csv.flush()?;
    Ok(())
}

#[cfg(feature = "parquet")]
mod parquet_output {
    use std::io::Write;
    use std::sync::Arc;

    use arrow::array::{ArrayRef, RecordBatch, StringArray, UInt32Array};
    use arrow::datatypes::{DataType, Field, Schema};
    use parquet::arrow::ArrowWriter;
    use parquet::basic::Compression;
    use parquet::file::properties::WriterProperties;

    use super::Report;

    pub fn write(report: &Report, writer: &mut dyn Write) -> anyhow::Result<()> {
        let schema = Arc::new(Schema::new(vec![
            Field::new("ip", DataType::Utf8, false),
            Field::new("prefix", DataType::Utf8, true),
            Field::new("asn", DataType::UInt32, true),
            Field::new("origins", DataType::Utf8, true),
            Field::new("snapshot", DataType::Utf8, true),
            Field::new("error", DataType::Utf8, true),
        ]));
        let results = &report.results;
        let columns: Vec<ArrayRef> = vec![
            Arc::new(StringArray::from(
                results.iter().map(|r| r.ip.as_str()).collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(
                results.iter().map(|r| r.prefix.as_deref()).collect::<Vec<_>>(),
            )),
            Arc::new(UInt32Array::from(
                results.iter().map(|r| r.asn).collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(
                results.iter().map(|r| r.origins.as_deref()).collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(
                results.iter().map(|r| r.snapshot.as_deref()).collect::<Vec<_>>(),
            )),
            Arc::new(StringArray::from(
                results.iter().map(|r| r.error.as_deref()).collect::<Vec<_>>(),
            )),
        ];
        let batch = RecordBatch::try_new(schema.clone(), columns)?;
        let properties = WriterProperties::builder()
            .set_compression(Compression::SNAPPY)
            .set_created_by(format!("originet {}", crate::VERSION))
            .build();
        let mut buffer = Vec::new();
        let mut parquet = ArrowWriter::try_new(&mut buffer, schema, Some(properties))?;
        parquet.write(&batch)?;
        parquet.close()?;
        writer.write_all(&buffer)?;
        Ok(())
    }
}

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

    fn report() -> Report {
        Report {
            tool: Tool {
                name: "originet",
                version: "0.0.0",
            },
            generated_at: "2026-10-22T00:00:00Z".into(),
            snapshots: vec![],
            summary: Summary::of(&records()),
            results: records(),
        }
    }

    fn records() -> Vec<Record> {
        vec![
            Record {
                ip: "8.8.8.8".into(),
                prefix: Some("8.8.8.0/24".into()),
                asn: Some(15169),
                origins: Some("15169".into()),
                snapshot: Some("2026-10-21".into()),
                error: None,
            },
            Record {
                ip: "10.0.0.1".into(),
                prefix: None,
                asn: None,
                origins: None,
                snapshot: Some("2026-10-21".into()),
                error: None,
            },
            Record {
                ip: "bogus".into(),
                prefix: None,
                asn: None,
                origins: None,
                snapshot: None,
                error: Some("invalid IP address syntax".into()),
            },
        ]
    }

    #[test]
    fn summary_counts() {
        let summary = Summary::of(&records());
        assert_eq!(
            summary,
            Summary {
                total: 3,
                found: 1,
                not_found: 1,
                invalid: 1
            }
        );
    }

    #[test]
    fn csv_and_tsv_have_a_header_and_empty_nulls() {
        let mut out = Vec::new();
        write(Format::Csv, &report(), &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();
        let lines: Vec<_> = text.lines().collect();
        assert_eq!(lines[0], "ip,prefix,asn,origins,snapshot,error");
        assert_eq!(lines[1], "8.8.8.8,8.8.8.0/24,15169,15169,2026-10-21,");
        assert_eq!(lines[2], "10.0.0.1,,,,2026-10-21,");
        assert_eq!(lines[3], "bogus,,,,,invalid IP address syntax");

        let mut out = Vec::new();
        write(Format::Tsv, &report(), &mut out).unwrap();
        assert!(
            String::from_utf8(out)
                .unwrap()
                .starts_with("ip\tprefix\tasn\torigins\tsnapshot\terror\n")
        );
    }

    #[test]
    fn table_is_aligned_with_dashes_for_nulls() {
        let mut out = Vec::new();
        write(Format::Table, &report(), &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();
        assert_eq!(
            text,
            "\
ip        prefix        asn  snapshot    error
8.8.8.8   8.8.8.0/24  15169  2026-10-21
10.0.0.1  -               -  2026-10-21
bogus     -               -  -           invalid IP address syntax
"
        );
        assert!(!Format::Table.is_binary());

        let mut colored = Vec::new();
        write_table(&report(), &mut colored, &Theme::colored()).unwrap();
        let colored = String::from_utf8(colored).unwrap();
        assert!(colored.starts_with("\x1b[1mip\x1b[0m"), "{colored:?}");
        assert!(colored.contains("\x1b[1m\x1b[32m15169\x1b[0m"), "{colored:?}");
        assert!(colored.contains("\x1b[31mbogus\x1b[0m"), "{colored:?}");
        assert_eq!(anstream::adapter::strip_str(&colored).to_string(), text);
    }

    #[test]
    fn table_lists_every_origin() {
        assert_eq!(display_origins("15169"), ("15169".into(), false));
        assert_eq!(display_origins("149511_138421"), ("149511, 138421".into(), true));
        assert_eq!(display_origins("8035,13979"), ("{8035, 13979}".into(), true));
        assert_eq!(
            display_origins("1_2,3"),
            ("1, {2, 3}".into(), true),
            "an AS set inside a MOAS keeps its braces"
        );
        let mut report = report();
        report.results[0].origins = Some("149511_138421".into());
        let mut out = Vec::new();
        write_table(&report, &mut out, &Theme::colored()).unwrap();
        let text = String::from_utf8(out).unwrap();
        assert!(text.contains("\x1b[33m149511, 138421\x1b[0m"), "{text:?}");
    }

    #[test]
    fn json_and_jsonl() {
        let mut out = Vec::new();
        write(Format::Json, &report(), &mut out).unwrap();
        let value: serde_json::Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(value["summary"]["found"], 1);
        assert_eq!(value["results"][0]["asn"], 15169);
        assert!(value["results"][1]["asn"].is_null());

        let mut out = Vec::new();
        write(Format::Jsonl, &report(), &mut out).unwrap();
        let text = String::from_utf8(out).unwrap();
        assert_eq!(text.lines().count(), 3);
        let first: serde_json::Value = serde_json::from_str(text.lines().next().unwrap()).unwrap();
        assert_eq!(first["prefix"], "8.8.8.0/24");
    }

    #[cfg(feature = "parquet")]
    #[test]
    fn parquet_round_trips() {
        use arrow::array::RecordBatch;
        use parquet::arrow::arrow_reader::ParquetRecordBatchReaderBuilder;
        let mut out = Vec::new();
        write(Format::Parquet, &report(), &mut out).unwrap();
        let reader = ParquetRecordBatchReaderBuilder::try_new(bytes::Bytes::from(out))
            .unwrap()
            .build()
            .unwrap();
        let batches: Vec<_> = reader.map(Result::unwrap).collect();
        assert_eq!(batches.iter().map(RecordBatch::num_rows).sum::<usize>(), 3);
        assert_eq!(batches[0].schema().fields().len(), 6);
    }
}