Skip to content

Commit 92a1b95

Browse files
authored
fix(dcutr): Skip unparsable multiaddr (#3300)
With this commit `libp2p-dcutr` no longer discards the whole remote payload in case an addr is unparsable, but instead logs the failure and skips the unparsable multiaddr. See #3244 for details.
1 parent 773c370 commit 92a1b95

File tree

4 files changed

+41
-14
lines changed

4 files changed

+41
-14
lines changed

protocols/dcutr/CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# 0.8.1
2+
3+
- Skip unparsable multiaddr in `InboundUpgrade::upgrade_inbound` and
4+
`OutboundUpgrade::upgrade_outbound`. See [PR 3300].
5+
6+
[PR 3300]: https://github.com/libp2p/rust-libp2p/pull/3300
7+
18
# 0.8.0
29

310
- Update to `prost-codec` `v0.3.0`.

protocols/dcutr/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "libp2p-dcutr"
33
edition = "2021"
44
rust-version = "1.62.0"
55
description = "Direct connection upgrade through relay"
6-
version = "0.8.0"
6+
version = "0.8.1"
77
authors = ["Max Inden <[email protected]>"]
88
license = "MIT"
99
repository = "https://github.com/libp2p/rust-libp2p"
@@ -34,7 +34,7 @@ libp2p = { path = "../..", features = ["full"] }
3434
rand = "0.8"
3535
clap = { version = "4.0.13", features = ["derive"] }
3636

37-
# Passing arguments to the docsrs builder in order to properly document cfg's.
37+
# Passing arguments to the docsrs builder in order to properly document cfg's.
3838
# More information: https://docs.rs/about/builds#cross-compiling
3939
[package.metadata.docs.rs]
4040
all-features = true

protocols/dcutr/src/protocol/inbound.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,23 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
5858
} else {
5959
obs_addrs
6060
.into_iter()
61-
.map(Multiaddr::try_from)
61+
.filter_map(|a| match Multiaddr::try_from(a) {
62+
Ok(a) => Some(a),
63+
Err(e) => {
64+
log::debug!("Unable to parse multiaddr: {e}");
65+
None
66+
}
67+
})
6268
// Filter out relayed addresses.
63-
.filter(|a| match a {
64-
Ok(a) => !a.iter().any(|p| p == Protocol::P2pCircuit),
65-
Err(_) => true,
69+
.filter(|a| {
70+
if a.iter().any(|p| p == Protocol::P2pCircuit) {
71+
log::debug!("Dropping relayed address {a}");
72+
false
73+
} else {
74+
true
75+
}
6676
})
67-
.collect::<Result<Vec<Multiaddr>, _>>()
68-
.map_err(|_| UpgradeError::InvalidAddrs)?
77+
.collect::<Vec<Multiaddr>>()
6978
};
7079

7180
let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?;
@@ -124,6 +133,7 @@ pub enum UpgradeError {
124133
StreamClosed,
125134
#[error("Expected at least one address in reservation.")]
126135
NoAddresses,
136+
#[deprecated(since = "0.8.1", note = "Error is no longer constructed.")]
127137
#[error("Invalid addresses.")]
128138
InvalidAddrs,
129139
#[error("Failed to parse response type field.")]

protocols/dcutr/src/protocol/outbound.rs

+16-6
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,23 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
8585
} else {
8686
obs_addrs
8787
.into_iter()
88-
.map(Multiaddr::try_from)
88+
.filter_map(|a| match Multiaddr::try_from(a) {
89+
Ok(a) => Some(a),
90+
Err(e) => {
91+
log::debug!("Unable to parse multiaddr: {e}");
92+
None
93+
}
94+
})
8995
// Filter out relayed addresses.
90-
.filter(|a| match a {
91-
Ok(a) => !a.iter().any(|p| p == Protocol::P2pCircuit),
92-
Err(_) => true,
96+
.filter(|a| {
97+
if a.iter().any(|p| p == Protocol::P2pCircuit) {
98+
log::debug!("Dropping relayed address {a}");
99+
false
100+
} else {
101+
true
102+
}
93103
})
94-
.collect::<Result<Vec<Multiaddr>, _>>()
95-
.map_err(|_| UpgradeError::InvalidAddrs)?
104+
.collect::<Vec<Multiaddr>>()
96105
};
97106

98107
let msg = HolePunch {
@@ -128,6 +137,7 @@ pub enum UpgradeError {
128137
NoAddresses,
129138
#[error("Invalid expiration timestamp in reservation.")]
130139
InvalidReservationExpiration,
140+
#[deprecated(since = "0.8.1", note = "Error is no longer constructed.")]
131141
#[error("Invalid addresses in reservation.")]
132142
InvalidAddrs,
133143
#[error("Failed to parse response type field.")]

0 commit comments

Comments
 (0)