2013-10-22 17:13:18 -05:00
|
|
|
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-11-11 00:46:32 -06:00
|
|
|
use ai = std::io::net::addrinfo;
|
2014-01-17 21:33:29 -06:00
|
|
|
use std::cast;
|
2013-11-05 00:52:33 -06:00
|
|
|
use std::libc::c_int;
|
|
|
|
use std::ptr::null;
|
2013-12-12 19:47:48 -06:00
|
|
|
use std::rt::task::BlockedTask;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
use net;
|
2013-12-12 19:47:48 -06:00
|
|
|
use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
|
2013-11-05 00:52:33 -06:00
|
|
|
use uvll;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
struct Addrinfo {
|
|
|
|
handle: *uvll::addrinfo,
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
struct Ctx {
|
|
|
|
slot: Option<BlockedTask>,
|
|
|
|
status: c_int,
|
|
|
|
addrinfo: Option<Addrinfo>,
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
pub struct GetAddrInfoRequest;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 02:27:41 -06:00
|
|
|
impl GetAddrInfoRequest {
|
2013-11-05 00:52:33 -06:00
|
|
|
pub fn run(loop_: &Loop, node: Option<&str>, service: Option<&str>,
|
|
|
|
hints: Option<ai::Hint>) -> Result<~[ai::Info], UvError> {
|
2013-10-22 17:13:18 -05:00
|
|
|
assert!(node.is_some() || service.is_some());
|
2013-11-05 00:52:33 -06:00
|
|
|
let (_c_node, c_node_ptr) = match node {
|
2013-10-22 17:13:18 -05:00
|
|
|
Some(n) => {
|
|
|
|
let c_node = n.to_c_str();
|
|
|
|
let c_node_ptr = c_node.with_ref(|r| r);
|
|
|
|
(Some(c_node), c_node_ptr)
|
|
|
|
}
|
|
|
|
None => (None, null())
|
|
|
|
};
|
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
let (_c_service, c_service_ptr) = match service {
|
2013-10-22 17:13:18 -05:00
|
|
|
Some(s) => {
|
|
|
|
let c_service = s.to_c_str();
|
|
|
|
let c_service_ptr = c_service.with_ref(|r| r);
|
|
|
|
(Some(c_service), c_service_ptr)
|
|
|
|
}
|
|
|
|
None => (None, null())
|
|
|
|
};
|
|
|
|
|
|
|
|
let hint = hints.map(|hint| {
|
|
|
|
let mut flags = 0;
|
2013-11-20 17:46:49 -06:00
|
|
|
each_ai_flag(|cval, aival| {
|
2013-10-22 17:13:18 -05:00
|
|
|
if hint.flags & (aival as uint) != 0 {
|
|
|
|
flags |= cval as i32;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-10-22 17:13:18 -05:00
|
|
|
let socktype = 0;
|
|
|
|
let protocol = 0;
|
|
|
|
|
|
|
|
uvll::addrinfo {
|
|
|
|
ai_flags: flags,
|
|
|
|
ai_family: hint.family as c_int,
|
|
|
|
ai_socktype: socktype,
|
|
|
|
ai_protocol: protocol,
|
|
|
|
ai_addrlen: 0,
|
|
|
|
ai_canonname: null(),
|
|
|
|
ai_addr: null(),
|
|
|
|
ai_next: null(),
|
|
|
|
}
|
|
|
|
});
|
2013-12-06 12:51:10 -06:00
|
|
|
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *uvll::addrinfo);
|
2013-11-07 17:13:06 -06:00
|
|
|
let mut req = Request::new(uvll::UV_GETADDRINFO);
|
2013-11-05 00:52:33 -06:00
|
|
|
|
|
|
|
return match unsafe {
|
2013-11-05 13:29:45 -06:00
|
|
|
uvll::uv_getaddrinfo(loop_.handle, req.handle,
|
2013-11-05 00:52:33 -06:00
|
|
|
getaddrinfo_cb, c_node_ptr, c_service_ptr,
|
|
|
|
hint_ptr)
|
|
|
|
} {
|
|
|
|
0 => {
|
2013-11-07 17:13:06 -06:00
|
|
|
req.defuse(); // uv callback now owns this request
|
2013-11-05 00:52:33 -06:00
|
|
|
let mut cx = Ctx { slot: None, status: 0, addrinfo: None };
|
2013-11-07 17:13:06 -06:00
|
|
|
|
2013-11-20 17:46:49 -06:00
|
|
|
wait_until_woken_after(&mut cx.slot, || {
|
2013-11-07 17:13:06 -06:00
|
|
|
req.set_data(&cx);
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
match cx.status {
|
|
|
|
0 => Ok(accum_addrinfo(cx.addrinfo.get_ref())),
|
|
|
|
n => Err(UvError(n))
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
2013-11-05 00:52:33 -06:00
|
|
|
n => Err(UvError(n))
|
|
|
|
};
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
extern fn getaddrinfo_cb(req: *uvll::uv_getaddrinfo_t,
|
|
|
|
status: c_int,
|
|
|
|
res: *uvll::addrinfo) {
|
2013-11-05 02:27:41 -06:00
|
|
|
let req = Request::wrap(req);
|
2013-11-07 17:13:06 -06:00
|
|
|
assert!(status != uvll::ECANCELED);
|
|
|
|
let cx: &mut Ctx = unsafe { req.get_data() };
|
2013-11-05 00:52:33 -06:00
|
|
|
cx.status = status;
|
|
|
|
cx.addrinfo = Some(Addrinfo { handle: res });
|
|
|
|
|
2013-12-12 19:47:48 -06:00
|
|
|
wakeup(&mut cx.slot);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-05 00:52:33 -06:00
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2013-11-05 00:52:33 -06:00
|
|
|
impl Drop for Addrinfo {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { uvll::uv_freeaddrinfo(self.handle) }
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-19 20:15:10 -06:00
|
|
|
fn each_ai_flag(_f: |c_int, ai::Flag|) {
|
2013-10-22 17:13:18 -05:00
|
|
|
/* XXX: do we really want to support these?
|
|
|
|
unsafe {
|
|
|
|
f(uvll::rust_AI_ADDRCONFIG(), ai::AddrConfig);
|
|
|
|
f(uvll::rust_AI_ALL(), ai::All);
|
|
|
|
f(uvll::rust_AI_CANONNAME(), ai::CanonName);
|
|
|
|
f(uvll::rust_AI_NUMERICHOST(), ai::NumericHost);
|
|
|
|
f(uvll::rust_AI_NUMERICSERV(), ai::NumericServ);
|
|
|
|
f(uvll::rust_AI_PASSIVE(), ai::Passive);
|
|
|
|
f(uvll::rust_AI_V4MAPPED(), ai::V4Mapped);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the addrinfo linked list, producing a vector of Rust socket addresses
|
2013-11-05 00:52:33 -06:00
|
|
|
pub fn accum_addrinfo(addr: &Addrinfo) -> ~[ai::Info] {
|
2013-10-22 17:13:18 -05:00
|
|
|
unsafe {
|
2013-11-05 00:52:33 -06:00
|
|
|
let mut addr = addr.handle;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
let mut addrs = ~[];
|
|
|
|
loop {
|
2014-01-17 21:33:29 -06:00
|
|
|
let rustaddr = net::sockaddr_to_addr(cast::transmute((*addr).ai_addr),
|
|
|
|
(*addr).ai_addrlen as uint);
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
let mut flags = 0;
|
2013-11-20 17:46:49 -06:00
|
|
|
each_ai_flag(|cval, aival| {
|
2013-10-22 17:13:18 -05:00
|
|
|
if (*addr).ai_flags & cval != 0 {
|
|
|
|
flags |= aival as uint;
|
|
|
|
}
|
2013-11-20 17:46:49 -06:00
|
|
|
});
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
/* XXX: do we really want to support these
|
|
|
|
let protocol = match (*addr).ai_protocol {
|
|
|
|
p if p == uvll::rust_IPPROTO_UDP() => Some(ai::UDP),
|
|
|
|
p if p == uvll::rust_IPPROTO_TCP() => Some(ai::TCP),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
let socktype = match (*addr).ai_socktype {
|
|
|
|
p if p == uvll::rust_SOCK_STREAM() => Some(ai::Stream),
|
|
|
|
p if p == uvll::rust_SOCK_DGRAM() => Some(ai::Datagram),
|
|
|
|
p if p == uvll::rust_SOCK_RAW() => Some(ai::Raw),
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
let protocol = None;
|
|
|
|
let socktype = None;
|
|
|
|
|
|
|
|
addrs.push(ai::Info {
|
|
|
|
address: rustaddr,
|
|
|
|
family: (*addr).ai_family as uint,
|
|
|
|
socktype: socktype,
|
|
|
|
protocol: protocol,
|
|
|
|
flags: flags,
|
|
|
|
});
|
|
|
|
if (*addr).ai_next.is_not_null() {
|
|
|
|
addr = (*addr).ai_next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return addrs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-25 23:59:08 -06:00
|
|
|
// cannot give tcp/ip permission without help of apk
|
|
|
|
#[cfg(test, not(target_os="android"))]
|
2013-10-22 17:13:18 -05:00
|
|
|
mod test {
|
2013-11-11 00:46:32 -06:00
|
|
|
use std::io::net::ip::{SocketAddr, Ipv4Addr};
|
2013-11-07 17:13:06 -06:00
|
|
|
use super::super::local_loop;
|
2013-12-13 13:30:59 -06:00
|
|
|
use super::GetAddrInfoRequest;
|
2013-10-22 17:13:18 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn getaddrinfo_test() {
|
2013-12-13 13:30:59 -06:00
|
|
|
let loop_ = &mut local_loop().loop_;
|
|
|
|
match GetAddrInfoRequest::run(loop_, Some("localhost"), None, None) {
|
2013-11-07 17:13:06 -06:00
|
|
|
Ok(infos) => {
|
|
|
|
let mut found_local = false;
|
|
|
|
let local_addr = &SocketAddr {
|
|
|
|
ip: Ipv4Addr(127, 0, 0, 1),
|
|
|
|
port: 0
|
|
|
|
};
|
|
|
|
for addr in infos.iter() {
|
|
|
|
found_local = found_local || addr.address == *local_addr;
|
2013-11-06 13:03:11 -06:00
|
|
|
}
|
2013-11-07 17:13:06 -06:00
|
|
|
assert!(found_local);
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
2013-11-07 17:13:06 -06:00
|
|
|
Err(e) => fail!("{:?}", e),
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|
2013-11-25 23:59:08 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn issue_10663() {
|
2013-12-13 13:30:59 -06:00
|
|
|
let loop_ = &mut local_loop().loop_;
|
2013-11-25 23:59:08 -06:00
|
|
|
// Something should happen here, but this certainly shouldn't cause
|
|
|
|
// everything to die. The actual outcome we don't care too much about.
|
2013-12-13 13:30:59 -06:00
|
|
|
GetAddrInfoRequest::run(loop_, Some("irc.n0v4.com"), None,
|
2013-11-25 23:59:08 -06:00
|
|
|
None);
|
|
|
|
}
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|