-
Notifications
You must be signed in to change notification settings - Fork 990
/
transport.rs
854 lines (780 loc) · 28.8 KB
/
transport.rs
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
// Copyright 2017-2020 Parity Technologies (UK) Ltd.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use crate::endpoint::{Config, QuinnConfig, ToEndpoint};
use crate::provider::Provider;
use crate::{endpoint, Connecting, Connection, Error};
use futures::channel::{mpsc, oneshot};
use futures::future::BoxFuture;
use futures::ready;
use futures::stream::StreamExt;
use futures::{prelude::*, stream::SelectAll};
use if_watch::IfEvent;
use libp2p_core::{
multiaddr::{Multiaddr, Protocol},
transport::{ListenerId, TransportError, TransportEvent},
PeerId, Transport,
};
use std::collections::hash_map::{DefaultHasher, Entry};
use std::collections::{HashMap, VecDeque};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::net::IpAddr;
use std::task::Waker;
use std::time::Duration;
use std::{
net::SocketAddr,
pin::Pin,
task::{Context, Poll},
};
/// Implementation of the [`Transport`] trait for QUIC.
///
/// By default only QUIC Version 1 (RFC 9000) is supported. In the [`Multiaddr`] this maps to
/// [`libp2p_core::multiaddr::Protocol::QuicV1`].
/// The [`libp2p_core::multiaddr::Protocol::Quic`] codepoint is interpreted as QUIC version
/// draft-29 and only supported if [`Config::support_draft_29`] is set to `true`.
/// Note that in that case servers support both version an all QUIC listening addresses.
///
/// Version draft-29 should only be used to connect to nodes from other libp2p implementations
/// that do not support `QuicV1` yet. Support for it will be removed long-term.
/// See <https://github.com/multiformats/multiaddr/issues/145>.
#[derive(Debug)]
pub struct GenTransport<P: Provider> {
/// Config for the inner [`quinn_proto`] structs.
quinn_config: QuinnConfig,
/// Timeout for the [`Connecting`] future.
handshake_timeout: Duration,
/// Whether draft-29 is supported for dialing and listening.
support_draft_29: bool,
/// Streams of active [`Listener`]s.
listeners: SelectAll<Listener<P>>,
/// Dialer for each socket family if no matching listener exists.
dialer: HashMap<SocketFamily, Dialer>,
}
impl<P: Provider> GenTransport<P> {
/// Create a new [`GenTransport`] with the given [`Config`].
pub fn new(config: Config) -> Self {
let handshake_timeout = config.handshake_timeout;
let support_draft_29 = config.support_draft_29;
let quinn_config = config.into();
Self {
listeners: SelectAll::new(),
quinn_config,
handshake_timeout,
dialer: HashMap::new(),
support_draft_29,
}
}
}
impl<P: Provider> Transport for GenTransport<P> {
type Output = (PeerId, Connection);
type Error = Error;
type ListenerUpgrade = Connecting;
type Dial = BoxFuture<'static, Result<Self::Output, Self::Error>>;
fn listen_on(&mut self, addr: Multiaddr) -> Result<ListenerId, TransportError<Self::Error>> {
let (socket_addr, version) = multiaddr_to_socketaddr(&addr, self.support_draft_29)
.ok_or(TransportError::MultiaddrNotSupported(addr))?;
let listener_id = ListenerId::new();
let listener = Listener::new(
listener_id,
socket_addr,
self.quinn_config.clone(),
self.handshake_timeout,
version,
)?;
self.listeners.push(listener);
// Remove dialer endpoint so that the endpoint is dropped once the last
// connection that uses it is closed.
// New outbound connections will use the bidirectional (listener) endpoint.
self.dialer.remove(&socket_addr.ip().into());
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) {
// Close the listener, which will eventually finish its stream.
// `SelectAll` removes streams once they are finished.
listener.close(Ok(()));
true
} else {
false
}
}
fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if !is_quic_addr(listen) || !is_quic_addr(observed) {
return None;
}
Some(observed.clone())
}
fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let (socket_addr, version) = multiaddr_to_socketaddr(&addr, self.support_draft_29)
.ok_or_else(|| TransportError::MultiaddrNotSupported(addr.clone()))?;
if socket_addr.port() == 0 || socket_addr.ip().is_unspecified() {
return Err(TransportError::MultiaddrNotSupported(addr));
}
let mut listeners = self
.listeners
.iter_mut()
.filter(|l| {
if l.is_closed {
return false;
}
let listen_addr = l.endpoint_channel.socket_addr();
SocketFamily::is_same(&listen_addr.ip(), &socket_addr.ip())
&& listen_addr.ip().is_loopback() == socket_addr.ip().is_loopback()
})
.collect::<Vec<_>>();
let dialer_state = match listeners.len() {
0 => {
// No listener. Get or create an explicit dialer.
let socket_family = socket_addr.ip().into();
let dialer = match self.dialer.entry(socket_family) {
Entry::Occupied(occupied) => occupied.into_mut(),
Entry::Vacant(vacant) => {
vacant.insert(Dialer::new::<P>(self.quinn_config.clone(), socket_family)?)
}
};
&mut dialer.state
}
1 => &mut listeners[0].dialer_state,
_ => {
// Pick any listener to use for dialing.
// We hash the socket address to achieve determinism.
let mut hasher = DefaultHasher::new();
socket_addr.hash(&mut hasher);
let index = hasher.finish() as usize % listeners.len();
&mut listeners[index].dialer_state
}
};
Ok(dialer_state.new_dial(socket_addr, self.handshake_timeout, version))
}
fn dial_as_listener(
&mut self,
addr: Multiaddr,
) -> Result<Self::Dial, TransportError<Self::Error>> {
// TODO: As the listener of a QUIC hole punch, we need to send a random UDP packet to the
// `addr`. See DCUtR specification below.
//
// https://github.com/libp2p/specs/blob/master/relay/DCUtR.md#the-protocol
Err(TransportError::MultiaddrNotSupported(addr))
}
fn poll(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<TransportEvent<Self::ListenerUpgrade, Self::Error>> {
let mut errored = Vec::new();
for (key, dialer) in &mut self.dialer {
if let Poll::Ready(_error) = dialer.poll(cx) {
errored.push(*key);
}
}
for key in errored {
// Endpoint driver of dialer crashed.
// Drop dialer and all pending dials so that the connection receiver is notified.
self.dialer.remove(&key);
}
match self.listeners.poll_next_unpin(cx) {
Poll::Ready(Some(ev)) => Poll::Ready(ev),
_ => Poll::Pending,
}
}
}
impl From<Error> for TransportError<Error> {
fn from(err: Error) -> Self {
TransportError::Other(err)
}
}
/// Dialer for addresses if no matching listener exists.
#[derive(Debug)]
struct Dialer {
/// Channel to the [`crate::endpoint::Driver`] that
/// is driving the endpoint.
endpoint_channel: endpoint::Channel,
/// Queued dials for the endpoint.
state: DialerState,
}
impl Dialer {
fn new<P: Provider>(
config: QuinnConfig,
socket_family: SocketFamily,
) -> Result<Self, TransportError<Error>> {
let endpoint_channel = endpoint::Channel::new_dialer::<P>(config, socket_family)
.map_err(TransportError::Other)?;
Ok(Dialer {
endpoint_channel,
state: DialerState::default(),
})
}
fn poll(&mut self, cx: &mut Context<'_>) -> Poll<Error> {
self.state.poll(&mut self.endpoint_channel, cx)
}
}
impl Drop for Dialer {
fn drop(&mut self) {
self.endpoint_channel.send_on_drop(ToEndpoint::Decoupled);
}
}
/// Pending dials to be sent to the endpoint was the [`endpoint::Channel`]
/// has capacity
#[derive(Default, Debug)]
struct DialerState {
pending_dials: VecDeque<ToEndpoint>,
waker: Option<Waker>,
}
impl DialerState {
fn new_dial(
&mut self,
address: SocketAddr,
timeout: Duration,
version: ProtocolVersion,
) -> BoxFuture<'static, Result<(PeerId, Connection), Error>> {
let (rx, tx) = oneshot::channel();
let message = ToEndpoint::Dial {
addr: address,
result: rx,
version,
};
self.pending_dials.push_back(message);
if let Some(waker) = self.waker.take() {
waker.wake();
}
async move {
// Our oneshot getting dropped means the message didn't make it to the endpoint driver.
let connection = tx.await.map_err(|_| Error::EndpointDriverCrashed)??;
let (peer, connection) = Connecting::new(connection, timeout).await?;
Ok((peer, connection))
}
.boxed()
}
/// Send all pending dials into the given [`endpoint::Channel`].
///
/// This only ever returns [`Poll::Pending`], or an error in case the channel is closed.
fn poll(&mut self, channel: &mut endpoint::Channel, cx: &mut Context<'_>) -> Poll<Error> {
while let Some(to_endpoint) = self.pending_dials.pop_front() {
match channel.try_send(to_endpoint, cx) {
Ok(Ok(())) => {}
Ok(Err(to_endpoint)) => {
self.pending_dials.push_front(to_endpoint);
break;
}
Err(endpoint::Disconnected {}) => return Poll::Ready(Error::EndpointDriverCrashed),
}
}
self.waker = Some(cx.waker().clone());
Poll::Pending
}
}
/// Listener for incoming connections.
struct Listener<P: Provider> {
/// Id of the listener.
listener_id: ListenerId,
version: ProtocolVersion,
/// Channel to the endpoint to initiate dials.
endpoint_channel: endpoint::Channel,
/// Queued dials.
dialer_state: DialerState,
/// Channel where new connections are being sent.
new_connections_rx: mpsc::Receiver<Connection>,
/// Timeout for connection establishment on inbound connections.
handshake_timeout: Duration,
/// Watcher for network interface changes.
///
/// None if we are only listening on a single interface.
if_watcher: Option<P::IfWatcher>,
/// Whether the listener was closed and the stream should terminate.
is_closed: bool,
/// Pending event to reported.
pending_event: Option<<Self as Stream>::Item>,
}
impl<P: Provider> Listener<P> {
fn new(
listener_id: ListenerId,
socket_addr: SocketAddr,
config: QuinnConfig,
handshake_timeout: Duration,
version: ProtocolVersion,
) -> Result<Self, Error> {
let (endpoint_channel, new_connections_rx) =
endpoint::Channel::new_bidirectional::<P>(config, socket_addr)?;
let if_watcher;
let pending_event;
if socket_addr.ip().is_unspecified() {
if_watcher = Some(P::new_if_watcher()?);
pending_event = None;
} else {
if_watcher = None;
let ma = socketaddr_to_multiaddr(endpoint_channel.socket_addr(), version);
pending_event = Some(TransportEvent::NewAddress {
listener_id,
listen_addr: ma,
})
}
Ok(Listener {
endpoint_channel,
listener_id,
version,
new_connections_rx,
handshake_timeout,
if_watcher,
is_closed: false,
pending_event,
dialer_state: DialerState::default(),
})
}
/// Report the listener as closed in a [`TransportEvent::ListenerClosed`] and
/// terminate the stream.
fn close(&mut self, reason: Result<(), Error>) {
if self.is_closed {
return;
}
self.pending_event = Some(TransportEvent::ListenerClosed {
listener_id: self.listener_id,
reason,
});
self.is_closed = true;
}
/// Poll for a next If Event.
fn poll_if_addr(&mut self, cx: &mut Context<'_>) -> Poll<<Self as Stream>::Item> {
let if_watcher = match self.if_watcher.as_mut() {
Some(iw) => iw,
None => return Poll::Pending,
};
loop {
match ready!(P::poll_if_event(if_watcher, cx)) {
Ok(IfEvent::Up(inet)) => {
if let Some(listen_addr) = ip_to_listenaddr(
self.endpoint_channel.socket_addr(),
inet.addr(),
self.version,
) {
log::debug!("New listen address: {}", listen_addr);
return Poll::Ready(TransportEvent::NewAddress {
listener_id: self.listener_id,
listen_addr,
});
}
}
Ok(IfEvent::Down(inet)) => {
if let Some(listen_addr) = ip_to_listenaddr(
self.endpoint_channel.socket_addr(),
inet.addr(),
self.version,
) {
log::debug!("Expired listen address: {}", listen_addr);
return Poll::Ready(TransportEvent::AddressExpired {
listener_id: self.listener_id,
listen_addr,
});
}
}
Err(err) => {
return Poll::Ready(TransportEvent::ListenerError {
listener_id: self.listener_id,
error: err.into(),
})
}
}
}
}
/// Poll [`DialerState`] to initiate requested dials.
fn poll_dialer(&mut self, cx: &mut Context<'_>) -> Poll<Error> {
let Self {
dialer_state,
endpoint_channel,
..
} = self;
dialer_state.poll(endpoint_channel, cx)
}
}
impl<P: Provider> Stream for Listener<P> {
type Item = TransportEvent<Connecting, Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
loop {
if let Some(event) = self.pending_event.take() {
return Poll::Ready(Some(event));
}
if self.is_closed {
return Poll::Ready(None);
}
match self.poll_if_addr(cx) {
Poll::Ready(event) => return Poll::Ready(Some(event)),
Poll::Pending => {}
}
match self.poll_dialer(cx) {
Poll::Ready(error) => {
self.close(Err(error));
continue;
}
Poll::Pending => {}
}
match self.new_connections_rx.poll_next_unpin(cx) {
Poll::Ready(Some(connection)) => {
let local_addr = socketaddr_to_multiaddr(connection.local_addr(), self.version);
let send_back_addr =
socketaddr_to_multiaddr(&connection.remote_addr(), self.version);
let event = TransportEvent::Incoming {
upgrade: Connecting::new(connection, self.handshake_timeout),
local_addr,
send_back_addr,
listener_id: self.listener_id,
};
return Poll::Ready(Some(event));
}
Poll::Ready(None) => {
self.close(Err(Error::EndpointDriverCrashed));
continue;
}
Poll::Pending => {}
};
return Poll::Pending;
}
}
}
impl<P: Provider> fmt::Debug for Listener<P> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Listener")
.field("listener_id", &self.listener_id)
.field("endpoint_channel", &self.endpoint_channel)
.field("dialer_state", &self.dialer_state)
.field("new_connections_rx", &self.new_connections_rx)
.field("handshake_timeout", &self.handshake_timeout)
.field("is_closed", &self.is_closed)
.field("pending_event", &self.pending_event)
.finish()
}
}
impl<P: Provider> Drop for Listener<P> {
fn drop(&mut self) {
self.endpoint_channel.send_on_drop(ToEndpoint::Decoupled);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolVersion {
V1, // i.e. RFC9000
Draft29,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SocketFamily {
Ipv4,
Ipv6,
}
impl SocketFamily {
fn is_same(a: &IpAddr, b: &IpAddr) -> bool {
matches!(
(a, b),
(IpAddr::V4(_), IpAddr::V4(_)) | (IpAddr::V6(_), IpAddr::V6(_))
)
}
}
impl From<IpAddr> for SocketFamily {
fn from(ip: IpAddr) -> Self {
match ip {
IpAddr::V4(_) => SocketFamily::Ipv4,
IpAddr::V6(_) => SocketFamily::Ipv6,
}
}
}
/// Turn an [`IpAddr`] reported by the interface watcher into a
/// listen-address for the endpoint.
///
/// For this, the `ip` is combined with the port that the endpoint
/// is actually bound.
///
/// Returns `None` if the `ip` is not the same socket family as the
/// address that the endpoint is bound to.
fn ip_to_listenaddr(
endpoint_addr: &SocketAddr,
ip: IpAddr,
version: ProtocolVersion,
) -> Option<Multiaddr> {
// True if either both addresses are Ipv4 or both Ipv6.
if !SocketFamily::is_same(&endpoint_addr.ip(), &ip) {
return None;
}
let socket_addr = SocketAddr::new(ip, endpoint_addr.port());
Some(socketaddr_to_multiaddr(&socket_addr, version))
}
/// Tries to turn a QUIC multiaddress into a UDP [`SocketAddr`]. Returns None if the format
/// of the multiaddr is wrong.
fn multiaddr_to_socketaddr(
addr: &Multiaddr,
support_draft_29: bool,
) -> Option<(SocketAddr, ProtocolVersion)> {
let mut iter = addr.iter();
let proto1 = iter.next()?;
let proto2 = iter.next()?;
let proto3 = iter.next()?;
for proto in iter {
match proto {
Protocol::P2p(_) => {} // Ignore a `/p2p/...` prefix of possibly outer protocols, if present.
_ => return None,
}
}
let version = match proto3 {
Protocol::QuicV1 => ProtocolVersion::V1,
Protocol::Quic if support_draft_29 => ProtocolVersion::Draft29,
_ => return None,
};
match (proto1, proto2) {
(Protocol::Ip4(ip), Protocol::Udp(port)) => {
Some((SocketAddr::new(ip.into(), port), version))
}
(Protocol::Ip6(ip), Protocol::Udp(port)) => {
Some((SocketAddr::new(ip.into(), port), version))
}
_ => None,
}
}
/// Whether an [`Multiaddr`] is a valid for the QUIC transport.
fn is_quic_addr(addr: &Multiaddr) -> bool {
use Protocol::*;
let mut iter = addr.iter();
let first = match iter.next() {
Some(p) => p,
None => return false,
};
let second = match iter.next() {
Some(p) => p,
None => return false,
};
let third = match iter.next() {
Some(p) => p,
None => return false,
};
let fourth = iter.next();
let fifth = iter.next();
matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_))
&& matches!(second, Udp(_))
&& matches!(third, QuicV1)
&& matches!(fourth, Some(P2p(_)) | None)
&& matches!(fifth, None)
}
/// Turns an IP address and port into the corresponding QUIC multiaddr.
fn socketaddr_to_multiaddr(socket_addr: &SocketAddr, version: ProtocolVersion) -> Multiaddr {
let quic_proto = match version {
ProtocolVersion::V1 => Protocol::QuicV1,
ProtocolVersion::Draft29 => Protocol::Quic,
};
Multiaddr::empty()
.with(socket_addr.ip().into())
.with(Protocol::Udp(socket_addr.port()))
.with(quic_proto)
}
#[cfg(test)]
#[cfg(any(feature = "async-std", feature = "tokio"))]
mod test {
use futures::future::poll_fn;
use futures_timer::Delay;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};
use super::*;
#[test]
fn multiaddr_to_udp_conversion() {
assert!(multiaddr_to_socketaddr(
&"/ip4/127.0.0.1/udp/1234".parse::<Multiaddr>().unwrap(),
true
)
.is_none());
assert_eq!(
multiaddr_to_socketaddr(
&"/ip4/127.0.0.1/udp/12345/quic-v1"
.parse::<Multiaddr>()
.unwrap(),
false
),
Some((
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 12345,),
ProtocolVersion::V1
))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip4/255.255.255.255/udp/8080/quic-v1"
.parse::<Multiaddr>()
.unwrap(),
false
),
Some((
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(255, 255, 255, 255)), 8080,),
ProtocolVersion::V1
))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip4/127.0.0.1/udp/55148/quic-v1/p2p/12D3KooW9xk7Zp1gejwfwNpfm6L9zH5NL4Bx5rm94LRYJJHJuARZ"
.parse::<Multiaddr>()
.unwrap(), false
),
Some((SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
55148,
), ProtocolVersion::V1))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip6/::1/udp/12345/quic-v1".parse::<Multiaddr>().unwrap(),
false
),
Some((
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 12345,),
ProtocolVersion::V1
))
);
assert_eq!(
multiaddr_to_socketaddr(
&"/ip6/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/udp/8080/quic-v1"
.parse::<Multiaddr>()
.unwrap(),
false
),
Some((
SocketAddr::new(
IpAddr::V6(Ipv6Addr::new(
65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535,
)),
8080,
),
ProtocolVersion::V1
))
);
assert!(multiaddr_to_socketaddr(
&"/ip4/127.0.0.1/udp/1234/quic".parse::<Multiaddr>().unwrap(),
false
)
.is_none());
assert_eq!(
multiaddr_to_socketaddr(
&"/ip4/127.0.0.1/udp/1234/quic".parse::<Multiaddr>().unwrap(),
true
),
Some((
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 1234,),
ProtocolVersion::Draft29
))
);
}
#[cfg(feature = "async-std")]
#[async_std::test]
async fn test_close_listener() {
let keypair = libp2p_core::identity::Keypair::generate_ed25519();
let config = Config::new(&keypair);
let mut transport = crate::async_std::Transport::new(config);
assert!(poll_fn(|cx| Pin::new(&mut transport).as_mut().poll(cx))
.now_or_never()
.is_none());
// Run test twice to check that there is no unexpected behaviour if `Transport.listener`
// is temporarily empty.
for _ in 0..2 {
let id = transport
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap())
.unwrap();
// Copy channel to use it later.
let mut channel = transport
.listeners
.iter()
.next()
.unwrap()
.endpoint_channel
.clone();
match poll_fn(|cx| Pin::new(&mut transport).as_mut().poll(cx)).await {
TransportEvent::NewAddress {
listener_id,
listen_addr,
} => {
assert_eq!(listener_id, id);
assert!(
matches!(listen_addr.iter().next(), Some(Protocol::Ip4(a)) if !a.is_unspecified())
);
assert!(
matches!(listen_addr.iter().nth(1), Some(Protocol::Udp(port)) if port != 0)
);
assert!(matches!(listen_addr.iter().nth(2), Some(Protocol::QuicV1)));
}
e => panic!("Unexpected event: {e:?}"),
}
assert!(transport.remove_listener(id), "Expect listener to exist.");
match poll_fn(|cx| Pin::new(&mut transport).as_mut().poll(cx)).await {
TransportEvent::ListenerClosed {
listener_id,
reason: Ok(()),
} => {
assert_eq!(listener_id, id);
}
e => panic!("Unexpected event: {e:?}"),
}
// Poll once again so that the listener has the chance to return `Poll::Ready(None)` and
// be removed from the list of listeners.
assert!(poll_fn(|cx| Pin::new(&mut transport).as_mut().poll(cx))
.now_or_never()
.is_none());
assert!(transport.listeners.is_empty());
// Check that the [`Driver`] has shut down.
Delay::new(Duration::from_millis(10)).await;
poll_fn(|cx| {
assert!(channel.try_send(ToEndpoint::Decoupled, cx).is_err());
Poll::Ready(())
})
.await;
}
}
#[cfg(feature = "tokio")]
#[tokio::test]
async fn test_dialer_drop() {
let keypair = libp2p_core::identity::Keypair::generate_ed25519();
let config = Config::new(&keypair);
let mut transport = crate::tokio::Transport::new(config);
let _dial = transport
.dial("/ip4/123.45.67.8/udp/1234/quic-v1".parse().unwrap())
.unwrap();
// Expect a dialer and its background task to exist.
let mut channel = transport
.dialer
.get(&SocketFamily::Ipv4)
.unwrap()
.endpoint_channel
.clone();
assert!(!transport.dialer.contains_key(&SocketFamily::Ipv6));
// Send dummy dial to check that the endpoint driver is running.
poll_fn(|cx| {
let (tx, _) = oneshot::channel();
let _ = channel
.try_send(
ToEndpoint::Dial {
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), 0),
result: tx,
version: ProtocolVersion::V1,
},
cx,
)
.unwrap();
Poll::Ready(())
})
.await;
// Start listening so that the dialer and driver are dropped.
let _ = transport
.listen_on("/ip4/0.0.0.0/udp/0/quic-v1".parse().unwrap())
.unwrap();
assert!(!transport.dialer.contains_key(&SocketFamily::Ipv4));
// Check that the [`Driver`] has shut down.
Delay::new(Duration::from_millis(10)).await;
poll_fn(|cx| {
assert!(channel.try_send(ToEndpoint::Decoupled, cx).is_err());
Poll::Ready(())
})
.await;
}
}