Auto merge of #23419 - murarth:lookup-addr, r=alexcrichton
Closes #22608
This commit is contained in:
commit
0c9de8140b
3 changed files with 57 additions and 1 deletions
|
@ -47,6 +47,15 @@ pub struct SocketAddrV4 { inner: libc::sockaddr_in }
|
||||||
pub struct SocketAddrV6 { inner: libc::sockaddr_in6 }
|
pub struct SocketAddrV6 { inner: libc::sockaddr_in6 }
|
||||||
|
|
||||||
impl SocketAddr {
|
impl SocketAddr {
|
||||||
|
/// Creates a new socket address from the (ip, port) pair.
|
||||||
|
#[unstable(feature = "ip_addr", reason = "recent addition")]
|
||||||
|
pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
|
||||||
|
match ip {
|
||||||
|
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
|
||||||
|
IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the IP address associated with this socket address.
|
/// Gets the IP address associated with this socket address.
|
||||||
#[unstable(feature = "ip_addr", reason = "recent addition")]
|
#[unstable(feature = "ip_addr", reason = "recent addition")]
|
||||||
pub fn ip(&self) -> IpAddr {
|
pub fn ip(&self) -> IpAddr {
|
||||||
|
|
|
@ -111,3 +111,13 @@ impl Iterator for LookupHost {
|
||||||
pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
|
pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
|
||||||
net_imp::lookup_host(host).map(LookupHost)
|
net_imp::lookup_host(host).map(LookupHost)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve the given address to a hostname.
|
||||||
|
///
|
||||||
|
/// This function may perform a DNS query to resolve `addr` and may also inspect
|
||||||
|
/// system configuration to resolve the specified address. If the address
|
||||||
|
/// cannot be resolved, it is returned in string format.
|
||||||
|
#[unstable(feature = "lookup_addr", reason = "recent addition")]
|
||||||
|
pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
|
||||||
|
net_imp::lookup_addr(addr)
|
||||||
|
}
|
||||||
|
|
|
@ -10,11 +10,12 @@
|
||||||
|
|
||||||
use prelude::v1::*;
|
use prelude::v1::*;
|
||||||
|
|
||||||
use ffi::CString;
|
use ffi::{CStr, CString};
|
||||||
use io::{self, Error, ErrorKind};
|
use io::{self, Error, ErrorKind};
|
||||||
use libc::{self, c_int, c_char, c_void, socklen_t};
|
use libc::{self, c_int, c_char, c_void, socklen_t};
|
||||||
use mem;
|
use mem;
|
||||||
use net::{SocketAddr, Shutdown, IpAddr};
|
use net::{SocketAddr, Shutdown, IpAddr};
|
||||||
|
use str::from_utf8;
|
||||||
use sys::c;
|
use sys::c;
|
||||||
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
|
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
|
||||||
use sys_common::{AsInner, FromInner, IntoInner};
|
use sys_common::{AsInner, FromInner, IntoInner};
|
||||||
|
@ -126,6 +127,42 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// lookup_addr
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
extern "system" {
|
||||||
|
fn getnameinfo(sa: *const libc::sockaddr, salen: 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: usize = 1025;
|
||||||
|
|
||||||
|
pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
|
||||||
|
init();
|
||||||
|
|
||||||
|
let saddr = SocketAddr::new(*addr, 0);
|
||||||
|
let (inner, len) = saddr.into_inner();
|
||||||
|
let mut hostbuf = [0 as c_char; NI_MAXHOST];
|
||||||
|
|
||||||
|
let data = unsafe {
|
||||||
|
try!(cvt_gai(getnameinfo(inner, len,
|
||||||
|
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
|
||||||
|
0 as *mut _, 0, 0)));
|
||||||
|
|
||||||
|
CStr::from_ptr(hostbuf.as_ptr())
|
||||||
|
};
|
||||||
|
|
||||||
|
match from_utf8(data.to_bytes()) {
|
||||||
|
Ok(name) => Ok(name.to_string()),
|
||||||
|
Err(_) => Err(io::Error::new(io::ErrorKind::Other,
|
||||||
|
"failed to lookup address information",
|
||||||
|
Some("invalid host name".to_string())))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// TCP streams
|
// TCP streams
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue