| 1 |
//! # `dns_lookup` |
| 2 |
//! A small wrapper for libc to perform simple DNS lookups. |
| 3 |
//! |
| 4 |
//! Two main functions are provided. |
| 5 |
//! |
| 6 |
//! PS: If you only need a single result, consider [ToSocketAddrs](https://doc.rust-lang.org/std/net/trait.ToSocketAddrs.html) in libstd. |
| 7 |
//! |
| 8 |
//! |
| 9 |
//! # `lookup_host` |
| 10 |
//! Given a hostname, return an Iterator the IP Addresses associated with |
| 11 |
//! it. |
| 12 |
//! |
| 13 |
//! ```rust |
| 14 |
//! use dns_lookup::lookup_host; |
| 15 |
//! |
| 16 |
//! let hostname = "localhost"; |
| 17 |
//! let ips: Vec<std::net::IpAddr> = lookup_host(hostname).unwrap(); |
| 18 |
//! assert!(ips.contains(&"127.0.0.1".parse().unwrap())); |
| 19 |
//! ``` |
| 20 |
//! |
| 21 |
//! # `lookup_addr` |
| 22 |
//! Given an IP Address, return the reverse DNS entry (hostname) for the |
| 23 |
//! given IP Address. |
| 24 |
//! |
| 25 |
//! |
| 26 |
//! ```rust |
| 27 |
//! use dns_lookup::lookup_addr; |
| 28 |
//! |
| 29 |
//! let ip: std::net::IpAddr = "127.0.0.1".parse().unwrap(); |
| 30 |
//! let host = lookup_addr(&ip).unwrap(); |
| 31 |
//! |
| 32 |
//! // The string "localhost" on unix, and the hostname on Windows. |
| 33 |
//! ``` |
| 34 |
//! |
| 35 |
//! # `getaddrinfo` |
| 36 |
//! ```rust |
| 37 |
//! extern crate dns_lookup; |
| 38 |
//! |
| 39 |
//! use dns_lookup::{getaddrinfo, AddrInfoHints, SockType}; |
| 40 |
//! |
| 41 |
//! fn main() { |
| 42 |
//! let hostname = "localhost"; |
| 43 |
//! let service = "ssh"; |
| 44 |
//! let hints = AddrInfoHints { |
| 45 |
//! socktype: SockType::Stream.into(), |
| 46 |
//! .. AddrInfoHints::default() |
| 47 |
//! }; |
| 48 |
//! let sockets = |
| 49 |
//! getaddrinfo(Some(hostname), Some(service), Some(hints)) |
| 50 |
//! .unwrap().collect::<std::io::Result<Vec<_>>>().unwrap(); |
| 51 |
//! |
| 52 |
//! for socket in sockets { |
| 53 |
//! // Try connecting to socket |
| 54 |
//! let _ = socket; |
| 55 |
//! } |
| 56 |
//! } |
| 57 |
//! ``` |
| 58 |
//! |
| 59 |
//! # `getnameinfo` |
| 60 |
//! ```rust |
| 61 |
//! use dns_lookup::getnameinfo; |
| 62 |
//! use std::net::{IpAddr, SocketAddr}; |
| 63 |
//! |
| 64 |
//! let ip: IpAddr = "127.0.0.1".parse().unwrap(); |
| 65 |
//! let port = 22; |
| 66 |
//! let socket: SocketAddr = (ip, port).into(); |
| 67 |
//! |
| 68 |
//! let (name, service) = match getnameinfo(&socket, 0) { |
| 69 |
//! Ok((n, s)) => (n, s), |
| 70 |
//! Err(e) => panic!("Failed to lookup socket {:?}", e), |
| 71 |
//! }; |
| 72 |
//! |
| 73 |
//! println!("{:?} {:?}", name, service); |
| 74 |
//! let _ = (name, service); |
| 75 |
//! ``` |
| 76 |
|
| 77 |
#![cfg_attr(feature="clippy", feature(plugin))] |
| 78 |
#![cfg_attr(feature="clippy", plugin(clippy))] |
| 79 |
|
| 80 |
#[cfg(unix)] extern crate libc; |
| 81 |
|
| 82 |
#[cfg(windows)] extern crate winapi; |
| 83 |
|
| 84 |
extern crate socket2; |
| 85 |
|
| 86 |
mod addrinfo; |
| 87 |
mod nameinfo; |
| 88 |
mod err; |
| 89 |
mod lookup; |
| 90 |
mod types; |
| 91 |
mod hostname; |
| 92 |
#[cfg(windows)] |
| 93 |
mod win; |
| 94 |
|
| 95 |
pub use addrinfo::{getaddrinfo, AddrInfoIter, AddrInfo, AddrInfoHints}; |
| 96 |
pub use hostname::get_hostname; |
| 97 |
pub use lookup::{lookup_host, lookup_addr}; |
| 98 |
pub use nameinfo::getnameinfo; |
| 99 |
pub use types::{SockType, Protocol, AddrFamily}; |
| 100 |
pub use err::{LookupErrorKind, LookupError}; |