|
11 | 11 | use ai = std::io::net::addrinfo;
|
12 | 12 | use std::c_str::CString;
|
13 | 13 | use std::cast;
|
14 |
| -use std::io; |
15 | 14 | use std::io::IoError;
|
16 | 15 | use std::libc;
|
17 | 16 | use std::libc::{c_char, c_int};
|
18 | 17 | use std::ptr::null;
|
19 |
| -use std::str::raw::from_c_str; |
20 | 18 |
|
21 | 19 | use super::net::sockaddr_to_addr;
|
22 | 20 |
|
@@ -55,13 +53,7 @@ impl GetAddrInfoRequest {
|
55 | 53 |
|
56 | 54 | // Error?
|
57 | 55 | if s != 0 {
|
58 |
| - let err_str = unsafe { from_c_str(gai_strerror(s)) }; |
59 |
| - |
60 |
| - return Err(IoError { |
61 |
| - kind: io::OtherIoError, |
62 |
| - desc: "unable to resolve host", |
63 |
| - detail: Some(err_str), |
64 |
| - }); |
| 56 | + return Err(get_error(s)); |
65 | 57 | }
|
66 | 58 |
|
67 | 59 | // Collect all the results we found
|
@@ -92,9 +84,34 @@ impl GetAddrInfoRequest {
|
92 | 84 | }
|
93 | 85 | }
|
94 | 86 |
|
95 |
| -extern { |
| 87 | +extern "system" { |
96 | 88 | fn getaddrinfo(node: *c_char, service: *c_char,
|
97 | 89 | hints: *libc::addrinfo, res: **libc::addrinfo) -> c_int;
|
98 |
| - fn gai_strerror(errcode: c_int) -> *c_char; |
99 | 90 | fn freeaddrinfo(res: *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 | + use super::translate_error; |
| 100 | + |
| 101 | + unsafe { |
| 102 | + translate_error(WSAGetLastError() as i32, true) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +#[cfg(not(windows))] |
| 107 | +fn get_error(s: c_int) -> IoError { |
| 108 | + use std::io; |
| 109 | + use std::str::raw::from_c_str; |
| 110 | + |
| 111 | + let err_str = unsafe { from_c_str(gai_strerror(s)) }; |
| 112 | + IoError { |
| 113 | + kind: io::OtherIoError, |
| 114 | + desc: "unable to resolve host", |
| 115 | + detail: Some(err_str), |
| 116 | + } |
100 | 117 | }
|
0 commit comments