Skip to content

Commit af63130

Browse files
fix(relay): send correct PeerId to client in outbound STOP message
Previously, the relay server would erroneously send its own `PeerId` in the STOP message to the client upon an incoming relay connection. This is obviously wrong and results in failed connection upgrades in other implementations. Pull-Request: #3767.
1 parent 436459b commit af63130

File tree

6 files changed

+41
-33
lines changed

6 files changed

+41
-33
lines changed

protocols/relay/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
## 0.15.2 - unreleased
22

3+
- Send correct `PeerId` in outbound STOP message to client.
4+
See [PR 3767].
5+
36
- As a relay, when forwarding data between relay-connection-source and -destination and vice versa, flush write side when read currently has no more data available.
47
See [PR 3765].
58

9+
[PR 3767]: https://github.com/libp2p/rust-libp2p/pull/3767
610
[PR 3765]: https://github.com/libp2p/rust-libp2p/pull/3765
711

812
## 0.15.1

protocols/relay/src/behaviour.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,6 @@ impl NetworkBehaviour for Behaviour {
520520
event: Either::Left(handler::In::NegotiateOutboundConnect {
521521
circuit_id,
522522
inbound_circuit_req,
523-
relay_peer_id: self.local_peer_id,
524523
src_peer_id: event_source,
525524
src_connection_id: connection,
526525
}),

protocols/relay/src/behaviour/handler.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ pub enum In {
6969
NegotiateOutboundConnect {
7070
circuit_id: CircuitId,
7171
inbound_circuit_req: inbound_hop::CircuitReq,
72-
relay_peer_id: PeerId,
7372
src_peer_id: PeerId,
7473
src_connection_id: ConnectionId,
7574
},
@@ -112,13 +111,11 @@ impl fmt::Debug for In {
112111
In::NegotiateOutboundConnect {
113112
circuit_id,
114113
inbound_circuit_req: _,
115-
relay_peer_id,
116114
src_peer_id,
117115
src_connection_id,
118116
} => f
119117
.debug_struct("In::NegotiateOutboundConnect")
120118
.field("circuit_id", circuit_id)
121-
.field("relay_peer_id", relay_peer_id)
122119
.field("src_peer_id", src_peer_id)
123120
.field("src_connection_id", src_connection_id)
124121
.finish(),
@@ -655,15 +652,14 @@ impl ConnectionHandler for Handler {
655652
In::NegotiateOutboundConnect {
656653
circuit_id,
657654
inbound_circuit_req,
658-
relay_peer_id,
659655
src_peer_id,
660656
src_connection_id,
661657
} => {
662658
self.queued_events
663659
.push_back(ConnectionHandlerEvent::OutboundSubstreamRequest {
664660
protocol: SubstreamProtocol::new(
665661
outbound_stop::Upgrade {
666-
relay_peer_id,
662+
src_peer_id,
667663
max_circuit_duration: self.config.max_circuit_duration,
668664
max_circuit_bytes: self.config.max_circuit_bytes,
669665
},

protocols/relay/src/priv_client/handler.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ impl Handler {
199199
});
200200

201201
self.queued_events.push_back(ConnectionHandlerEvent::Custom(
202-
Event::InboundCircuitEstablished {
203-
src_peer_id: self.remote_peer_id,
204-
limit,
205-
},
202+
Event::InboundCircuitEstablished { src_peer_id, limit },
206203
));
207204
}
208205
Reservation::None => {

protocols/relay/src/protocol/outbound_stop.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::time::Duration;
3232
use thiserror::Error;
3333

3434
pub struct Upgrade {
35-
pub relay_peer_id: PeerId,
35+
pub src_peer_id: PeerId,
3636
pub max_circuit_duration: Duration,
3737
pub max_circuit_bytes: u64,
3838
}
@@ -55,7 +55,7 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
5555
let msg = proto::StopMessage {
5656
type_pb: proto::StopMessageType::CONNECT,
5757
peer: Some(proto::Peer {
58-
id: self.relay_peer_id.to_bytes(),
58+
id: self.src_peer_id.to_bytes(),
5959
addrs: vec![],
6060
}),
6161
limit: Some(proto::Limit {

protocols/relay/tests/lib.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -203,34 +203,46 @@ fn connect() {
203203
relay_peer_id,
204204
false, // No renewal.
205205
));
206-
spawn_swarm_on_pool(&pool, dst);
207206

208207
let mut src = build_client();
208+
let src_peer_id = *src.local_peer_id();
209209

210210
src.dial(dst_addr).unwrap();
211211

212-
pool.run_until(async {
213-
loop {
214-
match src.select_next_some().await {
215-
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
216-
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {}
217-
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
218-
if peer == dst_peer_id =>
219-
{
220-
break
221-
}
222-
SwarmEvent::Behaviour(ClientEvent::Relay(
223-
relay::client::Event::OutboundCircuitEstablished { .. },
224-
)) => {}
225-
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
226-
if peer == relay_peer_id => {}
227-
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == dst_peer_id => {
228-
break
229-
}
230-
e => panic!("{e:?}"),
212+
pool.run_until(futures::future::join(
213+
connection_established_to(src, relay_peer_id, dst_peer_id),
214+
connection_established_to(dst, relay_peer_id, src_peer_id),
215+
));
216+
}
217+
218+
async fn connection_established_to(mut swarm: Swarm<Client>, relay_peer_id: PeerId, other: PeerId) {
219+
loop {
220+
match swarm.select_next_some().await {
221+
SwarmEvent::Dialing(peer_id) if peer_id == relay_peer_id => {}
222+
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == relay_peer_id => {}
223+
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. })) if peer == other => {
224+
break
225+
}
226+
SwarmEvent::Behaviour(ClientEvent::Relay(
227+
relay::client::Event::OutboundCircuitEstablished { .. },
228+
)) => {}
229+
SwarmEvent::Behaviour(ClientEvent::Relay(
230+
relay::client::Event::InboundCircuitEstablished { src_peer_id, .. },
231+
)) => {
232+
assert_eq!(src_peer_id, other);
233+
}
234+
SwarmEvent::Behaviour(ClientEvent::Ping(ping::Event { peer, .. }))
235+
if peer == relay_peer_id => {}
236+
SwarmEvent::ConnectionEstablished { peer_id, .. } if peer_id == other => break,
237+
SwarmEvent::IncomingConnection { send_back_addr, .. } => {
238+
let peer_id_from_addr =
239+
PeerId::try_from_multiaddr(&send_back_addr).expect("to have /p2p");
240+
241+
assert_eq!(peer_id_from_addr, other)
231242
}
243+
e => panic!("{e:?}"),
232244
}
233-
})
245+
}
234246
}
235247

236248
#[test]

0 commit comments

Comments
 (0)