Skip to content

Commit e35d4ff

Browse files
committed
2075: Add ControlMessage::Ipv6HopLimit
When sending IPv6 packets with `sendmsg`, Linux does not use the hop limit set on the socket. Instead, the hop limit has to be specified for each individual message with ancillary data in a cmsg. This commit adds the enum variant `ControlMessage::Ipv6HopLimit` to specify the limit. The variant is available on the `net` feature flag for Linux, MacOs, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
1 parent ee91423 commit e35d4ff

File tree

2 files changed

+54
-14
lines changed

2 files changed

+54
-14
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ This project adheres to [Semantic Versioning](https://semver.org/).
2727
- Added `SO_RTABLE` for OpenBSD and `SO_ACCEPTFILTER` for FreeBSD/NetBSD to `nix::sys::socket::sockopt`.
2828
([#2085](https://github.com/nix-rust/nix/pull/2085))
2929
- Removed `flock` from `::nix::fcntl` on Solaris. ([#2082](https://github.com/nix-rust/nix/pull/2082))
30+
- Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for linux.
31+
([#2074](https://github.com/nix-rust/nix/pull/2074))
3032

3133
### Changed
3234

src/sys/socket/mod.rs

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use libc::{
1818
use std::io::{IoSlice, IoSliceMut};
1919
#[cfg(feature = "net")]
2020
use std::net;
21-
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, RawFd, OwnedFd};
21+
use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd};
2222
use std::{mem, ptr};
2323

2424
#[deny(missing_docs)]
@@ -62,7 +62,11 @@ pub use crate::sys::socket::addr::netlink::NetlinkAddr;
6262
#[cfg(any(target_os = "ios", target_os = "macos"))]
6363
#[cfg(feature = "ioctl")]
6464
pub use crate::sys::socket::addr::sys_control::SysControlAddr;
65-
#[cfg(any(target_os = "android", target_os = "linux", target_os = "macos"))]
65+
#[cfg(any(
66+
target_os = "android",
67+
target_os = "linux",
68+
target_os = "macos"
69+
))]
6670
pub use crate::sys::socket::addr::vsock::VsockAddr;
6771

6872
#[cfg(all(feature = "uio", not(target_os = "redox")))]
@@ -1118,7 +1122,7 @@ pub enum ControlMessage<'a> {
11181122
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
11191123
UdpGsoSegments(&'a u16),
11201124

1121-
/// Configure the sending addressing and interface for v4
1125+
/// Configure the sending addressing and interface for v4.
11221126
///
11231127
/// For further information, please refer to the
11241128
/// [`ip(7)`](https://man7.org/linux/man-pages/man7/ip.7.html) man page.
@@ -1131,7 +1135,7 @@ pub enum ControlMessage<'a> {
11311135
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
11321136
Ipv4PacketInfo(&'a libc::in_pktinfo),
11331137

1134-
/// Configure the sending addressing and interface for v6
1138+
/// Configure the sending addressing and interface for v6.
11351139
///
11361140
/// For further information, please refer to the
11371141
/// [`ipv6(7)`](https://man7.org/linux/man-pages/man7/ipv6.7.html) man page.
@@ -1156,6 +1160,22 @@ pub enum ControlMessage<'a> {
11561160
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
11571161
Ipv4SendSrcAddr(&'a libc::in_addr),
11581162

1163+
/// Configure the hop limit for v6 multicast traffic.
1164+
///
1165+
/// Set the IPv6 hop limit for this message. The argument is an integer
1166+
/// between 0 and 255. A value of -1 will set the hop limit to the route
1167+
/// default if possible on the interface. Without this cmsg, packets sent
1168+
/// with sendmsg have a hop limit of 1 and will not leave the local network.
1169+
/// For further information, please refer to the
1170+
/// [`ipv6(7)`](https://man7.org/linux/man-pages/man7/ipv6.7.html) man page.
1171+
#[cfg(any(target_os = "linux", target_os = "macos",
1172+
target_os = "freebsd", target_os = "dragonfly",
1173+
target_os = "android", target_os = "ios",
1174+
target_os = "haiku"))]
1175+
#[cfg(feature = "net")]
1176+
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
1177+
Ipv6HopLimit(&'a libc::c_int),
1178+
11591179
/// SO_RXQ_OVFL indicates that an unsigned 32 bit value
11601180
/// ancilliary msg (cmsg) should be attached to recieved
11611181
/// skbs indicating the number of packets dropped by the
@@ -1269,6 +1289,12 @@ impl<'a> ControlMessage<'a> {
12691289
target_os = "openbsd", target_os = "dragonfly"))]
12701290
#[cfg(feature = "net")]
12711291
ControlMessage::Ipv4SendSrcAddr(addr) => addr as *const _ as *const u8,
1292+
#[cfg(any(target_os = "linux", target_os = "macos",
1293+
target_os = "freebsd", target_os = "dragonfly",
1294+
target_os = "android", target_os = "ios",
1295+
target_os = "haiku"))]
1296+
#[cfg(feature = "net")]
1297+
ControlMessage::Ipv6HopLimit(limit) => limit as *const _ as *const u8,
12721298
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
12731299
ControlMessage::RxqOvfl(drop_count) => {
12741300
drop_count as *const _ as *const u8
@@ -1332,6 +1358,14 @@ impl<'a> ControlMessage<'a> {
13321358
target_os = "openbsd", target_os = "dragonfly"))]
13331359
#[cfg(feature = "net")]
13341360
ControlMessage::Ipv4SendSrcAddr(addr) => mem::size_of_val(addr),
1361+
#[cfg(any(target_os = "linux", target_os = "macos",
1362+
target_os = "freebsd", target_os = "dragonfly",
1363+
target_os = "android", target_os = "ios",
1364+
target_os = "haiku"))]
1365+
#[cfg(feature = "net")]
1366+
ControlMessage::Ipv6HopLimit(limit) => {
1367+
mem::size_of_val(limit)
1368+
},
13351369
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
13361370
ControlMessage::RxqOvfl(drop_count) => {
13371371
mem::size_of_val(drop_count)
@@ -1371,6 +1405,12 @@ impl<'a> ControlMessage<'a> {
13711405
target_os = "openbsd", target_os = "dragonfly"))]
13721406
#[cfg(feature = "net")]
13731407
ControlMessage::Ipv4SendSrcAddr(_) => libc::IPPROTO_IP,
1408+
#[cfg(any(target_os = "linux", target_os = "macos",
1409+
target_os = "freebsd", target_os = "dragonfly",
1410+
target_os = "android", target_os = "ios",
1411+
target_os = "haiku"))]
1412+
#[cfg(feature = "net")]
1413+
ControlMessage::Ipv6HopLimit(_) => libc::IPPROTO_IPV6,
13741414
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
13751415
ControlMessage::RxqOvfl(_) => libc::SOL_SOCKET,
13761416
#[cfg(target_os = "linux")]
@@ -1417,6 +1457,12 @@ impl<'a> ControlMessage<'a> {
14171457
target_os = "openbsd", target_os = "dragonfly"))]
14181458
#[cfg(feature = "net")]
14191459
ControlMessage::Ipv4SendSrcAddr(_) => libc::IP_SENDSRCADDR,
1460+
#[cfg(any(target_os = "linux", target_os = "macos",
1461+
target_os = "freebsd", target_os = "dragonfly",
1462+
target_os = "android", target_os = "ios",
1463+
target_os = "haiku"))]
1464+
#[cfg(feature = "net")]
1465+
ControlMessage::Ipv6HopLimit(_) => libc::IPV6_HOPLIMIT,
14201466
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
14211467
ControlMessage::RxqOvfl(_) => {
14221468
libc::SO_RXQ_OVFL
@@ -2145,9 +2191,7 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(
21452191
Errno::result(res)?;
21462192

21472193
// Safe because socketpair returned success.
2148-
unsafe {
2149-
Ok((OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])))
2150-
}
2194+
unsafe { Ok((OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1]))) }
21512195
}
21522196

21532197
/// Listen for connections on a socket
@@ -2255,13 +2299,7 @@ pub fn recvfrom<T: SockaddrLike>(
22552299
&mut len as *mut socklen_t,
22562300
))? as usize;
22572301

2258-
Ok((
2259-
ret,
2260-
T::from_raw(
2261-
addr.assume_init().as_ptr(),
2262-
Some(len),
2263-
),
2264-
))
2302+
Ok((ret, T::from_raw(addr.assume_init().as_ptr(), Some(len))))
22652303
}
22662304
}
22672305

0 commit comments

Comments
 (0)