DnsResolver

Struct DnsResolver 

Source
pub struct DnsResolver { /* private fields */ }
Expand description

The DNS resolver used throughout iroh.

By default, we use a built-in resolver that reads the system’s DNS configuration. The nameservers can be customized by constructing the resolver with Self::builder. Alternatively, you can create a fully custom DNS resolver by implementing the Resolver trait and creating the resolver with Self::custom.

§Usage on Android

The system-defaults reader uses JNI through ndk_context, which must be initialized with a JavaVM and Application context before the resolver is constructed. Glue crates like ndk-glue and android-activity do this before main. Apps that don’t use either should call install_android_jni_context once at startup, see docs there for details.

If ndk_context is not initialized, fetching the system config on Android will fail and the resolver will use the fallback nameservers instead, subject to the configured FallbackMode. Due to how things are implemented in ndk_context, detecting the failure relies on unwinding a panic. If your app uses panic = "abort" in its compilation profile, this doesn’t work, so in that case your app will panic if no JNI context is initialized. Therefore, either make sure that the JNI context is installed, or don’t use panic = "abort".

Implementations§

Source§

impl DnsResolver

Source

pub fn new() -> Self

Creates a new DNS resolver with sensible cross-platform defaults.

Reads the host system’s DNS configuration, with the default public resolvers behind it as a fallback tier. Those are queried only when the system configuration cannot be read, which happens on some Android versions, or when its nameservers do not answer.

Source

pub fn with_nameserver(nameserver: SocketAddr) -> Self

Creates a new DNS resolver configured with a single UDP DNS nameserver.

Source

pub fn builder() -> Builder

Creates a builder to construct a DNS resolver with custom options.

Source

pub fn custom(resolver: impl Resolver) -> Self

Creates a new DnsResolver from a struct that implements Resolver.

If you need more customization for DNS resolving than the Builder allows, you can implement the Resolver trait on a struct and implement DNS resolution however you see fit.

Source

pub fn clear_cache(&self)

Removes all entries from the cache.

Source

pub fn reset(&self)

Replaces the inner resolver with a freshly-built one.

Call this on a major host network change to pick up the new system DNS configuration and rebind sockets. The swap is atomic and non-blocking; in-flight lookups retry against the new resolver. See Resolver::reset.

Source

pub async fn lookup_txt<T: ToString>( &self, host: T, timeout: Duration, ) -> Result<impl Iterator<Item = TxtRecordData>, DnsError>

Looks up a TXT record.

Source

pub async fn lookup_ipv4<T: ToString>( &self, host: T, timeout: Duration, ) -> Result<impl Iterator<Item = IpAddr> + use<T>, DnsError>

Performs an IPv4 lookup with a timeout.

Source

pub async fn lookup_ipv6<T: ToString>( &self, host: T, timeout: Duration, ) -> Result<impl Iterator<Item = IpAddr> + use<T>, DnsError>

Performs an IPv6 lookup with a timeout.

Source

pub async fn lookup_ipv4_ipv6<T: ToString>( &self, host: T, timeout: Duration, ) -> Result<impl Iterator<Item = IpAddr> + use<T>, DnsError>

Resolves IPv4 and IPv6 in parallel with a timeout.

LookupIpStrategy::Ipv4AndIpv6 will wait for ipv6 resolution timeout, even if it is not usable on the stack, so we manually query both lookups concurrently and time them out individually.

Source

pub async fn resolve_host( &self, url: &Url, prefer_ipv6: bool, timeout: Duration, ) -> Result<IpAddr, DnsError>

Resolves a hostname from a URL to an IP address.

Source

pub fn resolve_host_all<'a>( &'a self, url: &Url, timeout: Duration, ) -> impl Stream<Item = Result<IpAddr, DnsError>> + Send + 'a

Resolves a hostname from a URL to its IP addresses, streamed as they resolve.

IPv4 and IPv6 are looked up concurrently and each address is yielded as soon as its lookup completes, so a caller can start dialing without waiting for both families.

A lookup failure is swallowed as long as the other family yields an address. Only if both lookups fail does the stream yield a single DnsError.

The stream ends once both lookups have finished and every address or the error have been yielded.

Source

pub async fn lookup_ipv4_staggered( &self, host: impl ToString, timeout: Duration, delays_ms: &[u64], ) -> Result<impl Iterator<Item = IpAddr>, StaggeredError<DnsError>>

Performs an IPv4 lookup with a timeout in a staggered fashion.

From the moment this function is called, each lookup is scheduled after the delays in delays_ms with the first call being done immediately. [200ms, 300ms] results in calls at T+0ms, T+200ms and T+300ms. The timeout is applied to each call individually. The result of the first successful call is returned, or a summary of all errors otherwise.

Source

pub async fn lookup_ipv6_staggered( &self, host: impl ToString, timeout: Duration, delays_ms: &[u64], ) -> Result<impl Iterator<Item = IpAddr>, StaggeredError<DnsError>>

Performs an IPv6 lookup with a timeout in a staggered fashion.

From the moment this function is called, each lookup is scheduled after the delays in delays_ms with the first call being done immediately. [200ms, 300ms] results in calls at T+0ms, T+200ms and T+300ms. The timeout is applied to each call individually. The result of the first successful call is returned, or a summary of all errors otherwise.

Source

pub async fn lookup_ipv4_ipv6_staggered( &self, host: impl ToString, timeout: Duration, delays_ms: &[u64], ) -> Result<impl Iterator<Item = IpAddr>, StaggeredError<DnsError>>

Races an IPv4 and IPv6 lookup with a timeout in a staggered fashion.

From the moment this function is called, each lookup is scheduled after the delays in delays_ms with the first call being done immediately. [200ms, 300ms] results in calls at T+0ms, T+200ms and T+300ms. The timeout is applied as stated in Self::lookup_ipv4_ipv6. The result of the first successful call is returned, or a summary of all errors otherwise.

Source

pub async fn lookup_endpoint_by_id( &self, endpoint_id: &EndpointId, origin: &str, ) -> Result<EndpointInfo, LookupError>

Looks up endpoint info by EndpointId and origin domain name.

To lookup endpoints that published their endpoint info to the DNS servers run by n0, pass N0_DNS_ENDPOINT_ORIGIN_PROD as origin.

Source

pub async fn lookup_endpoint_by_domain_name( &self, name: &str, ) -> Result<EndpointInfo, LookupError>

Looks up endpoint info by DNS name.

Source

pub async fn lookup_endpoint_by_domain_name_staggered( &self, name: &str, delays_ms: &[u64], ) -> Result<EndpointInfo, StaggeredError<LookupError>>

Looks up endpoint info by DNS name in a staggered fashion.

From the moment this function is called, each lookup is scheduled after the delays in delays_ms with the first call being done immediately. [200ms, 300ms] results in calls at T+0ms, T+200ms and T+300ms. The result of the first successful call is returned, or a summary of all errors otherwise.

Source

pub async fn lookup_endpoint_by_id_staggered( &self, endpoint_id: &EndpointId, origin: &str, delays_ms: &[u64], ) -> Result<EndpointInfo, StaggeredError<LookupError>>

Looks up endpoint info by EndpointId and origin domain name.

From the moment this function is called, each lookup is scheduled after the delays in delays_ms with the first call being done immediately. [200ms, 300ms] results in calls at T+0ms, T+200ms and T+300ms. The result of the first successful call is returned, or a summary of all errors otherwise.

Trait Implementations§

Source§

impl Clone for DnsResolver

Source§

fn clone(&self) -> DnsResolver

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DnsResolver

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DnsResolver

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more