Skip to content

Commit 0e5052c

Browse files
committed
rollup merge of rust-lang#23419: murarth/lookup-addr
Closes rust-lang#22608
2 parents 242ed0b + c0dd239 commit 0e5052c

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

src/libstd/net/addr.rs

+9
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ pub struct SocketAddrV4 { inner: libc::sockaddr_in }
4747
pub struct SocketAddrV6 { inner: libc::sockaddr_in6 }
4848

4949
impl SocketAddr {
50+
/// Creates a new socket address from the (ip, port) pair.
51+
#[unstable(feature = "ip_addr", reason = "recent addition")]
52+
pub fn new(ip: IpAddr, port: u16) -> SocketAddr {
53+
match ip {
54+
IpAddr::V4(a) => SocketAddr::V4(SocketAddrV4::new(a, port)),
55+
IpAddr::V6(a) => SocketAddr::V6(SocketAddrV6::new(a, port, 0, 0)),
56+
}
57+
}
58+
5059
/// Gets the IP address associated with this socket address.
5160
#[unstable(feature = "ip_addr", reason = "recent addition")]
5261
pub fn ip(&self) -> IpAddr {

src/libstd/net/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,13 @@ impl Iterator for LookupHost {
111111
pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
112112
net_imp::lookup_host(host).map(LookupHost)
113113
}
114+
115+
/// Resolve the given address to a hostname.
116+
///
117+
/// This function may perform a DNS query to resolve `addr` and may also inspect
118+
/// system configuration to resolve the specified address. If the address
119+
/// cannot be resolved, it is returned in string format.
120+
#[unstable(feature = "lookup_addr", reason = "recent addition")]
121+
pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
122+
net_imp::lookup_addr(addr)
123+
}

src/libstd/sys/common/net2.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010

1111
use prelude::v1::*;
1212

13-
use ffi::CString;
13+
use ffi::{CStr, CString};
1414
use io::{self, Error, ErrorKind};
1515
use libc::{self, c_int, c_char, c_void, socklen_t};
1616
use mem;
1717
use net::{SocketAddr, Shutdown, IpAddr};
18+
use str::from_utf8;
1819
use sys::c;
1920
use sys::net::{cvt, cvt_r, cvt_gai, Socket, init, wrlen_t};
2021
use sys_common::{AsInner, FromInner, IntoInner};
@@ -126,6 +127,42 @@ pub fn lookup_host(host: &str) -> io::Result<LookupHost> {
126127
}
127128
}
128129

130+
////////////////////////////////////////////////////////////////////////////////
131+
// lookup_addr
132+
////////////////////////////////////////////////////////////////////////////////
133+
134+
extern "system" {
135+
fn getnameinfo(sa: *const libc::sockaddr, salen: socklen_t,
136+
host: *mut c_char, hostlen: libc::size_t,
137+
serv: *mut c_char, servlen: libc::size_t,
138+
flags: c_int) -> c_int;
139+
}
140+
141+
const NI_MAXHOST: usize = 1025;
142+
143+
pub fn lookup_addr(addr: &IpAddr) -> io::Result<String> {
144+
init();
145+
146+
let saddr = SocketAddr::new(*addr, 0);
147+
let (inner, len) = saddr.into_inner();
148+
let mut hostbuf = [0 as c_char; NI_MAXHOST];
149+
150+
let data = unsafe {
151+
try!(cvt_gai(getnameinfo(inner, len,
152+
hostbuf.as_mut_ptr(), NI_MAXHOST as libc::size_t,
153+
0 as *mut _, 0, 0)));
154+
155+
CStr::from_ptr(hostbuf.as_ptr())
156+
};
157+
158+
match from_utf8(data.to_bytes()) {
159+
Ok(name) => Ok(name.to_string()),
160+
Err(_) => Err(io::Error::new(io::ErrorKind::Other,
161+
"failed to lookup address information",
162+
Some("invalid host name".to_string())))
163+
}
164+
}
165+
129166
////////////////////////////////////////////////////////////////////////////////
130167
// TCP streams
131168
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)