1 // Copyright 2013 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.
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.
14 use std::ptr::{null, mut_null};
15 use std::rt::task::BlockedTask;
19 use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
23 handle: *const libc::addrinfo,
27 slot: Option<BlockedTask>,
29 addrinfo: Option<Addrinfo>,
32 pub struct GetAddrInfoRequest;
34 impl GetAddrInfoRequest {
35 pub fn run(loop_: &Loop, node: Option<&str>, service: Option<&str>,
36 hints: Option<rtio::AddrinfoHint>)
37 -> Result<Vec<rtio::AddrinfoInfo>, UvError>
39 assert!(node.is_some() || service.is_some());
40 let (_c_node, c_node_ptr) = match node {
42 let c_node = n.to_c_str();
43 let c_node_ptr = c_node.as_ptr();
44 (Some(c_node), c_node_ptr)
46 None => (None, null())
49 let (_c_service, c_service_ptr) = match service {
51 let c_service = s.to_c_str();
52 let c_service_ptr = c_service.as_ptr();
53 (Some(c_service), c_service_ptr)
55 None => (None, null())
58 let hint = hints.map(|hint| {
61 ai_family: hint.family as c_int,
65 ai_canonname: mut_null(),
70 let hint_ptr = hint.as_ref().map_or(null(), |x| {
71 x as *const libc::addrinfo
73 let mut req = Request::new(uvll::UV_GETADDRINFO);
76 uvll::uv_getaddrinfo(loop_.handle, req.handle,
77 getaddrinfo_cb, c_node_ptr, c_service_ptr,
81 req.defuse(); // uv callback now owns this request
82 let mut cx = Ctx { slot: None, status: 0, addrinfo: None };
84 wait_until_woken_after(&mut cx.slot, loop_, || {
85 req.set_data(&mut cx);
89 0 => Ok(accum_addrinfo(cx.addrinfo.as_ref().unwrap())),
97 extern fn getaddrinfo_cb(req: *mut uvll::uv_getaddrinfo_t,
99 res: *const libc::addrinfo) {
100 let req = Request::wrap(req);
101 assert!(status != uvll::ECANCELED);
102 let cx: &mut Ctx = unsafe { req.get_data() };
104 cx.addrinfo = Some(Addrinfo { handle: res });
106 wakeup(&mut cx.slot);
111 impl Drop for Addrinfo {
113 unsafe { uvll::uv_freeaddrinfo(self.handle as *mut _) }
117 // Traverse the addrinfo linked list, producing a vector of Rust socket addresses
118 pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<rtio::AddrinfoInfo> {
120 let mut addr = addr.handle;
122 let mut addrs = Vec::new();
124 let rustaddr = net::sockaddr_to_addr(mem::transmute((*addr).ai_addr),
125 (*addr).ai_addrlen as uint);
127 addrs.push(rtio::AddrinfoInfo {
129 family: (*addr).ai_family as uint,
134 if (*addr).ai_next.is_not_null() {
135 addr = (*addr).ai_next as *const _;