Skip to main content

trace/
trace.rs

1//! `cargo run -p netcore-probe --example trace -- 1.1.1.1`
2use std::net::IpAddr;
3use std::time::Duration;
4
5use netcore::diag::{PingOpts, TraceOpts};
6use netcore::link::L4Proto;
7use netcore::traits::Reachability;
8use netcore_probe::ProbeBackend;
9
10fn main() {
11    let target = std::env::args().nth(1).unwrap_or_else(|| "1.1.1.1".into());
12    let ip: IpAddr = target.parse().expect("pass an IP literal");
13    let b = ProbeBackend::new();
14    println!("caps = {:?}", b.capabilities());
15
16    println!("--- ping {ip} ---");
17    let r = b
18        .ping(
19            ip,
20            PingOpts {
21                count: 3,
22                timeout: Duration::from_millis(1000),
23            },
24        )
25        .expect("ping");
26    println!(
27        "sent={} recv={} min={:?} avg={:?} max={:?}",
28        r.sent, r.received, r.rtt_min, r.rtt_avg, r.rtt_max
29    );
30
31    println!("--- trace {ip} ---");
32    let hops = b
33        .trace(
34            ip,
35            TraceOpts {
36                max_hops: 15,
37                timeout_per_hop: Duration::from_millis(1000),
38                proto: L4Proto::Tcp,
39            },
40        )
41        .expect("trace");
42    for h in &hops {
43        println!(
44            "  {:2} {:>20} {:>10}",
45            h.ttl,
46            h.ip.map(|i| i.to_string()).unwrap_or_else(|| "*".into()),
47            h.rtt
48                .map(|d| format!("{:.1}ms", d.as_secs_f64() * 1000.0))
49                .unwrap_or_else(|| "-".into())
50        );
51    }
52}