1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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.haxx.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>
33 #ifdef HAVE_ARPA_INET_H
34 # include <arpa/inet.h>
45 #if defined(NETWARE) && defined(__NOVELL_LIBC__)
47 # define in_addr_t unsigned long
52 #include "curl_addrinfo.h"
53 #include "inet_pton.h"
55 /* The last 3 #include files should be in this order */
56 #include "curl_printf.h"
57 #include "curl_memory.h"
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.
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
78 Curl_freeaddrinfo(Curl_addrinfo *cahead)
80 Curl_addrinfo *vqualifier canext;
83 for(ca = cahead; ca != NULL; ca = canext) {
85 free(ca->ai_canonname);
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 Curl_addrinfo **result)
114 const struct addrinfo *ai;
115 struct addrinfo *aihead;
116 Curl_addrinfo *cafirst = NULL;
117 Curl_addrinfo *calast = NULL;
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) {
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 == NULL) || !(ai->ai_addrlen > 0))
147 /* ignore elements with bogus address size */
148 if((size_t)ai->ai_addrlen < ss_size)
151 ca = malloc(sizeof(Curl_addrinfo));
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 = malloc(ss_size);
175 memcpy(ca->ai_addr, ai->ai_addr, ss_size);
177 if(ai->ai_canonname != NULL) {
178 ca->ai_canonname = strdup(ai->ai_canonname);
179 if(!ca->ai_canonname) {
187 /* if the return list is empty, this becomes the first element */
191 /* add this element last in the return list */
193 calast->ai_next = ca;
198 /* destroy the addrinfo list */
200 freeaddrinfo(aihead);
202 /* if we failed, also destroy the Curl_addrinfo list */
204 Curl_freeaddrinfo(cafirst);
209 /* rfc3493 conformant */
212 /* rfc3493 obsoleted */
216 SET_SOCKERRNO(error);
222 /* This is not a CURLcode */
225 #endif /* HAVE_GETADDRINFO */
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.
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().
240 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
242 * struct Curl_addrinfo {
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;
252 * typedef struct Curl_addrinfo Curl_addrinfo;
254 * hostent defined in <netdb.h>
261 * char **h_addr_list;
264 * for backward compatibility:
266 * #define h_addr h_addr_list[0]
270 Curl_he2ai(const struct hostent *he, int port)
273 Curl_addrinfo *prevai = NULL;
274 Curl_addrinfo *firstai = NULL;
275 struct sockaddr_in *addr;
277 struct sockaddr_in6 *addr6;
279 CURLcode result = CURLE_OK;
284 /* no input == no output! */
287 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
289 for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
293 if(he->h_addrtype == AF_INET6)
294 ss_size = sizeof(struct sockaddr_in6);
297 ss_size = sizeof(struct sockaddr_in);
299 ai = calloc(1, sizeof(Curl_addrinfo));
301 result = CURLE_OUT_OF_MEMORY;
304 ai->ai_canonname = strdup(he->h_name);
305 if(!ai->ai_canonname) {
306 result = CURLE_OUT_OF_MEMORY;
310 ai->ai_addr = calloc(1, ss_size);
312 result = CURLE_OUT_OF_MEMORY;
313 free(ai->ai_canonname);
319 /* store the pointer we want to return from this function */
323 /* make the previous entry point to this */
324 prevai->ai_next = ai;
326 ai->ai_family = he->h_addrtype;
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;
332 ai->ai_addrlen = (curl_socklen_t)ss_size;
334 /* leave the rest of the struct filled with zero */
336 switch(ai->ai_family) {
338 addr = (void *)ai->ai_addr; /* storage area for this info */
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);
347 addr6 = (void *)ai->ai_addr; /* storage area for this info */
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);
360 Curl_freeaddrinfo(firstai);
369 struct hostent hostentry;
373 struct in6_addr ina6;
376 char *h_addr_list[2];
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
390 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
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
402 struct namebuff *buf;
407 DEBUGASSERT(inaddr && hostname);
409 buf = malloc(sizeof(struct namebuff));
413 hoststr = strdup(hostname);
421 addrsize = sizeof(struct in_addr);
422 addrentry = (void *)&buf->addrentry.ina4;
423 memcpy(addrentry, inaddr, sizeof(struct in_addr));
427 addrsize = sizeof(struct in6_addr);
428 addrentry = (void *)&buf->addrentry.ina6;
429 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
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 */
447 #if defined(__VMS) && \
448 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
449 #pragma pointer_size restore
450 #pragma message enable PTRMISMATCH
453 ai = Curl_he2ai(h, port);
462 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
463 * allocated Curl_addrinfo struct and returns it.
465 Curl_addrinfo *Curl_str2addr(char *address, int port)
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);
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);
479 return NULL; /* bad input format */
482 #ifdef USE_UNIX_SOCKETS
484 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
485 * struct initialized with this path.
486 * Set '*longpath' to TRUE if the error is a too long path.
488 Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath, bool abstract)
491 struct sockaddr_un *sa_un;
496 ai = calloc(1, sizeof(Curl_addrinfo));
499 ai->ai_addr = calloc(1, sizeof(struct sockaddr_un));
505 sa_un = (void *) ai->ai_addr;
506 sa_un->sun_family = AF_UNIX;
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)) {
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);
522 /* Abstract Unix domain socket have NULL prefix instead of suffix */
524 memcpy(sa_un->sun_path + 1, path, path_len - 1);
526 memcpy(sa_un->sun_path, path, path_len); /* copy NUL byte */
532 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
534 * curl_dofreeaddrinfo()
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
542 curl_dofreeaddrinfo(struct addrinfo *freethis,
543 int line, const char *source)
546 lwip_freeaddrinfo(freethis);
548 (freeaddrinfo)(freethis);
550 curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
551 source, line, (void *)freethis);
553 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
556 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
558 * curl_dogetaddrinfo()
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
566 curl_dogetaddrinfo(const char *hostname,
568 const struct addrinfo *hints,
569 struct addrinfo **result,
570 int line, const char *source)
573 int res=lwip_getaddrinfo(hostname, service, hints, result);
575 int res=(getaddrinfo)(hostname, service, hints, result);
579 curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
580 source, line, (void *)*result);
582 curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
586 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */
588 #if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS)
590 * Work-arounds the sin6_port is always zero bug on iOS 9.3.2 and Mac OS X
593 void Curl_addrinfo_set_port(Curl_addrinfo *addrinfo, int port)
596 struct sockaddr_in *addr;
598 struct sockaddr_in6 *addr6;
600 for(ca = addrinfo; ca != NULL; ca = ca->ai_next) {
601 switch(ca->ai_family) {
603 addr = (void *)ca->ai_addr; /* storage area for this info */
604 addr->sin_port = htons((unsigned short)port);
609 addr6 = (void *)ca->ai_addr; /* storage area for this info */
610 addr6->sin6_port = htons((unsigned short)port);