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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ fs-set-times = "0.20.0"
system-interface = { version = "0.26.0", features = ["cap_std_impls"] }
io-lifetimes = { version = "2.0.2", default-features = false }
io-extras = "0.18.0"
rustix = "0.38.8"
rustix = "0.38.21"
is-terminal = "0.4.0"
# wit-bindgen:
wit-bindgen = { version = "0.13.0", default-features = false }
Expand Down
41 changes: 41 additions & 0 deletions crates/test-programs/src/bin/preview2_udp_connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use test_programs::wasi::sockets::network::{
ErrorCode, IpAddress, IpAddressFamily, IpSocketAddress, Network,
};
use test_programs::wasi::sockets::udp::UdpSocket;

fn test_udp_connect_disconnect_reconnect(net: &Network, family: IpAddressFamily) {
let unspecified_addr = IpSocketAddress::new(IpAddress::new_unspecified(family), 0);
let remote1 = IpSocketAddress::new(IpAddress::new_loopback(family), 4321);
let remote2 = IpSocketAddress::new(IpAddress::new_loopback(family), 4320);

let client = UdpSocket::new(family).unwrap();
client.blocking_bind(&net, unspecified_addr).unwrap();

_ = client.stream(None).unwrap();
assert_eq!(client.remote_address(), Err(ErrorCode::InvalidState));

_ = client.stream(None).unwrap();
assert_eq!(client.remote_address(), Err(ErrorCode::InvalidState));

_ = client.stream(Some(remote1)).unwrap();
assert_eq!(client.remote_address(), Ok(remote1));

_ = client.stream(Some(remote1)).unwrap();
assert_eq!(client.remote_address(), Ok(remote1));

_ = client.stream(Some(remote2)).unwrap();
assert_eq!(client.remote_address(), Ok(remote2));

_ = client.stream(None).unwrap();
assert_eq!(client.remote_address(), Err(ErrorCode::InvalidState));

_ = client.stream(Some(remote1)).unwrap();
assert_eq!(client.remote_address(), Ok(remote1));
}

fn main() {
let net = Network::default();

test_udp_connect_disconnect_reconnect(&net, IpAddressFamily::Ipv4);
test_udp_connect_disconnect_reconnect(&net, IpAddressFamily::Ipv6);
}
23 changes: 19 additions & 4 deletions crates/wasi/src/preview2/host/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,19 @@ impl<T: WasiView> udp::HostUdpSocket for T {
_ => 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 let UdpState::Connected = socket.udp_state {
// FIXME: Allow multiple (dis)connects. This needs to be supported by rustix first.
// rustix::net::disconnect(socket.udp_socket())?;
// socket.udp_state = UdpState::Bound;
return Err(ErrorCode::NotSupported.into());
disconnect(socket.udp_socket())?;
socket.udp_state = UdpState::Bound;
}

// Step #2: (Re)connect
if let Some(connect_addr) = remote_address {
rustix::net::connect(socket.udp_socket(), &connect_addr)?;
socket.udp_state = UdpState::Connected;
Expand Down Expand Up @@ -481,3 +487,12 @@ impl Subscribe for OutgoingDatagramStream {
}
}
}

fn disconnect<Fd: rustix::fd::AsFd>(sockfd: Fd) -> rustix::io::Result<()> {
match rustix::net::connect_unspec(sockfd) {
// BSD platforms return an error even if the socket was disconnected successfully.
#[cfg(target_os = "macos")]
Err(rustix::io::Errno::INVAL | rustix::io::Errno::AFNOSUPPORT) => Ok(()),
r => r,
}
}
4 changes: 4 additions & 0 deletions crates/wasi/tests/all/async_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ async fn preview2_tcp_bind() {
run(PREVIEW2_TCP_BIND_COMPONENT, false).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn preview2_udp_connect() {
run(PREVIEW2_UDP_CONNECT_COMPONENT, false).await.unwrap()
}
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn preview2_udp_sample_application() {
run(PREVIEW2_UDP_SAMPLE_APPLICATION_COMPONENT, false)
.await
Expand Down
4 changes: 4 additions & 0 deletions crates/wasi/tests/all/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ fn preview2_tcp_bind() {
run(PREVIEW2_TCP_BIND_COMPONENT, false).unwrap()
}
#[test_log::test]
fn preview2_udp_connect() {
run(PREVIEW2_UDP_CONNECT_COMPONENT, false).unwrap()
}
#[test_log::test]
fn preview2_udp_sample_application() {
run(PREVIEW2_UDP_SAMPLE_APPLICATION_COMPONENT, false).unwrap()
}
Expand Down