mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-30 18:21:33 +00:00
core/src/transport: Add Transport::dial_as_listener
(#2363)
Allows `NetworkBehaviour` implementations to dial a peer, but instruct the dialed connection to be upgraded as if it were the listening endpoint. This is needed when establishing direct connections through NATs and/or Firewalls (hole punching). When hole punching via TCP (QUIC is different but similar) both ends dial the other at the same time resulting in a simultaneously opened TCP connection. To disambiguate who is the dialer and who the listener there are two options: 1. Use the Simultaneous Open Extension of Multistream Select. See [sim-open] specification and [sim-open-rust] Rust implementation. 2. Disambiguate the role (dialer or listener) based on the role within the DCUtR [dcutr] protocol. More specifically the node initiating the DCUtR process will act as a listener and the other as a dialer. This commit enables (2), i.e. enables the DCUtR protocol to specify the role used once the connection is established. While on the positive side (2) requires one round trip less than (1), on the negative side (2) only works for coordinated simultaneous dials. I.e. when a simultaneous dial happens by chance, and not coordinated via DCUtR, the connection attempt fails when only (2) is in place. [sim-open]: https://github.com/libp2p/specs/blob/master/connections/simopen.md [sim-open-rust]: https://github.com/libp2p/rust-libp2p/pull/2066 [dcutr]: https://github.com/libp2p/specs/blob/master/relay/DCUtR.md
This commit is contained in:
@ -97,7 +97,10 @@ pub enum PendingPoint {
|
||||
/// There is no single address associated with the Dialer of a pending
|
||||
/// connection. Addresses are dialed in parallel. Only once the first dial
|
||||
/// is successful is the address of the connection known.
|
||||
Dialer,
|
||||
Dialer {
|
||||
/// Same as [`ConnectedPoint::Dialer`] `role_override`.
|
||||
role_override: Endpoint,
|
||||
},
|
||||
/// The socket comes from a listener.
|
||||
Listener {
|
||||
/// Local connection address.
|
||||
@ -110,7 +113,7 @@ pub enum PendingPoint {
|
||||
impl From<ConnectedPoint> for PendingPoint {
|
||||
fn from(endpoint: ConnectedPoint) -> Self {
|
||||
match endpoint {
|
||||
ConnectedPoint::Dialer { .. } => PendingPoint::Dialer,
|
||||
ConnectedPoint::Dialer { role_override, .. } => PendingPoint::Dialer { role_override },
|
||||
ConnectedPoint::Listener {
|
||||
local_addr,
|
||||
send_back_addr,
|
||||
@ -129,6 +132,27 @@ pub enum ConnectedPoint {
|
||||
Dialer {
|
||||
/// Multiaddress that was successfully dialed.
|
||||
address: Multiaddr,
|
||||
/// Whether the role of the local node on the connection should be
|
||||
/// overriden. I.e. whether the local node should act as a listener on
|
||||
/// the outgoing connection.
|
||||
///
|
||||
/// This option is needed for NAT and firewall hole punching.
|
||||
///
|
||||
/// - [`Endpoint::Dialer`] represents the default non-overriding option.
|
||||
///
|
||||
/// - [`Endpoint::Listener`] represents the overriding option.
|
||||
/// Realization depends on the transport protocol. E.g. in the case of
|
||||
/// TCP, both endpoints dial each other, resulting in a _simultaneous
|
||||
/// open_ TCP connection. On this new connection both endpoints assume
|
||||
/// to be the dialer of the connection. This is problematic during the
|
||||
/// connection upgrade process where an upgrade assumes one side to be
|
||||
/// the listener. With the help of this option, both peers can
|
||||
/// negotiate the roles (dialer and listener) for the new connection
|
||||
/// ahead of time, through some external channel, e.g. the DCUtR
|
||||
/// protocol, and thus have one peer dial the other and upgrade the
|
||||
/// connection as a dialer and one peer dial the other and upgrade the
|
||||
/// connection _as a listener_ overriding its role.
|
||||
role_override: Endpoint,
|
||||
},
|
||||
/// We received the node.
|
||||
Listener {
|
||||
@ -179,7 +203,10 @@ impl ConnectedPoint {
|
||||
/// Returns true if the connection is relayed.
|
||||
pub fn is_relayed(&self) -> bool {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { address } => address,
|
||||
ConnectedPoint::Dialer {
|
||||
address,
|
||||
role_override: _,
|
||||
} => address,
|
||||
ConnectedPoint::Listener { local_addr, .. } => local_addr,
|
||||
}
|
||||
.iter()
|
||||
@ -194,7 +221,7 @@ impl ConnectedPoint {
|
||||
/// not be usable to establish new connections.
|
||||
pub fn get_remote_address(&self) -> &Multiaddr {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { address } => address,
|
||||
ConnectedPoint::Dialer { address, .. } => address,
|
||||
ConnectedPoint::Listener { send_back_addr, .. } => send_back_addr,
|
||||
}
|
||||
}
|
||||
@ -204,7 +231,7 @@ impl ConnectedPoint {
|
||||
/// For `Dialer`, this modifies `address`. For `Listener`, this modifies `send_back_addr`.
|
||||
pub fn set_remote_address(&mut self, new_address: Multiaddr) {
|
||||
match self {
|
||||
ConnectedPoint::Dialer { address } => *address = new_address,
|
||||
ConnectedPoint::Dialer { address, .. } => *address = new_address,
|
||||
ConnectedPoint::Listener { send_back_addr, .. } => *send_back_addr = new_address,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user