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

protocols/dcutr/example: Wait for relay to accept reservation request #2642

Merged
merged 6 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
22 changes: 19 additions & 3 deletions protocols/dcutr/examples/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,25 +197,41 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}

// 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(_)) => {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also catch the case of client::Event::ReservationReqFailed and return an error in that case.
Shouldn't happen in practice but I'd rather have an explicit error than a non-terminating program.
Alternatively, I think there is no valid case in which any other relay event should occur anyway/ So we could remove the match block here and let it fall to the blank panic below.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be easier to understand if we matched explicitly on Mode and returned true for dial and relay_accepted_reservation for listen?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of 75697c2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, thank you!

{
break;
}
}
});

Expand Down
8 changes: 7 additions & 1 deletion src/tutorials/hole_punching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,20 @@
//!
//! ``` 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
//! can not reach it directly through the Internet. From the dialing client test that you can not
//! 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
Expand Down