Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(quic): implement hole punching #3964

Merged
merged 44 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
7a714bf
Implement Transport::dial_as_listener for QUIC
arpankapoor May 21, 2023
145bf11
Merge QUIC dcutr and relay examples
arpankapoor May 23, 2023
0eafaf4
Simplify nested if let
arpankapoor May 23, 2023
fd70f73
Return error when dial_as_listener is called without an active listener
arpankapoor May 23, 2023
ddacaa0
Extract peer_id in multiaddr_to_socketaddr
arpankapoor May 25, 2023
3325dbe
Add peerid to holepunching key
arpankapoor May 25, 2023
271f234
Update dcutr example
arpankapoor May 26, 2023
8b9d778
Check if holepunching is already in progress
arpankapoor May 26, 2023
2290811
Dedup dial and dial_as_listener
arpankapoor May 26, 2023
2621c13
Store random packet on failure
arpankapoor May 30, 2023
3b17e38
Use `Display` formatting instead of `Debug`
arpankapoor May 30, 2023
45446bb
Use slice pattern
arpankapoor May 30, 2023
8a344c7
Resolve merge conflict
arpankapoor May 31, 2023
81988c2
Resolve merge conflict
arpankapoor May 31, 2023
a831f51
Merge branch 'master' into master
arpankapoor Jun 1, 2023
28ece03
Use futures::future::select instead of select! macro
arpankapoor Jun 1, 2023
ce86b7c
Merge branch 'master' into master
arpankapoor Jun 5, 2023
794cabc
Implement HolePuncher as an async function
arpankapoor Jun 5, 2023
e501175
Remove entry from HolePunchMap on timeout
arpankapoor Jun 5, 2023
ad2b644
Run rustfmt
arpankapoor Jun 5, 2023
ba60bba
Implement MaybeHolePunchedConnection as an async function
arpankapoor Jun 5, 2023
6c378ef
Use `SendExt::send` instead of `poll_ready` and `start_send`
arpankapoor Jun 5, 2023
3490940
Swap position of hole_puncher and punch_holes
arpankapoor Jun 6, 2023
26cd328
Clean up maybe_hole_punched_connection
arpankapoor Jun 6, 2023
0b1eaa5
Rename hole_punch_map to hole_punch_attempts
arpankapoor Jun 6, 2023
fbbd1a1
Create newtype HolePunchMap
arpankapoor Jun 6, 2023
2ad9255
Don't mix private/public imports
arpankapoor Jun 6, 2023
bbbf7a1
Return single eligible listener
arpankapoor Jun 7, 2023
26da28d
Return different error on successful holepunch
arpankapoor Jun 7, 2023
8eda5a7
Update external address
arpankapoor Jun 8, 2023
6c2638a
Merge branch 'master' of https://github.com/libp2p/rust-libp2p
arpankapoor Jun 8, 2023
5f58955
Resolve issue after merge
arpankapoor Jun 8, 2023
928314f
Remove peer_id from hole punching key
arpankapoor Jun 10, 2023
6e1b02d
Merge branch 'master' of https://github.com/libp2p/rust-libp2p
arpankapoor Jun 10, 2023
4f4f528
Remove redudant match arm from relay example
arpankapoor Jun 10, 2023
3f6bb59
Update CHANGELOG
arpankapoor Jun 10, 2023
02b1268
Fix imports
arpankapoor Jun 10, 2023
35de850
Review fixes
arpankapoor Jun 10, 2023
e46c170
More review fixes
arpankapoor Jun 11, 2023
36655ee
Move timeout and sleep to Provider trait
arpankapoor Jun 11, 2023
49873f0
Implement timeout using sleep
arpankapoor Jun 11, 2023
0eb9246
Loop through listener events
arpankapoor Jun 11, 2023
05b7a9f
Remove unused TimeoutError
arpankapoor Jun 11, 2023
9d001c7
Merge branch 'master' into master
mergify[bot] Jun 13, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"examples/chat-example",
"examples/autonat",
"examples/dcutr",
"examples/dcutr-quic",
"examples/distributed-key-value-store",
"examples/file-sharing",
"examples/identify",
Expand All @@ -12,6 +13,7 @@ members = [
"examples/metrics",
"examples/ping-example",
"examples/relay-server",
"examples/relay-server-quic",
"examples/rendezvous",
"identity",
"interop-tests",
Expand Down
15 changes: 15 additions & 0 deletions examples/dcutr-quic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "dcutr-quic"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT"

[dependencies]
clap = { version = "4.2.7", features = ["derive"] }
env_logger = "0.10.0"
futures = "0.3.28"
futures-timer = "3.0"
libp2p = { path = "../../libp2p", features = ["async-std", "dns", "dcutr", "identify", "macros", "noise", "ping", "relay", "rendezvous", "tcp", "tokio", "yamux"] }
libp2p-quic = { path = "../../transports/quic", features = ["async-std"] }
log = "0.4"
290 changes: 290 additions & 0 deletions examples/dcutr-quic/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,290 @@
// Copyright 2021 Protocol Labs.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use clap::Parser;
use futures::{
executor::{block_on, ThreadPool},
future::{Either, FutureExt},
stream::StreamExt,
};
use libp2p::{
core::{
multiaddr::{Multiaddr, Protocol},
muxing::StreamMuxerBox,
transport::{OrTransport, Transport},
upgrade,
},
dcutr,
dns::DnsConfig,
identify, identity, noise, ping, relay,
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
yamux, PeerId,
};
use libp2p_quic as quic;
use log::info;
use std::error::Error;
use std::net::Ipv4Addr;
use std::str::FromStr;

#[derive(Debug, Parser)]
#[clap(name = "libp2p DCUtR client")]
struct Opts {
/// The mode (client-listen, client-dial).
#[clap(long)]
mode: Mode,

/// Fixed value to generate deterministic peer id.
#[clap(long)]
secret_key_seed: u8,

/// The listening address
#[clap(long)]
relay_address: Multiaddr,

/// Peer ID of the remote peer to hole punch to.
#[clap(long)]
remote_peer_id: Option<PeerId>,
}

#[derive(Clone, Debug, PartialEq, Parser)]
enum Mode {
Dial,
Listen,
}

impl FromStr for Mode {
type Err = String;
fn from_str(mode: &str) -> Result<Self, Self::Err> {
match mode {
"dial" => Ok(Mode::Dial),
"listen" => Ok(Mode::Listen),
_ => Err("Expected either 'dial' or 'listen'".to_string()),
}
}
}

fn main() -> Result<(), Box<dyn Error>> {
env_logger::init();

let opts = Opts::parse();

let local_key = generate_ed25519(opts.secret_key_seed);
let local_peer_id = PeerId::from(local_key.public());
info!("Local peer id: {:?}", local_peer_id);

let (relay_transport, client) = relay::client::new(local_peer_id);
let relay_transport = relay_transport
.upgrade(upgrade::Version::V1)
.authenticate(noise::Config::new(&local_key).unwrap())
.multiplex(yamux::Config::default());

let transport = OrTransport::new(
relay_transport,
block_on(DnsConfig::system(quic::async_std::Transport::new(
quic::Config::new(&local_key),
)))
.unwrap(),
)
.map(|either_output, _| match either_output {
Either::Left((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
Either::Right((peer_id, muxer)) => (peer_id, StreamMuxerBox::new(muxer)),
})
.boxed();

#[derive(NetworkBehaviour)]
#[behaviour(to_swarm = "Event", event_process = false)]
struct Behaviour {
relay_client: relay::client::Behaviour,
ping: ping::Behaviour,
identify: identify::Behaviour,
dcutr: dcutr::Behaviour,
}

#[derive(Debug)]
#[allow(clippy::large_enum_variant)]
enum Event {
Ping(ping::Event),
Identify(identify::Event),
Relay(relay::client::Event),
Dcutr(dcutr::Event),
}

impl From<ping::Event> for Event {
fn from(e: ping::Event) -> Self {
Event::Ping(e)
}
}

impl From<identify::Event> for Event {
fn from(e: identify::Event) -> Self {
Event::Identify(e)
}
}

impl From<relay::client::Event> for Event {
fn from(e: relay::client::Event) -> Self {
Event::Relay(e)
}
}

impl From<dcutr::Event> for Event {
fn from(e: dcutr::Event) -> Self {
Event::Dcutr(e)
}
}

let behaviour = Behaviour {
relay_client: client,
ping: ping::Behaviour::new(ping::Config::new()),
identify: identify::Behaviour::new(identify::Config::new(
"/TODO/0.0.1".to_string(),
local_key.public(),
)),
dcutr: dcutr::Behaviour::new(local_peer_id),
};

let mut swarm = match ThreadPool::new() {
Ok(tp) => SwarmBuilder::with_executor(transport, behaviour, local_peer_id, tp),
Err(_) => SwarmBuilder::without_executor(transport, behaviour, local_peer_id),
}
.build();

swarm
.listen_on(
Multiaddr::empty()
.with("0.0.0.0".parse::<Ipv4Addr>().unwrap().into())
.with(Protocol::Udp(0))
.with(Protocol::QuicV1),
)
.unwrap();

// Wait to listen on all interfaces.
block_on(async {
let mut delay = futures_timer::Delay::new(std::time::Duration::from_secs(1)).fuse();
loop {
futures::select! {
event = swarm.next() => {
match event.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
info!("Listening on {:?}", address);
}
event => panic!("{event:?}"),
}
}
_ = delay => {
// Likely listening on all interfaces now, thus continuing by breaking the loop.
break;
}
}
}
});

// Connect to the relay server. Not for the reservation or relayed connection, but to (a) learn
// our local public address and (b) enable a freshly started relay to learn its public address.
swarm.dial(opts.relay_address.clone()).unwrap();
block_on(async {
let mut learned_observed_addr = false;
let mut told_relay_observed_addr = false;

loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { .. } => {}
SwarmEvent::Dialing { .. } => {}
SwarmEvent::ConnectionEstablished { .. } => {}
SwarmEvent::Behaviour(Event::Ping(_)) => {}
SwarmEvent::Behaviour(Event::Identify(identify::Event::Sent { .. })) => {
info!("Told relay its public address.");
told_relay_observed_addr = true;
}
SwarmEvent::Behaviour(Event::Identify(identify::Event::Received {
info: identify::Info { observed_addr, .. },
..
})) => {
info!("Relay told us our public address: {:?}", observed_addr);
learned_observed_addr = true;
}
event => panic!("{event:?}"),
}

if learned_observed_addr && told_relay_observed_addr {
break;
}
}
});

