about summary refs log tree commit diff
path: root/src/libstd/rt/io
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2013-10-15 20:37:39 -0700
committerAlex Crichton <alex@alexcrichton.com>2013-10-24 14:21:56 -0700
commit1db783bdcf05954e066adf6cefbbc5ac72e13173 (patch)
tree3452ba6c392a17b576c8a4c91807e1fb12a68ce7 /src/libstd/rt/io
parentbac96818580a97c049532e50702c2a8204e11754 (diff)
downloadrust-1db783bdcf05954e066adf6cefbbc5ac72e13173.tar.gz
rust-1db783bdcf05954e066adf6cefbbc5ac72e13173.zip
Finish implementing io::net::addrinfo
This fills in the `hints` structure and exposes libuv's full functionality for
doing dns lookups.
Diffstat (limited to 'src/libstd/rt/io')
-rw-r--r--src/libstd/rt/io/net/addrinfo.rs127
-rw-r--r--src/libstd/rt/io/net/mod.rs48
2 files changed, 129 insertions, 46 deletions
diff --git a/src/libstd/rt/io/net/addrinfo.rs b/src/libstd/rt/io/net/addrinfo.rs
new file mode 100644
index 00000000000..ae0d542acbe
--- /dev/null
+++ b/src/libstd/rt/io/net/addrinfo.rs
@@ -0,0 +1,127 @@
+// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/*!
+
+Synchronous DNS Resolution
+
+Contains the functionality to perform DNS resolution in a style related to
+getaddrinfo()
+
+*/
+
+use option::{Option, Some, None};
+use result::{Ok, Err};
+use rt::io::io_error;
+use rt::io::net::ip::{SocketAddr, IpAddr};
+use rt::rtio::{IoFactory, IoFactoryObject};
+use rt::local::Local;
+
+/// Hints to the types of sockets that are desired when looking up hosts
+pub enum SocketType {
+    Stream, Datagram, Raw
+}
+
+/// Flags which can be or'd into the `flags` field of a `Hint`. These are used
+/// to manipulate how a query is performed.
+///
+/// The meaning of each of these flags can be found with `man -s 3 getaddrinfo`
+pub enum Flag {
+    AddrConfig,
+    All,
+    CanonName,
+    NumericHost,
+    NumericServ,
+    Passive,
+    V4Mapped,
+}
+
+/// A transport protocol associated with either a hint or a return value of
+/// `lookup`
+pub enum Protocol {
+    TCP, UDP
+}
+
+/// This structure is used to provide hints when fetching addresses for a
+/// remote host to control how the lookup is performed.
+///
+/// For details on these fields, see their corresponding definitions via
+/// `man -s 3 getaddrinfo`
+pub struct Hint {
+    family: uint,
+    socktype: Option<SocketType>,
+    protocol: Option<Protocol>,
+    flags: uint,
+}
+
+pub struct Info {
+    address: SocketAddr,
+    family: uint,
+    socktype: Option<SocketType>,
+    protocol: Option<Protocol>,
+    flags: uint,
+}
+
+/// Easy name resolution. Given a hostname, returns the list of IP addresses for
+/// that hostname.
+///
+/// # Failure
+///
+/// On failure, this will raise on the `io_error` condition.
+pub fn get_host_addresses(host: &str) -> Option<~[IpAddr]> {
+    lookup(Some(host), None, None).map(|a| a.map(|i| i.address.ip))
+}
+
+/// Full-fleged resolution. This function will perform a synchronous call to
+/// getaddrinfo, controlled by the parameters
+///
+/// # Arguments
+///
+/// * hostname - an optional hostname to lookup against
+/// * servname - an optional service name, listed in the system services
+/// * hint - see the hint structure, and "man -s 3 getaddrinfo", for how this
+///          controls lookup
+///
+/// # Failure
+///
+/// On failure, this will raise on the `io_error` condition.
+pub fn lookup(hostname: Option<&str>, servname: Option<&str>,
+              hint: Option<Hint>) -> Option<~[Info]> {
+    let ipaddrs = unsafe {
+        let io: *mut IoFactoryObject = Local::unsafe_borrow();
+        (*io).get_host_addresses(hostname, servname, hint)
+    };
+
+    match ipaddrs {
+        Ok(i) => Some(i),
+        Err(ioerr) => {
+            io_error::cond.raise(ioerr);
+            None
+        }
+    }
+}
+
+#[cfg(test)]
+mod test {
+    use option::Some;
+    use rt::io::net::ip::Ipv4Addr;
+    use super::*;
+
+    #[test]
+    fn dns_smoke_test() {
+        let ipaddrs = get_host_addresses("localhost").unwrap();
+        let mut found_local = false;
+        let local_addr = &Ipv4Addr(127, 0, 0, 1);
+        for addr in ipaddrs.iter() {
+            found_local = found_local || addr == local_addr;
+        }
+        assert!(found_local);
+    }
+}
diff --git a/src/libstd/rt/io/net/mod.rs b/src/libstd/rt/io/net/mod.rs
index f44e879a63a..cf109167089 100644
--- a/src/libstd/rt/io/net/mod.rs
+++ b/src/libstd/rt/io/net/mod.rs
@@ -8,55 +8,11 @@
 // option. This file may not be copied, modified, or distributed
 // except according to those terms.
 
-use option::{Option, Some, None};
-use result::{Ok, Err};
-use rt::io::io_error;
-use rt::io::net::ip::IpAddr;
-use rt::rtio::{IoFactory, IoFactoryObject};
-use rt::local::Local;
+pub use self::addrinfo::get_host_addresses;
 
+pub mod addrinfo;
 pub mod tcp;
 pub mod udp;
 pub mod ip;
 #[cfg(unix)]
 pub mod unix;
-
-/// Simplistic name resolution
-pub fn get_host_addresses(host: &str) -> Option<~[IpAddr]> {
-    /*!
-     * Get the IP addresses for a given host name.
-     *
-     * Raises io_error on failure.
-     */
-
-    let ipaddrs = unsafe {
-        let io: *mut IoFactoryObject = Local::unsafe_borrow();
-        (*io).get_host_addresses(host)
-    };
-
-    match ipaddrs {
-        Ok(i) => Some(i),
-        Err(ioerr) => {
-            io_error::cond.raise(ioerr);
-            None
-        }
-    }
-}
-
-#[cfg(test)]
-mod test {
-    use option::Some;
-    use rt::io::net::ip::Ipv4Addr;
-    use super::*;
-
-    #[test]
-    fn dns_smoke_test() {
-        let ipaddrs = get_host_addresses("localhost").unwrap();
-        let mut found_local = false;
-        let local_addr = &Ipv4Addr(127, 0, 0, 1);
-        for addr in ipaddrs.iter() {
-            found_local = found_local || addr == local_addr;
-        }
-        assert!(found_local);
-    }
-}