DnsConfig

Struct DnsConfig 

Source
pub struct DnsConfig {
    pub name_servers: Vec<SocketAddr>,
    pub search: Vec<String>,
    pub n_dots: u32,
    pub timeout: Duration,
    pub attempts: u32,
    pub rotate: bool,
    pub use_inet6: bool,
}
Expand description

Configures the behavior of DNS requests

Fields§

§name_servers: Vec<SocketAddr>

List of name servers; must not be empty

§search: Vec<String>

List of search domains

§n_dots: u32

Minimum number of dots in a name to trigger an initial absolute query

§timeout: Duration

Duration before retrying or failing an unanswered request

§attempts: u32

Number of attempts made before returning an error

§rotate: bool

Whether to rotate through available nameservers

§use_inet6: bool

If true, perform AAAA queries first and return IPv4 addresses as IPv4-mapped IPv6 addresses.

Implementations§

Source§

impl DnsConfig

Source

pub fn load_default() -> Result<DnsConfig>

Returns the default system configuration for DNS requests.

Examples found in repository?
examples/lookup_srv.rs (line 15)
6fn main() {
7    let args = args().collect::<Vec<_>>();
8
9    if args.len() != 4 {
10        println!("Usage: {} <service> <proto> <name>", args[0]);
11        println!(" e.g.  {} _http _tcp example.com", args[0]);
12        return;
13    }
14
15    let config = match DnsConfig::load_default() {
16        Ok(config) => config,
17        Err(e) => {
18            println!("failed to load system configuration: {}", e);
19            return;
20        }
21    };
22
23    let resolver = match DnsResolver::new(config) {
24        Ok(resolver) => resolver,
25        Err(e) => {
26            println!("failed to create DNS resolver: {}", e);
27            return;
28        }
29    };
30
31    let name = format!("{}.{}.{}", args[1], args[2], args[3]);
32
33    match resolver.resolve_record::<Srv>(&name) {
34        Ok(records) => {
35            for srv in records {
36                println!(
37                    "SRV priority={} weight={} port={} target={}",
38                    srv.priority, srv.weight, srv.port, srv.target
39                );
40            }
41        }
42        Err(e) => {
43            println!("{}", e);
44            return;
45        }
46    }
47}
More examples
Hide additional examples
examples/lookup_txt.rs (line 16)
7fn main() {
8    let args = args().collect::<Vec<_>>();
9
10    if args.len() != 2 {
11        println!("Usage: {} <name>", args[0]);
12        println!(" e.g.  {} example.com", args[0]);
13        return;
14    }
15
16    let config = match DnsConfig::load_default() {
17        Ok(config) => config,
18        Err(e) => {
19            println!("Failed to load system configuration: {}", e);
20            return;
21        }
22    };
23
24    let resolver = match DnsResolver::new(config) {
25        Ok(resolver) => resolver,
26        Err(e) => {
27            println!("Failed to create DNS resolver: {}", e);
28            return;
29        }
30    };
31
32    match resolver.resolve_record::<Txt>(&args[1]) {
33        Ok(records) => {
34            for txt in records {
35                let data = match str::from_utf8(&txt.data) {
36                    Ok(string) => string,
37                    Err(e) => {
38                        println!("Failed to decode UTF8 data: {}", e);
39                        return;
40                    }
41                };
42                println!("TXT data={}", data);
43            }
44        }
45        Err(e) => {
46            println!("{}", e);
47            return;
48        }
49    }
50}
Source

pub fn with_name_servers(name_servers: Vec<SocketAddr>) -> DnsConfig

Returns a DnsConfig using the given set of name servers, setting all other fields to generally sensible default values.

Examples found in repository?
examples/resolver.rs (lines 8-12)
7fn main() {
8    let config = DnsConfig::with_name_servers(vec![
9        // Use Google's public DNS servers instead of the system default.
10        "8.8.8.8:53".parse().unwrap(),
11        "8.8.4.4:53".parse().unwrap(),
12    ]);
13
14    let resolver = match DnsResolver::new(config) {
15        Ok(r) => r,
16        Err(e) => {
17            println!("failed to create DNS resolver: {}", e);
18            return;
19        }
20    };
21
22    let args = args().collect::<Vec<_>>();
23
24    if args.len() == 1 {
25        println!("Usage: {} <host name> [...]", args[0]);
26        return;
27    }
28
29    for arg in &args[1..] {
30        match resolver.resolve_host(&arg) {
31            Ok(mut addrs) => {
32                let addr = addrs.next().expect("empty ResolveHost");
33                let n = addrs.count();
34
35                if n == 0 {
36                    println!("\"{}\" resolved to {}", arg, addr);
37                } else {
38                    println!("\"{}\" resolved to {} ({} more)", arg, addr, n);
39                }
40            }
41            Err(e) => println!("failed to resolve \"{}\": {}", arg, e),
42        }
43    }
44}

Trait Implementations§

Source§

impl Clone for DnsConfig

Source§

fn clone(&self) -> DnsConfig

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 DnsConfig

Source§

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

Formats the value using the given formatter. 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.

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.

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V