reqwest/dns/
hickory.rs

1//! DNS resolution via the [hickory-resolver](https://github.com/hickory-dns/hickory-dns) crate
2
3use hickory_resolver::{
4    config::LookupIpStrategy, lookup_ip::LookupIpIntoIter, ResolveError, TokioResolver,
5};
6use once_cell::sync::OnceCell;
7
8use std::fmt;
9use std::net::SocketAddr;
10use std::sync::Arc;
11
12use super::{Addrs, Name, Resolve, Resolving};
13
14/// Wrapper around an `AsyncResolver`, which implements the `Resolve` trait.
15#[derive(Debug, Default, Clone)]
16pub(crate) struct HickoryDnsResolver {
17    /// Since we might not have been called in the context of a
18    /// Tokio Runtime in initialization, so we must delay the actual
19    /// construction of the resolver.
20    state: Arc<OnceCell<TokioResolver>>,
21}
22
23struct SocketAddrs {
24    iter: LookupIpIntoIter,
25}
26
27#[derive(Debug)]
28struct HickoryDnsSystemConfError(ResolveError);
29
30impl Resolve for HickoryDnsResolver {
31    fn resolve(&self, name: Name) -> Resolving {
32        let resolver = self.clone();
33        Box::pin(async move {
34            let resolver = resolver.state.get_or_try_init(new_resolver)?;
35
36            let lookup = resolver.lookup_ip(name.as_str()).await?;
37            let addrs: Addrs = Box::new(SocketAddrs {
38                iter: lookup.into_iter(),
39            });
40            Ok(addrs)
41        })
42    }
43}
44
45impl Iterator for SocketAddrs {
46    type Item = SocketAddr;
47
48    fn next(&mut self) -> Option<Self::Item> {
49        self.iter.next().map(|ip_addr| SocketAddr::new(ip_addr, 0))
50    }
51}
52
53/// Create a new resolver with the default configuration,
54/// which reads from `/etc/resolve.conf`. The options are
55/// overridden to look up for both IPv4 and IPv6 addresses
56/// to work with "happy eyeballs" algorithm.
57fn new_resolver() -> Result<TokioResolver, HickoryDnsSystemConfError> {
58    let mut builder = TokioResolver::builder_tokio().map_err(HickoryDnsSystemConfError)?;
59    builder.options_mut().ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
60    Ok(builder.build())
61}
62
63impl fmt::Display for HickoryDnsSystemConfError {
64    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65        f.write_str("error reading DNS system conf for hickory-dns")
66    }
67}
68
69impl std::error::Error for HickoryDnsSystemConfError {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        Some(&self.0)
72    }
73}