Skip to content

Commit 5321a44

Browse files
authored
fix(dcutr): always check for relayed connection first
For a non-relay connection, check if it corresponds to a direct connection upgrade. As discussed in #3964. Pull-Request: #3982.
1 parent b73f720 commit 5321a44

File tree

1 file changed

+39
-49
lines changed

1 file changed

+39
-49
lines changed

protocols/dcutr/src/behaviour_impl.rs

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -242,37 +242,27 @@ impl NetworkBehaviour for Behaviour {
242242
fn handle_established_inbound_connection(
243243
&mut self,
244244
connection_id: ConnectionId,
245-
peer: PeerId,
245+
_peer: PeerId,
246246
local_addr: &Multiaddr,
247247
remote_addr: &Multiaddr,
248248
) -> Result<THandler<Self>, ConnectionDenied> {
249-
match self
250-
.outgoing_direct_connection_attempts
251-
.remove(&(connection_id, peer))
252-
{
253-
None => {
254-
let handler = if is_relayed(local_addr) {
255-
Either::Left(handler::relayed::Handler::new(ConnectedPoint::Listener {
256-
local_addr: local_addr.clone(),
257-
send_back_addr: remote_addr.clone(),
258-
})) // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound.
259-
} else {
260-
Either::Right(Either::Right(dummy::ConnectionHandler))
261-
};
262-
263-
Ok(handler)
264-
}
265-
Some(_) => {
266-
assert!(
267-
!is_relayed(local_addr),
268-
"`Prototype::DirectConnection` is never created for relayed connection."
269-
);
270-
271-
Ok(Either::Right(Either::Left(
272-
handler::direct::Handler::default(),
273-
)))
274-
}
249+
if is_relayed(local_addr) {
250+
return Ok(Either::Left(handler::relayed::Handler::new(
251+
ConnectedPoint::Listener {
252+
local_addr: local_addr.clone(),
253+
send_back_addr: remote_addr.clone(),
254+
},
255+
))); // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound.
275256
}
257+
258+
assert!(
259+
self.direct_to_relayed_connections
260+
.get(&connection_id)
261+
.is_none(),
262+
"state mismatch"
263+
);
264+
265+
Ok(Either::Right(Either::Right(dummy::ConnectionHandler)))
276266
}
277267

278268
fn handle_established_outbound_connection(
@@ -282,33 +272,33 @@ impl NetworkBehaviour for Behaviour {
282272
addr: &Multiaddr,
283273
role_override: Endpoint,
284274
) -> Result<THandler<Self>, ConnectionDenied> {
285-
match self
286-
.outgoing_direct_connection_attempts
287-
.remove(&(connection_id, peer))
275+
if is_relayed(addr) {
276+
return Ok(Either::Left(handler::relayed::Handler::new(
277+
ConnectedPoint::Dialer {
278+
address: addr.clone(),
279+
role_override,
280+
},
281+
))); // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound.
282+
}
283+
284+
// Whether this is a connection requested by this behaviour.
285+
if let Some(&relayed_connection_id) = self.direct_to_relayed_connections.get(&connection_id)
288286
{
289-
None => {
290-
let handler = if is_relayed(addr) {
291-
Either::Left(handler::relayed::Handler::new(ConnectedPoint::Dialer {
292-
address: addr.clone(),
293-
role_override,
294-
})) // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound.
295-
} else {
296-
Either::Right(Either::Right(dummy::ConnectionHandler))
297-
};
298-
299-
Ok(handler)
300-
}
301-
Some(_) => {
287+
if role_override == Endpoint::Listener {
302288
assert!(
303-
!is_relayed(addr),
304-
"`Prototype::DirectConnection` is never created for relayed connection."
289+
self.outgoing_direct_connection_attempts
290+
.remove(&(relayed_connection_id, peer))
291+
.is_some(),
292+
"state mismatch"
305293
);
306-
307-
Ok(Either::Right(Either::Left(
308-
handler::direct::Handler::default(),
309-
)))
310294
}
295+
296+
return Ok(Either::Right(Either::Left(
297+
handler::direct::Handler::default(),
298+
)));
311299
}
300+
301+
Ok(Either::Right(Either::Right(dummy::ConnectionHandler)))
312302
}
313303

314304
fn on_connection_handler_event(

0 commit comments

Comments
 (0)