1 // Copyright 2014 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.
11 use libc::{c_char, c_int};
13 use std::c_str::CString;
15 use std::ptr::{null, mut_null};
17 use std::rt::rtio::IoError;
21 pub struct GetAddrInfoRequest;
23 impl GetAddrInfoRequest {
24 pub fn run(host: Option<&str>, servname: Option<&str>,
25 hint: Option<rtio::AddrinfoHint>)
26 -> Result<Vec<rtio::AddrinfoInfo>, IoError>
28 assert!(host.is_some() || servname.is_some());
30 let c_host = host.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
31 let c_serv = servname.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
33 let hint = hint.map(|hint| {
35 ai_flags: hint.flags as c_int,
36 ai_family: hint.family as c_int,
46 let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
47 let mut res = mut_null();
51 let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
52 let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
53 getaddrinfo(ch, cs, hint_ptr, &mut res)
58 return Err(get_error(s));
61 // Collect all the results we found
62 let mut addrs = Vec::new();
64 while rp.is_not_null() {
66 let addr = match net::sockaddr_to_addr(mem::transmute((*rp).ai_addr),
67 (*rp).ai_addrlen as uint) {
69 Err(e) => return Err(e)
71 addrs.push(rtio::AddrinfoInfo {
73 family: (*rp).ai_family as uint,
76 flags: (*rp).ai_flags as uint
79 rp = (*rp).ai_next as *mut libc::addrinfo;
83 unsafe { freeaddrinfo(res); }
90 fn getaddrinfo(node: *c_char, service: *c_char,
91 hints: *libc::addrinfo, res: *mut *mut libc::addrinfo) -> c_int;
92 fn freeaddrinfo(res: *mut libc::addrinfo);
94 fn gai_strerror(errcode: c_int) -> *c_char;
98 fn get_error(_: c_int) -> IoError {
103 fn get_error(s: c_int) -> IoError {
105 let err_str = unsafe {
106 CString::new(gai_strerror(s), false).as_str().unwrap().to_string()
111 detail: Some(err_str),