Skip to content

Commit

Permalink
swarm: translate external address candidate
Browse files Browse the repository at this point in the history
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 libp2p#4153
  • Loading branch information
b-zee committed Jul 6, 2023
1 parent 7308221 commit 0582c9b
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -1083,6 +1077,15 @@ where
addrs.dedup();
addrs
};

// 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 },
));
}

for addr in translated_addresses {
self.behaviour
.on_swarm_event(FromSwarm::NewExternalAddrCandidate(
Expand Down

0 comments on commit 0582c9b

Please sign in to comment.