From 8659c50949366a77591825f66f1d2ea48663a543 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Fri, 17 Nov 2023 12:56:50 +0200 Subject: [PATCH 1/3] fix(swarm): do not generate `NewExternalAddrCandidate` events if there are no listeners --- Cargo.lock | 2 +- Cargo.toml | 2 +- swarm/CHANGELOG.md | 5 +++++ swarm/Cargo.toml | 2 +- swarm/src/lib.rs | 9 ++++++--- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 332764c36f3..3128321b495 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3095,7 +3095,7 @@ dependencies = [ [[package]] name = "libp2p-swarm" -version = "0.44.0" +version = "0.44.1" dependencies = [ "async-std", "either", diff --git a/Cargo.toml b/Cargo.toml index 35439a1a696..519dfae4df6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -99,7 +99,7 @@ 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" } libp2p-server = { version = "0.12.4", path = "misc/server" } -libp2p-swarm = { version = "0.44.0", path = "swarm" } +libp2p-swarm = { version = "0.44.1", path = "swarm" } libp2p-swarm-derive = { version = "=0.34.0", path = "swarm-derive" } # `libp2p-swarm-derive` may not be compatible with different `libp2p-swarm` non-breaking releases. E.g. `libp2p-swarm` might introduce a new enum variant `FromSwarm` (which is `#[non-exhaustive]`) in a non-breaking release. Older versions of `libp2p-swarm-derive` would not forward this enum variant within the `NetworkBehaviour` hierarchy. Thus the version pinning is required. libp2p-swarm-test = { version = "0.3.0", path = "swarm-test" } libp2p-tcp = { version = "0.41.0", path = "transports/tcp" } diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index 48cafee6ced..c467a4c08ad 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.44.1 - unreleased + +- Do not generate `NewExternalAddrCandidate` events if there are no listeners. + See [PR 4886](https://github.com/libp2p/rust-libp2p/pull/4886). + ## 0.44.0 - Add `#[non_exhaustive]` to `FromSwarm`, `ToSwarm`, `SwarmEvent`, `ConnectionHandlerEvent`, `ConnectionEvent`. diff --git a/swarm/Cargo.toml b/swarm/Cargo.toml index 3b706df6d2b..817e4b4855e 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.44.0" +version = "0.44.1" authors = ["Parity Technologies "] license = "MIT" repository = "https://github.com/libp2p/rust-libp2p" diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 0354f39cfdc..4b03d38bec2 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1118,13 +1118,16 @@ where self.pending_handler_event = Some((peer_id, handler, event)); } ToSwarm::NewExternalAddrCandidate(addr) => { + // Unless we have listeners, all candidates are ephemeral and are not reachable externally + if self.listeners().next().is_none() { + return; + } + // 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 - .values() - .flatten() + .listeners() .filter_map(|server| self.transport.address_translation(server, &addr)) .collect(); From 1220007b8bdaf93034ec98614cf45236699aab95 Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Tue, 21 Nov 2023 09:00:21 +1100 Subject: [PATCH 2/3] Update swarm/CHANGELOG.md --- swarm/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/swarm/CHANGELOG.md b/swarm/CHANGELOG.md index c467a4c08ad..96bee29d2df 100644 --- a/swarm/CHANGELOG.md +++ b/swarm/CHANGELOG.md @@ -1,4 +1,4 @@ -## 0.44.1 - unreleased +## 0.44.1 - Do not generate `NewExternalAddrCandidate` events if there are no listeners. See [PR 4886](https://github.com/libp2p/rust-libp2p/pull/4886). From 62109e99bc16656ee810a880b414f7c23c2a6a13 Mon Sep 17 00:00:00 2001 From: Nazar Mokrynskyi Date: Tue, 21 Nov 2023 00:04:30 +0200 Subject: [PATCH 3/3] Add log messages about skipped external address candidate --- swarm/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 4b03d38bec2..cc5759f10a4 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -1120,6 +1120,7 @@ where ToSwarm::NewExternalAddrCandidate(addr) => { // Unless we have listeners, all candidates are ephemeral and are not reachable externally if self.listeners().next().is_none() { + tracing::debug!(%addr, "Discarding external address candidate because we have no listeners"); return; }