-
Notifications
You must be signed in to change notification settings - Fork 990
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement `Transport::dial_as_listener` for QUIC as specified by the [DCUtR spec](https://github.com/libp2p/specs/blob/master/relay/DCUtR.md). To facilitate hole punching in QUIC, one side needs to send random UDP packets to establish a mapping in the routing table of the NAT device. If successful, our listener will emit a new inbound connection. This connection needs to then be sent to the dialing task. We achieve this by storing a `HashMap` of hole punch attempts indexed by the remote's `SocketAddr`. A matching incoming connection is then sent via a oneshot channel to the dialing task which continues with upgrading the connection. Related #2883. Pull-Request: #3964.
- Loading branch information
1 parent
2a6311f
commit cf3e4c6
Showing
14 changed files
with
293 additions
and
78 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use std::{net::SocketAddr, time::Duration}; | ||
|
||
use futures::future::Either; | ||
use rand::{distributions, Rng}; | ||
|
||
use crate::{ | ||
endpoint::{self, ToEndpoint}, | ||
Error, Provider, | ||
}; | ||
|
||
pub(crate) async fn hole_puncher<P: Provider>( | ||
endpoint_channel: endpoint::Channel, | ||
remote_addr: SocketAddr, | ||
timeout_duration: Duration, | ||
) -> Error { | ||
let punch_holes_future = punch_holes::<P>(endpoint_channel, remote_addr); | ||
futures::pin_mut!(punch_holes_future); | ||
match futures::future::select(P::sleep(timeout_duration), punch_holes_future).await { | ||
Either::Left(_) => Error::HandshakeTimedOut, | ||
Either::Right((hole_punch_err, _)) => hole_punch_err, | ||
} | ||
} | ||
|
||
async fn punch_holes<P: Provider>( | ||
mut endpoint_channel: endpoint::Channel, | ||
remote_addr: SocketAddr, | ||
) -> Error { | ||
loop { | ||
let sleep_duration = Duration::from_millis(rand::thread_rng().gen_range(10..=200)); | ||
P::sleep(sleep_duration).await; | ||
|
||
let random_udp_packet = ToEndpoint::SendUdpPacket(quinn_proto::Transmit { | ||
destination: remote_addr, | ||
ecn: None, | ||
contents: rand::thread_rng() | ||
.sample_iter(distributions::Standard) | ||
.take(64) | ||
.collect(), | ||
segment_size: None, | ||
src_ip: None, | ||
}); | ||
|
||
if endpoint_channel.send(random_udp_packet).await.is_err() { | ||
return Error::EndpointDriverCrashed; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.