fix(swarm): external address candidate only after address translation

Pull-Request: #4158.
This commit is contained in:
Benno 2023-07-10 01:03:11 +02:00 committed by GitHub
parent b6b8844123
commit b4c7dfca63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 12 deletions

2
Cargo.lock generated
View File

@ -3104,7 +3104,7 @@ dependencies = [
[[package]]
name = "libp2p-swarm"
version = "0.43.0"
version = "0.43.1"
dependencies = [
"async-std",
"either",

View File

@ -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" }

View File

@ -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].

View File

@ -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 <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"

View File

@ -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) => {