about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorMurarth <murarth@gmail.com>2014-12-28 15:44:25 -0700
committerMurarth <murarth@gmail.com>2014-12-28 15:45:43 -0700
commite6c8b8f480712d346c9b92f06217b4c56c15610c (patch)
tree784645a066c92de80a6bdf3f7e8c5ab1adbdd6a2 /src/libstd
parent3e6b29f8ad1ddfcb134d743a66ee5f467e16c350 (diff)
downloadrust-e6c8b8f480712d346c9b92f06217b4c56c15610c.tar.gz
rust-e6c8b8f480712d346c9b92f06217b4c56c15610c.zip
Added `get_address_name`, an interface to `getnameinfo`
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/io/net/addrinfo.rs11
-rw-r--r--src/libstd/sys/common/net.rs38
-rw-r--r--src/libstd/sys/unix/mod.rs1
-rw-r--r--src/libstd/sys/windows/mod.rs1
4 files changed, 49 insertions, 2 deletions
diff --git a/src/libstd/io/net/addrinfo.rs b/src/libstd/io/net/addrinfo.rs
index 69ba64d856e..e8fbb121181 100644
--- a/src/libstd/io/net/addrinfo.rs
+++ b/src/libstd/io/net/addrinfo.rs
@@ -10,8 +10,8 @@
 
 //! Synchronous DNS Resolution
 //!
-//! Contains the functionality to perform DNS resolution in a style related to
-//! `getaddrinfo()`
+//! Contains the functionality to perform DNS resolution or reverse lookup,
+//! in a style related to `getaddrinfo()` and `getnameinfo()`, respectively.
 
 #![allow(missing_docs)]
 
@@ -24,6 +24,7 @@ use io::{IoResult};
 use io::net::ip::{SocketAddr, IpAddr};
 use option::Option;
 use option::Option::{Some, None};
+use string::String;
 use sys;
 use vec::Vec;
 
@@ -83,6 +84,12 @@ 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
 ///
diff --git a/src/libstd/sys/common/net.rs b/src/libstd/sys/common/net.rs
index 382f6875b28..d1373db9a72 100644
--- a/src/libstd/sys/common/net.rs
+++ b/src/libstd/sys/common/net.rs
@@ -13,6 +13,7 @@ use self::InAddr::*;
 
 use alloc::arc::Arc;
 use libc::{mod, c_char, c_int};
+use c_str::CString;
 use mem;
 use num::Int;
 use ptr::{mod, null, null_mut};
@@ -291,6 +292,43 @@ pub fn get_host_addresses(host: Option<&str>, servname: Option<&str>,
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+// get_address_name
+////////////////////////////////////////////////////////////////////////////////
+
+extern "system" {
+    fn getnameinfo(sa: *const libc::sockaddr, salen: libc::socklen_t,
+        host: *mut c_char, hostlen: libc::size_t,
+        serv: *mut c_char, servlen: libc::size_t,
+        flags: c_int) -> c_int;
+}
+
+const NI_MAXHOST: uint = 1025;
+
+pub fn get_address_name(addr: IpAddr) -> Result<String, IoError> {
+    let addr = SocketAddr{ip: addr, port: 0};
+
+    let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
+    let len = addr_to_sockaddr(addr, &mut storage);
+
+    let mut hostbuf = [0 as c_char, ..NI_MAXHOST];
+
+    let res = unsafe {
+        getnameinfo(&storage as *const _ as *const libc::sockaddr, len,
+            hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
+            ptr::null_mut(), 0,
+            0)
+    };
+
+    if res != 0 {
+        return Err(last_gai_error(res));
+    }
+
+    unsafe {
+        Ok(CString::new(hostbuf.as_ptr(), false).as_str().unwrap().to_string())
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
 // Timeout helpers
 //
 // The read/write functions below are the helpers for reading/writing a socket
diff --git a/src/libstd/sys/unix/mod.rs b/src/libstd/sys/unix/mod.rs
index f3babca3287..51526f1f023 100644
--- a/src/libstd/sys/unix/mod.rs
+++ b/src/libstd/sys/unix/mod.rs
@@ -56,6 +56,7 @@ pub mod udp;
 
 pub mod addrinfo {
     pub use sys_common::net::get_host_addresses;
+    pub use sys_common::net::get_address_name;
 }
 
 // FIXME: move these to c module
diff --git a/src/libstd/sys/windows/mod.rs b/src/libstd/sys/windows/mod.rs
index 6924687d8c4..4a8db917387 100644
--- a/src/libstd/sys/windows/mod.rs
+++ b/src/libstd/sys/windows/mod.rs
@@ -57,6 +57,7 @@ pub mod udp;
 
 pub mod addrinfo {
     pub use sys_common::net::get_host_addresses;
+    pub use sys_common::net::get_address_name;
 }
 
 // FIXME: move these to c module