]> granicus.if.org Git - postgresql/blobdiff - src/port/getaddrinfo.c
Centralize single quote escaping in src/port/quotes.c
[postgresql] / src / port / getaddrinfo.c
index 906e1610807a0b2023a2e51d2b361c43f2e6c019..e2b59efac86edbd144583f410ffbf711f5582596 100644 (file)
@@ -8,11 +8,15 @@
  * platform, we'll need to split this file and provide a separate configure
  * test for getnameinfo().
  *
+ * Windows may or may not have these routines, so we handle Windows specially
+ * by dynamically checking for their existence.  If they already exist, we
+ * use the Windows native routines, but if not, we use our own.
  *
- * Copyright (c) 2003, PostgreSQL Global Development Group
+ *
+ * Copyright (c) 2003-2013, PostgreSQL Global Development Group
  *
  * IDENTIFICATION
- *       $PostgreSQL: pgsql/src/port/getaddrinfo.c,v 1.12 2003/11/29 19:52:13 pgsql Exp $
+ *       src/port/getaddrinfo.c
  *
  *-------------------------------------------------------------------------
  */
 /* This is intended to be used in both frontend and backend, so use c.h */
 #include "c.h"
 
-#if !defined(_MSC_VER) && !defined(__BORLANDC__)
-#include <sys/types.h>
 #include <sys/socket.h>
 #include <netdb.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#endif
 
 #include "getaddrinfo.h"
