Skip to content

Commit

Permalink
refactor: use DNS resolver from MagicEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Frando committed Mar 25, 2024
1 parent c3e8f0a commit f798084
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
25 changes: 4 additions & 21 deletions iroh-net/src/discovery/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
};
use anyhow::Result;
use futures::{future::FutureExt, stream::BoxStream, StreamExt};
use hickory_resolver::TokioAsyncResolver;

use crate::dns;

Expand All @@ -29,25 +28,12 @@ pub const N0_TESTDNS_NODE_ORIGIN: &str = "testdns.iroh.link";
#[derive(Debug)]
pub struct DnsDiscovery {
node_origin: String,
resolver: Option<TokioAsyncResolver>,
}

impl DnsDiscovery {
/// Create a new DNS discovery with `node_origin` appended to all lookups.
pub fn new(node_origin: String) -> Self {
Self {
node_origin,
resolver: None,
}
}

/// Create a new DNS discovery with `node_origin` appended to all lookups, and a custom DNS
/// resolver instance
pub fn with_resolver(resolver: TokioAsyncResolver, node_origin: String) -> Self {
Self {
node_origin,
resolver: Some(resolver),
}
Self { node_origin }
}

/// Create a new DNS discovery which uses the n0 testdns origin.
Expand All @@ -59,16 +45,13 @@ impl DnsDiscovery {
impl Discovery for DnsDiscovery {
fn resolve(
&self,
_ep: MagicEndpoint,
ep: MagicEndpoint,
node_id: NodeId,
) -> Option<BoxStream<'_, Result<DiscoveryItem>>> {
let resolver = ep.dns_resolver().clone();
let fut = async move {
let resolver = match &self.resolver {
Some(resolver) => resolver,
None => dns::resolver(),
};
let node_addr =
dns::node_info::lookup_by_id(resolver, &node_id, &self.node_origin).await?;
dns::node_info::lookup_by_id(&resolver, &node_id, &self.node_origin).await?;
Ok(DiscoveryItem {
provenance: "iroh-dns",
last_updated: None,
Expand Down
5 changes: 5 additions & 0 deletions iroh-net/src/magic_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ impl MagicEndpoint {
Ok(())
}

/// Get a reference to the DNS resolver used in this [`MagicEndpoint`].
pub fn dns_resolver(&self) -> &DnsResolver {
&self.msock.dns_resolver()
}

/// Close the QUIC endpoint and the magic socket.
///
/// This will close all open QUIC connections with the provided error_code and reason. See
Expand Down
5 changes: 5 additions & 0 deletions iroh-net/src/magicsock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,11 @@ impl MagicSock {
self.inner.node_map.add_node_addr(addr);
}

/// Get a reference to the DNS resolver used in this [`MagicSock`].
pub fn dns_resolver(&self) -> &DnsResolver {
&self.inner.dns_resolver
}

/// Closes the connection.
///
/// Only the first close does anything. Any later closes return nil.
Expand Down

0 comments on commit f798084

Please sign in to comment.