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

transports/tcp: only translate tcp addresses #2970

Merged
merged 6 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 4 additions & 0 deletions transports/tcp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@

- Remove default features. If you previously depended on `async-std` you need to enable this explicitly now. See [PR 2918].

- Return `None` in `GenTcpTransport::address_translation` if listen- or observed address are not tcp addresses.
See [PR 2970].

[PR 2813]: https://github.com/libp2p/rust-libp2p/pull/2813
[PR 2918]: https://github.com/libp2p/rust-libp2p/pull/2918
[PR 2970]: https://github.com/libp2p/rust-libp2p/pull/2970

# 0.36.0

Expand Down
60 changes: 60 additions & 0 deletions transports/tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ where
/// `None` is returned if one of the given addresses is not a TCP/IP
/// address.
fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if !is_tcp_addr(listen) || !is_tcp_addr(observed) {
return None;
}
match &self.port_reuse {
PortReuse::Disabled => address_translation(listen, observed),
PortReuse::Enabled { .. } => Some(observed.clone()),
Expand Down Expand Up @@ -829,13 +832,31 @@ fn ip_to_multiaddr(ip: IpAddr, port: u16) -> Multiaddr {
Multiaddr::empty().with(ip.into()).with(Protocol::Tcp(port))
}

fn is_tcp_addr(addr: &Multiaddr) -> bool {
use Protocol::*;

let mut iter = addr.iter();

let first = match iter.next() {
None => return false,
Some(p) => p,
};
let second = match iter.next() {
None => return false,
Some(p) => p,
};

matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) && matches!(second, Tcp(_))
}

#[cfg(test)]
mod tests {
use super::*;
use futures::{
channel::{mpsc, oneshot},
future::poll_fn,
};
use libp2p_core::PeerId;

#[test]
fn multiaddr_to_tcp_conversion() {
Expand Down Expand Up @@ -1240,4 +1261,43 @@ mod tests {

test("/ip4/127.0.0.1/tcp/12345/tcp/12345".parse().unwrap());
}

#[cfg(any(feature = "async-io", feature = "tcp"))]
#[test]
fn test_address_translation() {
#[cfg(feature = "async-io")]
let transport = TcpTransport::new(GenTcpConfig::new());
#[cfg(all(feature = "tokio", not(feature = "async-io")))]
let transport = TokioTcpTransport::new(GenTcpConfig::new());

let port = 42;
let tcp_listen_addr = Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.with(Protocol::Tcp(port));
Comment on lines +1274 to +1276
Copy link
Contributor

Choose a reason for hiding this comment

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

I always find these easier to read if we parse them from a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer using types where possible, avoiding any unwraps that would be needed when parsing from string :)

let observed_ip = Ipv4Addr::new(123, 45, 67, 8);
let tcp_observed_addr = Multiaddr::empty()
.with(Protocol::Ip4(observed_ip))
.with(Protocol::Tcp(1))
.with(Protocol::P2p(PeerId::random().into()));

let translated = transport
.address_translation(&tcp_listen_addr, &tcp_observed_addr)
.unwrap();
let mut iter = translated.iter();
assert_eq!(iter.next(), Some(Protocol::Ip4(observed_ip)));
assert_eq!(iter.next(), Some(Protocol::Tcp(port)));
assert_eq!(iter.next(), None);

let quic_addr = Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::new(87, 65, 43, 21)))
.with(Protocol::Udp(1))
.with(Protocol::Quic);

assert!(transport
.address_translation(&tcp_listen_addr, &quic_addr)
.is_none());
assert!(transport
.address_translation(&quic_addr, &tcp_observed_addr)
.is_none());
}
}