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