curl_addrinfo.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at https://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #include <curl/curl.h>
26 
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
29 #endif
30 #ifdef HAVE_NETDB_H
31 # include <netdb.h>
32 #endif
33 #ifdef HAVE_ARPA_INET_H
34 # include <arpa/inet.h>
35 #endif
36 #ifdef HAVE_SYS_UN_H
37 # include <sys/un.h>
38 #endif
39 
40 #ifdef __VMS
41 # include <in.h>
42 # include <inet.h>
43 #endif
44 
45 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
46 # undef in_addr_t
47 # define in_addr_t unsigned long
48 #endif
49 
50 #include <stddef.h>
51 
52 #include "curl_addrinfo.h"
53 #include "inet_pton.h"
54 #include "warnless.h"
55 /* The last 3 #include files should be in this order */
56 #include "curl_printf.h"
57 #include "curl_memory.h"
58 #include "memdebug.h"
59 
60 /*
61  * Curl_freeaddrinfo()
62  *
63  * This is used to free a linked list of Curl_addrinfo structs along
64  * with all its associated allocated storage. This function should be
65  * called once for each successful call to Curl_getaddrinfo_ex() or to
66  * any function call which actually allocates a Curl_addrinfo struct.
67  */
68 
69 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
70  defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
71  /* workaround icc 9.1 optimizer issue */
72 # define vqualifier volatile
73 #else
74 # define vqualifier
75 #endif
76 
77 void
79 {
80  Curl_addrinfo *vqualifier canext;
81  Curl_addrinfo *ca;
82 
83  for(ca = cahead; ca != NULL; ca = canext) {
84  free(ca->ai_addr);
85  free(ca->ai_canonname);
86  canext = ca->ai_next;
87 
88  free(ca);
89  }
90 }
91 
92 
93 #ifdef HAVE_GETADDRINFO
94 /*
95  * Curl_getaddrinfo_ex()
96  *
97  * This is a wrapper function around system's getaddrinfo(), with
98  * the only difference that instead of returning a linked list of
99  * addrinfo structs this one returns a linked list of Curl_addrinfo
100  * ones. The memory allocated by this function *MUST* be free'd with
101  * Curl_freeaddrinfo(). For each successful call to this function
102  * there must be an associated call later to Curl_freeaddrinfo().
103  *
104  * There should be no single call to system's getaddrinfo() in the
105  * whole library, any such call should be 'routed' through this one.
106  */
107 
108 int
109 Curl_getaddrinfo_ex(const char *nodename,
110  const char *servname,
111  const struct addrinfo *hints,
113 {
114  const struct addrinfo *ai;
115  struct addrinfo *aihead;
116  Curl_addrinfo *cafirst = NULL;
117  Curl_addrinfo *calast = NULL;
118  Curl_addrinfo *ca;
119  size_t ss_size;
120  int error;
121 
122  *result = NULL; /* assume failure */
123 
124  error = getaddrinfo(nodename, servname, hints, &aihead);
125  if(error)
126  return error;
127 
128  /* traverse the addrinfo list */
129 
130  for(ai = aihead; ai != NULL; ai = ai->ai_next) {
131 
132  /* ignore elements with unsupported address family, */
133  /* settle family-specific sockaddr structure size. */
134  if(ai->ai_family == AF_INET)
135  ss_size = sizeof(struct sockaddr_in);
136 #ifdef ENABLE_IPV6
137  else if(ai->ai_family == AF_INET6)
138  ss_size = sizeof(struct sockaddr_in6);
139 #endif
140  else
141  continue;
142 
143  /* ignore elements without required address info */
144  if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
145  continue;
146 
147  /* ignore elements with bogus address size */
148  if((size_t)ai->ai_addrlen < ss_size)
149  continue;
150 
151  ca = malloc(sizeof(Curl_addrinfo));
152  if(!ca) {
153  error = EAI_MEMORY;
154  break;
155  }
156 
157  /* copy each structure member individually, member ordering, */
158  /* size, or padding might be different for each platform. */
159 
160  ca->ai_flags = ai->ai_flags;
161  ca->ai_family = ai->ai_family;
162  ca->ai_socktype = ai->ai_socktype;
163  ca->ai_protocol = ai->ai_protocol;
164  ca->ai_addrlen = (curl_socklen_t)ss_size;
165  ca->ai_addr = NULL;
166  ca->ai_canonname = NULL;
167  ca->ai_next = NULL;
168 
169  ca->ai_addr = malloc(ss_size);
170  if(!ca->ai_addr) {
171  error = EAI_MEMORY;
172  free(ca);
173  break;
174  }
175  memcpy(ca->ai_addr, ai->ai_addr, ss_size);
176 
177  if(ai->ai_canonname != NULL) {
178  ca->ai_canonname = strdup(ai->ai_canonname);
179  if(!ca->ai_canonname) {
180  error = EAI_MEMORY;
181  free(ca->ai_addr);
182  free(ca);
183  break;
184  }
185  }
186 
187  /* if the return list is empty, this becomes the first element */
188  if(!cafirst)
189  cafirst = ca;
190 
191  /* add this element last in the return list */
192  if(calast)
193  calast->ai_next = ca;
194  calast = ca;
195 
196  }
197 
198  /* destroy the addrinfo list */
199  if(aihead)
200  freeaddrinfo(aihead);
201 
202  /* if we failed, also destroy the Curl_addrinfo list */
203  if(error) {
204  Curl_freeaddrinfo(cafirst);
205  cafirst = NULL;
206  }
207  else if(!cafirst) {
208 #ifdef EAI_NONAME
209  /* rfc3493 conformant */
210  error = EAI_NONAME;
211 #else
212  /* rfc3493 obsoleted */
213  error = EAI_NODATA;
214 #endif
215 #ifdef USE_WINSOCK
216  SET_SOCKERRNO(error);
217 #endif
218  }
219 
220  *result = cafirst;
221 
222  /* This is not a CURLcode */
223  return error;
224 }
225 #endif /* HAVE_GETADDRINFO */
226 
227 
228 /*
229  * Curl_he2ai()
230  *
231  * This function returns a pointer to the first element of a newly allocated
232  * Curl_addrinfo struct linked list filled with the data of a given hostent.
233  * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
234  * stack, but usable also for IPv4, all hosts and environments.
235  *
236  * The memory allocated by this function *MUST* be free'd later on calling
237  * Curl_freeaddrinfo(). For each successful call to this function there
238  * must be an associated call later to Curl_freeaddrinfo().
239  *
240  * Curl_addrinfo defined in "lib/curl_addrinfo.h"
241  *
242  * struct Curl_addrinfo {
243  * int ai_flags;
244  * int ai_family;
245  * int ai_socktype;
246  * int ai_protocol;
247  * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
248  * char *ai_canonname;
249  * struct sockaddr *ai_addr;
250  * struct Curl_addrinfo *ai_next;
251  * };
252  * typedef struct Curl_addrinfo Curl_addrinfo;
253  *
254  * hostent defined in <netdb.h>
255  *
256  * struct hostent {
257  * char *h_name;
258  * char **h_aliases;
259  * int h_addrtype;
260  * int h_length;
261  * char **h_addr_list;
262  * };
263  *
264  * for backward compatibility:
265  *
266  * #define h_addr h_addr_list[0]
267  */
268 
270 Curl_he2ai(const struct hostent *he, int port)
271 {
272  Curl_addrinfo *ai;
273  Curl_addrinfo *prevai = NULL;
274  Curl_addrinfo *firstai = NULL;
275  struct sockaddr_in *addr;
276 #ifdef ENABLE_IPV6
277  struct sockaddr_in6 *addr6;
278 #endif
279  CURLcode result = CURLE_OK;
280  int i;
281  char *curr;
282 
283  if(!he)
284  /* no input == no output! */
285  return NULL;
286 
287  DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
288 
289  for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) {
290 
291  size_t ss_size;
292 #ifdef ENABLE_IPV6
293  if(he->h_addrtype == AF_INET6)
294  ss_size = sizeof(struct sockaddr_in6);
295  else
296 #endif
297  ss_size = sizeof(struct sockaddr_in);
298 
299  ai = calloc(1, sizeof(Curl_addrinfo));
300  if(!ai) {
301  result = CURLE_OUT_OF_MEMORY;
302  break;
303  }
304  ai->ai_canonname = strdup(he->h_name);
305  if(!ai->ai_canonname) {
306  result = CURLE_OUT_OF_MEMORY;
307  free(ai);
308  break;
309  }
310  ai->ai_addr = calloc(1, ss_size);
311  if(!ai->ai_addr) {
312  result = CURLE_OUT_OF_MEMORY;
313  free(ai->ai_canonname);
314  free(ai);
315  break;
316  }
317 
318  if(!firstai)
319  /* store the pointer we want to return from this function */
320  firstai = ai;
321 
322  if(prevai)
323  /* make the previous entry point to this */
324  prevai->ai_next = ai;
325 
326  ai->ai_family = he->h_addrtype;
327 
328  /* we return all names as STREAM, so when using this address for TFTP
329  the type must be ignored and conn->socktype be used instead! */
330  ai->ai_socktype = SOCK_STREAM;
331 
332  ai->ai_addrlen = (curl_socklen_t)ss_size;
333 
334  /* leave the rest of the struct filled with zero */
335 
336  switch(ai->ai_family) {
337  case AF_INET:
338  addr = (void *)ai->ai_addr; /* storage area for this info */
339 
340  memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
341  addr->sin_family = (unsigned short)(he->h_addrtype);
342  addr->sin_port = htons((unsigned short)port);
343  break;
344 
345 #ifdef ENABLE_IPV6
346  case AF_INET6:
347  addr6 = (void *)ai->ai_addr; /* storage area for this info */
348 
349  memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
350  addr6->sin6_family = (unsigned short)(he->h_addrtype);
351  addr6->sin6_port = htons((unsigned short)port);
352  break;
353 #endif
354  }
355 
356  prevai = ai;
357  }
358 
359  if(result) {
360  Curl_freeaddrinfo(firstai);
361  firstai = NULL;
362  }
363 
364  return firstai;
365 }
366 
367 
368 struct namebuff {
369  struct hostent hostentry;
370  union {
371  struct in_addr ina4;
372 #ifdef ENABLE_IPV6
373  struct in6_addr ina6;
374 #endif
375  } addrentry;
376  char *h_addr_list[2];
377 };
378 
379 
380 /*
381  * Curl_ip2addr()
382  *
383  * This function takes an internet address, in binary form, as input parameter
384  * along with its address family and the string version of the address, and it
385  * returns a Curl_addrinfo chain filled in correctly with information for the
386  * given address/host
387  */
388 
390 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
391 {
392  Curl_addrinfo *ai;
393 
394 #if defined(__VMS) && \
395  defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
396 #pragma pointer_size save
397 #pragma pointer_size short
398 #pragma message disable PTRMISMATCH
399 #endif
400 
401  struct hostent *h;
402  struct namebuff *buf;
403  char *addrentry;
404  char *hoststr;
405  size_t addrsize;
406 
407  DEBUGASSERT(inaddr && hostname);
408 
409  buf = malloc(sizeof(struct namebuff));
410  if(!buf)
411  return NULL;
412 
413  hoststr = strdup(hostname);
414  if(!hoststr) {
415  free(buf);
416  return NULL;
417  }
418 
419  switch(af) {
420  case AF_INET:
421  addrsize = sizeof(struct in_addr);
422  addrentry = (void *)&buf->addrentry.ina4;
423  memcpy(addrentry, inaddr, sizeof(struct in_addr));
424  break;
425 #ifdef ENABLE_IPV6
426  case AF_INET6:
427  addrsize = sizeof(struct in6_addr);
428  addrentry = (void *)&buf->addrentry.ina6;
429  memcpy(addrentry, inaddr, sizeof(struct in6_addr));
430  break;
431 #endif
432  default:
433  free(hoststr);
434  free(buf);
435  return NULL;
436  }
437 
438  h = &buf->hostentry;
439  h->h_name = hoststr;
440  h->h_aliases = NULL;
441  h->h_addrtype = (short)af;
442  h->h_length = (short)addrsize;
443  h->h_addr_list = &buf->h_addr_list[0];
444  h->h_addr_list[0] = addrentry;
445  h->h_addr_list[1] = NULL; /* terminate list of entries */
446 
447 #if defined(__VMS) && \
448  defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
449 #pragma pointer_size restore
450 #pragma message enable PTRMISMATCH
451 #endif
452 
453  ai = Curl_he2ai(h, port);
454 
455  free(hoststr);
456  free(buf);
457 
458  return ai;
459 }
460 
461 /*
462  * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
463  * allocated Curl_addrinfo struct and returns it.
464  */
465 Curl_addrinfo *Curl_str2addr(char *address, int port)
466 {
467  struct in_addr in;
468  if(Curl_inet_pton(AF_INET, address, &in) > 0)
469  /* This is a dotted IP address 123.123.123.123-style */
470  return Curl_ip2addr(AF_INET, &in, address, port);
471 #ifdef ENABLE_IPV6
472  {
473  struct in6_addr in6;
474  if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
475  /* This is a dotted IPv6 address ::1-style */
476  return Curl_ip2addr(AF_INET6, &in6, address, port);
477  }
478 #endif
479  return NULL; /* bad input format */
480 }
481 
482 #ifdef USE_UNIX_SOCKETS
483 
488 Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath, bool abstract)
489 {
490  Curl_addrinfo *ai;
491  struct sockaddr_un *sa_un;
492  size_t path_len;
493 
494  *longpath = FALSE;
495 
496  ai = calloc(1, sizeof(Curl_addrinfo));
497  if(!ai)
498  return NULL;
499  ai->ai_addr = calloc(1, sizeof(struct sockaddr_un));
500  if(!ai->ai_addr) {
501  free(ai);
502  return NULL;
503  }
504 
505  sa_un = (void *) ai->ai_addr;
506  sa_un->sun_family = AF_UNIX;
507 
508  /* sun_path must be able to store the NUL-terminated path */
509  path_len = strlen(path) + 1;
510  if(path_len > sizeof(sa_un->sun_path)) {
511  free(ai->ai_addr);
512  free(ai);
513  *longpath = TRUE;
514  return NULL;
515  }
516 
517  ai->ai_family = AF_UNIX;
518  ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
519  ai->ai_addrlen = (curl_socklen_t)
520  ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
521 
522  /* Abstract Unix domain socket have NULL prefix instead of suffix */
523  if(abstract)
524  memcpy(sa_un->sun_path + 1, path, path_len - 1);
525  else
526  memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
527 
528  return ai;
529 }
530 #endif
531 
532 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
533 /*
534  * curl_dofreeaddrinfo()
535  *
536  * This is strictly for memory tracing and are using the same style as the
537  * family otherwise present in memdebug.c. I put these ones here since they
538  * require a bunch of structs I didn't want to include in memdebug.c
539  */
540 
541 void
542 curl_dofreeaddrinfo(struct addrinfo *freethis,
543  int line, const char *source)
544 {
545 #ifdef USE_LWIPSOCK
546  lwip_freeaddrinfo(freethis);
547 #else
548  (freeaddrinfo)(freethis);
549 #endif
550  curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
551  source, line, (void *)freethis);
552 }
553 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
554 
555 
556 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
557 /*
558  * curl_dogetaddrinfo()
559  *
560  * This is strictly for memory tracing and are using the same style as the
561  * family otherwise present in memdebug.c. I put these ones here since they
562  * require a bunch of structs I didn't want to include in memdebug.c
563  */
564 
565 int
566 curl_dogetaddrinfo(const char *hostname,
567  const char *service,
568  const struct addrinfo *hints,
569  struct addrinfo **result,
570  int line, const char *source)
571 {
572 #ifdef USE_LWIPSOCK
573  int res = lwip_getaddrinfo(hostname, service, hints, result);
574 #else
575  int res = (getaddrinfo)(hostname, service, hints, result);
576 #endif
577  if(0 == res)
578  /* success */
579  curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
580  source, line, (void *)*result);
581  else
582  curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
583  source, line);
584  return res;
585 }
586 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
587 
588 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
589 /*
590  * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
591  * 10.11.5.
592  */
593 void Curl_addrinfo_set_port(Curl_addrinfo *addrinfo, int port)
594 {
595  Curl_addrinfo *ca;
596  struct sockaddr_in *addr;
597 #ifdef ENABLE_IPV6
598  struct sockaddr_in6 *addr6;
599 #endif
600  for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
601  switch(ca->ai_family) {
602  case AF_INET:
603  addr = (void *)ca->ai_addr; /* storage area for this info */
604  addr->sin_port = htons((unsigned short)port);
605  break;
606 
607 #ifdef ENABLE_IPV6
608  case AF_INET6:
609  addr6 = (void *)ca->ai_addr; /* storage area for this info */
610  addr6->sin6_port = htons((unsigned short)port);
611  break;
612 #endif
613  }
614  }
615 }
616 #endif
#define free(ptr)
Definition: curl_memory.h:130
struct hostent hostentry
char * ai_canonname
Definition: curl_addrinfo.h:57
union namebuff::@8 addrentry
struct in_addr ina4
#define strdup(ptr)
Definition: curl_memory.h:122
#define DEBUGASSERT(x)
CURLcode
Definition: curl.h:454
Curl_addrinfo * Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
void Curl_freeaddrinfo(Curl_addrinfo *cahead)
Definition: curl_addrinfo.c:78
static int res
#define malloc(size)
Definition: curl_memory.h:124
char * h_addr_list[2]
UNITTEST_START int result
Definition: unit1304.c:49
unsigned int i
Definition: unit1303.c:79
struct sockaddr * ai_addr
Definition: curl_addrinfo.h:58
memcpy(filename, filename1, strlen(filename1))
CURL_TYPEOF_CURL_SOCKLEN_T curl_socklen_t
Definition: system.h:414
#define FALSE
Definition: curl.h:455
int Curl_inet_pton(int af, const char *src, void *dst)
Definition: inet_pton.c:66
static unsigned short port
Definition: sockfilt.c:137
struct Curl_addrinfo * ai_next
Definition: curl_addrinfo.h:59
#define SET_SOCKERRNO(x)
char buf[3]
Definition: unit1398.c:32
Curl_addrinfo * Curl_str2addr(char *address, int port)
curl_socklen_t ai_addrlen
Definition: curl_addrinfo.h:56
Curl_addrinfo * Curl_he2ai(const struct hostent *he, int port)
#define TRUE
#define vqualifier
Definition: curl_addrinfo.c:74
#define getaddrinfo
Definition: setup-os400.h:48
#define calloc(nbelem, size)
Definition: curl_memory.h:126
const char * path
Definition: util.c:192


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:08