Skip to content

Commit

Permalink
fix relayed dial not performed if local peer listen on same relay
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Oct 24, 2024
1 parent f5f2241 commit bac825e
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ pub use handler::{
ConnectionHandler, ConnectionHandlerEvent, ConnectionHandlerSelect, OneShotHandler,
OneShotHandlerConfig, StreamUpgradeError, SubstreamProtocol,
};
use libp2p_core::multiaddr::Protocol;
#[cfg(feature = "macros")]
pub use libp2p_swarm_derive::NetworkBehaviour;
pub use listen_opts::ListenOpts;
Expand Down Expand Up @@ -509,10 +510,25 @@ where
}
}

let mut unique_addresses = HashSet::new();
let mut unique_addresses: HashSet<_> = HashSet::new();
addresses_from_opts.retain(|addr| {
!self.listened_addrs.values().flatten().any(|a| a == addr)
&& unique_addresses.insert(addr.clone())
if !unique_addresses.insert(addr.clone()) {
// Address already added, don't dial it twice.
false
} else {
// Check the address against the local peer listen addresses
// to prevent the local peer of dialing himself.

if addr.iter().any(|p| p == Protocol::P2pCircuit) {
// Address is relayed. It's ok to dial a peer that listens
// on the same relay that the local peer does.
true
} else {
// Address is not relayed. Prevent dial on any addresses
// the local peer listens on.
!self.listened_addrs.values().flatten().any(|a| a == addr)
}
}
});

if addresses_from_opts.is_empty() {
Expand Down

0 comments on commit bac825e

Please sign in to comment.