PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
getaddrinfo.c File Reference
#include "c.h"
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "getaddrinfo.h"
#include "libpq/pqcomm.h"
Include dependency graph for getaddrinfo.c:

Go to the source code of this file.

Functions

int getaddrinfo (const char *node, const char *service, const struct addrinfo *hintp, struct addrinfo **res)
 
void freeaddrinfo (struct addrinfo *res)
 
const char * gai_strerror (int errcode)
 
int getnameinfo (const struct sockaddr *sa, int salen, char *node, int nodelen, char *service, int servicelen, int flags)
 

Function Documentation

void freeaddrinfo ( struct addrinfo res)

Definition at line 265 of file getaddrinfo.c.

References addrinfo::ai_addr, and free.

266 {
267  if (res)
268  {
269 #ifdef WIN32
270 
271  /*
272  * If Windows has native IPv6 support, use the native Windows routine.
273  * Otherwise, fall through and use our own code.
274  */
275  if (haveNativeWindowsIPv6routines())
276  {
277  (*freeaddrinfo_ptr) (res);
278  return;
279  }
280 #endif
281 
282  if (res->ai_addr)
283  free(res->ai_addr);
284  free(res);
285  }
286 }
#define free(a)
Definition: header.h:60
struct sockaddr * ai_addr
Definition: getaddrinfo.h:107
const char* gai_strerror ( int  errcode)

Definition at line 290 of file getaddrinfo.c.

References EAI_AGAIN, EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_NONAME, EAI_SERVICE, and EAI_SOCKTYPE.

291 {
292 #ifdef HAVE_HSTRERROR
293  int hcode;
294 
295  switch (errcode)
296  {
297  case EAI_NONAME:
298  hcode = HOST_NOT_FOUND;
299  break;
300  case EAI_AGAIN:
301  hcode = TRY_AGAIN;
302  break;
303  case EAI_FAIL:
304  default:
305  hcode = NO_RECOVERY;
306  break;
307  }
308 
309  return hstrerror(hcode);
310 #else /* !HAVE_HSTRERROR */
311 
312  switch (errcode)
313  {
314  case EAI_NONAME:
315  return "Unknown host";
316  case EAI_AGAIN:
317  return "Host name lookup failure";
318  /* Errors below are probably WIN32 only */
319 #ifdef EAI_BADFLAGS
320  case EAI_BADFLAGS:
321  return "Invalid argument";
322 #endif
323 #ifdef EAI_FAMILY
324  case EAI_FAMILY:
325  return "Address family not supported";
326 #endif
327 #ifdef EAI_MEMORY
328  case EAI_MEMORY:
329  return "Not enough memory";
330 #endif
331 #if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME /* MSVC/WIN64 duplicate */
332  case EAI_NODATA:
333  return "No host data of that type was found";
334 #endif
335 #ifdef EAI_SERVICE
336  case EAI_SERVICE:
337  return "Class type not found";
338 #endif
339 #ifdef EAI_SOCKTYPE
340  case EAI_SOCKTYPE:
341  return "Socket type not supported";
342 #endif
343  default:
344  return "Unknown server error";
345  }
346 #endif /* HAVE_HSTRERROR */
347 }
#define EAI_FAIL
Definition: getaddrinfo.h:36
int errcode(int sqlerrcode)
Definition: elog.c:575
#define EAI_FAMILY
Definition: getaddrinfo.h:37
#define EAI_BADFLAGS
Definition: getaddrinfo.h:33
#define EAI_SOCKTYPE
Definition: getaddrinfo.h:38
#define EAI_AGAIN
Definition: getaddrinfo.h:35
#define EAI_SERVICE
Definition: getaddrinfo.h:39
#define EAI_NONAME
Definition: getaddrinfo.h:34
#define EAI_MEMORY
Definition: getaddrinfo.h:40
int getaddrinfo ( const char *  node,
const char *  service,
const struct addrinfo hintp,
struct addrinfo **  res 
)

