PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
getaddrinfo.h File Reference
#include <sys/socket.h>
#include <netdb.h>
Include dependency graph for getaddrinfo.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  addrinfo
 

Macros

#define EAI_BADFLAGS   (-1)
 
#define EAI_NONAME   (-2)
 
#define EAI_AGAIN   (-3)
 
#define EAI_FAIL   (-4)
 
#define EAI_FAMILY   (-6)
 
#define EAI_SOCKTYPE   (-7)
 
#define EAI_SERVICE   (-8)
 
#define EAI_MEMORY   (-10)
 
#define EAI_SYSTEM   (-11)
 
#define AI_PASSIVE   0x0001
 
#define AI_NUMERICHOST   0x0004
 
#define NI_NUMERICHOST   1
 
#define NI_NUMERICSERV   2
 
#define NI_NAMEREQD   4
 
#define NI_MAXHOST   1025
 
#define NI_MAXSERV   32
 
#define getaddrinfo   pg_getaddrinfo
 
#define freeaddrinfo   pg_freeaddrinfo
 
#define gai_strerror   pg_gai_strerror
 
#define getnameinfo   pg_getnameinfo
 

Functions

int getaddrinfo (const char *node, const char *service, const struct addrinfo *hints, 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)
 

Macro Definition Documentation

#define AI_NUMERICHOST   0x0004

Definition at line 73 of file getaddrinfo.h.

Referenced by connectDBStart(), getaddrinfo(), ident_inet(), parse_hba_line(), and setup_config().

#define AI_PASSIVE   0x0001

Definition at line 62 of file getaddrinfo.h.

Referenced by getaddrinfo(), pgstat_init(), and StreamServerPort().

#define EAI_AGAIN   (-3)

Definition at line 35 of file getaddrinfo.h.

Referenced by gai_strerror(), getaddrinfo(), and getnameinfo().

#define EAI_BADFLAGS   (-1)

Definition at line 33 of file getaddrinfo.h.

Referenced by gai_strerror().

#define EAI_FAIL   (-4)

Definition at line 36 of file getaddrinfo.h.

Referenced by gai_strerror(), getaddrinfo(), and getnameinfo().

#define EAI_FAMILY   (-6)

Definition at line 37 of file getaddrinfo.h.

Referenced by gai_strerror(), getaddrinfo(), and getnameinfo().

#define EAI_MEMORY   (-10)

Definition at line 40 of file getaddrinfo.h.

Referenced by gai_strerror(), getaddrinfo(), and getnameinfo().

#define EAI_NONAME   (-2)

Definition at line 34 of file getaddrinfo.h.

Referenced by gai_strerror(), getaddrinfo(), and parse_hba_line().

#define EAI_SERVICE   (-8)

Definition at line 39 of file getaddrinfo.h.

Referenced by gai_strerror().

#define EAI_SOCKTYPE   (-7)

Definition at line 38 of file getaddrinfo.h.

Referenced by gai_strerror().

#define EAI_SYSTEM   (-11)

Definition at line 41 of file getaddrinfo.h.

#define freeaddrinfo   pg_freeaddrinfo

Definition at line 141 of file getaddrinfo.h.

Referenced by check_hostname(), and pg_freeaddrinfo_all().

#define gai_strerror   pg_gai_strerror
#define getaddrinfo   pg_getaddrinfo

Definition at line 136 of file getaddrinfo.h.

Referenced by check_hostname(), pg_getaddrinfo_all(), and setup_config().

#define getnameinfo   pg_getnameinfo

Definition at line 151 of file getaddrinfo.h.

Referenced by pg_getnameinfo_all(), and print_addr().

#define NI_NAMEREQD   4

Definition at line 84 of file getaddrinfo.h.

Referenced by check_hostname(), and getnameinfo().

Function Documentation

void freeaddrinfo ( struct addrinfo res)

Definition at line 266 of file getaddrinfo.c.

References addrinfo::ai_addr, and free.

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 }
#define free(a)
Definition: header.h:65
struct sockaddr * ai_addr
Definition: getaddrinfo.h:105
const char* gai_strerror ( int  errcode)

Definition at line 291 of file getaddrinfo.c.

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

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 }
#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 hints,
struct addrinfo **  res 
)

Definition at line 138 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, pg_hton16, pg_hton32, and pqGethostbyname().

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 }
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 EAI_FAMILY
Definition: getaddrinfo.h:37
#define AI_NUMERICHOST
Definition: getaddrinfo.h:73
#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
static char * buf
Definition: pg_test_fsync.c:67
#define AI_PASSIVE
Definition: getaddrinfo.h:62
#define EAI_NONAME
Definition: getaddrinfo.h:34
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
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
int getnameinfo ( const struct sockaddr *  sa,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)

Definition at line 358 of file getaddrinfo.c.

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

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
#define EAI_FAIL
Definition: getaddrinfo.h:36
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
#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:77
#define EAI_MEMORY
Definition: getaddrinfo.h:40