Crate dns_lookup [] [src]

dns_lookup

A small wrapper for libc to perform simple DNS lookups.

Two main functions are provided.

lookup_host

Given a hostname, return an Iterator the IP Addresses associated with it.

use dns_lookup::lookup_host;

  let hostname = "localhost";
  let ips: Vec<std::net::IpAddr> = lookup_host(hostname).unwrap();
  assert!(ips.contains(&"127.0.0.1".parse().unwrap()));

lookup_addr

Given an IP Address, return the reverse DNS entry (hostname) for the given IP Address.

use dns_lookup::lookup_addr;

  let ip: std::net::IpAddr = "127.0.0.1".parse().unwrap();
  let hostname = lookup_addr(&ip).unwrap();
  assert_eq!(hostname, "localhost");

getaddrinfo

extern crate dns_lookup;

  use dns_lookup::{getaddrinfo, AddrInfoHints, SockType};

  fn main() {
    let hostname = "localhost";
    let service = "ssh";
    let hints = AddrInfoHints {
      socktype: SockType::Stream.into(),
      .. AddrInfoHints::default()
    };
    let sockets =
      getaddrinfo(Some(hostname), Some(service), Some(hints))
        .unwrap().collect::<std::io::Result<Vec<_>>>().unwrap();

    for socket in sockets {
      // Try connecting to socket
      let _ = socket;
    }
  }

getnameinfo

use dns_lookup::getnameinfo;
  use std::net::{IpAddr, SocketAddr};

  let ip: IpAddr = "127.0.0.1".parse().unwrap();
  let port = 22;
  let socket: SocketAddr = (ip, port).into();

  let (name, service) = match getnameinfo(&socket, 0) {
    Ok((n, s)) => (n, s),
    Err(e) => panic!("Failed to lookup socket {:?}", e),
  };

  println!("{:?} {:?}", name, service);
  let _ = (name, service);

Structs

AddrInfo

Struct that stores socket information, as returned by getaddrinfo.

AddrInfoHints

A struct used as the hints argument to getaddrinfo.

AddrInfoIter

An iterator of AddrInfo structs, wrapping a linked-list returned by getaddrinfo.

Enums

AddrFamily

Address Family

Protocol

Socket Protocol

SockType

Socket Type

Functions

getaddrinfo

Retrieve socket information for a host, service, or both. Acts as a thin wrapper around the libc getaddrinfo.

getnameinfo

Retrieve the name for a given IP and Service. Acts as a thin wrapper around the libc getnameinfo.

lookup_addr

Lookup the hostname of a given IP Address via DNS.

lookup_host

Lookup the address for a given hostname via DNS.