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
// Copyright 2023 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.

//! WebRTC transport.

use crate::{
    error::{AddressError, Error},
    transport::{
        manager::TransportHandle,
        webrtc::{config::Config, connection::WebRtcConnection, opening::OpeningWebRtcConnection},
        Endpoint, Transport, TransportBuilder, TransportEvent,
    },
    types::ConnectionId,
    PeerId,
};

use futures::{future::BoxFuture, Future, Stream};
use futures_timer::Delay;
use multiaddr::{multihash::Multihash, Multiaddr, Protocol};
use socket2::{Domain, Socket, Type};
use str0m::{
    change::{DtlsCert, IceCreds},
    channel::{ChannelConfig, ChannelId},
    net::{DatagramRecv, Protocol as Str0mProtocol, Receive},
    Candidate, Input, Rtc,
};
use tokio::{
    io::ReadBuf,
    net::UdpSocket,
    sync::mpsc::{channel, error::TrySendError, Sender},
};

use std::{
    collections::{HashMap, VecDeque},
    net::{IpAddr, SocketAddr},
    pin::Pin,
    sync::Arc,
    task::{Context, Poll},
    time::{Duration, Instant},
};

pub(crate) use substream::Substream;

mod connection;
mod opening;
mod substream;
mod util;

pub mod config;

pub(super) mod schema {
    pub(super) mod webrtc {
        include!(concat!(env!("OUT_DIR"), "/webrtc.rs"));
    }

    pub(super) mod noise {
        include!(concat!(env!("OUT_DIR"), "/noise.rs"));
    }
}

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

/// Hardcoded remote fingerprint.
const REMOTE_FINGERPRINT: &str =
    "sha-256 FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF";

/// Connection context.
struct ConnectionContext {
    /// Remote peer ID.
    peer: PeerId,

    /// Connection ID.
    connection_id: ConnectionId,

    /// TX channel for sending datagrams to the connection event loop.
    tx: Sender<Vec<u8>>,
}

/// Events received from opening connections that are handled
/// by the [`WebRtcTransport`] event loop.
enum ConnectionEvent {
    /// Connection established.
    ConnectionEstablished {
        /// Remote peer ID.
        peer: PeerId,

        /// Endpoint.
        endpoint: Endpoint,
    },

    /// Connection to peer closed.
    ConnectionClosed,

    /// Timeout.
    Timeout {
        /// Timeout duration.
        duration: Duration,
    },
}

/// WebRTC transport.
pub(crate) struct WebRtcTransport {
    /// Transport context.
    context: TransportHandle,

    /// UDP socket.
    socket: Arc<UdpSocket>,

    /// DTLS certificate.
    dtls_cert: DtlsCert,

    /// Assigned listen addresss.
    listen_address: SocketAddr,

    /// Datagram buffer size.
    datagram_buffer_size: usize,

    /// Connected peers.
    open: HashMap<SocketAddr, ConnectionContext>,

    /// OpeningWebRtc connections.
    opening: HashMap<SocketAddr, OpeningWebRtcConnection>,

    /// `ConnectionId -> SocketAddr` mappings.
    connections: HashMap<ConnectionId, (PeerId, SocketAddr, Endpoint)>,

    /// Pending timeouts.
    timeouts: HashMap<SocketAddr, BoxFuture<'static, ()>>,

    /// Pending events.
    pending_events: VecDeque<TransportEvent>,
}

