Skip to content

Commit 56e625e

Browse files
Add Socket::(set_)recv_hoplimit_v6 (#543)
1 parent 60d118f commit 56e625e

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

src/socket.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,6 +2018,67 @@ impl Socket {
20182018
)
20192019
}
20202020
}
2021+
2022+
/// Get the value of the `IPV6_RECVHOPLIMIT` option for this socket.
2023+
///
2024+
/// For more information about this option, see [`set_recv_hoplimit_v6`].
2025+
///
2026+
/// [`set_recv_hoplimit_v6`]: Socket::set_recv_hoplimit_v6
2027+
#[cfg(all(
2028+
feature = "all",
2029+
not(any(
2030+
windows,
2031+
target_os = "dragonfly",
2032+
target_os = "fuchsia",
2033+
target_os = "illumos",
2034+
target_os = "netbsd",
2035+
target_os = "openbsd",
2036+
target_os = "redox",
2037+
target_os = "solaris",
2038+
target_os = "haiku",
2039+
target_os = "hurd",
2040+
target_os = "espidf",
2041+
target_os = "vita",
2042+
))
2043+
))]
2044+
pub fn recv_hoplimit_v6(&self) -> io::Result<bool> {
2045+
unsafe {
2046+
getsockopt::<c_int>(self.as_raw(), sys::IPPROTO_IPV6, sys::IPV6_RECVHOPLIMIT)
2047+
.map(|recv_hoplimit| recv_hoplimit > 0)
2048+
}
2049+
}
2050+
/// Set the value of the `IPV6_RECVHOPLIMIT` option for this socket.
2051+
///
2052+
/// The received hop limit is returned as ancillary data by recvmsg()
2053+
/// only if the application has enabled the IPV6_RECVHOPLIMIT socket
2054+
/// option:
2055+
#[cfg(all(
2056+
feature = "all",
2057+
not(any(
2058+
windows,
2059+
target_os = "dragonfly",
2060+
target_os = "fuchsia",
2061+
target_os = "illumos",
2062+
target_os = "netbsd",
2063+
target_os = "openbsd",
2064+
target_os = "redox",
2065+
target_os = "solaris",
2066+
target_os = "haiku",
2067+
target_os = "hurd",
2068+
target_os = "espidf",
2069+
target_os = "vita",
2070+
))
2071+
))]
2072+
pub fn set_recv_hoplimit_v6(&self, recv_hoplimit: bool) -> io::Result<()> {
2073+
unsafe {
2074+
setsockopt(
2075+
self.as_raw(),
2076+
sys::IPPROTO_IPV6,
2077+
sys::IPV6_RECVHOPLIMIT,
2078+
recv_hoplimit as c_int,
2079+
)
2080+
}
2081+
}
20212082
}
20222083

20232084
/// Socket options for TCP sockets, get/set using `IPPROTO_TCP`.

src/sys/unix.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,23 @@ pub(crate) use libc::SO_OOBINLINE;
122122
// Used in `Socket`.
123123
#[cfg(not(target_os = "nto"))]
124124
pub(crate) use libc::ipv6_mreq as Ipv6Mreq;
125+
#[cfg(all(
126+
feature = "all",
127+
not(any(
128+
target_os = "dragonfly",
129+
target_os = "fuchsia",
130+
target_os = "hurd",
131+
target_os = "illumos",
132+
target_os = "netbsd",
133+
target_os = "openbsd",
134+
target_os = "redox",
135+
target_os = "solaris",
136+
target_os = "haiku",
137+
target_os = "espidf",
138+
target_os = "vita",
139+
))
140+
))]
141+
pub(crate) use libc::IPV6_RECVHOPLIMIT;
125142
#[cfg(not(any(
126143
target_os = "dragonfly",
127144
target_os = "fuchsia",

tests/socket.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,24 @@ test!(IPv6 tclass_v6, set_tclass_v6(96));
14751475
)))]
14761476
test!(IPv6 recv_tclass_v6, set_recv_tclass_v6(true));
14771477

1478+
#[cfg(all(
1479+
feature = "all",
1480+
not(any(
1481+
target_os = "dragonfly",
1482+
target_os = "fuchsia",
1483+
target_os = "hurd",
1484+
target_os = "illumos",
1485+
target_os = "netbsd",
1486+
target_os = "openbsd",
1487+
target_os = "redox",
1488+
target_os = "solaris",
1489+
target_os = "windows",
1490+
target_os = "vita",
1491+
target_os = "haiku",
1492+
))
1493+
))]
1494+
test!(IPv6 recv_hoplimit_v6, set_recv_hoplimit_v6(true));
1495+
14781496
#[cfg(all(
14791497
feature = "all",
14801498
any(target_os = "android", target_os = "fuchsia", target_os = "linux")

0 commit comments

Comments
 (0)