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

fix(swarm): don't emit NewExternalAddrCandidate on clients #4886

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions swarm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 0.44.1

- Do not generate `NewExternalAddrCandidate` events if there are no listeners.
See [PR 4886](https://github.com/libp2p/rust-libp2p/pull/4886).
- Implement `Clone` & `Copy` for `FromSwarm.
This makes it easier to forward these events when wrapping other behaviours.
See [PR 4825](https://github.com/libp2p/rust-libp2p/pull/4825).
Expand Down
10 changes: 7 additions & 3 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,13 +1118,17 @@ 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() {
tracing::debug!(%addr, "Discarding external address candidate because we have no listeners");
return;
nazar-pc marked this conversation as resolved.
Show resolved Hide resolved
}

// 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();

Expand Down
Loading