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
// Copyright 2024 litep2p developers
//
// 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.

//! Shared socket listener between TCP and WebSocket.

use crate::{
    error::{AddressError, DnsError},
    PeerId,
};

use futures::Stream;
use hickory_resolver::{
    config::{ResolverConfig, ResolverOpts},
    TokioAsyncResolver,
};
use multiaddr::{Multiaddr, Protocol};
use network_interface::{Addr, NetworkInterface, NetworkInterfaceConfig};
use socket2::{Domain, Socket, Type};
use tokio::net::{TcpListener as TokioTcpListener, TcpStream};

use std::{
    io,
    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr},
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
};

/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::transport::listener";

/// Address type.
#[derive(Debug)]
pub enum AddressType {
    /// Socket address.
    Socket(SocketAddr),

    /// DNS address.
    Dns {
        address: String,
        port: u16,
        dns_type: DnsType,
    },
}

/// The DNS type of the address.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DnsType {
    /// DNS supports both IPv4 and IPv6.
    Dns,
    /// DNS supports only IPv4.
    Dns4,
    /// DNS supports only IPv6.
    Dns6,
}

impl AddressType {
    /// Resolve the address to a concrete IP.
    pub async fn lookup_ip(self) -> Result<SocketAddr, DnsError> {
        let (url, port, dns_type) = match self {
            // We already have the IP address.
            AddressType::Socket(address) => return Ok(address),
            AddressType::Dns {
                address,
                port,
                dns_type,
            } => (address, port, dns_type),
        };

        let lookup =
            match TokioAsyncResolver::tokio(ResolverConfig::default(), ResolverOpts::default())
                .lookup_ip(url.clone())
                .await
            {
                Ok(lookup) => lookup,
                Err(error) => {
                    tracing::debug!(
                        target: LOG_TARGET,
                        ?error,
                        "failed to resolve DNS address `{}`",
                        url
                    );

                    return Err(DnsError::ResolveError(url));
                }
            };

        let Some(ip) = lookup.iter().find(|ip| match dns_type {
            DnsType::Dns => true,
            DnsType::Dns4 => ip.is_ipv4(),
            DnsType::Dns6 => ip.is_ipv6(),
        }) else {
            tracing::debug!(
                target: LOG_TARGET,
                "Multiaddr DNS type does not match IP version `{}`",
                url
            );
            return Err(DnsError::IpVersionMismatch);
        };

        Ok(SocketAddr::new(ip, port))
    }
}

/// Local addresses to use for outbound connections.
#[derive(Clone, Default)]
pub enum DialAddresses {
    /// Reuse port from listen addresses.
    Reuse {
        listen_addresses: Arc<Vec<SocketAddr>>,
    },
    /// Do not reuse port.
    #[default]
    NoReuse,
}

impl DialAddresses {
    /// Get local dial address for an outbound connection.
    pub fn local_dial_address(&self, remote_address: &IpAddr) -> Result<Option<SocketAddr>, ()> {
        match self {
            DialAddresses::Reuse { listen_addresses } => {
                for address in listen_addresses.iter() {
                    if remote_address.is_ipv4() == address.is_ipv4()
                        && remote_address.is_loopback() == address.ip().is_loopback()
                    {
                        if remote_address.is_ipv4() {
                            return Ok(Some(SocketAddr::new(
                                IpAddr::V4(Ipv4Addr::UNSPECIFIED),
                                address.port(),
                            )));
                        } else {
                            return Ok(Some(SocketAddr::new(
                                IpAddr::V6(Ipv6Addr::UNSPECIFIED),
                                address.port(),
                            )));
                        }
                    }
                }

                Err(())
            }
            DialAddresses::NoReuse => Ok(None),
        }
    }
}

/// Socket listening to zero or more addresses.
pub struct SocketListener {
    /// Listeners.
    listeners: Vec<TokioTcpListener>,
    /// The index in the listeners from which the polling is resumed.
    poll_index: usize,
}

