Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions quinn-udp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ windows-sys = { workspace = true }
criterion = { version = "0.8", default-features = false, features = ["async_tokio"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "net"] }

[target.'cfg(target_vendor = "apple")'.dev-dependencies]
nix = { version = "0.31.3", default-features = false, features = ["net"] }

[build-dependencies]
cfg_aliases = { workspace = true }

Expand Down
12 changes: 7 additions & 5 deletions quinn-udp/src/apple_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ fn prepare_msg_x(
if let Some(ip) = &transmit.src_ip {
match ip {
IpAddr::V4(v4) => {
if encode_src_ip {
let addr = libc::in_addr {
let pktinfo = libc::in_pktinfo {
ipi_ifindex: 0,
ipi_spec_dst: libc::in_addr {
s_addr: u32::from_ne_bytes(v4.octets()),
};
encoder.push(libc::IPPROTO_IP, libc::IP_RECVDSTADDR, addr);
}
},
ipi_addr: libc::in_addr { s_addr: 0 },
};
encoder.push(libc::IPPROTO_IP, libc::IP_PKTINFO, pktinfo);
}
IpAddr::V6(v6) => {
let pktinfo = libc::in6_pktinfo {
Expand Down
6 changes: 3 additions & 3 deletions quinn-udp/src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ fn prepare_msg(
hdr: &mut libc::msghdr,
iov: &mut libc::iovec,
ctrl: &mut cmsg::Aligned<[u8; cmsg::LEN]>,
#[allow(unused_variables)] // only used on FreeBSD & macOS
#[allow(unused_variables)] // only used on BSD & Solaris
encode_src_ip: bool,
sendmsg_einval: bool,
) {
Expand Down Expand Up @@ -734,7 +734,7 @@ fn prepare_msg(
if let Some(ip) = &transmit.src_ip {
match ip {
IpAddr::V4(v4) => {
#[cfg(any(target_os = "linux", target_os = "android"))]
#[cfg(any(target_os = "linux", target_os = "android", apple))]
{
let pktinfo = libc::in_pktinfo {
ipi_ifindex: 0,
Expand All @@ -745,7 +745,7 @@ fn prepare_msg(
};
encoder.push(libc::IPPROTO_IP, libc::IP_PKTINFO, pktinfo);
}
#[cfg(any(bsd, apple, solarish))]
#[cfg(any(bsd, solarish))]
{
if encode_src_ip {
let addr = libc::in_addr {
Expand Down
53 changes: 53 additions & 0 deletions quinn-udp/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,59 @@ fn basic_src_ip() {
);
}

#[test]
#[cfg(apple)]
fn ipv4_source_address_override() {
use nix::{ifaddrs::getifaddrs, net::if_::InterfaceFlags};

let Some(src_ip) = getifaddrs()
.unwrap()
.filter(|interface| interface.flags.contains(InterfaceFlags::IFF_UP))
.filter_map(|interface| Some(interface.address?.as_sockaddr_in()?.ip()))
.find(|ip| {
*ip != Ipv4Addr::LOCALHOST
&& !ip.is_unspecified()
&& !ip.is_multicast()
&& !ip.is_broadcast()
})
else {
eprintln!("skipping source override test: no alternate assigned IPv4 address");
return;
};

let send = UdpSocket::bind((Ipv4Addr::UNSPECIFIED, 0)).unwrap();
let recv = UdpSocket::bind((Ipv4Addr::LOCALHOST, 0)).unwrap();
recv.set_read_timeout(Some(std::time::Duration::from_secs(3)))
.unwrap();
let send_state = UdpSocketState::new((&send).into()).unwrap();
#[cfg(apple_fast)]
// SAFETY: sendmsg_x is available on the macOS test host.
unsafe {
send_state.set_apple_fast_path();
}
UdpSockRef::from(&send).set_nonblocking(false).unwrap();

let contents = b"hello";
let sent = send_state
.try_send(
(&send).into(),
&Transmit {
destination: recv.local_addr().unwrap(),
ecn: None,
contents,
segment_size: None,
src_ip: Some(src_ip.into()),
},
)
.unwrap();
assert_eq!(sent.get(), 1);

let mut buf = [0; 32];
let (received, source) = recv.recv_from(&mut buf).unwrap();
assert_eq!(&buf[..received], contents);
assert_eq!(source.ip(), src_ip);
}

// The WASI backend sets no ECN codepoint and always reports `None`.
#[test]
#[cfg(not(target_os = "wasi"))]
Expand Down
Loading