diff --git a/misc/allow-block-list/src/lib.rs b/misc/allow-block-list/src/lib.rs index eed79d740a12..516e2ee0d079 100644 --- a/misc/allow-block-list/src/lib.rs +++ b/misc/allow-block-list/src/lib.rs @@ -233,19 +233,20 @@ where fn on_swarm_event(&mut self, event: FromSwarm) { match event { - FromSwarm::ConnectionClosed(_) => {} - FromSwarm::ConnectionEstablished(_) => {} - FromSwarm::AddressChange(_) => {} - FromSwarm::DialFailure(_) => {} - FromSwarm::ListenFailure(_) => {} - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + FromSwarm::ConnectionClosed(_) + | FromSwarm::ConnectionEstablished(_) + | FromSwarm::AddressChange(_) + | FromSwarm::DialFailure(_) + | FromSwarm::ListenFailure(_) + | FromSwarm::NewListener(_) + | FromSwarm::NewListenAddr(_) + | FromSwarm::ExpiredListenAddr(_) + | FromSwarm::ListenerError(_) + | FromSwarm::ListenerClosed(_) + | FromSwarm::NewExternalAddrCandidate(_) + | FromSwarm::ExternalAddrExpired(_) + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/misc/connection-limits/src/lib.rs b/misc/connection-limits/src/lib.rs index e4723dd95c61..f7860b60071d 100644 --- a/misc/connection-limits/src/lib.rs +++ b/misc/connection-limits/src/lib.rs @@ -344,14 +344,15 @@ impl NetworkBehaviour for Behaviour { FromSwarm::ListenFailure(ListenFailure { connection_id, .. }) => { self.pending_inbound_connections.remove(&connection_id); } - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + FromSwarm::NewListener(_) + | FromSwarm::NewListenAddr(_) + | FromSwarm::ExpiredListenAddr(_) + | FromSwarm::ListenerError(_) + | FromSwarm::ListenerClosed(_) + | FromSwarm::NewExternalAddrCandidate(_) + | FromSwarm::ExternalAddrExpired(_) + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/protocols/autonat/src/behaviour.rs b/protocols/autonat/src/behaviour.rs index 439543f8318d..11fe698499d4 100644 --- a/protocols/autonat/src/behaviour.rs +++ b/protocols/autonat/src/behaviour.rs @@ -37,7 +37,7 @@ use libp2p_request_response::{ use libp2p_swarm::{ behaviour::{ AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredListenAddr, - ExternalAddrExpired, FromSwarm, + ExternalAddrExpired, FromSwarm, NewExternalAddrOfPeer, }, ConnectionDenied, ConnectionId, ListenAddresses, NetworkBehaviour, NewExternalAddrCandidate, PollParameters, THandler, THandlerInEvent, THandlerOutEvent, ToSwarm, @@ -589,6 +589,9 @@ impl NetworkBehaviour for Behaviour { self.inner.on_swarm_event(listener_closed) } confirmed @ FromSwarm::ExternalAddrConfirmed(_) => self.inner.on_swarm_event(confirmed), + FromSwarm::NewExternalAddrOfPeer(NewExternalAddrOfPeer { addr }) => { + todo!(); + } } } diff --git a/protocols/dcutr/src/behaviour_impl.rs b/protocols/dcutr/src/behaviour_impl.rs index 4993da655d33..5b2e813df068 100644 --- a/protocols/dcutr/src/behaviour_impl.rs +++ b/protocols/dcutr/src/behaviour_impl.rs @@ -375,7 +375,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/protocols/floodsub/src/layer.rs b/protocols/floodsub/src/layer.rs index 29fe8ba250fc..a4874d8a259a 100644 --- a/protocols/floodsub/src/layer.rs +++ b/protocols/floodsub/src/layer.rs @@ -496,7 +496,8 @@ impl NetworkBehaviour for Floodsub { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/protocols/gossipsub/src/behaviour.rs b/protocols/gossipsub/src/behaviour.rs index 7ca9f6df9da4..f0b67b5b4162 100644 --- a/protocols/gossipsub/src/behaviour.rs +++ b/protocols/gossipsub/src/behaviour.rs @@ -3507,7 +3507,8 @@ where | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/protocols/identify/src/behaviour.rs b/protocols/identify/src/behaviour.rs index f572b937d386..2d03ac2fcc16 100644 --- a/protocols/identify/src/behaviour.rs +++ b/protocols/identify/src/behaviour.rs @@ -395,6 +395,7 @@ impl NetworkBehaviour for Behaviour { } } } + FromSwarm::NewExternalAddrOfPeer(_addr) => todo!(), FromSwarm::NewListenAddr(_) | FromSwarm::ExpiredListenAddr(_) | FromSwarm::AddressChange(_) diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 340fefb276af..e60411458b4d 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -2551,6 +2551,7 @@ where } FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), + FromSwarm::NewExternalAddrOfPeer(_addr) => todo!(), FromSwarm::ExpiredListenAddr(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::NewListenAddr(_) diff --git a/protocols/mdns/src/behaviour.rs b/protocols/mdns/src/behaviour.rs index bc102f832df9..d8ea2541f946 100644 --- a/protocols/mdns/src/behaviour.rs +++ b/protocols/mdns/src/behaviour.rs @@ -245,7 +245,8 @@ where | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/protocols/perf/src/client.rs b/protocols/perf/src/client.rs index 93c2086a49e9..49d3e273cac2 100644 --- a/protocols/perf/src/client.rs +++ b/protocols/perf/src/client.rs @@ -181,7 +181,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrConfirmed(_) - | FromSwarm::ExternalAddrExpired(_) => {} + | FromSwarm::ExternalAddrExpired(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} }; self.request_response.on_swarm_event(event); diff --git a/protocols/ping/src/lib.rs b/protocols/ping/src/lib.rs index d1c4a2facaf0..951482f12d80 100644 --- a/protocols/ping/src/lib.rs +++ b/protocols/ping/src/lib.rs @@ -167,7 +167,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/protocols/relay/src/behaviour.rs b/protocols/relay/src/behaviour.rs index 8fcfa103c4f8..921401bc384a 100644 --- a/protocols/relay/src/behaviour.rs +++ b/protocols/relay/src/behaviour.rs @@ -353,7 +353,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/protocols/relay/src/priv_client.rs b/protocols/relay/src/priv_client.rs index c3c80c5b504e..276234cfb938 100644 --- a/protocols/relay/src/priv_client.rs +++ b/protocols/relay/src/priv_client.rs @@ -231,7 +231,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/protocols/rendezvous/src/client.rs b/protocols/rendezvous/src/client.rs index 505635efda8a..4fb8ddbd03de 100644 --- a/protocols/rendezvous/src/client.rs +++ b/protocols/rendezvous/src/client.rs @@ -274,7 +274,8 @@ impl NetworkBehaviour for Behaviour { | ToSwarm::Dial { .. } | ToSwarm::CloseConnection { .. } | ToSwarm::ListenOn { .. } - | ToSwarm::RemoveListener { .. }), + | ToSwarm::RemoveListener { .. } + | ToSwarm::NewExternalAddrOfPeer { .. }), ) => { let new_to_swarm = other.map_out(|_| unreachable!("we manually map `GenerateEvent` variants")); diff --git a/protocols/rendezvous/src/server.rs b/protocols/rendezvous/src/server.rs index 44f2f97a6a09..8ded37247521 100644 --- a/protocols/rendezvous/src/server.rs +++ b/protocols/rendezvous/src/server.rs @@ -218,6 +218,7 @@ impl NetworkBehaviour for Behaviour { | ToSwarm::NewExternalAddrCandidate(_) | ToSwarm::ExternalAddrConfirmed(_) | ToSwarm::ExternalAddrExpired(_) + | ToSwarm::NewExternalAddrOfPeer { .. } | ToSwarm::CloseConnection { .. } => { let new_to_swarm = to_swarm .map_out(|_| unreachable!("we manually map `GenerateEvent` variants")); diff --git a/protocols/request-response/src/lib.rs b/protocols/request-response/src/lib.rs index 7b1a80884430..249702bedbfc 100644 --- a/protocols/request-response/src/lib.rs +++ b/protocols/request-response/src/lib.rs @@ -775,15 +775,16 @@ where } FromSwarm::AddressChange(address_change) => self.on_address_change(address_change), FromSwarm::DialFailure(dial_failure) => self.on_dial_failure(dial_failure), - FromSwarm::ListenFailure(_) => {} - FromSwarm::NewListener(_) => {} - FromSwarm::NewListenAddr(_) => {} - FromSwarm::ExpiredListenAddr(_) => {} - FromSwarm::ListenerError(_) => {} - FromSwarm::ListenerClosed(_) => {} - FromSwarm::NewExternalAddrCandidate(_) => {} - FromSwarm::ExternalAddrExpired(_) => {} - FromSwarm::ExternalAddrConfirmed(_) => {} + FromSwarm::ListenFailure(_) + | FromSwarm::NewListener(_) + | FromSwarm::NewListenAddr(_) + | FromSwarm::ExpiredListenAddr(_) + | FromSwarm::ListenerError(_) + | FromSwarm::ListenerClosed(_) + | FromSwarm::NewExternalAddrCandidate(_) + | FromSwarm::ExternalAddrExpired(_) + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/swarm-derive/src/lib.rs b/swarm-derive/src/lib.rs index e54cd058dafe..1f666da55d15 100644 --- a/swarm-derive/src/lib.rs +++ b/swarm-derive/src/lib.rs @@ -83,6 +83,7 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result syn::Result quote! { + self.#i.on_swarm_event(#from_swarm::NewExternalAddrOfPeer(#new_external_addr_of_peer { + addr, + })); + }, + None => quote! { + self.#field_n.on_swarm_event(#from_swarm::NewExternalAddrOfPeer(#new_external_addr_of_peer { + addr, + })); + }, + }) + }; + // Build the list of statements to put in the body of `on_swarm_event()` // for the `FromSwarm::ExternalAddrExpired` variant. let on_external_addr_expired_stmts = { @@ -757,6 +779,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result { return std::task::Poll::Ready(#network_behaviour_action::CloseConnection { peer_id, connection }); } + std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrOfPeer(addr)) => { + return std::task::Poll::Ready(#network_behaviour_action::NewExternalAddrOfPeer(addr)); + } std::task::Poll::Pending => {}, } } @@ -883,6 +908,9 @@ fn build_struct(ast: &DeriveInput, data_struct: &DataStruct) -> syn::Result { #(#on_listener_closed_stmts)* } + #from_swarm::NewExternalAddrOfPeer( + #new_external_addr_of_peer {addr}) + => { #(#on_new_external_addr_of_peer_stmts)* } _ => {} } } diff --git a/swarm/src/behaviour.rs b/swarm/src/behaviour.rs index 0ecdf7b31f04..abf507b29763 100644 --- a/swarm/src/behaviour.rs +++ b/swarm/src/behaviour.rs @@ -320,6 +320,10 @@ pub enum ToSwarm { /// Whether to close a specific or all connections to the given peer. connection: CloseConnection, }, + + /// TODO: more documentation needed + /// Reports external address of a remote peer to other `NetworkBehaviours` + NewExternalAddrOfPeer(Multiaddr), } impl ToSwarm { @@ -352,6 +356,7 @@ impl ToSwarm { ToSwarm::NewExternalAddrCandidate(addr) => ToSwarm::NewExternalAddrCandidate(addr), ToSwarm::ExternalAddrConfirmed(addr) => ToSwarm::ExternalAddrConfirmed(addr), ToSwarm::ExternalAddrExpired(addr) => ToSwarm::ExternalAddrExpired(addr), + ToSwarm::NewExternalAddrOfPeer(addr) => ToSwarm::NewExternalAddrOfPeer(addr), } } } @@ -383,6 +388,7 @@ impl ToSwarm { peer_id, connection, }, + ToSwarm::NewExternalAddrOfPeer(addr) => ToSwarm::NewExternalAddrOfPeer(addr), } } } @@ -447,6 +453,8 @@ pub enum FromSwarm<'a, Handler> { ExternalAddrConfirmed(ExternalAddrConfirmed<'a>), /// Informs the behaviour that an external address of the local node expired, i.e. is no-longer confirmed. ExternalAddrExpired(ExternalAddrExpired<'a>), + /// TODO: document it + NewExternalAddrOfPeer(NewExternalAddrOfPeer<'a>), } /// [`FromSwarm`] variant that informs the behaviour about a newly established connection to a peer. @@ -559,6 +567,12 @@ pub struct ExternalAddrExpired<'a> { pub addr: &'a Multiaddr, } +/// [`FromSwarm`] variant that informs the behaviour that a new external address was detected. +#[derive(Clone, Copy)] +pub struct NewExternalAddrOfPeer<'a> { + pub addr: &'a Multiaddr, +} + impl<'a, Handler> FromSwarm<'a, Handler> { fn map_handler( self, @@ -658,6 +672,7 @@ impl<'a, Handler> FromSwarm<'a, Handler> { FromSwarm::NewExternalAddrCandidate(e) => Some(FromSwarm::NewExternalAddrCandidate(e)), FromSwarm::ExternalAddrExpired(e) => Some(FromSwarm::ExternalAddrExpired(e)), FromSwarm::ExternalAddrConfirmed(e) => Some(FromSwarm::ExternalAddrConfirmed(e)), + FromSwarm::NewExternalAddrOfPeer(e) => Some(FromSwarm::NewExternalAddrOfPeer(e)), } } } diff --git a/swarm/src/dummy.rs b/swarm/src/dummy.rs index 6810abec591c..0d1bccbd5cfc 100644 --- a/swarm/src/dummy.rs +++ b/swarm/src/dummy.rs @@ -72,7 +72,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/swarm/src/keep_alive.rs b/swarm/src/keep_alive.rs index 05cbcdf7b8cf..7cc1cb99ec90 100644 --- a/swarm/src/keep_alive.rs +++ b/swarm/src/keep_alive.rs @@ -75,7 +75,8 @@ impl NetworkBehaviour for Behaviour { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } } diff --git a/swarm/src/lib.rs b/swarm/src/lib.rs index 867e49410c53..82d438287f01 100644 --- a/swarm/src/lib.rs +++ b/swarm/src/lib.rs @@ -85,6 +85,7 @@ pub mod derive_prelude { pub use crate::behaviour::ListenerClosed; pub use crate::behaviour::ListenerError; pub use crate::behaviour::NewExternalAddrCandidate; + pub use crate::behaviour::NewExternalAddrOfPeer; pub use crate::behaviour::NewListenAddr; pub use crate::behaviour::NewListener; pub use crate::connection::ConnectionId; @@ -1112,6 +1113,37 @@ where self.pool.disconnect(peer_id); } }, + ToSwarm::NewExternalAddrOfPeer(addr) => { + log::debug!("ToSwarm::NewExternalAddrOfPeer"); + + let translated_addresses = { + let mut addrs: Vec<_> = self + .listened_addrs + .values() + .flatten() + .filter_map(|server| self.transport.address_translation(server, &addr)) + .collect(); + + // remove duplicates + addrs.sort_unstable(); + addrs.dedup(); + addrs + }; + + 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 }, + )); + } + } + } } None diff --git a/swarm/src/test.rs b/swarm/src/test.rs index 6f39d56da910..55a944f5fdb5 100644 --- a/swarm/src/test.rs +++ b/swarm/src/test.rs @@ -132,7 +132,8 @@ where | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } diff --git a/swarm/tests/swarm_derive.rs b/swarm/tests/swarm_derive.rs index fa3f6c69dd0d..fc48260c732f 100644 --- a/swarm/tests/swarm_derive.rs +++ b/swarm/tests/swarm_derive.rs @@ -520,7 +520,8 @@ fn custom_out_event_no_type_parameters() { | FromSwarm::ListenerClosed(_) | FromSwarm::NewExternalAddrCandidate(_) | FromSwarm::ExternalAddrExpired(_) - | FromSwarm::ExternalAddrConfirmed(_) => {} + | FromSwarm::ExternalAddrConfirmed(_) + | FromSwarm::NewExternalAddrOfPeer(_) => {} } } }