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.24 2006/06/07 22:24:46 momjian Exp $
21 *-------------------------------------------------------------------------
24 /* This is intended to be used in both frontend and backend, so use c.h */
27 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
32 #include "getaddrinfo.h"
37 * The native routines may or may not exist on the Windows platform we are on,
38 * so we dynamically look up the routines, and call them via function pointers.
39 * Here we need to declare what the function pointers look like
41 typedef int (__stdcall * getaddrinfo_ptr_t) (const char *nodename,
43 const struct addrinfo * hints,
44 struct addrinfo ** res);
46 typedef void (__stdcall * freeaddrinfo_ptr_t) (struct addrinfo * ai);
48 typedef int (__stdcall * getnameinfo_ptr_t) (const struct sockaddr * sa,
50 char *host, int hostlen,
51 char *serv, int servlen,
54 /* static pointers to the native routines, so we only do the lookup once. */
55 static getaddrinfo_ptr_t getaddrinfo_ptr = NULL;
56 static freeaddrinfo_ptr_t freeaddrinfo_ptr = NULL;
57 static getnameinfo_ptr_t getnameinfo_ptr = NULL;
61 haveNativeWindowsIPv6routines(void)
63 void *hLibrary = NULL;
64 static bool alreadyLookedForIpv6routines = false;
66 if (alreadyLookedForIpv6routines)
67 return (getaddrinfo_ptr != NULL);
70 * For Windows XP and Windows 2003 (and longhorn/vista), the IPv6 routines
71 * are present in the WinSock 2 library (ws2_32.dll). Try that first
74 hLibrary = LoadLibraryA("ws2_32");
76 if (hLibrary == NULL || GetProcAddress(hLibrary, "getaddrinfo") == NULL)
79 * Well, ws2_32 doesn't exist, or more likely doesn't have
83 FreeLibrary(hLibrary);
86 * In Windows 2000, there was only the IPv6 Technology Preview look in
87 * the IPv6 WinSock library (wship6.dll).
90 hLibrary = LoadLibraryA("wship6");
93 /* If hLibrary is null, we couldn't find a dll with functions */
96 /* We found a dll, so now get the addresses of the routines */
98 getaddrinfo_ptr = (getaddrinfo_ptr_t) GetProcAddress(hLibrary,
100 freeaddrinfo_ptr = (freeaddrinfo_ptr_t) GetProcAddress(hLibrary,
102 getnameinfo_ptr = (getnameinfo_ptr_t) GetProcAddress(hLibrary,
106 * If any one of the routines is missing, let's play it safe and
109 if (getaddrinfo_ptr == NULL ||
110 freeaddrinfo_ptr == NULL ||
111 getnameinfo_ptr == NULL)
113 FreeLibrary(hLibrary);
115 getaddrinfo_ptr = NULL;
116 freeaddrinfo_ptr = NULL;
117 getnameinfo_ptr = NULL;
121 alreadyLookedForIpv6routines = true;
122 return (getaddrinfo_ptr != NULL);
128 * get address info for ipv4 sockets.
130 * Bugs: - only one addrinfo is set even though hintp is NULL or
132 * - AI_CANONNAME is not supported.
133 * - servname can only be a number, not text.
136 getaddrinfo(const char *node, const char *service,
137 const struct addrinfo * hintp,
138 struct addrinfo ** res)
141 struct sockaddr_in sin,
143 struct addrinfo hints;
148 * If Windows has native IPv6 support, use the native Windows routine.
149 * Otherwise, fall through and use our own code.
151 if (haveNativeWindowsIPv6routines())
152 return (*getaddrinfo_ptr) (node, service, hintp, res);
157 memset(&hints, 0, sizeof(hints));
158 hints.ai_family = AF_INET;
159 hints.ai_socktype = SOCK_STREAM;
162 memcpy(&hints, hintp, sizeof(hints));
164 if (hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC)
167 if (hints.ai_socktype == 0)
168 hints.ai_socktype = SOCK_STREAM;
170 if (!node && !service)
173 memset(&sin, 0, sizeof(sin));
175 sin.sin_family = AF_INET;
180 sin.sin_addr.s_addr = htonl(INADDR_ANY);
181 else if (hints.ai_flags & AI_NUMERICHOST)
183 if (!inet_aton(node, &sin.sin_addr))
191 struct hostent hpstr;
195 pqGethostbyname(node, &hpstr, buf, sizeof(buf),
198 hp = gethostbyname(node);
214 if (hp->h_addrtype != AF_INET)
217 memcpy(&(sin.sin_addr), hp->h_addr, hp->h_length);
222 if (hints.ai_flags & AI_PASSIVE)
223 sin.sin_addr.s_addr = htonl(INADDR_ANY);
225 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
229 sin.sin_port = htons((unsigned short) atoi(service));
231 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
232 sin.sin_len = sizeof(sin);
235 ai = malloc(sizeof(*ai));
239 psin = malloc(sizeof(*psin));
246 memcpy(psin, &sin, sizeof(*psin));
249 ai->ai_family = AF_INET;
250 ai->ai_socktype = hints.ai_socktype;
251 ai->ai_protocol = hints.ai_protocol;
252 ai->ai_addrlen = sizeof(*psin);
253 ai->ai_addr = (struct sockaddr *) psin;
254 ai->ai_canonname = NULL;
264 freeaddrinfo(struct addrinfo * res)
271 * If Windows has native IPv6 support, use the native Windows routine.
272 * Otherwise, fall through and use our own code.
274 if (haveNativeWindowsIPv6routines())
276 (*freeaddrinfo_ptr) (res);
289 gai_strerror(int errcode)
291 #ifdef HAVE_HSTRERROR
297 hcode = HOST_NOT_FOUND;
308 return hstrerror(hcode);
309 #else /* !HAVE_HSTRERROR */
314 return "Unknown host";
316 return "Host name lookup failure";
317 /* Errors below are probably WIN32 only */
320 return "Invalid argument";
324 return "Address family not supported";
328 return "Not enough memory";
331 #ifndef WIN32_ONLY_COMPILER /* MSVC complains because another case has the same value */
333 return "No host data of that type was found";
338 return "Class type not found";
342 return "Socket type not supported";
345 return "Unknown server error";
347 #endif /* HAVE_HSTRERROR */
351 * Convert an ipv4 address to a hostname.
353 * Bugs: - Only supports NI_NUMERICHOST and NI_NUMERICSERV
354 * It will never resolv a hostname.
358 getnameinfo(const struct sockaddr * sa, int salen,
359 char *node, int nodelen,
360 char *service, int servicelen, int flags)
365 * If Windows has native IPv6 support, use the native Windows routine.
366 * Otherwise, fall through and use our own code.
368 if (haveNativeWindowsIPv6routines())
369 return (*getnameinfo_ptr) (sa, salen, node, nodelen,
370 service, servicelen, flags);
373 /* Invalid arguments. */
374 if (sa == NULL || (node == NULL && service == NULL))
377 /* We don't support those. */
378 if ((node && !(flags & NI_NUMERICHOST))
379 || (service && !(flags & NI_NUMERICSERV)))
383 if (sa->sa_family == AF_INET6)
391 if (sa->sa_family == AF_INET)
395 p = inet_ntoa(((struct sockaddr_in *) sa)->sin_addr);
396 ret = snprintf(node, nodelen, "%s", p);
398 if (ret == -1 || ret > nodelen)
406 if (sa->sa_family == AF_INET)
408 ret = snprintf(service, servicelen, "%d",
409 ntohs(((struct sockaddr_in *) sa)->sin_port));
411 if (ret == -1 || ret > servicelen)