Skip to content
Open
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 src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ pub fn matches(bind: SocketAddr, dst: SocketAddr) -> bool {
/// Returns true if loopback is supported between two addresses, or
/// if the IPs are the same (in which case turmoil treats it like loopback)
pub(crate) fn is_same(src: SocketAddr, dst: SocketAddr) -> bool {
dst.ip().is_loopback() || src.ip() == dst.ip()
dst.ip().is_loopback() || dst.ip().is_unspecified() || src.ip() == dst.ip()
}

#[cfg(test)]
Expand Down
5 changes: 1 addition & 4 deletions src/net/tcp/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,9 @@ impl TcpListener {
let host = world.current_host_mut();

let mut my_addr = self.local_addr;
if origin.ip().is_loopback() {
if origin.ip().is_loopback() || origin.ip().is_unspecified() {
my_addr.set_ip(origin.ip());
}
if my_addr.ip().is_unspecified() {
my_addr.set_ip(host.addr);
}

let pair = SocketPair::new(my_addr, origin);
let rx = host.tcp.new_stream(pair);
Expand Down
2 changes: 1 addition & 1 deletion src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl TcpStream {

let host = world.current_host_mut();
let mut local_addr = SocketAddr::new(host.addr, host.assign_ephemeral_port());
if dst.ip().is_loopback() {
if dst.ip().is_loopback() || dst.ip().is_unspecified() {
local_addr.set_ip(dst.ip());
}

Expand Down
2 changes: 1 addition & 1 deletion src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl UdpSocket {

fn send(&self, world: &mut World, dst: SocketAddr, packet: Datagram) -> Result<()> {
let mut src = self.local_addr;
if dst.ip().is_loopback() {
if dst.ip().is_loopback() || dst.ip().is_unspecified() {
src.set_ip(dst.ip());
}
if src.ip().is_unspecified() {
Expand Down
39 changes: 39 additions & 0 deletions tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1374,3 +1374,42 @@ fn try_write() -> Result {

sim.run()
}

#[test]
fn connect_0000() -> Result {
init_tracing();

let mut sim = Builder::new().build();
sim.client("client", async move {
let listener = TcpListener::bind((Ipv4Addr::UNSPECIFIED, 1234))
.await
.unwrap();

tokio::spawn(async move {
let (socket, _) = listener.accept().await.unwrap();

let written = socket.try_write(b"hello!").unwrap();
assert_eq!(written, 6);
});

let mut socket = TcpStream::connect((Ipv4Addr::UNSPECIFIED, 1234))
.await
.unwrap();
let mut buf: [u8; 6] = [0; 6];
socket.read_exact(&mut buf).await?;
assert_eq!(&buf, b"hello!");

Ok(())
});

sim.run()
}

fn init_tracing() {
use tracing_subscriber::filter::EnvFilter;

let _ = tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_test_writer()
.try_init();
}
Loading