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
5 changes: 5 additions & 0 deletions src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ pub fn matches(bind: SocketAddr, dst: SocketAddr) -> bool {
bind == dst
}

/// Returns whether loopback is supported from src to dst
Copy link

Choose a reason for hiding this comment

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

Technically this is not a loopback. It ends up behaving like loopback because of how turmoil is routing to the local IP address. Mayeb is_same?

pub(crate) fn is_loopback(src: SocketAddr, dst: SocketAddr) -> bool {
dst.ip().is_loopback() || src.ip() == dst.ip()
}

#[cfg(test)]
mod test {
use crate::{Host, Result};
Expand Down
5 changes: 3 additions & 2 deletions src/net/tcp/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::{

use crate::{
envelope::{Envelope, Protocol, Segment, Syn},
host::is_loopback,
host::SequencedSegment,
net::SocketPair,
world::World,
Expand Down Expand Up @@ -74,7 +75,7 @@ impl TcpStream {
let rx = host.tcp.new_stream(pair);

let syn = Protocol::Tcp(Segment::Syn(Syn { ack }));
if !dst.ip().is_loopback() && dst.ip() != local_addr.ip() {
if !is_loopback(local_addr, dst) {
world.send_message(local_addr, dst, syn)?;
} else {
send_loopback(local_addr, dst, syn);
Expand Down Expand Up @@ -270,7 +271,7 @@ impl WriteHalf {

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

use crate::{
envelope::{Datagram, Envelope, Protocol},
host::is_loopback,
ToSocketAddrs, World, TRACING_TARGET,
};

Expand Down Expand Up @@ -291,7 +292,7 @@ impl UdpSocket {
src.set_ip(world.current_host_mut().addr);
}

if dst.ip().is_loopback() || src.ip() == dst.ip() {
if is_loopback(src, dst) {
send_loopback(src, dst, msg);
} else {
world.send_message(src, dst, msg)?;
Expand Down
4 changes: 3 additions & 1 deletion tests/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,9 @@ fn non_zero_bind() -> Result {
sim.client("client", async move {
let sock = TcpListener::bind("1.1.1.1:1").await;

let Err(err) = sock else { panic!("bind should have failed") };
let Err(err) = sock else {
panic!("bind should have failed")
};
assert_eq!(err.to_string(), "1.1.1.1:1 is not supported");
Ok(())
});
Expand Down
7 changes: 4 additions & 3 deletions tests/udp.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{
io::{self, ErrorKind},
matches,
net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
rc::Rc,
sync::{atomic::AtomicUsize, atomic::Ordering},
Expand Down Expand Up @@ -210,7 +209,7 @@ fn hold_and_release() -> Result {
send_ping(&sock).await?;

let res = timeout(Duration::from_secs(1), recv_pong(&sock)).await;
assert!(matches!(res, Err(_)));
assert!(res.is_err());

// resume the network. note that the client ping does not have to be
// resent.
Expand Down Expand Up @@ -406,7 +405,9 @@ fn non_zero_bind() -> Result {
sim.client("client", async move {
let sock = UdpSocket::bind("1.1.1.1:1").await;

let Err(err) = sock else { panic!("socket creation should have failed") };
let Err(err) = sock else {
panic!("socket creation should have failed")
};
assert_eq!(err.to_string(), "1.1.1.1:1 is not supported");
Ok(())
});
Expand Down
Loading