pub struct DnsResolver { /* private fields */ }Expand description
Performs resolution operations
Implementations§
Source§impl DnsResolver
impl DnsResolver
Sourcepub fn new(config: DnsConfig) -> Result<DnsResolver>
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
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}Sourcepub fn bind<A: ToSocketAddrs>(addr: A, config: DnsConfig) -> Result<DnsResolver>
pub fn bind<A: ToSocketAddrs>(addr: A, config: DnsConfig) -> Result<DnsResolver>
Constructs a DnsResolver using the given configuration and bound
to the given address.
Sourcepub fn resolve_addr(&self, addr: &IpAddr) -> Result<String>
pub fn resolve_addr(&self, addr: &IpAddr) -> Result<String>
Resolves an IPv4 or IPv6 address to a hostname.
Sourcepub fn resolve_host(&self, host: &str) -> Result<ResolveHost>
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}Sourcepub fn resolve_record<Rec: Record>(&self, name: &str) -> Result<Vec<Rec>>
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
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}Auto Trait Implementations§
impl !Freeze for DnsResolver
impl !RefUnwindSafe for DnsResolver
impl Send for DnsResolver
impl !Sync for DnsResolver
impl Unpin for DnsResolver
impl UnwindSafe for DnsResolver
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more