Skip to content

Commit 8de3fab

Browse files
committed
auto merge of #11732 : luqmana/rust/native-getaddrinfo, r=alexcrichton
The last bit I needed to be able to use libnative :P
2 parents a1d9d9e + adb5128 commit 8de3fab

File tree

7 files changed

+190
-100
lines changed

7 files changed

+190
-100
lines changed

src/libnative/io/addrinfo.rs

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use ai = std::io::net::addrinfo;
12+
use std::c_str::CString;
13+
use std::cast;
14+
use std::io::IoError;
15+
use std::libc;
16+
use std::libc::{c_char, c_int};
17+
use std::ptr::null;
18+
19+
use super::net::sockaddr_to_addr;
20+
21+
pub struct GetAddrInfoRequest;
22+
23+
impl GetAddrInfoRequest {
24+
pub fn run(host: Option<&str>, servname: Option<&str>,
25+
hint: Option<ai::Hint>) -> Result<~[ai::Info], IoError> {
26+
assert!(host.is_some() || servname.is_some());
27+
28+
let c_host = host.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
29+
let c_serv = servname.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
30+
31+
let hint = hint.map(|hint| {
32+
libc::addrinfo {
33+
ai_flags: hint.flags as c_int,
34+
ai_family: hint.family as c_int,
35+
ai_socktype: 0,
36+
ai_protocol: 0,
37+
ai_addrlen: 0,
38+
ai_canonname: null(),
39+
ai_addr: null(),
40+
ai_next: null()
41+
}
42+
});
43+
44+
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
45+
let res = null();
46+
47+
// Make the call
48+
let s = unsafe {
49+
let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
50+
let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
51+
getaddrinfo(ch, cs, hint_ptr, &res)
52+
};
53+
54+
// Error?
55+
if s != 0 {
56+
return Err(get_error(s));
57+
}
58+
59+
// Collect all the results we found
60+
let mut addrs = ~[];
61+
let mut rp = res;
62+
while rp.is_not_null() {
63+
unsafe {
64+
let addr = match sockaddr_to_addr(cast::transmute((*rp).ai_addr),
65+
(*rp).ai_addrlen as uint) {
66+
Ok(a) => a,
67+
Err(e) => return Err(e)
68+
};
69+
addrs.push(ai::Info {
70+
address: addr,
71+
family: (*rp).ai_family as uint,
72+
socktype: None,
73+
protocol: None,
74+
flags: (*rp).ai_flags as uint
75+
});
76+
77+
rp = (*rp).ai_next;
78+
}
79+
}
80+
81+
unsafe { freeaddrinfo(res); }
82+
83+
Ok(addrs)
84+
}
85+
}
86+
87+
extern "system" {
88+
fn getaddrinfo(node: *c_char, service: *c_char,
89+
hints: *libc::addrinfo, res: **libc::addrinfo) -> c_int;
90+
fn freeaddrinfo(res: *libc::addrinfo);
91+
#[cfg(not(windows))]
92+
fn gai_strerror(errcode: c_int) -> *c_char;
93+
#[cfg(windows)]
94+
fn WSAGetLastError() -> c_int;
95+
}
96+
97+
#[cfg(windows)]
98+
fn get_error(_: c_int) -> IoError {
99+
use super::translate_error;
100+
101+
unsafe {
102+
translate_error(WSAGetLastError() as i32, true)
103+
}
104+
}
105+
106+
#[cfg(not(windows))]
107+
fn get_error(s: c_int) -> IoError {
108+
use std::io;
109+
use std::str::raw::from_c_str;
110+
111+
let err_str = unsafe { from_c_str(gai_strerror(s)) };
112+
IoError {
113+
kind: io::OtherIoError,
114+
desc: "unable to resolve host",
115+
detail: Some(err_str),
116+
}
117+
}

src/libnative/io/mod.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,29 @@
2323
2424
use std::c_str::CString;
2525
use std::comm::SharedChan;
26+
use std::io;
27+
use std::io::IoError;
28+
use std::io::net::ip::SocketAddr;
29+
use std::io::process::ProcessConfig;
30+
use std::io::signal::Signum;
2631
use std::libc::c_int;
2732
use std::libc;
2833
use std::os;
2934
use std::rt::rtio;
3035
use std::rt::rtio::{RtioTcpStream, RtioTcpListener, RtioUdpSocket,
3136
RtioUnixListener, RtioPipe, RtioFileStream, RtioProcess,
3237
RtioSignal, RtioTTY, CloseBehavior, RtioTimer};
33-
use std::io;
34-
use std::io::IoError;
35-
use std::io::net::ip::SocketAddr;
36-
use std::io::process::ProcessConfig;
37-
use std::io::signal::Signum;
3838
use ai = std::io::net::addrinfo;
3939

