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
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! The easiest way to create a transport is to use [`build_development_transport`].
|
|
|
|
//! 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();
|
|
|
|
//! let _transport = libp2p::build_development_transport(keypair);
|
2018-08-22 10:46:23 +02:00
|
|
|
//! // _transport.dial(...);
|
|
|
|
//! ```
|
|
|
|
//!
|
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`].
|
|
|
|
//!
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
//! Example ([`secio`] + [`yamux`] Protocol Upgrade):
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
|
|
|
//! ```rust
|
2020-05-18 18:37:42 +10:00
|
|
|
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "secio", feature = "yamux"))] {
|
2020-03-11 15:33:22 +01:00
|
|
|
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, secio::SecioConfig, identity::Keypair, yamux};
|
2018-12-10 13:39:11 +01:00
|
|
|
//! let tcp = TcpConfig::new();
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
//! let secio = SecioConfig::new(Keypair::generate_ed25519());
|
|
|
|
//! let yamux = yamux::Config::default();
|
2020-03-11 15:33:22 +01:00
|
|
|
//! let transport = tcp.upgrade(upgrade::Version::V1).authenticate(secio).multiplex(yamux);
|
2018-08-29 11:24:44 +02:00
|
|
|
//! # }
|
2018-08-22 10:46:23 +02:00
|
|
|
//! ```
|
2019-03-19 12:45:57 +01:00
|
|
|
//! In this example, `tcp_secio` is a new [`Transport`] that negotiates the secio protocol
|
|
|
|
//! 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
|
|
|
|
//! [`secio`] that authenticates the peer. See the implementation of
|
|
|
|
//! [`build_development_transport`] for an example.
|
|
|
|
//! 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.
|
|
|
|
//!
|
|
|
|
//! The swarm instance can then be polled with the [tokio] library, in order to
|
|
|
|
//! 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")]
|
|
|
|
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "pnet")]
|
2020-01-28 13:22:09 +01:00
|
|
|
use libp2p_pnet::{PnetConfig, PreSharedKey};
|
2020-03-11 15:33:22 +01:00
|
|
|
|
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)]
|
2019-02-11 14:58:15 +01:00
|
|
|
pub use 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")))]
|
2019-06-20 13:03:35 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "unknown")))]
|
2019-05-29 11:03:50 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_deflate as deflate;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "dns")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
|
2019-01-02 15:50:08 +01:00
|
|
|
#[cfg(not(any(target_os = "emscripten", 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-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "mdns")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
|
2019-01-02 15:50:08 +01:00
|
|
|
#[cfg(not(any(target_os = "emscripten", 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;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "secio")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "secio")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_secio as secio;
|
2019-07-04 14:47:59 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_swarm as swarm;
|
2020-05-18 18:37:42 +10:00
|
|
|
#[cfg(any(feature = "tcp-async-std", feature = "tcp-tokio-std"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-std", feature = "tcp-tokio-std"))))]
|
2019-01-02 15:50:08 +01:00
|
|
|
#[cfg(not(any(target_os = "emscripten", 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")))]
|
|
|
|
#[cfg(not(any(target_os = "emscripten", 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;
|
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}
|
|
|
|
};
|
2018-11-12 17:12:47 +01:00
|
|
|
pub use libp2p_core_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
|
|
|
|
2018-12-14 10:41:54 +01:00
|
|
|
/// Builds a `Transport` that supports the most commonly-used protocols that libp2p supports.
|
|
|
|
///
|
|
|
|
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
|
|
|
|
/// > reserves the right to support additional protocols or remove deprecated protocols.
|
2020-05-18 18:37:42 +10:00
|
|
|
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
|
2019-03-11 13:42:53 +01:00
|
|
|
pub fn build_development_transport(keypair: identity::Keypair)
|
2020-05-13 17:51:11 +02:00
|
|
|
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
|
2018-12-14 10:41:54 +01:00
|
|
|
{
|
2019-03-11 13:42:53 +01:00
|
|
|
build_tcp_ws_secio_mplex_yamux(keypair)
|
2018-12-14 10:41:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds an implementation of `Transport` that is suitable for usage with the `Swarm`.
|
|
|
|
///
|
|
|
|
/// The implementation supports TCP/IP, WebSockets over TCP/IP, secio as the encryption layer,
|
|
|
|
/// and mplex or yamux as the multiplexing layer.
|
|
|
|
///
|
|
|
|
/// > **Note**: If you ever need to express the type of this `Transport`.
|
2020-05-18 18:37:42 +10:00
|
|
|
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux"))))]
|
2019-03-11 13:42:53 +01:00
|
|
|
pub fn build_tcp_ws_secio_mplex_yamux(keypair: identity::Keypair)
|
2020-05-13 17:51:11 +02:00
|
|
|
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
|
2018-12-14 10:41:54 +01:00
|
|
|
{
|
2020-03-11 15:33:22 +01:00
|
|
|
let transport = {
|
|
|
|
let tcp = tcp::TcpConfig::new().nodelay(true);
|
|
|
|
let transport = dns::DnsConfig::new(tcp)?;
|
|
|
|
let trans_clone = transport.clone();
|
|
|
|
transport.or_transport(websocket::WsConfig::new(trans_clone))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(transport
|
2019-10-10 11:31:44 +02:00
|
|
|
.upgrade(core::upgrade::Version::V1)
|
Rework the transport upgrade API. (#1240)
* Rework the transport upgrade API.
ALthough transport upgrades must follow a specific pattern
in order fot the resulting transport to be usable with a
`Network` or `Swarm`, that pattern is currently not well
reflected in the transport upgrade API. Rather, transport
upgrades are rather laborious and involve non-trivial code
duplication.
This commit introduces a `transport::upgrade::Builder` that is
obtained from `Transport::upgrade`. The `Builder` encodes the
previously implicit rules for transport upgrades:
1. Authentication upgrades must happen first.
2. Any number of upgrades may follow.
3. A multiplexer upgrade must happen last.
Since multiplexing is the last (regular) transport upgrade (because
that upgrade yields a `StreamMuxer` which is no longer a `AsyncRead`
/ `AsyncWrite` resource, which the upgrade process is based on),
the upgrade starts with `Transport::upgrade` and ends with
`Builder::multiplex`, which drops back down to the `Transport`,
providing a fluent API.
Authentication and multiplexer upgrades must furthermore adhere
to a minimal contract w.r.t their outputs:
1. An authentication upgrade is given an (async) I/O resource `C`
and must produce a pair `(I, D)` where `I: ConnectionInfo` and
`D` is a new (async) I/O resource `D`.
2. A multiplexer upgrade is given an (async) I/O resource `C`
and must produce a `M: StreamMuxer`.
To that end, two changes to the `secio` and `noise` protocols have been
made:
1. The `secio` upgrade now outputs a pair of `(PeerId, SecioOutput)`.
The former implements `ConnectionInfo` and the latter `AsyncRead` /
`AsyncWrite`, fulfilling the `Builder` contract.
2. A new `NoiseAuthenticated` upgrade has been added that wraps around
any noise upgrade (i.e. `NoiseConfig`) and has an output of
`(PeerId, NoiseOutput)`, i.e. it checks if the `RemoteIdentity` from
the handshake output is an `IdentityKey`, failing if that is not the
case. This is the standard upgrade procedure one wants for integrating
noise with libp2p-core/swarm.
* Cleanup
* Add a new integration test.
* Add missing license.
2019-09-10 15:42:45 +02:00
|
|
|
.authenticate(secio::SecioConfig::new(keypair))
|
|
|
|
.multiplex(core::upgrade::SelectUpgrade::new(yamux::Config::default(), mplex::MplexConfig::new()))
|
|
|
|
.map(|(peer, muxer), _| (peer, core::muxing::StreamMuxerBox::new(muxer)))
|
2020-05-13 17:51:11 +02:00
|
|
|
.timeout(std::time::Duration::from_secs(20)))
|
2018-12-14 10:41:54 +01:00
|
|
|
}
|
|
|
|
|
2020-01-28 13:22:09 +01:00
|
|
|
/// Builds an implementation of `Transport` that is suitable for usage with the `Swarm`.
|
|
|
|
///
|
|
|
|
/// The implementation supports TCP/IP, WebSockets over TCP/IP, secio as the encryption layer,
|
|
|
|
/// and mplex or yamux as the multiplexing layer.
|
|
|
|
///
|
|
|
|
/// > **Note**: If you ever need to express the type of this `Transport`.
|
2020-05-18 18:37:42 +10:00
|
|
|
#[cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "unknown")), feature = "tcp-async-std", feature = "websocket", feature = "secio", feature = "mplex", feature = "yamux", feature = "pnet"))))]
|
2020-01-28 13:22:09 +01:00
|
|
|
pub fn build_tcp_ws_pnet_secio_mplex_yamux(keypair: identity::Keypair, psk: PreSharedKey)
|
2020-05-13 17:51:11 +02:00
|
|
|
-> std::io::Result<impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send, Error = impl Into<std::io::Error>> + Send + Sync), Error = impl std::error::Error + Send, Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone>
|
2020-01-28 13:22:09 +01:00
|
|
|
{
|
2020-03-11 15:33:22 +01:00
|
|
|
let transport = {
|
|
|
|
let tcp = tcp::TcpConfig::new().nodelay(true);
|
|
|
|
let transport = dns::DnsConfig::new(tcp)?;
|
|
|
|
let trans_clone = transport.clone();
|
|
|
|
transport.or_transport(websocket::WsConfig::new(trans_clone))
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(transport
|
2020-01-28 13:22:09 +01:00
|
|
|
.and_then(move |socket, _| PnetConfig::new(psk).handshake(socket))
|
|
|
|
.upgrade(core::upgrade::Version::V1)
|
|
|
|
.authenticate(secio::SecioConfig::new(keypair))
|
|
|
|
.multiplex(core::upgrade::SelectUpgrade::new(yamux::Config::default(), mplex::MplexConfig::new()))
|
|
|
|
.map(|(peer, muxer), _| (peer, core::muxing::StreamMuxerBox::new(muxer)))
|
2020-05-13 17:51:11 +02:00
|
|
|
.timeout(std::time::Duration::from_secs(20)))
|
2020-01-28 13:22:09 +01:00
|
|
|
}
|