Skip to content

Commit fa314d2

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 842142f commit fa314d2

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).
4141
([#2097](https://github.com/nix-rust/nix/pull/2097))
4242
- Add the ability to set `kevent_flags` on `SigEvent`.
4343
([#1731](https://github.com/nix-rust/nix/pull/1731))
44+
- Added `Ipv6HopLimit` to `::nix::sys::socket::ControlMessage` for Linux,
45+
MacOS, FreeBSD, DragonflyBSD, Android, iOS and Haiku.
46+
([#2074](https://github.com/nix-rust/nix/pull/2074))
4447

4548
### Changed
4649

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")))]
@@ -1139,7 +1143,7 @@ pub enum ControlMessage<'a> {
11391143
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
11401144
UdpGsoSegments(&'a u16),
11411145

1142-
/// Configure the sending addressing and interface for v4
1146+
/// Configure the sending addressing and interface for v4.
11431147
///
11441148
/// For further information, please refer to the
11451149
/// [`ip(7)`](https://man7.org/linux/man-pages/man7/ip.7.html) man page.
@@ -1152,7 +1156,7 @@ pub enum ControlMessage<'a> {
11521156
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
11531157
Ipv4PacketInfo(&'a libc::in_pktinfo),
11541158

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

1184+
/// Configure the hop limit for v6 multicast traffic.
1185+
///
1186+
/// Set the IPv6 hop limit for this message. The argument is an integer
1187+
/// between 0 and 255. A value of -1 will set the hop limit to the route
1188+
/// default if possible on the interface. Without this cmsg, packets sent
1189+
/// with sendmsg have a hop limit of 1 and will not leave the local network.
1190+
/// For further information, please refer to the
1191+
/// [`ipv6(7)`](https://man7.org/linux/man-pages/man7/ipv6.7.html) man page.
1192+
#[cfg(any(target_os = "linux", target_os = "macos",
1193+
target_os = "freebsd", target_os = "dragonfly",
1194+
target_os = "android", target_os = "ios",
1195+
target_os = "haiku"))]
1196+
#[cfg(feature = "net")]
1197+
#[cfg_attr(docsrs, doc(cfg(feature = "net")))]
1198+
Ipv6HopLimit(&'a libc::c_int),
1199+
11801200
/// SO_RXQ_OVFL indicates that an unsigned 32 bit value
11811201
/// ancilliary msg (cmsg) should be attached to recieved
11821202
/// skbs indicating the number of packets dropped by the
@@ -1290,6 +1310,12 @@ impl<'a> ControlMessage<'a> {
12901310
target_os = "openbsd", target_os = "dragonfly"))]
12911311
#[cfg(feature = "net")]
12921312
ControlMessage::Ipv4SendSrcAddr(addr) => addr as *const _ as *const u8,
1313+
#[cfg(any(target_os = "linux", target_os = "macos",
1314+
target_os = "freebsd", target_os = "dragonfly",
1315+
target_os = "android", target_os = "ios",
1316+
target_os = "haiku"))]
1317+
#[cfg(feature = "net")]
1318+
ControlMessage::Ipv6HopLimit(limit) => limit as *const _ as *const u8,
12931319
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
12941320
ControlMessage::RxqOvfl(drop_count) => {
12951321
drop_count as *const _ as *const u8
@@ -1353,6 +1379,14 @@ impl<'a> ControlMessage<'a> {
13531379
target_os = "openbsd", target_os = "dragonfly"))]
13541380
#[cfg(feature = "net")]
13551381
ControlMessage::Ipv4SendSrcAddr(addr) => mem::size_of_val(addr),
1382+
#[cfg(any(target_os = "linux", target_os = "macos",
1383+
target_os = "freebsd", target_os = "dragonfly",
1384+
target_os = "android", target_os = "ios",
1385+
target_os = "haiku"))]
1386+
#[cfg(feature = "net")]
1387+
ControlMessage::Ipv6HopLimit(limit) => {
1388+
mem::size_of_val(limit)
1389+
},
13561390
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
13571391
ControlMessage::RxqOvfl(drop_count) => {
13581392
mem::size_of_val(drop_count)
@@ -1392,6 +1426,12 @@ impl<'a> ControlMessage<'a> {
13921426
target_os = "openbsd", target_os = "dragonfly"))]
13931427
#[cfg(feature = "net")]
13941428
ControlMessage::Ipv4SendSrcAddr(_) => libc::IPPROTO_IP,
1429+
#[cfg(any(target_os = "linux", target_os = "macos",
1430+
target_os = "freebsd", target_os = "dragonfly",
1431+
target_os = "android", target_os = "ios",
1432+
target_os = "haiku"))]
1433+
#[cfg(feature = "net")]
1434+
ControlMessage::Ipv6HopLimit(_) => libc::IPPROTO_IPV6,
13951435
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
13961436
ControlMessage::RxqOvfl(_) => libc::SOL_SOCKET,
13971437
#[cfg(target_os = "linux")]
@@ -1438,6 +1478,12 @@ impl<'a> ControlMessage<'a> {
14381478
target_os = "openbsd", target_os = "dragonfly"))]
14391479
#[cfg(feature = "net")]
14401480
ControlMessage::Ipv4SendSrcAddr(_) => libc::IP_SENDSRCADDR,
1481+
#[cfg(any(target_os = "linux", target_os = "macos",
1482+
target_os = "freebsd", target_os = "dragonfly",
1483+
target_os = "android", target_os = "ios",
1484+
target_os = "haiku"))]
1485+
#[cfg(feature = "net")]
1486+
ControlMessage::Ipv6HopLimit(_) => libc::IPV6_HOPLIMIT,
14411487
#[cfg(any(target_os = "android", target_os = "fuchsia", target_os = "linux"))]
14421488
ControlMessage::RxqOvfl(_) => {
14431489
libc::SO_RXQ_OVFL
@@ -2166,9 +2212,7 @@ pub fn socketpair<T: Into<Option<SockProtocol>>>(
21662212
Errno::result(res)?;
21672213

21682214
// Safe because socketpair returned success.
2169-
unsafe {
2170-
Ok((OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])))
2171-
}
2215+
unsafe { Ok((OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1]))) }
21722216
}
21732217

21742218
/// Listen for connections on a socket
@@ -2276,13 +2320,7 @@ pub fn recvfrom<T: SockaddrLike>(
22762320
&mut len as *mut socklen_t,
22772321
))? as usize;
22782322

2279-
Ok((
2280-
ret,
2281-
T::from_raw(
2282-
addr.assume_init().as_ptr(),
2283-
Some(len),
2284-
),
2285-
))
2323+
Ok((ret, T::from_raw(addr.assume_init().as_ptr(), Some(len))))
22862324
}
22872325
}
22882326

0 commit comments

Comments
 (0)