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.
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 ai = std::io::net::addrinfo;
12 use std::c_str::CString;
13 use std::cast;
14 use std::io::IoError;
15 use libc;
16 use libc::{c_char, c_int};
17 use std::ptr::{null, mut_null};
18
19 use super::net::sockaddr_to_addr;
20
21 pub struct GetAddrInfoRequest;
22
23 impl GetAddrInfoRequest {
24 pub fn run(host: Option<&str>, servname: Option<&str>,
25 hint: Option<ai::Hint>) -> Result<Vec<ai::Info>, IoError> {
26 assert!(host.is_some() || servname.is_some());
27
28 let c_host = host.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
29 let c_serv = servname.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
30
31 let hint = hint.map(|hint| {
32 libc::addrinfo {
33 ai_flags: hint.flags as c_int,
34 ai_family: hint.family as c_int,
35 ai_socktype: 0,
36 ai_protocol: 0,
37 ai_addrlen: 0,
38 ai_canonname: null(),
39 ai_addr: null(),
40 ai_next: null()
41 }
42 });
43
44 let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
45 let mut res = mut_null();
46
47 // Make the call
48 let s = unsafe {
49 let ch = if c_host.is_null() { null() } else { c_host.with_ref(|x| x) };
50 let cs = if c_serv.is_null() { null() } else { c_serv.with_ref(|x| x) };
51 getaddrinfo(ch, cs, hint_ptr, &mut res)
52 };
53
54 // Error?
55 if s != 0 {
56 return Err(get_error(s));
57 }
58
59 // Collect all the results we found
60 let mut addrs = Vec::new();
61 let mut rp = res;
62 while rp.is_not_null() {
63 unsafe {
64 let addr = match sockaddr_to_addr(cast::transmute((*rp).ai_addr),
65 (*rp).ai_addrlen as uint) {
66 Ok(a) => a,
67 Err(e) => return Err(e)
68 };
69 addrs.push(ai::Info {
70 address: addr,
71 family: (*rp).ai_family as uint,
72 socktype: None,
73 protocol: None,
74 flags: (*rp).ai_flags as uint
75 });
76
77 rp = (*rp).ai_next as *mut libc::addrinfo;
78 }
79 }
80
81 unsafe { freeaddrinfo(res); }
82
83 Ok(addrs)
84 }
85 }
86
87 extern "system" {
88 fn getaddrinfo(node: *c_char, service: *c_char,
89 hints: *libc::addrinfo, res: *mut *mut libc::addrinfo) -> c_int;
90 fn freeaddrinfo(res: *mut libc::addrinfo);
91 #[cfg(not(windows))]
92 fn gai_strerror(errcode: c_int) -> *c_char;
93 #[cfg(windows)]
94 fn WSAGetLastError() -> c_int;
95 }
96
97 #[cfg(windows)]
98 fn get_error(_: c_int) -> IoError {
99 unsafe {
100 IoError::from_errno(WSAGetLastError() as uint, true)
101 }
102 }
103
104 #[cfg(not(windows))]
105 fn get_error(s: c_int) -> IoError {
106 use std::io;
107 use std::str::raw::from_c_str;
108
109 let err_str = unsafe { from_c_str(gai_strerror(s)) };
110 IoError {
111 kind: io::OtherIoError,
112 desc: "unable to resolve host",
113 detail: Some(err_str),
114 }
115 }