Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## 0.44.0

- Add `MultiaddrExt::is_relayed()` for checking whether a `Multiaddr` is relayed
(contains a `p2p-circuit`).
See [PR 6501](https://github.com/libp2p/rust-libp2p/pull/6501).

- Raise MSRV to 1.88.0.
See [PR 6273](https://github.com/libp2p/rust-libp2p/pull/6273).

Expand Down
8 changes: 2 additions & 6 deletions core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use crate::{
multiaddr::{Multiaddr, Protocol},
transport::PortUse,
};
use crate::{multiaddr::Multiaddr, multiaddr_ext::MultiaddrExt, transport::PortUse};

/// The endpoint roles associated with a peer-to-peer communication channel.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -135,8 +132,7 @@ impl ConnectedPoint {
ConnectedPoint::Dialer { address, .. } => address,
ConnectedPoint::Listener { local_addr, .. } => local_addr,
}
.iter()
.any(|p| p == Protocol::P2pCircuit)
.is_relayed()
}

/// Returns the address of the remote stored in this struct.
Expand Down
2 changes: 2 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub type Negotiated<T> = multistream_select::Negotiated<T>;

pub mod connection;
pub mod either;
pub mod multiaddr_ext;
pub mod muxing;
pub mod peer_record;
pub mod signed_envelope;
Expand All @@ -55,6 +56,7 @@ pub mod upgrade;
pub use connection::{ConnectedPoint, Endpoint};
pub use libp2p_identity::PeerId;
pub use multiaddr::Multiaddr;
pub use multiaddr_ext::MultiaddrExt;
pub use multihash;
pub use muxing::StreamMuxer;
pub use peer_record::PeerRecord;
Expand Down
31 changes: 31 additions & 0 deletions core/src/multiaddr_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::multiaddr::{Multiaddr, Protocol};

/// Extension trait providing convenience helpers for [`Multiaddr`].
pub trait MultiaddrExt {
/// Returns `true` if the address is relayed, i.e. it contains a
/// [`p2p-circuit`](Protocol::P2pCircuit) component.
fn is_relayed(&self) -> bool;
}

