Merge pull request #4 from zaphar/allow_dgram_sockets

Add dgram sockets as an option
This commit is contained in:
Jeremy Wall 2022-11-23 18:07:35 -05:00 committed by GitHub
commit 02b0714302
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -70,10 +70,14 @@ pub struct IcmpSocket4 {
} }
impl IcmpSocket4 { impl IcmpSocket4 {
/// Construct a new socket. The socket must be bound to an address using `bind_to` /// Construct a new raw socket. The socket must be bound to an address using `bind_to`
/// before it can be used to send and receive packets. /// before it can be used to send and receive packets.
pub fn new() -> std::io::Result<Self> { pub fn new() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV4, Type::RAW, Some(Protocol::ICMPV4))?; 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)?; socket.set_recv_buffer_size(512)?;
Ok(Self { Ok(Self {
bound_to: None, bound_to: None,
@ -85,6 +89,13 @@ impl IcmpSocket4 {
}, },
}) })
} }
/// 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 { impl IcmpSocket for IcmpSocket4 {
@ -136,10 +147,14 @@ pub struct IcmpSocket6 {
} }
impl IcmpSocket6 { impl IcmpSocket6 {
/// Construct a new socket. The socket must be bound to an address using `bind_to` /// Construct a new raw socket. The socket must be bound to an address using `bind_to`
/// before it can be used to send and receive packets. /// before it can be used to send and receive packets.
pub fn new() -> std::io::Result<Self> { pub fn new() -> std::io::Result<Self> {
let socket = Socket::new(Domain::IPV6, Type::RAW, Some(Protocol::ICMPV6))?; 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)?; socket.set_recv_buffer_size(512)?;
Ok(Self { Ok(Self {
bound_to: None, bound_to: None,
@ -151,6 +166,13 @@ impl IcmpSocket6 {
}, },
}) })
} }
/// 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 { impl IcmpSocket for IcmpSocket6 {