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.
|
|
|
|
|
2014-02-26 11:58:41 -06:00
|
|
|
use libc::c_int;
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
use libc;
|
|
|
|
use std::mem;
|
2013-11-05 00:52:33 -06:00
|
|
|
use std::ptr::null;
|
2013-12-12 19:47:48 -06:00
|
|
|
use std::rt::task::BlockedTask;
|
2014-06-04 02:00:59 -05:00
|
|
|
use std::rt::rtio;
|
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 {
|
2014-01-22 14:38:19 -06:00
|
|
|
handle: *libc::addrinfo,
|
2013-11-05 00:52:33 -06:00
|
|
|
}
|
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>,
|
2014-06-04 02:00:59 -05:00
|
|
|
hints: Option<rtio::AddrinfoHint>)
|
|
|
|
-> Result<Vec<rtio::AddrinfoInfo>, 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| {
|
2014-01-22 14:38:19 -06:00
|
|
|
libc::addrinfo {
|
2014-06-04 02:00:59 -05:00
|
|
|
ai_flags: 0,
|
2013-10-22 17:13:18 -05:00
|
|
|
ai_family: hint.family as c_int,
|
2014-06-04 02:00:59 -05:00
|
|
|
ai_socktype: 0,
|
|
|
|
ai_protocol: 0,
|
2013-10-22 17:13:18 -05:00
|
|
|
ai_addrlen: 0,
|
|
|
|
ai_canonname: null(),
|
|
|
|
ai_addr: null(),
|
|
|
|
ai_next: null(),
|
|
|
|
}
|
|
|
|
});
|
2014-01-22 14:38:19 -06:00
|
|
|
let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::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
|
|
|
|
2014-02-10 21:59:35 -06:00
|
|
|
wait_until_woken_after(&mut cx.slot, loop_, || {
|
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,
|
2014-01-22 14:38:19 -06:00
|
|
|
res: *libc::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Traverse the addrinfo linked list, producing a vector of Rust socket addresses
|
2014-06-04 02:00:59 -05:00
|
|
|
pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<rtio::AddrinfoInfo> {
|
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
|
|
|
|
2014-04-17 17:59:07 -05:00
|
|
|
let mut addrs = Vec::new();
|
2013-10-22 17:13:18 -05:00
|
|
|
loop {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 12:34:51 -05:00
|
|
|
let rustaddr = net::sockaddr_to_addr(mem::transmute((*addr).ai_addr),
|
2014-01-17 21:33:29 -06:00
|
|
|
(*addr).ai_addrlen as uint);
|
2013-10-22 17:13:18 -05:00
|
|
|
|
2014-06-04 02:00:59 -05:00
|
|
|
addrs.push(rtio::AddrinfoInfo {
|
2013-10-22 17:13:18 -05:00
|
|
|
address: rustaddr,
|
|
|
|
family: (*addr).ai_family as uint,
|
2014-06-04 02:00:59 -05:00
|
|
|
socktype: 0,
|
|
|
|
protocol: 0,
|
|
|
|
flags: 0,
|
2013-10-22 17:13:18 -05:00
|
|
|
});
|
|
|
|
if (*addr).ai_next.is_not_null() {
|
|
|
|
addr = (*addr).ai_next;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-04 02:44:19 -05:00
|
|
|
addrs
|
2013-10-22 17:13:18 -05:00
|
|
|
}
|
|
|
|
}
|