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.
15 use std::rt::task::BlockedTask;
19 use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
23 handle: *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.with_ref(|r| r);
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.with_ref(|r| r);
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,
70 let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
71 let mut req = Request::new(uvll::UV_GETADDRINFO);
74 uvll::uv_getaddrinfo(loop_.handle, req.handle,
75 getaddrinfo_cb, c_node_ptr, c_service_ptr,
79 req.defuse(); // uv callback now owns this request
80 let mut cx = Ctx { slot: None, status: 0, addrinfo: None };
82 wait_until_woken_after(&mut cx.slot, loop_, || {
87 0 => Ok(accum_addrinfo(cx.addrinfo.get_ref())),
95 extern fn getaddrinfo_cb(req: *uvll::uv_getaddrinfo_t,
97 res: *libc::addrinfo) {
98 let req = Request::wrap(req);
99 assert!(status != uvll::ECANCELED);
100 let cx: &mut Ctx = unsafe { req.get_data() };
102 cx.addrinfo = Some(Addrinfo { handle: res });
104 wakeup(&mut cx.slot);
109 impl Drop for Addrinfo {
111 unsafe { uvll::uv_freeaddrinfo(self.handle) }
115 // Traverse the addrinfo linked list, producing a vector of Rust socket addresses
116 pub fn accum_addrinfo(addr: &Addrinfo) -> Vec<rtio::AddrinfoInfo> {
118 let mut addr = addr.handle;
120 let mut addrs = Vec::new();
122 let rustaddr = net::sockaddr_to_addr(mem::transmute((*addr).ai_addr),
123 (*addr).ai_addrlen as uint);
125 addrs.push(rtio::AddrinfoInfo {
127 family: (*addr).ai_family as uint,
132 if (*addr).ai_next.is_not_null() {
133 addr = (*addr).ai_next;