write_traceroute/
write_traceroute.rs

1use chrono::Utc;
2use deku::DekuContainerWrite;
3use std::ffi::CString;
4use std::io;
5use std::io::Write;
6use std::net::Ipv4Addr;
7use warts::{
8    Address, CycleStart, CycleStop, Flags, List, Object, Timeval, TraceGapAction, TraceProbe,
9    TraceStopReason, TraceType, Traceroute,
10};
11
12fn main() -> io::Result<()> {
13    let list_name = CString::new("default").unwrap();
14    let hostname = CString::new("ubuntu-linux-20-04-desktop").unwrap();
15
16    let list = List {
17        length: 0,
18        list_id: 1,
19        list_id_human: 0,
20        name: list_name.clone(),
21        flags: Default::default(),
22        param_length: None,
23        description: Some(list_name.clone()),
24        monitor_name: None,
25    };
26    // The `finalize()` method computes and set the flags and length fields.
27    io::stdout().write_all(&Object::List(list.finalize()).to_bytes().unwrap())?;
28
29    let cycle_start = CycleStart {
30        length: 0,
31        cycle_id: 1,
32        list_id: 1,
33        cycle_id_human: 0,
34        start_time: Utc::now().timestamp() as u32,
35        flags: Default::default(),
36        param_length: None,
37        stop_time: None,
38        hostname: Some(hostname),
39    };
40    io::stdout().write_all(
41        &Object::CycleStart(cycle_start.finalize())
42            .to_bytes()
43            .unwrap(),
44    )?;
45
46    let tp = TraceProbe {
47        flags: Default::default(),
48        param_length: None,
49        addr_id: None,
50        probe_ttl: Some(1),
51        reply_ttl: Some(254),
52        hop_flags: Some(17),
53        probe_id: Some(0),
54        rtt_usec: Some(1057),
55        icmp_type: Some(11),
56        icmp_code: Some(0),
57        probe_size: Some(44),
58        reply_size: Some(56),
59        reply_ip_id: Some(387),
60        reply_ip_tos: Some(0),
61        next_hop_mtu: None,
62        quoted_length: None,
63        quoted_ttl: None,
64        reply_tcp_flags: None,
65        quoted_tos: Some(0),
66        icmp_extensions_length: None,
67        icmp_extensions: vec![],
68        addr: Some(Address::from(Ipv4Addr::new(137, 194, 164, 254))),
69        tx: Some(Timeval::from(Utc::now().naive_utc())),
70    };
71
72    let traceroute = Traceroute {
73        length: 0,
74        flags: Flags::default(),
75        param_length: None,
76        list_id: Some(1),
77        cycle_id: Some(1),
78        src_addr_id: None,
79        dst_addr_id: None,
80        start_time: Some(Timeval::from(Utc::now().naive_utc())),
81        stop_reason: Some(TraceStopReason::Completed),
82        stop_data: Some(0),
83        trace_flags: None,
84        attempts: Some(2),
85        hop_limit: Some(0),
86        trace_type: Some(TraceType::UDPParis),
87        probe_size: Some(44),
88        src_port: Some(57352),
89        dst_port: Some(33435),
90        first_ttl: Some(1),
91        ip_tos: Some(0),
92        timeout_sec: Some(5),
93        allowed_loops: Some(1),
94        hops_probed: Some(7),
95        gap_limit: Some(5),
96        gap_limit_action: Some(TraceGapAction::LastDitch),
97        loop_action: Some(0),
98        probes_sent: Some(8),
99        interval_csec: Some(0),
100        confidence_level: Some(0),
101        src_addr: Some(Address::from(Ipv4Addr::new(137, 194, 165, 109))),
102        dst_addr: Some(Address::from(Ipv4Addr::new(8, 8, 8, 8))),
103        user_id: None,
104        ip_offset: None,
105        router_addr: None,
106        hop_count: 1,
107        hops: vec![tp.finalize()],
108        eof: 0,
109    };
110    io::stdout().write_all(
111        Object::Traceroute(traceroute.finalize())
112            .to_bytes()
113            .unwrap()
114            .as_slice(),
115    )?;
116
117    let cycle_stop = CycleStop {
118        length: 0,
119        cycle_id: 1,
120        stop_time: Utc::now().timestamp() as u32,
121        flags: Default::default(),
122    };
123    io::stdout().write_all(&Object::CycleStop(cycle_stop.finalize()).to_bytes().unwrap())?;
124
125    Ok(())
126}