[tcp] Port-reuse, async-io, if-watch (#1887)

* Update tomls.

* Let transports decide when to translate.

* Improve tcp transport.

* Update stuff.

* Remove background task. Enhance documentation.

To avoid spawning a background task and thread within
`TcpConfig::new()`, with communication via unbounded channels,
a `TcpConfig` now keeps track of the listening addresses
for port reuse in an `Arc<RwLock>`. Furthermore, an `IfWatcher`
is only used by a `TcpListenStream` if it listens on any interface
and directly polls the `IfWatcher` both for initialisation and
new events.

Includes some documentation and test enhancements.

* Reintroduce feature flags for tokio vs async-io.

To avoid having an extra reactor thread running for tokio
users and to make sure all TCP I/O uses the mio-based
tokio reactor.

Thereby run tests with both backends.

* Add missing files.

* Fix docsrs attributes.

* Update transports/tcp/src/lib.rs

Co-authored-by: Max Inden <mail@max-inden.de>

* Restore chat-tokio example.

* Forward poll_write_vectored for tokio's AsyncWrite.

* Update changelogs.

Co-authored-by: David Craven <david@craven.ch>
Co-authored-by: Max Inden <mail@max-inden.de>
This commit is contained in:
Roman Borschel 2021-01-12 13:35:11 +01:00 committed by GitHub
parent c98b9ef407
commit ec0f8a3150
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
44 changed files with 1390 additions and 612 deletions

View File

@ -25,9 +25,10 @@
# Version 0.34.0 [unreleased]
- Update `libp2p-gossipsub`, `libp2p-kad` and `libp2p-request-response`.
- Update `libp2p-core` and all dependent crates.
- Update dependencies.
- The `tcp-async-std` feature is now `tcp-async-io`, still
enabled by default.
# Version 0.33.0 [2020-12-17]

View File

@ -25,7 +25,7 @@ default = [
"pnet",
"request-response",
"secp256k1",
"tcp-async-std",
"tcp-async-io",
"uds",
"wasm-ext",
"websocket",
@ -44,7 +44,7 @@ ping = ["libp2p-ping"]
plaintext = ["libp2p-plaintext"]
pnet = ["libp2p-pnet"]
request-response = ["libp2p-request-response"]
tcp-async-std = ["libp2p-tcp", "libp2p-tcp/async-std"]
tcp-async-io = ["libp2p-tcp", "libp2p-tcp/async-io"]
tcp-tokio = ["libp2p-tcp", "libp2p-tcp/tokio"]
uds = ["libp2p-uds"]
wasm-ext = ["libp2p-wasm-ext"]
@ -91,7 +91,7 @@ libp2p-tcp = { version = "0.27.0", path = "transports/tcp", optional = true }
libp2p-websocket = { version = "0.28.0", path = "transports/websocket", optional = true }
[dev-dependencies]
async-std = "1.6.2"
async-std = { version = "1.6.2", features = ["attributes"] }
env_logger = "0.8.1"
tokio = { version = "0.3", features = ["io-util", "io-std", "stream", "macros", "rt", "rt-multi-thread"] }

View File

@ -1,5 +1,9 @@
# 0.27.0 [unreleased]
- (Re)add `Transport::address_translation` to permit transport-specific
translations of observed addresses onto listening addresses.
[PR 1887](https://github.com/libp2p/rust-libp2p/pull/1887)
- Update dependencies.
# 0.26.0 [2020-12-17]

View File

@ -39,11 +39,11 @@ zeroize = "1"
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false }
[dev-dependencies]
async-std = "1.6.2"
async-std = { version = "1.6.2", features = ["attributes"] }
criterion = "0.3"
libp2p-mplex = { path = "../muxers/mplex" }
libp2p-noise = { path = "../protocols/noise" }
libp2p-tcp = { path = "../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../transports/tcp" }
multihash = { version = "0.13", default-features = false, features = ["arb"] }
quickcheck = "0.9.0"
wasm-timer = "0.2"

View File

@ -428,6 +428,8 @@ mod tests {
fn dial(self, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
panic!()
}
fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> { None }
}
async_std::task::block_on(async move {
@ -466,6 +468,8 @@ mod tests {
fn dial(self, _: Multiaddr) -> Result<Self::Dial, transport::TransportError<Self::Error>> {
panic!()
}
fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> { None }
}
async_std::task::block_on(async move {

View File

@ -477,4 +477,11 @@ where
},
}
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
match self {
EitherTransport::Left(a) => a.address_translation(server, observed),
EitherTransport::Right(b) => b.address_translation(server, observed),
}
}
}

View File

@ -30,7 +30,6 @@ use crate::{
Executor,
Multiaddr,
PeerId,
address_translation,
connection::{
ConnectionId,
ConnectionLimit,
@ -176,30 +175,27 @@ where
self.listeners.listen_addrs()
}
/// Call this function in order to know which address remotes should dial to
/// access your local node.
/// Maps the given `observed_addr`, representing an address of the local
/// node observed by a remote peer, onto the locally known listen addresses
/// to yield one or more addresses of the local node that may be publicly
/// reachable.
///
/// When receiving an observed address on a tcp connection that we initiated, the observed
/// address contains our tcp dial port, not our tcp listen port. We know which port we are
/// listening on, thereby we can replace the port within the observed address.
///
/// When receiving an observed address on a tcp connection that we did **not** initiated, the
/// observed address should contain our listening port. In case it differs from our listening
/// port there might be a proxy along the path.
///
/// # Arguments
///
/// * `observed_addr` - should be an address a remote observes you as, which can be obtained for
/// example with the identify protocol.
/// I.e. this method incorporates the view of other peers into the listen
/// addresses seen by the local node to account for possible IP and port
/// mappings performed by intermediate network devices in an effort to
/// obtain addresses for the local peer that are also reachable for peers
/// other than the peer who reported the `observed_addr`.
///
/// The translation is transport-specific. See [`Transport::address_translation`].
pub fn address_translation<'a>(&'a self, observed_addr: &'a Multiaddr)
-> impl Iterator<Item = Multiaddr> + 'a
where
TMuxer: 'a,
THandler: 'a,
{
let transport = self.listeners.transport();
let mut addrs: Vec<_> = self.listen_addrs()
.filter_map(move |server| address_translation(server, observed_addr))
.filter_map(move |server| transport.address_translation(server, observed_addr))
.collect();
// remove duplicates

View File

@ -128,6 +128,11 @@ pub trait Transport {
where
Self: Sized;
/// Performs a transport-specific mapping of an address `observed` by
/// a remote onto a local `listen` address to yield an address for
/// the local node that may be reachable for other peers.
fn address_translation(&self, listen: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
/// Boxes the transport, including custom transport errors.
fn boxed(self) -> boxed::Boxed<Self::Output>
where

View File

@ -69,6 +69,10 @@ where
};
Ok(future)
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}
}
/// Custom `Stream` to avoid boxing.

View File

@ -51,6 +51,7 @@ type ListenerUpgrade<O> = Pin<Box<dyn Future<Output = io::Result<O>> + Send>>;
trait Abstract<O> {
fn listen_on(&self, addr: Multiaddr) -> Result<Listener<O>, TransportError<io::Error>>;
fn dial(&self, addr: Multiaddr) -> Result<Dial<O>, TransportError<io::Error>>;
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr>;
}
impl<T, O> Abstract<O> for T
@ -78,6 +79,10 @@ where
.map_err(|e| e.map(box_err))?;
Ok(Box::pin(fut) as Dial<_>)
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
Transport::address_translation(self, server, observed)
}
}
impl<O> fmt::Debug for Boxed<O> {
@ -108,6 +113,10 @@ impl<O> Transport for Boxed<O> {
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
self.inner.dial(addr)
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}
}
fn box_err<E: Error + Send + Sync + 'static>(e: E) -> io::Error {

View File

@ -74,4 +74,12 @@ where
Err(TransportError::MultiaddrNotSupported(addr))
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if let Some(addr) = self.0.address_translation(server, observed) {
Some(addr)
} else {
self.1.address_translation(server, observed)
}
}
}

