Hi,
I’m new to rust and i’m write a bit of code to understand how rust works with the network.
So, as std::net::lookup_host
is still unstable, i’m trying to find a way to get the IPs of a domain, and strangly, I don’t find any complete workaround to do that while it became stable.
I found a way to do it on stackoverflow ( here ) but in this usecase, you have to gives a port to connect, and there is no error handling. In my usecase, all domain will not listen to port 80, and some domain may don’t even exist as i’m trying to discover them.
Do anyone know, at least, how to correctly handle error ( like if the domain don’t resolve any IP), or if there is a way to resolve DNS without trying to connect to a port ?
Here is the code I’m ending to, my goal being to list the IPs of various subdomains if they exists :
for subdomain in subdomains {
let mut dom: String = subdomain.to_string() + domain +":80" ;
println!("{}", dom);
let ips: Vec<String> = dom.to_socket_addrs()
.expect("Unable to resolve domain")
.collect();
println!("{:?}", ips)
}
and what I would like ( as I don’t really know yet the best way to do that ) would looks like that :
for subdomain in subdomains {
let mut dom: String = subdomain.to_string() + domain; // no more ":80"
println!("{}", dom);
try {
let ips: Vec<String> = dom.to_socket_addrs()
.expect("Unable to resolve domain")
.collect();
println!("{:?}", ips)
} catch Err {
println!("lookup failure: {}", Err); // print error without panicking
}
}
Many thanks in advance.