DnsResolver

Struct DnsResolver 

Source
pub struct DnsResolver { /* private fields */ }
Expand description

Performs resolution operations

Implementations§

Source§

impl DnsResolver

Source

pub fn new(config: DnsConfig) -> Result<DnsResolver>

Constructs a DnsResolver using the given configuration.

Examples found in repository?
examples/resolver.rs (line 14)
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}
More examples
Hide additional examples
examples/lookup_srv.rs (line 23)
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}
examples/lookup_txt.rs (line 24)
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 bind<A: ToSocketAddrs>(addr: A, config: DnsConfig) -> Result<DnsResolver>

Constructs a DnsResolver using the given configuration and bound to the given address.

Source

pub fn resolve_addr(&self, addr: &IpAddr) -> Result<String>

Resolves an IPv4 or IPv6 address to a hostname.

Source

pub fn resolve_host(&self, host: &str) -> Result<ResolveHost>

Resolves a hostname to a series of IPv4 or IPv6 addresses.

Examples found in repository?
examples/resolver.rs (line 30)
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}
Source

pub fn resolve_record<Rec: Record>(&self, name: &str) -> Result<Vec<Rec>>

Requests a type of record from the DNS server and returns the results.

Examples found in repository?
examples/lookup_srv.rs (line 33)
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 32)
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 send_message<'buf>( &self, out_msg: &Message<'_>, buf: &'buf mut [u8], ) -> Result<Message<'buf>, Error>

Sends a message to the DNS server and attempts to read a response.

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> 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, 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