View File

@ -67,6 +67,10 @@ impl<TOut> Transport for DummyTransport<TOut> {
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
Err(TransportError::MultiaddrNotSupported(addr))
}
fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}
}
/// Implementation of `AsyncRead` and `AsyncWrite`. Not meant to be instanciated.

View File

@ -57,6 +57,10 @@ where
let p = ConnectedPoint::Dialer { address: addr };
Ok(MapFuture { inner: future, args: Some((self.fun, p)) })
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}
}
/// Custom `Stream` implementation to avoid boxing.

View File

@ -64,6 +64,10 @@ where
Err(err) => Err(err.map(map)),
}
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}
}
/// Listening stream for `MapErr`.

View File

@ -191,6 +191,10 @@ impl Transport for MemoryTransport {
DialFuture::new(port).ok_or(TransportError::Other(MemoryTransportError::Unreachable))
}
fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}
}
/// Error that can be produced from the `MemoryTransport`.

View File

@ -74,4 +74,12 @@ where
Err(TransportError::MultiaddrNotSupported(addr))
}
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
if let Some(inner) = &self.0 {
inner.address_translation(server, observed)
} else {
None
}
}
}

View File

@ -101,6 +101,10 @@ where
timer: Delay::new(self.outgoing_timeout),
})
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}
}
// TODO: can be removed and replaced with an `impl Stream` once impl Trait is fully stable