impl MultiaddrExt for Multiaddr {
fn is_relayed(&self) -> bool {
self.iter().any(|p| p == Protocol::P2pCircuit)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn direct_address_is_not_relayed() {
let addr: Multiaddr = "/ip4/127.0.0.1/tcp/1234".parse().unwrap();
assert!(!addr.is_relayed());
}

#[test]
fn circuit_address_is_relayed() {
let addr: Multiaddr = "/ip4/127.0.0.1/tcp/1234/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN/p2p-circuit/p2p/12D3KooWGzBRs8WVcMaEXGWwsvFSfJKYphwM6ojrPanuJfup1xFz".parse().unwrap();
assert!(addr.is_relayed());
}
}
13 changes: 5 additions & 8 deletions protocols/dcutr/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ use std::{
use either::Either;
use hashlink::LruCache;
use libp2p_core::{
Endpoint, Multiaddr, connection::ConnectedPoint, multiaddr::Protocol, transport::PortUse,
Endpoint, Multiaddr, MultiaddrExt, connection::ConnectedPoint, multiaddr::Protocol,
transport::PortUse,
};
use libp2p_identity::PeerId;
use libp2p_swarm::{
Expand Down Expand Up @@ -177,7 +178,7 @@ impl NetworkBehaviour for Behaviour {
local_addr: &Multiaddr,
remote_addr: &Multiaddr,
) -> Result<THandler<Self>, ConnectionDenied> {
if is_relayed(local_addr) {
if local_addr.is_relayed() {
let connected_point = ConnectedPoint::Listener {
local_addr: local_addr.clone(),
send_back_addr: remote_addr.clone(),
Expand Down Expand Up @@ -212,7 +213,7 @@ impl NetworkBehaviour for Behaviour {
role_override: Endpoint,
port_use: PortUse,
) -> Result<THandler<Self>, ConnectionDenied> {
if is_relayed(addr) {
if addr.is_relayed() {
return Ok(Either::Left(handler::relayed::Handler::new(
ConnectedPoint::Dialer {
address: addr.clone(),
Expand Down Expand Up @@ -367,7 +368,7 @@ impl Candidates {
}

fn add(&mut self, mut address: Multiaddr) {
if is_relayed(&address) {
if address.is_relayed() {
return;
}

Expand All @@ -382,7 +383,3 @@ impl Candidates {
self.inner.iter().map(|(a, _)| a)
}
}

fn is_relayed(addr: &Multiaddr) -> bool {
addr.iter().any(|p| p == Protocol::P2pCircuit)
}
4 changes: 2 additions & 2 deletions protocols/dcutr/src/protocol/inbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::io;

use asynchronous_codec::Framed;
use futures::prelude::*;
use libp2p_core::{Multiaddr, multiaddr::Protocol};
use libp2p_core::{Multiaddr, MultiaddrExt};
use libp2p_swarm::Stream;
use thiserror::Error;

Expand Down Expand Up @@ -57,7 +57,7 @@ pub(crate) async fn handshake(
})
// Filter out relayed addresses.
.filter(|a| {
if a.iter().any(|p| p == Protocol::P2pCircuit) {
if a.is_relayed() {
tracing::debug!(address=%a, "Dropping relayed address");
false
} else {
Expand Down
4 changes: 2 additions & 2 deletions protocols/dcutr/src/protocol/outbound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::io;
use asynchronous_codec::Framed;
use futures::prelude::*;
use futures_timer::Delay;
use libp2p_core::{Multiaddr, multiaddr::Protocol};
use libp2p_core::{Multiaddr, MultiaddrExt};
use libp2p_swarm::Stream;
use thiserror::Error;
use web_time::Instant;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub(crate) async fn handshake(
})
// Filter out relayed addresses.
.filter(|a| {
if a.iter().any(|p| p == Protocol::P2pCircuit) {
if a.is_relayed() {
tracing::debug!(address=%a, "Dropping relayed address");
false
} else {
Expand Down
5 changes: 3 additions & 2 deletions protocols/relay/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ use std::{
};

use either::Either;
use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr, multiaddr::Protocol, transport::PortUse};
use libp2p_core::{
ConnectedPoint, Endpoint, Multiaddr, MultiaddrExt, multiaddr::Protocol, transport::PortUse,
};
use libp2p_identity::PeerId;
use libp2p_swarm::{
ConnectionClosed, ConnectionDenied, ConnectionId, ExternalAddresses, FromSwarm,
Expand All @@ -42,7 +44,6 @@ use web_time::Instant;

use crate::{
behaviour::handler::Handler,
multiaddr_ext::MultiaddrExt,
proto,
protocol::{inbound_hop, outbound_stop},
};
Expand Down
1 change: 0 additions & 1 deletion protocols/relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

mod behaviour;
mod copy_future;
mod multiaddr_ext;
mod priv_client;
mod protocol;

Expand Down
11 changes: 0 additions & 11 deletions protocols/relay/src/multiaddr_ext.rs

This file was deleted.

3 changes: 1 addition & 2 deletions protocols/relay/src/priv_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use futures::{
stream::StreamExt,
};
use libp2p_core::{
Endpoint, Multiaddr,
Endpoint, Multiaddr, MultiaddrExt,
multiaddr::Protocol,
transport::{ListenerId, PortUse},
};
Expand All @@ -56,7 +56,6 @@ use libp2p_swarm::{
use transport::Transport;

use crate::{
multiaddr_ext::MultiaddrExt,
priv_client::handler::Handler,
protocol::{self, inbound_stop},
};
Expand Down
2 changes: 1 addition & 1 deletion protocols/relay/src/priv_client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use futures::{
stream::{SelectAll, Stream, StreamExt},
};
use libp2p_core::{
MultiaddrExt,
multiaddr::{Multiaddr, Protocol},
transport::{DialOpts, ListenerId, TransportError, TransportEvent},
};
Expand All @@ -40,7 +41,6 @@ use thiserror::Error;

use crate::{
RequestId,
multiaddr_ext::MultiaddrExt,
priv_client::Connection,
protocol::{
outbound_hop,
Expand Down
7 changes: 7 additions & 0 deletions protocols/request-response/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## 0.30.0

- Add `Config::with_relay_for_requests` to control whether outbound requests may be sent over
relayed connections (enabled by default). When disabled and only a relayed connection to the peer
is available, the request is kept queued so a direct connection established shortly after (e.g. via
DCUtR) can carry it, and fails with the new `OutboundFailure::NoDirectConnection` variant if the
peer fully disconnects first.
See [PR 6501](https://github.com/libp2p/rust-libp2p/pull/6501).

- Use `futures-timer` instead of tokio's timer for stream timeouts so the bounded `Delay` works on
`wasm32`; tokio's timer has no driver in the browser and panics at runtime.
See [PR 6488](https://github.com/libp2p/rust-libp2p/pull/6488).
Expand Down
6 changes: 6 additions & 0 deletions protocols/request-response/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ cbor = ["dep:serde", "dep:cbor4ii", "libp2p-swarm/macros"]
anyhow = "1.0.102"
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "time"] }
rand = { workspace = true }
libp2p-identify = { workspace = true }
libp2p-plaintext = { workspace = true }
libp2p-relay = { workspace = true }
libp2p-swarm = { workspace = true, features = ["macros"] }
libp2p-swarm-test = { path = "../../swarm-test" }
libp2p-tcp = { workspace = true, features = ["tokio"] }
libp2p-yamux = { workspace = true }
futures_ringbuf = "0.4.0"
serde = { version = "1.0", features = ["derive"] }
tracing-subscriber = { workspace = true, features = ["env-filter"] }
Expand Down
Loading