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
// Smoldot
// Copyright (C) 2019-2022  Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

use alloc::borrow::Cow;
use core::{
    fmt, future::Future, net::IpAddr, ops, panic::UnwindSafe, pin::Pin, str, time::Duration,
};
use futures_util::future;

pub use smoldot::libp2p::read_write;

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use smoldot::libp2p::with_buffers;

// TODO: this module should probably not be public?
pub mod address_parse;
pub mod default;

mod with_prefix;

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use default::DefaultPlatform;

pub use with_prefix::WithPrefix;

/// Access to a platform's capabilities.
///
/// Implementations of this trait are expected to be cheaply-clonable "handles". All clones of the
/// same platform share the same objects. For instance, it is legal to create clone a platform,
/// then create a connection on one clone, then access this connection on the other clone.
pub trait PlatformRef: UnwindSafe + Clone + Send + Sync + 'static {
    /// `Future` that resolves once a certain amount of time has passed or a certain point in time
    /// is reached. See [`PlatformRef::sleep`] and [`PlatformRef::sleep_until`].
    type Delay: Future<Output = ()> + Send + 'static;

    /// A certain point in time. Typically `std::time::Instant`, but one can also
    /// use `core::time::Duration`.
    ///
    /// The implementations of the `Add` and `Sub` traits must panic in case of overflow or
    /// underflow, similar to the ones of `std::time::Instant` and `core::time::Duration`.
    type Instant: Clone
        + ops::Add<Duration, Output = Self::Instant>
        + ops::Sub<Self::Instant, Output = Duration>
        + PartialOrd
        + Ord
        + PartialEq
        + Eq
        + Send
        + Sync
        + 'static;

    /// A multi-stream connection.
    ///
    /// This object is merely a handle. The underlying connection should be dropped only after
    /// the `MultiStream` and all its associated substream objects ([`PlatformRef::Stream`]) have
    /// been dropped.
    type MultiStream: Send + Sync + 'static;

    /// Opaque object representing either a single-stream connection or a substream in a
    /// multi-stream connection.
    ///
    /// If this object is abruptly dropped, the underlying single stream connection or substream
    /// should be abruptly dropped (i.e. RST) as well, unless its reading and writing sides
    /// have been gracefully closed in the past.
    type Stream: Send + 'static;

    /// Object that dereferences to [`read_write::ReadWrite`] and gives access to the stream's
    /// buffers. See the [`read_write`] module for more information.
    /// See also [`PlatformRef::read_write_access`].
    type ReadWriteAccess<'a>: ops::DerefMut<Target = read_write::ReadWrite<Self::Instant>> + 'a;

    /// Reference to an error that happened on a stream.
    ///
    /// Potentially returned by [`PlatformRef::read_write_access`].
    ///
    /// Typically `&'a std::io::Error`.
    type StreamErrorRef<'a>: fmt::Display + fmt::Debug;

    /// `Future` returned by [`PlatformRef::connect_stream`].
    type StreamConnectFuture: Future<Output = Self::Stream> + Send + 'static;
    /// `Future` returned by [`PlatformRef::connect_multistream`].
    type MultiStreamConnectFuture: Future<Output = MultiStreamWebRtcConnection<Self::MultiStream>>
        + Send
        + 'static;
    /// `Future` returned by [`PlatformRef::wait_read_write_again`].
    type StreamUpdateFuture<'a>: Future<Output = ()> + Send + 'a;
    /// `Future` returned by [`PlatformRef::next_substream`].
    type NextSubstreamFuture<'a>: Future<Output = Option<(Self::Stream, SubstreamDirection)>>
        + Send
        + 'a;

    /// Returns the time elapsed since [the Unix Epoch](https://en.wikipedia.org/wiki/Unix_time)
    /// (i.e. 00:00:00 UTC on 1 January 1970), ignoring leap seconds.
    ///
    /// # Panic
    ///
    /// Panics if the system time is configured to be below the UNIX epoch. This situation is a
    /// very very niche edge case that isn't worth handling.
    ///
    fn now_from_unix_epoch(&self) -> Duration;

    /// Returns an object that represents "now".
    fn now(&self) -> Self::Instant;

    /// The given buffer must be completely filled with pseudo-random bytes.
    ///
    /// # Panic
    ///
    /// Must panic if for some reason it is not possible to fill the buffer.
    ///
    fn fill_random_bytes(&self, buffer: &mut [u8]);

    /// Creates a future that becomes ready after at least the given duration has elapsed.
    fn sleep(&self, duration: Duration) -> Self::Delay;

    /// Creates a future that becomes ready after the given instant has been reached.
    fn sleep_until(&self, when: Self::Instant) -> Self::Delay;

    /// Spawns a task that runs indefinitely in the background.
    ///
    /// The first parameter is the name of the task, which can be useful for debugging purposes.
    ///
    /// The `Future` must be run until it yields a value.
    ///
    /// Implementers should be aware of the fact that polling the `Future` might panic (never
    /// intentionally, but in case of a bug). Many tasks can be restarted if they panic, and
    /// implementers are encouraged to absorb the panics that happen when polling. If a panic
    /// happens, the `Future` that has panicked must never be polled again.
    ///
    /// > **Note**: Ideally, the `task` parameter would require the `UnwindSafe` trait.
    /// >           Unfortunately, at the time of writing of this comment, it is extremely
    /// >           difficult if not impossible to implement this trait on `Future`s. It is for
    /// >           the same reason that the `std::thread::spawn` function of the standard library
    /// >           doesn't require its parameter to implement `UnwindSafe`.
    fn spawn_task(
        &self,
        task_name: Cow<str>,
        task: impl future::Future<Output = ()> + Send + 'static,
    );

    /// Emit a log line.
    ///
    /// The `log_level` and `log_target` can be used in order to filter desired log lines.
    ///
    /// The `message` is typically a short constant message indicating the nature of the log line.
    /// The `key_values` are a list of keys and values that are the parameters of the log message.
    ///
    /// For example, `message` can be `"block-announce-received"` and `key_values` can contain
    /// the entries `("block_hash", ...)` and `("sender", ...)`.
    ///
    /// At the time of writing of this comment, `message` can also be a message written in plain
    /// english and destined to the user of the library. This might disappear in the future.
    // TODO: act on this last sentence ^
    fn log<'a>(
        &self,
        log_level: LogLevel,
        log_target: &'a str,
        message: &'a str,
        key_values: impl Iterator<Item = (&'a str, &'a dyn fmt::Display)>,
    );

    /// Value returned when a JSON-RPC client requests the name of the client, or when a peer
    /// performs an identification request. Reasonable value is `env!("CARGO_PKG_NAME")`.
    fn client_name(&self) -> Cow<str>;

    /// Value returned when a JSON-RPC client requests the version of the client, or when a peer
    /// performs an identification request. Reasonable value is `env!("CARGO_PKG_VERSION")`.
    fn client_version(&self) -> Cow<str>;

    /// Returns `true` if [`PlatformRef::connect_stream`] or [`PlatformRef::connect_multistream`]
    /// accepts a connection of the corresponding type.
    ///
    /// > **Note**: This function is meant to be pure. Implementations are expected to always
    /// >           return the same value for the same [`ConnectionType`] input. Enabling or
    /// >           disabling certain connection types after start-up is not supported.
    fn supports_connection_type(&self, connection_type: ConnectionType) -> bool;

    /// Starts a connection attempt to the given address.
    ///
    /// This function is only ever called with an `address` of a type for which
    /// [`PlatformRef::supports_connection_type`] returned `true` in the past.
    ///
    /// This function returns a `Future`. This `Future` **must** return as soon as possible, and
    /// must **not** wait for the connection to be established.
    /// In most scenarios, the `Future` returned by this function should immediately produce
    /// an output.
    ///
    /// # Panic
    ///
    /// The function implementation panics if [`Address`] is of a type for which
    /// [`PlatformRef::supports_connection_type`] returns `false`.
    ///
    fn connect_stream(&self, address: Address) -> Self::StreamConnectFuture;

    /// Starts a connection attempt to the given address.
    ///
    /// This function is only ever called with an `address` of a type for which
    /// [`PlatformRef::supports_connection_type`] returned `true` in the past.
    ///
    /// > **Note**: A so-called "multistream connection" is a connection made of multiple
    /// >           substreams, and for which the opening and closing or substreams is handled by
    /// >           the environment rather than by smoldot itself. Most platforms do not need to
    /// >           support multistream connections. This function is in practice used in order
    /// >           to support WebRTC connections when embedding smoldot-light within a web
    /// >           browser.
    ///
    /// This function returns a `Future`. This `Future` **must** return as soon as possible, and
    /// must **not** wait for the connection to be established.
    /// In most scenarios, the `Future` returned by this function should immediately produce
    /// an output.
    ///
    /// # Panic
    ///
    /// The function implementation panics if [`MultiStreamAddress`] is of a type for which
    /// [`PlatformRef::supports_connection_type`] returns `false`.
    ///
    fn connect_multistream(&self, address: MultiStreamAddress) -> Self::MultiStreamConnectFuture;

    /// Queues the opening of an additional outbound substream.
    ///
    /// The substream, once opened, must be yielded by [`PlatformRef::next_substream`].
    ///
    /// Calls to this function should be ignored if the connection has already been killed by the
    /// remote.
    ///
    /// > **Note**: No mechanism exists in this API to handle the situation where a substream fails
    /// >           to open, as this is not supposed to happen. If you need to handle such a
    /// >           situation, either try again opening a substream again or reset the entire
    /// >           connection.
    fn open_out_substream(&self, connection: &mut Self::MultiStream);

    /// Waits until a new incoming substream arrives on the connection.
    ///
    /// This returns both inbound and outbound substreams. Outbound substreams should only be
    /// yielded once for every call to [`PlatformRef::open_out_substream`].
    ///
    /// The future can also return `None` if the connection has been killed by the remote. If
    /// the future returns `None`, the user of the `PlatformRef` should drop the `MultiStream` and
    /// all its associated `Stream`s as soon as possible.
    fn next_substream<'a>(
        &self,
        connection: &'a mut Self::MultiStream,
    ) -> Self::NextSubstreamFuture<'a>;

    /// Returns an object that implements `DerefMut<Target = ReadWrite>`. The
    /// [`read_write::ReadWrite`] object allows the API user to read data from the stream and write
    /// data to the stream.
    ///
    /// If the stream has been reset in the past, this function should return a reference to
    /// the error that happened.
    ///
    /// See the documentation of [`read_write`] for more information
    ///
    /// > **Note**: The `with_buffers` module provides a helper to more easily implement this
    /// >           function.
    fn read_write_access<'a>(
        &self,
        stream: Pin<&'a mut Self::Stream>,
    ) -> Result<Self::ReadWriteAccess<'a>, Self::StreamErrorRef<'a>>;

    /// Returns a future that becomes ready when [`PlatformRef::read_write_access`] should be
    /// called again on this stream.
    ///
    /// Must always returned immediately if [`PlatformRef::read_write_access`] has never been
    /// called on this stream.
    ///
    /// See the documentation of [`read_write`] for more information.
    ///
    /// > **Note**: The `with_buffers` module provides a helper to more easily implement this
    /// >           function.
    fn wait_read_write_again<'a>(
        &self,
        stream: Pin<&'a mut Self::Stream>,
    ) -> Self::StreamUpdateFuture<'a>;
}