View File

@ -334,6 +334,10 @@ where
fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
self.0.listen_on(addr)
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.0.address_translation(server, observed)
}
}
/// An inbound or outbound upgrade.
@ -383,6 +387,10 @@ where
upgrade: self.upgrade
})
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}
}
/// Errors produced by a transport upgrade.

View File

@ -41,10 +41,12 @@ fn deny_incoming_connec() {
swarm1.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
let address = async_std::task::block_on(future::poll_fn(|cx| {
if let Poll::Ready(NetworkEvent::NewListenerAddress { listen_addr, .. }) = swarm1.poll(cx) {
Poll::Ready(listen_addr)
} else {
panic!("Was expecting the listen address to be reported")
match swarm1.poll(cx) {
Poll::Ready(NetworkEvent::NewListenerAddress { listen_addr, .. }) => {
Poll::Ready(listen_addr)
}
Poll::Pending => Poll::Pending,
_ => panic!("Was expecting the listen address to be reported"),
}
}));
@ -95,15 +97,15 @@ fn dial_self() {
let mut swarm = test_network(NetworkConfig::default());
swarm.listen_on("/ip4/127.0.0.1/tcp/0".parse().unwrap()).unwrap();
let (local_address, mut swarm) = async_std::task::block_on(
future::lazy(move |cx| {
if let Poll::Ready(NetworkEvent::NewListenerAddress { listen_addr, .. }) = swarm.poll(cx) {
Ok::<_, void::Void>((listen_addr, swarm))
} else {
panic!("Was expecting the listen address to be reported")
let local_address = async_std::task::block_on(future::poll_fn(|cx| {
match swarm.poll(cx) {
Poll::Ready(NetworkEvent::NewListenerAddress { listen_addr, .. }) => {
Poll::Ready(listen_addr)
}
}))
.unwrap();
Poll::Pending => Poll::Pending,
_ => panic!("Was expecting the listen address to be reported"),
}
}));
swarm.dial(&local_address, TestHandler()).unwrap();

View File

@ -36,7 +36,6 @@
//! --features="floodsub mplex noise tcp-tokio mdns-tokio"
//! ```
use futures::prelude::*;
use libp2p::{
Multiaddr,
NetworkBehaviour,
@ -154,10 +153,15 @@ async fn main() -> Result<(), Box<dyn Error>> {
loop {
let to_publish = {
tokio::select! {
line = stdin.try_next() => Some((floodsub_topic.clone(), line?.expect("Stdin closed"))),
line = stdin.next_line() => {
let line = line?.expect("stdin closed");
Some((floodsub_topic.clone(), line))
}
event = swarm.next() => {
println!("New Event: {:?}", event);
None
// All events are handled by the `NetworkBehaviourEventProcess`es.
// I.e. the `swarm.next()` future drives the `Swarm` without ever
// terminating.
panic!("Unexpected event: {:?}", event);
}
}
};
@ -171,4 +175,4 @@ async fn main() -> Result<(), Box<dyn Error>> {
}
}
}
}
}

View File

@ -26,7 +26,7 @@ async-std = "1.7.0"
criterion = "0.3"
env_logger = "0.8"
futures = "0.3"
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
libp2p-plaintext = { path = "../../protocols/plaintext" }
quickcheck = "0.9"
rand = "0.7"

View File

@ -16,6 +16,6 @@ flate2 = "1.0"
[dev-dependencies]
async-std = "1.6.2"
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
quickcheck = "0.9"
rand = "0.7"

View File

@ -22,8 +22,7 @@ wasm-timer = "0.2"
async-std = "1.6.2"
libp2p-mplex = { path = "../../muxers/mplex" }
libp2p-noise = { path = "../../protocols/noise" }
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
[build-dependencies]
prost-build = "0.6"

View File

@ -28,11 +28,11 @@ snow = { version = "0.7.1", features = ["ring-resolver"], default-features = fal
snow = { version = "0.7.1", features = ["default-resolver"], default-features = false }
[dev-dependencies]
async-io = "1.2.0"
env_logger = "0.8.1"
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
quickcheck = "0.9.0"
sodiumoxide = "0.2.5"
[build-dependencies]
prost-build = "0.6"

View File

@ -18,15 +18,16 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
use async_io::Async;
use futures::{future::{self, Either}, prelude::*};
use libp2p_core::identity;
use libp2p_core::upgrade::{self, Negotiated, apply_inbound, apply_outbound};
use libp2p_core::transport::{Transport, ListenerEvent};
use libp2p_noise::{Keypair, X25519, X25519Spec, NoiseConfig, RemoteIdentity, NoiseError, NoiseOutput};
use libp2p_tcp::{TcpConfig, TcpTransStream};
use libp2p_tcp::TcpConfig;
use log::info;
use quickcheck::QuickCheck;
use std::{convert::TryInto, io};
use std::{convert::TryInto, io, net::TcpStream};
#[allow(dead_code)]
fn core_upgrade_compat() {
@ -175,7 +176,7 @@ fn ik_xx() {
QuickCheck::new().max_tests(30).quickcheck(prop as fn(Vec<Message>) -> bool)
}
type Output<C> = (RemoteIdentity<C>, NoiseOutput<Negotiated<TcpTransStream>>);
type Output<C> = (RemoteIdentity<C>, NoiseOutput<Negotiated<Async<TcpStream>>>);
fn run<T, U, I, C>(server_transport: T, client_transport: U, messages: I)
where

View File

@ -20,7 +20,7 @@ wasm-timer = "0.2"
[dev-dependencies]
async-std = "1.6.2"
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
libp2p-noise = { path = "../../protocols/noise" }
libp2p-yamux = { path = "../../muxers/yamux" }
libp2p-mplex = { path = "../../muxers/mplex" }

View File

@ -62,21 +62,16 @@ fn ping_pong() {
let mut count2 = count.get();
let peer1 = async move {
while let Some(_) = swarm1.next().now_or_never() {}
for l in Swarm::listeners(&swarm1) {
tx.send(l.clone()).await.unwrap();
}
loop {
match swarm1.next().await {
PingEvent { peer, result: Ok(PingSuccess::Ping { rtt }) } => {
match swarm1.next_event().await {
SwarmEvent::NewListenAddr(listener) => tx.send(listener).await.unwrap(),
SwarmEvent::Behaviour(PingEvent { peer, result: Ok(PingSuccess::Ping { rtt }) }) => {
count1 -= 1;
if count1 == 0 {
return (pid1.clone(), peer, rtt)
}
},
PingEvent { result: Err(e), .. } => panic!("Ping failure: {:?}", e),
SwarmEvent::Behaviour(PingEvent { result: Err(e), .. }) => panic!("Ping failure: {:?}", e),
_ => {}
}
}
@ -132,16 +127,11 @@ fn max_failures() {
Swarm::listen_on(&mut swarm1, addr).unwrap();
let peer1 = async move {
while let Some(_) = swarm1.next().now_or_never() {}
for l in Swarm::listeners(&swarm1) {
tx.send(l.clone()).await.unwrap();
}
let mut count1: u8 = 0;
loop {
match swarm1.next_event().await {
SwarmEvent::NewListenAddr(listener) => tx.send(listener).await.unwrap(),
SwarmEvent::Behaviour(PingEvent {
result: Ok(PingSuccess::Ping { .. }), ..
}) => {

View File

@ -26,6 +26,6 @@ wasm-timer = "0.2"
[dev-dependencies]
async-std = "1.6.2"
libp2p-noise = { path = "../noise" }
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }
libp2p-yamux = { path = "../../muxers/yamux" }
rand = "0.7"

View File

@ -31,7 +31,7 @@ use libp2p_core::{
};
use libp2p_noise::{NoiseConfig, X25519Spec, Keypair};
use libp2p_request_response::*;
use libp2p_swarm::Swarm;
use libp2p_swarm::{Swarm, SwarmEvent};
use libp2p_tcp::TcpConfig;
use futures::{prelude::*, channel::mpsc, executor::LocalPool, task::SpawnExt};
use rand::{self, Rng};
@ -64,27 +64,24 @@ fn ping_protocol() {
let expected_pong = pong.clone();
let peer1 = async move {
while let Some(_) = swarm1.next().now_or_never() {}
let l = Swarm::listeners(&swarm1).next().unwrap();
tx.send(l.clone()).await.unwrap();
loop {
match swarm1.next().await {
RequestResponseEvent::Message {
match swarm1.next_event().await {
SwarmEvent::NewListenAddr(addr) => tx.send(addr).await.unwrap(),
SwarmEvent::Behaviour(RequestResponseEvent::Message {
peer,
message: RequestResponseMessage::Request { request, channel, .. }
} => {
}) => {
assert_eq!(&request, &expected_ping);
assert_eq!(&peer, &peer2_id);
swarm1.send_response(channel, pong.clone()).unwrap();
},
RequestResponseEvent::ResponseSent {
SwarmEvent::Behaviour(RequestResponseEvent::ResponseSent {
peer, ..
} => {
}) => {
assert_eq!(&peer, &peer2_id);
}
e => panic!("Peer1: Unexpected event: {:?}", e)
SwarmEvent::Behaviour(e) => panic!("Peer1: Unexpected event: {:?}", e),
_ => {}
}
}
};
@ -205,26 +202,24 @@ fn ping_protocol_throttled() {
swarm2.set_receive_limit(NonZeroU16::new(limit2).unwrap());
let peer1 = async move {
while let Some(_) = swarm1.next().now_or_never() {}
let l = Swarm::listeners(&swarm1).next().unwrap();
tx.send(l.clone()).await.unwrap();
for i in 1 .. {
match swarm1.next().await {
throttled::Event::Event(RequestResponseEvent::Message {
match swarm1.next_event().await {
SwarmEvent::NewListenAddr(addr) => tx.send(addr).await.unwrap(),
SwarmEvent::Behaviour(throttled::Event::Event(RequestResponseEvent::Message {
peer,
message: RequestResponseMessage::Request { request, channel, .. },
}) => {
})) => {
assert_eq!(&request, &expected_ping);
assert_eq!(&peer, &peer2_id);
swarm1.send_response(channel, pong.clone()).unwrap();
},
throttled::Event::Event(RequestResponseEvent::ResponseSent {
SwarmEvent::Behaviour(throttled::Event::Event(RequestResponseEvent::ResponseSent {
peer, ..
}) => {
})) => {
assert_eq!(&peer, &peer2_id);
}
e => panic!("Peer1: Unexpected event: {:?}", e)
SwarmEvent::Behaviour(e) => panic!("Peer1: Unexpected event: {:?}", e),
_ => {}
}
if i % 31 == 0 {
let lim = rand::thread_rng().gen_range(1, 17);

View File

@ -52,4 +52,4 @@ aes-all = ["aesni"]
async-std = "1.6.2"
criterion = "0.3"
libp2p-mplex = { path = "../../muxers/mplex" }
libp2p-tcp = { path = "../../transports/tcp", features = ["async-std"] }
libp2p-tcp = { path = "../../transports/tcp" }

View File

@ -74,6 +74,10 @@ where
.dial(addr)
.map(move |fut| BandwidthFuture { inner: fut, sinks })
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}
}
/// Wraps around a `Stream` that produces connections. Wraps each connection around a bandwidth

View File

@ -85,7 +85,7 @@
//! Example ([`noise`] + [`yamux`] Protocol Upgrade):
//!
//! ```rust
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "tcp-async-std", feature = "noise", feature = "yamux"))] {
//! # #[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), feature = "noise", feature = "yamux"))] {
//! use libp2p::{Transport, core::upgrade, tcp::TcpConfig, noise, identity::Keypair, yamux};
//! let tcp = TcpConfig::new();
//! let id_keys = Keypair::generate_ed25519();
@ -215,8 +215,8 @@ pub use libp2p_ping as ping;
pub use libp2p_plaintext as plaintext;
#[doc(inline)]
pub use libp2p_swarm as swarm;
#[cfg(any(feature = "tcp-async-std", feature = "tcp-tokio"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-std", feature = "tcp-tokio"))))]
#[cfg(any(feature = "tcp-async-io", feature = "tcp-tokio"))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "tcp-async-io", feature = "tcp-tokio"))))]
#[cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")))]
#[doc(inline)]
pub use libp2p_tcp as tcp;
@ -268,8 +268,8 @@ pub use self::transport_ext::TransportExt;
///
/// > **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")), any(feature = "tcp-async-std", feature = "tcp-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")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-io", feature = "tcp-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")), any(feature = "tcp-async-io", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
pub fn build_development_transport(keypair: identity::Keypair)
-> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>>
{
@ -280,13 +280,13 @@ pub fn build_development_transport(keypair: identity::Keypair)
///
/// The implementation supports TCP/IP, WebSockets over TCP/IP, noise as the encryption layer,
/// and mplex or yamux as the multiplexing layer.
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-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")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-io", feature = "tcp-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")), any(feature = "tcp-async-io", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux"))))]
pub fn build_tcp_ws_noise_mplex_yamux(keypair: identity::Keypair)
-> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>>
{
let transport = {
#[cfg(feature = "tcp-async-std")]
#[cfg(feature = "tcp-async-io")]
let tcp = tcp::TcpConfig::new().nodelay(true);
#[cfg(feature = "tcp-tokio")]
let tcp = tcp::TokioTcpConfig::new().nodelay(true);
@ -311,13 +311,13 @@ pub fn build_tcp_ws_noise_mplex_yamux(keypair: identity::Keypair)
///
/// The implementation supports TCP/IP, WebSockets over TCP/IP, noise as the encryption layer,
/// and mplex or yamux as the multiplexing layer.
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-std", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux", feature = "pnet"))))]
#[cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-io", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux", feature = "pnet"))]
#[cfg_attr(docsrs, doc(cfg(all(not(any(target_os = "emscripten", target_os = "wasi", target_os = "unknown")), any(feature = "tcp-async-io", feature = "tcp-tokio"), feature = "websocket", feature = "noise", feature = "mplex", feature = "yamux", feature = "pnet"))))]
pub fn build_tcp_ws_pnet_noise_mplex_yamux(keypair: identity::Keypair, psk: PreSharedKey)
-> std::io::Result<core::transport::Boxed<(PeerId, core::muxing::StreamMuxerBox)>>
{
let transport = {
#[cfg(feature = "tcp-async-std")]
#[cfg(feature = "tcp-async-io")]
let tcp = tcp::TcpConfig::new().nodelay(true);
#[cfg(feature = "tcp-tokio")]
let tcp = tcp::TokioTcpConfig::new().nodelay(true);

View File

@ -202,6 +202,10 @@ where
Ok(future.boxed().right_future())
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.inner.address_translation(server, observed)
}
}
/// Error that can be generated by the DNS layer.
@ -289,6 +293,10 @@ mod tests {
};
Ok(Box::pin(future::ready(Ok(()))))
}
fn address_translation(&self, _: &Multiaddr, _: &Multiaddr) -> Option<Multiaddr> {
None
}
}
futures::executor::block_on(async move {

View File

@ -1,5 +1,12 @@
# 0.27.0 [unreleased]
- Add support for port reuse and (re)add transport-specific
address translation. Thereby use only `async-io` instead of
`async-std`, renaming the feature accordingly. `async-io`
is a default feature, with an additional `tokio` feature
as before.
[PR 1887](https://github.com/libp2p/rust-libp2p/pull/1887)
- Update dependencies.
# 0.26.0 [2020-12-17]

View File

@ -10,16 +10,24 @@ keywords = ["peer-to-peer", "libp2p", "networking"]
categories = ["network-programming", "asynchronous"]
[dependencies]
async-std = { version = "1.6.5", optional = true }
futures = "0.3.1"
async-io-crate = { package = "async-io", version = "1.2.0", optional = true }
futures = "0.3.8"
futures-timer = "3.0"
if-addrs = "0.6.4"
if-watch = { version = "0.1.4", optional = true }
if-addrs = { version = "0.6.4", optional = true }
ipnet = "2.0.0"
libc = "0.2.80"
libp2p-core = { version = "0.27.0", path = "../../core" }
log = "0.4.1"
socket2 = { version = "0.3.12" }
tokio = { version = "0.3", default-features = false, features = ["net"], optional = true }
log = "0.4.11"
socket2 = { version = "0.3.17", features = ["reuseport"] }
tokio-crate = { package = "tokio", version = "0.3", default-features = false, features = ["net"], optional = true }
[features]
default = ["async-io"]
tokio = ["tokio-crate", "if-addrs"]
async-io = ["async-io-crate", "if-watch"]
[dev-dependencies]
libp2p-tcp = { path = ".", features = ["async-std"] }
async-std = { version = "1.6.5", features = ["attributes"] }
tokio-crate = { package = "tokio", version = "0.3", default-features = false, features = ["net", "rt"] }
env_logger = "0.8.2"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,81 @@
// Copyright 2020 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.
//! The interface for providers of non-blocking TCP implementations.
#[cfg(feature = "async-io")]
pub mod async_io;
#[cfg(feature = "tokio")]
pub mod tokio;
use futures::io::{AsyncRead, AsyncWrite};
use futures::future::BoxFuture;
use ipnet::IpNet;
use std::task::{Context, Poll};
use std::{fmt, io};
use std::net::{SocketAddr, TcpListener, TcpStream};
/// An event relating to a change of availability of an address
/// on a network interface.
pub enum IfEvent {
Up(IpNet),
Down(IpNet),
}
/// An incoming connection returned from [`Provider::poll_accept()`].
pub struct Incoming<S> {
pub stream: S,
pub local_addr: SocketAddr,
pub remote_addr: SocketAddr,
}
/// The interface for non-blocking TCP I/O providers.
pub trait Provider: Clone + Send + 'static {
/// The type of TCP streams obtained from [`Provider::new_stream`]
/// and [`Provider::poll_accept`].
type Stream: AsyncRead + AsyncWrite + Send + Unpin + fmt::Debug;
/// The type of TCP listeners obtained from [`Provider::new_listener`].
type Listener: Send + Unpin;
/// The type of network interface observers obtained from [`Provider::if_watcher`].
type IfWatcher: Send + Unpin;
/// Creates an instance of [`Self::IfWatcher`] that can be polled for
/// network interface changes via [`Self::poll_interfaces`].
fn if_watcher() -> BoxFuture<'static, io::Result<Self::IfWatcher>>;
/// Creates a new listener wrapping the given [`TcpListener`] that
/// can be polled for incoming connections via [`Self::poll_accept()`].
fn new_listener(_: TcpListener) -> io::Result<Self::Listener>;
/// Creates a new stream for an outgoing connection, wrapping the
/// given [`TcpStream`]. The given `TcpStream` is initiating a
/// connection, but implementations must wait for the connection
/// setup to complete, i.e. for the stream to be writable.
fn new_stream(_: TcpStream) -> BoxFuture<'static, io::Result<Self::Stream>>;
/// Polls a [`Self::Listener`] for an incoming connection, ensuring a task wakeup,
/// if necessary.
fn poll_accept(_: &mut Self::Listener, _: &mut Context<'_>) -> Poll<io::Result<Incoming<Self::Stream>>>;
/// Polls a [`Self::IfWatcher`] for network interface changes, ensuring a task wakeup,
/// if necessary.
fn poll_interfaces(_: &mut Self::IfWatcher, _: &mut Context<'_>) -> Poll<io::Result<IfEvent>>;
}

View File

@ -0,0 +1,83 @@
// Copyright 2020 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.
use super::{Provider, IfEvent, Incoming};
use async_io_crate::Async;
use futures::{
future::{BoxFuture, FutureExt},
prelude::*,
};
use std::io;
use std::task::{Poll, Context};
use std::net;
#[derive(Copy, Clone)]
pub enum Tcp {}
impl Provider for Tcp {
type Stream = Async<net::TcpStream>;
type Listener = Async<net::TcpListener>;
type IfWatcher = if_watch::IfWatcher;
fn if_watcher() -> BoxFuture<'static, io::Result<Self::IfWatcher>> {
if_watch::IfWatcher::new().boxed()
}
fn new_listener(l: net::TcpListener) -> io::Result<Self::Listener> {
Async::new(l)
}
fn new_stream(s: net::TcpStream) -> BoxFuture<'static, io::Result<Self::Stream>> {
async move {
let stream = Async::new(s)?;
stream.writable().await?;
Ok(stream)
}.boxed()
}
fn poll_accept(l: &mut Self::Listener, cx: &mut Context<'_>) -> Poll<io::Result<Incoming<Self::Stream>>> {
let (stream, remote_addr) = loop {
match l.poll_readable(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
Poll::Ready(Ok(())) => match l.accept().now_or_never() {
Some(Err(e)) => return Poll::Ready(Err(e)),
Some(Ok(res)) => break res,
None => {
// Since it doesn't do any harm, account for false positives of
// `poll_readable` just in case, i.e. try again.
}
}
}
};
let local_addr = stream.get_ref().local_addr()?;
Poll::Ready(Ok(Incoming { stream, local_addr, remote_addr }))
}
fn poll_interfaces(w: &mut Self::IfWatcher, cx: &mut Context<'_>) -> Poll<io::Result<IfEvent>> {
w.next().map_ok(|e| match e {
if_watch::IfEvent::Up(a) => IfEvent::Up(a),
if_watch::IfEvent::Down(a) => IfEvent::Down(a),
}).boxed().poll_unpin(cx)
}
}

View File

@ -0,0 +1,168 @@
// Copyright 2020 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.
use super::{Provider, IfEvent, Incoming};
use futures::{
future::{self, BoxFuture, FutureExt},
prelude::*,
};
use futures_timer::Delay;
use if_addrs::{IfAddr, get_if_addrs};
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use std::collections::HashSet;
use std::convert::TryFrom;
use std::io;
use std::task::{Poll, Context};
use std::time::Duration;
use std::net;
use std::pin::Pin;
#[derive(Copy, Clone)]
pub enum Tcp {}
pub struct IfWatcher {
addrs: HashSet<IpNet>,
delay: Delay,
pending: Vec<IfEvent>,
}
impl Provider for Tcp {
type Stream = TcpStream;
type Listener = tokio_crate::net::TcpListener;
type IfWatcher = IfWatcher;
fn if_watcher() -> BoxFuture<'static, io::Result<Self::IfWatcher>> {
future::ready(Ok(
IfWatcher {
addrs: HashSet::new(),
delay: Delay::new(Duration::from_secs(0)),
pending: Vec::new(),
}
)).boxed()
}
fn new_listener(l: net::TcpListener) -> io::Result<Self::Listener> {
tokio_crate::net::TcpListener::try_from(l)
}
fn new_stream(s: net::TcpStream) -> BoxFuture<'static, io::Result<Self::Stream>> {
async move {
let stream = tokio_crate::net::TcpStream::try_from(s)?;
stream.writable().await?;
Ok(TcpStream(stream))
}.boxed()
}
fn poll_accept(l: &mut Self::Listener, cx: &mut Context<'_>)
-> Poll<io::Result<Incoming<Self::Stream>>>
{
let (stream, remote_addr) = match l.poll_accept(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Ready(Ok((stream, remote_addr))) => (stream, remote_addr)
};
let local_addr = stream.local_addr()?;
let stream = TcpStream(stream);
Poll::Ready(Ok(Incoming { stream, local_addr, remote_addr }))
}
fn poll_interfaces(w: &mut Self::IfWatcher, cx: &mut Context<'_>) -> Poll<io::Result<IfEvent>> {
loop {
if let Some(event) = w.pending.pop() {
return Poll::Ready(Ok(event))
}
match Pin::new(&mut w.delay).poll(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(()) => {
let ifs = get_if_addrs()?;
let addrs = ifs.into_iter().map(|iface| match iface.addr {
IfAddr::V4(ip4) => {
let prefix_len = (!u32::from_be_bytes(ip4.netmask.octets())).leading_zeros();
let ipnet = Ipv4Net::new(ip4.ip, prefix_len as u8)
.expect("prefix_len can not exceed 32");
IpNet::V4(ipnet)
}
IfAddr::V6(ip6) => {
let prefix_len = (!u128::from_be_bytes(ip6.netmask.octets())).leading_zeros();
let ipnet = Ipv6Net::new(ip6.ip, prefix_len as u8)
.expect("prefix_len can not exceed 128");
IpNet::V6(ipnet)
}
}).collect::<HashSet<_>>();
for down in w.addrs.difference(&addrs) {
w.pending.push(IfEvent::Down(*down));
}
for up in addrs.difference(&w.addrs) {
w.pending.push(IfEvent::Up(*up));
}
w.addrs = addrs;
w.delay.reset(Duration::from_secs(10));
}
}
}
}
}
/// A [`tokio_crate::net::TcpStream`] that implements [`AsyncRead`] and [`AsyncWrite`].
#[derive(Debug)]
pub struct TcpStream(pub tokio_crate::net::TcpStream);
impl Into<tokio_crate::net::TcpStream> for TcpStream {
fn into(self: TcpStream) -> tokio_crate::net::TcpStream {
self.0
}
}
impl AsyncRead for TcpStream {
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<Result<usize, io::Error>> {
let mut read_buf = tokio_crate::io::ReadBuf::new(buf);
futures::ready!(tokio_crate::io::AsyncRead::poll_read(Pin::new(&mut self.0), cx, &mut read_buf))?;
Poll::Ready(Ok(read_buf.filled().len()))
}
}
impl AsyncWrite for TcpStream {
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<Result<usize, io::Error>> {
tokio_crate::io::AsyncWrite::poll_write(Pin::new(&mut self.0), cx, buf)
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
tokio_crate::io::AsyncWrite::poll_flush(Pin::new(&mut self.0), cx)
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
tokio_crate::io::AsyncWrite::poll_shutdown(Pin::new(&mut self.0), cx)
}
fn poll_write_vectored(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[io::IoSlice<'_>]
) -> Poll<io::Result<usize>> {
tokio_crate::io::AsyncWrite::poll_write_vectored(Pin::new(&mut self.0), cx, bufs)
}
}

View File

@ -109,6 +109,10 @@ impl Transport for $uds_config {
Err(TransportError::MultiaddrNotSupported(addr))
}
}
fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}
}
};

View File

@ -206,6 +206,10 @@ impl Transport for ExtTransport {
inner: SendWrapper::new(promise.into()),
})
}
fn address_translation(&self, _server: &Multiaddr, _observed: &Multiaddr) -> Option<Multiaddr> {
None
}
}
/// Future that dial a remote through an external transport.

View File

@ -24,4 +24,4 @@ webpki = "0.21"
webpki-roots = "0.21"
[dev-dependencies]
libp2p-tcp = { path = "../tcp", features = ["async-std"] }
libp2p-tcp = { path = "../tcp" }

View File

@ -262,6 +262,10 @@ where
Ok(Box::pin(future))
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}
}
impl<T> WsConfig<T>
@ -586,4 +590,3 @@ where
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))
}
}

View File

@ -113,6 +113,10 @@ where
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
self.transport.map(wrap_connection as WrapperFn<T::Output>).dial(addr)
}
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
self.transport.address_translation(server, observed)
}
}
/// Type alias corresponding to `framed::WsConfig::Listener`.