Definition at line 137 of file getaddrinfo.c.

References addrinfo::ai_addr, addrinfo::ai_addrlen, addrinfo::ai_canonname, addrinfo::ai_family, addrinfo::ai_flags, addrinfo::ai_next, AI_NUMERICHOST, AI_PASSIVE, addrinfo::ai_protocol, addrinfo::ai_socktype, buf, EAI_AGAIN, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, EAI_NONAME, free, inet_aton(), malloc, NULL, and pqGethostbyname().

140 {
141  struct addrinfo *ai;
142  struct sockaddr_in sin,
143  *psin;
144  struct addrinfo hints;
145 
146 #ifdef WIN32
147 
148  /*
149  * If Windows has native IPv6 support, use the native Windows routine.
150  * Otherwise, fall through and use our own code.
151  */
152  if (haveNativeWindowsIPv6routines())
153  return (*getaddrinfo_ptr) (node, service, hintp, res);
154 #endif
155 
156  if (hintp == NULL)
157  {
158  memset(&hints, 0, sizeof(hints));
159  hints.ai_family = AF_INET;
160  hints.ai_socktype = SOCK_STREAM;
161  }
162  else
163  memcpy(&hints, hintp, sizeof(hints));
164 
165  if (hints.ai_family != AF_INET && hints.ai_family != AF_UNSPEC)
166  return EAI_FAMILY;
167 
168  if (hints.ai_socktype == 0)
169  hints.ai_socktype = SOCK_STREAM;
170 
171  if (!node && !service)
172  return EAI_NONAME;
173 
174  memset(&sin, 0, sizeof(sin));
175 
176  sin.sin_family = AF_INET;
177 
178  if (node)
179  {
180  if (node[0] == '\0')
181  sin.sin_addr.s_addr = htonl(INADDR_ANY);
182  else if (hints.ai_flags & AI_NUMERICHOST)
183  {
184  if (!inet_aton(node, &sin.sin_addr))
185  return EAI_NONAME;
186  }
187  else
188  {
189  struct hostent *hp;
190 
191 #ifdef FRONTEND
192  struct hostent hpstr;
193  char buf[BUFSIZ];
194  int herrno = 0;
195 
196  pqGethostbyname(node, &hpstr, buf, sizeof(buf),
197  &hp, &herrno);
198 #else
199  hp = gethostbyname(node);
200 #endif
201  if (hp == NULL)
202  {
203  switch (h_errno)
204  {
205  case HOST_NOT_FOUND:
206  case NO_DATA:
207  return EAI_NONAME;
208  case TRY_AGAIN:
209  return EAI_AGAIN;
210  case NO_RECOVERY:
211  default:
212  return EAI_FAIL;
213  }
214  }
215  if (hp->h_addrtype != AF_INET)
216  return EAI_FAIL;
217 
218  memcpy(&(sin.sin_addr), hp->h_addr, hp->h_length);
219  }
220  }
221  else
222  {
223  if (hints.ai_flags & AI_PASSIVE)
224  sin.sin_addr.s_addr = htonl(INADDR_ANY);
225  else
226  sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
227  }
228 
229  if (service)
230  sin.sin_port = htons((unsigned short) atoi(service));
231 
232 #ifdef HAVE_STRUCT_SOCKADDR_STORAGE_SS_LEN
233  sin.sin_len = sizeof(sin);
234 #endif
235 
236  ai = malloc(sizeof(*ai));
237  if (!ai)
238  return EAI_MEMORY;
239 
240  psin = malloc(sizeof(*psin));
241  if (!psin)
242  {
243  free(ai);
244  return EAI_MEMORY;
245  }
246 
247  memcpy(psin, &sin, sizeof(*psin));
248 
249  ai->ai_flags = 0;
250  ai->ai_family = AF_INET;
251  ai->ai_socktype = hints.ai_socktype;
252  ai->ai_protocol = hints.ai_protocol;
253  ai->ai_addrlen = sizeof(*psin);
254  ai->ai_addr = (struct sockaddr *) psin;
255  ai->ai_canonname = NULL;
256  ai->ai_next = NULL;
257 
258  *res = ai;
259 
260  return 0;
261 }
int pqGethostbyname(const char *name, struct hostent *resultbuf, char *buffer, size_t buflen, struct hostent **result, int *herrno)
Definition: thread.c:117
#define EAI_FAIL
Definition: getaddrinfo.h:36
#define EAI_FAMILY
Definition: getaddrinfo.h:37
#define AI_NUMERICHOST
Definition: getaddrinfo.h:75
#define malloc(a)
Definition: header.h:45
#define EAI_AGAIN
Definition: getaddrinfo.h:35
char * ai_canonname
Definition: getaddrinfo.h:108
static char * buf
Definition: pg_test_fsync.c:65
#define AI_PASSIVE
Definition: getaddrinfo.h:64
#define EAI_NONAME
Definition: getaddrinfo.h:34
int inet_aton(const char *cp, struct in_addr *addr)
Definition: inet_aton.c:54
#define free(a)
Definition: header.h:60
int ai_protocol
Definition: getaddrinfo.h:105
#define NULL
Definition: c.h:226
int ai_socktype
Definition: getaddrinfo.h:104
struct addrinfo * ai_next
Definition: getaddrinfo.h:109
size_t ai_addrlen
Definition: getaddrinfo.h:106
#define EAI_MEMORY
Definition: getaddrinfo.h:40
int ai_flags
Definition: getaddrinfo.h:102
struct sockaddr * ai_addr
Definition: getaddrinfo.h:107
int ai_family
Definition: getaddrinfo.h:103
int getnameinfo ( const struct sockaddr *  sa,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)

