Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(identify): use function call to validate Multiaddr #5898

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 9 additions & 4 deletions protocols/identify/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ fn is_quic_addr(addr: &Multiaddr, v1: bool) -> bool {
&& fifth.is_none()
}

/// Whether an [`Multiaddr`] is a valid for the TCP transport.
fn is_tcp_addr(addr: &Multiaddr) -> bool {
use Protocol::*;

Expand All @@ -85,6 +86,13 @@ fn is_tcp_addr(addr: &Multiaddr) -> bool {
matches!(first, Ip4(_) | Ip6(_) | Dns(_) | Dns4(_) | Dns6(_)) && matches!(second, Tcp(_))
}

/// Whether the server, and observed [`Multiaddr`] are equivalent transports.
fn is_valid_connection(server: &Multiaddr, observed: &Multiaddr) -> bool {
(is_tcp_addr(server) && is_tcp_addr(observed))
|| (is_quic_addr(server, true) && is_quic_addr(observed, true))
|| (is_quic_addr(server, false) && is_quic_addr(observed, false))
}

/// Network behaviour that automatically identifies nodes periodically, returns information
/// about them, and answers identify queries from other nodes.
///
Expand Down Expand Up @@ -329,10 +337,7 @@ impl Behaviour {
.listen_addresses
.iter()
.filter_map(|server| {
if (is_tcp_addr(server) && is_tcp_addr(observed))
|| (is_quic_addr(server, true) && is_quic_addr(observed, true))
|| (is_quic_addr(server, false) && is_quic_addr(observed, false))
{
if is_valid_connection(server, observed) {
_address_translation(server, observed)
} else {
None
Expand Down