Skip to content

Commit

Permalink
refactor(iroh-net): Switch to (now stable) IpAddr::to_canonical
Browse files Browse the repository at this point in the history
  • Loading branch information
matheus23 committed Jul 31, 2024
1 parent f085e63 commit 331c670
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 30 deletions.
4 changes: 2 additions & 2 deletions iroh-net/src/disco.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 1 addition & 19 deletions iroh-net/src/net/ip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions iroh-net/src/netcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions iroh-net/src/netcheck/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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")),
Expand All @@ -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")),
Expand Down
6 changes: 2 additions & 4 deletions iroh-net/src/stun.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
_ => {}
Expand Down

0 comments on commit 331c670

Please sign in to comment.