Skip to content

Commit

Permalink
Set sock nonblocking after connect
Browse files Browse the repository at this point in the history
  • Loading branch information
dlon committed Nov 19, 2023
1 parent 80039ac commit 33bba9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion test/test-manager/src/tests/settings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::helpers;
use super::helpers::{connect_and_wait, disconnect_and_wait, get_tunnel_state, send_guest_probes};
use super::helpers::{connect_and_wait, get_tunnel_state, send_guest_probes};
use super::{Error, TestContext};
use crate::assert_tunnel_state;
use crate::vm::network::DUMMY_LAN_INTERFACE_IP;
Expand Down
4 changes: 2 additions & 2 deletions test/test-manager/src/tests/tunnel_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::helpers::{
self, connect_and_wait, disconnect_and_wait, get_tunnel_state, send_guest_probes,
set_relay_settings, unreachable_wireguard_tunnel, wait_for_tunnel_state,
self, connect_and_wait, get_tunnel_state, send_guest_probes, set_relay_settings,
unreachable_wireguard_tunnel, wait_for_tunnel_state,
};
use super::{ui, Error, TestContext};
use crate::assert_tunnel_state;
Expand Down
50 changes: 27 additions & 23 deletions test/test-runner/src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ pub async fn send_tcp(
test_rpc::Error::SendTcp
})?;

sock.set_nonblocking(true).map_err(|error| {
log::error!("Failed to set non-blocking TCP socket: {error}");
test_rpc::Error::SendTcp
})?;

if let Some(iface) = bind_interface {
#[cfg(target_os = "macos")]
let interface_index = unsafe {
Expand Down Expand Up @@ -59,24 +54,33 @@ pub async fn send_tcp(
log::trace!("Bind interface {iface} is ignored on Windows")
}

sock.bind(&SockAddr::from(bind_addr)).map_err(|error| {
log::error!("Failed to bind TCP socket to {bind_addr}: {error}");
test_rpc::Error::SendTcp
})?;

log::debug!("Connecting from {bind_addr} to {destination}/TCP");

sock.connect(&SockAddr::from(destination))
.map_err(|error| {
log::error!("Failed to connect to {destination}: {error}");
let mut stream = tokio::task::spawn_blocking(move || {
sock.bind(&SockAddr::from(bind_addr)).map_err(|error| {
log::error!("Failed to bind TCP socket to {bind_addr}: {error}");
test_rpc::Error::SendTcp
})?;

let std_stream = std::net::TcpStream::from(sock);
let mut stream = TcpStream::from_std(std_stream).map_err(|error| {
log::error!("Failed to convert to TCP stream to tokio stream: {error}");
test_rpc::Error::SendTcp
})?;
sock.connect(&SockAddr::from(destination))
.map_err(|error| {
log::error!("Failed to connect to {destination}: {error}");
test_rpc::Error::SendTcp
})?;

sock.set_nonblocking(true).map_err(|error| {
log::error!("Failed to set non-blocking TCP socket: {error}");
test_rpc::Error::SendTcp
})?;

let std_stream = std::net::TcpStream::from(sock);
TcpStream::from_std(std_stream).map_err(|error| {
log::error!("Failed to convert to TCP stream to tokio stream: {error}");
test_rpc::Error::SendTcp
})
})
.await
.unwrap()?;

stream.write_all(b"hello").await.map_err(|error| {
log::error!("Failed to send message to {destination}: {error}");
Expand All @@ -101,11 +105,6 @@ pub async fn send_udp(
test_rpc::Error::SendUdp
})?;

sock.set_nonblocking(true).map_err(|error| {
log::error!("Failed to set non-blocking UDP socket: {error}");
test_rpc::Error::SendUdp
})?;

if let Some(iface) = bind_interface {
#[cfg(target_os = "macos")]
let interface_index = unsafe {
Expand Down Expand Up @@ -139,6 +138,11 @@ pub async fn send_udp(
test_rpc::Error::SendUdp
})?;

sock.set_nonblocking(true).map_err(|error| {
log::error!("Failed to set non-blocking UDP socket: {error}");
test_rpc::Error::SendUdp
})?;

log::debug!("Send message from {bind_addr} to {destination}/UDP");

let std_socket = std::net::UdpSocket::from(sock);
Expand Down

0 comments on commit 33bba9e

Please sign in to comment.