4040
// Local re-exports
4141
pub use self::file::FileDesc;
4242
pub use self::process::Process;
4343

4444
// Native I/O implementations
45+
pub mod addrinfo;
4546
pub mod file;
46-
pub mod process;
4747
pub mod net;
48+
pub mod process;
4849

4950
#[cfg(target_os = "macos")]
5051
#[cfg(target_os = "freebsd")]
@@ -202,9 +203,9 @@ impl rtio::IoFactory for IoFactory {
202203
fn unix_connect(&mut self, _path: &CString) -> IoResult<~RtioPipe> {
203204
Err(unimpl())
204205
}
205-
fn get_host_addresses(&mut self, _host: Option<&str>, _servname: Option<&str>,
206-
_hint: Option<ai::Hint>) -> IoResult<~[ai::Info]> {
207-
Err(unimpl())
206+
fn get_host_addresses(&mut self, host: Option<&str>, servname: Option<&str>,
207+
hint: Option<ai::Hint>) -> IoResult<~[ai::Info]> {
208+
addrinfo::GetAddrInfoRequest::run(host, servname, hint)
208209
}
209210

210211
// filesystem operations

src/libnative/io/net.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ fn sockname(fd: sock_t,
134134
return sockaddr_to_addr(&storage, len as uint);
135135
}
136136

137-
fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
138-
len: uint) -> IoResult<ip::SocketAddr> {
137+
pub fn sockaddr_to_addr(storage: &libc::sockaddr_storage,
138+
len: uint) -> IoResult<ip::SocketAddr> {
139139
match storage.ss_family as libc::c_int {
140140
libc::AF_INET => {
141141
assert!(len as uint >= mem::size_of::<libc::sockaddr_in>());

src/librustuv/addrinfo.rs

+5-40
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use ai = std::io::net::addrinfo;
1212
use std::cast;
13+
use std::libc;
1314
use std::libc::c_int;
1415
use std::ptr::null;
1516
use std::rt::task::BlockedTask;
@@ -19,7 +20,7 @@ use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
1920
use uvll;
2021

2122
struct Addrinfo {
22-
handle: *uvll::addrinfo,
23+
handle: *libc::addrinfo,
2324
}
2425

2526
struct Ctx {
@@ -62,7 +63,7 @@ impl GetAddrInfoRequest {
6263
let socktype = 0;
6364
let protocol = 0;
6465

65-
uvll::addrinfo {
66+
libc::addrinfo {
6667
ai_flags: flags,
6768
ai_family: hint.family as c_int,
6869
ai_socktype: socktype,
@@ -73,7 +74,7 @@ impl GetAddrInfoRequest {
7374
ai_next: null(),
7475
}
7576
});
76-
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *uvll::addrinfo);
77+
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
7778
let mut req = Request::new(uvll::UV_GETADDRINFO);
7879

7980
return match unsafe {
@@ -100,7 +101,7 @@ impl GetAddrInfoRequest {
100101

101102
extern fn getaddrinfo_cb(req: *uvll::uv_getaddrinfo_t,
102103
status: c_int,
103-
res: *uvll::addrinfo) {
104+
res: *libc::addrinfo) {
104105
let req = Request::wrap(req);
105106
assert!(status != uvll::ECANCELED);
106107
let cx: &mut Ctx = unsafe { req.get_data() };
@@ -182,39 +183,3 @@ pub fn accum_addrinfo(addr: &Addrinfo) -> ~[ai::Info] {
182183
return addrs;
183184
}
184185
}
185-
186-
// cannot give tcp/ip permission without help of apk
187-
#[cfg(test, not(target_os="android"))]
188-
mod test {
189-
use std::io::net::ip::{SocketAddr, Ipv4Addr};
190-
use super::super::local_loop;
191-
use super::GetAddrInfoRequest;
192-
193-
#[test]
194-
fn getaddrinfo_test() {
195-
let loop_ = &mut local_loop().loop_;
196-
match GetAddrInfoRequest::run(loop_, Some("localhost"), None, None) {
197-
Ok(infos) => {
198-
let mut found_local = false;
199-
let local_addr = &SocketAddr {
200-
ip: Ipv4Addr(127, 0, 0, 1),
201-
port: 0
202-
};
203-
for addr in infos.iter() {
204-
found_local = found_local || addr.address == *local_addr;
205-
}
206-
assert!(found_local);
207-
}
208-
Err(e) => fail!("{:?}", e),
209-
}
210-
}
211-
212-
#[test]
213-
fn issue_10663() {
214-
let loop_ = &mut local_loop().loop_;
215-
// Something should happen here, but this certainly shouldn't cause
216-
// everything to die. The actual outcome we don't care too much about.
217-
GetAddrInfoRequest::run(loop_, Some("irc.n0v4.com"), None,
218-
None);
219-
}
220-
}

src/librustuv/uvll.rs

+1-40
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#[allow(non_camel_case_types)]; // C types
3131

3232
use std::libc::{size_t, c_int, c_uint, c_void, c_char, c_double};
33-
use std::libc::{ssize_t, sockaddr, free};
33+
use std::libc::{ssize_t, sockaddr, free, addrinfo};
3434
use std::libc;
3535
use std::rt::global_heap::malloc_raw;
3636

@@ -249,45 +249,6 @@ pub type uv_signal_cb = extern "C" fn(handle: *uv_signal_t,
249249
signum: c_int);
250250
pub type uv_fs_cb = extern "C" fn(req: *uv_fs_t);
251251

252-
// XXX: This is a standard C type. Could probably be defined in libc
253-
#[cfg(target_os = "android")]
254-
#[cfg(target_os = "linux")]
255-
pub struct addrinfo {
256-
ai_flags: c_int,
257-
ai_family: c_int,
258-
ai_socktype: c_int,
259-
ai_protocol: c_int,
260-
ai_addrlen: libc::socklen_t,
261-
ai_addr: *sockaddr,
262-
ai_canonname: *char,
263-
ai_next: *addrinfo
264-
}
265-
266-
#[cfg(target_os = "macos")]
267-
#[cfg(target_os = "freebsd")]
268-
pub struct addrinfo {
269-
ai_flags: c_int,
270-
ai_family: c_int,
271-
ai_socktype: c_int,
272-
ai_protocol: c_int,
273-
ai_addrlen: libc::socklen_t,
274-
ai_canonname: *char,
275-
ai_addr: *sockaddr,
276-
ai_next: *addrinfo
277-
}
278-
279-
#[cfg(windows)]
280-
pub struct addrinfo {
281-
ai_flags: c_int,
282-
ai_family: c_int,
283-
ai_socktype: c_int,
284-
ai_protocol: c_int,
285-
ai_addrlen: size_t,
286-
ai_canonname: *char,
287-
ai_addr: *sockaddr,
288-
ai_next: *addrinfo
289-
}
290-
291252
#[cfg(unix)] pub type uv_uid_t = libc::types::os::arch::posix88::uid_t;
292253
#[cfg(unix)] pub type uv_gid_t = libc::types::os::arch::posix88::gid_t;
293254
#[cfg(windows)] pub type uv_uid_t = libc::c_uchar;

src/libstd/io/net/addrinfo.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,27 @@ fn lookup(hostname: Option<&str>, servname: Option<&str>, hint: Option<Hint>)
9898
LocalIo::maybe_raise(|io| io.get_host_addresses(hostname, servname, hint))
9999
}
100100

101-
#[cfg(test)]
101+
// Ignored on android since we cannot give tcp/ip
102+
// permission without help of apk
103+
#[cfg(test, not(target_os = "android"))]
102104
mod test {
103105
use io::net::ip::Ipv4Addr;
104106
use prelude::*;
105107
use super::*;
106108

107-
#[test]
108-
#[ignore(cfg(target_os="android"))] // cannot give tcp/ip permission without help of apk
109-
fn dns_smoke_test() {
109+
iotest!(fn dns_smoke_test() {
110110
let ipaddrs = get_host_addresses("localhost").unwrap();
111111
let mut found_local = false;
112112
let local_addr = &Ipv4Addr(127, 0, 0, 1);
113113
for addr in ipaddrs.iter() {
114114
found_local = found_local || addr == local_addr;
115115
}
116116
assert!(found_local);
117-
}
117+
})
118+
119+
iotest!(fn issue_10663() {
120+
// Something should happen here, but this certainly shouldn't cause
121+
// everything to die. The actual outcome we don't care too much about.
122+
get_host_addresses("example.com");
123+
} #[ignore])
118124
}

0 commit comments

Comments
 (0)