Skip to main content

veilid_tools/resolver/native/
mod.rs

1use super::*;
2
3// Only the backends that receive wire-format answers decode them; the rest get parsed records
4#[cfg_attr(
5    any(target_os = "macos", target_os = "ios", target_os = "windows"),
6    allow(
7        dead_code,
8        reason = "decoder is used by the Android and libresolv backends"
9    )
10)]
11pub(crate) mod dns;
12
13cfg_if! {
14    if #[cfg(target_os = "android")] {
15        pub(crate) mod android;
16        mod fd_ready;
17        mod ptr_unix;
18        use android as txt;
19        use ptr_unix as ptr;
20    } else if #[cfg(any(target_os = "macos", target_os = "ios"))] {
21        pub(crate) mod apple;
22        mod fd_ready;
23        mod ptr_unix;
24        use apple as txt;
25        use ptr_unix as ptr;
26    } else if #[cfg(target_os = "windows")] {
27        pub(crate) mod windows;
28        use windows as txt;
29        use windows as ptr;
30    } else {
31        pub(crate) mod resolv;
32        mod ptr_unix;
33        use resolv as txt;
34        use ptr_unix as ptr;
35    }
36}
37
38impl From<dns::DnsError> for ResolverError {
39    fn from(e: dns::DnsError) -> Self {
40        match e {
41            dns::DnsError::Unsupported => ResolverError::Unsupported,
42            e => ResolverError::Generic(e.to_string()),
43        }
44    }
45}
46
47/// Cross-platform DNS resolver backed by the platform's system resolver.
48pub struct Resolver {}
49
50impl Resolver {
51    /// Whether this platform can resolve TXT records. False on Android below API 29.
52    #[must_use]
53    pub fn supports_txt() -> bool {
54        cfg_if! {
55            if #[cfg(target_os = "android")] {
56                android::available()
57            } else {
58                true
59            }
60        }
61    }
62
63    /// Resolve the TXT records for `host`, returning each record's text.
64    ///
65    /// Errors with [`ResolverError::Unsupported`] when the platform has no TXT resolver, and
66    /// [`ResolverError::Generic`] when the lookup fails or returns no records.
67    pub async fn txt_lookup<S: AsRef<str>>(host: S) -> Result<Vec<String>, ResolverError> {
68        Ok(txt::txt_lookup(host.as_ref()).await?)
69    }
70
71    /// Reverse-resolve `ip_addr` to a hostname via its PTR record.
72    ///
73    /// Errors with [`ResolverError::Generic`] when the lookup fails or returns no name.
74    pub async fn ptr_lookup(ip_addr: IpAddr) -> Result<String, ResolverError> {
75        Ok(ptr::ptr_lookup(ip_addr).await?)
76    }
77}