impl WebRtcTransport {
    /// Extract socket address and `PeerId`, if found, from `address`.
    fn get_socket_address(address: &Multiaddr) -> crate::Result<(SocketAddr, Option<PeerId>)> {
        tracing::trace!(target: LOG_TARGET, ?address, "parse multi address");

        let mut iter = address.iter();
        let socket_address = match iter.next() {
            Some(Protocol::Ip6(address)) => match iter.next() {
                Some(Protocol::Udp(port)) => SocketAddr::new(IpAddr::V6(address), port),
                protocol => {
                    tracing::error!(
                        target: LOG_TARGET,
                        ?protocol,
                        "invalid transport protocol, expected `Upd`",
                    );
                    return Err(Error::AddressError(AddressError::InvalidProtocol));
                }
            },
            Some(Protocol::Ip4(address)) => match iter.next() {
                Some(Protocol::Udp(port)) => SocketAddr::new(IpAddr::V4(address), port),
                protocol => {
                    tracing::error!(
                        target: LOG_TARGET,
                        ?protocol,
                        "invalid transport protocol, expected `Udp`",
                    );
                    return Err(Error::AddressError(AddressError::InvalidProtocol));
                }
            },
            protocol => {
                tracing::error!(target: LOG_TARGET, ?protocol, "invalid transport protocol");
                return Err(Error::AddressError(AddressError::InvalidProtocol));
            }
        };

        match iter.next() {
            Some(Protocol::WebRTC) => {}
            protocol => {
                tracing::error!(
                    target: LOG_TARGET,
                    ?protocol,
                    "invalid protocol, expected `WebRTC`"
                );
                return Err(Error::AddressError(AddressError::InvalidProtocol));
            }
        }

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

        Ok((socket_address, maybe_peer))
    }

    /// Create RTC client and open channel for Noise handshake.
    fn make_rtc_client(
        &self,
        ufrag: &str,
        pass: &str,
        source: SocketAddr,
        destination: SocketAddr,
    ) -> (Rtc, ChannelId) {
        let mut rtc = Rtc::builder()
            .set_ice_lite(true)
            .set_dtls_cert(self.dtls_cert.clone())
            .set_fingerprint_verification(false)
            .build();
        rtc.add_local_candidate(Candidate::host(destination, Str0mProtocol::Udp).unwrap());
        rtc.add_remote_candidate(Candidate::host(source, Str0mProtocol::Udp).unwrap());
        rtc.direct_api()
            .set_remote_fingerprint(REMOTE_FINGERPRINT.parse().expect("parse() to succeed"));
        rtc.direct_api().set_remote_ice_credentials(IceCreds {
            ufrag: ufrag.to_owned(),
            pass: pass.to_owned(),
        });
        rtc.direct_api().set_local_ice_credentials(IceCreds {
            ufrag: ufrag.to_owned(),
            pass: pass.to_owned(),
        });
        rtc.direct_api().set_ice_controlling(false);
        rtc.direct_api().start_dtls(false).unwrap();
        rtc.direct_api().start_sctp(false);

        let noise_channel_id = rtc.direct_api().create_data_channel(ChannelConfig {
            label: "noise".to_string(),
            ordered: false,
            reliability: Default::default(),
            negotiated: Some(0),
            protocol: "".to_string(),
        });

        (rtc, noise_channel_id)
    }

    /// Poll opening connection.
    fn poll_connection(&mut self, source: &SocketAddr) -> ConnectionEvent {
        let Some(connection) = self.opening.get_mut(source) else {
            tracing::warn!(
                target: LOG_TARGET,
                ?source,
                "connection doesn't exist",
            );
            return ConnectionEvent::ConnectionClosed;
        };

        loop {
            match connection.poll_process() {
                opening::WebRtcEvent::Timeout { timeout } => {
                    let duration = timeout - Instant::now();

                    match duration.is_zero() {
                        true => match connection.on_timeout() {
                            Ok(()) => continue,
                            Err(error) => {
                                tracing::debug!(
                                    target: LOG_TARGET,
                                    ?source,
                                    ?error,
                                    "failed to handle timeout",
                                );

                                return ConnectionEvent::ConnectionClosed;
                            }
                        },
                        false => return ConnectionEvent::Timeout { duration },
                    }
                }
                opening::WebRtcEvent::Transmit {
                    destination,
                    datagram,
                } =>
                    if let Err(error) = self.socket.try_send_to(&datagram, destination) {
                        tracing::warn!(
                            target: LOG_TARGET,
                            ?source,
                            ?error,
                            "failed to send datagram",
                        );
                    },
                opening::WebRtcEvent::ConnectionClosed => return ConnectionEvent::ConnectionClosed,
                opening::WebRtcEvent::ConnectionOpened { peer, endpoint } => {
                    return ConnectionEvent::ConnectionEstablished { peer, endpoint };
                }
            }
        }
    }

