originet 1.0.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
//! `originet`: map IP addresses to origin ASNs with CAIDA's RouteViews
//! prefix-to-AS snapshots.

use std::fs::File;
use std::io::{self, BufReader, BufWriter, IsTerminal, Write};
use std::net::IpAddr;
use std::path::PathBuf;
use std::process::ExitCode;
use std::time::Instant;

use anstream::AutoStream;
use anstream::stream::RawStream;
use anstyle::{AnsiColor, Style};
use anyhow::{Context, bail};
use clap::{ArgAction, Parser};
use jiff::civil::Date;

use originet::caida::Family;
use originet::fetch::Fetcher;
use originet::input::{self, Item};
use originet::load;
use originet::output::{self, Format, Record, Report, SnapshotInfo, Summary, Theme, Tool};
use originet::table::Table;

mod progress;

/// Map IP addresses to origin ASNs with CAIDA's RouteViews prefix-to-AS snapshots.
///
/// Addresses come from the command line, from a file (`--file`, one per line) or from
/// standard input (`--file -`). The matching snapshot is downloaded from CAIDA on first
/// use and cached; `--date` picks the newest snapshot on or before a day, and
/// `--pfx2as`/`--pfx2as6` use local files instead of downloading. `--fetch` only fills
/// the cache.
#[derive(Debug, Parser)]
#[command(name = "originet", version, about, long_about, after_help = EXAMPLES)]
struct Args {
    /// IP addresses to look up (IPv4 or IPv6).
    #[arg(value_name = "IP")]
    addresses: Vec<String>,

    /// Read addresses from a file, one per line (`-` for standard input).
    #[arg(short, long, value_name = "PATH")]
    file: Option<PathBuf>,

    /// Use the newest snapshot taken on or before this day (YYYY-MM-DD). Default: the newest.
    #[arg(short, long, value_name = "YYYY-MM-DD", value_parser = parse_date)]
    date: Option<Date>,

    /// Local IPv4 pfx2as file (plain or gzip) instead of downloading from CAIDA.
    #[arg(long, value_name = "PATH")]
    pfx2as: Option<PathBuf>,

    /// Local IPv6 pfx2as file (plain or gzip) instead of downloading from CAIDA.
    #[arg(long, value_name = "PATH")]
    pfx2as6: Option<PathBuf>,

    /// Output format. Default: `table` on a terminal, `json` when piped or with `--output`.
    #[arg(short = 'F', long, value_enum, value_name = "FORMAT")]
    format: Option<Format>,

    /// Write the output to this file instead of standard output.
    #[arg(short, long, value_name = "PATH")]
    output: Option<PathBuf>,

    /// Color the table, the summary and the progress bar: `auto` only on a terminal
    /// (and not with `NO_COLOR`), `always` even into a pipe or file, `never` for plain text.
    #[arg(long, value_enum, default_value_t = clap::ColorChoice::Auto, value_name = "WHEN")]
    color: clap::ColorChoice,

    /// Directory for downloaded indexes and snapshots.
    #[arg(long, value_name = "DIR", env = "ORIGINET_CACHE_DIR")]
    cache_dir: Option<PathBuf>,

    /// Never download; fail unless the index and snapshot are already cached.
    #[arg(long)]
    offline: bool,

    /// Re-download the snapshot index even if the cached copy is fresh.
    #[arg(long, conflicts_with = "offline")]
    refresh: bool,

    /// Download the IPv4 and IPv6 snapshots for the day (`--date`, default the newest)
    /// into the cache and exit without looking anything up; print one line per snapshot
    /// with its date, whether it was already cached, and its path. With `--offline` it
    /// only checks that they are cached.
    #[arg(
        long,
        conflicts_with_all = ["addresses", "file", "pfx2as", "pfx2as6", "format", "output", "strict"]
    )]
    fetch: bool,

    /// Exit with status 1 when any input is not an IP address.
    #[arg(long)]
    strict: bool,

    /// Do not print the summary to standard error.
    #[arg(short, long)]
    quiet: bool,

    /// More detail on standard error (`-v` what is fetched, `-vv` debugging).
    #[arg(short, long, action = ArgAction::Count)]
    verbose: u8,
}

