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>
impl<V: IcmpVersion> IcmpSocket<V>
Sourcepub fn new(config: SocketConfig<V>) -> Result<Self>
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
).
Sourcepub async fn recv<'a>(
&self,
buf: &'a mut [u8],
) -> Result<(&'a [u8], Range<usize>)>
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.
Sourcepub async fn send_to(
&self,
msg: &mut impl EncodeIcmpMessage<V>,
addr: V::Address,
) -> Result<()>
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.
Sourcepub fn local_port(&self) -> u16
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.
Sourcepub fn platform_echo_id(&self) -> Option<EchoId>
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)
}