From b01f831a616b123872d1a6e7f050a41a583afb38 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 22 Nov 2022 13:50:43 -0500 Subject: [PATCH] Add dgram sockets as an option Closes #3 --- src/socket.rs | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/src/socket.rs b/src/socket.rs index bb36a51..fbb0a9a 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -70,10 +70,14 @@ pub struct 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. pub fn new() -> std::io::Result { 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 { socket.set_recv_buffer_size(512)?; Ok(Self { 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 { + let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::ICMPV4))?; + Self::new_from_socket(socket) + } } impl IcmpSocket for IcmpSocket4 { @@ -136,10 +147,14 @@ pub struct 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. pub fn new() -> std::io::Result { 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 { socket.set_recv_buffer_size(512)?; Ok(Self { 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 { + let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(Protocol::ICMPV6))?; + Self::new_from_socket(socket) + } } impl IcmpSocket for IcmpSocket6 {