#ping #icmp #linux #echo #fragment #sync #async

bin+lib ping-rs

Provide ICMP Echo (ping) functionality for both Windows and Linux

3 releases

0.1.2 Jan 7, 2023
0.1.1 Jan 6, 2023
0.1.0 Jan 6, 2023

#14 in #fragment

Download history 115/week @ 2023-12-17 114/week @ 2023-12-24 104/week @ 2023-12-31 231/week @ 2024-01-07 257/week @ 2024-01-14 215/week @ 2024-01-21 161/week @ 2024-01-28 112/week @ 2024-02-04 128/week @ 2024-02-11 216/week @ 2024-02-18 229/week @ 2024-02-25 222/week @ 2024-03-03 171/week @ 2024-03-10 217/week @ 2024-03-17 273/week @ 2024-03-24 345/week @ 2024-03-31

1,046 downloads per month

MIT license

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–49MB
~662K SLoC