]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/uv/addrinfo.rs
Upgrade libuv to the current master (again)
[rust.git] / src / libstd / rt / uv / addrinfo.rs
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.
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 cast::transmute;
12 use cell::Cell;
13 use c_str::ToCStr;
14 use libc::{c_int, c_void};
15 use option::{Option, Some, None};
16 use ptr::null;
17 use rt::uv::uvll;
18 use rt::uv::uvll::UV_GETADDRINFO;
19 use rt::uv::{Loop, UvError, NativeHandle};
20 use rt::uv::status_to_maybe_uv_error;
21 use rt::uv::net::UvAddrInfo;
22
23 type GetAddrInfoCallback = ~fn(GetAddrInfoRequest, &UvAddrInfo, Option<UvError>);
24
25 pub struct GetAddrInfoRequest(*uvll::uv_getaddrinfo_t);
26
27 pub struct RequestData {
28     getaddrinfo_cb: Option<GetAddrInfoCallback>,
29 }
30
31 impl GetAddrInfoRequest {
32     pub fn new() -> GetAddrInfoRequest {
33         let req = unsafe { uvll::malloc_req(UV_GETADDRINFO) };
34         assert!(req.is_not_null());
35         let mut req: GetAddrInfoRequest = NativeHandle::from_native_handle(req);
36         req.install_req_data();
37         return req;
38     }
39
40     pub fn getaddrinfo(&mut self, loop_: &Loop, node: Option<&str>,
41                        service: Option<&str>, hints: Option<UvAddrInfo>,
42                        cb: GetAddrInfoCallback) {
43
44         assert!(node.is_some() || service.is_some());
45
46         let (c_node, c_node_ptr) = match node {
47             Some(n) => {
48                 let c_node = n.to_c_str();
49                 let c_node_ptr = c_node.with_ref(|r| r);
50                 (Some(c_node), c_node_ptr)
51             }
52             None => (None, null())
53         };
54
55         let (c_service, c_service_ptr) = match service {
56             Some(s) => {
57                 let c_service = s.to_c_str();
58                 let c_service_ptr = c_service.with_ref(|r| r);
59                 (Some(c_service), c_service_ptr)
60             }
61             None => (None, null())
62         };
63
64         let cb = Cell::new(cb);
65         let wrapper_cb: GetAddrInfoCallback = |req, addrinfo, err| {
66             // Capture some heap values that need to stay alive for the
67             // getaddrinfo call
68             let _ = &c_node;
69             let _ = &c_service;
70
71             let cb = cb.take();
72             cb(req, addrinfo, err)
73         };
74
75         // XXX: Implement hints
76         assert!(hints.is_none());
77
78         self.get_req_data().getaddrinfo_cb = Some(wrapper_cb);
79
80         unsafe {
81             assert!(0 == uvll::getaddrinfo(loop_.native_handle(),
82                                            self.native_handle(),
83                                            getaddrinfo_cb,
84                                            c_node_ptr,
85                                            c_service_ptr,
86                                            null()));
87         }
88
89         extern "C" fn getaddrinfo_cb(req: *uvll::uv_getaddrinfo_t,
90                                      status: c_int,
91                                      res: *uvll::addrinfo) {
92             let mut req: GetAddrInfoRequest = NativeHandle::from_native_handle(req);
93             let err = status_to_maybe_uv_error(status);
94             let addrinfo = UvAddrInfo(res);
95             let data = req.get_req_data();
96             (*data.getaddrinfo_cb.get_ref())(req, &addrinfo, err);
97             unsafe {
98                 uvll::freeaddrinfo(res);
99             }
100         }
101     }
102
103     fn get_loop(&self) -> Loop {
104         unsafe {
105             Loop {
106                 handle: uvll::get_loop_from_fs_req(self.native_handle())
107             }
108         }
109     }
110
111     fn install_req_data(&mut self) {
112         let req = self.native_handle() as *uvll::uv_getaddrinfo_t;
113         let data = ~RequestData {
114             getaddrinfo_cb: None
115         };
116         unsafe {
117             let data = transmute::<~RequestData, *c_void>(data);
118             uvll::set_data_for_req(req, data);
119         }
120     }
121
122     fn get_req_data<'r>(&'r mut self) -> &'r mut RequestData {
123         unsafe {
124             let data = uvll::get_data_for_req(self.native_handle());
125             let data = transmute::<&*c_void, &mut ~RequestData>(&data);
126             return &mut **data;
127         }
128     }
129
130     fn delete(self) {
131         unsafe {
132             let data = uvll::get_data_for_req(self.native_handle());
133             let _data = transmute::<*c_void, ~RequestData>(data);
134             uvll::set_data_for_req(self.native_handle(), null::<()>());
135             uvll::free_req(self.native_handle());
136         }
137     }
138 }
139
140 impl NativeHandle<*uvll::uv_getaddrinfo_t> for GetAddrInfoRequest {
141     fn from_native_handle(handle: *uvll::uv_getaddrinfo_t) -> GetAddrInfoRequest {
142         GetAddrInfoRequest(handle)
143     }
144     fn native_handle(&self) -> *uvll::uv_getaddrinfo_t {
145         match self { &GetAddrInfoRequest(ptr) => ptr }
146     }
147 }
148
149 #[cfg(test)]
150 mod test {
151     use option::{Some, None};
152     use rt::uv::Loop;
153     use rt::uv::net::accum_sockaddrs;
154     use rt::io::net::ip::{SocketAddr, Ipv4Addr};
155     use super::*;
156
157     #[test]
158     fn getaddrinfo_test() {
159         let mut loop_ = Loop::new();
160         let mut req = GetAddrInfoRequest::new();
161         do req.getaddrinfo(&loop_, Some("localhost"), None, None) |_, addrinfo, _| {
162             let sockaddrs = accum_sockaddrs(addrinfo);
163             let mut found_local = false;
164             let local_addr = &SocketAddr {
165                 ip: Ipv4Addr(127, 0, 0, 1),
166                 port: 0
167             };
168             for addr in sockaddrs.iter() {
169                 found_local = found_local || addr == local_addr;
170             }
171             assert!(found_local);
172         }
173         loop_.run();
174         loop_.close();
175         req.delete();
176     }
177 }