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.
4 //
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.
10
11 /*!
12
13 Synchronous DNS Resolution
14
15 Contains the functionality to perform DNS resolution in a style related to
16 getaddrinfo()
17
18 */
19
20 #![allow(missing_doc)]
21
22 use iter::Iterator;
23 use io::IoResult;
24 use io::net::ip::{SocketAddr, IpAddr};
25 use option::{Option, Some, None};
26 use rt::rtio::{IoFactory, LocalIo};
27 use vec::Vec;
28
29 /// Hints to the types of sockets that are desired when looking up hosts
30 pub enum SocketType {
31 Stream, Datagram, Raw
32 }
33
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.
36 ///
37 /// The meaning of each of these flags can be found with `man -s 3 getaddrinfo`
38 pub enum Flag {
39 AddrConfig,
40 All,
41 CanonName,
42 NumericHost,
43 NumericServ,
44 Passive,
45 V4Mapped,
46 }
47
48 /// A transport protocol associated with either a hint or a return value of
49 /// `lookup`
50 pub enum Protocol {
51 TCP, UDP
52 }
53
54 /// This structure is used to provide hints when fetching addresses for a
55 /// remote host to control how the lookup is performed.
56 ///
57 /// For details on these fields, see their corresponding definitions via
58 /// `man -s 3 getaddrinfo`
59 pub struct Hint {
60 pub family: uint,
61 pub socktype: Option<SocketType>,
62 pub protocol: Option<Protocol>,
63 pub flags: uint,
64 }
65
66 pub struct Info {
67 pub address: SocketAddr,
68 pub family: uint,
69 pub socktype: Option<SocketType>,
70 pub protocol: Option<Protocol>,
71 pub flags: uint,
72 }
73
74 /// Easy name resolution. Given a hostname, returns the list of IP addresses for
75 /// that hostname.
76 pub fn get_host_addresses(host: &str) -> IoResult<Vec<IpAddr>> {
77 lookup(Some(host), None, None).map(|a| a.move_iter().map(|i| i.address.ip).collect())
78 }
79
80 /// Full-fleged resolution. This function will perform a synchronous call to
81 /// getaddrinfo, controlled by the parameters
82 ///
83 /// # Arguments
84 ///
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
88 /// controls lookup
89 ///
90 /// FIXME: this is not public because the `Hint` structure is not ready for public
91 /// consumption just yet.
92 fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
93 -> IoResult<Vec<Info>> {
94 LocalIo::maybe_raise(|io| io.get_host_addresses(hostname, servname, hint))
95 }
96
97 // Ignored on android since we cannot give tcp/ip
98 // permission without help of apk
99 #[cfg(test, not(target_os = "android"))]
100 mod test {
101 iotest!(fn dns_smoke_test() {
102 let ipaddrs = get_host_addresses("localhost").unwrap();
103 let mut found_local = false;
104 let local_addr = &Ipv4Addr(127, 0, 0, 1);
105 for addr in ipaddrs.iter() {
106 found_local = found_local || addr == local_addr;
107 }
108 assert!(found_local);
109 })
110
111 iotest!(fn issue_10663() {
112 // Something should happen here, but this certainly shouldn't cause
113 // everything to die. The actual outcome we don't care too much about.
114 get_host_addresses("example.com").unwrap();
115 } #[ignore])
116 }
libstd/io/net/addrinfo.rs:65:1-65:1 -struct- definition:
pub struct Info {
pub address: SocketAddr,
pub family: uint,
references:- 2libstd/rt/rtio.rs:
163: fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
164: hint: Option<ai::Hint>) -> IoResult<Vec<ai::Info>>;
libstd/io/net/addrinfo.rs:
92: fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
93: -> IoResult<Vec<Info>> {
94: LocalIo::maybe_raise(|io| io.get_host_addresses(hostname, servname, hint))
libstd/io/net/addrinfo.rs:58:27-58:27 -struct- definition:
/// `man -s 3 getaddrinfo`
pub struct Hint {
pub family: uint,
references:- 2libstd/rt/rtio.rs:
163: fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
164: hint: Option<ai::Hint>) -> IoResult<Vec<ai::Info>>;
libstd/io/net/addrinfo.rs:
91: /// consumption just yet.
92: fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
93: -> IoResult<Vec<Info>> {
libstd/io/net/addrinfo.rs:29:73-29:73 -enum- definition:
/// Hints to the types of sockets that are desired when looking up hosts
pub enum SocketType {
Stream, Datagram, Raw
references:- 268: pub family: uint,
69: pub socktype: Option<SocketType>,
70: pub protocol: Option<Protocol>,
libstd/io/net/addrinfo.rs:49:13-49:13 -enum- definition:
/// `lookup`
pub enum Protocol {
TCP, UDP
references:- 261: pub socktype: Option<SocketType>,
62: pub protocol: Option<Protocol>,
63: pub flags: uint,
--
69: pub socktype: Option<SocketType>,
70: pub protocol: Option<Protocol>,
71: pub flags: uint,