#ping #icmp #echo #linux #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

#11 in #fragment

Download history 543/week @ 2024-07-15 284/week @ 2024-07-22 349/week @ 2024-07-29 314/week @ 2024-08-05 430/week @ 2024-08-12 770/week @ 2024-08-19 682/week @ 2024-08-26 634/week @ 2024-09-02 844/week @ 2024-09-09 485/week @ 2024-09-16 604/week @ 2024-09-23 431/week @ 2024-09-30 362/week @ 2024-10-07 472/week @ 2024-10-14 786/week @ 2024-10-21 627/week @ 2024-10-28

2,294 downloads per month
Used in 2 crates (via bones_framework)

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–42MB
~599K SLoC