Skip to content

Commit

Permalink
core/src/transport: Add Transport::dial_as_listener (libp2p#2363)
Browse files Browse the repository at this point in the history
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]: libp2p#2066
[dcutr]: https://github.com/libp2p/specs/blob/master/relay/DCUtR.md
  • Loading branch information
mxinden authored Jan 17, 2022
1 parent ae96f4b commit f079c8e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use futures_timer::Delay;
use instant::Instant;
use libp2p_core::{
connection::{ConnectionId, ListenerId},
ConnectedPoint, Multiaddr, PeerId,
ConnectedPoint, Endpoint, Multiaddr, PeerId,
};
use libp2p_request_response::{
handler::RequestResponseHandlerEvent, ProtocolSupport, RequestId, RequestResponse,
Expand Down Expand Up @@ -315,12 +315,23 @@ impl NetworkBehaviour for Behaviour {
connections.insert(*conn, addr);

match endpoint {
ConnectedPoint::Dialer { address } => {
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
} => {
if let Some(event) = self.as_server().on_outbound_connection(peer, address) {
self.pending_out_events
.push_back(Event::InboundProbe(event));
}
}
ConnectedPoint::Dialer {
address: _,
role_override: Endpoint::Listener,
} => {
// Outgoing connection was dialed as a listener. In other words outgoing connection
// was dialed as part of a hole punch. `libp2p-autonat` never attempts to hole
// punch, thus this connection has not been requested by this [`NetworkBehaviour`].
}
ConnectedPoint::Listener { .. } => self.as_client().on_inbound_connection(),
}
}
Expand Down
14 changes: 11 additions & 3 deletions tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libp2p::{
use libp2p_autonat::{
Behaviour, Config, Event, InboundProbeError, InboundProbeEvent, ResponseError,
};
use libp2p_core::ConnectedPoint;
use libp2p_core::{ConnectedPoint, Endpoint};
use libp2p_swarm::DialError;
use std::{num::NonZeroU32, time::Duration};

Expand Down Expand Up @@ -191,7 +191,11 @@ async fn test_dial_back() {
match server.select_next_some().await {
SwarmEvent::ConnectionEstablished {
peer_id,
endpoint: ConnectedPoint::Dialer { address },
endpoint:
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
},
num_established,
concurrent_dial_errors,
} => {
Expand Down Expand Up @@ -399,7 +403,11 @@ async fn test_dial_multiple_addr() {
match server.select_next_some().await {
SwarmEvent::ConnectionEstablished {
peer_id,
endpoint: ConnectedPoint::Dialer { address },
endpoint:
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
},
concurrent_dial_errors,
..
} => {
Expand Down

0 comments on commit f079c8e

Please sign in to comment.