Skip to content

Commit adb5128

Browse files
committed
libnative: Avoid gai_strerror on windows.
1 parent a04cc4d commit adb5128

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/libnative/io/addrinfo.rs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,10 @@
1111
use ai = std::io::net::addrinfo;
1212
use std::c_str::CString;
1313
use std::cast;
14-
use std::io;
1514
use std::io::IoError;
1615
use std::libc;
1716
use std::libc::{c_char, c_int};
1817
use std::ptr::null;
19-
use std::str::raw::from_c_str;
2018

2119
use super::net::sockaddr_to_addr;
2220

@@ -55,13 +53,7 @@ impl GetAddrInfoRequest {
5553

5654
// Error?
5755
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));
6557
}
6658

6759
// Collect all the results we found
@@ -92,9 +84,34 @@ impl GetAddrInfoRequest {
9284
}
9385
}
9486

95-
extern {
87+
extern "system" {
9688
fn getaddrinfo(node: *c_char, service: *c_char,
9789
hints: *libc::addrinfo, res: **libc::addrinfo) -> c_int;
98-
fn gai_strerror(errcode: c_int) -> *c_char;
9990
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+
}
100117
}

0 commit comments

Comments
 (0)