const EXAMPLES: &str = "\
Examples:
  originet 8.8.8.8 2001:4860::8888
  originet --format json 8.8.8.8 2001:4860::8888
  originet --file ips.txt --format csv --output results.csv
  originet --date 2019-06-01 --file ips.txt --format parquet -o june-2019.parquet
  originet --pfx2as routeviews-rv2-20261021-0800.pfx2as.gz --file ips.txt
  cat ips.txt | originet -f - -F jsonl
  originet --fetch --date 2019-06-01";

fn parse_date(text: &str) -> Result<Date, String> {
    text.parse::<Date>()
        .map_err(|error| format!("expected YYYY-MM-DD: {error}"))
}

fn main() -> ExitCode {
    let args = Args::parse();
    init_logging(args.verbose);
    match run(&args) {
        Ok(code) => code,
        Err(error) => {
            let mut stderr = AutoStream::new(io::stderr(), color_choice(&args, &io::stderr()));
            let style = AnsiColor::Red.on_default().bold();
            // Nothing sensible is left to do if standard error is gone.
            let _ = writeln!(stderr, "{style}error:{style:#} {}", render(&error));
            ExitCode::FAILURE
        }
    }
}

/// The error and its causes, oldest last, without repeating a cause whose text the
/// previous message already carries (the crate's errors embed their source's message).
fn render(error: &anyhow::Error) -> String {
    let mut text = error.to_string();
    for cause in error.chain().skip(1) {
        let cause = cause.to_string();
        if !text.contains(&cause) {
            text.push_str(": ");
            text.push_str(&cause);
        }
    }
    text
}

/// Whether what goes to `stream` is colored: `--color`, and for `auto` the stream (a
/// terminal or not, `NO_COLOR`, `TERM=dumb`, `CLICOLOR_FORCE`).
fn color_choice<S: RawStream>(args: &Args, stream: &S) -> anstream::ColorChoice {
    match args.color {
        clap::ColorChoice::Always => anstream::ColorChoice::Always,
        clap::ColorChoice::Never => anstream::ColorChoice::Never,
        clap::ColorChoice::Auto => AutoStream::choice(stream),
    }
}

/// `style` when `count` is not zero, so a zero stays unremarkable.
fn count_style(count: usize, style: Style) -> Style {
    if count > 0 { style } else { Style::new() }
}

fn init_logging(verbosity: u8) {
    let level = match verbosity {
        0 => log::LevelFilter::Warn,
        1 => log::LevelFilter::Info,
        _ => log::LevelFilter::Debug,
    };
    env_logger::Builder::from_env(env_logger::Env::default().default_filter_or(level.as_str()))
        .format_timestamp(None)
        .format_target(false)
        .init();
}

