1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * SPDX-License-Identifier: curl
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #include <curl/curl.h>
29 #ifdef HAVE_NETINET_IN_H
30 # include <netinet/in.h>
32 #ifdef HAVE_NETINET_IN6_H
33 # include <netinet/in6.h>
38 #ifdef HAVE_ARPA_INET_H
39 # include <arpa/inet.h>
50 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
52 # define in_addr_t unsigned long
57 #include "curl_addrinfo.h"
58 #include "inet_pton.h"
60 /* The last 3 #include files should be in this order */
61 #include "curl_printf.h"
62 #include "curl_memory.h"
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.
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
83 Curl_freeaddrinfo(struct Curl_addrinfo *cahead)
85 struct Curl_addrinfo *vqualifier canext;
86 struct Curl_addrinfo *ca;
88 for(ca = cahead; ca; ca = canext) {
95 #ifdef HAVE_GETADDRINFO
97 * Curl_getaddrinfo_ex()
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().
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.
111 Curl_getaddrinfo_ex(const char *nodename,
112 const char *servname,
113 const struct addrinfo *hints,
114 struct Curl_addrinfo **result)
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;
124 *result = NULL; /* assume failure */
126 error = getaddrinfo(nodename, servname, hints, &aihead);
130 /* traverse the addrinfo list */
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);
139 else if(ai->ai_family == AF_INET6)
140 ss_size = sizeof(struct sockaddr_in6);
145 /* ignore elements without required address info */
146 if(!ai->ai_addr || !(ai->ai_addrlen > 0))
149 /* ignore elements with bogus address size */
150 if((size_t)ai->ai_addrlen < ss_size)
153 ca = malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen);
159 /* copy each structure member individually, member ordering, */
160 /* size, or padding might be different for each platform. */
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;
168 ca->ai_canonname = NULL;
171 ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
172 memcpy(ca->ai_addr, ai->ai_addr, ss_size);
175 ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size);
176 memcpy(ca->ai_canonname, ai->ai_canonname, namelen);
179 /* if the return list is empty, this becomes the first element */
183 /* add this element last in the return list */
185 calast->ai_next = ca;
190 /* destroy the addrinfo list */
192 freeaddrinfo(aihead);
194 /* if we failed, also destroy the Curl_addrinfo list */
196 Curl_freeaddrinfo(cafirst);
201 /* rfc3493 conformant */
204 /* rfc3493 obsoleted */
208 SET_SOCKERRNO(error);
214 /* This is not a CURLcode */
217 #endif /* HAVE_GETADDRINFO */
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.
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().
232 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
234 * struct Curl_addrinfo {
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;
245 * hostent defined in <netdb.h>
252 * char **h_addr_list;
255 * for backward compatibility:
257 * #define h_addr h_addr_list[0]
260 struct Curl_addrinfo *
261 Curl_he2ai(const struct hostent *he, int port)
263 struct Curl_addrinfo *ai;
264 struct Curl_addrinfo *prevai = NULL;
265 struct Curl_addrinfo *firstai = NULL;
266 struct sockaddr_in *addr;
268 struct sockaddr_in6 *addr6;
270 CURLcode result = CURLE_OK;
275 /* no input == no output! */
278 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
280 for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) {
282 size_t namelen = strlen(he->h_name) + 1; /* include null-terminatior */
284 if(he->h_addrtype == AF_INET6)
285 ss_size = sizeof(struct sockaddr_in6);
288 ss_size = sizeof(struct sockaddr_in);
290 /* allocate memory to hold the struct, the address and the name */
291 ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + namelen);
293 result = CURLE_OUT_OF_MEMORY;
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);
303 /* store the pointer we want to return from this function */
307 /* make the previous entry point to this */
308 prevai->ai_next = ai;
310 ai->ai_family = he->h_addrtype;
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;
316 ai->ai_addrlen = (curl_socklen_t)ss_size;
318 /* leave the rest of the struct filled with zero */
320 switch(ai->ai_family) {
322 addr = (void *)ai->ai_addr; /* storage area for this info */
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);
331 addr6 = (void *)ai->ai_addr; /* storage area for this info */
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);
344 Curl_freeaddrinfo(firstai);
353 struct hostent hostentry;
357 struct in6_addr ina6;
360 char *h_addr_list[2];
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
373 struct Curl_addrinfo *
374 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
376 struct Curl_addrinfo *ai;
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
386 struct namebuff *buf;
391 DEBUGASSERT(inaddr && hostname);
393 buf = malloc(sizeof(struct namebuff));
397 hoststr = strdup(hostname);
405 addrsize = sizeof(struct in_addr);
406 addrentry = (void *)&buf->addrentry.ina4;
407 memcpy(addrentry, inaddr, sizeof(struct in_addr));
411 addrsize = sizeof(struct in6_addr);
412 addrentry = (void *)&buf->addrentry.ina6;
413 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
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 */
431 #if defined(__VMS) && \
432 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
433 #pragma pointer_size restore
434 #pragma message enable PTRMISMATCH
437 ai = Curl_he2ai(h, port);
446 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
447 * allocated Curl_addrinfo struct and returns it.
449 struct Curl_addrinfo *Curl_str2addr(char *address, int port)
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);
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);
463 return NULL; /* bad input format */
466 #ifdef USE_UNIX_SOCKETS
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.
472 struct Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath,
475 struct Curl_addrinfo *ai;
476 struct sockaddr_un *sa_un;
481 ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un));
484 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
486 sa_un = (void *) ai->ai_addr;
487 sa_un->sun_family = AF_UNIX;
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)) {
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);
502 /* Abstract Unix domain socket have NULL prefix instead of suffix */
504 memcpy(sa_un->sun_path + 1, path, path_len - 1);
506 memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
512 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) && \
513 defined(HAVE_FREEADDRINFO)
515 * curl_dbg_freeaddrinfo()
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
523 curl_dbg_freeaddrinfo(struct addrinfo *freethis,
524 int line, const char *source)
526 curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n",
527 source, line, (void *)freethis);
529 lwip_freeaddrinfo(freethis);
531 (freeaddrinfo)(freethis);
534 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
537 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
539 * curl_dbg_getaddrinfo()
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
547 curl_dbg_getaddrinfo(const char *hostname,
549 const struct addrinfo *hints,
550 struct addrinfo **result,
551 int line, const char *source)
554 int res = lwip_getaddrinfo(hostname, service, hints, result);
556 int res = (getaddrinfo)(hostname, service, hints, result);
560 curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n",
561 source, line, (void *)*result);
563 curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n",
567 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
569 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
571 * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
574 void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port)
576 struct Curl_addrinfo *ca;
577 struct sockaddr_in *addr;
579 struct sockaddr_in6 *addr6;
581 for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
582 switch(ca->ai_family) {
584 addr = (void *)ca->ai_addr; /* storage area for this info */
585 addr->sin_port = htons((unsigned short)port);
590 addr6 = (void *)ca->ai_addr; /* storage area for this info */
591 addr6->sin6_port = htons((unsigned short)port);