From 665913ffe6b43a6eeb01759304307de414a65c9f Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 11 May 2022 12:47:23 +0200 Subject: [PATCH 1/3] protocols/dcutr/example: Wait for relay to accept reservation request When in listening mode, wait for the relay to accept our reservation request. Only then can a client in dialing mode establish a relayed connection to us via the relay. See also https://github.com/libp2p/rust-libp2p/issues/2621#issuecomment-1123549348 --- protocols/dcutr/examples/client.rs | 22 +++++++++++++++++++--- src/tutorials/hole_punching.rs | 8 +++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/protocols/dcutr/examples/client.rs b/protocols/dcutr/examples/client.rs index d8cbe4aaeab..a5221503d82 100644 --- a/protocols/dcutr/examples/client.rs +++ b/protocols/dcutr/examples/client.rs @@ -197,25 +197,41 @@ fn main() -> Result<(), Box> { } } - // Wait till connected to relay to learn external address. + // Wait till connected to relay to learn external address. In case we are in listening mode, + // wait for the relay to accept our reservation request. block_on(async { + let mut learned_observed_addr = false; + let mut relay_accepted_reservation = false; + loop { match swarm.next().await.unwrap() { SwarmEvent::NewListenAddr { .. } => {} SwarmEvent::Dialing { .. } => {} SwarmEvent::ConnectionEstablished { .. } => {} SwarmEvent::Behaviour(Event::Ping(_)) => {} + SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted { + .. + })) => { + info!("Relay accepted our reservation request."); + relay_accepted_reservation = true + } SwarmEvent::Behaviour(Event::Relay(_)) => {} SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {} SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received { info: IdentifyInfo { observed_addr, .. }, .. })) => { - info!("Observed address: {:?}", observed_addr); - break; + info!("Relay observes us under the address: {:?}", observed_addr); + learned_observed_addr = true; } event => panic!("{:?}", event), } + + if learned_observed_addr + && (matches!(opts.mode, Mode::Dial) || relay_accepted_reservation) + { + break; + } } }); diff --git a/src/tutorials/hole_punching.rs b/src/tutorials/hole_punching.rs index a73d66a2794..3cd3bde5d16 100644 --- a/src/tutorials/hole_punching.rs +++ b/src/tutorials/hole_punching.rs @@ -132,6 +132,12 @@ //! //! ``` bash //! RUST_LOG=info ./client --secret-key-seed 1 --mode listen --relay-address /ip4/$RELAY_SERVER_IP/tcp/4001/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN +//! +//! [2022-05-11T10:38:52Z INFO client] Local peer id: PeerId("XXX") +//! [2022-05-11T10:38:52Z INFO client] Listening on "/ip4/127.0.0.1/tcp/44703" +//! [2022-05-11T10:38:52Z INFO client] Listening on "/ip4/XXX/tcp/44703" +//! [2022-05-11T10:38:54Z INFO client] Relay accepted our reservation request. +//! [2022-05-11T10:38:54Z INFO client] Relay observes us under the address: "/ip4/XXX/tcp/53160" //! ``` //! //! Now let's make sure that the listening client is not public, in other words let's make sure one @@ -139,7 +145,7 @@ //! connect on Layer 4 (TCP): //! //! ``` bash -//! telnet $RELAY_SERVER_IP 4001 +//! telnet $LISTENING_CLIENT_IP_OBSERVED_BY_RELAY 53160 //! ``` //! //! ## Connecting to the listening client from the dialing client From 1128711acbec906399e91c884e9ef449565bfdae Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 18 May 2022 10:51:28 +0200 Subject: [PATCH 2/3] protocols/dcutr/examples: Don't ignore all remaining Event::Relay --- protocols/dcutr/examples/client.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/protocols/dcutr/examples/client.rs b/protocols/dcutr/examples/client.rs index a5221503d82..84294a46380 100644 --- a/protocols/dcutr/examples/client.rs +++ b/protocols/dcutr/examples/client.rs @@ -215,7 +215,6 @@ fn main() -> Result<(), Box> { info!("Relay accepted our reservation request."); relay_accepted_reservation = true } - SwarmEvent::Behaviour(Event::Relay(_)) => {} SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {} SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received { info: IdentifyInfo { observed_addr, .. }, From 75697c253d774294abdf2a00a40718f8a6d32493 Mon Sep 17 00:00:00 2001 From: Max Inden Date: Wed, 18 May 2022 11:01:55 +0200 Subject: [PATCH 3/3] protocols/dcutr/examples/: Refactor matches! on opts.mode --- protocols/dcutr/examples/client.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/protocols/dcutr/examples/client.rs b/protocols/dcutr/examples/client.rs index 84294a46380..6d09bfb6352 100644 --- a/protocols/dcutr/examples/client.rs +++ b/protocols/dcutr/examples/client.rs @@ -226,11 +226,17 @@ fn main() -> Result<(), Box> { event => panic!("{:?}", event), } - if learned_observed_addr - && (matches!(opts.mode, Mode::Dial) || relay_accepted_reservation) - { - break; + // Check whether we are done. + + if !learned_observed_addr { + continue; + } + + if opts.mode == Mode::Listen && !relay_accepted_reservation { + continue; } + + break; } });