mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-09 17:46:52 +00:00
`Transport::listen_on` is an asynchronous operation. It returns immediately but the actual process of establishing a listening socket happens as part of `Transport::poll` which will return one or more `TransportEvent`s related to a particular `listen_on` call. Currently, `listen_on` returns a `ListenerId` which allows the user of the `Transport` interface to correlate the events with a particular `listen_on` call. This "user" is the `Swarm` runtime. Currently, a user of libp2p establishes a new listening socket by talking to the `Swarm::listen_on` interface and it is not possible to do the same thing via the `NetworkBehaviour` trait. Within the `NetworkBehaviour` trait, we emit _commands_ to the `Swarm` like `ToSwarm::Dial`. These commands don't have a "return value" like a synchronous function does and thus, if we were to add a `ToSwarm::ListenOn` command, it could not receive the `ListenerId` from the `Transport`. To fix this and to be consistent with our [coding guidelines](https://github.com/libp2p/rust-libp2p/blob/master/docs/coding-guidelines.md#allow-correlating-asynchronous-responses-to-their-requests) we change the interface of `Transport::listen_on` to require the user to pass in a `ListenerId`. This will allow us to construct a command in a `NetworkBehaviour` that remembers this ID which enables precise tracking of which events containing a `ListenerId` correlate which a particular `listen_on` command. This is especially important in the context of listening on wildcard addresses like `0.0.0.0` because we end up binding to multiple network interfaces and thus emit multiple events for a single `listen_on` call. Pull-Request: #3567.
129 lines
4.4 KiB
Rust
129 lines
4.4 KiB
Rust
// Copyright 2019 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 futures::prelude::*;
|
|
use libp2p_core::transport::{ListenerId, MemoryTransport, Transport};
|
|
use libp2p_core::upgrade::{self, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
|
use libp2p_identity as identity;
|
|
use libp2p_mplex::MplexConfig;
|
|
use libp2p_noise as noise;
|
|
use multiaddr::{Multiaddr, Protocol};
|
|
use rand::random;
|
|
use std::{io, pin::Pin};
|
|
|
|
#[derive(Clone)]
|
|
struct HelloUpgrade {}
|
|
|
|
impl UpgradeInfo for HelloUpgrade {
|
|
type Info = &'static str;
|
|
type InfoIter = std::iter::Once<Self::Info>;
|
|
|
|
fn protocol_info(&self) -> Self::InfoIter {
|
|
std::iter::once("/hello/1")
|
|
}
|
|
}
|
|
|
|
impl<C> InboundUpgrade<C> for HelloUpgrade
|
|
where
|
|
C: AsyncRead + AsyncWrite + Send + Unpin + 'static,
|
|
{
|
|
type Output = C;
|
|
type Error = io::Error;
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
|
|
|
|
fn upgrade_inbound(self, mut socket: C, _: Self::Info) -> Self::Future {
|
|
Box::pin(async move {
|
|
let mut buf = [0u8; 5];
|
|
socket.read_exact(&mut buf).await.unwrap();
|
|
assert_eq!(&buf[..], "hello".as_bytes());
|
|
Ok(socket)
|
|
})
|
|
}
|
|
}
|
|
|
|
impl<C> OutboundUpgrade<C> for HelloUpgrade
|
|
where
|
|
C: AsyncWrite + AsyncRead + Send + Unpin + 'static,
|
|
{
|
|
type Output = C;
|
|
type Error = io::Error;
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>>;
|
|
|
|
fn upgrade_outbound(self, mut socket: C, _: Self::Info) -> Self::Future {
|
|
Box::pin(async move {
|
|
socket.write_all(b"hello").await.unwrap();
|
|
socket.flush().await.unwrap();
|
|
Ok(socket)
|
|
})
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn upgrade_pipeline() {
|
|
let listener_keys = identity::Keypair::generate_ed25519();
|
|
let listener_id = listener_keys.public().to_peer_id();
|
|
let mut listener_transport = MemoryTransport::default()
|
|
.upgrade(upgrade::Version::V1)
|
|
.authenticate(noise::Config::new(&listener_keys).unwrap())
|
|
.apply(HelloUpgrade {})
|
|
.apply(HelloUpgrade {})
|
|
.apply(HelloUpgrade {})
|
|
.multiplex(MplexConfig::default())
|
|
.boxed();
|
|
|
|
let dialer_keys = identity::Keypair::generate_ed25519();
|
|
let dialer_id = dialer_keys.public().to_peer_id();
|
|
let mut dialer_transport = MemoryTransport::default()
|
|
.upgrade(upgrade::Version::V1)
|
|
.authenticate(noise::Config::new(&dialer_keys).unwrap())
|
|
.apply(HelloUpgrade {})
|
|
.apply(HelloUpgrade {})
|
|
.apply(HelloUpgrade {})
|
|
.multiplex(MplexConfig::default())
|
|
.boxed();
|
|
|
|
let listen_addr1 = Multiaddr::from(Protocol::Memory(random::<u64>()));
|
|
let listen_addr2 = listen_addr1.clone();
|
|
|
|
listener_transport
|
|
.listen_on(ListenerId::next(), listen_addr1)
|
|
.unwrap();
|
|
|
|
let server = async move {
|
|
loop {
|
|
let (upgrade, _send_back_addr) =
|
|
match listener_transport.select_next_some().await.into_incoming() {
|
|
Some(u) => u,
|
|
None => continue,
|
|
};
|
|
let (peer, _mplex) = upgrade.await.unwrap();
|
|
assert_eq!(peer, dialer_id);
|
|
}
|
|
};
|
|
|
|
let client = async move {
|
|
let (peer, _mplex) = dialer_transport.dial(listen_addr2).unwrap().await.unwrap();
|
|
assert_eq!(peer, listener_id);
|
|
};
|
|
|
|
async_std::task::spawn(server);
|
|
async_std::task::block_on(client);
|
|
}
|