/// Log level of a log entry.
///
/// See [`PlatformRef::log`].
#[derive(Debug)]
pub enum LogLevel {
    Error = 1,
    Warn = 2,
    Info = 3,
    Debug = 4,
    Trace = 5,
}

/// Established multistream connection information. See [`PlatformRef::connect_multistream`].
#[derive(Debug)]
pub struct MultiStreamWebRtcConnection<TConnection> {
    /// Object representing the WebRTC connection.
    pub connection: TConnection,
    /// SHA256 hash of the TLS certificate used by the local node at the DTLS layer.
    pub local_tls_certificate_sha256: [u8; 32],
}

/// Direction in which a substream has been opened. See [`PlatformRef::next_substream`].
#[derive(Debug)]
pub enum SubstreamDirection {
    /// Substream has been opened by the remote.
    Inbound,
    /// Substream has been opened locally in response to [`PlatformRef::open_out_substream`].
    Outbound,
}

/// Connection type passed to [`PlatformRef::supports_connection_type`].
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ConnectionType {
    /// TCP/IP connection.
    TcpIpv4,
    /// TCP/IP connection.
    TcpIpv6,
    /// TCP/IP connection.
    TcpDns,
    /// Non-secure WebSocket connection.
    WebSocketIpv4 {
        /// `true` if the target of the connection is `localhost`.
        ///
        /// > **Note**: Some platforms (namely browsers) sometimes only accept non-secure WebSocket
        /// >           connections only towards `localhost`.
        remote_is_localhost: bool,
    },
    /// Non-secure WebSocket connection.
    WebSocketIpv6 {
        /// `true` if the target of the connection is `localhost`.
        ///
        /// > **Note**: Some platforms (namely browsers) sometimes only accept non-secure WebSocket
        /// >           connections only towards `localhost`.
        remote_is_localhost: bool,
    },
    /// WebSocket connection.
    WebSocketDns {
        /// `true` for WebSocket secure connections.
        secure: bool,
        /// `true` if the target of the connection is `localhost`.
        ///
        /// > **Note**: Some platforms (namely browsers) sometimes only accept non-secure WebSocket
        /// >           connections only towards `localhost`.
        remote_is_localhost: bool,
    },
    /// Libp2p-specific WebRTC flavour.
    WebRtcIpv4,
    /// Libp2p-specific WebRTC flavour.
    WebRtcIpv6,
}

