Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for socket options to get/set TOS/TTL info #2203

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
14ccc66
Add support for getting/setting IPv4 type-of-service (TOS) and
larseggert Nov 20, 2023
2f865a5
Linux fixes
larseggert Nov 20, 2023
97dfca8
Linux works
larseggert Nov 21, 2023
bb78faa
Add back TTL stuff
larseggert Nov 21, 2023
cd032fd
libc has been updated
larseggert Nov 23, 2023
b39b444
Rebase
larseggert Nov 27, 2023
0427a4a
Add changelog entry
larseggert Nov 27, 2023
f2dd35c
Fix syntax
larseggert Nov 27, 2023
4f93a8e
Add tests
larseggert Nov 27, 2023
101f048
Patch libc again until the release drops
larseggert Nov 27, 2023
743ed07
Undo libc patch
larseggert Dec 4, 2023
7593d31
Fix tests
larseggert Dec 4, 2023
b095f33
Fix warnings
larseggert Dec 4, 2023
74dacfb
More fixes
larseggert Dec 4, 2023
5fcea6f
More fixes
larseggert Dec 4, 2023
cc012f1
Test fixes
larseggert Dec 5, 2023
6c29437
Fix
larseggert Dec 5, 2023
e075a66
TTL not on FreeBSD
larseggert Dec 5, 2023
21d18fb
Not FreeBSD
larseggert Dec 5, 2023
750f729
Don't test on FreeBSD
larseggert Dec 5, 2023
8867665
Address code review comments
larseggert Dec 12, 2023
7e79ab2
Merge branch 'master' into feat-tos
larseggert Dec 13, 2023
0281496
Fix merge
larseggert Dec 13, 2023
dac97d3
Fix failures
larseggert Dec 13, 2023
f2a3488
Fix nits
larseggert Dec 13, 2023
083cf3a
Fix FreeBSD
larseggert Dec 13, 2023
43104a9
Try and fix MIPS
larseggert Dec 13, 2023
00c42ab
Try and fix MIPS again
larseggert Dec 13, 2023
90e0e0e
Try and debug MIPS failure
larseggert Dec 13, 2023
1d2c708
Temporarily disable IP_TTL to see if that's the issue
larseggert Dec 13, 2023
13d259a
Ignore MIPS failures
larseggert Dec 13, 2023
53494f6
Linux fix
larseggert Dec 18, 2023
dd3cdcb
Add changelog for IpDontFrag and Ipv6DontFrag.
larseggert Jan 9, 2024
6dfe0f8
Address code review
larseggert Jan 9, 2024
fb85b13
Undo FreeBSD
larseggert Jan 9, 2024
98a03c5
Address review feedback
larseggert Jan 9, 2024
2cecf06
Don't test test_ttl_opts on apple_targets
larseggert Jan 9, 2024
2e22157
Re-enable apple_targets for test_ttl_opts
larseggert Jan 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog/2203.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Added support for getting/setting IPv4 type-of-service (TOS) information via
`nix::sys::socket::sockopt::IpRecvTos` and `nix::sys::socket::sockopt::IpTos`.

Added support for getting/setting IPv6 traffic class information via
`nix::sys::socket::sockopt::Ipv6RecvTClass` and
`nix::sys::socket::sockopt::Ipv6TClass`.

Added support for getting/setting IPv4 time-to-live (TTL) information via
`nix::sys::socket::sockopt::IpRecvTtl` and `nix::sys::socket::sockopt::IpTtl`.

