Skip to content

Commit

Permalink
Merge pull request #3 from umgefahren/autonat-v2-transport-changes
Browse files Browse the repository at this point in the history
Autonat v2 transport changes
  • Loading branch information
umgefahren authored May 14, 2024
2 parents 0dc5003 + 45863c3 commit a38d35f
Show file tree
Hide file tree
Showing 55 changed files with 1,270 additions and 948 deletions.
1,395 changes: 750 additions & 645 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ libp2p = { version = "0.54.0", path = "libp2p" }
libp2p-allow-block-list = { version = "0.3.0", path = "misc/allow-block-list" }
libp2p-autonat = { version = "0.12.0", path = "protocols/autonat" }
libp2p-connection-limits = { version = "0.3.1", path = "misc/connection-limits" }
libp2p-core = { version = "0.41.2", path = "core" }
libp2p-core = { version = "0.42.0", path = "core" }
libp2p-dcutr = { version = "0.11.0", path = "protocols/dcutr" }
libp2p-dns = { version = "0.41.1", path = "transports/dns" }
libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" }
Expand All @@ -89,7 +89,6 @@ libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.14.1", path = "misc/metrics" }
libp2p-mplex = { version = "0.41.0", path = "muxers/mplex" }
libp2p-muxer-test-harness = { path = "muxers/test-harness" }
libp2p-noise = { version = "0.44.0", path = "transports/noise" }
libp2p-perf = { version = "0.3.0", path = "protocols/perf" }
libp2p-ping = { version = "0.44.1", path = "protocols/ping" }
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-core"
edition = "2021"
rust-version = { workspace = true }
description = "Core traits and structs of libp2p"
version = "0.41.2"
version = "0.42.0"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
2 changes: 1 addition & 1 deletion core/src/transport/and_then.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use crate::{
connection::ConnectedPoint,
transport::{ListenerId, Transport, TransportError, TransportEvent, DialOpts},
transport::{DialOpts, ListenerId, Transport, TransportError, TransportEvent},
};
use either::Either;
use futures::prelude::*;
Expand Down
4 changes: 2 additions & 2 deletions core/src/transport/global_only.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ impl<T: crate::Transport + Unpin> crate::Transport for Transport<T> {
match addr.iter().next() {
Some(Protocol::Ip4(a)) => {
if !ipv4_global::is_global(a) {
tracing::debug!(ip=?a, "Not dialing non global IP address");
tracing::debug!(ip=%a, "Not dialing non global IP address");
return Err(TransportError::MultiaddrNotSupported(addr));
}
self.inner.dial(addr, opts)
}
Some(Protocol::Ip6(a)) => {
if !ipv6_global::is_global(a) {
tracing::debug!(ip=?a, "Not dialing non global IP address");
tracing::debug!(ip=%a, "Not dialing non global IP address");
return Err(TransportError::MultiaddrNotSupported(addr));
}
self.inner.dial(addr, opts)
Expand Down
2 changes: 1 addition & 1 deletion core/src/upgrade/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct ReadyUpgrade<P> {
}

impl<P> ReadyUpgrade<P> {
pub fn new(protocol_name: P) -> Self {
pub const fn new(protocol_name: P) -> Self {
Self { protocol_name }
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/autonat/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "autonat-example"
version = "0.1.0"
edition = "2021"
publish = false
license = "MIT"
license = "MIT or Apache-2.0"

[package.metadata.release]
release = false
Expand Down
1 change: 1 addition & 0 deletions examples/autonat/src/bin/autonat_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE.

#![doc = include_str!("../../README.md")]
#![allow(deprecated)]

use clap::Parser;
use futures::StreamExt;
Expand Down
1 change: 1 addition & 0 deletions examples/autonat/src/bin/autonat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// DEALINGS IN THE SOFTWARE.

#![doc = include_str!("../../README.md")]
#![allow(deprecated)]

use clap::Parser;
use futures::StreamExt;
Expand Down
2 changes: 1 addition & 1 deletion examples/dcutr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
libp2p::SwarmBuilder::with_existing_identity(generate_ed25519(opts.secret_key_seed))
.with_tokio()
.with_tcp(
tcp::Config::default().port_reuse(true).nodelay(true),
tcp::Config::default().nodelay(true),
noise::Config::new,
yamux::Config::default,
)?
Expand Down
2 changes: 1 addition & 1 deletion hole-punching-tests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ async fn main() -> Result<()> {
let mut swarm = libp2p::SwarmBuilder::with_new_identity()
.with_tokio()
.with_tcp(
tcp::Config::new().port_reuse(true).nodelay(true),
tcp::Config::new().nodelay(true),
noise::Config::new,
yamux::Config::default,
)?
Expand Down
2 changes: 2 additions & 0 deletions misc/allow-block-list/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
//! # }
//! ```
use libp2p_core::transport::PortUse;
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
Expand Down Expand Up @@ -225,6 +226,7 @@ where
peer: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
self.state.enforce(&peer)?;

Expand Down
4 changes: 3 additions & 1 deletion misc/connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr};
use libp2p_core::{transport::PortUse, ConnectedPoint, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
behaviour::{ConnectionEstablished, DialFailure, ListenFailure},
Expand Down Expand Up @@ -278,6 +278,7 @@ impl NetworkBehaviour for Behaviour {
peer: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
self.pending_outbound_connections.remove(&connection_id);

Expand Down Expand Up @@ -569,6 +570,7 @@ mod tests {
_peer: PeerId,
_addr: &Multiaddr,
_role_override: Endpoint,
_port_use: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
Err(ConnectionDenied::new(std::io::Error::new(
std::io::ErrorKind::Other,
Expand Down
3 changes: 2 additions & 1 deletion misc/memory-connection-limits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use libp2p_core::{Endpoint, Multiaddr};
use libp2p_core::{transport::PortUse, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent,
Expand Down Expand Up @@ -174,6 +174,7 @@ impl NetworkBehaviour for Behaviour {
_: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(dummy::ConnectionHandler)
}
Expand Down
3 changes: 2 additions & 1 deletion misc/memory-connection-limits/tests/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

use std::task::{Context, Poll};

use libp2p_core::{Endpoint, Multiaddr};
use libp2p_core::{transport::PortUse, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::{
dummy, ConnectionDenied, ConnectionId, FromSwarm, NetworkBehaviour, THandler, THandlerInEvent,
Expand Down Expand Up @@ -102,6 +102,7 @@ impl<const MEM_PENDING: usize, const MEM_ESTABLISHED: usize> NetworkBehaviour
_: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
self.handle_established();
Ok(dummy::ConnectionHandler)
Expand Down
17 changes: 4 additions & 13 deletions misc/metrics/src/bandwidth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use futures::{
};
use libp2p_core::{
muxing::{StreamMuxer, StreamMuxerEvent},
transport::{ListenerId, TransportError, TransportEvent},
transport::{DialOpts, ListenerId, TransportError, TransportEvent},
Multiaddr,
};
use libp2p_identity::PeerId;
Expand Down Expand Up @@ -84,24 +84,15 @@ where
self.transport.remove_listener(id)
}

fn dial(&mut self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
let metrics = ConnectionMetrics::from_family_and_addr(&self.metrics, &addr);
Ok(self
.transport
.dial(addr.clone())?
.map_ok(Box::new(|(peer_id, stream_muxer)| {
(peer_id, Muxer::new(stream_muxer, metrics))
})))
}

fn dial_as_listener(
fn dial(
&mut self,
addr: Multiaddr,
dial_opts: DialOpts,
) -> Result<Self::Dial, TransportError<Self::Error>> {
let metrics = ConnectionMetrics::from_family_and_addr(&self.metrics, &addr);
Ok(self
.transport
.dial_as_listener(addr.clone())?
.dial(addr.clone(), dial_opts)?
.map_ok(Box::new(|(peer_id, stream_muxer)| {
(peer_id, Muxer::new(stream_muxer, metrics))
})))
Expand Down
2 changes: 1 addition & 1 deletion misc/server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut swarm = libp2p::SwarmBuilder::with_existing_identity(local_keypair)
.with_tokio()
.with_tcp(
tcp::Config::default().port_reuse(true).nodelay(true),
tcp::Config::default().nodelay(true),
noise::Config::new,
yamux::Config::default,
)?
Expand Down
13 changes: 12 additions & 1 deletion muxers/mplex/benches/split_send_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use futures::prelude::*;
use futures::{channel::oneshot, future::join};
use libp2p_core::muxing::StreamMuxerExt;
use libp2p_core::transport::ListenerId;
use libp2p_core::Endpoint;
use libp2p_core::{multiaddr::multiaddr, muxing, transport, upgrade, Multiaddr, Transport};
use libp2p_identity as identity;
use libp2p_identity::PeerId;
Expand Down Expand Up @@ -146,7 +147,17 @@ fn run(
// Spawn and block on the sender, i.e. until all data is sent.
let sender = async move {
let addr = addr_receiver.await.unwrap();
let (_peer, mut conn) = sender_trans.dial(addr).unwrap().await.unwrap();
let (_peer, mut conn) = sender_trans
.dial(
addr,
transport::DialOpts {
role: Endpoint::Dialer,
port_use: transport::PortUse::Reuse,
},
)
.unwrap()
.await
.unwrap();
// Just calling `poll_outbound` without `poll` is fine here because mplex makes progress through all `poll_` functions. It is hacky though.
let mut stream = poll_fn(|cx| conn.poll_outbound_unpin(cx)).await.unwrap();
let mut off = 0;
Expand Down
13 changes: 11 additions & 2 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use as_server::AsServer;
pub use as_server::{InboundProbeError, InboundProbeEvent};
use futures_timer::Delay;
use instant::Instant;
use libp2p_core::transport::PortUse;
use libp2p_core::{multiaddr::Protocol, ConnectedPoint, Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_request_response::{
Expand Down Expand Up @@ -338,6 +339,7 @@ impl Behaviour {
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
..
} => {
if let Some(event) = self.as_server().on_outbound_connection(&peer, address) {
self.pending_actions
Expand All @@ -347,6 +349,7 @@ impl Behaviour {
ConnectedPoint::Dialer {
address: _,
role_override: Endpoint::Listener,
..
} => {
// Outgoing connection was dialed as a listener. In other words outgoing connection
// was dialed as part of a hole punch. `libp2p-autonat` never attempts to hole
Expand Down Expand Up @@ -512,9 +515,15 @@ impl NetworkBehaviour for Behaviour {
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
port_use: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
self.inner
.handle_established_outbound_connection(connection_id, peer, addr, role_override)
self.inner.handle_established_outbound_connection(
connection_id,
peer,
addr,
role_override,
port_use,
)
}

fn on_swarm_event(&mut self, event: FromSwarm) {
Expand Down
1 change: 1 addition & 0 deletions protocols/autonat/src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
NonZeroU8::new(1).expect("1 > 0"),
)
.addresses(addrs)
.allocate_new_port()
.build(),
},
])
Expand Down
2 changes: 2 additions & 0 deletions protocols/autonat/tests/test_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async fn test_dial_back() {
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
..
},
num_established,
concurrent_dial_errors,
Expand Down Expand Up @@ -300,6 +301,7 @@ async fn test_dial_multiple_addr() {
ConnectedPoint::Dialer {
address,
role_override: Endpoint::Dialer,
..
},
concurrent_dial_errors,
..
Expand Down
3 changes: 3 additions & 0 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use crate::{handler, protocol};
use either::Either;
use libp2p_core::connection::ConnectedPoint;
use libp2p_core::multiaddr::Protocol;
use libp2p_core::transport::PortUse;
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, DialFailure, FromSwarm};
Expand Down Expand Up @@ -206,12 +207,14 @@ impl NetworkBehaviour for Behaviour {
peer: PeerId,
addr: &Multiaddr,
role_override: Endpoint,
port_use: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
if is_relayed(addr) {
return Ok(Either::Left(handler::relayed::Handler::new(
ConnectedPoint::Dialer {
address: addr.clone(),
role_override,
port_use,
},
self.observed_addresses(),
))); // TODO: We could make two `handler::relayed::Handler` here, one inbound one outbound.
Expand Down
2 changes: 2 additions & 0 deletions protocols/floodsub/src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::FloodsubConfig;
use bytes::Bytes;
use cuckoofilter::{CuckooError, CuckooFilter};
use fnv::FnvHashSet;
use libp2p_core::transport::PortUse;
use libp2p_core::{Endpoint, Multiaddr};
use libp2p_identity::PeerId;
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, FromSwarm};
Expand Down Expand Up @@ -346,6 +347,7 @@ impl NetworkBehaviour for Floodsub {
_: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(Default::default())
}
Expand Down
5 changes: 4 additions & 1 deletion protocols/gossipsub/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use prometheus_client::registry::Registry;
use rand::{seq::SliceRandom, thread_rng};

use instant::Instant;
use libp2p_core::{multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, Endpoint, Multiaddr};
use libp2p_core::{
multiaddr::Protocol::Ip4, multiaddr::Protocol::Ip6, transport::PortUse, Endpoint, Multiaddr,
};
use libp2p_identity::Keypair;
use libp2p_identity::PeerId;
use libp2p_swarm::{
Expand Down Expand Up @@ -3011,6 +3013,7 @@ where
_: PeerId,
_: &Multiaddr,
_: Endpoint,
_: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
Ok(Handler::new(self.config.protocol_config()))
}
Expand Down
Loading

0 comments on commit a38d35f

Please sign in to comment.