1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Synchronous DNS Resolution
13 //! Contains the functionality to perform DNS resolution in a style related to
16 #![allow(missing_docs)]
18 pub use self::SocketType::*;
19 pub use self::Flag::*;
20 pub use self::Protocol::*;
22 use iter::IteratorExt;
24 use io::net::ip::{SocketAddr, IpAddr};
25 use option::{Option, Some, None};
29 /// Hints to the types of sockets that are desired when looking up hosts
34 /// Flags which can be or'd into the `flags` field of a `Hint`. These are used
35 /// to manipulate how a query is performed.
37 /// The meaning of each of these flags can be found with `man -s 3 getaddrinfo`
48 /// A transport protocol associated with either a hint or a return value of
54 /// This structure is used to provide hints when fetching addresses for a
55 /// remote host to control how the lookup is performed.
57 /// For details on these fields, see their corresponding definitions via
58 /// `man -s 3 getaddrinfo`
61 pub socktype: Option<SocketType>,
62 pub protocol: Option<Protocol>,
67 pub address: SocketAddr,
69 pub socktype: Option<SocketType>,
70 pub protocol: Option<Protocol>,
74 /// Easy name resolution. Given a hostname, returns the list of IP addresses for
76 pub fn get_host_addresses(host: &str) -> IoResult<Vec<IpAddr>> {
77 lookup(Some(host), None, None).map(|a| a.into_iter().map(|i| i.address.ip).collect())
80 /// Full-fledged resolution. This function will perform a synchronous call to
81 /// getaddrinfo, controlled by the parameters
85 /// * hostname - an optional hostname to lookup against
86 /// * servname - an optional service name, listed in the system services
87 /// * hint - see the hint structure, and "man -s 3 getaddrinfo", for how this
90 /// FIXME: this is not public because the `Hint` structure is not ready for public
91 /// consumption just yet.
92 #[allow(unused_variables)]
93 fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
94 -> IoResult<Vec<Info>> {
95 sys::addrinfo::get_host_addresses(hostname, servname, hint)
98 // Ignored on android since we cannot give tcp/ip
99 // permission without help of apk
100 #[cfg(all(test, not(target_os = "android")))]
107 fn dns_smoke_test() {
108 let ipaddrs = get_host_addresses("localhost").unwrap();
109 let mut found_local = false;
110 let local_addr = &Ipv4Addr(127, 0, 0, 1);
111 for addr in ipaddrs.iter() {
112 found_local = found_local || addr == local_addr;
114 assert!(found_local);
120 // Something should happen here, but this certainly shouldn't cause
121 // everything to die. The actual outcome we don't care too much about.
122 get_host_addresses("example.com").unwrap();