Added support for getting/setting IPv6 hop limit information via
`nix::sys::socket::sockopt::Ipv6RecvHopLimit` and
`nix::sys::socket::sockopt::Ipv6HopLimit`.
204 changes: 204 additions & 0 deletions src/sys/socket/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,38 @@ pub enum ControlMessageOwned {
#[cfg(any(target_os = "linux"))]
TlsGetRecordType(TlsGetRecordType),

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
IpTos(libc::c_int),
larseggert marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
Ipv6TClass(libc::c_int),

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
IpTtl(libc::c_int),

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
Ipv6HopLimit(libc::c_int),
larseggert marked this conversation as resolved.
Show resolved Hide resolved

/// Catch-all variant for unimplemented cmsg types.
#[doc(hidden)]
Unknown(UnknownCmsg),
Expand Down Expand Up @@ -982,6 +1014,53 @@ impl ControlMessageOwned {
let content_type = unsafe { ptr::read_unaligned(p as *const u8) };
ControlMessageOwned::TlsGetRecordType(content_type.into())
},
#[cfg(any(
apple_targets,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
(libc::IPPROTO_IP, libc::IP_RECVTOS) => {
let tos = unsafe { ptr::read_unaligned(p as *const u8) };
ControlMessageOwned::IpTos(tos as libc::c_int)
},
#[cfg(linux_android)]
#[cfg(feature = "net")]
(libc::IPPROTO_IP, libc::IP_TOS) => {
let tos = unsafe { ptr::read_unaligned(p as *const u8) };
ControlMessageOwned::IpTos(tos as libc::c_int)
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
(libc::IPPROTO_IPV6, libc::IPV6_TCLASS) => {
let tclass = unsafe { ptr::read_unaligned(p as *const libc::c_int) };
ControlMessageOwned::Ipv6TClass(tclass)
},
#[cfg(any(apple_targets))]
#[cfg(feature = "net")]
(libc::IPPROTO_IP, libc::IP_RECVTTL) => {
let ttl = unsafe { ptr::read_unaligned(p as *const libc::c_int) };
ControlMessageOwned::IpTtl(ttl)
},
#[cfg(linux_android)]
#[cfg(feature = "net")]
(libc::IPPROTO_IP, libc::IP_TTL) => {
let ttl = unsafe { ptr::read_unaligned(p as *const libc::c_int) };
ControlMessageOwned::IpTtl(ttl)
},
larseggert marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
(libc::IPPROTO_IPV6, libc::IPV6_HOPLIMIT) => {
let hop_limit = unsafe { ptr::read_unaligned(p as *const libc::c_int) };
ControlMessageOwned::Ipv6HopLimit(hop_limit)
},
(_, _) => {
let sl = unsafe { std::slice::from_raw_parts(p, len) };
let ucmsg = UnknownCmsg(*header, Vec::<u8>::from(sl));
Expand Down Expand Up @@ -1147,6 +1226,34 @@ pub enum ControlMessage<'a> {
/// page.
#[cfg(target_os = "linux")]
TxTime(&'a u64),

#[cfg(any(
apple_targets,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
IpTos(&'a libc::c_int),
#[cfg(any(
linux_android,
))]
#[cfg(feature = "net")]
IpTos(&'a u8),

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
Ipv6TClass(&'a libc::c_int),

#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
IpTtl(&'a libc::c_int),
}

// An opaque structure used to prevent cmsghdr from being a public type
Expand Down Expand Up @@ -1251,6 +1358,34 @@ impl<'a> ControlMessage<'a> {
ControlMessage::TxTime(tx_time) => {
tx_time as *const _ as *const u8
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
#[allow(clippy::unnecessary_cast)]
ControlMessage::IpTos(tos) => {
tos as *const _ as *const u8
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::Ipv6TClass(tclass) => {
tclass as *const _ as *const u8
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTtl(ttl) => {
ttl as *const _ as *const u8
},
};
unsafe {
ptr::copy_nonoverlapping(
Expand Down Expand Up @@ -1315,6 +1450,33 @@ impl<'a> ControlMessage<'a> {
ControlMessage::TxTime(tx_time) => {
mem::size_of_val(tx_time)
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTos(tos) => {
mem::size_of_val(tos)
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::Ipv6TClass(tclass) => {
mem::size_of_val(tclass)
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTtl(ttl) => {
mem::size_of_val(ttl)
},
}
}

Expand Down Expand Up @@ -1349,6 +1511,27 @@ impl<'a> ControlMessage<'a> {
ControlMessage::RxqOvfl(_) => libc::SOL_SOCKET,
#[cfg(target_os = "linux")]
ControlMessage::TxTime(_) => libc::SOL_SOCKET,
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTos(_) => libc::IPPROTO_IP,
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::Ipv6TClass(_) => libc::IPPROTO_IPV6,
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTtl(_) => libc::IPPROTO_IP,
}
}

Expand Down Expand Up @@ -1398,6 +1581,27 @@ impl<'a> ControlMessage<'a> {
ControlMessage::TxTime(_) => {
libc::SCM_TXTIME
},
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTos(_) => libc::IP_TOS,
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::Ipv6TClass(_) => libc::IPV6_TCLASS,
#[cfg(any(
apple_targets,
linux_android,
target_os = "freebsd",
))]
#[cfg(feature = "net")]
ControlMessage::IpTtl(_) => libc::IP_TTL,
}
}

Expand Down
Loading