]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
ensure that WSAStartup is called before getservbyname
authorMark Andrews <marka@isc.org>
Tue, 15 Jan 2019 03:19:59 +0000 (14:19 +1100)
committerMark Andrews <marka@isc.org>
Tue, 15 Jan 2019 05:29:04 +0000 (16:29 +1100)
(cherry picked from commit ac01359871ae1e80b3915b7744373422ddec7ea1)

lib/irs/getaddrinfo.c

index c828e14b3529b4f3740b8b989960f2e2c494158f..0f22ac514fe1adb333a5db046a36d999de152255 100644 (file)
 #include <string.h>
 #include <errno.h>
 
+#ifdef _WIN32
+#include <windows.h>
+#include <winsock2.h>
+#include <ws2tcpip.h>
+#endif
+
 #include <isc/app.h>
 #include <isc/buffer.h>
 #include <isc/lib.h>
@@ -341,25 +347,45 @@ getaddrinfo(const char *hostname, const char *servname,
 
                port = strtol(servname, &e, 10);
                if (*e == '\0') {
-                       if (socktype == 0)
+                       if (socktype == 0) {
                                return (EAI_SOCKTYPE);
-                       if (port < 0 || port > 65535)
+                       }
+                       if (port < 0 || port > 65535) {
                                return (EAI_SERVICE);
+                       }
                        port = htons((unsigned short) port);
                } else {
+#ifdef _WIN32
+                       WORD wVersionRequested;
+                       WSADATA wsaData;
+
+                       wVersionRequested = MAKEWORD(2, 0);
+
+                       err = WSAStartup(wVersionRequested, &wsaData );
+                       if (err != 0) {
+                               return (EAI_FAIL);
+                       }
+#endif
                        sp = getservbyname(servname, proto);
-                       if (sp == NULL)
+                       if (sp != NULL)
+                               port = sp->s_port;
+#ifdef _WIN32
+                       WSACleanup();
+#endif
+                       if (sp == NULL) {
                                return (EAI_SERVICE);
-                       port = sp->s_port;
+                       }
                        if (socktype == 0) {
-                               if (strcmp(sp->s_proto, "tcp") == 0)
+                               if (strcmp(sp->s_proto, "tcp") == 0) {
                                        socktype = SOCK_STREAM;
-                               else if (strcmp(sp->s_proto, "udp") == 0)
+                               } else if (strcmp(sp->s_proto, "udp") == 0) {
                                        socktype = SOCK_DGRAM;
+                               }
                        }
                }
-       } else
+       } else {
                port = 0;
+       }
 
        /*
         * Next, deal with just a service name, and no hostname.