1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
27 #ifdef HAVE_NETINET_IN_H
28 # include <netinet/in.h>
30 #ifdef HAVE_NETINET_IN6_H
31 # include <netinet/in6.h>
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
48 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
50 # define in_addr_t unsigned long
55 #include "curl_addrinfo.h"
56 #include "inet_pton.h"
58 /* The last 3 #include files should be in this order */
59 #include "curl_printf.h"
60 #include "curl_memory.h"
66 * This is used to free a linked list of Curl_addrinfo structs along
67 * with all its associated allocated storage. This function should be
68 * called once for each successful call to Curl_getaddrinfo_ex() or to
69 * any function call which actually allocates a Curl_addrinfo struct.
72 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
73 defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
74 /* workaround icc 9.1 optimizer issue */
75 # define vqualifier volatile
81 Curl_freeaddrinfo(struct Curl_addrinfo *cahead)
83 struct Curl_addrinfo *vqualifier canext;
84 struct Curl_addrinfo *ca;
86 for(ca = cahead; ca; ca = canext) {
93 #ifdef HAVE_GETADDRINFO
95 * Curl_getaddrinfo_ex()
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().
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.
109 Curl_getaddrinfo_ex(const char *nodename,
110 const char *servname,
111 const struct addrinfo *hints,
112 struct Curl_addrinfo **result)
114 const struct addrinfo *ai;
115 struct addrinfo *aihead;
116 struct Curl_addrinfo *cafirst = NULL;
117 struct Curl_addrinfo *calast = NULL;
118 struct Curl_addrinfo *ca;
122 *result = NULL; /* assume failure */
124 error = getaddrinfo(nodename, servname, hints, &aihead);
128 /* traverse the addrinfo list */
130 for(ai = aihead; ai != NULL; ai = ai->ai_next) {
131 size_t namelen = ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0;
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);
137 else if(ai->ai_family == AF_INET6)
138 ss_size = sizeof(struct sockaddr_in6);
143 /* ignore elements without required address info */
144 if(!ai->ai_addr || !(ai->ai_addrlen > 0))
147 /* ignore elements with bogus address size */
148 if((size_t)ai->ai_addrlen < ss_size)
151 ca = malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen);
157 /* copy each structure member individually, member ordering, */
158 /* size, or padding might be different for each platform. */
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;
166 ca->ai_canonname = NULL;
169 ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo));
170 memcpy(ca->ai_addr, ai->ai_addr, ss_size);
173 ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size);
174 memcpy(ca->ai_canonname, ai->ai_canonname, namelen);
177 /* if the return list is empty, this becomes the first element */
181 /* add this element last in the return list */
183 calast->ai_next = ca;
188 /* destroy the addrinfo list */
190 freeaddrinfo(aihead);
192 /* if we failed, also destroy the Curl_addrinfo list */
194 Curl_freeaddrinfo(cafirst);
199 /* rfc3493 conformant */
202 /* rfc3493 obsoleted */
206 SET_SOCKERRNO(error);
212 /* This is not a CURLcode */
215 #endif /* HAVE_GETADDRINFO */
221 * This function returns a pointer to the first element of a newly allocated
222 * Curl_addrinfo struct linked list filled with the data of a given hostent.
223 * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
224 * stack, but usable also for IPv4, all hosts and environments.
226 * The memory allocated by this function *MUST* be free'd later on calling
227 * Curl_freeaddrinfo(). For each successful call to this function there
228 * must be an associated call later to Curl_freeaddrinfo().
230 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
232 * struct Curl_addrinfo {
237 * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
238 * char *ai_canonname;
239 * struct sockaddr *ai_addr;
240 * struct Curl_addrinfo *ai_next;
243 * hostent defined in <netdb.h>
250 * char **h_addr_list;
253 * for backward compatibility:
255 * #define h_addr h_addr_list[0]
258 struct Curl_addrinfo *
259 Curl_he2ai(const struct hostent *he, int port)
261 struct Curl_addrinfo *ai;
262 struct Curl_addrinfo *prevai = NULL;
263 struct Curl_addrinfo *firstai = NULL;
264 struct sockaddr_in *addr;
266 struct sockaddr_in6 *addr6;
268 CURLcode result = CURLE_OK;
273 /* no input == no output! */
276 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
278 for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) {
280 size_t namelen = strlen(he->h_name) + 1; /* include zero termination */
282 if(he->h_addrtype == AF_INET6)
283 ss_size = sizeof(struct sockaddr_in6);
286 ss_size = sizeof(struct sockaddr_in);
288 /* allocate memory to hold the struct, the address and the name */
289 ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + namelen);
291 result = CURLE_OUT_OF_MEMORY;
294 /* put the address after the struct */
295 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
296 /* then put the name after the address */
297 ai->ai_canonname = (char *)ai->ai_addr + ss_size;
298 memcpy(ai->ai_canonname, he->h_name, namelen);
301 /* store the pointer we want to return from this function */
305 /* make the previous entry point to this */
306 prevai->ai_next = ai;
308 ai->ai_family = he->h_addrtype;
310 /* we return all names as STREAM, so when using this address for TFTP
311 the type must be ignored and conn->socktype be used instead! */
312 ai->ai_socktype = SOCK_STREAM;
314 ai->ai_addrlen = (curl_socklen_t)ss_size;
316 /* leave the rest of the struct filled with zero */
318 switch(ai->ai_family) {
320 addr = (void *)ai->ai_addr; /* storage area for this info */
322 memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
323 addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
324 addr->sin_port = htons((unsigned short)port);
329 addr6 = (void *)ai->ai_addr; /* storage area for this info */
331 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
332 addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype);
333 addr6->sin6_port = htons((unsigned short)port);
342 Curl_freeaddrinfo(firstai);
351 struct hostent hostentry;
355 struct in6_addr ina6;
358 char *h_addr_list[2];
365 * This function takes an internet address, in binary form, as input parameter
366 * along with its address family and the string version of the address, and it
367 * returns a Curl_addrinfo chain filled in correctly with information for the
371 struct Curl_addrinfo *
372 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
374 struct Curl_addrinfo *ai;
376 #if defined(__VMS) && \
377 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
378 #pragma pointer_size save
379 #pragma pointer_size short
380 #pragma message disable PTRMISMATCH
384 struct namebuff *buf;
389 DEBUGASSERT(inaddr && hostname);
391 buf = malloc(sizeof(struct namebuff));
395 hoststr = strdup(hostname);
403 addrsize = sizeof(struct in_addr);
404 addrentry = (void *)&buf->addrentry.ina4;
405 memcpy(addrentry, inaddr, sizeof(struct in_addr));
409 addrsize = sizeof(struct in6_addr);
410 addrentry = (void *)&buf->addrentry.ina6;
411 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
423 h->h_addrtype = (short)af;
424 h->h_length = (short)addrsize;
425 h->h_addr_list = &buf->h_addr_list[0];
426 h->h_addr_list[0] = addrentry;
427 h->h_addr_list[1] = NULL; /* terminate list of entries */
429 #if defined(__VMS) && \
430 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
431 #pragma pointer_size restore
432 #pragma message enable PTRMISMATCH
435 ai = Curl_he2ai(h, port);
444 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
445 * allocated Curl_addrinfo struct and returns it.
447 struct Curl_addrinfo *Curl_str2addr(char *address, int port)
450 if(Curl_inet_pton(AF_INET, address, &in) > 0)
451 /* This is a dotted IP address 123.123.123.123-style */
452 return Curl_ip2addr(AF_INET, &in, address, port);
456 if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
457 /* This is a dotted IPv6 address ::1-style */
458 return Curl_ip2addr(AF_INET6, &in6, address, port);
461 return NULL; /* bad input format */
464 #ifdef USE_UNIX_SOCKETS
466 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
467 * struct initialized with this path.
468 * Set '*longpath' to TRUE if the error is a too long path.
470 struct Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath,
473 struct Curl_addrinfo *ai;
474 struct sockaddr_un *sa_un;
479 ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un));
482 ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
484 sa_un = (void *) ai->ai_addr;
485 sa_un->sun_family = AF_UNIX;
487 /* sun_path must be able to store the NUL-terminated path */
488 path_len = strlen(path) + 1;
489 if(path_len > sizeof(sa_un->sun_path)) {
495 ai->ai_family = AF_UNIX;
496 ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
497 ai->ai_addrlen = (curl_socklen_t)
498 ((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF);
500 /* Abstract Unix domain socket have NULL prefix instead of suffix */
502 memcpy(sa_un->sun_path + 1, path, path_len - 1);
504 memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
510 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) && \
511 defined(HAVE_FREEADDRINFO)
513 * curl_dbg_freeaddrinfo()
515 * This is strictly for memory tracing and are using the same style as the
516 * family otherwise present in memdebug.c. I put these ones here since they
517 * require a bunch of structs I didn't want to include in memdebug.c
521 curl_dbg_freeaddrinfo(struct addrinfo *freethis,
522 int line, const char *source)
524 curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n",
525 source, line, (void *)freethis);
527 lwip_freeaddrinfo(freethis);
529 (freeaddrinfo)(freethis);
532 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
535 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
537 * curl_dbg_getaddrinfo()
539 * This is strictly for memory tracing and are using the same style as the
540 * family otherwise present in memdebug.c. I put these ones here since they
541 * require a bunch of structs I didn't want to include in memdebug.c
545 curl_dbg_getaddrinfo(const char *hostname,
547 const struct addrinfo *hints,
548 struct addrinfo **result,
549 int line, const char *source)
552 int res = lwip_getaddrinfo(hostname, service, hints, result);
554 int res = (getaddrinfo)(hostname, service, hints, result);
558 curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n",
559 source, line, (void *)*result);
561 curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n",
565 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
567 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
569 * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
572 void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port)
574 struct Curl_addrinfo *ca;
575 struct sockaddr_in *addr;
577 struct sockaddr_in6 *addr6;
579 for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
580 switch(ca->ai_family) {
582 addr = (void *)ca->ai_addr; /* storage area for this info */
583 addr->sin_port = htons((unsigned short)port);
588 addr6 = (void *)ca->ai_addr; /* storage area for this info */
589 addr6->sin6_port = htons((unsigned short)port);