1 /*-------------------------------------------------------------------------
4 * Support getaddrinfo() on platforms that don't have it.
6 * We also supply getnameinfo() here, assuming that the platform will have
7 * it if and only if it has getaddrinfo(). If this proves false on some
8 * platform, we'll need to split this file and provide a separate configure
9 * test for getnameinfo().
11 * Windows may or may not have these routines, so we handle Windows special
12 * by dynamically checking for their existence. If they already exist, we
13 * use the Windows native routines, but if not, we use our own.
16 * Copyright (c) 2003-2006, PostgreSQL Global Development Group
19 * $PostgreSQL: pgsql/src/port/getaddrinfo.c,v 1.23 2006/03/05 15:59:10 momjian Exp $
21 *-------------------------------------------------------------------------
24 /* This is intended to be used in both frontend and backend, so use c.h */
27 #ifndef WIN32_CLIENT_ONLY
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
34 #include "getaddrinfo.h"
39 #define WIN32_LEAN_AND_MEAN
43 #if !defined(WIN32_CLIENT_ONLY)
45 * The native routines may or may not exist on the Windows platform we are on,
46 * so we dynamically look up the routines, and call them via function pointers.
47 * Here we need to declare what the function pointers look like
49 typedef int (__stdcall * getaddrinfo_ptr_t) (const char *nodename,
51 const struct addrinfo * hints,
52 struct addrinfo ** res);
54 typedef void (__stdcall * freeaddrinfo_ptr_t) (struct addrinfo * ai);
56 typedef int (__stdcall * getnameinfo_ptr_t) (const struct sockaddr * sa,
58 char *host, int hostlen,
59 char *serv, int servlen,
62 /* static pointers to the native routines, so we only do the lookup once. */
63 static getaddrinfo_ptr_t getaddrinfo_ptr = NULL;
64 static freeaddrinfo_ptr_t freeaddrinfo_ptr = NULL;
65 static getnameinfo_ptr_t getnameinfo_ptr = NULL;
69 haveNativeWindowsIPv6routines(void)
71 void *hLibrary = NULL;
72 static bool alreadyLookedForIpv6routines = false;
74 if (alreadyLookedForIpv6routines)
75 return (getaddrinfo_ptr != NULL);
78 * For Windows XP and Windows 2003 (and longhorn/vista), the IPv6 routines
79 * are present in the WinSock 2 library (ws2_32.dll). Try that first
82 hLibrary = LoadLibraryA("ws2_32");
84 if (hLibrary == NULL || GetProcAddress(hLibrary, "getaddrinfo") == NULL)
87 * Well, ws2_32 doesn't exist, or more likely doesn't have
91 FreeLibrary(hLibrary);
94 * In Windows 2000, there was only the IPv6 Technology Preview look in
95 * the IPv6 WinSock library (wship6.dll).
98 hLibrary = LoadLibraryA("wship6");
101 /* If hLibrary is null, we couldn't find a dll with functions */
102 if (hLibrary != NULL)
104 /* We found a dll, so now get the addresses of the routines */
106 getaddrinfo_ptr = (getaddrinfo_ptr_t) GetProcAddress(hLibrary,
108 freeaddrinfo_ptr = (freeaddrinfo_ptr_t) GetProcAddress(hLibrary,
110 getnameinfo_ptr = (getnameinfo_ptr_t) GetProcAddress(hLibrary,
114 * If any one of the routines is missing, let's play it safe and
117 if (getaddrinfo_ptr == NULL ||
118 freeaddrinfo_ptr == NULL ||
119 getnameinfo_ptr == NULL)
121 FreeLibrary(hLibrary);
123 getaddrinfo_ptr = NULL;
124 freeaddrinfo_ptr = NULL;
125 getnameinfo_ptr = NULL;
129 alreadyLookedForIpv6routines = true;
130 return (getaddrinfo_ptr != NULL);
137 * get address info for ipv4 sockets.
139 * Bugs: - only one addrinfo is set even though hintp is NULL or
141 * - AI_CANONNAME is not supported.
142 * - servname can only be a number, not text.
145 getaddrinfo(const char *node, const char *service,
146 const struct addrinfo * hintp,
147 struct addrinfo ** res)
150 struct sockaddr_in sin,
152 struct addrinfo hints;
154 #if defined(WIN32) && !defined(WIN32_CLIENT_ONLY)
157 * If Windows has native IPv6 support, use the native Windows routine.
158 * Otherwise, fall through and use our own code.
160 if (haveNativeWindowsIPv6routines())
161 return (*getaddrinfo_ptr) (node, service, hintp, res);
166 memset(&hints, 0, sizeof(hints));
167 hints.ai_family = AF_INET;
168 hints.ai_socktype = SOCK_STREAM;
171 memcpy(&hints, hintp, sizeof(hints));
173 if (hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC)
176 if (hints.ai_socktype == 0)
177 hints.ai_socktype = SOCK_STREAM;
179 if (!node && !service)
182 memset(&sin, 0, sizeof(sin));
184 sin.sin_family = AF_INET;
189 sin.sin_addr.s_addr = htonl(INADDR_ANY);
190 else if (hints.ai_flags & AI_NUMERICHOST)
192 if (!inet_aton(node, &sin.sin_addr))
200 struct hostent hpstr;
204 pqGethostbyname(node, &hpstr, buf, sizeof(buf),
207 hp = gethostbyname(node);
223 if (hp->h_addrtype != AF_INET)
226 memcpy(&(sin.sin_addr), hp->h_addr, hp->h_length);
231 if (hints.ai_flags & AI_PASSIVE)
232 sin.sin_addr.s_addr = htonl(INADDR_ANY);
234 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
238 sin.sin_port = htons((unsigned short) atoi(service));
240 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
241 sin.sin_len = sizeof(sin);
244 ai = malloc(sizeof(*ai));
248 psin = malloc(sizeof(*psin));
255 memcpy(psin, &sin, sizeof(*psin));
258 ai->ai_family = AF_INET;
259 ai->ai_socktype = hints.ai_socktype;
260 ai->ai_protocol = hints.ai_protocol;
261 ai->ai_addrlen = sizeof(*psin);
262 ai->ai_addr = (struct sockaddr *) psin;
263 ai->ai_canonname = NULL;
273 freeaddrinfo(struct addrinfo * res)
277 #if defined(WIN32) && !defined(WIN32_CLIENT_ONLY)
280 * If Windows has native IPv6 support, use the native Windows routine.
281 * Otherwise, fall through and use our own code.
283 if (haveNativeWindowsIPv6routines())
285 (*freeaddrinfo_ptr) (res);
298 gai_strerror(int errcode)
300 #ifdef HAVE_HSTRERROR
306 hcode = HOST_NOT_FOUND;
317 return hstrerror(hcode);
318 #else /* !HAVE_HSTRERROR */
323 return "Unknown host";
325 return "Host name lookup failure";
326 /* Errors below are probably WIN32 only */
329 return "Invalid argument";
333 return "Address family not supported";
337 return "Not enough memory";
341 return "No host data of that type was found";
345 return "Class type not found";
349 return "Socket type not supported";
352 return "Unknown server error";
354 #endif /* HAVE_HSTRERROR */
358 * Convert an ipv4 address to a hostname.
360 * Bugs: - Only supports NI_NUMERICHOST and NI_NUMERICSERV
361 * It will never resolv a hostname.
365 getnameinfo(const struct sockaddr * sa, int salen,
366 char *node, int nodelen,
367 char *service, int servicelen, int flags)
369 #if defined(WIN32) && !defined(WIN32_CLIENT_ONLY)
372 * If Windows has native IPv6 support, use the native Windows routine.
373 * Otherwise, fall through and use our own code.
375 if (haveNativeWindowsIPv6routines())
376 return (*getnameinfo_ptr) (sa, salen, node, nodelen,
377 service, servicelen, flags);
380 /* Invalid arguments. */
381 if (sa == NULL || (node == NULL && service == NULL))
384 /* We don't support those. */
385 if ((node && !(flags & NI_NUMERICHOST))
386 || (service && !(flags & NI_NUMERICSERV)))
390 if (sa->sa_family == AF_INET6)
398 if (sa->sa_family == AF_INET)
402 p = inet_ntoa(((struct sockaddr_in *) sa)->sin_addr);
403 ret = snprintf(node, nodelen, "%s", p);
405 if (ret == -1 || ret > nodelen)
413 if (sa->sa_family == AF_INET)
415 ret = snprintf(service, servicelen, "%d",
416 ntohs(((struct sockaddr_in *) sa)->sin_port));
418 if (ret == -1 || ret > servicelen)