/// Trait to convert between `Multiaddr` and `SocketAddr`.
pub trait GetSocketAddr {
    /// Convert `Multiaddr` to `SocketAddr`.
    ///
    /// # Note
    ///
    /// This method is called from two main code paths:
    ///  - When creating a new `SocketListener` to bind to a specific address.
    ///  - When dialing a new connection to a remote address.
    ///
    /// The `AddressType` is either `SocketAddr` or a `Dns` address.
    /// For the `Dns` the concrete IP address is resolved later in our code.
    ///
    /// The `PeerId` is optional and may not be present.
    fn multiaddr_to_socket_address(
        address: &Multiaddr,
    ) -> Result<(AddressType, Option<PeerId>), AddressError>;

    /// Convert concrete `SocketAddr` to `Multiaddr`.
    fn socket_address_to_multiaddr(address: &SocketAddr) -> Multiaddr;
}

/// TCP helper to convert between `Multiaddr` and `SocketAddr`.
pub struct TcpAddress;

impl GetSocketAddr for TcpAddress {
    fn multiaddr_to_socket_address(
        address: &Multiaddr,
    ) -> Result<(AddressType, Option<PeerId>), AddressError> {
        multiaddr_to_socket_address(address, SocketListenerType::Tcp)
    }

    fn socket_address_to_multiaddr(address: &SocketAddr) -> Multiaddr {
        Multiaddr::empty()
            .with(Protocol::from(address.ip()))
            .with(Protocol::Tcp(address.port()))
    }
}

/// WebSocket helper to convert between `Multiaddr` and `SocketAddr`.
pub struct WebSocketAddress;

impl GetSocketAddr for WebSocketAddress {
    fn multiaddr_to_socket_address(
        address: &Multiaddr,
    ) -> Result<(AddressType, Option<PeerId>), AddressError> {
        multiaddr_to_socket_address(address, SocketListenerType::WebSocket)
    }

    fn socket_address_to_multiaddr(address: &SocketAddr) -> Multiaddr {
        Multiaddr::empty()
            .with(Protocol::from(address.ip()))
            .with(Protocol::Tcp(address.port()))
            .with(Protocol::Ws(std::borrow::Cow::Borrowed("/")))
    }
}

impl SocketListener {
    /// Create new [`SocketListener`]
    pub fn new<T: GetSocketAddr>(
        addresses: Vec<Multiaddr>,
        reuse_port: bool,
        nodelay: bool,
    ) -> (Self, Vec<Multiaddr>, DialAddresses) {
        let (listeners, listen_addresses): (_, Vec<Vec<_>>) = addresses
            .into_iter()
            .filter_map(|address| {
                let address = match T::multiaddr_to_socket_address(&address).ok()?.0 {
                    AddressType::Dns { address, port, .. } => {
                        tracing::debug!(
                            target: LOG_TARGET,
                            ?address,
                            ?port,
                            "dns not supported as bind address"
                        );

                        return None;
                    }
                    AddressType::Socket(address) => address,
                };

                let socket = if address.is_ipv4() {
                    Socket::new(Domain::IPV4, Type::STREAM, Some(socket2::Protocol::TCP)).ok()?
                } else {
                    let socket =
                        Socket::new(Domain::IPV6, Type::STREAM, Some(socket2::Protocol::TCP))
                            .ok()?;
                    socket.set_only_v6(true).ok()?;
                    socket
                };

                socket.set_nodelay(nodelay).ok()?;
                socket.set_nonblocking(true).ok()?;
                socket.set_reuse_address(true).ok()?;
                #[cfg(unix)]
                if reuse_port {
                    socket.set_reuse_port(true).ok()?;
                }
                socket.bind(&address.into()).ok()?;
                socket.listen(1024).ok()?;

                let socket: std::net::TcpListener = socket.into();
                let listener = TokioTcpListener::from_std(socket).ok()?;
                let local_address = listener.local_addr().ok()?;

                let listen_addresses = if address.ip().is_unspecified() {
                    match NetworkInterface::show() {
                        Ok(ifaces) => ifaces
                            .into_iter()
                            .flat_map(|record| {
                                record.addr.into_iter().filter_map(|iface_address| {
                                    match (iface_address, address.is_ipv4()) {
                                        (Addr::V4(inner), true) => Some(SocketAddr::new(
                                            IpAddr::V4(inner.ip),
                                            local_address.port(),
                                        )),
                                        (Addr::V6(inner), false) =>
                                            match inner.ip.segments().first() {
                                                Some(0xfe80) => None,
                                                _ => Some(SocketAddr::new(
                                                    IpAddr::V6(inner.ip),
                                                    local_address.port(),
                                                )),
                                            },
                                        _ => None,
                                    }
                                })
                            })
                            .collect(),
                        Err(error) => {
                            tracing::warn!(
                                target: LOG_TARGET,
                                ?error,
                                "failed to fetch network interfaces",
                            );

                            return None;
                        }
                    }
                } else {
                    vec![local_address]
                };

                Some((listener, listen_addresses))
            })
            .unzip();

        let listen_addresses = listen_addresses.into_iter().flatten().collect::<Vec<_>>();
        let listen_multi_addresses =
            listen_addresses.iter().map(T::socket_address_to_multiaddr).collect();

        let dial_addresses = if reuse_port {
            DialAddresses::Reuse {
                listen_addresses: Arc::new(listen_addresses),
            }
        } else {
            DialAddresses::NoReuse
        };

        (
            Self {
                listeners,
                poll_index: 0,
            },
            listen_multi_addresses,
            dial_addresses,
        )
    }
}