    /// Handle socket input.
    ///
    /// If the datagram was received from an active client, it's dispatched to the connection
    /// handler, if there is space in the queue. If the datagram opened a new connection or it
    /// belonged to a client who is opening, the event loop is instructed to poll the client
    /// until it timeouts.
    ///
    /// Returns `true` if the client should be polled.
    fn on_socket_input(&mut self, source: SocketAddr, buffer: Vec<u8>) -> crate::Result<bool> {
        if let Some(ConnectionContext {
            peer,
            connection_id,
            tx,
        }) = self.open.get_mut(&source)
        {
            match tx.try_send(buffer) {
                Ok(_) => return Ok(false),
                Err(TrySendError::Full(_)) => {
                    tracing::warn!(
                        target: LOG_TARGET,
                        ?source,
                        ?peer,
                        ?connection_id,
                        "channel full, dropping datagram",
                    );

                    return Ok(false);
                }
                Err(TrySendError::Closed(_)) => return Ok(false),
            }
        }

        // if the peer doesn't exist, decode the message and expect to receive `Stun`
        // so that a new connection can be initialized
        let contents: DatagramRecv =
            buffer.as_slice().try_into().map_err(|_| Error::InvalidData)?;

        match contents {
            DatagramRecv::Stun(message) if !self.opening.contains_key(&source) => {
                if let Some((ufrag, pass)) = message.split_username() {
                    tracing::debug!(
                        target: LOG_TARGET,
                        ?source,
                        ?ufrag,
                        ?pass,
                        "received stun message"
                    );

                    // create new `Rtc` object for the peer and give it the received STUN message
                    let (mut rtc, noise_channel_id) = self.make_rtc_client(
                        ufrag,
                        pass,
                        source,
                        self.socket.local_addr().unwrap(),
                    );

                    rtc.handle_input(Input::Receive(
                        Instant::now(),
                        Receive {
                            source,
                            proto: Str0mProtocol::Udp,
                            destination: self.socket.local_addr().unwrap(),
                            contents: DatagramRecv::Stun(message.clone()),
                        },
                    ))
                    .expect("client to handle input successfully");

                    let connection_id = self.context.next_connection_id();
                    let connection = OpeningWebRtcConnection::new(
                        rtc,
                        connection_id,
                        noise_channel_id,
                        self.context.keypair.clone(),
                        source,
                        self.listen_address,
                    );
                    self.opening.insert(source, connection);
                }
            }
            msg => {
                if let Err(error) = self.opening.get_mut(&source).expect("to exist").on_input(msg) {
                    tracing::error!(
                        target: LOG_TARGET,
                        ?error,
                        ?source,
                        "failed to handle inbound datagram"
                    );
                }
            }
        }

        Ok(true)
    }
}

impl TransportBuilder for WebRtcTransport {
    type Config = Config;
    type Transport = WebRtcTransport;

