Skip to content

Commit

Permalink
use ping cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Aug 27, 2024
1 parent f144621 commit fd14090
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions test/connection-checker/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use eyre::{eyre, Context};
use std::{
io::Write,
net::{IpAddr, Ipv4Addr, SocketAddr},
process::Command,
time::Duration,
};

Expand Down Expand Up @@ -61,16 +62,33 @@ pub fn send_udp(opt: &Opt, destination: SocketAddr) -> Result<(), eyre::Error> {
}

pub fn send_ping(opt: &Opt, destination: IpAddr) -> eyre::Result<()> {
eprintln!("Leaking IMCP packets to {destination}");

ping::dgramsock::ping(
destination,
Some(Duration::from_millis(opt.leak_timeout)),
None,
None,
None,
None,
)?;
eprintln!("Leaking ICMP packets to {destination}");

let mut cmd = Command::new("ping");

#[cfg(target_os = "windows")]
cmd.args(["/n", "1"])
.args(["/w", &opt.leak_timeout.to_string()]);

#[cfg(target_os = "linux")]
{
let timeout_sec = ((opt.leak_timeout as f32) / 1000f32).to_string();
cmd.args(["-c", "1"]).args(["-W", &timeout_sec]);
}

#[cfg(target_os = "macos")]
cmd.args(["-c", "1"])
.args(["-W", &opt.leak_timeout.to_string()]);

cmd.arg(&destination.to_string());

let output = cmd.output().wrap_err(eyre!(
"Failed to execute ping for destination {destination}"
))?;

if !output.status.success() {
return Err(eyre!("ping for destination {destination} failed"));
}

Ok(())
}

0 comments on commit fd14090

Please sign in to comment.