Skip to content

Commit

Permalink
Add Socket::(set_)recv_hoplimit_v6 (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreybuchinskiy authored Dec 5, 2024
1 parent 60d118f commit 56e625e
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,67 @@ 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(
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 recv_hoplimit_v6(&self) -> io::Result<bool> {
unsafe {
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_RECVHOPLIMIT)
.map(|recv_hoplimit| recv_hoplimit > 0)
}
}
/// 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(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(
self.as_raw(),
sys::IPPROTO_IPV6,
sys::IPV6_RECVHOPLIMIT,
recv_hoplimit as c_int,
)
}
}
}

/// Socket options for TCP sockets, get/set using `IPPROTO_TCP`.
Expand Down
17 changes: 17 additions & 0 deletions src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ pub(crate) use libc::SO_OOBINLINE;
// Used in `Socket`.
#[cfg(not(target_os = "nto"))]
pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
#[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",
target_os = "fuchsia",
Expand Down
18 changes: 18 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 56e625e

Please sign in to comment.