Struct IcmpSocket

Source
pub struct IcmpSocket<V> { /* private fields */ }
Expand description

An ICMP socket.

Commonly this would be wrapped in an Arc so that it may be used from multiple tasks (e.g. one for sending, one for receiving).

§Platform differences

On Linux, ICMP Echo Request messages are rewritten to use the local port as the id, and only ICMP Echo Reply messages where the id = the local port will be returned from recv(). IcmpSocket::local_port and IcmpSocket::platform_echo_id exist for such use cases. See platform::icmp_send_overwrite_echo_id_with_local_port.

On macOS, the kernel is less restrictive, so you can set whatever id you like. In addition, other ICMP packets will also be returned from recv(), so additional filtering may be needed depending on the use case.

Implementations§

Source§

impl<V: IcmpVersion> IcmpSocket<V>

Source

pub fn new(config: SocketConfig<V>) -> Result<Self>

Create a new socket for IP version V.

When a socket is created, it’s either IPv4 (socket type AF_INET) or IPv6 (AF_INET6), which governs which type of IP address is valid to use with IcmpSocket::send_to (net::Ipv4Addr or net::Ipv6Addr).

Source

pub async fn recv<'a>( &self, buf: &'a mut [u8], ) -> Result<(&'a [u8], Range<usize>)>

Write the contents of a received ICMP message into buf, returning a tuple containing the ICMP message and the range of indices in buf holding the message, in case mutable access to the slice is desired.

Bytes outside the returned range may have been written to, and skipped during subsequent parsing.

See crate::message::decode::DecodedIcmpMsg to extract basic ICMP message structure.

Source

pub async fn send_to( &self, msg: &mut impl EncodeIcmpMessage<V>, addr: V::Address, ) -> Result<()>

Send msg to addr.

If msg doesn’t support the socket’s IP version, an error will be returned.

Source

pub fn local_port(&self) -> u16

Returns the local port of the socket.

This is useful on Linux since the local port is used as the ICMP Echo ID regardless of what is set in userspace.

On macOS, the local port is always zero, but ICMP Echo ids are not tied to the local port, so it’s not an issue in practice.

See platform::icmp_send_overwrite_echo_id_with_local_port.

Source

pub fn platform_echo_id(&self) -> Option<EchoId>

Returns the local port of the socket as the id to be used in an ICMP Echo Request message, if the current platform is one that forces the id to match the local port.

See platform::icmp_send_overwrite_echo_id_with_local_port.

§Examples

Use the platform echo id, otherwise a random id.

use async_icmp::{IcmpVersion, message::echo::EchoId, socket::IcmpSocket};
use std::io;

fn echo_id<V: IcmpVersion>(socket: &IcmpSocket<V>) -> EchoId {
    socket.platform_echo_id().unwrap_or_else(rand::random)
}

Trait Implementations§

Source§

impl<V: Debug> Debug for IcmpSocket<V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V> Freeze for IcmpSocket<V>

§

impl<V> !RefUnwindSafe for IcmpSocket<V>

§

impl<V> Send for IcmpSocket<V>
where V: Send,

§

impl<V> Sync for IcmpSocket<V>
where V: Sync,

§

impl<V> Unpin for IcmpSocket<V>
where V: Unpin,

§

impl<V> !UnwindSafe for IcmpSocket<V>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V