about summary refs log tree commit diff
path: root/src/libstd/old_io/net/addrinfo.rs
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-01-22 16:27:48 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-01-26 16:01:16 -0800
commitf72b1645103e12b581f7022b893c37b5fe41aef7 (patch)
tree91f98ee9b6a33110445bcf73d172e16c99042863 /src/libstd/old_io/net/addrinfo.rs
parent8ec3a833d5082a77e74a30c2d3d353ba7f5df644 (diff)
downloadrust-f72b1645103e12b581f7022b893c37b5fe41aef7.tar.gz
rust-f72b1645103e12b581f7022b893c37b5fe41aef7.zip
std: Rename io to old_io
In preparation for the I/O rejuvination of the standard library, this commit
renames the current `io` module to `old_io` in order to make room for the new
I/O modules. It is expected that the I/O RFCs will land incrementally over time
instead of all at once, and this provides a fresh clean path for new modules to
enter into as well as guaranteeing that all old infrastructure will remain in
place for some time.

As each `old_io` module is replaced it will be deprecated in-place for new
structures in `std::{io, fs, net}` (as appropriate).

This commit does *not* leave a reexport of `old_io as io` as the deprecation
lint does not currently warn on this form of use. This is quite a large breaking
change for all imports in existing code, but all functionality is retained
precisely as-is and path statements simply need to be renamed from `io` to
`old_io`.

[breaking-change]
Diffstat (limited to 'src/libstd/old_io/net/addrinfo.rs')
-rw-r--r--src/libstd/old_io/net/addrinfo.rs137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/libstd/old_io/net/addrinfo.rs b/src/libstd/old_io/net/addrinfo.rs
new file mode 100644
index 00000000000..9800cc6829e
--- /dev/null
+++ b/src/libstd/old_io/net/addrinfo.rs
@@ -0,0 +1,137 @@
+// 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 or reverse lookup,
+//! in a style related to `getaddrinfo()` and `getnameinfo()`, respectively.
+
+#![allow(missing_docs)]
+
+pub use self::SocketType::*;
+pub use self::Flag::*;
+pub use self::Protocol::*;
+
+use iter::IteratorExt;
+use old_io::{IoResult};
+use old_io::net::ip::{SocketAddr, IpAddr};
+use option::Option;
+use option::Option::{Some, None};
+use string::String;
+use sys;
+use vec::Vec;
+
+/// Hints to the types of sockets that are desired when looking up hosts
+#[derive(Copy, Show)]
+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`
+#[derive(Copy, Show)]
+pub enum Flag {
+    AddrConfig,
+    All,
+    CanonName,
+    NumericHost,
+    NumericServ,
+    Passive,
+    V4Mapped,
+}
+
+/// A transport protocol associated with either a hint or a return value of
+/// `lookup`
+#[derive(Copy, Show)]
+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`
+#[derive(Copy, Show)]
+pub struct Hint {
+    pub family: uint,
+    pub socktype: Option<SocketType>,
+    pub protocol: Option<Protocol>,
+    pub flags: uint,
+}
+
+#[derive(Copy, Show)]
+pub struct Info {
+    pub address: SocketAddr,
+    pub family: uint,
+    pub socktype: Option<SocketType>,
+    pub protocol: Option<Protocol>,
+    pub flags: uint,
+}
+
+/// Easy name resolution. Given a hostname, returns the list of IP addresses for
+/// that hostname.
+pub fn get_host_addresses(host: &str) -> IoResult<Vec<IpAddr>> {
+    lookup(Some(host), None, None).map(|a| a.into_iter().map(|i| i.address.ip).collect())
+}
+
+/// Reverse name resolution. Given an address, returns the corresponding
+/// hostname.
+pub fn get_address_name(addr: IpAddr) -> IoResult<String> {
+    sys::addrinfo::get_address_name(addr)
+}
+
+/// Full-fledged 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
+///
+/// FIXME: this is not public because the `Hint` structure is not ready for public
+///      consumption just yet.
+#[allow(unused_variables)]
+fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
+          -> IoResult<Vec<Info>> {
+    sys::addrinfo::get_host_addresses(hostname, servname, hint)
+}
+
+// Ignored on android since we cannot give tcp/ip
+// permission without help of apk
+#[cfg(all(test, not(target_os = "android")))]
+mod test {
+    use prelude::v1::*;
+    use super::*;
+    use old_io::net::ip::*;
+
+    #[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);
+    }
+
+    #[ignore]
+    #[test]
+    fn issue_10663() {
+        // Something should happen here, but this certainly shouldn't cause
+        // everything to die. The actual outcome we don't care too much about.
+        get_host_addresses("example.com").unwrap();
+    }
+}