Skip to content

Commit

Permalink
removed Transport::address_translation and moved generic address_tran…
Browse files Browse the repository at this point in the history
…slation to libp2p_swarm
  • Loading branch information
stormshield-ebzh committed May 23, 2024
1 parent 4b16e42 commit 0f538f3
Show file tree
Hide file tree
Showing 31 changed files with 9 additions and 269 deletions.
7 changes: 0 additions & 7 deletions core/src/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,4 @@ where
},
}
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
match self {
Either::Left(a) => a.address_translation(server, observed),
Either::Right(b) => b.address_translation(server, observed),
}
}
}
3 changes: 0 additions & 3 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ mod proto {
pub use multiaddr;
pub type Negotiated<T> = multistream_select::Negotiated<T>;

mod translation;

pub mod connection;
pub mod either;
pub mod muxing;
Expand All @@ -61,7 +59,6 @@ pub use multihash;
pub use muxing::StreamMuxer;
pub use peer_record::PeerRecord;
pub use signed_envelope::SignedEnvelope;
pub use translation::address_translation;
pub use transport::Transport;
pub use upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo};

Expand Down
18 changes: 0 additions & 18 deletions core/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,24 +175,6 @@ pub trait Transport {
cx: &mut Context<'_>,
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>>;

/// Performs a transport-specific mapping of an address `observed` by a remote onto a
/// local `listen` address to yield an address for the local node that may be reachable
/// for other peers.
///
/// This is relevant for transports where Network Address Translation (NAT) can occur
/// so that e.g. the peer is observed at a different IP than the IP of the local
/// listening address. See also [`address_translation`][crate::address_translation].
///
/// Within [`libp2p::Swarm`](<https://docs.rs/libp2p/latest/libp2p/struct.Swarm.html>) this is
/// used when extending the listening addresses of the local peer with external addresses
/// observed by remote peers.
/// On transports where this is not relevant (i.e. no NATs are present) `None` should be
/// returned for the sake of de-duplication.
///
/// Note: if the listen or observed address is not a valid address of this transport,
/// `None` should be returned as well.
fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;

/// Boxes the transport, including custom transport errors.
fn boxed(self) -> boxed::Boxed<Self::Output>
where
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@ where
Ok(future)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
9 changes: 0 additions & 9 deletions core/src/transport/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ trait Abstract<O> {
addr: Multiaddr,
opts: DialOpts,
) -> Result<Dial<O>, TransportError<io::Error>>;
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -100,10 +99,6 @@ where
Ok(Box::pin(fut) as Dial<_>)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
Transport::address_translation(self, server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -151,10 +146,6 @@ impl<O> Transport for Boxed<O> {
self.inner.dial(addr, opts)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}

fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
8 changes: 0 additions & 8 deletions core/src/transport/choice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,6 @@ where
Err(TransportError::MultiaddrNotSupported(addr))
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if let Some(addr) = self.0.address_translation(server, observed) {
Some(addr)
} else {
self.1.address_translation(server, observed)
}
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/dummy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ impl<TOut> Transport for DummyTransport<TOut> {
Err(TransportError::MultiaddrNotSupported(addr))
}

fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}

fn poll(
self: Pin<&mut Self>,
_: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/global_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,6 @@ impl<T: crate::Transport + Unpin> crate::Transport for Transport<T> {
}
}

fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(listen, observed)
}

fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ where
})
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/map_err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ where
}
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,6 @@ impl Transport for MemoryTransport {
DialFuture::new(port).ok_or(TransportError::Other(MemoryTransportError::Unreachable))
}

fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}

fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
8 changes: 0 additions & 8 deletions core/src/transport/optional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ where
}
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if let Some(inner) = &self.0 {
inner.address_translation(server, observed)
} else {
None
}
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions core/src/transport/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ where
})
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
8 changes: 0 additions & 8 deletions core/src/transport/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,6 @@ where
self.0.listen_on(id, addr)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.0.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -431,10 +427,6 @@ where
.map_err(|err| err.map(TransportUpgradeError::Transport))
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
4 changes: 0 additions & 4 deletions misc/metrics/src/bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ where
})))
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
6 changes: 3 additions & 3 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ use crate::handler::{self, Handler, InEvent};
use crate::protocol::{Info, UpgradeError};
use libp2p_core::multiaddr::Protocol;
use libp2p_core::transport::PortUse;
use libp2p_core::{address_translation, multiaddr, ConnectedPoint, Endpoint, Multiaddr};
use libp2p_core::{multiaddr, ConnectedPoint, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_identity::PublicKey;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
use libp2p_swarm::{
ConnectionDenied, DialError, ExternalAddresses, ListenAddresses, NetworkBehaviour,
NotifyHandler, PeerAddresses, StreamUpgradeError, THandlerInEvent, ToSwarm,
address_translation, ConnectionDenied, DialError, ExternalAddresses, ListenAddresses,
NetworkBehaviour, NotifyHandler, PeerAddresses, StreamUpgradeError, THandlerInEvent, ToSwarm,
};
use libp2p_swarm::{ConnectionId, THandler, THandlerOutEvent};

Expand Down
7 changes: 3 additions & 4 deletions protocols/mdns/src/behaviour/iface/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use hickory_proto::{
op::Message,
rr::{Name, RData},
};
use libp2p_core::{
address_translation,
multiaddr::{Multiaddr, Protocol},
};
use libp2p_core::multiaddr::{Multiaddr, Protocol};
use libp2p_swarm::address_translation;

use libp2p_identity::PeerId;
use std::time::Instant;
use std::{fmt, net::SocketAddr, str, time::Duration};
Expand Down
4 changes: 0 additions & 4 deletions protocols/relay/src/priv_client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ impl libp2p_core::Transport for Transport {
.boxed())
}

fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}

fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down
2 changes: 2 additions & 0 deletions swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub mod dial_opts;
pub mod dummy;
pub mod handler;
mod listen_opts;
pub mod translation;

/// Bundles all symbols required for the [`libp2p_swarm_derive::NetworkBehaviour`] macro.
#[doc(hidden)]
Expand Down Expand Up @@ -153,6 +154,7 @@ use std::{
task::{Context, Poll},
};
use tracing::Instrument;
pub use translation::address_translation;

/// Event generated by the [`NetworkBehaviour`] that the swarm will report back.
type TBehaviourOutEvent<TBehaviour> = <TBehaviour as NetworkBehaviour>::ToSwarm;
Expand Down
2 changes: 1 addition & 1 deletion core/src/translation.rs → swarm/src/translation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use multiaddr::{Multiaddr, Protocol};
use libp2p_core::{multiaddr::Protocol, Multiaddr};

/// Perform IP address translation.
///
Expand Down
8 changes: 0 additions & 8 deletions transports/dns/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,6 @@ where
self.do_dial(addr, dial_opts)
}

fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.lock().address_translation(server, observed)
}

fn poll(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
Expand Down Expand Up @@ -668,10 +664,6 @@ mod tests {
Ok(Box::pin(future::ready(Ok(()))))
}

fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> {
None
}

fn poll(
self: Pin<&mut Self>,
_: &mut Context<'_>,
Expand Down
36 changes: 0 additions & 36 deletions transports/quic/src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,6 @@ impl<P: Provider> Transport for GenTransport<P> {
}
}

fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if !is_quic_addr(listen, self.support_draft_29)
|| !is_quic_addr(observed, self.support_draft_29)
{
return None;
}
Some(observed.clone())
}

fn dial(
&mut self,
addr: Multiaddr,
Expand Down Expand Up @@ -723,33 +714,6 @@ fn multiaddr_to_socketaddr(
}
}

/// Whether an [`Multiaddr`] is a valid for the QUIC transport.
fn is_quic_addr(addr: &Multiaddr, support_draft_29: bool) -> bool {
use Protocol::*;
let mut iter = addr.iter();
let Some(first) = iter.next() else {
return false;
};
let Some(second) = iter.next() else {
return false;
};
let Some(third) = iter.next() else {
return false;
};
let fourth = iter.next();
let fifth = iter.next();

matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_))
&& matches!(second, Udp(_))
&& if support_draft_29 {
matches!(third, QuicV1 | Quic)
} else {
matches!(third, QuicV1)
}
&& matches!(fourth, Some(P2p(_)) | None)
&& fifth.is_none()
}

/// Turns an IP address and port into the corresponding QUIC multiaddr.
fn socketaddr_to_multiaddr(socket_addr: &SocketAddr, version: ProtocolVersion) -> Multiaddr {
let quic_proto = match version {
Expand Down
8 changes: 0 additions & 8 deletions transports/quic/tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,6 @@ async fn wrapped_with_delay() {
self.0.lock().unwrap().remove_listener(id)
}

fn address_translation(
&self,
listen: &Multiaddr,
observed: &Multiaddr,
) -> Option<Multiaddr> {
self.0.lock().unwrap().address_translation(listen, observed)
}

/// Delayed dial, i.e. calling [`Transport::dial`] on the inner [`Transport`] not within the
/// synchronous [`Transport::dial`] method, but within the [`Future`] returned by the outer
/// [`Transport::dial`].
Expand Down
Loading

0 comments on commit 0f538f3

Please sign in to comment.