Make sure getaddrinfo and getnameinfo works as intended on Windows
[openssl.git] / crypto / bio / b_addr.c
index 8f94fe8ffb118a663d1bb9a4097659b34990b310..20cb66dce0cc10d2237b8a2757568bd0edbad2b8 100644 (file)
@@ -214,6 +214,9 @@ unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap)
 static int addr_strings(const BIO_ADDR *ap, int numeric,
                         char **hostname, char **service)
 {
+    if (BIO_sock_init() != 1)
+        return 0;
+
     if (1) {
 #ifdef AI_PASSIVE
         int ret = 0;
@@ -633,7 +636,11 @@ int BIO_lookup(const char *host, const char *service,
     }
 #endif
 
+    if (BIO_sock_init() != 1)
+        return 0;
+
     if (1) {
+        int gai_ret = 0;
 #ifdef AI_PASSIVE
         struct addrinfo hints;
 
@@ -655,7 +662,7 @@ int BIO_lookup(const char *host, const char *service,
         /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to
          * macro magic in bio_lcl.h
          */
-        switch (getaddrinfo(host, service, &hints, res)) {
+        switch ((gai_ret = getaddrinfo(host, service, &hints, res))) {
 # ifdef EAI_SYSTEM
         case EAI_SYSTEM:
             SYSerr(SYS_F_GETADDRINFO, get_last_socket_error());
@@ -667,7 +674,7 @@ int BIO_lookup(const char *host, const char *service,
             break;
         default:
             BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
-            ERR_add_error_data(1, gai_strerror(ret));
+            ERR_add_error_data(1, gai_strerror(gai_ret));
             break;
         }
     } else {
@@ -715,8 +722,12 @@ int BIO_lookup(const char *host, const char *service,
             he = gethostbyname(host);
 
             if (he == NULL) {
+#ifndef OPENSSL_SYS_WINDOWS
                 BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
                 ERR_add_error_data(1, hstrerror(h_errno));
+#else
+                SYSerr(SYS_F_GETHOSTBYNAME, WSAGetLastError());
+#endif
                 ret = 0;
                 goto err;
             }
@@ -747,8 +758,12 @@ int BIO_lookup(const char *host, const char *service,
                 se = getservbyname(service, proto);
 
                 if (se == NULL) {
+#ifndef OPENSSL_SYS_WINDOWS
                     BIOerr(BIO_F_BIO_LOOKUP, ERR_R_SYS_LIB);
                     ERR_add_error_data(1, hstrerror(h_errno));
+#else
+                    SYSerr(SYS_F_GETSERVBYNAME, WSAGetLastError());
+#endif
                     goto err;
                 }
             } else {