Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend loopback workaround #140

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl TcpStream {
let rx = host.tcp.new_stream(pair);

let syn = Protocol::Tcp(Segment::Syn(Syn { ack }));
if !dst.ip().is_loopback() {
if !dst.ip().is_loopback() && dst.ip() != local_addr.ip() {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe introduce a function that can compare the host for 2 IPs.

world.send_message(local_addr, dst, syn)?;
} else {
send_loopback(local_addr, dst, syn);
Expand Down Expand Up @@ -270,7 +270,7 @@ impl WriteHalf {

fn send(&self, world: &mut World, segment: Segment) -> Result<()> {
let message = Protocol::Tcp(segment);
if self.pair.remote.ip().is_loopback() {
if self.pair.remote.ip().is_loopback() || self.pair.local.ip() == self.pair.remote.ip() {
send_loopback(self.pair.local, self.pair.remote, message);
} else {
world.send_message(self.pair.local, self.pair.remote, message)?;
Expand Down
2 changes: 1 addition & 1 deletion src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl UdpSocket {
src.set_ip(world.current_host_mut().addr);
}

if dst.ip().is_loopback() {
if dst.ip().is_loopback() || src.ip() == dst.ip() {
send_loopback(src, dst, msg);
} else {
world.send_message(src, dst, msg)?;
Expand Down
64 changes: 64 additions & 0 deletions tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,38 @@ fn loopback_to_localhost_v4() -> Result {
run_localhost_test(IpVersion::V4, bind_addr, connect_addr)
}

#[test]
fn loopback_wildcard_public_v4() -> Result {
let bind_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 1234);
let connect_addr = SocketAddr::from((Ipv4Addr::new(192, 168, 0, 1), 1234));
run_localhost_test(IpVersion::V4, bind_addr, connect_addr)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this verify the stream's addrs are set the same as what tokio::net types return? These need to behave the same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

run_localhost_test asserts:

assert_eq!(socket.local_addr()?.ip(), connect_addr.ip());
assert_eq!(socket.peer_addr()?.ip(), connect_addr.ip());

on both server and client.

}

#[test]
fn loopback_localhost_public_v4() -> Result {
let bind_addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 1234);
let connect_addr = SocketAddr::from((Ipv4Addr::new(192, 168, 0, 1), 1234));
let mut sim = Builder::new().ip_version(IpVersion::V4).build();
sim.client("client", async move {
let listener = TcpListener::bind(bind_addr).await?;

tokio::spawn(async move {
let (mut socket, socket_addr) = listener.accept().await.unwrap();
socket.write_all(&[0, 1, 3, 7, 8]).await.unwrap();

assert_eq!(socket_addr.ip(), connect_addr.ip());
assert_eq!(socket.local_addr().unwrap().ip(), connect_addr.ip());
assert_eq!(socket.peer_addr().unwrap().ip(), connect_addr.ip());
});

let res = TcpStream::connect(connect_addr).await;
assert_error_kind(res, io::ErrorKind::ConnectionRefused);

Ok(())
});
sim.run()
}

#[test]
fn loopback_to_wildcard_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 1234);
Expand All @@ -844,6 +876,38 @@ fn loopback_to_localhost_v6() -> Result {
run_localhost_test(IpVersion::V6, bind_addr, connect_addr)
}

#[test]
fn loopback_wildcard_public_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 1234);
let connect_addr = SocketAddr::from((Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1), 1234));
run_localhost_test(IpVersion::V6, bind_addr, connect_addr)
}

#[test]
fn loopback_localhost_public_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 1234);
let connect_addr = SocketAddr::from((Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1), 1234));
let mut sim = Builder::new().ip_version(IpVersion::V6).build();
sim.client("client", async move {
let listener = TcpListener::bind(bind_addr).await?;

tokio::spawn(async move {
let (mut socket, socket_addr) = listener.accept().await.unwrap();
socket.write_all(&[0, 1, 3, 7, 8]).await.unwrap();

assert_eq!(socket_addr.ip(), connect_addr.ip());
assert_eq!(socket.local_addr().unwrap().ip(), connect_addr.ip());
assert_eq!(socket.peer_addr().unwrap().ip(), connect_addr.ip());
});

let res = TcpStream::connect(connect_addr).await;
assert_error_kind(res, io::ErrorKind::ConnectionRefused);

Ok(())
});
sim.run()
}

