Skip to content

Instantly share code, notes, and snippets.

@Bas-Man
Last active March 26, 2021 07:39
Show Gist options
  • Save Bas-Man/1567424e68dd98bf9ea02796cb72b5c6 to your computer and use it in GitHub Desktop.
Save Bas-Man/1567424e68dd98bf9ea02796cb72b5c6 to your computer and use it in GitHub Desktop.
trust-dns-resolver looking up MX records in Rust
use trust_dns_resolver::config::*;
use trust_dns_resolver::Resolver;
// Note you should add the crate to your Cargo.toml file
// trust-dns-resolver = "0.20.1"
fn main() {
// Construct a new Resolver with default configuration options
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default()).unwrap();
// Lookup the IP addresses associated with a name.
// The final dot forces this to be an FQDN, otherwise the search rules as specified
// in `ResolverOpts` will take effect. FQDN's are generally cheaper queries.
let mx_response = resolver.mx_lookup("gmail.com.");
// There can be many addresses associated with the name,
// this can return IPv4 and/or IPv6 addresses
match mx_response {
Err(_) => println!("No Records"),
Ok(mx_response) => {
let records = mx_response.iter();
for record in records {
println!("{} {}", record.preference(), record.exchange());
let lookup_response = resolver.lookup_ip(record.exchange().to_string().as_str());
match lookup_response {
Err(_) => println!("This exchange host has no address."),
Ok(lookup_response) => {
let ip_addrs = lookup_response.iter();
for ip_addr in ip_addrs {
if ip_addr.is_ipv4() {
println!(" ip4: {}", ip_addr)
} else {
println!(" ip6: {}", ip_addr)
}
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment