Function get_host_by_addr

Source
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)
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
fn main() {
    let mut args = env::args();
    let cmd = args.next().unwrap();
    let addr = match args.next() {
        None => {
            println!("Usage: {} <addr>", cmd);
            return;
        }
        Some(addr) => addr
    };

    let addr = match IpAddr::from_str(&addr) {
        Ok(addr) => addr,
        Err(err) => {
            println!("Not an address: {:?}", err);
            return;
        }
    };

    match get_host_by_addr(addr) {
        Ok(Some(ent)) => {
            println!("{}", addr);
            println!("  {}", ent.name());
            if !ent.aliases().is_empty() {
                println!("  Aliases:");
                for name in ent.aliases() {
                    println!("     {}", name);
                }
            }
            println!("  Addresses:");
            for addr in ent.addrs() {
                println!("     {}", addr);
            }
        }
        Ok(None) => println!("Not found."),
        Err(err) => println!("Error: {:?}", err),
    }
}