Skip to content

Commit 172025e

Browse files
committed
refactor(identify): use function call is_valid_connection to validate multiaddr
1 parent 8c8b3c3 commit 172025e

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

protocols/identify/src/behaviour.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ fn is_quic_addr(addr: &Multiaddr, v1: bool) -> bool {
6868
&& fifth.is_none()
6969
}
7070

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

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

89+
/// Whether the server, and observed [`Multiaddr`] are equivalent transports.
90+
fn is_valid_connection(server: &Multiaddr, observed: &Multiaddr) -> bool {
91+
(is_tcp_addr(server) && is_tcp_addr(observed))
92+
|| (is_quic_addr(server, true) && is_quic_addr(observed, true))
93+
|| (is_quic_addr(server, false) && is_quic_addr(observed, false))
94+
}
95+
8896
/// Network behaviour that automatically identifies nodes periodically, returns information
8997
/// about them, and answers identify queries from other nodes.
9098
///
@@ -329,10 +337,7 @@ impl Behaviour {
329337
.listen_addresses
330338
.iter()
331339
.filter_map(|server| {
332-
if (is_tcp_addr(server) && is_tcp_addr(observed))
333-
|| (is_quic_addr(server, true) && is_quic_addr(observed, true))
334-
|| (is_quic_addr(server, false) && is_quic_addr(observed, false))
335-
{
340+
if is_valid_connection(server, observed) {
336341
_address_translation(server, observed)
337342
} else {
338343
None

0 commit comments

Comments
 (0)