From 331c670ca263d2e0560b284166b0855c4c41d557 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philipp=20Kr=C3=BCger?= Date: Wed, 31 Jul 2024 10:08:17 +0200 Subject: [PATCH] refactor(iroh-net): Switch to (now stable) `IpAddr::to_canonical` --- iroh-net/src/disco.rs | 4 ++-- iroh-net/src/net/ip.rs | 20 +------------------- iroh-net/src/netcheck.rs | 3 +-- iroh-net/src/netcheck/reportgen.rs | 5 ++--- iroh-net/src/stun.rs | 6 ++---- 5 files changed, 8 insertions(+), 30 deletions(-) diff --git a/iroh-net/src/disco.rs b/iroh-net/src/disco.rs index 3914c6e89c..9e95eeeea4 100644 --- a/iroh-net/src/disco.rs +++ b/iroh-net/src/disco.rs @@ -27,7 +27,7 @@ use anyhow::{anyhow, bail, ensure, Context, Result}; use serde::{Deserialize, Serialize}; use url::Url; -use crate::{key, net::ip::to_canonical, relay::RelayUrl}; +use crate::{key, relay::RelayUrl}; use super::{key::PublicKey, stun}; @@ -269,7 +269,7 @@ fn socket_addr_from_bytes(p: [u8; EP_LENGTH]) -> SocketAddr { let raw_src_ip: [u8; 16] = p[..16].try_into().expect("array long enough"); let raw_port: [u8; 2] = p[16..].try_into().expect("array long enough"); - let src_ip = to_canonical(IpAddr::from(raw_src_ip)); + let src_ip = IpAddr::from(raw_src_ip).to_canonical(); let src_port = u16::from_le_bytes(raw_port); SocketAddr::new(src_ip, src_port) diff --git a/iroh-net/src/net/ip.rs b/iroh-net/src/net/ip.rs index 78bdeabfbf..180fcd908e 100644 --- a/iroh-net/src/net/ip.rs +++ b/iroh-net/src/net/ip.rs @@ -46,7 +46,7 @@ impl LocalAddresses { .chain(iface.ipv6.iter().map(|a| IpAddr::V6(a.addr))); for ip in addrs { - let ip = to_canonical(ip); + let ip = ip.to_canonical(); if ip.is_loopback() || ifc_is_loopback { loopback.push(ip); @@ -128,24 +128,6 @@ pub(super) fn is_link_local(ip: IpAddr) -> bool { } } -/// Converts IPv4-mappend IPv6 addresses to IPv4. -/// -/// Converts this address to an [`IpAddr::V4`] if it is an IPv4-mapped IPv6 addresses, -/// otherwise it return self as-is. -// TODO: replace with IpAddr::to_canonical once stabilized. -pub fn to_canonical(ip: IpAddr) -> IpAddr { - match ip { - ip @ IpAddr::V4(_) => ip, - IpAddr::V6(ip) => { - if let Some(ip) = ip.to_ipv4_mapped() { - IpAddr::V4(ip) - } else { - IpAddr::V6(ip) - } - } - } -} - /// Returns true if the address is a unicast address with link-local scope, as defined in RFC 4291. // Copied from std lib, not stable yet pub const fn is_unicast_link_local(addr: Ipv6Addr) -> bool { diff --git a/iroh-net/src/netcheck.rs b/iroh-net/src/netcheck.rs index e8e731f0a0..59890fbf96 100644 --- a/iroh-net/src/netcheck.rs +++ b/iroh-net/src/netcheck.rs @@ -20,7 +20,6 @@ use tokio_util::sync::CancellationToken; use tracing::{debug, error, info_span, trace, warn, Instrument}; use crate::dns::DnsResolver; -use crate::net::ip::to_canonical; use crate::net::{IpFamily, UdpSocket}; use crate::relay::RelayUrl; use crate::util::CancelOnDrop; @@ -764,7 +763,7 @@ async fn recv_stun_once(sock: &UdpSocket, buf: &mut [u8], actor_addr: &Addr) -> .await .context("Error reading from stun socket")?; let payload = &buf[..count]; - from_addr.set_ip(to_canonical(from_addr.ip())); + from_addr.set_ip(from_addr.ip().to_canonical()); let msg = Message::StunPacket { payload: Bytes::from(payload.to_vec()), from_addr, diff --git a/iroh-net/src/netcheck/reportgen.rs b/iroh-net/src/netcheck/reportgen.rs index 0bdd77ee43..d29055b774 100644 --- a/iroh-net/src/netcheck/reportgen.rs +++ b/iroh-net/src/netcheck/reportgen.rs @@ -34,7 +34,6 @@ use super::NetcheckMetrics; use crate::defaults::DEFAULT_STUN_PORT; use crate::dns::{DnsResolver, ResolverExt}; use crate::net::interfaces; -use crate::net::ip; use crate::net::UdpSocket; use crate::netcheck::{self, Report}; use crate::ping::{PingError, Pinger}; @@ -953,7 +952,7 @@ async fn get_relay_addr( { Ok(mut addrs) => addrs .next() - .map(ip::to_canonical) + .map(|ip| ip.to_canonical()) .map(|addr| SocketAddr::new(addr, port)) .ok_or(anyhow!("No suitable relay addr found")), Err(err) => Err(err.context("No suitable relay addr found")), @@ -973,7 +972,7 @@ async fn get_relay_addr( { Ok(mut addrs) => addrs .next() - .map(ip::to_canonical) + .map(|ip| ip.to_canonical()) .map(|addr| SocketAddr::new(addr, port)) .ok_or(anyhow!("No suitable relay addr found")), Err(err) => Err(err.context("No suitable relay addr found")), diff --git a/iroh-net/src/stun.rs b/iroh-net/src/stun.rs index e0ed936782..73cdea566f 100644 --- a/iroh-net/src/stun.rs +++ b/iroh-net/src/stun.rs @@ -11,8 +11,6 @@ pub use stun_rs::{ TransactionId, }; -use crate::net::ip::to_canonical; - /// Errors that can occur when handling a STUN packet. #[derive(Debug, thiserror::Error)] pub enum Error { @@ -126,12 +124,12 @@ pub fn parse_response(b: &[u8]) -> Result<(TransactionId, SocketAddr), Error> { match attr { StunAttribute::XorMappedAddress(a) => { let mut a = *a.socket_address(); - a.set_ip(to_canonical(a.ip())); + a.set_ip(a.ip().to_canonical()); addr = Some(a); } StunAttribute::MappedAddress(a) => { let mut a = *a.socket_address(); - a.set_ip(to_canonical(a.ip())); + a.set_ip(a.ip().to_canonical()); fallback_addr = Some(a); } _ => {}