Skip to content

Commit

Permalink
docs clean up, renaming IpMappedAddrs to IpMappedAddresses
Browse files Browse the repository at this point in the history
  • Loading branch information
“ramfox” committed Jan 31, 2025
1 parent 0ee3607 commit 1a9c773
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 41 deletions.
33 changes: 13 additions & 20 deletions iroh-net-report/src/ip_mapped_addrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ pub const MAPPED_ADDR_PORT: u16 = 12345;
#[error("Failed to convert")]
pub struct IpMappedAddrError;

/// A mirror for the `NodeIdMappedAddr`, mapping a fake Ipv6 address with an actual IP address.
/// A map fake Ipv6 address with an actual IP address.
///
/// You can consider this as nothing more than a lookup key for an IP that iroh's magicsocket knows
/// about.
///
/// And in our QUIC-facing socket APIs like iroh's `AsyncUdpSocket` it
/// comes in as the inn his type.
/// It is essentially a lookup key for an IP that iroh's magicsocket knows about.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Ord, PartialOrd)]
pub struct IpMappedAddr(Ipv6Addr);

Expand All @@ -38,7 +34,7 @@ impl IpMappedAddr {

/// Generates a globally unique fake UDP address.
///
/// This generates a new IPv6 address in the Unique Local Address range (RFC 4193)
/// This generates a new IPv6 address in the Unique Local Address range (RFC 4193)
/// which is recognised by iroh as an IP mapped address.
pub fn generate() -> Self {
let mut addr = [0u8; 16];
Expand Down Expand Up @@ -79,29 +75,32 @@ impl std::fmt::Display for IpMappedAddr {
}
}

/// A Map of [`IpMappedAddrs`] to [`SocketAddr`].
/// A Map of [`IpMappedAddresses`] to [`SocketAddr`].
// TODO(ramfox): before this is ready to be used beyond QAD, we should add
// mechanisms for keeping track of "aliveness" and pruning address, as we do
// with the `NodeMap`
#[derive(Debug, Clone)]
pub struct IpMappedAddrs(Arc<std::sync::Mutex<Inner>>);
#[derive(Debug, Clone, Default)]
pub struct IpMappedAddresses(Arc<std::sync::Mutex<Inner>>);

#[derive(Debug, Default)]
pub struct Inner {
by_mapped_addr: BTreeMap<IpMappedAddr, SocketAddr>,
by_socket_addr: BTreeMap<SocketAddr, IpMappedAddr>,
}

impl IpMappedAddrs {
/// Creates an empty [`IpMappedAddrs`].
impl IpMappedAddresses {
/// Creates an empty [`IpMappedAddresses`].
pub fn new() -> Self {
Self(Arc::new(std::sync::Mutex::new(Inner::default())))
}

/// Adds a [`SocketAddr`] to the map and returns the generated [`IpMappedAddr`].
///
/// If this [`SocketAddr`] already exists in the map, it returns its associated [`IpMappedAddr`]. Otherwise a new [`IpMappedAddr`] is generated for it and returned.
pub fn add(&self, ip_addr: SocketAddr) -> IpMappedAddr {
/// If this [`SocketAddr`] already exists in the map, it returns its
/// associated [`IpMappedAddr`].
///
/// Otherwise a new [`IpMappedAddr`] is generated for it and returned.
pub fn get_or_register(&self, ip_addr: SocketAddr) -> IpMappedAddr {
let mut inner = self.0.lock().expect("poisoned");
if let Some(mapped_addr) = inner.by_socket_addr.get(&ip_addr) {
return *mapped_addr;
Expand All @@ -124,9 +123,3 @@ impl IpMappedAddrs {
inner.by_mapped_addr.get(mapped_addr).copied()
}
}

impl Default for IpMappedAddrs {
fn default() -> Self {
IpMappedAddrs::new()
}
}
10 changes: 5 additions & 5 deletions iroh-net-report/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod metrics;
mod ping;
mod reportgen;

pub use ip_mapped_addrs::{IpMappedAddr, IpMappedAddrError, IpMappedAddrs, MAPPED_ADDR_PORT};
pub use ip_mapped_addrs::{IpMappedAddr, IpMappedAddrError, IpMappedAddresses, MAPPED_ADDR_PORT};
pub use metrics::Metrics;
use reportgen::ProbeProto;
pub use reportgen::QuicConfig;
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Client {
pub fn new(
port_mapper: Option<portmapper::Client>,
dns_resolver: DnsResolver,
ip_mapped_addrs: Option<IpMappedAddrs>,
ip_mapped_addrs: Option<IpMappedAddresses>,
) -> Result<Self> {
let mut actor = Actor::new(port_mapper, dns_resolver, ip_mapped_addrs)?;
let addr = actor.addr();
Expand Down Expand Up @@ -573,8 +573,8 @@ struct Actor {
/// The DNS resolver to use for probes that need to perform DNS lookups
dns_resolver: DnsResolver,

/// The [`IpMappedAddrs`] that allows you to do QAD in iroh
ip_mapped_addrs: Option<IpMappedAddrs>,
/// The [`IpMappedAddresses`] that allows you to do QAD in iroh
ip_mapped_addrs: Option<IpMappedAddresses>,
}

impl Actor {
Expand All @@ -585,7 +585,7 @@ impl Actor {
fn new(
port_mapper: Option<portmapper::Client>,
dns_resolver: DnsResolver,
ip_mapped_addrs: Option<IpMappedAddrs>,
ip_mapped_addrs: Option<IpMappedAddresses>,
) -> Result<Self> {
// TODO: consider an instrumented flume channel so we have metrics.
let (sender, receiver) = mpsc::channel(32);
Expand Down
21 changes: 12 additions & 9 deletions iroh-net-report/src/reportgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use crate::Metrics;
use crate::{
self as net_report,
dns::ResolverExt,
ip_mapped_addrs::IpMappedAddrs,
ip_mapped_addrs::IpMappedAddresses,
ping::{PingError, Pinger},
Report,
};
Expand Down Expand Up @@ -93,7 +93,7 @@ impl Client {
quic_config: Option<QuicConfig>,
dns_resolver: DnsResolver,
protocols: BTreeSet<ProbeProto>,
ip_mapped_addrs: Option<IpMappedAddrs>,
ip_mapped_addrs: Option<IpMappedAddresses>,
) -> Self {
let (msg_tx, msg_rx) = mpsc::channel(32);
let addr = Addr {
Expand Down Expand Up @@ -200,8 +200,8 @@ struct Actor {
/// Protocols we should attempt to create probes for, if we have the correct
/// configuration for that protocol.
protocols: BTreeSet<ProbeProto>,
/// Optional [`IpMappedAddrs`] used to enable QAD in iroh
ip_mapped_addrs: Option<IpMappedAddrs>,
/// Optional [`IpMappedAddresses`] used to enable QAD in iroh
ip_mapped_addrs: Option<IpMappedAddresses>,
}

impl Actor {
Expand Down Expand Up @@ -720,7 +720,7 @@ async fn run_probe(
net_report: net_report::Addr,
pinger: Pinger,
dns_resolver: DnsResolver,
ip_mapped_addrs: Option<IpMappedAddrs>,
ip_mapped_addrs: Option<IpMappedAddresses>,
) -> Result<ProbeReport, ProbeError> {
if !probe.delay().is_zero() {
trace!("delaying probe");
Expand Down Expand Up @@ -908,9 +908,12 @@ async fn run_stun_probe(
}
}

fn maybe_to_mapped_addr(ip_mapped_addrs: Option<IpMappedAddrs>, addr: SocketAddr) -> SocketAddr {
fn maybe_to_mapped_addr(
ip_mapped_addrs: Option<IpMappedAddresses>,
addr: SocketAddr,
) -> SocketAddr {
if let Some(ip_mapped_addrs) = ip_mapped_addrs.as_ref() {
return ip_mapped_addrs.add(addr).socket_addr();
return ip_mapped_addrs.get_or_register(addr).socket_addr();
}
addr
}
Expand All @@ -921,7 +924,7 @@ async fn run_quic_probe(
url: RelayUrl,
relay_addr: SocketAddr,
probe: Probe,
ip_mapped_addrs: Option<IpMappedAddrs>,
ip_mapped_addrs: Option<IpMappedAddresses>,
) -> Result<ProbeReport, ProbeError> {
match probe.proto() {
ProbeProto::QuicIpv4 => debug_assert!(relay_addr.is_ipv4()),
Expand Down Expand Up @@ -1070,7 +1073,7 @@ fn get_port(relay_node: &RelayNode, proto: &ProbeProto) -> Result<u16> {
/// different results. Obviously IPv4 vs IPv6 but a [`RelayNode`] may also have disabled
/// some protocols.
///
/// If the protocol is `QuicIpv4` or `QuicIpv6`, and `IpMappedAddrs` is not `None`, we
/// If the protocol is `QuicIpv4` or `QuicIpv6`, and `IpMappedAddresses` is not `None`, we
/// assume that we are running this net report with `iroh`, and need to provide mapped
/// addresses to the probe in order for it to function in the specialize iroh-quinn
/// endpoint that expects mapped addresses.
Expand Down
4 changes: 0 additions & 4 deletions iroh-relay/src/quic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ pub(crate) mod server {
}
};
debug!("established");
tracing::warn!("QAD SERVER - CONN ACCEPTED");
// wait for the client to close the connection
let connection_err = connection.closed().await;
tracing::warn!("QAD SERVER - CONN CLOSED");
match connection_err {
quinn::ConnectionError::ApplicationClosed(ApplicationClose { error_code, .. })
if error_code == QUIC_ADDR_DISC_CLOSE_CODE =>
Expand Down Expand Up @@ -251,7 +249,6 @@ impl QuicClient {
// Ok((addr, latency))
// }

tracing::warn!("QAD CLIENT: WAIT FOR ADDRESS");
let res = match external_addresses.wait_for(|addr| addr.is_some()).await {
Ok(res) => res,
Err(err) => {
Expand All @@ -267,7 +264,6 @@ impl QuicClient {
let latency = conn.rtt() / 2;
// gracefully close the connections
conn.close(QUIC_ADDR_DISC_CLOSE_CODE, QUIC_ADDR_DISC_CLOSE_REASON);
tracing::warn!("QAD CLIENT: GRACEFULLY CLOSED");
Ok((observed_addr, latency))
}
}
Expand Down
6 changes: 3 additions & 3 deletions iroh/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use n0_future::{
time::{Duration, Instant},
FutureExt, StreamExt,
};
use net_report::{IpMappedAddr, IpMappedAddrs, QuicConfig, MAPPED_ADDR_PORT};
use net_report::{IpMappedAddr, IpMappedAddresses, QuicConfig, MAPPED_ADDR_PORT};
use netwatch::{interfaces, ip::LocalAddresses, netmon, UdpSocket};
use quinn::{AsyncUdpSocket, ServerConfig};
use rand::{seq::SliceRandom, Rng, SeedableRng};
Expand Down Expand Up @@ -244,7 +244,7 @@ pub(crate) struct MagicSock {
/// Tracks the networkmap node entity for each node discovery key.
node_map: NodeMap,
/// Tracks the mapped IP addresses
ip_mapped_addrs: IpMappedAddrs,
ip_mapped_addrs: IpMappedAddresses,
/// UDP IPv4 socket
pconn4: UdpConn,
/// UDP IPv6 socket
Expand Down Expand Up @@ -1643,7 +1643,7 @@ impl Handle {
let ipv4_addr = pconn4.local_addr()?;
let ipv6_addr = pconn6.as_ref().and_then(|c| c.local_addr().ok());

let ip_mapped_addrs = IpMappedAddrs::default();
let ip_mapped_addrs = IpMappedAddresses::default();

let net_reporter = net_report::Client::new(
Some(port_mapper.clone()),
Expand Down

0 comments on commit 1a9c773

Please sign in to comment.