Skip to content

Commit

Permalink
fix(quic): fix address translation
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed Nov 19, 2023
1 parent 0ceb658 commit 9b6ca0e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ libp2p-perf = { version = "0.3.0", path = "protocols/perf" }
libp2p-ping = { version = "0.44.0", path = "protocols/ping" }
libp2p-plaintext = { version = "0.41.0", path = "transports/plaintext" }
libp2p-pnet = { version = "0.24.0", path = "transports/pnet" }
libp2p-quic = { version = "0.10.1", path = "transports/quic" }
libp2p-quic = { version = "0.10.2", path = "transports/quic" }
libp2p-relay = { version = "0.17.1", path = "protocols/relay" }
libp2p-rendezvous = { version = "0.14.0", path = "protocols/rendezvous" }
libp2p-request-response = { version = "0.26.0", path = "protocols/request-response" }
Expand Down
5 changes: 5 additions & 0 deletions transports/quic/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.10.2 - unreleased

- Fix address translation.
See [PR 4896](https://github.com/libp2p/rust-libp2p/pull/4896).

## 0.10.1

- Allow disabling path MTU discovery.
Expand Down
2 changes: 1 addition & 1 deletion transports/quic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libp2p-quic"
version = "0.10.1"
version = "0.10.2"
authors = ["Parity Technologies <[email protected]>"]
edition = "2021"
rust-version = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion transports/quic/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use futures::{prelude::*, stream::SelectAll};
use if_watch::IfEvent;

use libp2p_core::{
address_translation,
multiaddr::{Multiaddr, Protocol},
transport::{ListenerId, TransportError, TransportEvent},
Transport,
Expand Down Expand Up @@ -253,7 +254,7 @@ impl<P: Provider> Transport for GenTransport<P> {
{
return None;
}
Some(observed.clone())
address_translation(listen, observed)
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
Expand Down
52 changes: 52 additions & 0 deletions transports/quic/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use quic::Provider;
use rand::RngCore;
use std::future::Future;
use std::io;
use std::net::Ipv4Addr;
use std::num::NonZeroU8;
use std::task::Poll;
use std::time::Duration;
Expand Down Expand Up @@ -236,6 +237,57 @@ fn new_tcp_quic_transport() -> (PeerId, Boxed<(PeerId, StreamMuxerBox)>) {
(peer_id, transport)
}

#[cfg(feature = "async-std")]
#[test]
fn test_address_translation_async_std() {
test_address_translation::<quic::async_std::Provider>()
}

#[cfg(feature = "tokio")]
#[test]
fn test_address_translation_tokio() {
test_address_translation::<quic::tokio::Provider>()
}

fn test_address_translation<T>()
where
T: Provider,
{
let (_peer_id, transport) = create_default_transport::<T>();

let port = 42;
let quic_listen_addr = Multiaddr::empty()
.with(Protocol::Ip4(Ipv4Addr::new(127, 0, 0, 1)))
.with(Protocol::Udp(port))
.with(Protocol::QuicV1);
let observed_ip = Ipv4Addr::new(123, 45, 67, 8);
let quic_observed_addr = Multiaddr::empty()
.with(Protocol::Ip4(observed_ip))
.with(Protocol::Udp(1))
.with(Protocol::QuicV1)
.with(Protocol::P2p(PeerId::random()));

let translated = transport
.address_translation(&quic_listen_addr, &quic_observed_addr)
.unwrap();
let mut iter = translated.iter();
assert_eq!(iter.next(), Some(Protocol::Ip4(observed_ip)));
assert_eq!(iter.next(), Some(Protocol::Udp(port)));
assert_eq!(iter.next(), Some(Protocol::QuicV1));
assert_eq!(iter.next(), None);

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

assert!(transport
.address_translation(&quic_listen_addr, &tcp_addr)
.is_none());
assert!(transport
.address_translation(&tcp_addr, &quic_observed_addr)
.is_none());
}

#[cfg(feature = "async-std")]
#[async_std::test]
async fn tcp_and_quic() {
Expand Down

0 comments on commit 9b6ca0e

Please sign in to comment.