From a18677fbf3bc94a4c68175402567eef08ef9d996 Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Wed, 5 Jul 2023 16:39:10 +0200 Subject: [PATCH 1/2] swarm: translate external address candidate The `NewExternalAddrCandidate` event is yielded both before and after address translation. This will cause, in the case of TCP, ephemeral ports to be added as candidate. In turn, that will cause protocols like AutoNAT to fail as these candidates are not actually reachable/external. We will now only yield the original candidate if translation did not apply. Fixes #4153 --- swarm/src/lib.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 9b739f33780..f6cae20af09 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1062,14 +1062,8 @@ where self.pending_event = Some((peer_id, handler, event)); } ToSwarm::NewExternalAddrCandidate(addr) => { - self.behaviour - .on_swarm_event(FromSwarm::NewExternalAddrCandidate( - NewExternalAddrCandidate { addr: &addr }, - )); - - // Generate more candidates based on address translation. + // Apply address translation to the candidate address. // For TCP without port-reuse, the observed address contains an ephemeral port which needs to be replaced by the port of a listen address. - let translated_addresses = { let mut addrs: Vec<_> = self .listened_addrs @@ -1083,11 +1077,20 @@ where addrs.dedup(); addrs }; - for addr in translated_addresses { + + // If address translation yielded nothing, broacast the original candidate address. + if translated_addresses.is_empty() { self.behaviour .on_swarm_event(FromSwarm::NewExternalAddrCandidate( NewExternalAddrCandidate { addr: &addr }, )); + } else { + for addr in translated_addresses { + self.behaviour + .on_swarm_event(FromSwarm::NewExternalAddrCandidate( + NewExternalAddrCandidate { addr: &addr }, + )); + } } } ToSwarm::ExternalAddrConfirmed(addr) => { From 21338e19b049987eec1dbe41c5a80ca9b280af59 Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Thu, 6 Jul 2023 11:52:17 +0200 Subject: [PATCH 2/2] chore: bump patch for swarm; CHANGELOG entry --- Cargo.lock | 2 +- Cargo.toml | 2 +- swarm/CHANGELOG.md | 9 ++++++++- swarm/Cargo.toml | 2 +- 4 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cef33101b5f..f4899ebe120 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3104,7 +3104,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.43.0" +version = "0.43.1" dependencies = [ "async-std", "either", diff --git a/Cargo.toml b/Cargo.toml index db54152709f..eff66e42986 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -87,7 +87,7 @@ libp2p-quic = { version = "0.8.0-alpha", path = "transports/quic" } libp2p-relay = { version = "0.16.0", path = "protocols/relay" } libp2p-rendezvous = { version = "0.13.0", path = "protocols/rendezvous" } libp2p-request-response = { version = "0.25.0", path = "protocols/request-response" } -libp2p-swarm = { version = "0.43.0", path = "swarm" } +libp2p-swarm = { version = "0.43.1", path = "swarm" } libp2p-swarm-derive = { version = "0.33.0", path = "swarm-derive" } libp2p-swarm-test = { version = "0.2.0", path = "swarm-test" } libp2p-tcp = { version = "0.40.0", path = "transports/tcp" } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index ab5be59c578..545a5236c72 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,4 +1,11 @@ -## 0.43.0 +## 0.43.1 - unreleased + +- Do not announce external address candidate before address translation, unless translation does not apply. + This will prevent ephemeral TCP addresses being announced as external address candidates. + See [PR 4158]. + + +## 0.43.0 - Allow `NetworkBehaviours` to create and remove listeners. See [PR 3292]. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index e09158fcc0f..e1f8f2513f9 100644 --- a/swarm/Cargo.toml +++ b/swarm/Cargo.toml @@ -3,7 +3,7 @@ name = "libp2p-swarm" edition = "2021" rust-version = { workspace = true } description = "The libp2p swarm" -version = "0.43.0" +version = "0.43.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p"