    /// Create new [`Transport`] object.
    fn new(context: TransportHandle, config: Self::Config) -> crate::Result<(Self, Vec<Multiaddr>)>
    where
        Self: Sized,
    {
        tracing::info!(
            target: LOG_TARGET,
            listen_addresses = ?config.listen_addresses,
            "start webrtc transport",
        );

        let (listen_address, _) = Self::get_socket_address(&config.listen_addresses[0])?;
        let socket = match listen_address.is_ipv4() {
            true => {
                let socket = Socket::new(Domain::IPV4, Type::DGRAM, Some(socket2::Protocol::UDP))?;
                socket.bind(&listen_address.into())?;
                socket
            }
            false => {
                let socket = Socket::new(Domain::IPV6, Type::DGRAM, Some(socket2::Protocol::UDP))?;
                socket.set_only_v6(true)?;
                socket.bind(&listen_address.into())?;
                socket
            }
        };

        socket.set_reuse_address(true)?;
        socket.set_nonblocking(true)?;
        #[cfg(unix)]
        socket.set_reuse_port(true)?;

        let socket = UdpSocket::from_std(socket.into())?;
        let listen_address = socket.local_addr()?;
        let dtls_cert = DtlsCert::new();

        let listen_multi_addresses = {
            let fingerprint = dtls_cert.fingerprint().bytes;

            const MULTIHASH_SHA256_CODE: u64 = 0x12;
            let certificate = Multihash::wrap(MULTIHASH_SHA256_CODE, &fingerprint)
                .expect("fingerprint's len to be 32 bytes");

            vec![Multiaddr::empty()
                .with(Protocol::from(listen_address.ip()))
                .with(Protocol::Udp(listen_address.port()))
                .with(Protocol::WebRTC)
                .with(Protocol::Certhash(certificate))]
        };

        Ok((
            Self {
                context,
                dtls_cert,
                listen_address,
                open: HashMap::new(),
                opening: HashMap::new(),
                connections: HashMap::new(),
                socket: Arc::new(socket),
                timeouts: HashMap::new(),
                pending_events: VecDeque::new(),
                datagram_buffer_size: config.datagram_buffer_size,
            },
            listen_multi_addresses,
        ))
    }
}

impl Transport for WebRtcTransport {
    fn dial(&mut self, connection_id: ConnectionId, address: Multiaddr) -> crate::Result<()> {
        tracing::warn!(
            target: LOG_TARGET,
            ?connection_id,
            ?address,
            "webrtc cannot dial",
        );

        Err(Error::NotSupported("webrtc cannot dial peers".to_string()))
    }

    fn accept(&mut self, connection_id: ConnectionId) -> crate::Result<()> {
        tracing::trace!(
            target: LOG_TARGET,
            ?connection_id,
            "inbound connection accepted",
        );

        let (peer, source, endpoint) =
            self.connections.remove(&connection_id).ok_or_else(|| {
                tracing::warn!(
                    target: LOG_TARGET,
                    ?connection_id,
                    "pending connection doens't exist",
                );

                Error::InvalidState
            })?;

        let connection = self.opening.remove(&source).ok_or_else(|| {
            tracing::warn!(
                target: LOG_TARGET,
                ?connection_id,
                "pending connection doens't exist",
            );

            Error::InvalidState
        })?;

        let rtc = connection.on_accept()?;
        let (tx, rx) = channel(self.datagram_buffer_size);
        let protocol_set = self.context.protocol_set(connection_id);
        let connection_id = endpoint.connection_id();

        let connection = WebRtcConnection::new(
            rtc,
            peer,
            source,
            self.listen_address,
            Arc::clone(&self.socket),
            protocol_set,
            endpoint,
            rx,
        );
        self.open.insert(
            source,
            ConnectionContext {
                tx,
                peer,
                connection_id,
            },
        );

        self.context.executor.run(Box::pin(async move {
            connection.run().await;
        }));

        Ok(())
    }

    fn reject(&mut self, connection_id: ConnectionId) -> crate::Result<()> {
        tracing::trace!(
            target: LOG_TARGET,
            ?connection_id,
            "inbound connection rejected",
        );

        let (_, source, _) = self.connections.remove(&connection_id).ok_or_else(|| {
            tracing::warn!(
                target: LOG_TARGET,
                ?connection_id,
                "pending connection doens't exist",
            );

            Error::InvalidState
        })?;

        self.opening
            .remove(&source)
            .ok_or_else(|| {
                tracing::warn!(
                    target: LOG_TARGET,
                    ?connection_id,
                    "pending connection doens't exist",
                );

                Error::InvalidState
            })
            .map(|_| ())
    }

