Skip to content

Commit b936fb3

Browse files
committed
rollup merge of #20286: murarth/get-address-name
2 parents 2dd5192 + e6c8b8f commit b936fb3

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

src/libstd/io/net/addrinfo.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010

1111
//! Synchronous DNS Resolution
1212
//!
13-
//! Contains the functionality to perform DNS resolution in a style related to
14-
//! `getaddrinfo()`
13+
//! Contains the functionality to perform DNS resolution or reverse lookup,
14+
//! in a style related to `getaddrinfo()` and `getnameinfo()`, respectively.
1515
1616
#![allow(missing_docs)]
1717

@@ -24,6 +24,7 @@ use io::{IoResult};
2424
use io::net::ip::{SocketAddr, IpAddr};
2525
use option::Option;
2626
use option::Option::{Some, None};
27+
use string::String;
2728
use sys;
2829
use vec::Vec;
2930

@@ -83,6 +84,12 @@ pub fn get_host_addresses(host: &str) -> IoResult<Vec<IpAddr>> {
8384
lookup(Some(host), None, None).map(|a| a.into_iter().map(|i| i.address.ip).collect())
8485
}
8586

87+
/// Reverse name resolution. Given an address, returns the corresponding
88+
/// hostname.
89+
pub fn get_address_name(addr: IpAddr) -> IoResult<String> {
90+
sys::addrinfo::get_address_name(addr)
91+
}
92+
8693
/// Full-fledged resolution. This function will perform a synchronous call to
8794
/// getaddrinfo, controlled by the parameters
8895
///

src/libstd/sys/common/net.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use self::InAddr::*;
1313

1414
use alloc::arc::Arc;
1515
use libc::{mod, c_char, c_int};
16+
use c_str::CString;
1617
use mem;
1718
use num::Int;
1819
use ptr::{mod, null, null_mut};
@@ -292,6 +293,43 @@ pub fn get_host_addresses(host: Option<&str>, servname: Option<&str>,
292293
Ok(addrs)
293294
}
294295

296+
////////////////////////////////////////////////////////////////////////////////
297+
// get_address_name
298+
////////////////////////////////////////////////////////////////////////////////
299+
300+
extern "system" {
301+
fn getnameinfo(sa: *const libc::sockaddr, salen: libc::socklen_t,
302+
host: *mut c_char, hostlen: libc::size_t,
303+
serv: *mut c_char, servlen: libc::size_t,
304+
flags: c_int) -> c_int;
305+
}
306+
307+
const NI_MAXHOST: uint = 1025;
308+
309+
pub fn get_address_name(addr: IpAddr) -> Result<String, IoError> {
310+
let addr = SocketAddr{ip: addr, port: 0};
311+
312+
let mut storage: libc::sockaddr_storage = unsafe { mem::zeroed() };
313+
let len = addr_to_sockaddr(addr, &mut storage);
314+
315+
let mut hostbuf = [0 as c_char, ..NI_MAXHOST];
316+
317+
let res = unsafe {
318+
getnameinfo(&storage as *const _ as *const libc::sockaddr, len,
319+
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
320+
ptr::null_mut(), 0,
321+
0)
322+
};
323+
324+
if res != 0 {
325+
return Err(last_gai_error(res));
326+
}
327+
328+
unsafe {
329+
Ok(CString::new(hostbuf.as_ptr(), false).as_str().unwrap().to_string())
330+
}
331+
}
332+
295333
////////////////////////////////////////////////////////////////////////////////
296334
// Timeout helpers
297335
//

src/libstd/sys/unix/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub mod udp;
4040

4141
pub mod addrinfo {
4242
pub use sys_common::net::get_host_addresses;
43+
pub use sys_common::net::get_address_name;
4344
}
4445

4546
// FIXME: move these to c module

src/libstd/sys/windows/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ pub mod udp;
4141

4242
pub mod addrinfo {
4343
pub use sys_common::net::get_host_addresses;
44+
pub use sys_common::net::get_address_name;
4445
}
4546

4647
// FIXME: move these to c module

0 commit comments

Comments
 (0)