/// The type of the socket listener.
#[derive(Clone, Copy, PartialEq, Eq)]
enum SocketListenerType {
    /// Listener for TCP.
    Tcp,
    /// Listener for WebSocket.
    WebSocket,
}

/// Extract socket address and `PeerId`, if found, from `address`.
fn multiaddr_to_socket_address(
    address: &Multiaddr,
    ty: SocketListenerType,
) -> Result<(AddressType, Option<PeerId>), AddressError> {
    tracing::trace!(target: LOG_TARGET, ?address, "parse multi address");

    let mut iter = address.iter();
    // Small helper to handle DNS types.
    let handle_dns_type =
        |address: String, dns_type: DnsType, protocol: Option<Protocol>| match protocol {
            Some(Protocol::Tcp(port)) => Ok(AddressType::Dns {
                address,
                port,
                dns_type,
            }),
            protocol => {
                tracing::error!(
                    target: LOG_TARGET,
                    ?protocol,
                    "invalid transport protocol, expected `Tcp`",
                );
                Err(AddressError::InvalidProtocol)
            }
        };

    let socket_address = match iter.next() {
        Some(Protocol::Ip6(address)) => match iter.next() {
            Some(Protocol::Tcp(port)) =>
                AddressType::Socket(SocketAddr::new(IpAddr::V6(address), port)),
            protocol => {
                tracing::error!(
                    target: LOG_TARGET,
                    ?protocol,
                    "invalid transport protocol, expected `Tcp`",
                );
                return Err(AddressError::InvalidProtocol);
            }
        },
        Some(Protocol::Ip4(address)) => match iter.next() {
            Some(Protocol::Tcp(port)) =>
                AddressType::Socket(SocketAddr::new(IpAddr::V4(address), port)),
            protocol => {
                tracing::error!(
                    target: LOG_TARGET,
                    ?protocol,
                    "invalid transport protocol, expected `Tcp`",
                );
                return Err(AddressError::InvalidProtocol);
            }
        },
        Some(Protocol::Dns(address)) => handle_dns_type(address.into(), DnsType::Dns, iter.next())?,
        Some(Protocol::Dns4(address)) =>
            handle_dns_type(address.into(), DnsType::Dns4, iter.next())?,
        Some(Protocol::Dns6(address)) =>
            handle_dns_type(address.into(), DnsType::Dns6, iter.next())?,
        protocol => {
            tracing::error!(target: LOG_TARGET, ?protocol, "invalid transport protocol");
            return Err(AddressError::InvalidProtocol);
        }
    };

    match ty {
        SocketListenerType::Tcp => (),
        SocketListenerType::WebSocket => {
            // verify that `/ws`/`/wss` is part of the multi address
            match iter.next() {
                Some(Protocol::Ws(_address)) => {}
                Some(Protocol::Wss(_address)) => {}
                protocol => {
                    tracing::error!(
                        target: LOG_TARGET,
                        ?protocol,
                        "invalid protocol, expected `Ws` or `Wss`"
                    );
                    return Err(AddressError::InvalidProtocol);
                }
            };
        }
    }

    let maybe_peer = match iter.next() {
        Some(Protocol::P2p(multihash)) =>
            Some(PeerId::from_multihash(multihash).map_err(AddressError::InvalidPeerId)?),
        None => None,
        protocol => {
            tracing::error!(
                target: LOG_TARGET,
                ?protocol,
                "invalid protocol, expected `P2p` or `None`"
            );
            return Err(AddressError::InvalidProtocol);
        }
    };

    Ok((socket_address, maybe_peer))
}

