Skip to content

Commit 2a51de3

Browse files
author
“ramfox”
committed
simplify QAD addresses in NodeMap
1 parent fb4696e commit 2a51de3

File tree

8 files changed

+63
-76
lines changed

8 files changed

+63
-76
lines changed

iroh-net-report/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ tokio = { version = "1", default-features = false, features = ["test-util"] }
4949
[features]
5050
default = ["metrics"]
5151
metrics = ["iroh-metrics/metrics", "portmapper/metrics"]
52-
iroh = []
5352

5453
[package.metadata.docs.rs]
5554
all-features = true

iroh-net-report/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ mod ping;
3838
mod reportgen;
3939

4040
pub use metrics::Metrics;
41-
pub use reportgen::MappedAddr;
4241
use reportgen::ProbeProto;
4342
pub use reportgen::QuicConfig;
4443

iroh-net-report/src/reportgen.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -700,25 +700,8 @@ pub struct QuicConfig {
700700
pub ipv4: bool,
701701
/// Enable ipv6 QUIC address discovery probes
702702
pub ipv6: bool,
703-
/// A map of RelayUrls to addresses
704-
///
705-
/// Only makes sense in the context of iroh, which uses QuicMappedAddrs
706-
/// to allow dialing by NodeId
707-
#[cfg(feature = "iroh")]
708-
pub mapped_addrs: MappedRelayAddrs,
709703
}
710704

711-
/// Holds a QuicMappedAddr
712-
#[cfg(feature = "iroh")]
713-
#[derive(Debug, Clone, Copy)]
714-
pub struct MappedAddr(pub SocketAddr);
715-
716-
/// A relationship between a socket address and it's QUIC mapped address
717-
///
718-
/// Only relevant when using the net-report with iroh.
719-
#[cfg(feature = "iroh")]
720-
type MappedRelayAddrs = std::collections::HashMap<SocketAddr, MappedAddr>;
721-
722705
/// Executes a particular [`Probe`], including using a delayed start if needed.
723706
///
724707
/// If *stun_sock4* and *stun_sock6* are `None` the STUN probes are disabled.
@@ -941,13 +924,6 @@ async fn run_quic_probe(
941924
}
942925
};
943926

944-
// if we are using net-report with iroh, we must dial using the mapped address
945-
#[cfg(feature = "iroh")]
946-
let relay_addr = quic_config
947-
.mapped_addrs
948-
.get(&relay_addr)
949-
.map_or(relay_addr, |mapped_addr| mapped_addr.0.clone());
950-
951927
let quic_client = iroh_relay::quic::QuicClient::new(quic_config.ep, quic_config.client_config)
952928
.map_err(|e| ProbeError::Error(e, probe.clone()))?;
953929
let (addr, latency) = quic_client
@@ -1617,7 +1593,6 @@ mod tests {
16171593
client_config,
16181594
ipv4: true,
16191595
ipv6: true,
1620-
mapped_addrs: std::collections::HashMap::new(),
16211596
};
16221597
let url = relay.url.clone();
16231598
let port = server.quic_addr().unwrap().port();

iroh/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ webpki = { package = "rustls-webpki", version = "0.102" }
106106
webpki-roots = "0.26"
107107
x509-parser = "0.16"
108108
z32 = "1.0.3"
109-
net-report = { package = "iroh-net-report", path = "../iroh-net-report", version = "0.29", default-features = false, features = ["iroh"] }
109+
net-report = { package = "iroh-net-report", path = "../iroh-net-report", version = "0.29", default-features = false }
110110

111111
# metrics
112112
iroh-metrics = { version = "0.29", default-features = false }

