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;
|
2021-08-11 13:12:12 +02: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;
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg(feature = "dns")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
|
2022-10-20 14:17:20 +11:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
|
|
|
#[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;
|
2021-08-11 13:12:12 +02:00
|
|
|
#[cfg(feature = "identify")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "identify")))]
|
2020-01-25 02:16:02 +11:00
|
|
|
#[doc(inline)]
|
2021-08-11 13:12:12 +02:00
|
|
|
pub use libp2p_identify as identify;
|
|
|
|
#[cfg(feature = "kad")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "kad")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_kad as kad;
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg(feature = "mdns")]
|
2020-06-30 17:31:02 +02:00
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
|
2022-10-20 14:17:20 +11: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-08-11 13:12:12 +02:00
|
|
|
#[cfg(feature = "mplex")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "mplex")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
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;
|
2021-08-11 13:12:12 +02:00
|
|
|
#[cfg(feature = "pnet")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "pnet")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_pnet as pnet;
|
|
|
|
#[cfg(feature = "relay")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "relay")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
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;
|
2021-08-11 13:12:12 +02:00
|
|
|
#[cfg(feature = "request-response")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "request-response")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_request_response as request_response;
|
2019-07-04 14:47:59 +02:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_swarm as swarm;
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg(feature = "tcp")]
|
|
|
|
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
|
2022-10-20 14:17:20 +11:00
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_tcp as tcp;
|
2022-10-24 11:54:44 +11:00
|
|
|
#[cfg(feature = "tls")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
|
|
|
|
#[doc(inline)]
|
|
|
|
pub use libp2p_tls as tls;
|
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,
|
2021-08-11 13:12:12 +02:00
|
|
|
upgrade::{InboundUpgrade, InboundUpgradeExt, OutboundUpgrade, OutboundUpgradeExt},
|
|
|
|
PeerId, Transport,
|
2018-11-15 17:41:11 +01:00
|
|
|
};
|
2021-08-11 13:12:12 +02: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-08-11 13:12:12 +02: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-08-11 13:12:12 +02:00
|
|
|
#[cfg(all(
|
|
|
|
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")),
|
2022-10-11 23:10:10 +11:00
|
|
|
any(
|
|
|
|
all(feature = "tcp-async-io", feature = "dns-async-std"),
|
|
|
|
all(feature = "tcp", feature = "dns", feature = "async-std")
|
|
|
|
),
|
2021-08-11 13:12:12 +02:00
|
|
|
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")),
|
2022-10-11 23:10:10 +11:00
|
|
|
any(
|
|
|
|
all(feature = "tcp-async-io", feature = "dns-async-std"),
|
|
|
|
all(feature = "tcp", feature = "dns", feature = "async-std")
|
|
|
|
),
|
2021-08-11 13:12:12 +02:00
|
|
|
feature = "websocket",
|
|
|
|
feature = "noise",
|
|
|
|
feature = "mplex",
|
|
|
|
feature = "yamux"
|
|
|
|
)))
|
|
|
|
)]
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg_attr(
|
|
|
|
all(
|
|
|
|
any(feature = "tcp-async-io", feature = "dns-async-std"),
|
|
|
|
not(feature = "async-std")
|
|
|
|
),
|
|
|
|
deprecated(
|
|
|
|
since = "0.49.0",
|
|
|
|
note = "The `tcp-async-io` and `dns-async-std` features are deprecated. Use the new `tcp` and `dns` features together with the `async-std` feature."
|
|
|
|
)
|
|
|
|
)]
|
2021-08-11 13:12:12 +02:00
|
|
|
pub async fn development_transport(
|
|
|
|
keypair: identity::Keypair,
|
|
|
|
) -> 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
|
|
|
};
|
|
|
|
|
|
|
|
Ok(transport
|
|
|
|
.upgrade(core::upgrade::Version::V1)
|
2022-09-16 11:41:35 +10:00
|
|
|
.authenticate(noise::NoiseAuthenticated::xx(&keypair).unwrap())
|
2021-08-11 13:12:12 +02: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.
|
2021-08-11 13:12:12 +02:00
|
|
|
#[cfg(all(
|
|
|
|
not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")),
|
2022-10-11 23:10:10 +11:00
|
|
|
any(
|
|
|
|
all(feature = "tcp-tokio", feature = "dns-tokio"),
|
|
|
|
all(feature = "tcp", feature = "dns", feature = "tokio")
|
|
|
|
),
|
2021-08-11 13:12:12 +02:00
|
|
|
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")),
|
2022-10-11 23:10:10 +11:00
|
|
|
any(
|
|
|
|
all(feature = "tcp-tokio", feature = "dns-tokio"),
|
|
|
|
all(feature = "tcp", feature = "dns", feature = "tokio")
|
|
|
|
),
|
2021-08-11 13:12:12 +02:00
|
|
|
feature = "websocket",
|
|
|
|
feature = "noise",
|
|
|
|
feature = "mplex",
|
|
|
|
feature = "yamux"
|
|
|
|
)))
|
|
|
|
)]
|
2022-10-11 23:10:10 +11:00
|
|
|
#[cfg_attr(
|
|
|
|
all(
|
|
|
|
any(feature = "tcp-tokio", feature = "dns-tokio"),
|
|
|
|
not(feature = "tokio")
|
|
|
|
),
|
|
|
|
deprecated(
|
|
|
|
since = "0.49.0",
|
|
|
|
note = "The `tcp-tokio` and `dns-tokio` features are deprecated. Use the new `tcp` and `dns` feature together with the `tokio` feature."
|
|
|
|
)
|
|
|
|
)]
|
2021-08-11 13:12:12 +02:00
|
|
|
pub fn tokio_development_transport(
|
|
|
|
keypair: identity::Keypair,
|
|
|
|
) -> 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
|
|
|
};
|
|
|
|
|
|
|
|
Ok(transport
|
2020-01-28 13:22:09 +01:00
|
|
|
.upgrade(core::upgrade::Version::V1)
|
2022-09-16 11:41:35 +10:00
|
|
|
.authenticate(noise::NoiseAuthenticated::xx(&keypair).unwrap())
|
2021-08-11 13:12:12 +02: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
|
|
|
}
|