Merge branch 'master' of github.com:zaphar/icmp-socket

This commit is contained in:
Jeremy Wall 2022-02-09 18:53:38 -05:00
commit 4c666baf78
3 changed files with 20 additions and 13 deletions

View File

@ -74,7 +74,7 @@ pub fn main() {
socket4 socket4
.send_to(address.parse::<Ipv4Addr>().unwrap(), packet) .send_to(address.parse::<Ipv4Addr>().unwrap(), packet)
.unwrap(); .unwrap();
socket4.set_timeout(Duration::from_secs(1)).unwrap(); socket4.set_timeout(Some(Duration::from_secs(1)));
loop { loop {
let (resp, sock_addr) = match socket4.rcv_from() { let (resp, sock_addr) = match socket4.rcv_from() {
Ok(tpl) => tpl, Ok(tpl) => tpl,

View File

@ -72,7 +72,7 @@ pub fn main() {
socket6 socket6
.send_to(address.parse::<Ipv6Addr>().unwrap(), packet) .send_to(address.parse::<Ipv6Addr>().unwrap(), packet)
.unwrap(); .unwrap();
socket6.set_timeout(Duration::from_secs(1)).unwrap(); socket6.set_timeout(Some(Duration::from_secs(1)));
loop { loop {
let (resp, sock_addr) = match socket6.rcv_from() { let (resp, sock_addr) = match socket6.rcv_from() {
Ok(tpl) => tpl, Ok(tpl) => tpl,

View File

@ -37,9 +37,9 @@ pub trait IcmpSocket {
/// The type of packet this socket handles. /// The type of packet this socket handles.
type PacketType; type PacketType;
/// Sets the timeout on the socket for rcv_from. A value of 0 will cause /// Sets the timeout on the socket for rcv_from. A value of None
/// rcv_from to block. /// will cause rcv_from to block.
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()>; fn set_timeout(&mut self, timeout: Option<Duration>);
/// Sets the ttl for packets sent on this socket. Controls the number of /// Sets the ttl for packets sent on this socket. Controls the number of
/// hops the packet will be allowed to traverse. /// hops the packet will be allowed to traverse.
@ -58,6 +58,7 @@ pub trait IcmpSocket {
/// Options for this socket. /// Options for this socket.
struct Opts { struct Opts {
hops: u32, hops: u32,
timeout: Option<Duration>,
} }
/// An ICMPv4 socket. /// An ICMPv4 socket.
@ -78,7 +79,10 @@ impl IcmpSocket4 {
bound_to: None, bound_to: None,
inner: socket, inner: socket,
buf: vec![0; 512], buf: vec![0; 512],
opts: Opts { hops: 50 }, opts: Opts {
hops: 50,
timeout: None,
},
}) })
} }
} }
@ -108,7 +112,7 @@ impl IcmpSocket for IcmpSocket4 {
} }
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> { fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
self.inner.set_read_timeout(None)?; self.inner.set_read_timeout(self.opts.timeout)?;
// NOTE(jwall): the `recv_from` implementation promises not to write uninitialised // NOTE(jwall): the `recv_from` implementation promises not to write uninitialised
// bytes to the `buf`fer, so this casting is safe. // bytes to the `buf`fer, so this casting is safe.
// TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes. // TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes.
@ -118,8 +122,8 @@ impl IcmpSocket for IcmpSocket4 {
Ok((self.buf[0..read_count].try_into()?, addr)) Ok((self.buf[0..read_count].try_into()?, addr))
} }
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> { fn set_timeout(&mut self, timeout: Option<Duration>) {
self.inner.set_read_timeout(Some(timeout)) self.opts.timeout = timeout;
} }
} }
@ -141,7 +145,10 @@ impl IcmpSocket6 {
bound_to: None, bound_to: None,
inner: socket, inner: socket,
buf: vec![0; 512], buf: vec![0; 512],
opts: Opts { hops: 50 }, opts: Opts {
hops: 50,
timeout: None,
},
}) })
} }
} }
@ -185,7 +192,7 @@ impl IcmpSocket for IcmpSocket6 {
} }
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> { fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
self.inner.set_read_timeout(None)?; self.inner.set_read_timeout(self.opts.timeout)?;
// NOTE(jwall): the `recv_from` implementation promises not to write uninitialised // NOTE(jwall): the `recv_from` implementation promises not to write uninitialised
// bytes to the `buf`fer, so this casting is safe. // bytes to the `buf`fer, so this casting is safe.
// TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes. // TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes.
@ -195,8 +202,8 @@ impl IcmpSocket for IcmpSocket6 {
Ok((self.buf[0..read_count].try_into()?, addr)) Ok((self.buf[0..read_count].try_into()?, addr))
} }
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> { fn set_timeout(&mut self, timeout: Option<Duration>) {
self.inner.set_read_timeout(Some(timeout)) self.opts.timeout = timeout;
} }
} }