Struct IcmpSocket6

Source
pub struct IcmpSocket6 { /* private fields */ }
Expand description

An Icmpv6 socket.

Implementations§

Source§

impl IcmpSocket6

Source

pub fn new() -> Result<Self>

Construct a new socket. The socket must be bound to an address using bind_to before it can be used to send and receive packets.

Examples found in repository?
examples/ping6.rs (line 54)
23pub fn main() {
24    let address = std::env::args().nth(1).unwrap_or("::1".to_owned());
25    let parsed_addr = address.parse::<Ipv6Addr>().unwrap();
26    let packet_handler = |pkt: Icmpv6Packet, send_time: Instant, addr: Ipv6Addr| -> Option<()> {
27        let now = Instant::now();
28        let elapsed = now - send_time;
29        if addr == parsed_addr {
30            // TODO
31            if let Icmpv6Message::EchoReply {
32                identifier: _,
33                sequence,
34                payload,
35            } = pkt.message
36            {
37                println!(
38                    "Ping {} seq={} time={}ms size={}",
39                    addr,
40                    sequence,
41                    (elapsed.as_micros() as f64) / 1000.0,
42                    payload.len()
43                );
44            } else {
45                //eprintln!("Discarding non-reply {:?}", pkt);
46                return None;
47            }
48            Some(())
49        } else {
50            eprintln!("Discarding packet from {}", addr);
51            None
52        }
53    };
54    let mut socket6 = IcmpSocket6::new().unwrap();
55    socket6.bind("::0".parse::<Ipv6Addr>().unwrap()).unwrap();
56    // TODO(jwall): The first packet we recieve will be the one we sent.
57    // We need to implement packet filtering for the socket.
58    let mut sequence = 0 as u16;
59    loop {
60        let packet = Icmpv6Packet::with_echo_request(
61            42,
62            sequence,
63            vec![
64                0x20, 0x20, 0x75, 0x73, 0x74, 0x20, 0x61, 0x20, 0x66, 0x6c, 0x65, 0x73, 0x68, 0x20,
65                0x77, 0x6f, 0x75, 0x6e, 0x64, 0x20, 0x20, 0x74, 0x69, 0x73, 0x20, 0x62, 0x75, 0x74,
66                0x20, 0x61, 0x20, 0x73, 0x63, 0x72, 0x61, 0x74, 0x63, 0x68, 0x20, 0x20, 0x6b, 0x6e,
67                0x69, 0x67, 0x68, 0x74, 0x73, 0x20, 0x6f, 0x66, 0x20, 0x6e, 0x69, 0x20, 0x20, 0x20,
68            ],
69        )
70        .unwrap();
71        let send_time = Instant::now();
72        socket6
73            .send_to(address.parse::<Ipv6Addr>().unwrap(), packet)
74            .unwrap();
75        socket6.set_timeout(Some(Duration::from_secs(1)));
76        loop {
77            let (resp, sock_addr) = match socket6.rcv_from() {
78                Ok(tpl) => tpl,
79                Err(e) => {
80                    eprintln!("{:?}", e);
81                    break;
82                }
83            };
84            if packet_handler(resp, send_time, *sock_addr.as_socket_ipv6().unwrap().ip()).is_some()
85            {
86                std::thread::sleep(Duration::from_millis(1000));
87                break;
88            }
89        }
90        sequence = sequence.wrapping_add(1);
91    }
92}

Trait Implementations§

Source§

impl IcmpSocket for IcmpSocket6

Source§

type AddrType = Ipv6Addr

The type of address this socket operates on.
Source§

type PacketType = Icmpv6Packet

The type of packet this socket handles.
Source§

fn set_max_hops(&mut self, hops: u32)

Sets the ttl for packets sent on this socket. Controls the number of hops the packet will be allowed to traverse.
Source§

fn bind<A: Into<Self::AddrType>>(&mut self, addr: A) -> Result<()>

Binds this socket to an address.
Source§

fn send_to( &mut self, dest: Self::AddrType, packet: Self::PacketType, ) -> Result<()>

Sends the packet to the given destination.
Source§

fn rcv_from(&mut self) -> Result<(Self::PacketType, SockAddr)>

Receive a packet on this socket.
Source§

fn set_timeout(&mut self, timeout: Option<Duration>)

Sets the timeout on the socket for rcv_from. A value of None will cause rcv_from to block.
Source§

impl TryFrom<Ipv6Addr> for IcmpSocket6

Source§

type Error = Error

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

fn try_from(addr: Ipv6Addr) -> Result<Self, Self::Error>

Performs the conversion.

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.