fix(dcutr): Skip unparsable multiaddr (#3320)

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 https://github.com/libp2p/rust-libp2p/issues/3244 for details.
This commit is contained in:
Max Inden 2023-01-12 09:46:31 +01:00 committed by GitHub
parent 7665e74cdb
commit 3cc824796d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 14 deletions

View File

@ -16,6 +16,13 @@
[issue 2217]: https://github.com/libp2p/rust-libp2p/issues/2217 [issue 2217]: https://github.com/libp2p/rust-libp2p/issues/2217
[PR 3214]: https://github.com/libp2p/rust-libp2p/pull/3214 [PR 3214]: https://github.com/libp2p/rust-libp2p/pull/3214
# 0.8.1
- Skip unparsable multiaddr in `InboundUpgrade::upgrade_inbound` and
`OutboundUpgrade::upgrade_outbound`. See [PR 3300].
[PR 3300]: https://github.com/libp2p/rust-libp2p/pull/3300
# 0.8.0 # 0.8.0
- Update to `prost-codec` `v0.3.0`. - Update to `prost-codec` `v0.3.0`.

View File

@ -58,14 +58,23 @@ impl upgrade::InboundUpgrade<NegotiatedSubstream> for Upgrade {
} else { } else {
obs_addrs obs_addrs
.into_iter() .into_iter()
.map(Multiaddr::try_from) .filter_map(|a| match Multiaddr::try_from(a) {
// Filter out relayed addresses. Ok(a) => Some(a),
.filter(|a| match a { Err(e) => {
Ok(a) => !a.iter().any(|p| p == Protocol::P2pCircuit), log::debug!("Unable to parse multiaddr: {e}");
Err(_) => true, None
}
}) })
.collect::<Result<Vec<Multiaddr>, _>>() // Filter out relayed addresses.
.map_err(|_| UpgradeError::InvalidAddrs)? .filter(|a| {
if a.iter().any(|p| p == Protocol::P2pCircuit) {
log::debug!("Dropping relayed address {a}");
false
} else {
true
}
})
.collect::<Vec<Multiaddr>>()
}; };
let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?; let r#type = hole_punch::Type::from_i32(r#type).ok_or(UpgradeError::ParseTypeField)?;
@ -124,6 +133,7 @@ pub enum UpgradeError {
StreamClosed, StreamClosed,
#[error("Expected at least one address in reservation.")] #[error("Expected at least one address in reservation.")]
NoAddresses, NoAddresses,
#[deprecated(since = "0.8.1", note = "Error is no longer constructed.")]
#[error("Invalid addresses.")] #[error("Invalid addresses.")]
InvalidAddrs, InvalidAddrs,
#[error("Failed to parse response type field.")] #[error("Failed to parse response type field.")]

View File

@ -85,14 +85,23 @@ impl upgrade::OutboundUpgrade<NegotiatedSubstream> for Upgrade {
} else { } else {
obs_addrs obs_addrs
.into_iter() .into_iter()
.map(Multiaddr::try_from) .filter_map(|a| match Multiaddr::try_from(a) {
// Filter out relayed addresses. Ok(a) => Some(a),
.filter(|a| match a { Err(e) => {
Ok(a) => !a.iter().any(|p| p == Protocol::P2pCircuit), log::debug!("Unable to parse multiaddr: {e}");
Err(_) => true, None
}
}) })
.collect::<Result<Vec<Multiaddr>, _>>() // Filter out relayed addresses.
.map_err(|_| UpgradeError::InvalidAddrs)? .filter(|a| {
if a.iter().any(|p| p == Protocol::P2pCircuit) {
log::debug!("Dropping relayed address {a}");
false
} else {
true
}
})
.collect::<Vec<Multiaddr>>()
}; };
let msg = HolePunch { let msg = HolePunch {
@ -128,6 +137,7 @@ pub enum UpgradeError {
NoAddresses, NoAddresses,
#[error("Invalid expiration timestamp in reservation.")] #[error("Invalid expiration timestamp in reservation.")]
InvalidReservationExpiration, InvalidReservationExpiration,
#[deprecated(since = "0.8.1", note = "Error is no longer constructed.")]
#[error("Invalid addresses in reservation.")] #[error("Invalid addresses in reservation.")]
InvalidAddrs, InvalidAddrs,
#[error("Failed to parse response type field.")] #[error("Failed to parse response type field.")]