Skip to content

Commit e7249ec

Browse files
committed
feat(request-response): configurable use of relayed connections
Add a config flag controlling whether outbound requests may be sent over relay connections. Defaults to enabled (current behaviour); when disabled, only direct connections are used, conserving relay bandwidth. Promote is_relayed helper to core.
1 parent a714395 commit e7249ec

17 files changed

Lines changed: 569 additions & 40 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
## 0.44.0
22

3+
- Add `MultiaddrExt::is_relayed()` for checking whether a `Multiaddr` is relayed
4+
(contains a `p2p-circuit`).
5+
See [PR 6501](https://github.com/libp2p/rust-libp2p/pull/6501).
6+
37
- Raise MSRV to 1.88.0.
48
See [PR 6273](https://github.com/libp2p/rust-libp2p/pull/6273).
59

core/src/connection.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::{
22-
multiaddr::{Multiaddr, Protocol},
23-
transport::PortUse,
24-
};
21+
use crate::{multiaddr::Multiaddr, multiaddr_ext::MultiaddrExt, transport::PortUse};
2522

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

142138
/// Returns the address of the remote stored in this struct.

core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ pub type Negotiated<T> = multistream_select::Negotiated<T>;
4646

4747
pub mod connection;
4848
pub mod either;
49+
pub mod multiaddr_ext;
4950
pub mod muxing;
5051
pub mod peer_record;
5152
pub mod signed_envelope;
@@ -55,6 +56,7 @@ pub mod upgrade;
5556
pub use connection::{ConnectedPoint, Endpoint};
5657
pub use libp2p_identity::PeerId;
5758
pub use multiaddr::Multiaddr;
59+
pub use multiaddr_ext::MultiaddrExt;
5860
pub use multihash;
5961
pub use muxing::StreamMuxer;
6062
pub use peer_record::PeerRecord;

core/src/multiaddr_ext.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate::multiaddr::{Multiaddr, Protocol};
2+
3+
/// Extension trait providing convenience helpers for [`Multiaddr`].
4+
pub trait MultiaddrExt {
5+
/// Returns `true` if the address is relayed, i.e. it contains a
6+
/// [`p2p-circuit`](Protocol::P2pCircuit) component.
7+
fn is_relayed(&self) -> bool;
8+
}
9+
10+
impl MultiaddrExt for Multiaddr {
11+
fn is_relayed(&self) -> bool {
12+
self.iter().any(|p| p == Protocol::P2pCircuit)
13+
}
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn direct_address_is_not_relayed() {
22+
let addr: Multiaddr = "/ip4/127.0.0.1/tcp/1234".parse().unwrap();
23+
assert!(!addr.is_relayed());
24+
}
25+
26+
#[test]
27+
fn circuit_address_is_relayed() {
28+
let addr: Multiaddr = "/ip4/127.0.0.1/tcp/1234/p2p/12D3KooWDpJ7As7BWAwRMfu1VU2WCqNjvq387JEYKDBj4kx6nXTN/p2p-circuit/p2p/12D3KooWGzBRs8WVcMaEXGWwsvFSfJKYphwM6ojrPanuJfup1xFz".parse().unwrap();
29+
assert!(addr.is_relayed());
30+
}
31+
}

protocols/dcutr/src/behaviour.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ use std::{
2929
use either::Either;
3030
use hashlink::LruCache;
3131
use libp2p_core::{
32-
Endpoint, Multiaddr, connection::ConnectedPoint, multiaddr::Protocol, transport::PortUse,
32+
Endpoint, Multiaddr, MultiaddrExt, connection::ConnectedPoint, multiaddr::Protocol,
33+
transport::PortUse,
3334
};
3435
use libp2p_identity::PeerId;
3536
use libp2p_swarm::{
@@ -177,7 +178,7 @@ impl NetworkBehaviour for Behaviour {
177178
local_addr: &Multiaddr,
178179
remote_addr: &Multiaddr,
179180
) -> Result<THandler<Self>, ConnectionDenied> {
180-
if is_relayed(local_addr) {
181+
if local_addr.is_relayed() {
181182
let connected_point = ConnectedPoint::Listener {
182183
local_addr: local_addr.clone(),
183184
send_back_addr: remote_addr.clone(),
@@ -212,7 +213,7 @@ impl NetworkBehaviour for Behaviour {
212213
role_override: Endpoint,
213214
port_use: PortUse,
214215
) -> Result<THandler<Self>, ConnectionDenied> {
215-
if is_relayed(addr) {
216+
if addr.is_relayed() {
216217
return Ok(Either::Left(handler::relayed::Handler::new(
217218
ConnectedPoint::Dialer {
218219
address: addr.clone(),
@@ -367,7 +368,7 @@ impl Candidates {
367368
}
368369

369370
fn add(&mut self, mut address: Multiaddr) {
370-
if is_relayed(&address) {
371+
if address.is_relayed() {
371372
return;
372373
}
373374

@@ -382,7 +383,3 @@ impl Candidates {
382383
self.inner.iter().map(|(a, _)| a)
383384
}
384385
}
385-
386-
fn is_relayed(addr: &Multiaddr) -> bool {
387-
addr.iter().any(|p| p == Protocol::P2pCircuit)
388-
}

protocols/dcutr/src/protocol/inbound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::io;
2222

2323
use asynchronous_codec::Framed;
2424
use futures::prelude::*;
25-
use libp2p_core::{Multiaddr, multiaddr::Protocol};
25+
use libp2p_core::{Multiaddr, MultiaddrExt};
2626
use libp2p_swarm::Stream;
2727
use thiserror::Error;
2828

@@ -57,7 +57,7 @@ pub(crate) async fn handshake(
5757
})
5858
// Filter out relayed addresses.
5959
.filter(|a| {
60-
if a.iter().any(|p| p == Protocol::P2pCircuit) {
60+
if a.is_relayed() {
6161
tracing::debug!(address=%a, "Dropping relayed address");
6262
false
6363
} else {

protocols/dcutr/src/protocol/outbound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::io;
2323
use asynchronous_codec::Framed;
2424
use futures::prelude::*;
2525
use futures_timer::Delay;
26-
use libp2p_core::{Multiaddr, multiaddr::Protocol};
26+
use libp2p_core::{Multiaddr, MultiaddrExt};
2727
use libp2p_swarm::Stream;
2828
use thiserror::Error;
2929
use web_time::Instant;
@@ -74,7 +74,7 @@ pub(crate) async fn handshake(
7474
})
7575
// Filter out relayed addresses.
7676
.filter(|a| {
77-
if a.iter().any(|p| p == Protocol::P2pCircuit) {
77+
if a.is_relayed() {
7878
tracing::debug!(address=%a, "Dropping relayed address");
7979
false
8080
} else {

protocols/relay/src/behaviour.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ use std::{
3131
};
3232

3333
use either::Either;
34-
use libp2p_core::{ConnectedPoint, Endpoint, Multiaddr, multiaddr::Protocol, transport::PortUse};
34+
use libp2p_core::{
35+
ConnectedPoint, Endpoint, Multiaddr, MultiaddrExt, multiaddr::Protocol, transport::PortUse,
36+
};
3537
use libp2p_identity::PeerId;
3638
use libp2p_swarm::{
3739
ConnectionClosed, ConnectionDenied, ConnectionId, ExternalAddresses, FromSwarm,
@@ -42,7 +44,6 @@ use web_time::Instant;
4244

4345
use crate::{
4446
behaviour::handler::Handler,
45-
multiaddr_ext::MultiaddrExt,
4647
proto,
4748
protocol::{inbound_hop, outbound_stop},
4849
};

protocols/relay/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
mod behaviour;
2727
mod copy_future;
28-
mod multiaddr_ext;
2928
mod priv_client;
3029
mod protocol;
3130

0 commit comments

Comments
 (0)