match opts.mode {
Mode::Dial => {
swarm
.dial(
opts.relay_address
.with(Protocol::P2pCircuit)
.with(Protocol::P2p(opts.remote_peer_id.unwrap().into())),
)
.unwrap();
}
Mode::Listen => {
swarm
.listen_on(opts.relay_address.with(Protocol::P2pCircuit))
.unwrap();
}
}

block_on(async {
loop {
match swarm.next().await.unwrap() {
SwarmEvent::NewListenAddr { address, .. } => {
info!("Listening on {:?}", address);
}
SwarmEvent::Behaviour(Event::Relay(
relay::client::Event::ReservationReqAccepted { .. },
)) => {
assert!(opts.mode == Mode::Listen);
info!("Relay accepted our reservation request.");
}
SwarmEvent::Behaviour(Event::Relay(event)) => {
info!("{:?}", event)
}
SwarmEvent::Behaviour(Event::Dcutr(event)) => {
info!("{:?}", event)
}
SwarmEvent::Behaviour(Event::Identify(event)) => {
info!("{:?}", event)
}
SwarmEvent::Behaviour(Event::Ping(_)) => {}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
info!("Established connection to {:?} via {:?}", peer_id, endpoint);
}
SwarmEvent::OutgoingConnectionError { peer_id, error, .. } => {
info!("Outgoing connection error to {:?}: {:?}", peer_id, error);
}
_ => {}
}
}
})
}

fn generate_ed25519(secret_key_seed: u8) -> identity::Keypair {
let mut bytes = [0u8; 32];
bytes[0] = secret_key_seed;

identity::Keypair::ed25519_from_bytes(bytes).expect("only errors on wrong length")
}
15 changes: 15 additions & 0 deletions examples/relay-server-quic/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "relay-server-example-quic"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT"

[dependencies]
clap = { version = "4.2.7", features = ["derive"] }
async-std = { version = "1.12", features = ["attributes"] }
async-trait = "0.1"
env_logger = "0.10.0"
futures = "0.3.28"
libp2p = { path = "../../libp2p", features = ["async-std", "noise", "macros", "ping", "tcp", "identify", "yamux", "relay"] }
libp2p-quic = { path = "../../transports/quic", features = ["async-std"] }
Loading