From 1d6b08b0b662a40920c41d041cd7997befb4234f Mon Sep 17 00:00:00 2001 From: Max Inden Date: Fri, 20 May 2022 21:59:32 +0200 Subject: [PATCH] protocols/dcutr/examples: Wait to tell relay its public addr (#2659) As a listening client, when requesting a reservation with a relay, the relay responds with its public addresses. The listening client can then use the public addresses of the relay to advertise itself as reachable under a relayed address (`//p2p-circuit/p2p/`). The above operates under the assumption that the relay knows its public address. A relay learns its public address from remote peers, via the identify protocol. In the case where the relay just started up, the listening client might be the very first node to connect to it. Such scenario allows for a race condition. The listening client requests a reservation from the relay, while the relay requests its public address from the listening client. The former needs to contain the response from the latter. This commit serializes the two requests, making sure, in the case of a freshly started relay, that the listening client tells the relay its public address before requesting a reservation from the relay. Co-authored-by: Elena Frank --- protocols/dcutr/examples/client.rs | 71 +++++++++++++----------------- src/tutorials/hole_punching.rs | 4 +- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/protocols/dcutr/examples/client.rs b/protocols/dcutr/examples/client.rs index 6d09bfb6352..94bed7c2d7f 100644 --- a/protocols/dcutr/examples/client.rs +++ b/protocols/dcutr/examples/client.rs @@ -186,22 +186,12 @@ fn main() -> Result<(), Box> { } }); - match opts.mode { - Mode::Dial => { - swarm.dial(opts.relay_address.clone()).unwrap(); - } - Mode::Listen => { - swarm - .listen_on(opts.relay_address.clone().with(Protocol::P2pCircuit)) - .unwrap(); - } - } - - // 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. + // 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 relay_accepted_reservation = false; + let mut told_relay_observed_addr = false; loop { match swarm.next().await.unwrap() { @@ -209,46 +199,41 @@ fn main() -> Result<(), Box> { 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::Identify(IdentifyEvent::Sent { .. })) => { + info!("Told relay its public address."); + told_relay_observed_addr = true; } - SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Sent { .. })) => {} SwarmEvent::Behaviour(Event::Identify(IdentifyEvent::Received { info: IdentifyInfo { observed_addr, .. }, .. })) => { - info!("Relay observes us under the address: {:?}", observed_addr); + info!("Relay told us our public address: {:?}", observed_addr); learned_observed_addr = true; } event => panic!("{:?}", event), } - // Check whether we are done. - - if !learned_observed_addr { - continue; - } - - if opts.mode == Mode::Listen && !relay_accepted_reservation { - continue; + if learned_observed_addr && told_relay_observed_addr { + break; } - - break; } }); - if opts.mode == Mode::Dial { - swarm - .dial( - opts.relay_address - .clone() - .with(Protocol::P2pCircuit) - .with(Protocol::P2p(opts.remote_peer_id.unwrap().into())), - ) - .unwrap(); + 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 { @@ -257,6 +242,12 @@ fn main() -> Result<(), Box> { SwarmEvent::NewListenAddr { address, .. } => { info!("Listening on {:?}", address); } + SwarmEvent::Behaviour(Event::Relay(client::Event::ReservationReqAccepted { + .. + })) => { + assert!(opts.mode == Mode::Listen); + info!("Relay accepted our reservation request."); + } SwarmEvent::Behaviour(Event::Relay(event)) => { info!("{:?}", event) } diff --git a/src/tutorials/hole_punching.rs b/src/tutorials/hole_punching.rs index 3cd3bde5d16..10e2ec4c462 100644 --- a/src/tutorials/hole_punching.rs +++ b/src/tutorials/hole_punching.rs @@ -136,8 +136,10 @@ //! [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 told us our public address: "/ip4/XXX/tcp/53160" +//! [2022-05-11T10:38:54Z INFO client] Told relay its public address. //! [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" +//! [2022-05-11T10:38:54Z INFO client] Listening on "/ip4/$RELAY_SERVER_IP/tcp/4001/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN/p2p-circuit/p2p/XXX" //! ``` //! //! Now let's make sure that the listening client is not public, in other words let's make sure one