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.
|
|
|
|
|
2021-04-01 15:46:41 +02:00
|
|
|
//! libp2p is a modular peer-to-peer networking framework.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2021-04-01 15:46:41 +02:00
|
|
|
//! To learn more about the general libp2p multi-language framework visit
|
|
|
|
//! [libp2p.io](https://libp2p.io/).
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2021-04-01 15:46:41 +02:00
|
|
|
//! To get started with this libp2p implementation in Rust, please take a look
|
|
|
|
//! at the [`tutorial`](crate::tutorial). Further examples can be found in the
|
|
|
|
//! [examples] directory.
|
2018-08-22 10:46:23 +02:00
|
|
|
//!
|
2021-04-01 15:46:41 +02:00
|
|
|
//! [examples]: https://github.com/libp2p/rust-libp2p/tree/master/examples
|
|
|
|
//! [ping tutorial]: https://github.com/libp2p/rust-libp2p/tree/master/examples/ping.rs
|
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;
|
|
|
|
|
2021-04-01 15:46:41 +02:00
|
|
|
#[cfg(doc)]
|
|
|
|
pub mod tutorial;
|
|
|
|
|
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-17 10:53:19 +01:00
|
|
|
let dns_tcp = dns::DnsConfig::system(tcp).await?;
|
|
|
|
let ws_dns_tcp = websocket::WsConfig::new(dns_tcp.clone());
|
|
|
|
dns_tcp.or_transport(ws_dns_tcp)
|
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-17 10:53:19 +01:00
|
|
|
let dns_tcp = dns::TokioDnsConfig::system(tcp)?;
|
|
|
|
let ws_dns_tcp = websocket::WsConfig::new(dns_tcp.clone());
|
|
|
|
dns_tcp.or_transport(ws_dns_tcp)
|
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
|
|
|
}
|