Definition at line 357 of file getaddrinfo.c.

References EAI_AGAIN, EAI_FAIL, EAI_FAMILY, EAI_MEMORY, inet_net_ntop(), NI_NAMEREQD, NULL, and snprintf().

360 {
361 #ifdef WIN32
362 
363  /*
364  * If Windows has native IPv6 support, use the native Windows routine.
365  * Otherwise, fall through and use our own code.
366  */
367  if (haveNativeWindowsIPv6routines())
368  return (*getnameinfo_ptr) (sa, salen, node, nodelen,
369  service, servicelen, flags);
370 #endif
371 
372  /* Invalid arguments. */
373  if (sa == NULL || (node == NULL && service == NULL))
374  return EAI_FAIL;
375 
376 #ifdef HAVE_IPV6
377  if (sa->sa_family == AF_INET6)
378  return EAI_FAMILY;
379 #endif
380 
381  /* Unsupported flags. */
382  if (flags & NI_NAMEREQD)
383  return EAI_AGAIN;
384 
385  if (node)
386  {
387  if (sa->sa_family == AF_INET)
388  {
389  if (inet_net_ntop(AF_INET, &((struct sockaddr_in *) sa)->sin_addr,
390  sa->sa_family == AF_INET ? 32 : 128,
391  node, nodelen) == NULL)
392  return EAI_MEMORY;
393  }
394  else
395  return EAI_MEMORY;
396  }
397 
398  if (service)
399  {
400  int ret = -1;
401 
402  if (sa->sa_family == AF_INET)
403  {
404  ret = snprintf(service, servicelen, "%d",
405  ntohs(((struct sockaddr_in *) sa)->sin_port));
406  }
407  if (ret == -1 || ret >= servicelen)
408  return EAI_MEMORY;
409  }
410 
411  return 0;
412 }
#define NI_NAMEREQD
Definition: getaddrinfo.h:86
#define EAI_FAIL
Definition: getaddrinfo.h:36
#define EAI_FAMILY
Definition: getaddrinfo.h:37
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define EAI_AGAIN
Definition: getaddrinfo.h:35
char * inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:78
#define NULL
Definition: c.h:226
#define EAI_MEMORY
Definition: getaddrinfo.h:40