From 78a927449c87225a4d811f8c1a419584e45e5109 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 31 Oct 2026 20:52:27 +0100 Subject: libp2p: ignored ranges aren't told --- src/transport/libp2p/behaviour.rs | 7 ++- src/transport/libp2p/ignore.rs | 101 +++++++++++++++++++++++++++++++++++++- src/transport/libp2p/mod.rs | 5 +- 3 files changed, 108 insertions(+), 5 deletions(-) (limited to 'src/transport/libp2p') diff --git a/src/transport/libp2p/behaviour.rs b/src/transport/libp2p/behaviour.rs index 4ced88a..e9b156c 100644 --- a/src/transport/libp2p/behaviour.rs +++ b/src/transport/libp2p/behaviour.rs @@ -11,7 +11,7 @@ pub(super) struct Behaviour { /// First: connections it denies need nothing from the others. pub(super) ignoring: Ignoring, pub(super) stream: libp2p_stream::Behaviour, - pub(super) identify: ::libp2p::identify::Behaviour, + pub(super) identify: Identifying, pub(super) relay_client: ::libp2p::relay::client::Behaviour, /// Only with `Libp2pConfig::relay`. pub(super) relay: ::libp2p::swarm::behaviour::toggle::Toggle<::libp2p::relay::Behaviour>, @@ -271,7 +271,10 @@ impl Behaviour { ranges: ignored.clone(), }, stream: libp2p_stream::Behaviour::new(), - identify: ::libp2p::identify::Behaviour::new(identify), + identify: Identifying { + inner: ::libp2p::identify::Behaviour::new(identify), + ranges: ignored.clone(), + }, relay_client, relay: relay.into(), dcutr: HolePunching::new(peer_id, ignored), diff --git a/src/transport/libp2p/ignore.rs b/src/transport/libp2p/ignore.rs index b14235e..659ec91 100644 --- a/src/transport/libp2p/ignore.rs +++ b/src/transport/libp2p/ignore.rs @@ -1,5 +1,6 @@ //! `--libp2p-ignore`: address ranges neither published nor dialled, nor connected over -//! (`Ignoring`, a behaviour that denies connections there). +//! (`Ignoring`, a behaviour that denies connections there), nor told peers by identify +//! (`Identifying`). use super::*; @@ -144,3 +145,101 @@ impl ::libp2p::swarm::NetworkBehaviour for Ignoring { std::task::Poll::Pending } } + +/// libp2p's identify, not told our listen addresses in the ranges `libp2p-ignore` names: +/// peers would remember them, show them, and dial them in vain. +pub(super) struct Identifying { + pub(super) inner: ::libp2p::identify::Behaviour, + pub(super) ranges: Vec, +} + +impl ::libp2p::swarm::NetworkBehaviour for Identifying { + type ConnectionHandler = + <::libp2p::identify::Behaviour as ::libp2p::swarm::NetworkBehaviour>::ConnectionHandler; + type ToSwarm = ::libp2p::identify::Event; + + fn handle_pending_inbound_connection( + &mut self, + connection_id: ConnectionId, + local_addr: &Multiaddr, + remote_addr: &Multiaddr, + ) -> Result<(), ::libp2p::swarm::ConnectionDenied> { + self.inner + .handle_pending_inbound_connection(connection_id, local_addr, remote_addr) + } + + fn handle_established_inbound_connection( + &mut self, + connection_id: ConnectionId, + peer: PeerId, + local_addr: &Multiaddr, + remote_addr: &Multiaddr, + ) -> Result<::libp2p::swarm::THandler, ::libp2p::swarm::ConnectionDenied> { + self.inner.handle_established_inbound_connection( + connection_id, + peer, + local_addr, + remote_addr, + ) + } + + fn handle_pending_outbound_connection( + &mut self, + connection_id: ConnectionId, + maybe_peer: Option, + addresses: &[Multiaddr], + effective_role: ::libp2p::core::Endpoint, + ) -> Result, ::libp2p::swarm::ConnectionDenied> { + self.inner.handle_pending_outbound_connection( + connection_id, + maybe_peer, + addresses, + effective_role, + ) + } + + fn handle_established_outbound_connection( + &mut self, + connection_id: ConnectionId, + peer: PeerId, + addr: &Multiaddr, + role_override: ::libp2p::core::Endpoint, + port_use: ::libp2p::core::transport::PortUse, + ) -> Result<::libp2p::swarm::THandler, ::libp2p::swarm::ConnectionDenied> { + self.inner.handle_established_outbound_connection( + connection_id, + peer, + addr, + role_override, + port_use, + ) + } + + fn on_swarm_event(&mut self, event: ::libp2p::swarm::FromSwarm) { + use ::libp2p::swarm::FromSwarm; + match event { + FromSwarm::NewListenAddr(new) if ignored(&self.ranges, new.addr) => {} + FromSwarm::ExpiredListenAddr(expired) if ignored(&self.ranges, expired.addr) => {} + _ => self.inner.on_swarm_event(event), + } + } + + fn on_connection_handler_event( + &mut self, + peer_id: PeerId, + connection_id: ConnectionId, + event: ::libp2p::swarm::THandlerOutEvent, + ) { + self.inner + .on_connection_handler_event(peer_id, connection_id, event) + } + + fn poll( + &mut self, + cx: &mut std::task::Context<'_>, + ) -> std::task::Poll< + ::libp2p::swarm::ToSwarm>, + > { + self.inner.poll(cx) + } +} diff --git a/src/transport/libp2p/mod.rs b/src/transport/libp2p/mod.rs index b2a3da7..f1006e3 100644 --- a/src/transport/libp2p/mod.rs +++ b/src/transport/libp2p/mod.rs @@ -43,8 +43,9 @@ //! AutoNAT asks connected peers (nodes of the swarm) to dial it back. Once AutoNAT says //! it can be dialled, it lets the relays it picked go. //! -//! With `--libp2p-ignore`, address ranges are left out (VPNs): not published, not -//! offered for hole punching, not dialled, and connections there refused (`Ignoring`). +//! With `--libp2p-ignore`, address ranges are left out (VPNs): not published, not told +//! peers by identify (`Identifying`), not offered for hole punching, not dialled, and +//! connections there refused (`Ignoring`). //! //! With `--libp2p-upnp`, a node asks its router to forward its listen ports (UPnP), and //! publishes the router's public address for them with its own (`Book::direct`): then -- cgit v1.3.1