impl<'a> From<&'a Address<'a>> for ConnectionType {
    fn from(address: &'a Address<'a>) -> ConnectionType {
        match address {
            Address::TcpIp {
                ip: IpAddr::V4(_), ..
            } => ConnectionType::TcpIpv4,
            Address::TcpIp {
                ip: IpAddr::V6(_), ..
            } => ConnectionType::TcpIpv6,
            Address::TcpDns { .. } => ConnectionType::TcpDns,
            Address::WebSocketIp {
                ip: IpAddr::V4(ip), ..
            } => ConnectionType::WebSocketIpv4 {
                remote_is_localhost: ip.is_loopback(),
            },
            Address::WebSocketIp {
                ip: IpAddr::V6(ip), ..
            } => ConnectionType::WebSocketIpv6 {
                remote_is_localhost: ip.is_loopback(),
            },
            Address::WebSocketDns {
                hostname, secure, ..
            } => ConnectionType::WebSocketDns {
                secure: *secure,
                remote_is_localhost: hostname.eq_ignore_ascii_case("localhost"),
            },
        }
    }
}

impl<'a> From<&'a MultiStreamAddress<'a>> for ConnectionType {
    fn from(address: &'a MultiStreamAddress) -> ConnectionType {
        match address {
            MultiStreamAddress::WebRtc {
                ip: IpAddr::V4(_), ..
            } => ConnectionType::WebRtcIpv4,
            MultiStreamAddress::WebRtc {
                ip: IpAddr::V6(_), ..
            } => ConnectionType::WebRtcIpv6,
        }
    }
}

