Skip to content

Commit c033328

Browse files
committed
Expand ECN support
libc's CMSG macros got built out to many more platforms. There may remain some obscure platforms where this will still fail to build, but we can account for them as they show up in practice.
1 parent 53143ad commit c033328

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

quinn/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ untrusted = "0.6.2"
3535
webpki = "0.19"
3636
webpki-roots = "0.16"
3737
ct-logs = "0.5"
38-
libc = "0.2.46"
38+
libc = "0.2.49"
3939
mio = "0.6"
4040

4141
[dev-dependencies]

quinn/src/platform/cmsg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ impl<'a> Encoder<'a> {
3636
assert!(mem::align_of::<T>() <= mem::align_of::<libc::cmsghdr>());
3737
let space = unsafe { libc::CMSG_SPACE(mem::size_of_val(&value) as _) as usize };
3838
assert!(
39-
self.hdr.msg_controllen >= self.len + space,
39+
self.hdr.msg_controllen as usize >= self.len + space,
4040
"control message buffer too small"
4141
);
4242
let cmsg = self.cmsg.take().expect("no control buffer space remaining");
@@ -60,7 +60,7 @@ impl<'a> Encoder<'a> {
6060
// by `sendmsg`.
6161
impl<'a> Drop for Encoder<'a> {
6262
fn drop(&mut self) {
63-
self.hdr.msg_controllen = self.len;
63+
self.hdr.msg_controllen = self.len as _;
6464
}
6565
}
6666

quinn/src/platform/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
use quinn_proto::EcnCodepoint;
33
use std::{io, net::SocketAddr};
44

5-
// The Linux code should work for most unixes, but as of this writing nobody's ported the
6-
// CMSG_... macros to the libc crate for any of the BSDs.
7-
#[cfg(target_os = "linux")]
5+
#[cfg(unix)]
86
mod cmsg;
9-
#[cfg(target_os = "linux")]
10-
mod linux;
7+
#[cfg(unix)]
8+
mod unix;
119

1210
// No ECN support
13-
#[cfg(not(target_os = "linux"))]
11+
#[cfg(not(unix))]
1412
mod fallback;
1513

1614
pub trait UdpExt {

quinn/src/platform/linux.rs renamed to quinn/src/platform/unix.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,25 @@ impl super::UdpExt for UdpSocket {
2222
mem::size_of::<SocketAddrV6>(),
2323
mem::size_of::<libc::sockaddr_in6>()
2424
);
25-
assert_eq!(CMSG_LEN, unsafe {
26-
libc::CMSG_SPACE(mem::size_of::<libc::c_int>() as _) as usize
27-
});
25+
assert!(
26+
CMSG_LEN >= unsafe { libc::CMSG_SPACE(mem::size_of::<libc::c_int>() as _) as usize }
27+
);
2828
assert!(
2929
mem::align_of::<libc::cmsghdr>() <= mem::align_of::<cmsg::Aligned<[u8; 0]>>(),
3030
"control message buffers will be misaligned"
3131
);
3232

3333
let addr = self.local_addr()?;
3434

35-
if addr.is_ipv4() || !self.only_v6()? {
35+
if addr.is_ipv4() || (!cfg!(target_os = "macos") && !self.only_v6()?) {
36+
let on: libc::c_int = 1;
3637
let rc = unsafe {
3738
libc::setsockopt(
3839
self.as_raw_fd(),
3940
libc::IPPROTO_IP,
4041
libc::IP_RECVTOS,
41-
&true as *const _ as _,
42-
1,
42+
&on as *const _ as _,
43+
mem::size_of_val(&on) as _,
4344
)
4445
};
4546
if rc == -1 {
@@ -54,7 +55,7 @@ impl super::UdpExt for UdpSocket {
5455
libc::IPPROTO_IPV6,
5556
libc::IPV6_RECVTCLASS,
5657
&on as *const _ as _,
57-
mem::size_of::<libc::c_int>() as _,
58+
mem::size_of_val(&on) as _,
5859
)
5960
};
6061
if rc == -1 {

quinn/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn echo_v4() {
2525
}
2626

2727
#[test]
28-
#[cfg(target_os = "linux")] // Dual-stack sockets aren't the default anywhere else.
28+
//#[cfg(target_os = "linux")] // Dual-stack sockets aren't the default anywhere else.
2929
fn echo_dualstack() {
3030
run_echo(
3131
SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), 0),

0 commit comments

Comments
 (0)