3 releases
0.1.2 | Jan 7, 2023 |
---|---|
0.1.1 | Jan 6, 2023 |
0.1.0 | Jan 6, 2023 |
#11 in #fragment
2,294 downloads per month
Used in 2 crates
(via bones_framework)
37KB
736 lines
ping-rs
ICMP ping library for Rust. Support Windows and Linux.
See usage in /bin/simple_ping.rs
lib.rs
:
Provide ICMP Echo (ping) functionality for both Windows and Linux. This library does not need root/admin privilege for pinging.
It provides sync and async ping functions: send_ping
and send_ping_async
.
Linux version still does not support "Do not Fragment" flag yet.
Usage Example
An example is also provided in /bin/sample_ping.rs
Synchronous ping
use std::time::Duration;
fn main(){
let addr = "8.8.8.8".parse().unwrap();
let data = [1,2,3,4]; // ping data
let timeout = Duration::from_secs(1);
let options = ping_rs::PingOptions { ttl: 128, dont_fragment: true };
let result = ping_rs::send_ping(&addr, timeout, &data, Some(&options));
match result {
Ok(reply) => println!("Reply from {}: bytes={} time={}ms TTL={}", reply.address, data.len(), reply.rtt, options.ttl),
Err(e) => println!("{:?}", e)
}
}
Asynchronous ping
Note that futures
crate is used in this example. Also, data passed in the function has to be wrapped with Arc
because in Windows' implementation
the address of this data will be passed to Win32 API.
use std::sync::Arc;
use std::time::Duration;
fn main(){
let addr = "8.8.8.8".parse().unwrap();
let data = [1,2,3,4]; // ping data
let data_arc = Arc::new(&data[..]);
let timeout = Duration::from_secs(1);
let options = ping_rs::PingOptions { ttl: 128, dont_fragment: true };
let future = ping_rs::send_ping_async(&addr, timeout, data_arc, Some(&options));
let result = futures::executor::block_on(future);
match result {
Ok(reply) => println!("Reply from {}: bytes={} time={}ms TTL={}", reply.address, data.len(), reply.rtt, options.ttl),
Err(e) => println!("{:?}", e)
}
}
Dependencies
~1–42MB
~599K SLoC