From aecb7ac234ddb38772f3a968c3fa43ec3b507315 Mon Sep 17 00:00:00 2001 From: ache Date: Tue, 4 Aug 2026 09:43:58 +0200 Subject: Init commit --- src/pinger.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/pinger.rs (limited to 'src/pinger.rs') diff --git a/src/pinger.rs b/src/pinger.rs new file mode 100644 index 0000000..ef87cbf --- /dev/null +++ b/src/pinger.rs @@ -0,0 +1,35 @@ +use std::net::IpAddr; +use std::time::Duration; +use tokio::sync::mpsc::Sender; +use tokio::task; + +pub enum PingResult { + NoPingErr { id: u32, error: String }, + Ping { rrt: u64, ttl: u32, id: u32 }, +} + +pub fn spawn_ping(target: IpAddr, tx: Sender, id: u32) { + // let _ = tx.try_send(PingResult::NoPingErr { id, error: "Test Ping".to_string() }); + task::spawn_blocking(move || { + let result = do_ping(target, id); + let _ = tx.try_send(result); + }); +} + +fn do_ping(target: IpAddr, id: u32) -> PingResult { + let payload: [u8; 24] = [0u8; 24]; + + let result = ping::new(target) + .timeout(Duration::from_secs(3)) + .payload(&payload) + .send(); + + match result { + Ok(reply) => PingResult::Ping { + rrt: reply.rtt.as_millis() as u64, + ttl: reply.ttl.unwrap_or(0) as u32, + id, + }, + Err(e) => PingResult::NoPingErr { id, error: format!("Ping failed: {}", e) }, + } +} -- cgit v1.3-2-g11bf