Struct IcmpSocket

Source
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

Source

pub fn connect(addr: IpAddr) -> Result<IcmpSocket>

Connect socket to addr

Source

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

Receives data from the socket. On success, returns the number of bytes read.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn set_qos(&self, qos: u8) -> Result<()>

Sets the QoS value of the IP_TOS/IPV6_TCLASS option for this socket.

This value sets the TOS/DSCP field that is used in every packet sent from this socket.

Source

pub fn qos(&self) -> Result<u8>

Gets the value of the IP_TOS/IPV6_TCLASS option for this socket.

For more information about this option, see set_qos.

Auto Trait Implementations§

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.