fn run(args: &Args) -> anyhow::Result<ExitCode> {
    let started = Instant::now();
    let mut fetcher = Fetcher::new(cache_dir(args.cache_dir.clone())?)
        .offline(args.offline)
        .refresh_index(args.refresh);
    let stderr_colors = color_choice(args, &io::stderr());
    if !args.quiet && io::stderr().is_terminal() {
        fetcher = fetcher.progress(progress::Bars::new(stderr_colors != anstream::ColorChoice::Never));
    }
    if args.fetch {
        return fetch_snapshots(args, &fetcher, started);
    }

    let items = collect_items(args)?;
    if items.is_empty() {
        bail!("no addresses given; pass them as arguments or with --file (see --help)");
    }
    let needs = Needs::of(&items);

    let mut table = Table::new();
    let mut snapshots = Vec::new();
    for (family, local) in [(Family::V4, &args.pfx2as), (Family::V6, &args.pfx2as6)] {
        if !needs.has(family) {
            continue;
        }
        let info = match local {
            Some(path) => {
                if let Some(date) = args.date {
                    log::warn!(
                        "--date {date} does not apply to {family}: it is read from {}",
                        path.display()
                    );
                }
                load::load_local(&mut table, family, path)?
            }
            None => load::load_caida(&mut table, &fetcher, family, args.date)?,
        };
        log::info!("{}: {} prefixes from {}", family, info.prefixes, info.file);
        snapshots.push(info);
    }

    let records = lookup_all(&table, &items, &snapshots);
    let summary = Summary::of(&records);
    let report = Report {
        tool: Tool {
            name: "originet",
            version: originet::VERSION,
        },
        generated_at: jiff::Timestamp::now().round(jiff::Unit::Second)?.to_string(),
        snapshots,
        summary,
        results: records,
    };

    write_report(args, &report)?;

    if !args.quiet {
        let used: Vec<String> = report
            .snapshots
            .iter()
            .map(|s| format!("{} {}", s.family, s.date.as_deref().unwrap_or(&s.file)))
            .collect();
        let found = count_style(summary.found, AnsiColor::Green.on_default().bold());
        let not_found = count_style(summary.not_found, AnsiColor::Yellow.on_default());
        let invalid = count_style(summary.invalid, AnsiColor::Red.on_default());
        let mut stderr = AutoStream::new(io::stderr(), stderr_colors);
        writeln!(
            stderr,
            "{} addresses: {found}{}{found:#} found, {not_found}{}{not_found:#} not found, {invalid}{}{invalid:#} invalid ({}; {:.1?})",
            summary.total,
            summary.found,
            summary.not_found,
            summary.invalid,
            if used.is_empty() {
                "no snapshot".to_owned()
            } else {
                used.join(", ")
            },
            started.elapsed()
        )?;
    }
    if args.strict && summary.invalid > 0 {
        bail!("{} input(s) were not IP addresses (--strict)", summary.invalid);
    }
    Ok(ExitCode::SUCCESS)
}

/// `--fetch`: put both families' snapshots for the day in the cache and report them.
fn fetch_snapshots(args: &Args, fetcher: &Fetcher, started: Instant) -> anyhow::Result<ExitCode> {
    let mut stdout = AutoStream::new(io::stdout(), color_choice(args, &io::stdout()));
    let mut used = Vec::new();
    for family in [Family::V4, Family::V6] {
        let ready = load::fetch_snapshot(fetcher, family, args.date)?;
        let (status, style) = if ready.cached {
            ("cached", Style::new().dimmed())
        } else {
            ("downloaded", AnsiColor::Green.on_default())
        };
        writeln!(
            stdout,
            "{family}  {}  {style}{status:<10}{style:#}  {}",
            ready.snapshot.date,
            ready.path.display()
        )?;
        used.push(format!("{family} {} {status}", ready.snapshot.date));
    }
    stdout.flush()?;
    if !args.quiet {
        eprintln!(
            "{} snapshots ready in {} ({}; {:.1?})",
            used.len(),
            fetcher.cache_dir().display(),
            used.join(", "),
            started.elapsed()
        );
    }
    Ok(ExitCode::SUCCESS)
}

fn collect_items(args: &Args) -> anyhow::Result<Vec<Item>> {
    let mut items: Vec<Item> = args.addresses.iter().map(|text| Item::parse(text)).collect();
    if let Some(path) = &args.file {
        let from_file = if path.as_os_str() == "-" {
            input::read_items(io::stdin().lock()).context("reading standard input")?
        } else {
            let file = File::open(path).with_context(|| format!("opening {}", path.display()))?;
            input::read_items(BufReader::new(file)).with_context(|| format!("reading {}", path.display()))?
        };
        items.extend(from_file);
    }
    Ok(items)
}