/// Address passed to [`PlatformRef::connect_stream`].
// TODO: we don't differentiate between Dns4 and Dns6
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Address<'a> {
    /// TCP/IP connection with a domain name.
    TcpDns {
        /// DNS hostname to connect to.
        ///
        /// > **Note**: According to RFC2181 section 11, a domain name is not necessarily an UTF-8
        /// >           string. Any binary data can be used as a domain name, provided it follows
        /// >           a few restrictions (notably its length). However, in the context of the
        /// >           [`PlatformRef`] trait, we automatically consider as non-supported a
        /// >           multiaddress that contains a non-UTF-8 domain name, for the sake of
        /// >           simplicity.
        hostname: &'a str,
        /// TCP port to connect to.
        port: u16,
    },

    /// TCP/IP connection with an IP address.
    TcpIp {
        /// IP address to connect to.
        ip: IpAddr,
        /// TCP port to connect to.
        port: u16,
    },

    /// WebSocket connection with an IP address.
    WebSocketIp {
        /// IP address to connect to.
        ip: IpAddr,
        /// TCP port to connect to.
        port: u16,
    },

    /// WebSocket connection with a domain name.
    WebSocketDns {
        /// DNS hostname to connect to.
        ///
        /// > **Note**: According to RFC2181 section 11, a domain name is not necessarily an UTF-8
        /// >           string. Any binary data can be used as a domain name, provided it follows
        /// >           a few restrictions (notably its length). However, in the context of the
        /// >           [`PlatformRef`] trait, we automatically consider as non-supported a
        /// >           multiaddress that contains a non-UTF-8 domain name, for the sake of
        /// >           simplicity.
        hostname: &'a str,
        /// TCP port to connect to.
        port: u16,
        /// `true` for WebSocket secure connections.
        secure: bool,
    },
}

