1 unstable release

0.1.0 Nov 26, 2024

#2104 in Network programming

Download history 155/week @ 2024-11-26

155 downloads per month

COIL-1.0

28KB
494 lines

Docs unsafe forbidden

An unprivileged async ICMP library. See the docs for how to use it.

Other related projects

These projects make different tradeoffs and API decisions, and while they didn't meet my needs, they may be of interest.


lib.rs:

An unprivileged async ICMP socket library.

This uses PROT_ICMP sockets, so root is not needed, though on Linux access to that socket type is limited by a sysctl (see Linux link below). Some distros set the sysctl to allow all users access out of the box, while others will need configuration.

See also:

Getting started

Examples

Sending a ping:

use std::net;
use async_icmp::{IpVersion, message::IcmpEchoRequest, socket::IcmpSocket};

async fn send_ping() {
    let socket = IcmpSocket::new(IpVersion::V4).unwrap();
    let mut echo = IcmpEchoRequest::new(100, 200, b"data");
    socket.send_to(net::Ipv4Addr::from([127, 0, 0, 1]).into(), &mut echo).await.unwrap();
}

Receving an ICMP message:

use async_icmp::{IpVersion,
    message::{decode::DecodedIcmpMsg, IcmpV4MsgType},
    socket::IcmpSocket};
use std::error;

async fn receive_and_decode() -> Result<(), Box<dyn error::Error>> {
    let s = IcmpSocket::new(IpVersion::V4)?;
    let mut buf = vec![0; 10_000];
    let (msg, _range) = s.recv(&mut buf).await?;
    let decoded = DecodedIcmpMsg::decode(msg)?;
    if decoded.msg_type() == IcmpV4MsgType::SourceQuench as u8 {
        // decode body as source quench
    }
    Ok(())
}

Dependencies

~4–12MB
~129K SLoC