+#include "libpq/pqcomm.h"              /* needed for struct sockaddr_storage */
+
+
+#ifdef WIN32
+/*
+ * The native routines may or may not exist on the Windows platform we are on,
+ * so we dynamically look up the routines, and call them via function pointers.
+ * Here we need to declare what the function pointers look like
+ */
+typedef int (__stdcall * getaddrinfo_ptr_t) (const char *nodename,
+                                                                                                                const char *servname,
+                                                                                          const struct addrinfo * hints,
+                                                                                                        struct addrinfo ** res);
+
+typedef void (__stdcall * freeaddrinfo_ptr_t) (struct addrinfo * ai);
+
+typedef int (__stdcall * getnameinfo_ptr_t) (const struct sockaddr * sa,
+                                                                                                                int salen,
+                                                                                                        char *host, int hostlen,
+                                                                                                        char *serv, int servlen,
+                                                                                                                int flags);
+
+/* static pointers to the native routines, so we only do the lookup once. */
+static getaddrinfo_ptr_t getaddrinfo_ptr = NULL;
+static freeaddrinfo_ptr_t freeaddrinfo_ptr = NULL;
+static getnameinfo_ptr_t getnameinfo_ptr = NULL;
+
+
+static bool
+haveNativeWindowsIPv6routines(void)
+{
+       void       *hLibrary = NULL;
+       static bool alreadyLookedForIpv6routines = false;
+
+       if (alreadyLookedForIpv6routines)
+               return (getaddrinfo_ptr != NULL);
+
+       /*
+        * For Windows XP and Windows 2003 (and longhorn/vista), the IPv6 routines
+        * are present in the WinSock 2 library (ws2_32.dll). Try that first
+        */
+
+       hLibrary = LoadLibraryA("ws2_32");
+
+       if (hLibrary == NULL || GetProcAddress(hLibrary, "getaddrinfo") == NULL)
+       {
+               /*
+                * Well, ws2_32 doesn't exist, or more likely doesn't have
+                * getaddrinfo.
+                */
+               if (hLibrary != NULL)
+                       FreeLibrary(hLibrary);
+
+               /*
+                * In Windows 2000, there was only the IPv6 Technology Preview look in
+                * the IPv6 WinSock library (wship6.dll).
+                */
+
+               hLibrary = LoadLibraryA("wship6");
+       }
+
+       /* If hLibrary is null, we couldn't find a dll with functions */
+       if (hLibrary != NULL)
+       {
+               /* We found a dll, so now get the addresses of the routines */
+
+               getaddrinfo_ptr = (getaddrinfo_ptr_t) GetProcAddress(hLibrary,
+                                                                                                                        "getaddrinfo");
+               freeaddrinfo_ptr = (freeaddrinfo_ptr_t) GetProcAddress(hLibrary,
+                                                                                                                        "freeaddrinfo");
+               getnameinfo_ptr = (getnameinfo_ptr_t) GetProcAddress(hLibrary,
+                                                                                                                        "getnameinfo");
+
+               /*
+                * If any one of the routines is missing, let's play it safe and
+                * ignore them all
+                */
+               if (getaddrinfo_ptr == NULL ||
+                       freeaddrinfo_ptr == NULL ||
+                       getnameinfo_ptr == NULL)
+               {
+                       FreeLibrary(hLibrary);
+                       hLibrary = NULL;
+                       getaddrinfo_ptr = NULL;
+                       freeaddrinfo_ptr = NULL;
+                       getnameinfo_ptr = NULL;
+               }
+       }
+
+       alreadyLookedForIpv6routines = true;
+       return (getaddrinfo_ptr != NULL);
+}
+#endif
+
 
 /*
  * get address info for ipv4 sockets.
@@ -48,6 +143,16 @@ getaddrinfo(const char *node, const char *service,
                           *psin;
        struct addrinfo hints;
 
+#ifdef WIN32
+
+       /*
+        * If Windows has native IPv6 support, use the native Windows routine.
+        * Otherwise, fall through and use our own code.
+        */
+       if (haveNativeWindowsIPv6routines())
+               return (*getaddrinfo_ptr) (node, service, hintp, res);
+#endif
+
        if (hintp == NULL)
        {
                memset(&hints, 0, sizeof(hints));
@@ -161,6 +266,19 @@ freeaddrinfo(struct addrinfo * res)
 {
        if (res)
        {
+#ifdef WIN32
+
+               /*
+                * If Windows has native IPv6 support, use the native Windows routine.
+                * Otherwise, fall through and use our own code.
+                */
+               if (haveNativeWindowsIPv6routines())
+               {
+                       (*freeaddrinfo_ptr) (res);
+                       return;
+               }
+#endif
+
                if (res->ai_addr)
                        free(res->ai_addr);
                free(res);
@@ -189,7 +307,6 @@ gai_strerror(int errcode)
        }
 
        return hstrerror(hcode);
-
 #else                                                  /* !HAVE_HSTRERROR */
 
        switch (errcode)
@@ -198,7 +315,31 @@ gai_strerror(int errcode)
                        return "Unknown host";
                case EAI_AGAIN:
                        return "Host name lookup failure";
-               case EAI_FAIL:
+                       /* Errors below are probably WIN32 only */
+#ifdef EAI_BADFLAGS
+               case EAI_BADFLAGS:
+                       return "Invalid argument";
+#endif
+#ifdef EAI_FAMILY
+               case EAI_FAMILY:
+                       return "Address family not supported";
+#endif
+#ifdef EAI_MEMORY
+               case EAI_MEMORY:
+                       return "Not enough memory";
+#endif
+#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME            /* MSVC/WIN64 duplicate */
+               case EAI_NODATA:
+                       return "No host data of that type was found";
+#endif
+#ifdef EAI_SERVICE
+               case EAI_SERVICE:
+                       return "Class type not found";
+#endif
+#ifdef EAI_SOCKTYPE
+               case EAI_SOCKTYPE:
+                       return "Socket type not supported";
+#endif
                default:
                        return "Unknown server error";
        }
@@ -217,15 +358,21 @@ getnameinfo(const struct sockaddr * sa, int salen,
                        char *node, int nodelen,
                        char *service, int servicelen, int flags)
 {
+#ifdef WIN32
+
+       /*
+        * If Windows has native IPv6 support, use the native Windows routine.
+        * Otherwise, fall through and use our own code.
+        */
+       if (haveNativeWindowsIPv6routines())
+               return (*getnameinfo_ptr) (sa, salen, node, nodelen,
+                                                                  service, servicelen, flags);
+#endif
+
        /* Invalid arguments. */
        if (sa == NULL || (node == NULL && service == NULL))
                return EAI_FAIL;
 
-       /* We don't support those. */
-       if ((node && !(flags & NI_NUMERICHOST))
-               || (service && !(flags & NI_NUMERICSERV)))
-               return EAI_FAIL;
-
 #ifdef HAVE_IPV6
        if (sa->sa_family == AF_INET6)
                return EAI_FAMILY;
@@ -233,16 +380,14 @@ getnameinfo(const struct sockaddr * sa, int salen,
 
        if (node)
        {
-               int                     ret = -1;
-
                if (sa->sa_family == AF_INET)
                {
-                       char       *p;
-
-                       p = inet_ntoa(((struct sockaddr_in *) sa)->sin_addr);
-                       ret = snprintf(node, nodelen, "%s", p);
+                       if (inet_net_ntop(AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
+                                                         sa->sa_family == AF_INET ? 32 : 128,
+                                                         node, nodelen) == NULL)
+                               return EAI_MEMORY;
                }
-               if (ret == -1 || ret > nodelen)
+               else
                        return EAI_MEMORY;
        }