Skip to content

Commit e9d83a1

Browse files
committed
---
yaml --- r: 195887 b: refs/heads/beta c: d77a951 h: refs/heads/master i: 195885: bf50657 195883: 2c50017 195879: fe62099 195871: 078b137 v: v3
1 parent b892839 commit e9d83a1

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ refs/tags/0.12.0: f0c419429ef30723ceaf6b42f9b5a2aeb5d2e2d1
2929
refs/heads/automation-fail: 1bf06495443584539b958873e04cc2f864ab10e4
3030
refs/heads/batch: b7fd822592a4fb577552d93010c4a4e14f314346
3131
refs/heads/building: 126db549b038c84269a1e4fe46f051b2c15d6970
32-
refs/heads/beta: 82b375b44d408e7ddd1e3b2c1e64b0bf0c4376d1
32+
refs/heads/beta: d77a9510f617dd68b0cb76beb31c43e98f25c300
3333
refs/heads/windistfix: 7608dbad651f02e837ed05eef3d74a6662a6e928
3434
refs/tags/1.0.0-alpha: e42bd6d93a1d3433c486200587f8f9e12590a4d7
3535
refs/heads/tmp: 9de34a84bb300bab1bf0227f577331620cd60511

branches/beta/src/libstd/net/addr.rs

Lines changed: 9 additions & 0 deletions
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 {

branches/beta/src/libstd/net/mod.rs

Lines changed: 10 additions & 0 deletions
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+
}

branches/beta/src/libstd/sys/common/net2.rs

Lines changed: 38 additions & 1 deletion
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)