Crate ping_rs

source ·
Expand description

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)
    }
}

Modules

Contains constant values represent general errors.

Structs

Ping reply contains the destination address (from ICMP reply) and Round-Trip Time

Enums

Ping errors

Functions

Send ICMP Echo package (ping) to the given address.
Asynchronously schedule ICMP Echo package (ping) to the given address. Note that some parameter signatures are different from send_ping function, as the caller should manage those parameters’ lifetime.

Type Definitions