impl Stream for SocketListener {
    type Item = io::Result<(TcpStream, SocketAddr)>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        if self.listeners.is_empty() {
            return Poll::Pending;
        }

        let len = self.listeners.len();
        for index in 0..len {
            let current = (self.poll_index + index) % len;
            let listener = &mut self.listeners[current];

            match listener.poll_accept(cx) {
                Poll::Pending => {}
                Poll::Ready(Err(error)) => {
                    self.poll_index = (self.poll_index + 1) % len;
                    return Poll::Ready(Some(Err(error)));
                }
                Poll::Ready(Ok((stream, address))) => {
                    self.poll_index = (self.poll_index + 1) % len;
                    return Poll::Ready(Some(Ok((stream, address))));
                }
            }
        }

        self.poll_index = (self.poll_index + 1) % len;
        Poll::Pending
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use futures::StreamExt;

    #[test]
    fn parse_multiaddresses_tcp() {
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/tcp/8888".parse().expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/tcp/8888".parse().expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/tcp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/tcp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/udp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/udp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::Tcp,
        )
        .is_err());
    }

    #[test]
    fn parse_multiaddresses_websocket() {
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/tcp/8888/ws".parse().expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/tcp/8888/ws".parse().expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/tcp/8888/ws/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/tcp/8888/ws/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/udp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/udp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/ip4/127.0.0.1/tcp/8888/ws/utp".parse().expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/ip6/::1/tcp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/dns/hello.world/tcp/8888/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
            SocketListenerType::WebSocket,
        )
        .is_err());
        assert!(multiaddr_to_socket_address(
            &"/dns6/hello.world/tcp/8888/ws/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress")
                ,SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/dns4/hello.world/tcp/8888/ws/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
                SocketListenerType::WebSocket,
        )
        .is_ok());
        assert!(multiaddr_to_socket_address(
            &"/dns6/hello.world/tcp/8888/ws/p2p/12D3KooWT2ouvz5uMmCvHJGzAGRHiqDts5hzXR7NdoQ27pGdzp9Q"
                .parse()
                .expect("valid multiaddress"),
                SocketListenerType::WebSocket,
        )
        .is_ok());
    }

    #[tokio::test]
    async fn no_listeners_tcp() {
        let (mut listener, _, _) = SocketListener::new::<TcpAddress>(Vec::new(), true, false);

        futures::future::poll_fn(|cx| match listener.poll_next_unpin(cx) {
            Poll::Pending => Poll::Ready(()),
            event => panic!("unexpected event: {event:?}"),
        })
        .await;
    }

    #[tokio::test]
    async fn no_listeners_websocket() {
        let (mut listener, _, _) = SocketListener::new::<WebSocketAddress>(Vec::new(), true, false);

        futures::future::poll_fn(|cx| match listener.poll_next_unpin(cx) {
            Poll::Pending => Poll::Ready(()),
            event => panic!("unexpected event: {event:?}"),
        })
        .await;
    }

    #[tokio::test]
    async fn one_listener_tcp() {
        let address: Multiaddr = "/ip6/::1/tcp/0".parse().unwrap();
        let (mut listener, listen_addresses, _) =
            SocketListener::new::<TcpAddress>(vec![address.clone()], true, false);

        let Some(Protocol::Tcp(port)) =
            listen_addresses.iter().next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        let (res1, res2) =
            tokio::join!(listener.next(), TcpStream::connect(format!("[::1]:{port}")));

        assert!(res1.unwrap().is_ok() && res2.is_ok());
    }

    #[tokio::test]
    async fn one_listener_websocket() {
        let address: Multiaddr = "/ip6/::1/tcp/0/ws".parse().unwrap();
        let (mut listener, listen_addresses, _) =
            SocketListener::new::<WebSocketAddress>(vec![address.clone()], true, false);
        let Some(Protocol::Tcp(port)) =
            listen_addresses.iter().next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        let (res1, res2) =
            tokio::join!(listener.next(), TcpStream::connect(format!("[::1]:{port}")));

        assert!(res1.unwrap().is_ok() && res2.is_ok());
    }

    #[tokio::test]
    async fn two_listeners_tcp() {
        let address1: Multiaddr = "/ip6/::1/tcp/0".parse().unwrap();
        let address2: Multiaddr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
        let (mut listener, listen_addresses, _) =
            SocketListener::new::<TcpAddress>(vec![address1, address2], true, false);
        let Some(Protocol::Tcp(port1)) =
            listen_addresses.iter().next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        let Some(Protocol::Tcp(port2)) =
            listen_addresses.iter().skip(1).next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        tokio::spawn(async move { while let Some(_) = listener.next().await {} });

        let (res1, res2) = tokio::join!(
            TcpStream::connect(format!("[::1]:{port1}")),
            TcpStream::connect(format!("127.0.0.1:{port2}"))
        );

        assert!(res1.is_ok() && res2.is_ok());
    }

    #[tokio::test]
    async fn two_listeners_websocket() {
        let address1: Multiaddr = "/ip6/::1/tcp/0/ws".parse().unwrap();
        let address2: Multiaddr = "/ip4/127.0.0.1/tcp/0/ws".parse().unwrap();
        let (mut listener, listen_addresses, _) =
            SocketListener::new::<WebSocketAddress>(vec![address1, address2], true, false);

        let Some(Protocol::Tcp(port1)) =
            listen_addresses.iter().next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        let Some(Protocol::Tcp(port2)) =
            listen_addresses.iter().skip(1).next().unwrap().clone().iter().skip(1).next()
        else {
            panic!("invalid address");
        };

        tokio::spawn(async move { while let Some(_) = listener.next().await {} });

        let (res1, res2) = tokio::join!(
            TcpStream::connect(format!("[::1]:{port1}")),
            TcpStream::connect(format!("127.0.0.1:{port2}"))
        );

        assert!(res1.is_ok() && res2.is_ok());
    }

    #[tokio::test]
    async fn local_dial_address() {
        let dial_addresses = DialAddresses::Reuse {
            listen_addresses: Arc::new(vec![
                "[2001:7d0:84aa:3900:2a5d:9e85::]:8888".parse().unwrap(),
                "92.168.127.1:9999".parse().unwrap(),
            ]),
        };

        assert_eq!(
            dial_addresses.local_dial_address(&IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1))),
            Ok(Some(SocketAddr::new(
                IpAddr::V4(Ipv4Addr::UNSPECIFIED),
                9999
            ))),
        );

        assert_eq!(
            dial_addresses.local_dial_address(&IpAddr::V6(Ipv6Addr::new(0, 1, 2, 3, 4, 5, 6, 7))),
            Ok(Some(SocketAddr::new(
                IpAddr::V6(Ipv6Addr::UNSPECIFIED),
                8888
            ))),
        );
    }
}