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
|
2022-02-10 16:36:20 +01:00
|
|
|
//! at the [`tutorials`](crate::tutorials). Further examples can be found in the
|
2021-04-01 15:46:41 +02:00
|
|
|
//! [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
|
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)]
|
2020-11-17 11:15:20 +01:00
|
|
|
pub use libp2p_core::multihash;
|
2019-02-18 13:35:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use multiaddr;
|
2018-10-26 11:07:59 +02:00
|
|
|
|
2022-01-14 10:27:28 +01:00
|
|
|
#[cfg(feature = "autonat")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "autonat")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_autonat as autonat;
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_core as core;
|
2022-02-08 15:56:35 +01:00
|
|
|
#[cfg(feature = "dcutr")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "dcutr")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_dcutr as dcutr;
|
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 = "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")))]
|
2022-02-14 21:24:58 +11:00
|
|
|
#[cfg(not(target_os = "unknown"))]
|
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 = "identify")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "identify")))]
|
2020-01-25 02:16:02 +11:00
|
|
|
#[doc(inline)]
|
2019-01-14 14:10:51 +01:00
|
|
|
pub use libp2p_identify as identify;
|
2020-03-11 15:33:22 +01:00
|
|
|
#[cfg(feature = "kad")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "kad")))]
|
2020-01-25 02:16:02 +11:00
|
|
|
#[doc(inline)]
|
2019-01-14 14:10:51 +01:00
|
|
|
pub use libp2p_kad as kad;
|
2022-09-01 23:53:38 -04:00
|
|
|
#[cfg(any(feature = "mdns-async-io", feature = "mdns-tokio"))]
|
|
|
|
#[cfg_attr(
|
|
|
|
docsrs,
|
|
|
|
doc(cfg(any(feature = "mdns-tokio", feature = "mdns-async-io")))
|
|
|
|
)]
|
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;
|
2021-08-13 22:51:54 +02:00
|
|
|
#[cfg(feature = "metrics")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_metrics as metrics;
|
2021-03-16 11:48:48 +01:00
|
|
|
#[cfg(feature = "mplex")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mplex")))]
|
2019-01-30 11:36:00 +01: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 = "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 = "pnet")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "pnet")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
2020-01-28 13:22:09 +01:00
|
|
|
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")))]
|
2019-01-14 14:10:51 +01:00
|
|
|
#[doc(inline)]
|
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
|
|
|
pub use libp2p_relay as relay;
|
2021-09-08 00:36:52 +10:00
|
|
|
#[cfg(feature = "rendezvous")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "rendezvous")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_rendezvous as rendezvous;
|
2020-07-01 15:36:20 +02:00
|
|
|
#[cfg(feature = "request-response")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "request-response")))]
|
2019-05-06 11:17:35 +02:00
|
|
|
#[doc(inline)]
|
2020-07-01 15:36:20 +02:00
|
|
|
pub use libp2p_request_response as request_response;
|
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;
|
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)]
|
2022-02-10 16:36:20 +01:00
|
|
|
pub mod tutorials;
|
2021-04-01 15:46:41 +02:00
|
|
|
|
2018-11-15 17:41:11 +01:00
|
|
|
pub use self::core::{
|
2019-03-11 13:42:53 +01:00
|
|
|
identity,
|
2019-01-10 11:27:06 +01:00
|
|
|
transport::TransportError,
|
2018-11-15 17:41:11 +01:00
|
|
|
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt},
|
2020-10-16 16:53:02 +02:00
|
|
|
PeerId, Transport,
|
2018-11-15 17:41:11 +01:00
|
|
|
};
|
2019-02-18 13:35:51 +01:00
|
|
|
pub use self::multiaddr::{multiaddr as build_multiaddr, 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;
|
2021-01-26 22:49:08 +01:00
|
|
|
pub use libp2p_swarm_derive::NetworkBehaviour;
|
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"
|
|
|
|
)))
|
2021-08-11 13:12:12 +02:00
|
|
|
)]
|
2021-03-16 11:48:48 +01:00
|
|
|
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 = {
|
2022-07-04 04:16:57 +02:00
|
|
|
let dns_tcp = dns::DnsConfig::system(tcp::TcpTransport::new(
|
|
|
|
tcp::GenTcpConfig::new().nodelay(true),
|
|
|
|
))
|
|
|
|
.await?;
|
transports/{tcp,dns,websocket}: Remove Clone imp for *Config (#2682)
This commit removes the `Clone` implementation on `GenTcpConfig` and consequently the `Clone`
implementations on `GenDnsConfig` and `WsConfig`.
When port-reuse is enabled, `GenTcpConfig` tracks the addresses it is listening in a `HashSet`. This
`HashSet` is shared with the `TcpListenStream`s via an `Arc<Mutex<_>>`. Given that `Clone` is
`derive`d on `GenTcpConfig`, cloning a `GenTcpConfig`, results in both instances sharing the same
set of listen addresses. This is not intuitive.
This behavior is for example error prone in the scenario where one wants to speak both plain DNS/TCP and
Websockets. Say a user creates the transport in the following way:
``` Rust
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true).port_reuse(true);
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)
};
```
Both `dns_tcp` and `ws_dns_tcp` share the set of listen addresses, given the `dns_tcp.clone()` to
create the `ws_dns_tcp`. Thus, with port-reuse, a Websocket dial might reuse a DNS/TCP listening
port instead of a Websocket listening port.
With this commit a user is forced to do the below, preventing the above error:
``` Rust
let transport = {
let dns_tcp = dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?;
let ws_dns_tcp = websocket::WsConfig::new(
dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?,
);
dns_tcp.or_transport(ws_dns_tcp)
};
```
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2022-05-31 12:29:43 +02:00
|
|
|
let ws_dns_tcp = websocket::WsConfig::new(
|
2022-07-04 04:16:57 +02:00
|
|
|
dns::DnsConfig::system(tcp::TcpTransport::new(
|
|
|
|
tcp::GenTcpConfig::new().nodelay(true),
|
|
|
|
))
|
|
|
|
.await?,
|
transports/{tcp,dns,websocket}: Remove Clone imp for *Config (#2682)
This commit removes the `Clone` implementation on `GenTcpConfig` and consequently the `Clone`
implementations on `GenDnsConfig` and `WsConfig`.
When port-reuse is enabled, `GenTcpConfig` tracks the addresses it is listening in a `HashSet`. This
`HashSet` is shared with the `TcpListenStream`s via an `Arc<Mutex<_>>`. Given that `Clone` is
`derive`d on `GenTcpConfig`, cloning a `GenTcpConfig`, results in both instances sharing the same
set of listen addresses. This is not intuitive.
This behavior is for example error prone in the scenario where one wants to speak both plain DNS/TCP and
Websockets. Say a user creates the transport in the following way:
``` Rust
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true).port_reuse(true);
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)
};
```
Both `dns_tcp` and `ws_dns_tcp` share the set of listen addresses, given the `dns_tcp.clone()` to
create the `ws_dns_tcp`. Thus, with port-reuse, a Websocket dial might reuse a DNS/TCP listening
port instead of a Websocket listening port.
With this commit a user is forced to do the below, preventing the above error:
``` Rust
let transport = {
let dns_tcp = dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?;
let ws_dns_tcp = websocket::WsConfig::new(
dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?,
);
dns_tcp.or_transport(ws_dns_tcp)
};
```
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2022-05-31 12:29:43 +02:00
|
|
|
);
|
2021-03-17 10:53:19 +01:00
|
|
|
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"
|
|
|
|
)))
|
2021-08-11 13:12:12 +02:00
|
|
|
)]
|
2021-03-16 11:48:48 +01:00
|
|
|
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)>> {
|
2020-03-11 15:33:22 +01:00
|
|
|
let transport = {
|
2022-07-04 04:16:57 +02:00
|
|
|
let dns_tcp = dns::TokioDnsConfig::system(tcp::TokioTcpTransport::new(
|
|
|
|
tcp::GenTcpConfig::new().nodelay(true),
|
|
|
|
))?;
|
transports/{tcp,dns,websocket}: Remove Clone imp for *Config (#2682)
This commit removes the `Clone` implementation on `GenTcpConfig` and consequently the `Clone`
implementations on `GenDnsConfig` and `WsConfig`.
When port-reuse is enabled, `GenTcpConfig` tracks the addresses it is listening in a `HashSet`. This
`HashSet` is shared with the `TcpListenStream`s via an `Arc<Mutex<_>>`. Given that `Clone` is
`derive`d on `GenTcpConfig`, cloning a `GenTcpConfig`, results in both instances sharing the same
set of listen addresses. This is not intuitive.
This behavior is for example error prone in the scenario where one wants to speak both plain DNS/TCP and
Websockets. Say a user creates the transport in the following way:
``` Rust
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true).port_reuse(true);
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)
};
```
Both `dns_tcp` and `ws_dns_tcp` share the set of listen addresses, given the `dns_tcp.clone()` to
create the `ws_dns_tcp`. Thus, with port-reuse, a Websocket dial might reuse a DNS/TCP listening
port instead of a Websocket listening port.
With this commit a user is forced to do the below, preventing the above error:
``` Rust
let transport = {
let dns_tcp = dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?;
let ws_dns_tcp = websocket::WsConfig::new(
dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?,
);
dns_tcp.or_transport(ws_dns_tcp)
};
```
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2022-05-31 12:29:43 +02:00
|
|
|
let ws_dns_tcp = websocket::WsConfig::new(dns::TokioDnsConfig::system(
|
2022-07-04 04:16:57 +02:00
|
|
|
tcp::TokioTcpTransport::new(tcp::GenTcpConfig::new().nodelay(true)),
|
transports/{tcp,dns,websocket}: Remove Clone imp for *Config (#2682)
This commit removes the `Clone` implementation on `GenTcpConfig` and consequently the `Clone`
implementations on `GenDnsConfig` and `WsConfig`.
When port-reuse is enabled, `GenTcpConfig` tracks the addresses it is listening in a `HashSet`. This
`HashSet` is shared with the `TcpListenStream`s via an `Arc<Mutex<_>>`. Given that `Clone` is
`derive`d on `GenTcpConfig`, cloning a `GenTcpConfig`, results in both instances sharing the same
set of listen addresses. This is not intuitive.
This behavior is for example error prone in the scenario where one wants to speak both plain DNS/TCP and
Websockets. Say a user creates the transport in the following way:
``` Rust
let transport = {
let tcp = tcp::TcpConfig::new().nodelay(true).port_reuse(true);
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)
};
```
Both `dns_tcp` and `ws_dns_tcp` share the set of listen addresses, given the `dns_tcp.clone()` to
create the `ws_dns_tcp`. Thus, with port-reuse, a Websocket dial might reuse a DNS/TCP listening
port instead of a Websocket listening port.
With this commit a user is forced to do the below, preventing the above error:
``` Rust
let transport = {
let dns_tcp = dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?;
let ws_dns_tcp = websocket::WsConfig::new(
dns::DnsConfig::system(tcp::TcpConfig::new().nodelay(true).port_reuse(true)).await?,
);
dns_tcp.or_transport(ws_dns_tcp)
};
```
Co-authored-by: Thomas Eizinger <thomas@eizinger.io>
2022-05-31 12:29:43 +02:00
|
|
|
)?);
|
2021-03-17 10:53:19 +01:00
|
|
|
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
|
|
|
}
|