/// Which address families the input needs.
#[derive(Debug, Clone, Copy, Default)]
struct Needs {
    v4: bool,
    v6: bool,
}

impl Needs {
    fn of(items: &[Item]) -> Self {
        let mut needs = Self::default();
        for address in items.iter().filter_map(Item::address) {
            match address {
                IpAddr::V4(_) => needs.v4 = true,
                IpAddr::V6(_) => needs.v6 = true,
            }
        }
        needs
    }

    fn has(self, family: Family) -> bool {
        match family {
            Family::V4 => self.v4,
            Family::V6 => self.v6,
        }
    }
}

fn cache_dir(explicit: Option<PathBuf>) -> anyhow::Result<PathBuf> {
    if let Some(dir) = explicit {
        return Ok(dir);
    }
    directories::ProjectDirs::from("", "", "originet")
        .map(|dirs| dirs.cache_dir().to_path_buf())
        .context("no cache directory could be determined; pass --cache-dir")
}

fn lookup_all(table: &Table, items: &[Item], snapshots: &[SnapshotInfo]) -> Vec<Record> {
    let snapshot_date = |family: Family| {
        snapshots
            .iter()
            .find(|s| s.family == family)
            .and_then(|s| s.date.clone())
    };
    let dates = (snapshot_date(Family::V4), snapshot_date(Family::V6));
    items
        .iter()
        .map(|item| match item {
            Item::Invalid { text, reason } => Record {
                ip: text.clone(),
                prefix: None,
                asn: None,
                origins: None,
                snapshot: None,
                error: Some(reason.clone()),
            },
            Item::Address(address) => {
                let hit = table.lookup(*address);
                Record {
                    ip: address.to_string(),
                    prefix: hit.as_ref().map(|m| m.prefix.to_string()),
                    asn: hit.as_ref().map(|m| m.origins.primary()),
                    origins: hit.as_ref().map(|m| m.origins.raw().to_owned()),
                    snapshot: match address {
                        IpAddr::V4(_) => dates.0.clone(),
                        IpAddr::V6(_) => dates.1.clone(),
                    },
                    error: None,
                }
            }
        })
        .collect()
}

/// The format asked for, else `table` for a person at a terminal and `json` for a file
/// or a pipe.
fn format(args: &Args) -> Format {
    args.format.unwrap_or_else(|| {
        if args.output.is_none() && io::stdout().is_terminal() {
            Format::Table
        } else {
            Format::Json
        }
    })
}

fn write_report(args: &Args, report: &Report) -> anyhow::Result<()> {
    let format = format(args);
    if let Some(path) = &args.output {
        let file = File::create(path).with_context(|| format!("creating {}", path.display()))?;
        let colors = color_choice(args, &file);
        return write_to(format, report, file, colors).with_context(|| format!("writing {}", path.display()));
    }
    if format.is_binary() && io::stdout().is_terminal() {
        bail!("refusing to write {format:?} output to a terminal; use --output or redirect stdout");
    }
    let stdout = io::stdout();
    let colors = color_choice(args, &stdout);
    write_to(format, report, stdout.lock(), colors).context("writing standard output")
}

/// Writes the report to `raw`; only the table is ever colored, and then through a
/// stream that strips or translates the escapes as the destination needs.
fn write_to<S: RawStream + anstream::stream::AsLockedWrite>(
    format: Format,
    report: &Report,
    raw: S,
    colors: anstream::ColorChoice,
) -> anyhow::Result<()> {
    if format == Format::Table && colors != anstream::ColorChoice::Never {
        let mut writer = BufWriter::new(AutoStream::new(raw, colors));
        output::write_table(report, &mut writer, &Theme::colored())?;
        writer.flush()?;
    } else {
        let mut writer = BufWriter::new(raw);
        output::write(format, report, &mut writer)?;
        writer.flush()?;
    }
    Ok(())
}