pub struct IcmpSocket { /* private fields */ }
Expand description
An Internet Control Message Protocol socket.
This is an implementation of a bound ICMP socket. This supports both IPv4 and IPv6 addresses, and there is no corresponding notion of a server because ICMP is a datagram protocol.
use icmp;
use std::net::{IpAddr, Ipv4Addr};
let localhost_v4 = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
let ping = icmp::IcmpSocket::connect(localhost_v4);
let mut ping = ping.unwrap();
let payload: &[u8] = &[1, 2];
let result = ping.send(payload);
assert_eq!(result.unwrap(), 2);
In case you received an error message, you need to enable the correct capabilites:
cargo build
sudo setcap cap_net_raw+ep ./target/debug/PROJECT_NAME
cargo run
Implementations§
Source§impl IcmpSocket
impl IcmpSocket
Sourcepub fn connect(addr: IpAddr) -> Result<IcmpSocket>
pub fn connect(addr: IpAddr) -> Result<IcmpSocket>
Connect socket to addr
Sourcepub fn recv(&self, buf: &mut [u8]) -> Result<usize>
pub fn recv(&self, buf: &mut [u8]) -> Result<usize>
Receives data from the socket. On success, returns the number of bytes read.
Sourcepub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpAddr)>
pub fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, IpAddr)>
Receives data from the socket. On success, returns the number of bytes read and the address from whence the data came.
Sourcepub fn send(&mut self, buf: &[u8]) -> Result<usize>
pub fn send(&mut self, buf: &[u8]) -> Result<usize>
Sends data on the socket to the remote address to which it is connected.
The connect
method will connect this socket to a remote address. This
method will fail if the socket is not connected.
Sourcepub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, dur: Option<Duration>) -> Result<()>
Sets the read timeout to the timeout specified.
If the value specified is None
, then read
calls will block
indefinitely. It is an error to pass the zero Duration
to this
method.
§Note
Platforms may return a different error code whenever a read times out as
a result of setting this option. For example Unix typically returns an
error of the kind WouldBlock
, but Windows may return TimedOut
.
Sourcepub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, dur: Option<Duration>) -> Result<()>
Sets the write timeout to the timeout specified.
If the value specified is None
, then write
calls will block
indefinitely. It is an error to pass the zero Duration
to this
method.
§Note
Platforms may return a different error code whenever a write times out
as a result of setting this option. For example Unix typically returns
an error of the kind WouldBlock
, but Windows may return TimedOut
.
Sourcepub fn read_timeout(&self) -> Result<Option<Duration>>
pub fn read_timeout(&self) -> Result<Option<Duration>>
Returns the read timeout of this socket.
If the timeout is None
, then read
calls will block indefinitely.
Sourcepub fn write_timeout(&self) -> Result<Option<Duration>>
pub fn write_timeout(&self) -> Result<Option<Duration>>
Returns the write timeout of this socket.
If the timeout is None
, then write
calls will block indefinitely.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
Sets the value for the IP_TTL
option on this socket.
This value sets the time-to-live field that is used in every packet sent from this socket.
Sourcepub fn ttl(&self) -> Result<u32>
pub fn ttl(&self) -> Result<u32>
Gets the value of the IP_TTL
option for this socket.
For more information about this option, see set_ttl
.
Sourcepub fn set_broadcast(&self, broadcast: bool) -> Result<()>
pub fn set_broadcast(&self, broadcast: bool) -> Result<()>
Sets the value of the SO_BROADCAST option for this socket.
When enabled, this socket is allowed to send packets to a broadcast address.
Sourcepub fn broadcast(&self) -> Result<bool>
pub fn broadcast(&self) -> Result<bool>
Gets the value of the SO_BROADCAST
option for this socket.
For more information about this option, see
set_broadcast
.