PostgreSQL Source Code  git master
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

◆ AI_NUMERICHOST

#define AI_NUMERICHOST   0x0004

Definition at line 73 of file getaddrinfo.h.

◆ AI_PASSIVE

#define AI_PASSIVE   0x0001

Definition at line 62 of file getaddrinfo.h.

◆ EAI_AGAIN

#define EAI_AGAIN   (-3)

Definition at line 35 of file getaddrinfo.h.

◆ EAI_BADFLAGS

#define EAI_BADFLAGS   (-1)

Definition at line 33 of file getaddrinfo.h.

◆ EAI_FAIL

#define EAI_FAIL   (-4)

Definition at line 36 of file getaddrinfo.h.

◆ EAI_FAMILY

#define EAI_FAMILY   (-6)

Definition at line 37 of file getaddrinfo.h.

◆ EAI_MEMORY

#define EAI_MEMORY   (-10)

Definition at line 40 of file getaddrinfo.h.

◆ EAI_NONAME

#define EAI_NONAME   (-2)

Definition at line 34 of file getaddrinfo.h.

◆ EAI_SERVICE

#define EAI_SERVICE   (-8)

Definition at line 39 of file getaddrinfo.h.

◆ EAI_SOCKTYPE

#define EAI_SOCKTYPE   (-7)

Definition at line 38 of file getaddrinfo.h.

◆ EAI_SYSTEM

#define EAI_SYSTEM   (-11)

Definition at line 41 of file getaddrinfo.h.

◆ freeaddrinfo

#define freeaddrinfo   pg_freeaddrinfo

Definition at line 141 of file getaddrinfo.h.

◆ gai_strerror

#define gai_strerror   pg_gai_strerror

Definition at line 146 of file getaddrinfo.h.

◆ getaddrinfo

#define getaddrinfo   pg_getaddrinfo

Definition at line 136 of file getaddrinfo.h.

◆ getnameinfo

#define getnameinfo   pg_getnameinfo

Definition at line 151 of file getaddrinfo.h.

◆ NI_MAXHOST

#define NI_MAXHOST   1025

Definition at line 88 of file getaddrinfo.h.

◆ NI_MAXSERV

#define NI_MAXSERV   32

Definition at line 91 of file getaddrinfo.h.

◆ NI_NAMEREQD

#define NI_NAMEREQD   4

Definition at line 84 of file getaddrinfo.h.

◆ NI_NUMERICHOST

#define NI_NUMERICHOST   1

Definition at line 78 of file getaddrinfo.h.

◆ NI_NUMERICSERV

#define NI_NUMERICSERV   2

Definition at line 81 of file getaddrinfo.h.

Function Documentation

◆ freeaddrinfo()

void freeaddrinfo ( struct addrinfo res)

Definition at line 256 of file getaddrinfo.c.

257 {
258  if (res)
259  {
260 #ifdef WIN32
261 
262  /*
263  * If Windows has native IPv6 support, use the native Windows routine.
264  * Otherwise, fall through and use our own code.
265  */
266  if (haveNativeWindowsIPv6routines())
267  {
268  (*freeaddrinfo_ptr) (res);
269  return;
270  }
271 #endif
272 
273  if (res->ai_addr)
274  free(res->ai_addr);
275  free(res);
276  }
277 }
#define free(a)
Definition: header.h:65

References free, and res.

◆ gai_strerror()

const char* gai_strerror ( int  errcode)

Definition at line 281 of file getaddrinfo.c.

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

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

◆ getaddrinfo()

int getaddrinfo ( const char *  node,
const char *  service,
const struct addrinfo hints,
struct addrinfo **  res 
)

Definition at line 128 of file getaddrinfo.c.

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

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 res.

◆ getnameinfo()

int getnameinfo ( const struct sockaddr *  sa,
int  salen,
char *  node,
int  nodelen,
char *  service,
int  servicelen,
int  flags 
)

Definition at line 348 of file getaddrinfo.c.

351 {
352 #ifdef WIN32
353 
354  /*
355  * If Windows has native IPv6 support, use the native Windows routine.
356  * Otherwise, fall through and use our own code.
357  */
358  if (haveNativeWindowsIPv6routines())
359  return (*getnameinfo_ptr) (sa, salen, node, nodelen,
360  service, servicelen, flags);
361 #endif
362 
363  /* Invalid arguments. */
364  if (sa == NULL || (node == NULL && service == NULL))
365  return EAI_FAIL;
366 
367 #ifdef HAVE_IPV6
368  if (sa->sa_family == AF_INET6)
369  return EAI_FAMILY;
370 #endif
371 
372  /* Unsupported flags. */
373  if (flags & NI_NAMEREQD)
374  return EAI_AGAIN;
375 
376  if (node)
377  {
378  if (sa->sa_family == AF_INET)
379  {
380  if (pg_inet_net_ntop(AF_INET,
381  &((struct sockaddr_in *) sa)->sin_addr,
382  sa->sa_family == AF_INET ? 32 : 128,
383  node, nodelen) == NULL)
384  return EAI_MEMORY;
385  }
386  else
387  return EAI_MEMORY;
388  }
389 
390  if (service)
391  {
392  int ret = -1;
393 
394  if (sa->sa_family == AF_INET)
395  {
396  ret = snprintf(service, servicelen, "%d",
397  pg_ntoh16(((struct sockaddr_in *) sa)->sin_port));
398  }
399  if (ret < 0 || ret >= servicelen)
400  return EAI_MEMORY;
401  }
402 
403  return 0;
404 }
#define NI_NAMEREQD
Definition: getaddrinfo.h:84
#define pg_ntoh16(x)
Definition: pg_bswap.h:124
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
Definition: inet_net_ntop.c:77
#define snprintf
Definition: port.h:225

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