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: u32Minimum number of dots in a name to trigger an initial absolute query
timeout: DurationDuration before retrying or failing an unanswered request
attempts: u32Number of attempts made before returning an error
rotate: boolWhether to rotate through available nameservers
use_inet6: boolIf true, perform AAAA queries first and return IPv4 addresses
as IPv4-mapped IPv6 addresses.
Implementations§
Source§impl DnsConfig
impl DnsConfig
Sourcepub fn load_default() -> Result<DnsConfig>
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
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}Sourcepub fn with_name_servers(name_servers: Vec<SocketAddr>) -> DnsConfig
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§
Auto Trait Implementations§
impl Freeze for DnsConfig
impl RefUnwindSafe for DnsConfig
impl Send for DnsConfig
impl Sync for DnsConfig
impl Unpin for DnsConfig
impl UnwindSafe for DnsConfig
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