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