Function get_host_by_name

Source
pub fn get_host_by_name(name: &str) -> Result<Option<HostEnt>, Error>
Expand description

Returns host information for a given host name.

The name is either a hostname, an IPv4 or IPv6 address in its standard text notation. In the latter two cases, no lookups are performed and a HostEnt is returned with name as the canonical name, the parsed address as the sole address, and no aliases.

Otherwise the name is interpreted as a host name and lookups according to the system configuraition are performed.

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 both A and AAAA records. This initial version also does not yet fill the aliases list of the returned HostEnt.

Examples found in repository?
examples/hostbyname.rs (line 17)
6
7
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
fn main() {
    let mut args = env::args();
    let cmd = args.next().unwrap();
    let name = match args.next() {
        None => {
            println!("Usage: {} <hostname>", cmd);
            return;
        }
        Some(name) => name
    };

    match get_host_by_name(&name) {
        Ok(Some(ent)) => {
            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),
    }
}