read_traceroute/
read_traceroute.rs

1use std::net::IpAddr;
2use std::{env, fs};
3use warts::Object;
4
5fn main() {
6    let args: Vec<String> = env::args().collect();
7    for path in &args[1..] {
8        let data = fs::read(path).unwrap();
9        let objects = Object::all_from_bytes(&data);
10        for mut object in objects {
11            // Resolve IP addresses references.
12            object.dereference();
13            print(object);
14        }
15    }
16}
17
18fn print(object: Object) {
19    match object {
20        Object::Traceroute(t) => {
21            // NOTE: In practice, you may want to handle the case where the fields
22            // behind flags are not present.
23            let src_addr = IpAddr::from(t.src_addr.unwrap());
24            let dst_addr = IpAddr::from(t.dst_addr.unwrap());
25            println!("Traceroute from {} to {}", src_addr, dst_addr);
26            for hop in t.hops {
27                let addr = IpAddr::from(hop.addr.unwrap());
28                println!("{} {}", hop.probe_ttl.unwrap(), addr);
29            }
30        }
31        _ => {}
32    }
33}