(index<- ) ./libstd/rt/io/net/mod.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 use option::{Option, Some, None};
12 use result::{Ok, Err};
13 use rt::io::io_error;
14 use rt::io::net::ip::IpAddr;
15 use rt::rtio::{IoFactory, IoFactoryObject};
16 use rt::local::Local;
17
18 pub mod tcp;
19 pub mod udp;
20 pub mod ip;
21 #[cfg(unix)]
22 pub mod unix;
23
24 /// Simplistic name resolution
25 pub fn get_host_addresses(host: &str) -> Option<~[IpAddr]> {
26 /*!
27 * Get the IP addresses for a given host name.
28 *
29 * Raises io_error on failure.
30 */
31
32 let ipaddrs = unsafe {
33 let io: *mut IoFactoryObject = Local::unsafe_borrow();
34 (*io).get_host_addresses(host)
35 };
36
37 match ipaddrs {
38 Ok(i) => Some(i),
39 Err(ioerr) => {
40 io_error::cond.raise(ioerr);
41 None
42 }
43 }
44 }
45
46 #[cfg(test)]
47 mod test {
48 use option::Some;
49 use rt::io::net::ip::Ipv4Addr;
50 use super::*;
51
52 #[test]
53 fn dns_smoke_test() {
54 let ipaddrs = get_host_addresses("localhost").unwrap();
55 let mut found_local = false;
56 let local_addr = &Ipv4Addr(127, 0, 0, 1);
57 for addr in ipaddrs.iter() {
58 found_local = found_local || addr == local_addr;
59 }
60 assert!(found_local);
61 }
62 }