PostgreSQL Source Code  git master
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 "port/pg_bswap.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

◆ freeaddrinfo()

void freeaddrinfo ( struct addrinfo res)

Definition at line 248 of file getaddrinfo.c.

References addrinfo::ai_addr, and free.

249 {
250  if (res)
251  {
252 #ifdef WIN32
253 
254  /*
255  * If Windows has native IPv6 support, use the native Windows routine.
256  * Otherwise, fall through and use our own code.
257  */
258  if (haveNativeWindowsIPv6routines())
259  {
260  (*freeaddrinfo_ptr) (res);
261  return;
262  }
263 #endif
264 
265  if (res->ai_addr)
266  free(res->ai_addr);
267  free(res);
268  }
269 }
#define free(a)
Definition: header.h:65
struct sockaddr * ai_addr
Definition: getaddrinfo.h:105

◆ gai_strerror()

const char* gai_strerror ( int  errcode)

Definition at line 273 of file getaddrinfo.c.

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

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

◆ getaddrinfo()

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

Definition at line 120 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().

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

◆ getnameinfo()

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

Definition at line 340 of file getaddrinfo.c.

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

343 {
344 #ifdef WIN32
345 
346  /*
347  * If Windows has native IPv6 support, use the native Windows routine.
348  * Otherwise, fall through and use our own code.
349  */
350  if (haveNativeWindowsIPv6routines())
351  return (*getnameinfo_ptr) (sa, salen, node, nodelen,
352  service, servicelen, flags);
353 #endif
354 
355  /* Invalid arguments. */
356  if (sa == NULL || (node == NULL && service == NULL))
357  return EAI_FAIL;
358 
359 #ifdef HAVE_IPV6
360  if (sa->sa_family == AF_INET6)
361  return EAI_FAMILY;
362 #endif
363 
364  /* Unsupported flags. */
365  if (flags & NI_NAMEREQD)
366  return EAI_AGAIN;
367 
368  if (node)
369  {
370  if (sa->sa_family == AF_INET)
371  {
372  if (pg_inet_net_ntop(AF_INET,
373  &((struct sockaddr_in *) sa)->sin_addr,
374  sa->sa_family == AF_INET ? 32 : 128,
375  node, nodelen) == NULL)
376  return EAI_MEMORY;
377  }
378  else
379  return EAI_MEMORY;
380  }
381 
382  if (service)
383  {
384  int ret = -1;
385 
386  if (sa->sa_family == AF_INET)
387  {
388  ret = snprintf(service, servicelen, "%d",
389  pg_ntoh16(((struct sockaddr_in *) sa)->sin_port));
390  }
391  if (ret < 0 || ret >= servicelen)
392  return EAI_MEMORY;
393  }
394 
395  return 0;
396 }
#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
#define EAI_AGAIN
Definition: getaddrinfo.h:35
char * pg_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
#define snprintf
Definition: port.h:193