nrf_modem/dns/
mod.rs

1#[cfg(not(feature = "dns-async"))]
2mod dns_blocking;
3
4#[cfg(not(feature = "dns-async"))]
5pub use dns_blocking::{
6    get_host_by_name, get_host_by_name_with_cancellation, resolve_dns,
7    resolve_dns_with_cancellation,
8};
9
10#[cfg(feature = "dns-async")]
11mod dns_async;
12
13#[cfg(feature = "dns-async")]
14mod dns_cache;
15
16#[cfg(feature = "dns-async")]
17pub use dns_async::{
18    get_host_by_name, get_host_by_name_with_cancellation, resolve_dns,
19    resolve_dns_with_cancellation,
20};
21
22/// A DNS query.
23///
24/// Pass to [`resolve_dns()`] or [`resolve_dns_with_cancellation()`].
25#[derive(Copy, Clone, Debug)]
26pub struct DnsQuery<'a> {
27    /// The hostname to resolve.
28    hostname: &'a str,
29
30    /// The desired address type.
31    addr_type: AddrType,
32}
33
34impl<'a> DnsQuery<'a> {
35    /// Create a new DNS query to resolve a given hostname.
36    ///
37    /// Does not restrict the address type to resolve.
38    pub fn new(hostname: &'a str) -> Self {
39        Self {
40            hostname,
41            addr_type: AddrType::Any,
42        }
43    }
44
45    /// Set the address type of the query.
46    ///
47    /// Can be used to ask only for an IPv4 or IPv6 address.
48    #[must_use = "this function returns a new query, it does not modify the existing one"]
49    pub fn with_address_type(self, addr_type: AddrType) -> Self {
50        let mut out = self;
51        out.addr_type = addr_type;
52        out
53    }
54
55    /// Get the hostname to resolve.
56    pub fn hostname(&self) -> &'a str {
57        self.hostname
58    }
59
60    /// Get the address type of the query.
61    pub fn addr_type(&self) -> AddrType {
62        self.addr_type
63    }
64}
65
66/// The address type for a DNS query.
67#[derive(Copy, Clone, Debug)]
68pub enum AddrType {
69    /// Resolve to an IPv4 or IPv6 address.
70    Any,
71
72    /// Resolve to an IPv4 address.
73    V4,
74
75    /// Resolve to an IPv6 address.
76    V6,
77}
78
79impl AddrType {
80    /// Check if the given IP address matches the address type.
81    fn addr_matches(&self, addr: core::net::IpAddr) -> bool {
82        match self {
83            Self::Any => true,
84            Self::V4 => matches!(addr, core::net::IpAddr::V4(_)),
85            Self::V6 => matches!(addr, core::net::IpAddr::V6(_)),
86        }
87    }
88}