Compare commits

..

No commits in common. "master" and "v0.1.2" have entirely different histories.

5 changed files with 19 additions and 47 deletions

View File

@ -1,7 +1,7 @@
[package]
name = "icmp-socket"
description = "ICMP sockets for both IPv4 and IPv6"
version = "0.2.0"
version = "0.1.2"
repository = "https://github.com/zaphar/icmp-socket"
authors = ["Jeremy Wall <jeremy@marzhillstudios.com>"]
edition = "2018"

View File

@ -14,8 +14,9 @@ It can construct and parse the common ICMP packets for both ICMP4 and ICMP6.
```rust
let packet4 = Icmpv4Packet::with_echo_request(42, 1, "payload".to_bytes());
let packet6 = Icmpv6Packet::with_echo_request(42, 1, "payload".to_bytes());
)
```
# API Documentation
https://docs.rs/icmp-socket/0.2.0
https://docs.rs/icmp-socket/0.1.1

View File

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

View File

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

View File

@ -37,9 +37,9 @@ pub trait IcmpSocket {
/// The type of packet this socket handles.
type PacketType;
/// Sets the timeout on the socket for rcv_from. A value of None
/// will cause rcv_from to block.
fn set_timeout(&mut self, timeout: Option<Duration>);
/// Sets the timeout on the socket for rcv_from. A value of 0 will cause
/// rcv_from to block.
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()>;
/// Sets the ttl for packets sent on this socket. Controls the number of
/// hops the packet will be allowed to traverse.
@ -56,9 +56,8 @@ pub trait IcmpSocket {
}
/// Options for this socket.
struct Opts {
pub struct Opts {
hops: u32,
timeout: Option<Duration>,
}
/// An ICMPv4 socket.
@ -70,32 +69,18 @@ pub struct IcmpSocket4 {
}
impl IcmpSocket4 {
/// Construct a new raw socket. The socket must be bound to an address using `bind_to`
/// 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.
pub fn new() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::ICMPV4))?;
Self::new_from_socket(socket)
}
fn new_from_socket(socket: Socket) -> std::io::Result<Self> {
socket.set_recv_buffer_size(512)?;
Ok(Self {
bound_to: None,
inner: socket,
buf: vec![0; 512],
opts: Opts {
hops: 50,
timeout: None,
},
opts: Opts { hops: 50 },
})
}
/// Construct a new dgram socket. The socket must be bound to an address using `bind_to`
/// before it can be used to send and receive packets.
pub fn new_dgram_socket() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4))?;
Self::new_from_socket(socket)
}
}
impl IcmpSocket for IcmpSocket4 {
@ -123,7 +108,7 @@ impl IcmpSocket for IcmpSocket4 {
}
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
self.inner.set_read_timeout(self.opts.timeout)?;
self.inner.set_read_timeout(None)?;
// NOTE(jwall): the `recv_from` implementation promises not to write uninitialised
// bytes to the `buf`fer, so this casting is safe.
// TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes.
@ -133,8 +118,8 @@ impl IcmpSocket for IcmpSocket4 {
Ok((self.buf[0..read_count].try_into()?, addr))
}
fn set_timeout(&mut self, timeout: Option<Duration>) {
self.opts.timeout = timeout;
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> {
self.inner.set_read_timeout(Some(timeout))
}
}
@ -147,32 +132,18 @@ pub struct IcmpSocket6 {
}
impl IcmpSocket6 {
/// Construct a new raw socket. The socket must be bound to an address using `bind_to`
/// 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.
pub fn new() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV6, Type::RAW, Some(Protocol::ICMPV6))?;
Self::new_from_socket(socket)
}
fn new_from_socket(socket: Socket) -> std::io::Result<Self> {
socket.set_recv_buffer_size(512)?;
Ok(Self {
bound_to: None,
inner: socket,
buf: vec![0; 512],
opts: Opts {
hops: 50,
timeout: None,
},
opts: Opts { hops: 50 },
})
}
/// Construct a new dgram socket. The socket must be bound to an address using `bind_to`
/// before it can be used to send and receive packets.
pub fn new_dgram_socket() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::ICMPV6))?;
Self::new_from_socket(socket)
}
}
impl IcmpSocket for IcmpSocket6 {
@ -214,7 +185,7 @@ impl IcmpSocket for IcmpSocket6 {
}
fn rcv_from(&mut self) -> std::io::Result<(Self::PacketType, SockAddr)> {
self.inner.set_read_timeout(self.opts.timeout)?;
self.inner.set_read_timeout(None)?;
// NOTE(jwall): the `recv_from` implementation promises not to write uninitialised
// bytes to the `buf`fer, so this casting is safe.
// TODO(jwall): change to `Vec::spare_capacity_mut` when it stabilizes.
@ -224,8 +195,8 @@ impl IcmpSocket for IcmpSocket6 {
Ok((self.buf[0..read_count].try_into()?, addr))
}
fn set_timeout(&mut self, timeout: Option<Duration>) {
self.opts.timeout = timeout;
fn set_timeout(&mut self, timeout: Duration) -> std::io::Result<()> {
self.inner.set_read_timeout(Some(timeout))
}
}