sync_resolve/
config.rs

1//! DNS resolver configuration
2
3use std::io;
4use std::net::SocketAddr;
5use std::time::Duration;
6
7/// Configures the behavior of DNS requests
8#[derive(Clone, Debug)]
9pub struct DnsConfig {
10    /// List of name servers; must not be empty
11    pub name_servers: Vec<SocketAddr>,
12    /// List of search domains
13    pub search: Vec<String>,
14
15    /// Minimum number of dots in a name to trigger an initial absolute query
16    pub n_dots: u32,
17    /// Duration before retrying or failing an unanswered request
18    pub timeout: Duration,
19    /// Number of attempts made before returning an error
20    pub attempts: u32,
21
22    /// Whether to rotate through available nameservers
23    pub rotate: bool,
24    /// If `true`, perform `AAAA` queries first and return IPv4 addresses
25    /// as IPv4-mapped IPv6 addresses.
26    pub use_inet6: bool,
27}
28
29impl DnsConfig {
30    /// Returns the default system configuration for DNS requests.
31    pub fn load_default() -> io::Result<DnsConfig> {
32        default_config_impl()
33    }
34
35    /// Returns a `DnsConfig` using the given set of name servers,
36    /// setting all other fields to generally sensible default values.
37    pub fn with_name_servers(name_servers: Vec<SocketAddr>) -> DnsConfig {
38        DnsConfig {
39            name_servers,
40            search: Vec::new(),
41
42            n_dots: 1,
43            timeout: Duration::from_secs(5),
44            attempts: 5,
45
46            rotate: false,
47            use_inet6: false,
48        }
49    }
50}
51
52#[cfg(unix)]
53fn default_config_impl() -> io::Result<DnsConfig> {
54    use crate::resolv_conf::load;
55    load()
56}
57
58#[cfg(windows)]
59fn default_config_impl() -> io::Result<DnsConfig> {
60    // TODO: Get a list of nameservers from Windows API.
61    // For now, return an IO error.
62    Err(io::Error::new(
63        io::ErrorKind::Other,
64        "Nameserver list not available on Windows",
65    ))
66}