Added get_address_name
, an interface to getnameinfo
This commit is contained in:
parent
3e6b29f8ad
commit
e6c8b8f480
4 changed files with 49 additions and 2 deletions
|
@ -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};
|
||||
|
@ -290,6 +291,43 @@ pub fn get_host_addresses(host: Option<&str>, servname: Option<&str>,
|
|||
Ok(addrs)
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
// 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
|
||||
//
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue