Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Test that local address actually changes (#12595)

Dave Bakkerf6418005cd28
diff --git a/crates/test-programs/src/bin/p3_sockets_udp_connect.rs b/crates/test-programs/src/bin/p3_sockets_udp_connect.rs
index ebb03ed395..dd46b7ff08 100644
--- a/crates/test-programs/src/bin/p3_sockets_udp_connect.rs
+++ b/crates/test-programs/src/bin/p3_sockets_udp_connect.rs
@@ -1,12 +1,18 @@
-use test_programs::p3::wasi::sockets::types::{
- ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, UdpSocket,
+use test_programs::{
+ p3::wasi::sockets::types::{
+ ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Ipv4Address, Ipv6Address, UdpSocket,
+ },
+ sockets::supports_ipv6,
};
struct Component;
test_programs::p3::export!(Component);
-const SOME_PORT: u16 = 47; // If the tests pass, this will never actually be connected to.
+// If the tests work as expected, these will never actually be connected to:
+const SOME_PORT: u16 = 47;
+const SOME_PUBLIC_IPV4: Ipv4Address = (123, 234, 12, 34);
+const SOME_PUBLIC_IPV6: Ipv6Address = (123, 234, 0, 0, 0, 0, 0, 34);
fn test_udp_connect_disconnect_reconnect(family: IpAddressFamily) {
let remote1 = IpSocketAddress::new(IpAddress::new_loopback(family), 4321);
@@ -48,6 +54,38 @@ fn test_udp_connect_unspec(family: IpAddressFamily) {
));
}
+/// If not explicitly bound, connecting a UDP socket should update the local
+/// address to reflect the best network path.
+fn test_udp_connect_local_address_change(family: IpAddressFamily) {
+ fn connect(sock: &UdpSocket, ip: IpAddress, port: u16) -> IpSocketAddress {
+ let remote = IpSocketAddress::new(ip, port);
+ sock.connect(remote).unwrap();
+ let local = sock.get_local_address().unwrap();
+ println!("connect({remote:?}) changed local address to: {local:?}",);
+ local
+ }
+
+ if !has_public_interface(family) {
+ println!("No public interface detected, skipping test");
+ return;
+ }
+
+ let loopback_ip = IpAddress::new_loopback(family);
+ let public_ip = some_public_ip(family);
+
+ let client = UdpSocket::create(family).unwrap();
+
+ let loopback_if1 = connect(&client, loopback_ip, 4321);
+ let loopback_if2 = connect(&client, loopback_ip, 4322);
+ let public_if = connect(&client, public_ip, 4323);
+
+ // Note: these assertions are based on observed behavior on Linux, MacOS and
+ // Windows, but there is nothing in their official documentation to
+ // corroborate this.
+ assert_eq!(loopback_if1, loopback_if2);
+ assert_ne!(loopback_if1, public_if);
+}
+
/// 0 is not a valid remote port.
fn test_udp_connect_port_0(family: IpAddressFamily) {
let addr = IpSocketAddress::new(IpAddress::new_loopback(family), 0);
@@ -131,26 +169,38 @@ fn test_udp_connect_dual_stack() {
impl test_programs::p3::exports::wasi::cli::run::Guest for Component {
async fn run() -> Result<(), ()> {
test_udp_connect_disconnect_reconnect(IpAddressFamily::Ipv4);
- test_udp_connect_disconnect_reconnect(IpAddressFamily::Ipv6);
-
test_udp_connect_unspec(IpAddressFamily::Ipv4);
- test_udp_connect_unspec(IpAddressFamily::Ipv6);
-
+ test_udp_connect_local_address_change(IpAddressFamily::Ipv4);
test_udp_connect_port_0(IpAddressFamily::Ipv4);
- test_udp_connect_port_0(IpAddressFamily::Ipv6);
-
test_udp_connect_wrong_family(IpAddressFamily::Ipv4);
- test_udp_connect_wrong_family(IpAddressFamily::Ipv6);
-
test_udp_connect_without_bind(IpAddressFamily::Ipv4);
- test_udp_connect_without_bind(IpAddressFamily::Ipv6);
-
test_udp_connect_with_bind(IpAddressFamily::Ipv4);
- test_udp_connect_with_bind(IpAddressFamily::Ipv6);
- test_udp_connect_dual_stack();
+ if supports_ipv6() {
+ test_udp_connect_disconnect_reconnect(IpAddressFamily::Ipv6);
+ test_udp_connect_unspec(IpAddressFamily::Ipv6);
+ test_udp_connect_local_address_change(IpAddressFamily::Ipv6);
+ test_udp_connect_port_0(IpAddressFamily::Ipv6);
+ test_udp_connect_wrong_family(IpAddressFamily::Ipv6);
+ test_udp_connect_without_bind(IpAddressFamily::Ipv6);
+ test_udp_connect_with_bind(IpAddressFamily::Ipv6);
+ test_udp_connect_dual_stack();
+ }
Ok(())
}
}
+fn some_public_ip(family: IpAddressFamily) -> IpAddress {
+ match family {
+ IpAddressFamily::Ipv4 => IpAddress::Ipv4(SOME_PUBLIC_IPV4),
+ IpAddressFamily::Ipv6 => IpAddress::Ipv6(SOME_PUBLIC_IPV6),
+ }
+}
+
+fn has_public_interface(family: IpAddressFamily) -> bool {
+ let sock = UdpSocket::create(family).unwrap();
+ sock.connect(IpSocketAddress::new(some_public_ip(family), SOME_PORT))
+ .is_ok()
+}
+
fn main() {}
diff --git a/crates/wasi/src/p2/host/udp.rs b/crates/wasi/src/p2/host/udp.rs
index 1ef8350399..3c3c9f4c7f 100644
--- a/crates/wasi/src/p2/host/udp.rs
+++ b/crates/wasi/src/p2/host/udp.rs
@@ -64,24 +64,14 @@ impl udp::HostUdpSocket for WasiSocketsCtxView<'_> {
return Err(ErrorCode::InvalidState.into());
}
- // We disconnect & (re)connect in two distinct steps for two reasons:
- // - To leave our socket instance in a consistent state in case the
- // connect fails.
- // - When reconnecting to a different address, Linux sometimes fails
- // if there isn't a disconnect in between.
-
- // Step #1: Disconnect
- if socket.is_connected() {
- socket.disconnect()?;
- }
-
- // Step #2: (Re)connect
if let Some(connect_addr) = remote_address {
let Some(check) = socket.socket_addr_check() else {
return Err(ErrorCode::InvalidState.into());
};
check.check(connect_addr, SocketAddrUse::UdpConnect).await?;
socket.connect_p2(connect_addr)?;
+ } else if socket.is_connected() {
+ socket.disconnect()?;
}
let incoming_stream = IncomingDatagramStream {
diff --git a/crates/wasi/src/sockets/udp.rs b/crates/wasi/src/sockets/udp.rs
index 482c6aa090..49cc6ece49 100644
--- a/crates/wasi/src/sockets/udp.rs
+++ b/crates/wasi/src/sockets/udp.rs
@@ -2,14 +2,13 @@ use crate::runtime::with_ambient_tokio_runtime;
use crate::sockets::util::{
ErrorCode, get_unicast_hop_limit, is_valid_address_family, is_valid_remote_address,
receive_buffer_size, send_buffer_size, set_receive_buffer_size, set_send_buffer_size,
- set_unicast_hop_limit, udp_bind, udp_disconnect, udp_socket,
+ set_unicast_hop_limit, udp_bind, udp_connect, udp_disconnect, udp_socket,
};
use crate::sockets::{SocketAddrCheck, SocketAddressFamily, WasiSocketsCtx};
use cap_net_ext::AddressFamily;
use io_lifetimes::AsSocketlike as _;
use io_lifetimes::raw::{FromRawSocketlike as _, IntoRawSocketlike as _};
use rustix::io::Errno;
-use rustix::net::connect;
use std::net::SocketAddr;
use std::sync::Arc;
use tracing::debug;
@@ -155,28 +154,26 @@ impl UdpSocket {
return Err(ErrorCode::InvalidArgument);
}
- // We disconnect & (re)connect in two distinct steps for two reasons:
- // - To leave our socket instance in a consistent state in case the
- // connect fails.
- // - When reconnecting to a different address, Linux sometimes fails
- // if there isn't a disconnect in between.
+ match udp_connect(&self.socket, addr) {
+ Ok(()) => {
+ self.udp_state = UdpState::Connected(addr);
+ Ok(())
+ }
+ Err(e) => {
+ // Revert to a consistent state:
+ _ = udp_disconnect(&self.socket);
+ self.udp_state = UdpState::Bound;
- // Step #1: Disconnect
- if let UdpState::Connected(..) = self.udp_state {
- udp_disconnect(&self.socket)?;
- self.udp_state = UdpState::Bound;
- }
- // Step #2: (Re)connect
- connect(&self.socket, &addr).map_err(|error| match error {
- Errno::AFNOSUPPORT => ErrorCode::InvalidArgument, // See `udp_bind` implementation.
- Errno::INPROGRESS => {
- debug!("UDP connect returned EINPROGRESS, which should never happen");
- ErrorCode::Unknown
+ Err(match e {
+ Errno::AFNOSUPPORT => ErrorCode::InvalidArgument, // See `udp_bind` implementation.
+ Errno::INPROGRESS => {
+ debug!("UDP connect returned EINPROGRESS, which should never happen");
+ ErrorCode::Unknown
+ }
+ err => err.into(),
+ })
}
- err => err.into(),
- })?;
- self.udp_state = UdpState::Connected(addr);
- Ok(())
+ }
}
/// Send data using p3 semantics. (with implicit bind)
diff --git a/crates/wasi/src/sockets/util.rs b/crates/wasi/src/sockets/util.rs
index 9623403807..5ef0534b6f 100644
--- a/crates/wasi/src/sockets/util.rs
+++ b/crates/wasi/src/sockets/util.rs
@@ -6,7 +6,7 @@ use core::time::Duration;
use cap_net_ext::{AddressFamily, Blocking, UdpSocketExt};
use rustix::fd::AsFd;
use rustix::io::Errno;
-use rustix::net::{bind, connect_unspec, sockopt};
+use rustix::net::{bind, connect, connect_unspec, sockopt};
use tracing::debug;
use crate::sockets::SocketAddressFamily;
@@ -396,7 +396,27 @@ pub fn udp_bind(sockfd: impl AsFd, addr: SocketAddr) -> Result<(), ErrorCode> {
})
}
-pub fn udp_disconnect(sockfd: impl AsFd) -> Result<(), ErrorCode> {
+pub fn udp_connect(sockfd: impl AsFd, addr: SocketAddr) -> Result<(), Errno> {
+ match connect(sockfd.as_fd(), &addr) {
+ // When connecting a UDP socket, the OS looks up the best route to the
+ // remote address and selects an appropriate outgoing interface.
+ // If the new destination routes through an interface different than the
+ // previously selected interface, most operating systems will
+ // automatically update the socket's local address to match that route.
+ //
+ // Linux however doesn't do that automatically and we manually
+ // dissolve the existing association and then connect again to the
+ // new destination.
+ #[cfg(target_os = "linux")]
+ Err(Errno::INVAL) => {
+ _ = udp_disconnect(sockfd.as_fd());
+ return connect(sockfd.as_fd(), &addr);
+ }
+ r => r,
+ }
+}
+
+pub fn udp_disconnect(sockfd: impl AsFd) -> Result<(), Errno> {
match connect_unspec(sockfd) {
// BSD platforms return an error even if the UDP socket was disconnected successfully.
//
@@ -411,8 +431,7 @@ pub fn udp_disconnect(sockfd: impl AsFd) -> Result<(), ErrorCode> {
// address family of the socket.
#[cfg(target_os = "macos")]
Err(Errno::INVAL | Errno::AFNOSUPPORT) => Ok(()),
- Err(err) => Err(err.into()),
- Ok(()) => Ok(()),
+ r => r,
}
}