#[test]
fn remote_to_localhost_refused() -> Result {
let mut sim = Builder::new().build();
Expand Down
74 changes: 74 additions & 0 deletions tests/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,43 @@ fn loopback_to_localhost_v4() -> Result {
run_localhost_test(IpVersion::V4, bind_addr, connect_addr)
}

#[test]
fn loopback_wildcard_public_v4() -> Result {
let bind_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), 1234);
let connect_addr = SocketAddr::from((Ipv4Addr::new(192, 168, 0, 1), 1234));
run_localhost_test(IpVersion::V4, bind_addr, connect_addr)
}

#[test]
fn loopback_localhost_public_v4() -> Result {
let bind_addr = SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 1234);
let connect_addr = SocketAddr::from((Ipv4Addr::new(192, 168, 0, 1), 1234));
let mut sim = Builder::new().ip_version(IpVersion::V4).build();
let expected = [0, 1, 7, 3, 8];
sim.client("client", async move {
let socket = UdpSocket::bind(bind_addr).await?;

tokio::spawn(async move {
let mut buf = [0; 5];
let (_, peer) = socket.recv_from(&mut buf).await.unwrap();

assert_eq!(expected, buf);
assert_eq!(peer.ip(), connect_addr.ip());
assert_eq!(socket.local_addr().unwrap().ip(), bind_addr.ip());

socket.send_to(&expected, peer).await.unwrap();
});

let bind_addr = SocketAddr::new(bind_addr.ip(), 0);
let socket = UdpSocket::bind(bind_addr).await?;
let res = socket.send_to(&expected, connect_addr).await;
assert_error_kind(res, io::ErrorKind::ConnectionRefused);

Ok(())
});
sim.run()
}

#[test]
fn loopback_to_wildcard_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 1234);
Expand All @@ -520,6 +557,43 @@ fn loopback_to_localhost_v6() -> Result {
run_localhost_test(IpVersion::V6, bind_addr, connect_addr)
}

#[test]
fn loopback_wildcard_public_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), 1234);
let connect_addr = SocketAddr::from((Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1), 1234));
run_localhost_test(IpVersion::V6, bind_addr, connect_addr)
}

#[test]
fn loopback_localhost_public_v6() -> Result {
let bind_addr = SocketAddr::new(Ipv6Addr::LOCALHOST.into(), 1234);
let connect_addr = SocketAddr::from((Ipv6Addr::new(0xfe80, 0, 0, 0, 0, 0, 0, 1), 1234));
let mut sim = Builder::new().ip_version(IpVersion::V6).build();
let expected = [0, 1, 7, 3, 8];
sim.client("client", async move {
let socket = UdpSocket::bind(bind_addr).await?;

tokio::spawn(async move {
let mut buf = [0; 5];
let (_, peer) = socket.recv_from(&mut buf).await.unwrap();

assert_eq!(expected, buf);
assert_eq!(peer.ip(), connect_addr.ip());
assert_eq!(socket.local_addr().unwrap().ip(), bind_addr.ip());

socket.send_to(&expected, peer).await.unwrap();
});

let bind_addr = SocketAddr::new(bind_addr.ip(), 0);
let socket = UdpSocket::bind(bind_addr).await?;
let res = socket.send_to(&expected, connect_addr).await;
assert_error_kind(res, io::ErrorKind::ConnectionRefused);

Ok(())
});
sim.run()
}

#[test]
fn remote_to_localhost_dropped() -> Result {
let mut sim = Builder::new().build();
Expand Down
Loading