iroh/examples/listen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async fn main() -> anyhow::Result<()> {
2828
// Use `RelayMode::Custom` to pass in a `RelayMap` with custom relay urls.
2929
// Use `RelayMode::Disable` to disable holepunching and relaying over HTTPS
3030
// If you want to experiment with relaying using your own relay server, you must pass in the same custom relay url to both the `listen` code AND the `connect` code
31-
.relay_mode(RelayMode::Staging)
31+
.relay_mode(RelayMode::Default)
3232
// you can choose a port to bind to, but passing in `0` will bind the socket to a random available port
3333
.bind()
3434
.await?;

iroh/src/magicsock.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ impl MagicSock {
565565
}
566566
}
567567
None => {
568-
// Check if this is a QUIC address discovery packet
569-
if let Some(addr) = self.node_map.get_qad_addr(&dest) {
568+
// Check if this is addr is used to perform QUIC Address Discovery
569+
if let Some(addr) = self.node_map.qad_addr_for_send(&dest.0) {
570570
// send udp
571571
// rewrite target address
572572
transmit.destination = addr;
@@ -828,16 +828,16 @@ impl MagicSock {
828828
// Update the NodeMap and remap RecvMeta to the QuicMappedAddr.
829829
match self.node_map.receive_udp(meta.addr) {
830830
None => {
831-
// Check if this is QUIC address discovery response
832-
if let Some(quic_mapped_addr) = self.node_map.receive_qad(meta.addr) {
831+
// Check if this address is used for QUIC address discovery
832+
if let Some(addr) = self.node_map.qad_addr_for_recv(&meta.addr) {
833833
trace!(
834834
src = ?meta.addr,
835835
count = %quic_datagram_count,
836836
len = meta.len,
837837
"UDP recv QUIC address discovery packets",
838838
);
839839
quic_packets_total += quic_datagram_count;
840-
meta.addr = quic_mapped_addr.0;
840+
meta.addr = addr;
841841
} else {
842842
warn!(
843843
src = ?meta.addr,
@@ -2049,6 +2049,9 @@ impl Actor {
20492049
}
20502050
}
20512051

2052+
// kick off resolving the URLs for relay addresses
2053+
self.resolve_qad_addrs(Duration::from_millis(10)).await;
2054+
20522055
let mut receiver_closed = false;
20532056
let mut portmap_watcher_closed = false;
20542057
let mut link_change_closed = false;
@@ -2429,8 +2432,7 @@ impl Actor {
24292432

24302433
// Need to add Quic Mapped Addrs for the relay nodes to use for
24312434
// QUIC Address Discovery
2432-
let mapped_addrs = self
2433-
.resolve_qad_addrs(std::time::Duration::from_millis(500))
2435+
self.resolve_qad_addrs(std::time::Duration::from_millis(300))
24342436
.await;
24352437
// create a client config for the endpoint to
24362438
// use for QUIC address discovery
@@ -2444,7 +2446,6 @@ impl Actor {
24442446
client_config,
24452447
ipv4: true,
24462448
ipv6: self.pconn6.is_some(),
2447-
mapped_addrs,
24482449
});
24492450
opts = opts.quic_config(quic_config);
24502451

@@ -2546,7 +2547,7 @@ impl Actor {
25462547

25472548
match relay_node.url.host() {
25482549
Some(url::Host::Domain(hostname)) => {
2549-
error!(%hostname, "Performing DNS A lookup for relay addr");
2550+
debug!(%hostname, "Performing DNS A lookup for relay addr");
25502551
let mut set = JoinSet::new();
25512552
let resolver = dns_resolver.clone();
25522553
let ipv4_resolver = resolver.clone();
@@ -2566,13 +2567,13 @@ impl Actor {
25662567
Err(_) => {}
25672568
Ok(resolved_addrs) => {
25682569
for addr in resolved_addrs {
2569-
addrs.insert(SocketAddr::new(addr.into(), port));
2570+
addrs.insert(SocketAddr::new(addr, port));
25702571
}
25712572
}
25722573
}
25732574
}
25742575
if addrs.is_empty() {
2575-
error!(%hostname, "Unable to resolve ip addresses for relay node");
2576+
debug!(%hostname, "Unable to resolve ip addresses for relay node");
25762577
}
25772578
}
25782579
Some(url::Host::Ipv4(addr)) => {
@@ -2585,15 +2586,11 @@ impl Actor {
25852586
error!(?relay_node.url, "No hostname for relay node, cannot resolve ip");
25862587
}
25872588
}
2588-
return addrs;
2589+
addrs
25892590
}
25902591

25912592
/// Resolve the relay addresses used for QUIC address discovery.
2592-
async fn resolve_qad_addrs(
2593-
&mut self,
2594-
duration: Duration,
2595-
) -> HashMap<SocketAddr, net_report::MappedAddr> {
2596-
let mut mapped_addrs = HashMap::new();
2593+
async fn resolve_qad_addrs(&mut self, duration: Duration) {
25972594
let mut set = JoinSet::new();
25982595
let resolver = self.msock.dns_resolver();
25992596
for relay_node in self.msock.relay_map.nodes() {
@@ -2606,11 +2603,9 @@ impl Actor {
26062603
let res = set.join_all().await;
26072604
for addrs in res {
26082605
addrs.iter().for_each(|addr| {
2609-
let mapped_addr = self.msock.node_map.add_qad_addr(*addr);
2610-
mapped_addrs.insert(*addr, net_report::MappedAddr(mapped_addr.0));
2606+
self.msock.node_map.add_qad_addr(*addr);
26112607
});
26122608
}
2613-
mapped_addrs
26142609
}
26152610

26162611
fn set_nearest_relay(&mut self, relay_url: Option<RelayUrl>) -> bool {

iroh/src/magicsock/node_map.rs

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ pub(super) struct NodeMapInner {
6666
by_id: HashMap<usize, NodeState>,
6767
next_id: usize,
6868
// special mapping for relay socket addresses so that we can do quic address discovery
69-
qad_mapped_addrs: HashMap<SocketAddr, QuicMappedAddr>,
69+
qad_addrs: BTreeSet<SocketAddr>,
7070
}
7171

7272
/// Identifier to look up a [`NodeState`] in the [`NodeMap`].
@@ -145,46 +145,65 @@ impl NodeMap {
145145
}
146146

147147
/// Add a the SocketAddr used to preform QUIC Address Discovery to the nodemap
148-
pub(super) fn add_qad_addr(&self, udp_addr: SocketAddr) -> QuicMappedAddr {
149-
let quic_mapped_addr = QuicMappedAddr::generate();
148+
pub(super) fn add_qad_addr(&self, udp_addr: SocketAddr) {
150149
self.inner
151150
.lock()
152151
.expect("poisoned")
153-
.qad_mapped_addrs
154-
.insert(udp_addr, quic_mapped_addr);
155-
quic_mapped_addr
152+
.qad_addrs
153+
.insert(udp_addr);
156154
}
157155

158-
/// Get the socket address used to preform QUIC Address Discovery
159-
pub(super) fn get_qad_addr(&self, addr: &QuicMappedAddr) -> Option<SocketAddr> {
160-
self.inner
156+
/// Return a correctly canonicalized SocketAddr if this address is one
157+
/// used to perform QUIC Address Discovery
158+
pub(super) fn qad_addr_for_send(&self, addr: &SocketAddr) -> Option<SocketAddr> {
159+
// all mapped addresses are Ipv6 addresses,
160+
// so we need to canonicalize before check
161+
let canonicalized_addr = SocketAddr::new(addr.ip().to_canonical(), addr.port());
162+
if self
163+
.inner
164+
.lock()
165+
.expect("poisoned")
166+
.qad_addrs
167+
.contains(&canonicalized_addr)
168+
{
169+
Some(canonicalized_addr)
170+
} else {
171+
None
172+
}
173+
}
174+
175+
/// Return a correctly formed SocketAddr if this adddress is one used to
176+
/// perform QUIC Address Discovery
177+
pub(super) fn qad_addr_for_recv(&self, addr: &SocketAddr) -> Option<SocketAddr> {
178+
if self
179+
.inner
161180
.lock()
162181
.expect("poisoned")
163-
.qad_mapped_addrs
164-
.iter()
165-
.find_map(|(udp_addr, quic_mapped_addr)| {
166-
if addr == quic_mapped_addr {
167-
Some(*udp_addr)
168-
} else {
169-
None
182+
.qad_addrs
183+
.contains(addr)
184+
{
185+
match addr.ip() {
186+
IpAddr::V4(ipv4_addr) => {
187+
// if this is an ipv4 addr, we need to map it back to
188+
// an ipv6 addr, since all addresses we use to dial on
189+
// the underlying quinn endpoint are mapped ipv6 addrs
190+
Some(SocketAddr::new(
191+
ipv4_addr.to_ipv6_mapped().into(),
192+
addr.port(),
193+
))
170194
}
171-
})
195+
IpAddr::V6(_) => Some(*addr),
196+
}
197+
} else {
198+
None
199+
}
172200
}
173201

174202
/// Number of nodes currently listed.
175203
pub(super) fn node_count(&self) -> usize {
176204
self.inner.lock().expect("poisoned").node_count()
177205
}
178206

179-
pub(super) fn receive_qad(&self, udp_addr: SocketAddr) -> Option<QuicMappedAddr> {
180-
self.inner
181-
.lock()
182-
.expect("poisoned")
183-
.qad_mapped_addrs
184-
.get(&udp_addr)
185-
.map(|addr| *addr)
186-
}
187-
188207
pub(super) fn receive_udp(&self, udp_addr: SocketAddr) -> Option<(PublicKey, QuicMappedAddr)> {
189208
self.inner.lock().expect("poisoned").receive_udp(udp_addr)
190209
}

iroh/src/magicsock/node_map/node_state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1671,7 +1671,7 @@ mod tests {
16711671
(d_endpoint.id, d_endpoint),
16721672
]),
16731673
next_id: 5,
1674-
qad_mapped_addrs: HashMap::new(),
1674+
qad_addrs: BTreeSet::new(),
16751675
});
16761676
let mut got = node_map.list_remote_infos(later);
16771677
got.sort_by_key(|p| p.node_id);

0 commit comments

Comments
 (0)