libp2p/
lib.rs

1// Copyright 2018 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! libp2p is a modular peer-to-peer networking framework.
22//!
23//! To learn more about the general libp2p multi-language framework visit <https://libp2p.io>.
24//!
25//! To get started with this libp2p implementation in Rust, please take a look
26//! at the [`tutorials`]. Further examples can be found in the
27//! [examples] directory.
28//!
29//! [examples]: https://github.com/libp2p/rust-libp2p/tree/master/examples
30
31#![doc(html_logo_url = "https://libp2p.io/img/logo_small.png")]
32#![doc(html_favicon_url = "https://libp2p.io/img/favicon.png")]
33#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
34
35pub use bytes;
36pub use futures;
37#[doc(inline)]
38pub use libp2p_core::multihash;
39#[doc(inline)]
40pub use multiaddr;
41
42#[doc(inline)]
43pub use libp2p_allow_block_list as allow_block_list;
44#[cfg(feature = "autonat")]
45#[doc(inline)]
46pub use libp2p_autonat as autonat;
47#[doc(inline)]
48pub use libp2p_connection_limits as connection_limits;
49#[doc(inline)]
50pub use libp2p_core as core;
51#[cfg(feature = "dcutr")]
52#[doc(inline)]
53pub use libp2p_dcutr as dcutr;
54
55#[cfg(feature = "deflate")]
56#[cfg(not(target_arch = "wasm32"))]
57#[deprecated(
58    note = "Will be removed in the next release, see https://github.com/libp2p/rust-libp2p/issues/4522 for details."
59)]
60pub mod deflate {
61    pub use libp2p_deflate::*;
62}
63#[cfg(feature = "dns")]
64#[cfg_attr(docsrs, doc(cfg(feature = "dns")))]
65#[cfg(not(target_arch = "wasm32"))]
66#[doc(inline)]
67pub use libp2p_dns as dns;
68#[cfg(feature = "floodsub")]
69#[doc(inline)]
70pub use libp2p_floodsub as floodsub;
71#[cfg(feature = "gossipsub")]
72#[doc(inline)]
73pub use libp2p_gossipsub as gossipsub;
74#[cfg(feature = "identify")]
75#[doc(inline)]
76pub use libp2p_identify as identify;
77#[cfg(feature = "kad")]
78#[doc(inline)]
79pub use libp2p_kad as kad;
80#[cfg(feature = "mdns")]
81#[cfg(not(target_arch = "wasm32"))]
82#[cfg_attr(docsrs, doc(cfg(feature = "mdns")))]
83#[doc(inline)]
84pub use libp2p_mdns as mdns;
85#[cfg(feature = "memory-connection-limits")]
86#[cfg(not(target_arch = "wasm32"))]
87#[cfg_attr(docsrs, doc(cfg(feature = "memory-connection-limits")))]
88#[doc(inline)]
89pub use libp2p_memory_connection_limits as memory_connection_limits;
90#[cfg(feature = "metrics")]
91#[doc(inline)]
92pub use libp2p_metrics as metrics;
93#[cfg(feature = "noise")]
94#[doc(inline)]
95pub use libp2p_noise as noise;
96#[cfg(feature = "ping")]
97#[doc(inline)]
98pub use libp2p_ping as ping;
99#[cfg(feature = "plaintext")]
100#[doc(inline)]
101pub use libp2p_plaintext as plaintext;
102#[cfg(feature = "pnet")]
103#[doc(inline)]
104pub use libp2p_pnet as pnet;
105#[cfg(feature = "quic")]
106#[cfg(not(target_arch = "wasm32"))]
107pub use libp2p_quic as quic;
108#[cfg(feature = "relay")]
109#[doc(inline)]
110pub use libp2p_relay as relay;
111#[cfg(feature = "rendezvous")]
112#[doc(inline)]
113pub use libp2p_rendezvous as rendezvous;
114#[cfg(feature = "request-response")]
115#[doc(inline)]
116pub use libp2p_request_response as request_response;
117#[doc(inline)]
118pub use libp2p_swarm as swarm;
119#[cfg(feature = "tcp")]
120#[cfg(not(target_arch = "wasm32"))]
121#[cfg_attr(docsrs, doc(cfg(feature = "tcp")))]
122#[doc(inline)]
123pub use libp2p_tcp as tcp;
124#[cfg(feature = "tls")]
125#[cfg_attr(docsrs, doc(cfg(feature = "tls")))]
126#[cfg(not(target_arch = "wasm32"))]
127#[doc(inline)]
128pub use libp2p_tls as tls;
129#[cfg(feature = "uds")]
130#[cfg_attr(docsrs, doc(cfg(feature = "uds")))]
131#[cfg(not(target_arch = "wasm32"))]
132#[doc(inline)]
133pub use libp2p_uds as uds;
134#[cfg(feature = "upnp")]
135#[cfg(not(target_arch = "wasm32"))]
136#[doc(inline)]
137pub use libp2p_upnp as upnp;
138#[cfg(feature = "wasm-ext")]
139#[deprecated(
140    note = "`wasm-ext` is deprecated and will be removed in favor of `libp2p-websocket-websys`."
141)]
142pub mod wasm_ext {
143    #[doc(inline)]
144    pub use libp2p_wasm_ext::*;
145}
146#[cfg(feature = "websocket")]
147#[cfg(not(target_arch = "wasm32"))]
148#[doc(inline)]
149pub use libp2p_websocket as websocket;
150#[cfg(feature = "websocket-websys")]
151#[doc(inline)]
152pub use libp2p_websocket_websys as websocket_websys;
153#[cfg(feature = "webtransport-websys")]
154#[cfg_attr(docsrs, doc(cfg(feature = "webtransport-websys")))]
155#[doc(inline)]
156pub use libp2p_webtransport_websys as webtransport_websys;
157#[cfg(feature = "yamux")]
158#[doc(inline)]
159pub use libp2p_yamux as yamux;
160
161mod builder;
162mod transport_ext;
163
164pub mod bandwidth;
165
166#[cfg(doc)]
167pub mod tutorials;
168
169pub use self::builder::SwarmBuilder;
170pub use self::core::{
171    transport::TransportError,
172    upgrade::{InboundUpgrade, OutboundUpgrade},
173    Transport,
174};
175pub use self::multiaddr::{multiaddr as build_multiaddr, Multiaddr};
176pub use self::swarm::Swarm;
177pub use self::transport_ext::TransportExt;
178pub use libp2p_identity as identity;
179pub use libp2p_identity::PeerId;
180pub use libp2p_swarm::{Stream, StreamProtocol};
181
182/// Builds a `Transport` based on TCP/IP that supports the most commonly-used features of libp2p:
183///
184///  * DNS resolution.
185///  * Noise protocol encryption.
186///  * Websockets.
187///  * Yamux for substream multiplexing.
188///
189/// All async I/O of the transport is based on `async-std`.
190///
191/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
192/// >           reserves the right to support additional protocols or remove deprecated protocols.
193#[deprecated(note = "Use `libp2p::SwarmBuilder` instead.")]
194#[cfg(all(
195    not(target_arch = "wasm32"),
196    feature = "tcp",
197    feature = "dns",
198    feature = "websocket",
199    feature = "noise",
200    feature = "yamux",
201    feature = "async-std",
202))]
203pub async fn development_transport(
204    keypair: identity::Keypair,
205) -> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>> {
206    let transport = {
207        let dns_tcp = dns::async_std::Transport::system(tcp::async_io::Transport::new(
208            tcp::Config::new().nodelay(true),
209        ))
210        .await?;
211        let ws_dns_tcp = websocket::WsConfig::new(
212            dns::async_std::Transport::system(tcp::async_io::Transport::new(
213                tcp::Config::new().nodelay(true),
214            ))
215            .await?,
216        );
217        dns_tcp.or_transport(ws_dns_tcp)
218    };
219
220    Ok(transport
221        .upgrade(core::upgrade::Version::V1)
222        .authenticate(noise::Config::new(&keypair).unwrap())
223        .multiplex(yamux::Config::default())
224        .timeout(std::time::Duration::from_secs(20))
225        .boxed())
226}
227
228/// Builds a `Transport` based on TCP/IP that supports the most commonly-used features of libp2p:
229///
230///  * DNS resolution.
231///  * Noise protocol encryption.
232///  * Websockets.
233///  * Yamux for substream multiplexing.
234///
235/// All async I/O of the transport is based on `tokio`.
236///
237/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
238/// >           reserves the right to support additional protocols or remove deprecated protocols.
239#[deprecated(note = "Use `libp2p::SwarmBuilder` instead.")]
240#[cfg(all(
241    not(target_arch = "wasm32"),
242    feature = "tcp",
243    feature = "dns",
244    feature = "websocket",
245    feature = "noise",
246    feature = "yamux",
247    feature = "tokio",
248))]
249pub fn tokio_development_transport(
250    keypair: identity::Keypair,
251) -> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>> {
252    let transport = {
253        let dns_tcp = dns::tokio::Transport::system(tcp::tokio::Transport::new(
254            tcp::Config::new().nodelay(true),
255        ))?;
256        let ws_dns_tcp = websocket::WsConfig::new(dns::tokio::Transport::system(
257            tcp::tokio::Transport::new(tcp::Config::new().nodelay(true)),
258        )?);
259        dns_tcp.or_transport(ws_dns_tcp)
260    };
261
262    Ok(transport
263        .upgrade(core::upgrade::Version::V1)
264        .authenticate(noise::Config::new(&keypair).unwrap())
265        .multiplex(yamux::Config::default())
266        .timeout(std::time::Duration::from_secs(20))
267        .boxed())
268}