Coder Social home page Coder Social logo

wladwm / surge-ping Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kolapapa/surge-ping

0.0 0.0 0.0 155 KB

Asynchronous implementation of ping(only ipv4) in rust, based on tokio 1.x

Home Page: https://docs.rs/surge-ping/

License: MIT License

Rust 100.00%

surge-ping's Introduction

surge-ping

Crates.io MIT licensed API docs

rust ping libray based on tokio + socket2 + pnet_packet.

Care

  • IPv6 is not fully implemented. If you have a need for IPv6, you can submit a PR and build together.

Example

use std::time::Duration;

use surge_ping::Pinger;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut pinger = Pinger::new("114.114.114.114".parse()?)?;
    pinger.timeout(Duration::from_secs(1));
    for seq_cnt in 0..10 {
        let (reply, dur) = pinger.ping(seq_cnt).await?;
        println!(
            "{} bytes from {}: icmp_seq={} ttl={:?} time={:?}",
            reply.size, reply.source, reply.sequence, reply.ttl, dur
        );
    }
    Ok(())
}

You can send ICMP packets with custom interface or set_ttl

pinger.bind_device(Some("eth0".as_bytes()))?;

# You can rely on ttl to implement the icmp version of the traceroute program.

pinger.set_ttl(20)?;

Ping(ICMP)

There are two example programs that you can run on your own.

$ git clone https://github.com/kolapapa/surge-ping.git
$ cd surge-ping


$ cargo build --example simple
sudo RUST_LOG=info ./target/debug/examples/simple -h www.baidu.com -s 56
INFO  simple > Ok((Icmpv4(Icmpv4Packet { source: 110.242.68.4, destination: 10.1.33.227, ttl: 45, icmp_type: IcmpType(0), icmp_code: IcmpCode(0), size: 64, identifier: 111, sequence: 0 }), 14.687909ms))

$ cargo build --example cmd
sudo ./target/debug/examples/cmd -h www.baidu.com -c 5
PING www.baidu.com (110.242.68.4): 56 data bytes
64 bytes from 110.242.68.4: icmp_seq=0 ttl=45 time=12.721 ms
64 bytes from 110.242.68.4: icmp_seq=1 ttl=45 time=15.458 ms
64 bytes from 110.242.68.4: icmp_seq=2 ttl=45 time=21.048 ms
64 bytes from 110.242.68.4: icmp_seq=3 ttl=45 time=18.368 ms
64 bytes from 110.242.68.4: icmp_seq=4 ttl=45 time=19.718 ms

--- www.baidu.com ping statistics ---
5 packets transmitted, 5 packets received, 0.00% packet loss
round-trip min/avg/max/stddev = 12.721/17.463/21.048/3.009 ms

Notice

If you are time sensitive, please do not use asynchronous ping program, because if there are a large number of asynchronous events waiting to wake up, it will cause inaccurate calculation time. You can directly use the ping command of the operating system.

License

This project is licensed under the MIT license.

surge-ping's People

Contributors

kolapapa avatar wladwm avatar tefiledo avatar nikclayton avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.