|
| 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_with_loop; |
| 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 loop_ = req.get_loop(); |
| 94 | + let err = status_to_maybe_uv_error_with_loop(loop_.native_handle(), status); |
| 95 | + let addrinfo = UvAddrInfo(res); |
| 96 | + let data = req.get_req_data(); |
| 97 | + (*data.getaddrinfo_cb.get_ref())(req, &addrinfo, err); |
| 98 | + unsafe { |
| 99 | + uvll::freeaddrinfo(res); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + fn get_loop(&self) -> Loop { |
| 105 | + unsafe { |
| 106 | + Loop { |
| 107 | + handle: uvll::get_loop_from_fs_req(self.native_handle()) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + fn install_req_data(&mut self) { |
| 113 | + let req = self.native_handle() as *uvll::uv_getaddrinfo_t; |
| 114 | + let data = ~RequestData { |
| 115 | + getaddrinfo_cb: None |
| 116 | + }; |
| 117 | + unsafe { |
| 118 | + let data = transmute::<~RequestData, *c_void>(data); |
| 119 | + uvll::set_data_for_req(req, data); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + fn get_req_data<'r>(&'r mut self) -> &'r mut RequestData { |
| 124 | + unsafe { |
| 125 | + let data = uvll::get_data_for_req(self.native_handle()); |
| 126 | + let data = transmute::<&*c_void, &mut ~RequestData>(&data); |
| 127 | + return &mut **data; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + fn delete(self) { |
| 132 | + unsafe { |
| 133 | + let data = uvll::get_data_for_req(self.native_handle()); |
| 134 | + let _data = transmute::<*c_void, ~RequestData>(data); |
| 135 | + uvll::set_data_for_req(self.native_handle(), null::<()>()); |
| 136 | + uvll::free_req(self.native_handle()); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl NativeHandle<*uvll::uv_getaddrinfo_t> for GetAddrInfoRequest { |
| 142 | + fn from_native_handle(handle: *uvll::uv_getaddrinfo_t) -> GetAddrInfoRequest { |
| 143 | + GetAddrInfoRequest(handle) |
| 144 | + } |
| 145 | + fn native_handle(&self) -> *uvll::uv_getaddrinfo_t { |
| 146 | + match self { &GetAddrInfoRequest(ptr) => ptr } |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +#[cfg(test)] |
| 151 | +mod test { |
| 152 | + use option::{Some, None}; |
| 153 | + use rt::uv::Loop; |
| 154 | + use rt::uv::net::accum_sockaddrs; |
| 155 | + use rt::io::net::ip::{SocketAddr, Ipv4Addr}; |
| 156 | + use super::*; |
| 157 | + |
| 158 | + #[test] |
| 159 | + fn getaddrinfo_test() { |
| 160 | + let mut loop_ = Loop::new(); |
| 161 | + let mut req = GetAddrInfoRequest::new(); |
| 162 | + do req.getaddrinfo(&loop_, Some("localhost"), None, None) |_, addrinfo, _| { |
| 163 | + let sockaddrs = accum_sockaddrs(addrinfo); |
| 164 | + let mut found_local = false; |
| 165 | + let local_addr = &SocketAddr { |
| 166 | + ip: Ipv4Addr(127, 0, 0, 1), |
| 167 | + port: 0 |
| 168 | + }; |
| 169 | + for addr in sockaddrs.iter() { |
| 170 | + found_local = found_local || addr == local_addr; |
| 171 | + } |
| 172 | + assert!(found_local); |
| 173 | + } |
| 174 | + loop_.run(); |
| 175 | + loop_.close(); |
| 176 | + req.delete(); |
| 177 | + } |
| 178 | +} |
0 commit comments