/// Address passed to [`PlatformRef::connect_multistream`].
// TODO: we don't differentiate between Dns4 and Dns6
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum MultiStreamAddress<'a> {
    /// Libp2p-specific WebRTC flavour.
    ///
    /// The implementation the [`PlatformRef`] trait is responsible for opening the SCTP
    /// connection. The API user of the [`PlatformRef`] trait is responsible for opening the first
    /// data channel and performing the Noise handshake.
    // TODO: maybe explain more what the implementation is supposed to do?
    WebRtc {
        /// IP address to connect to.
        ip: IpAddr,
        /// UDP port to connect to.
        port: u16,
        /// SHA-256 hash of the target's WebRTC certificate.
        remote_certificate_sha256: &'a [u8; 32],
    },
}

// TODO: find a way to keep this private somehow?
#[macro_export]
macro_rules! log_inner {
    () => {
        core::iter::empty()
    };
    (,) => {
        core::iter::empty()
    };
    ($key:ident = $value:expr) => {
        $crate::log_inner!($key = $value,)
    };
    ($key:ident = ?$value:expr) => {
        $crate::log_inner!($key = ?$value,)
    };
    ($key:ident) => {
        $crate::log_inner!($key = $key,)
    };
    (?$key:ident) => {
        $crate::log_inner!($key = ?$key,)
    };
    ($key:ident = $value:expr, $($rest:tt)*) => {
        core::iter::once((stringify!($key), &$value as &dyn core::fmt::Display)).chain($crate::log_inner!($($rest)*))
    };
    ($key:ident = ?$value:expr, $($rest:tt)*) => {
        core::iter::once((stringify!($key), &format_args!("{:?}", $value) as &dyn core::fmt::Display)).chain($crate::log_inner!($($rest)*))
    };
    ($key:ident, $($rest:tt)*) => {
        $crate::log_inner!($key = $key, $($rest)*)
    };
    (?$key:ident, $($rest:tt)*) => {
        $crate::log_inner!($key = ?$key, $($rest)*)
    };
}

/// Helper macro for using the [`crate::platform::PlatformRef::log`] function.
///
/// This macro takes at least 4 parameters:
///
/// - A reference to an implementation of the [`crate::platform::PlatformRef`] trait.
/// - A log level: `Error`, `Warn`, `Info`, `Debug`, or `Trace`. See [`crate::platform::LogLevel`].
/// - A `&str` representing the target of the logging. This can be used in order to filter log
/// entries belonging to a specific target.
/// - An object that implements of `AsRef<str>` and that contains the log message itself.
///
/// In addition to these parameters, the macro accepts an unlimited number of extra
/// (comma-separated) parameters.
///
/// Each parameter has one of these four syntaxes:
///
/// - `key = value`, where `key` is an identifier and `value` an expression that implements the
/// `Display` trait.
/// - `key = ?value`, where `key` is an identifier and `value` an expression that implements
/// the `Debug` trait.
/// - `key`, which is syntax sugar for `key = key`.
/// - `?key`, which is syntax sugar for `key = ?key`.
///
#[macro_export]
macro_rules! log {
    ($plat:expr, $level:ident, $target:expr, $message:expr) => {
        log!($plat, $level, $target, $message,)
    };
    ($plat:expr, $level:ident, $target:expr, $message:expr, $($params:tt)*) => {
        $crate::platform::PlatformRef::log(
            $plat,
            $crate::platform::LogLevel::$level,
            $target,
            AsRef::<str>::as_ref(&$message),
            $crate::log_inner!($($params)*)
        )
    };
}