Skip to content

Commit e6c8b8f

Browse files
committed
Added get_address_name, an interface to getnameinfo
1 parent 3e6b29f commit e6c8b8f

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};
@@ -290,6 +291,43 @@ pub fn get_host_addresses(host: Option<&str>, servname: Option<&str>,
290291
Ok(addrs)
291292
}
292293

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

src/libstd/sys/unix/mod.rs

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

5757
pub mod addrinfo {
5858
pub use sys_common::net::get_host_addresses;
59+
pub use sys_common::net::get_address_name;
5960
}
6061

6162
// 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
@@ -57,6 +57,7 @@ pub mod udp;
5757

5858
pub mod addrinfo {
5959
pub use sys_common::net::get_host_addresses;
60+
pub use sys_common::net::get_address_name;
6061
}
6162

6263
// FIXME: move these to c module

0 commit comments

Comments
 (0)