1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
use crate::v2::client::RelayedConnection;
use crate::v2::RequestId;
use futures::channel::mpsc;
use futures::channel::oneshot;
use futures::future::{ready, BoxFuture, FutureExt, Ready};
use futures::ready;
use futures::sink::SinkExt;
use futures::stream::SelectAll;
use futures::stream::{Stream, StreamExt};
use libp2p_core::multiaddr::{Multiaddr, Protocol};
use libp2p_core::transport::{ListenerId, TransportError, TransportEvent};
use libp2p_core::{PeerId, Transport};
use std::collections::VecDeque;
use std::pin::Pin;
use std::task::{Context, Poll};
use thiserror::Error;
pub struct ClientTransport {
to_behaviour: mpsc::Sender<TransportToBehaviourMsg>,
pending_to_behaviour: VecDeque<TransportToBehaviourMsg>,
listeners: SelectAll<RelayListener>,
}
impl ClientTransport {
pub(crate) fn new() -> (Self, mpsc::Receiver<TransportToBehaviourMsg>) {
let (to_behaviour, from_transport) = mpsc::channel(0);
let transport = ClientTransport {
to_behaviour,
pending_to_behaviour: VecDeque::new(),
listeners: SelectAll::new(),
};
(transport, from_transport)
}
}
impl Transport for ClientTransport {
type Output = RelayedConnection;
type Error = RelayError;
type ListenerUpgrade = Ready<Result<Self::Output, Self::Error>>;
type Dial = RelayedDial;
fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<Self::Error>> {
let (relay_peer_id, relay_addr) = match parse_relayed_multiaddr(addr)? {
RelayedMultiaddr {
relay_peer_id: None,
relay_addr: _,
..
} => return Err(RelayError::MissingDstPeerId.into()),
RelayedMultiaddr {
relay_peer_id: _,
relay_addr: None,
..
} => return Err(RelayError::MissingRelayAddr.into()),
RelayedMultiaddr {
relay_peer_id: Some(peer_id),
relay_addr: Some(addr),
..
} => (peer_id, addr),
};
let (to_listener, from_behaviour) = mpsc::channel(0);
self.pending_to_behaviour
.push_back(TransportToBehaviourMsg::ListenReq {
relay_peer_id,
relay_addr,
to_listener,
});
let listener_id = ListenerId::new();
let listener = RelayListener {
listener_id,
queued_events: Default::default(),
from_behaviour,
is_closed: false,
};
self.listeners.push(listener);
Ok(listener_id)
}
fn remove_listener(&mut self, id: ListenerId) -> bool {
if let Some(listener) = self.listeners.iter_mut().find(|l| l.listener_id == id) {
listener.close(Ok(()));
true
} else {
false
}
}
fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let RelayedMultiaddr {
relay_peer_id,
relay_addr,
dst_peer_id,
dst_addr,
} = parse_relayed_multiaddr(addr)?;
let relay_peer_id = relay_peer_id.ok_or(RelayError::MissingRelayPeerId)?;
let relay_addr = relay_addr.ok_or(RelayError::MissingRelayAddr)?;
let dst_peer_id = dst_peer_id.ok_or(RelayError::MissingDstPeerId)?;
let mut to_behaviour = self.to_behaviour.clone();
Ok(async move {
let (tx, rx) = oneshot::channel();
to_behaviour
.send(TransportToBehaviourMsg::DialReq {
request_id: RequestId::new(),
relay_addr,
relay_peer_id,
dst_addr,
dst_peer_id,
send_back: tx,
})
.await?;
let stream = rx.await?.map_err(|()| RelayError::Connect)?;
Ok(stream)
}
.boxed())
}
fn dial_as_listener(
&mut self,
addr: Multiaddr,
) -> Result<Self::Dial, TransportError<Self::Error>>
where
Self: Sized,
{
Err(TransportError::MultiaddrNotSupported(addr))
}
fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}
fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>>
where
Self: Sized,
{
loop {
if !self.pending_to_behaviour.is_empty() {
match self.to_behaviour.poll_ready(cx) {
Poll::Ready(Ok(())) => {
let msg = self
.pending_to_behaviour
.pop_front()
.expect("Called !is_empty().");
let _ = self.to_behaviour.start_send(msg);
continue;
}
Poll::Ready(Err(_)) => unreachable!("Receiver is never dropped."),
Poll::Pending => {}
}
}
match self.listeners.poll_next_unpin(cx) {
Poll::Ready(Some(event)) => return Poll::Ready(event),
_ => return Poll::Pending,
}
}
}
}
#[derive(Default)]
struct RelayedMultiaddr {
relay_peer_id: Option<PeerId>,
relay_addr: Option<Multiaddr>,
dst_peer_id: Option<PeerId>,
dst_addr: Option<Multiaddr>,
}
fn parse_relayed_multiaddr(
addr: Multiaddr,
) -> Result<RelayedMultiaddr, TransportError<RelayError>> {
if !addr.iter().any(|p| matches!(p, Protocol::P2pCircuit)) {
return Err(TransportError::MultiaddrNotSupported(addr));
}
let mut relayed_multiaddr = RelayedMultiaddr::default();
let mut before_circuit = true;
for protocol in addr.into_iter() {
match protocol {
Protocol::P2pCircuit => {
if before_circuit {
before_circuit = false;
} else {
return Err(RelayError::MultipleCircuitRelayProtocolsUnsupported.into());
}
}
Protocol::P2p(hash) => {
let peer_id = PeerId::from_multihash(hash).map_err(|_| RelayError::InvalidHash)?;
if before_circuit {
if relayed_multiaddr.relay_peer_id.is_some() {
return Err(RelayError::MalformedMultiaddr.into());
}
relayed_multiaddr.relay_peer_id = Some(peer_id)
} else {
if relayed_multiaddr.dst_peer_id.is_some() {
return Err(RelayError::MalformedMultiaddr.into());
}
relayed_multiaddr.dst_peer_id = Some(peer_id)
}
}
p => {
if before_circuit {
relayed_multiaddr
.relay_addr
.get_or_insert(Multiaddr::empty())
.push(p);
} else {
relayed_multiaddr
.dst_addr
.get_or_insert(Multiaddr::empty())
.push(p);
}
}
}
}
Ok(relayed_multiaddr)
}
pub struct RelayListener {
listener_id: ListenerId,
queued_events: VecDeque<<Self as Stream>::Item>,
from_behaviour: mpsc::Receiver<ToListenerMsg>,
is_closed: bool,
}
impl RelayListener {
fn close(&mut self, reason: Result<(), RelayError>) {
self.queued_events
.push_back(TransportEvent::ListenerClosed {
listener_id: self.listener_id,
reason,
});
self.is_closed = true;
}
}
impl Stream for RelayListener {
type Item = TransportEvent<<ClientTransport as Transport>::ListenerUpgrade, RelayError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
if let Some(event) = self.queued_events.pop_front() {
return Poll::Ready(Some(event));
}
if self.is_closed {
return Poll::Ready(None);
}
let msg = match ready!(self.from_behaviour.poll_next_unpin(cx)) {
Some(msg) => msg,
None => {
self.close(Ok(()));
continue;
}
};
match msg {
ToListenerMsg::Reservation(Ok(Reservation { addrs })) => {
debug_assert!(
self.queued_events.is_empty(),
"Assert empty due to previous `pop_front` attempt."
);
self.queued_events = addrs
.into_iter()
.map(|listen_addr| TransportEvent::NewAddress {
listener_id: self.listener_id,
listen_addr,
})
.collect();
}
ToListenerMsg::IncomingRelayedConnection {
stream,
src_peer_id,
relay_addr,
relay_peer_id: _,
} => {
let listener_id = self.listener_id;
self.queued_events.push_back(TransportEvent::Incoming {
upgrade: ready(Ok(stream)),
listener_id,
local_addr: relay_addr.with(Protocol::P2pCircuit),
send_back_addr: Protocol::P2p(src_peer_id.into()).into(),
})
}
ToListenerMsg::Reservation(Err(())) => self.close(Err(RelayError::Reservation)),
};
}
}
}
pub type RelayedDial = BoxFuture<'static, Result<RelayedConnection, RelayError>>;
#[derive(Debug, Error)]
pub enum RelayError {
#[error("Missing relay peer id.")]
MissingRelayPeerId,
#[error("Missing relay address.")]
MissingRelayAddr,
#[error("Missing destination peer id.")]
MissingDstPeerId,
#[error("Invalid peer id hash.")]
InvalidHash,
#[error("Failed to send message to relay behaviour: {0:?}")]
SendingMessageToBehaviour(#[from] mpsc::SendError),
#[error("Response from behaviour was canceled")]
ResponseFromBehaviourCanceled(#[from] oneshot::Canceled),
#[error(
"Address contains multiple circuit relay protocols (`p2p-circuit`) which is not supported."
)]
MultipleCircuitRelayProtocolsUnsupported,
#[error("One of the provided multiaddresses is malformed.")]
MalformedMultiaddr,
#[error("Failed to get Reservation.")]
Reservation,
#[error("Failed to connect to destination.")]
Connect,
}
impl From<RelayError> for TransportError<RelayError> {
fn from(error: RelayError) -> Self {
TransportError::Other(error)
}
}
pub enum TransportToBehaviourMsg {
DialReq {
request_id: RequestId,
relay_addr: Multiaddr,
relay_peer_id: PeerId,
dst_addr: Option<Multiaddr>,
dst_peer_id: PeerId,
send_back: oneshot::Sender<Result<RelayedConnection, ()>>,
},
ListenReq {
relay_peer_id: PeerId,
relay_addr: Multiaddr,
to_listener: mpsc::Sender<ToListenerMsg>,
},
}
#[allow(clippy::large_enum_variant)]
pub enum ToListenerMsg {
Reservation(Result<Reservation, ()>),
IncomingRelayedConnection {
stream: RelayedConnection,
src_peer_id: PeerId,
relay_peer_id: PeerId,
relay_addr: Multiaddr,
},
}
pub struct Reservation {
pub(crate) addrs: Vec<Multiaddr>,
}