From adb512802057a89b4339651c90dca81e5dd96751 Mon Sep 17 00:00:00 2001
From: Luqman Aden <me@luqman.ca>
Date: Wed, 22 Jan 2014 20:54:36 -0800
Subject: [PATCH] libnative: Avoid gai_strerror on windows.

---
 src/libnative/io/addrinfo.rs | 39 ++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/src/libnative/io/addrinfo.rs b/src/libnative/io/addrinfo.rs
index 3e7ce59b69d..5bdeaa17e74 100644
--- a/src/libnative/io/addrinfo.rs
+++ b/src/libnative/io/addrinfo.rs
@@ -11,12 +11,10 @@
 use ai = std::io::net::addrinfo;
 use std::c_str::CString;
 use std::cast;
-use std::io;
 use std::io::IoError;
 use std::libc;
 use std::libc::{c_char, c_int};
 use std::ptr::null;
-use std::str::raw::from_c_str;
 
 use super::net::sockaddr_to_addr;
 
@@ -55,13 +53,7 @@ impl GetAddrInfoRequest {
 
         // Error?
         if s != 0 {
-            let err_str = unsafe { from_c_str(gai_strerror(s)) };
-
-            return Err(IoError {
-                kind: io::OtherIoError,
-                desc: "unable to resolve host",
-                detail: Some(err_str),
-            });
+            return Err(get_error(s));
         }
 
         // Collect all the results we found
@@ -92,9 +84,34 @@ impl GetAddrInfoRequest {
     }
 }
 
-extern {
+extern "system" {
     fn getaddrinfo(node: *c_char, service: *c_char,
                    hints: *libc::addrinfo, res: **libc::addrinfo) -> c_int;
-    fn gai_strerror(errcode: c_int) -> *c_char;
     fn freeaddrinfo(res: *libc::addrinfo);
+    #[cfg(not(windows))]
+    fn gai_strerror(errcode: c_int) -> *c_char;
+    #[cfg(windows)]
+    fn WSAGetLastError() -> c_int;
+}
+
+#[cfg(windows)]
+fn get_error(_: c_int) -> IoError {
+    use super::translate_error;
+
+    unsafe {
+        translate_error(WSAGetLastError() as i32, true)
+    }
+}
+
+#[cfg(not(windows))]
+fn get_error(s: c_int) -> IoError {
+    use std::io;
+    use std::str::raw::from_c_str;
+
+    let err_str = unsafe { from_c_str(gai_strerror(s)) };
+    IoError {
+        kind: io::OtherIoError,
+        desc: "unable to resolve host",
+        detail: Some(err_str),
+    }
 }