2018-05-18 14:56:11 +02:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-08-22 10:46:23 +02:00
|
|
|
//! Libp2p is a peer-to-peer framework.
|
|
|
|
//!
|
|
|
|
//! # Major libp2p concepts
|
|
|
|
//!
|
|
|
|
//! Here is a list of all the major concepts of libp2p.
|
|
|
|
//!
|
|
|
|
//! ## Multiaddr
|
2018-09-12 09:10:05 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! A [`Multiaddr`] is a self-describing network address and protocol stack
|
|
|
|
//! that is used to establish connections to peers. Some examples:
|
2018-09-12 09:10:05 +02:00
|
|
|
//!
|
2018-08-22 10:46:23 +02:00
|
|
|
//! * `/ip4/80.123.90.4/tcp/5432`
|
2018-12-04 13:33:07 +01:00
|
|
|
//! * `/ip6/[::1]/udp/10560/quic`
|
2018-08-22 10:46:23 +02:00
|
|
|
//! * `/unix//path/to/socket`
|
|
|
|
//!
|
|
|
|
//! ## Transport
|
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! [`Transport`] is a trait for types that provide connection-oriented communication channels
|
|
|
|
//! based on dialing to or listening on a [`Multiaddr`]. To that end a transport
|
|
|
|
//! produces as output a type of data stream that varies depending on the concrete type of
|
|
|
|
//! transport.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! An implementation of transport typically supports only certain multi-addresses.
|
|
|
|
//! For example, the [`TcpConfig`] only supports multi-addresses of the format
|
2018-08-22 10:46:23 +02:00
|
|
|
//! `/ip4/.../tcp/...`.
|
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! Example (Dialing a TCP/IP multi-address):
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use libp2p::{Multiaddr, Transport, tcp::TcpConfig};
|
2018-12-10 13:39:11 +01:00
|
|
|
//! let tcp = TcpConfig::new();
|
2018-08-22 10:46:23 +02:00
|
|
|
//! let addr: Multiaddr = "/ip4/98.97.96.95/tcp/20500".parse().expect("invalid multiaddr");
|
2019-03-19 12:45:57 +01:00
|
|
|
//! let _conn = tcp.dial(addr);
|
2018-08-22 10:46:23 +02:00
|
|
|
//! ```
|
2019-03-19 12:45:57 +01:00
|
|
|
//! In the above example, `_conn` is a [`Future`] that needs to be polled in order for
|
|
|
|
//! the dialing to take place and eventually resolve to a connection. Polling
|
|
|
|
//! futures is typically done through a [tokio] runtime.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2021-03-16 11:48:48 +01:00
|
|
|
//! The easiest way to create a transport is to use [`development_transport`].
|
2019-03-19 12:45:57 +01:00
|
|
|
//! This function provides support for the most common protocols but it is also
|
|
|
|
//! subject to change over time and should thus not be used in production
|
|
|
|
//! configurations.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! Example (Creating a development transport):
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2019-03-19 12:45:57 +01:00
|
|
|
//! let keypair = libp2p::identity::Keypair::generate_ed25519();
|
2021-03-16 11:48:48 +01:00
|
|
|
//! let _transport = libp2p::development_transport(keypair);
|
|
|
|
//! // _transport.await?.dial(...);
|
2018-08-22 10:46:23 +02:00
|
|
|
//! ```
|
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! The keypair that is passed as an argument in the above example is used
|
|
|
|
//! to set up transport-layer encryption using a newly generated long-term
|
|
|
|
//! identity keypair. The public key of this keypair uniquely identifies
|
|
|
|
//! the node in the network in the form of a [`PeerId`].
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! See the documentation of the [`Transport`] trait for more details.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! ### Connection Upgrades
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! Once a connection has been established with a remote through a [`Transport`], it can be
|
|
|
|
//! *upgraded*. Upgrading a transport is the process of negotiating an additional protocol
|
|
|
|
//! with the remote, mediated through a negotiation protocol called [`multistream-select`].
|
|
|
|
//!
|
2020-09-07 12:13:10 +02:00
|
|
|
//! Example ([`noise`] + [`yamux`] Protocol Upgrade):
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2021-03-16 11:48:48 +01:00
|
|
|
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-async-io", feature = "noise", feature = "yamux"))] {
|
2020-09-07 12:13:10 +02:00
|
|
|
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, noise, identity::Keypair, yamux};
|
2018-12-10 13:39:11 +01:00
|
|
|
//! let tcp = TcpConfig::new();
|
2020-09-07 12:13:10 +02:00
|
|
|
//! let id_keys = Keypair::generate_ed25519();
|
|
|
|
//! let noise_keys = noise::Keypair::<noise::X25519Spec>::new().into_authentic(&id_keys).unwrap();
|
|
|
|
//! let noise = noise::NoiseConfig::xx(noise_keys).into_authenticated();
|
2020-11-06 09:46:22 +01:00
|
|
|
//! let yamux = yamux::YamuxConfig::default();
|
2020-09-07 12:13:10 +02:00
|
|
|
//! let transport = tcp.upgrade(upgrade::Version::V1).authenticate(noise).multiplex(yamux);
|
2018-08-29 11:24:44 +02:00
|
|
|
//! # }
|
2018-08-22 10:46:23 +02:00
|
|
|
//! ```
|
2020-07-15 21:14:29 +12:00
|
|
|
//! In this example, `transport` is a new [`Transport`] that negotiates the
|
2020-09-07 12:13:10 +02:00
|
|
|
//! noise and yamux protocols on all connections.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! ## Network Behaviour
|
2018-12-04 13:33:07 +01:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! The [`NetworkBehaviour`] trait is implemented on types that provide some capability to the
|
|
|
|
//! network. Examples of network behaviours include:
|
2018-12-04 13:33:07 +01:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! * Periodically pinging other nodes on established connections.
|
|
|
|
//! * Periodically asking for information from other nodes.
|
|
|
|
//! * Querying information from a DHT and propagating it to other nodes.
|
2018-12-04 13:33:07 +01:00
|
|
|
//!
|
2018-08-22 10:46:23 +02:00
|
|
|
//! ## Swarm
|
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! A [`Swarm`] manages a pool of connections established through a [`Transport`]
|
|
|
|
//! and drives a [`NetworkBehaviour`] through emitting events triggered by activity
|
|
|
|
//! on the managed connections. Creating a [`Swarm`] thus involves combining a
|
|
|
|
//! [`Transport`] with a [`NetworkBehaviour`].
|
2019-01-14 14:10:51 +01:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! See the documentation of the [`core`] module for more details about swarms.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
|
|
|
//! # Using libp2p
|
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! The easiest way to get started with libp2p involves the following steps:
|
|
|
|
//!
|
|
|
|
//! 1. Creating an identity [`Keypair`] for the local node, obtaining the local
|
|
|
|
//! [`PeerId`] from the [`PublicKey`].
|
|
|
|
//! 2. Creating an instance of a base [`Transport`], e.g. [`TcpConfig`], upgrading it with
|
|
|
|
//! all the desired protocols, such as for transport security and multiplexing.
|
|
|
|
//! In order to be usable with a [`Swarm`] later, the [`Output`](Transport::Output)
|
|
|
|
//! of the final transport must be a tuple of a [`PeerId`] and a value whose type
|
|
|
|
//! implements [`StreamMuxer`] (e.g. [`Yamux`]). The peer ID must be the
|
|
|
|
//! identity of the remote peer of the established connection, which is
|
|
|
|
//! usually obtained through a transport encryption protocol such as
|
2020-09-07 12:13:10 +02:00
|
|
|
//! [`noise`] that authenticates the peer. See the implementation of
|
2021-03-16 11:48:48 +01:00
|
|
|
//! [`development_transport`] for an example.
|
2019-03-19 12:45:57 +01:00
|
|
|
//! 3. Creating a struct that implements the [`NetworkBehaviour`] trait and combines all the
|
|
|
|
//! desired network behaviours, implementing the event handlers as per the
|
|
|
|
//! desired application's networking logic.
|
|
|
|
//! 4. Instantiating a [`Swarm`] with the transport, the network behaviour and the
|
|
|
|
//! local peer ID from the previous steps.
|
|
|
|
//!
|
2020-09-07 12:13:10 +02:00
|
|
|
//! The swarm instance can then be polled e.g. with the [tokio] library, in order to
|
2019-03-19 12:45:57 +01:00
|
|
|
//! continuously drive the network activity of the program.
|
|
|
|
//!
|
|
|
|
//! [`Keypair`]: identity::Keypair
|
|
|
|
//! [`PublicKey`]: identity::PublicKey
|
|
|
|
//! [`Future`]: futures::Future
|
|
|
|
//! [`TcpConfig`]: tcp::TcpConfig
|
2019-12-12 13:06:59 +03:00
|
|
|
//! [`NetworkBehaviour`]: swarm::NetworkBehaviour
|
2019-03-19 12:45:57 +01:00
|
|
|
//! [`StreamMuxer`]: core::muxing::StreamMuxer
|
|
|
|
//! [`Yamux`]: yamux::Yamux
|
|
|
|
//!
|
|
|
|
//! [tokio]: https://tokio.rs
|
|
|
|
//! [`multistream-select`]: https://github.com/multiformats/multistream-select
|
2018-08-22 10:46:23 +02:00
|
|
|
|
2019-01-14 14:10:51 +01:00
|
|
|
#![doc(html_logo_url = "https://libp2p.io/img/logo_small.png")]
|
|
|
|
#![doc(html_favicon_url = "https://libp2p.io/img/favicon.png")]
|
|
|
|
|
2019-02-11 14:58:15 +01:00
|
|
|
pub use bytes;
|
|
|
|
pub use futures;
|
2019-02-18 13:35:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use multiaddr;
|
|
|
|
#[doc(inline)]
|
2020-11-17 11:15:20 +01:00
|
|
|
pub use libp2p_core::multihash;
|
2018-10-26 11:07:59 +02:00
|
|
|
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_core as core;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "deflate")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "deflate")))]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2019-05-29 11:03:50 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_deflate as deflate;
|
2021-03-16 11:48:48 +01:00
|
|
|
#[cfg(any(feature = "dns-async-std", feature = "dns-tokio"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "dns-async-std", feature = "dns-tokio"))))]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_dns as dns;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "identify")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "identify")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_identify as identify;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "kad")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "kad")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_kad as kad;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "floodsub")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "floodsub")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_floodsub as floodsub;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "gossipsub")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "gossipsub")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
2020-01-25 02:16:02 +11:00
|
|
|
pub use libp2p_gossipsub as gossipsub;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "mplex")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mplex")))]
|
2020-01-25 02:16:02 +11:00
|
|
|
#[doc(inline)]
|
2019-01-14 14:10:51 +01:00
|
|
|
pub use libp2p_mplex as mplex;
|
2020-12-03 13:30:52 +01:00
|
|
|
#[cfg(feature = "mdns")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_mdns as mdns;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "noise")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "noise")))]
|
2019-01-30 11:36:00 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_noise as noise;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "ping")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "ping")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_ping as ping;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "plaintext")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "plaintext")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_plaintext as plaintext;
|
2019-07-04 14:47:59 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_swarm as swarm;
|
2021-01-12 13:35:11 +01:00
|
|
|
#[cfg(any(feature = "tcp-async-io", feature = "tcp-tokio"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-io", feature = "tcp-tokio"))))]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_tcp as tcp;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "uds")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "uds")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_uds as uds;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "wasm-ext")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "wasm-ext")))]
|
2019-05-06 11:17:35 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_wasm_ext as wasm_ext;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "websocket")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "websocket")))]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_websocket as websocket;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "yamux")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "yamux")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_yamux as yamux;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "pnet")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "pnet")))]
|
2020-01-28 13:22:09 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_pnet as pnet;
|
protocols/relay: Implement circuit relay specification (#1838)
This commit implements the [libp2p circuit
relay](https://github.com/libp2p/specs/tree/master/relay) specification. It is
based on previous work from https://github.com/libp2p/rust-libp2p/pull/1134.
Instead of altering the `Transport` trait, the approach taken in this commit
is to wrap an existing implementation of `Transport` allowing one to:
- Intercept `dial` requests with a relayed address.
- Inject incoming relayed connections with the local node being the destination.
- Intercept `listen_on` requests pointing to a relay, ensuring to keep a
constant connection to the relay, waiting for incoming requests with the local
node being the destination.
More concretely one would wrap an existing `Transport` implementation as seen
below, allowing the `Relay` behaviour and the `RelayTransport` to communicate
via channels.
### Example
```rust
let (relay_transport, relay_behaviour) = new_transport_and_behaviour(
RelayConfig::default(),
MemoryTransport::default(),
);
let transport = relay_transport
.upgrade(upgrade::Version::V1)
.authenticate(plaintext)
.multiplex(YamuxConfig::default())
.boxed();
let mut swarm = Swarm::new(transport, relay_behaviour, local_peer_id);
let relay_addr = Multiaddr::from_str("/memory/1234").unwrap()
.with(Protocol::P2p(PeerId::random().into()))
.with(Protocol::P2pCircuit);
let dst_addr = relay_addr.clone().with(Protocol::Memory(5678));
// Listen for incoming connections via relay node (1234).
Swarm::listen_on(&mut swarm, relay_addr).unwrap();
// Dial node (5678) via relay node (1234).
Swarm::dial_addr(&mut swarm, dst_addr).unwrap();
```
Co-authored-by: Pierre Krieger <pierre.krieger1708@gmail.com>
Co-authored-by: Roman Borschel <romanb@users.noreply.github.com>
Co-authored-by: David Craven <david@craven.ch>
2021-03-11 16:07:59 +01:00
|
|
|
#[cfg(feature = "relay")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "relay")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_relay as relay;
|
2020-07-01 15:36:20 +02:00
|
|
|
#[cfg(feature = "request-response")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "request-response")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_request_response as request_response;
|
2018-05-18 14:56:11 +02:00
|
|
|
|
2018-10-26 11:07:59 +02:00
|
|
|
mod transport_ext;
|
|
|
|
|
2019-02-14 16:39:18 +01:00
|
|
|
pub mod bandwidth;
|
2018-05-23 16:27:55 +02:00
|
|
|
pub mod simple;
|
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
pub use self::core::{
|
2019-03-11 13:42:53 +01:00
|
|
|
identity,
|
2019-07-04 14:47:59 +02:00
|
|
|
PeerId,
|
|
|
|
Transport,
|
2019-01-10 11:27:06 +01:00
|
|
|
transport::TransportError,
|
2018-11-15 17:41:11 +01:00
|
|
|
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt}
|
|
|
|
};
|
2021-01-26 22:49:08 +01:00
|
|
|
pub use libp2p_swarm_derive::NetworkBehaviour;
|
2019-02-18 13:35:51 +01:00
|
|
|
pub use self::multiaddr::{Multiaddr, multiaddr as build_multiaddr};
|
2018-05-23 16:27:55 +02:00
|
|
|
pub use self::simple::SimpleProtocol;
|
2019-07-04 14:47:59 +02:00
|
|
|
pub use self::swarm::Swarm;
|
2018-10-26 11:07:59 +02:00
|
|
|
pub use self::transport_ext::TransportExt;
|
2018-05-18 14:56:11 +02:00
|
|
|
|
2021-03-16 11:48:48 +01:00
|
|
|
/// Builds a `Transport` based on TCP/IP that supports the most commonly-used features of libp2p:
|
|
|
|
///
|
|
|
|
/// * DNS resolution.
|
|
|
|
/// * Noise protocol encryption.
|
|
|
|
/// * Websockets.
|
|
|
|
/// * Both Yamux and Mplex for substream multiplexing.
|
|
|
|
///
|
|
|
|
/// All async I/O of the transport is based on `async-std`.
|
2018-12-14 10:41:54 +01:00
|
|
|
///
|
|
|
|
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
|
|
|
|
/// > reserves the right to support additional protocols or remove deprecated protocols.
|
2021-03-16 11:48:48 +01:00
|
|
|
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-async-io", feature = "dns-async-std", feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-async-io", feature = "dns-async-std", feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
|
|
|
|
pub async fn development_transport(keypair: identity::Keypair)
|
2020-10-16 16:53:02 +02:00
|
|
|
-> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>>
|
2020-08-03 14:52:34 +02:00
|
|
|
{
|
|
|
|
let transport = {
|
|
|
|
let tcp = tcp::TcpConfig::new().nodelay(true);
|
2021-03-16 11:48:48 +01:00
|
|
|
let transport = dns::DnsConfig::system(tcp).await?;
|
|
|
|
let websockets = websocket::WsConfig::new(transport.clone());
|
|
|
|
transport.or_transport(websockets)
|
2020-08-03 14:52:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
|
|
|
|
.into_authentic(&keypair)
|
|
|
|
.expect("Signing libp2p-noise static DH keypair failed.");
|
|
|
|
|
|
|
|
Ok(transport
|
|
|
|
.upgrade(core::upgrade::Version::V1)
|
|
|
|
.authenticate(noise::NoiseConfig::xx(noise_keys).into_authenticated())
|
2020-11-06 09:46:22 +01:00
|
|
|
.multiplex(core::upgrade::SelectUpgrade::new(yamux::YamuxConfig::default(), mplex::MplexConfig::default()))
|
2020-10-07 11:10:54 +02:00
|
|
|
.timeout(std::time::Duration::from_secs(20))
|
|
|
|
.boxed())
|
2020-08-03 14:52:34 +02:00
|
|
|
}
|
|
|
|
|
2021-03-16 11:48:48 +01:00
|
|
|
/// Builds a `Transport` based on TCP/IP that supports the most commonly-used features of libp2p:
|
|
|
|
///
|
|
|
|
/// * DNS resolution.
|
|
|
|
/// * Noise protocol encryption.
|
|
|
|
/// * Websockets.
|
|
|
|
/// * Both Yamux and Mplex for substream multiplexing.
|
|
|
|
///
|
|
|
|
/// All async I/O of the transport is based on `tokio`.
|
2018-12-14 10:41:54 +01:00
|
|
|
///
|
2021-03-16 11:48:48 +01:00
|
|
|
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
|
|
|
|
/// > reserves the right to support additional protocols or remove deprecated protocols.
|
|
|
|
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-tokio", feature = "dns-tokio", feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-tokio", feature = "dns-tokio", feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
|
|
|
|
pub fn tokio_development_transport(keypair: identity::Keypair)
|
2020-10-16 16:53:02 +02:00
|
|
|
-> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>>
|
2018-12-14 10:41:54 +01:00
|
|
|
{
|
2020-03-11 15:33:22 +01:00
|
|
|
let transport = {
|
2020-05-26 19:47:41 +08:00
|
|
|
let tcp = tcp::TokioTcpConfig::new().nodelay(true);
|
2021-03-16 11:48:48 +01:00
|
|
|
let transport = dns::TokioDnsConfig::system(tcp)?;
|
|
|
|
let websockets = websocket::WsConfig::new(transport.clone());
|
|
|
|
transport.or_transport(websockets)
|
2020-03-11 15:33:22 +01:00
|
|
|
};
|
|
|
|
|
2020-09-07 12:13:10 +02:00
|
|
|
let noise_keys = noise::Keypair::<noise::X25519Spec>::new()
|
|
|
|
.into_authentic(&keypair)
|
|
|
|
.expect("Signing libp2p-noise static DH keypair failed.");
|
2020-03-11 15:33:22 +01:00
|
|
|
|
|
|
|
Ok(transport
|
2020-01-28 13:22:09 +01:00
|
|
|
.upgrade(core::upgrade::Version::V1)
|
2020-09-07 12:13:10 +02:00
|
|
|
.authenticate(noise::NoiseConfig::xx(noise_keys).into_authenticated())
|
2020-11-06 09:46:22 +01:00
|
|
|
.multiplex(core::upgrade::SelectUpgrade::new(yamux::YamuxConfig::default(), mplex::MplexConfig::default()))
|
2020-10-07 11:10:54 +02:00
|
|
|
.timeout(std::time::Duration::from_secs(20))
|
|
|
|
.boxed())
|
2020-01-28 13:22:09 +01:00
|
|
|
}
|