]> git.lizzy.rs Git - rust.git/blob - src/libnative/io/addrinfo.rs
255c3f4bd213dd8c510791c6f6adad4dc627bd66
[rust.git] / src / libnative / io / addrinfo.rs
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 libc::{c_char, c_int};
12 use libc;
13 use std::c_str::CString;
14 use std::mem;
15 use std::ptr::{null, mut_null};
16 use std::rt::rtio;
17 use std::rt::rtio::IoError;
18
19 use super::net;
20
21 pub struct GetAddrInfoRequest;
22
23 impl GetAddrInfoRequest {
24     pub fn run(host: Option<&str>, servname: Option<&str>,
25                hint: Option<rtio::AddrinfoHint>)
26         -> Result<Vec<rtio::AddrinfoInfo>, IoError>
27     {
28         assert!(host.is_some() || servname.is_some());
29
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());
32
33         let hint = hint.map(|hint| {
34             libc::addrinfo {
35                 ai_flags: hint.flags as c_int,
36                 ai_family: hint.family as c_int,
37                 ai_socktype: 0,
38                 ai_protocol: 0,
39                 ai_addrlen: 0,
40                 ai_canonname: null(),
41                 ai_addr: null(),
42                 ai_next: null()
43             }
44         });
45
46         let hint_ptr = hint.as_ref().map_or(null(), |x| x as *libc::addrinfo);
47         let mut res = mut_null();
48
49         // Make the call
50         let s = unsafe {
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)
54         };
55
56         // Error?
57         if s != 0 {
58             return Err(get_error(s));
59         }
60
61         // Collect all the results we found
62         let mut addrs = Vec::new();
63         let mut rp = res;
64         while rp.is_not_null() {
65             unsafe {
66                 let addr = match net::sockaddr_to_addr(mem::transmute((*rp).ai_addr),
67                                                        (*rp).ai_addrlen as uint) {
68                     Ok(a) => a,
69                     Err(e) => return Err(e)
70                 };
71                 addrs.push(rtio::AddrinfoInfo {
72                     address: addr,
73                     family: (*rp).ai_family as uint,
74                     socktype: 0,
75                     protocol: 0,
76                     flags: (*rp).ai_flags as uint
77                 });
78
79                 rp = (*rp).ai_next as *mut libc::addrinfo;
80             }
81         }
82
83         unsafe { freeaddrinfo(res); }
84
85         Ok(addrs)
86     }
87 }
88
89 extern "system" {
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);
93     #[cfg(not(windows))]
94     fn gai_strerror(errcode: c_int) -> *c_char;
95 }
96
97 #[cfg(windows)]
98 fn get_error(_: c_int) -> IoError {
99     net::last_error()
100 }
101
102 #[cfg(not(windows))]
103 fn get_error(s: c_int) -> IoError {
104
105     let err_str = unsafe {
106         CString::new(gai_strerror(s), false).as_str().unwrap().to_string()
107     };
108     IoError {
109         code: s as uint,
110         extra: 0,
111         detail: Some(err_str),
112     }
113 }