1
  2
  3
  4
  5
  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
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
//! # `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.
//!
//! ```rust
//!   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.
//!
//!
//! ```rust
//!   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`
//! ```rust
//!   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`
//! ```rust
//!   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);
//! ```

#![cfg_attr(feature="clippy", feature(plugin))]
#![cfg_attr(feature="clippy", plugin(clippy))]

#[cfg(unix)] extern crate libc;

#[cfg(windows)] extern crate winapi;
#[cfg(windows)] extern crate ws2_32;

extern crate socket2;

#[cfg(windows)]
use std::sync::{Once, ONCE_INIT};

// From socket2-rs
#[cfg(windows)]
fn init_winsock() {
    static INIT: Once = ONCE_INIT;

    INIT.call_once(|| {
        // Initialize winsock through the standard library by just creating a
        // dummy socket. Whether this is successful or not we drop the result as
        // libstd will be sure to have initialized winsock.
        let _ = std::net::UdpSocket::bind("127.0.0.1:34254");
    });
}

mod addrinfo;
mod nameinfo;
mod err;
mod lookup;
mod types;

pub use lookup::{lookup_host, lookup_addr};
pub use addrinfo::{getaddrinfo, AddrInfoIter, AddrInfo, AddrInfoHints};
pub use nameinfo::getnameinfo;
pub use types::{SockType, Protocol, AddrFamily};