From 7e1849718d125d0fc0f22789b69ad579bda282aa Mon Sep 17 00:00:00 2001 From: Andrey Buchinskiy Date: Thu, 28 Nov 2024 14:15:22 -0800 Subject: [PATCH 1/5] adding support to enable IPV6_RECVHOPLIMIT socket option --- src/socket.rs | 30 ++++++++++++++++++++++++++++++ src/sys/unix.rs | 14 ++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/socket.rs b/src/socket.rs index 8d517b47..da5d159a 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -1998,6 +1998,36 @@ impl Socket { ) } } + + /// Set the value of the `IPV6_RECVHOPLIMIT` option for this socket. + /// + /// The received hop limit is returned as ancillary data by recvmsg() + /// only if the application has enabled the IPV6_RECVHOPLIMIT socket + /// option: + #[cfg(not(any( + windows, + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "haiku", + target_os = "hurd", + target_os = "espidf", + target_os = "vita", + )))] + pub fn set_recv_hoplimit_v6(&self, recv_hoplimit: bool) -> io::Result<()> { + unsafe { + setsockopt( + self.as_raw(), + sys::IPPROTO_IPV6, + sys::IPV6_RECVHOPLIMIT, + recv_hoplimit as c_int, + ) + } + } } /// Socket options for TCP sockets, get/set using `IPPROTO_TCP`. diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 3a898bc3..1ea97a2f 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -135,6 +135,20 @@ pub(crate) use libc::ipv6_mreq as Ipv6Mreq; target_os = "espidf", target_os = "vita", )))] +pub(crate) use libc::IPV6_RECVHOPLIMIT; +#[cfg(not(any( + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "hurd", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "haiku", + target_os = "espidf", + target_os = "vita", +)))] pub(crate) use libc::IPV6_RECVTCLASS; #[cfg(all(feature = "all", not(any(target_os = "redox", target_os = "espidf"))))] pub(crate) use libc::IP_HDRINCL; From 0241610c3d0cb483b8d3a41f0c3dc5987e7ea90c Mon Sep 17 00:00:00 2001 From: Andrey Buchinskiy Date: Mon, 2 Dec 2024 11:18:54 -0800 Subject: [PATCH 2/5] require feature "all" for ipv6_recv_hoplimit --- src/socket.rs | 31 +++++++++++++++++-------------- src/sys/unix.rs | 29 ++++++++++++++++------------- 2 files changed, 33 insertions(+), 27 deletions(-) diff --git a/src/socket.rs b/src/socket.rs index 32c8dd9d..1cc24189 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2024,20 +2024,23 @@ impl Socket { /// The received hop limit is returned as ancillary data by recvmsg() /// only if the application has enabled the IPV6_RECVHOPLIMIT socket /// option: - #[cfg(not(any( - windows, - target_os = "dragonfly", - target_os = "fuchsia", - target_os = "illumos", - target_os = "netbsd", - target_os = "openbsd", - target_os = "redox", - target_os = "solaris", - target_os = "haiku", - target_os = "hurd", - target_os = "espidf", - target_os = "vita", - )))] + #[cfg(all( + feature = "all", + not(any( + windows, + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "haiku", + target_os = "hurd", + target_os = "espidf", + target_os = "vita", + )) + ))] pub fn set_recv_hoplimit_v6(&self, recv_hoplimit: bool) -> io::Result<()> { unsafe { setsockopt( diff --git a/src/sys/unix.rs b/src/sys/unix.rs index 237cbeb3..b40c3df1 100644 --- a/src/sys/unix.rs +++ b/src/sys/unix.rs @@ -122,19 +122,22 @@ pub(crate) use libc::SO_OOBINLINE; // Used in `Socket`. #[cfg(not(target_os = "nto"))] pub(crate) use libc::ipv6_mreq as Ipv6Mreq; -#[cfg(not(any( - target_os = "dragonfly", - target_os = "fuchsia", - target_os = "hurd", - target_os = "illumos", - target_os = "netbsd", - target_os = "openbsd", - target_os = "redox", - target_os = "solaris", - target_os = "haiku", - target_os = "espidf", - target_os = "vita", -)))] +#[cfg(all( + feature = "all", + not(any( + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "hurd", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "haiku", + target_os = "espidf", + target_os = "vita", + )) +))] pub(crate) use libc::IPV6_RECVHOPLIMIT; #[cfg(not(any( target_os = "dragonfly", From fc7905dbbfdadebb5b76374d57ab9a39278f79e4 Mon Sep 17 00:00:00 2001 From: Andrey Buchinskiy Date: Wed, 4 Dec 2024 12:07:28 -0800 Subject: [PATCH 3/5] adding method to retrieve recv hoplimit, and adding tests --- src/socket.rs | 27 +++++++++++++++++++++++++++ tests/socket.rs | 18 ++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/socket.rs b/src/socket.rs index 1cc24189..cd42d007 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2019,6 +2019,33 @@ impl Socket { } } + /// Get the value of the `IPV6_RECVHOPLIMIT` option for this socket. + /// + /// For more information about this option, see [`set_recv_hoplimit_v6`]. + /// + /// [`set_recv_hoplimit_v6`]: Socket::set_recv_hoplimit_v6 + #[cfg(all( + feature = "all", + not(any( + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "haiku", + target_os = "hurd", + target_os = "espidf", + target_os = "vita", + )) + ))] + pub fn recv_hoplimit_v6(&self) -> io::Result { + unsafe { + getsockopt::(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_RECVHOPLIMIT) + .map(|recv_tclass| recv_tclass > 0) + } + } /// Set the value of the `IPV6_RECVHOPLIMIT` option for this socket. /// /// The received hop limit is returned as ancillary data by recvmsg() diff --git a/tests/socket.rs b/tests/socket.rs index 0959ef39..98a535c7 100644 --- a/tests/socket.rs +++ b/tests/socket.rs @@ -1475,6 +1475,24 @@ test!(IPv6 tclass_v6, set_tclass_v6(96)); )))] test!(IPv6 recv_tclass_v6, set_recv_tclass_v6(true)); +#[cfg(all( + feature = "all", + not(any( + target_os = "dragonfly", + target_os = "fuchsia", + target_os = "hurd", + target_os = "illumos", + target_os = "netbsd", + target_os = "openbsd", + target_os = "redox", + target_os = "solaris", + target_os = "windows", + target_os = "vita", + target_os = "haiku", + )) +))] +test!(IPv6 recv_hoplimit_v6, set_recv_hoplimit_v6(true)); + #[cfg(all( feature = "all", any(target_os = "android", target_os = "fuchsia", target_os = "linux") From f107310b71b54abce3e6807d1d32f7b7b300d594 Mon Sep 17 00:00:00 2001 From: Andrey Buchinskiy Date: Wed, 4 Dec 2024 12:07:28 -0800 Subject: [PATCH 4/5] adding method to retrieve recv hoplimit, and adding tests --- src/socket.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/socket.rs b/src/socket.rs index cd42d007..3b8676f6 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2043,7 +2043,7 @@ impl Socket { pub fn recv_hoplimit_v6(&self) -> io::Result { unsafe { getsockopt::(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_RECVHOPLIMIT) - .map(|recv_tclass| recv_tclass > 0) + .map(|recv_hoplimit| recv_hoplimit > 0) } } /// Set the value of the `IPV6_RECVHOPLIMIT` option for this socket. From 6082050aa4dbfa82f2606352d74800d7b40e823d Mon Sep 17 00:00:00 2001 From: Andrey Buchinskiy Date: Wed, 4 Dec 2024 12:07:28 -0800 Subject: [PATCH 5/5] adding method to retrieve recv hoplimit, and adding tests --- src/socket.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/socket.rs b/src/socket.rs index 3b8676f6..698d3556 100644 --- a/src/socket.rs +++ b/src/socket.rs @@ -2027,6 +2027,7 @@ impl Socket { #[cfg(all( feature = "all", not(any( + windows, target_os = "dragonfly", target_os = "fuchsia", target_os = "illumos",