Debian Codegraph @

src/lib.rs

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
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 /*
36 //! # `getaddrinfo`
37 //! ```rust
38 //! extern crate dns_lookup;
39 //!
40 //! use dns_lookup::{getaddrinfo, AddrInfoHints, SockType};
41 //!
42 //! fn main() {
43 //! let hostname = "localhost";
44 //! let service = "ssh";
45 //! let hints = AddrInfoHints {
46 //! socktype: SockType::Stream.into(),
47 //! .. AddrInfoHints::default()
48 //! };
49 //! let sockets =
50 //! getaddrinfo(Some(hostname), Some(service), Some(hints))
51 //! .unwrap().collect::<std::io::Result<Vec<_>>>().unwrap();
52 //!
53 //! for socket in sockets {
54 //! // Try connecting to socket
55 //! let _ = socket;
56 //! }
57 //! }
58 //! ```
•59 */
60 //!
61 //! # `getnameinfo`
62 //! ```rust
63 //! use dns_lookup::getnameinfo;
64 //! use std::net::{IpAddr, SocketAddr};
65 //!
66 //! let ip: IpAddr = "127.0.0.1".parse().unwrap();
67 //! let port = 22;
68 //! let socket: SocketAddr = (ip, port).into();
69 //!
70 //! let (name, service) = match getnameinfo(&socket, 0) {
71 //! Ok((n, s)) => (n, s),
72 //! Err(e) => panic!("Failed to lookup socket {:?}", e),
73 //! };
74 //!
75 //! println!("{:?} {:?}", name, service);
76 //! let _ = (name, service);
77 //! ```
78
79 #![cfg_attr(feature="clippy", feature(plugin))]
80 #![cfg_attr(feature="clippy", plugin(clippy))]
81
82 #[cfg(unix)] extern crate libc;
83
84 #[cfg(windows)] extern crate winapi;
85
86 extern crate socket2;
87
88 mod addrinfo;
89 mod nameinfo;
90 mod err;
91 mod lookup;
92 mod types;
93 mod hostname;
94 #[cfg(windows)]
95 mod win;
96
97 pub use addrinfo::{getaddrinfo, AddrInfoIter, AddrInfo, AddrInfoHints};
98 pub use hostname::get_hostname;
99 pub use lookup::{lookup_host, lookup_addr};
100 pub use nameinfo::getnameinfo;
101 pub use types::{SockType, Protocol, AddrFamily};
102 pub use err::{LookupErrorKind, LookupError};