    fn open(
        &mut self,
        _connection_id: ConnectionId,
        _addresses: Vec<Multiaddr>,
    ) -> crate::Result<()> {
        Ok(())
    }

    fn negotiate(&mut self, _connection_id: ConnectionId) -> crate::Result<()> {
        Ok(())
    }

    fn cancel(&mut self, _connection_id: ConnectionId) {}
}

impl Stream for WebRtcTransport {
    type Item = TransportEvent;

    fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        let this = Pin::into_inner(self);

        if let Some(event) = this.pending_events.pop_front() {
            return Poll::Ready(Some(event));
        }

        loop {
            let mut buf = vec![0u8; 16384];
            let mut read_buf = ReadBuf::new(&mut buf);

            match this.socket.poll_recv_from(cx, &mut read_buf) {
                Poll::Pending => break,
                Poll::Ready(Err(error)) => {
                    tracing::info!(
                        target: LOG_TARGET,
                        ?error,
                        "webrtc udp socket closed",
                    );

                    return Poll::Ready(None);
                }
                Poll::Ready(Ok(source)) => {
                    let nread = read_buf.filled().len();
                    buf.truncate(nread);

                    match this.on_socket_input(source, buf) {
                        Ok(false) => {}
                        Ok(true) => loop {
                            match this.poll_connection(&source) {
                                ConnectionEvent::ConnectionEstablished { peer, endpoint } => {
                                    this.connections.insert(
                                        endpoint.connection_id(),
                                        (peer, source, endpoint.clone()),
                                    );

                                    // keep polling the connection until it registers a timeout
                                    this.pending_events.push_back(
                                        TransportEvent::ConnectionEstablished { peer, endpoint },
                                    );
                                }
                                ConnectionEvent::ConnectionClosed => {
                                    this.opening.remove(&source);
                                    this.timeouts.remove(&source);

                                    break;
                                }
                                ConnectionEvent::Timeout { duration } => {
                                    this.timeouts.insert(
                                        source,
                                        Box::pin(async move { Delay::new(duration).await }),
                                    );

                                    break;
                                }
                            }
                        },
                        Err(error) => {
                            tracing::debug!(
                                target: LOG_TARGET,
                                ?source,
                                ?error,
                                "failed to handle datagram",
                            );
                        }
                    }
                }
            }
        }

        // go over all pending timeouts to see if any of them have expired
        // and if any of them have, poll the connection until it registers another timeout
        let pending_events = this
            .timeouts
            .iter_mut()
            .filter_map(|(source, mut delay)| match Pin::new(&mut delay).poll(cx) {
                Poll::Pending => None,
                Poll::Ready(_) => Some(*source),
            })
            .collect::<Vec<_>>()
            .into_iter()
            .filter_map(|source| {
                let mut pending_event = None;

                loop {
                    match this.poll_connection(&source) {
                        ConnectionEvent::ConnectionEstablished { peer, endpoint } => {
                            this.connections
                                .insert(endpoint.connection_id(), (peer, source, endpoint.clone()));

                            // keep polling the connection until it registers a timeout
                            pending_event =
                                Some(TransportEvent::ConnectionEstablished { peer, endpoint });
                        }
                        ConnectionEvent::ConnectionClosed => {
                            this.opening.remove(&source);
                            return None;
                        }
                        ConnectionEvent::Timeout { duration } => {
                            this.timeouts.insert(
                                source,
                                Box::pin(async move {
                                    Delay::new(duration);
                                }),
                            );
                            break;
                        }
                    }
                }

                pending_event
            })
            .collect::<VecDeque<_>>();

        this.timeouts.retain(|source, _| this.opening.contains_key(source));
        this.pending_events.extend(pending_events);
        this.pending_events
            .pop_front()
            .map_or(Poll::Pending, |event| Poll::Ready(Some(event)))
    }
}