pub fn get_host_by_addr(addr: IpAddr) -> Result<Option<HostEnt>, Error>
Expand description
Returns host information for a given IP address.
The IP address can either be an IPv4 or IPv6 address. The function waits
for all necessary IO to resolve. Upon success, it
returns a HostEnt
value if a host for the given name was found or
Ok(None)
otherwise.
ยงLimitations
For this initial version of the crate, the lookup is a files
lookup
first and only if that does fail to yield a result, a DNS query for
PTR records. This initial version also does not yet fill
the aliases list of the returned HostEnt
.
Examples found in repository?
examples/hostbyaddr.rs (line 27)
8fn main() {
9 let mut args = env::args();
10 let cmd = args.next().unwrap();
11 let addr = match args.next() {
12 None => {
13 println!("Usage: {} <addr>", cmd);
14 return;
15 }
16 Some(addr) => addr
17 };
18
19 let addr = match IpAddr::from_str(&addr) {
20 Ok(addr) => addr,
21 Err(err) => {
22 println!("Not an address: {:?}", err);
23 return;
24 }
25 };
26
27 match get_host_by_addr(addr) {
28 Ok(Some(ent)) => {
29 println!("{}", addr);
30 println!(" {}", ent.name());
31 if !ent.aliases().is_empty() {
32 println!(" Aliases:");
33 for name in ent.aliases() {
34 println!(" {}", name);
35 }
36 }
37 println!(" Addresses:");
38 for addr in ent.addrs() {
39 println!(" {}", addr);
40 }
41 }
42 Ok(None) => println!("Not found."),
43 Err(err) => println!("Error: {:?}", err),
44 }
45}