PostgreSQL Source Code  git master
getaddrinfo.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * getaddrinfo.c
4  * Support getaddrinfo() on platforms that don't have it.
5  *
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().
10  *
11  * Windows may or may not have these routines, so we handle Windows specially
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.
14  *
15  *
16  * Copyright (c) 2003-2017, PostgreSQL Global Development Group
17  *
18  * IDENTIFICATION
19  * src/port/getaddrinfo.c
20  *
21  *-------------------------------------------------------------------------
22  */
23 
24 /* This is intended to be used in both frontend and backend, so use c.h */
25 #include "c.h"
26 
27 #include <sys/socket.h>
28 #include <netdb.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 
32 #include "getaddrinfo.h"
33 #include "libpq/pqcomm.h" /* needed for struct sockaddr_storage */
34 #include "port/pg_bswap.h"
35 
36 
37 #ifdef WIN32
38 /*
39  * The native routines may or may not exist on the Windows platform we are on,
40  * so we dynamically look up the routines, and call them via function pointers.
41  * Here we need to declare what the function pointers look like
42  */
43 typedef int (__stdcall * getaddrinfo_ptr_t) (const char *nodename,
44  const char *servname,
45  const struct addrinfo *hints,
46  struct addrinfo **res);
47 
48 typedef void (__stdcall * freeaddrinfo_ptr_t) (struct addrinfo *ai);
49 
50 typedef int (__stdcall * getnameinfo_ptr_t) (const struct sockaddr *sa,
51  int salen,
52  char *host, int hostlen,
53  char *serv, int servlen,
54  int flags);
55 
56 /* static pointers to the native routines, so we only do the lookup once. */
57 static getaddrinfo_ptr_t getaddrinfo_ptr = NULL;
58 static freeaddrinfo_ptr_t freeaddrinfo_ptr = NULL;
59 static getnameinfo_ptr_t getnameinfo_ptr = NULL;
60 
61 
62 static bool
63 haveNativeWindowsIPv6routines(void)
64 {
65  void *hLibrary = NULL;
66  static bool alreadyLookedForIpv6routines = false;
67 
68  if (alreadyLookedForIpv6routines)
69  return (getaddrinfo_ptr != NULL);
70 
71  /*
72  * For Windows XP and Windows 2003 (and longhorn/vista), the IPv6 routines
73  * are present in the WinSock 2 library (ws2_32.dll). Try that first
74  */
75 
76  hLibrary = LoadLibraryA("ws2_32");
77 
78  if (hLibrary == NULL || GetProcAddress(hLibrary, "getaddrinfo") == NULL)
79  {
80  /*
81  * Well, ws2_32 doesn't exist, or more likely doesn't have
82  * getaddrinfo.
83  */
84  if (hLibrary != NULL)
85  FreeLibrary(hLibrary);
86 
87  /*
88  * In Windows 2000, there was only the IPv6 Technology Preview look in
89  * the IPv6 WinSock library (wship6.dll).
90  */
91 
92  hLibrary = LoadLibraryA("wship6");
93  }
94 
95  /* If hLibrary is null, we couldn't find a dll with functions */
96  if (hLibrary != NULL)
97  {
98  /* We found a dll, so now get the addresses of the routines */
99 
100  getaddrinfo_ptr = (getaddrinfo_ptr_t) GetProcAddress(hLibrary,
101  "getaddrinfo");
102  freeaddrinfo_ptr = (freeaddrinfo_ptr_t) GetProcAddress(hLibrary,
103  "freeaddrinfo");
104  getnameinfo_ptr = (getnameinfo_ptr_t) GetProcAddress(hLibrary,
105  "getnameinfo");
106 
107  /*
108  * If any one of the routines is missing, let's play it safe and
109  * ignore them all
110  */
111  if (getaddrinfo_ptr == NULL ||
112  freeaddrinfo_ptr == NULL ||
113  getnameinfo_ptr == NULL)
114  {
115  FreeLibrary(hLibrary);
116  hLibrary = NULL;
117  getaddrinfo_ptr = NULL;
118  freeaddrinfo_ptr = NULL;
119  getnameinfo_ptr = NULL;
120  }
121  }
122 
123  alreadyLookedForIpv6routines = true;
124  return (getaddrinfo_ptr != NULL);
125 }
126 #endif
127 
128 
129 /*
130  * get address info for ipv4 sockets.
131  *
132  * Bugs: - only one addrinfo is set even though hintp is NULL or
133  * ai_socktype is 0
134  * - AI_CANONNAME is not supported.
135  * - servname can only be a number, not text.
136  */
137 int
138 getaddrinfo(const char *node, const char *service,
139  const struct addrinfo *hintp,
140  struct addrinfo **res)
141 {
142  struct addrinfo *ai;
143  struct sockaddr_in sin,
144  *psin;
145  struct addrinfo hints;
146 
147 #ifdef WIN32
148 
149  /*
150  * If Windows has native IPv6 support, use the native Windows routine.
151  * Otherwise, fall through and use our own code.
152  */
153  if (haveNativeWindowsIPv6routines())
154  return (*getaddrinfo_ptr) (node, service, hintp, res);
155 #endif
156 
157  if (hintp == NULL)
158  {
159  memset(&hints, 0, sizeof(hints));
160  hints.ai_family = AF_INET;
161  hints.ai_socktype = SOCK_STREAM;
162  }
163  else
164  memcpy(&hints, hintp, sizeof(hints));
165 
166  if (hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC)
167  return EAI_FAMILY;
168 
169  if (hints.ai_socktype == 0)
170  hints.ai_socktype = SOCK_STREAM;
171 
172  if (!node && !service)
173  return EAI_NONAME;
174 
175  memset(&sin, 0, sizeof(sin));
176 
177  sin.sin_family = AF_INET;
178 
179  if (node)
180  {
181  if (node[0] == '\0')
182  sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
183  else if (hints.ai_flags & AI_NUMERICHOST)
184  {
185  if (!inet_aton(node, &sin.sin_addr))
186  return EAI_NONAME;
187  }
188  else
189  {
190  struct hostent *hp;
191 
192 #ifdef FRONTEND
193  struct hostent hpstr;
194  char buf[BUFSIZ];
195  int herrno = 0;
196 
197  pqGethostbyname(node, &hpstr, buf, sizeof(buf),
198  &hp, &herrno);
199 #else
200  hp = gethostbyname(node);
201 #endif
202  if (hp == NULL)
203  {
204  switch (h_errno)
205  {
206  case HOST_NOT_FOUND:
207  case NO_DATA:
208  return EAI_NONAME;
209  case TRY_AGAIN:
210  return EAI_AGAIN;
211  case NO_RECOVERY:
212  default:
213  return EAI_FAIL;
214  }
215  }
216  if (hp->h_addrtype != AF_INET)
217  return EAI_FAIL;
218 
219  memcpy(&(sin.sin_addr), hp->h_addr, hp->h_length);
220  }
221  }
222  else
223  {
224  if (hints.ai_flags & AI_PASSIVE)
225  sin.sin_addr.s_addr = pg_hton32(INADDR_ANY);
226  else
227  sin.sin_addr.s_addr = pg_hton32(INADDR_LOOPBACK);
228  }
229 
230  if (service)
231  sin.sin_port = pg_hton16((unsigned short) atoi(service));
232 
233 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
234  sin.sin_len = sizeof(sin);
235 #endif
236 
237  ai = malloc(sizeof(*ai));
238  if (!ai)
239  return EAI_MEMORY;
240 
241  psin = malloc(sizeof(*psin));
242  if (!psin)
243  {
244  free(ai);
245  return EAI_MEMORY;
246  }
247 
248  memcpy(psin, &sin, sizeof(*psin));
249 
250  ai->ai_flags = 0;
251  ai->ai_family = AF_INET;
252  ai->ai_socktype = hints.ai_socktype;
253  ai->ai_protocol = hints.ai_protocol;
254  ai->ai_addrlen = sizeof(*psin);
255  ai->ai_addr = (struct sockaddr *) psin;
256  ai->ai_canonname = NULL;
257  ai->ai_next = NULL;
258 
259  *res = ai;
260 
261  return 0;
262 }
263 
264 
265 void
266 freeaddrinfo(struct addrinfo *res)
267 {
268  if (res)
269  {
270 #ifdef WIN32
271 
272  /*
273  * If Windows has native IPv6 support, use the native Windows routine.
274  * Otherwise, fall through and use our own code.
275  */
276  if (haveNativeWindowsIPv6routines())
277  {
278  (*freeaddrinfo_ptr) (res);
279  return;
280  }
281 #endif
282 
283  if (res->ai_addr)
284  free(res->ai_addr);
285  free(res);
286  }
287 }
288 
289 
290 const char *
292 {
293 #ifdef HAVE_HSTRERROR
294  int hcode;
295 
296  switch (errcode)
297  {
298  case EAI_NONAME:
299  hcode = HOST_NOT_FOUND;
300  break;
301  case EAI_AGAIN:
302  hcode = TRY_AGAIN;
303  break;
304  case EAI_FAIL:
305  default:
306  hcode = NO_RECOVERY;
307  break;
308  }
309 
310  return hstrerror(hcode);
311 #else /* !HAVE_HSTRERROR */
312 
313  switch (errcode)
314  {
315  case EAI_NONAME:
316  return "Unknown host";
317  case EAI_AGAIN:
318  return "Host name lookup failure";
319  /* Errors below are probably WIN32 only */
320 #ifdef EAI_BADFLAGS
321  case EAI_BADFLAGS:
322  return "Invalid argument";
323 #endif
324 #ifdef EAI_FAMILY
325  case EAI_FAMILY:
326  return "Address family not supported";
327 #endif
328 #ifdef EAI_MEMORY
329  case EAI_MEMORY:
330  return "Not enough memory";
331 #endif
332 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME /* MSVC/WIN64 duplicate */
333  case EAI_NODATA:
334  return "No host data of that type was found";
335 #endif
336 #ifdef EAI_SERVICE
337  case EAI_SERVICE:
338  return "Class type not found";
339 #endif
340 #ifdef EAI_SOCKTYPE
341  case EAI_SOCKTYPE:
342  return "Socket type not supported";
343 #endif
344  default:
345  return "Unknown server error";
346  }
347 #endif /* HAVE_HSTRERROR */
348 }
349 
350 /*
351  * Convert an ipv4 address to a hostname.
352  *
353  * Bugs: - Only supports NI_NUMERICHOST and NI_NUMERICSERV behavior.
354  * It will never resolve a hostname.
355  * - No IPv6 support.
356  */
357 int
358 getnameinfo(const struct sockaddr *sa, int salen,
359  char *node, int nodelen,
360  char *service, int servicelen, int flags)
361 {
362 #ifdef WIN32
363 
364  /*
365  * If Windows has native IPv6 support, use the native Windows routine.
366  * Otherwise, fall through and use our own code.
367  */
368  if (haveNativeWindowsIPv6routines())
369  return (*getnameinfo_ptr) (sa, salen, node, nodelen,
370  service, servicelen, flags);
371 #endif
372 
373  /* Invalid arguments. */
374  if (sa == NULL || (node == NULL && service == NULL))
375  return EAI_FAIL;
376 
377 #ifdef HAVE_IPV6
378  if (sa->sa_family == AF_INET6)
379  return EAI_FAMILY;
380 #endif
381 
382  /* Unsupported flags. */
383  if (flags & NI_NAMEREQD)
384  return EAI_AGAIN;
385 
386  if (node)
387  {
388  if (sa->sa_family == AF_INET)
389  {
390  if (inet_net_ntop(AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
391  sa->sa_family == AF_INET ? 32 : 128,
392  node, nodelen) == NULL)
393  return EAI_MEMORY;
394  }
395  else
396  return EAI_MEMORY;
397  }
398 
399  if (service)
400  {
401  int ret = -1;
402 
403  if (sa->sa_family == AF_INET)
404  {
405  ret = snprintf(service, servicelen, "%d",
406  pg_ntoh16(((struct sockaddr_in *) sa)->sin_port));
407  }
408  if (ret == -1 || ret >= servicelen)
409  return EAI_MEMORY;
410  }
411 
412  return 0;
413 }
#define NI_NAMEREQD
Definition: getaddrinfo.h:84
int pqGethostbyname(const char *name, struct hostent *resultbuf, char *buffer, size_t buflen, struct hostent **result, int *herrno)
Definition: thread.c:117
#define pg_hton16(x)
Definition: pg_bswap.h:120
#define EAI_FAIL
Definition: getaddrinfo.h:36
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
const char * gai_strerror(int errcode)
Definition: getaddrinfo.c:291
int errcode(int sqlerrcode)
Definition: elog.c:575
#define EAI_FAMILY
Definition: getaddrinfo.h:37
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void freeaddrinfo(struct addrinfo *res)
Definition: getaddrinfo.c:266
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
#define EAI_BADFLAGS
Definition: getaddrinfo.h:33
#define EAI_SOCKTYPE
Definition: getaddrinfo.h:38
#define malloc(a)
Definition: header.h:50
#define EAI_AGAIN
Definition: getaddrinfo.h:35
#define pg_hton32(x)
Definition: pg_bswap.h:121
char * ai_canonname
Definition: getaddrinfo.h:106
#define EAI_SERVICE
Definition: getaddrinfo.h:39
static char * buf
Definition: pg_test_fsync.c:67
#define AI_PASSIVE
Definition: getaddrinfo.h:62
#define EAI_NONAME
Definition: getaddrinfo.h:34
int getnameinfo(const struct sockaddr *sa, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
Definition: getaddrinfo.c:358
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
int inet_aton(const char *cp, struct in_addr *addr)
Definition: inet_aton.c:56
#define free(a)
Definition: header.h:65
int ai_protocol
Definition: getaddrinfo.h:103
int ai_socktype
Definition: getaddrinfo.h:102
struct addrinfo * ai_next
Definition: getaddrinfo.h:107
int getaddrinfo(const char *node, const char *service, const struct addrinfo *hintp, struct addrinfo **res)
Definition: getaddrinfo.c:138
size_t ai_addrlen
Definition: getaddrinfo.h:104
#define EAI_MEMORY
Definition: getaddrinfo.h:40
int ai_flags
Definition: getaddrinfo.h:100
struct sockaddr * ai_addr
Definition: getaddrinfo.h:105
int ai_family
Definition: getaddrinfo.h:101