1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2014, 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 http://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
50 #include "curl_addrinfo.h"
51 #include "inet_pton.h"
53 #include "curl_printf.h"
54 #include "curl_memory.h"
56 /* The last #include file should be: */
62 * This is used to free a linked list of Curl_addrinfo structs along
63 * with all its associated allocated storage. This function should be
64 * called once for each successful call to Curl_getaddrinfo_ex() or to
65 * any function call which actually allocates a Curl_addrinfo struct.
68 #if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \
69 defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__)
70 /* workaround icc 9.1 optimizer issue */
71 # define vqualifier volatile
77 Curl_freeaddrinfo(Curl_addrinfo *cahead)
79 Curl_addrinfo *vqualifier canext;
82 for(ca = cahead; ca != NULL; ca = canext) {
84 free(ca->ai_canonname);
92 #ifdef HAVE_GETADDRINFO
94 * Curl_getaddrinfo_ex()
96 * This is a wrapper function around system's getaddrinfo(), with
97 * the only difference that instead of returning a linked list of
98 * addrinfo structs this one returns a linked list of Curl_addrinfo
99 * ones. The memory allocated by this function *MUST* be free'd with
100 * Curl_freeaddrinfo(). For each successful call to this function
101 * there must be an associated call later to Curl_freeaddrinfo().
103 * There should be no single call to system's getaddrinfo() in the
104 * whole library, any such call should be 'routed' through this one.
108 Curl_getaddrinfo_ex(const char *nodename,
109 const char *servname,
110 const struct addrinfo *hints,
111 Curl_addrinfo **result)
113 const struct addrinfo *ai;
114 struct addrinfo *aihead;
115 Curl_addrinfo *cafirst = NULL;
116 Curl_addrinfo *calast = NULL;
121 *result = NULL; /* assume failure */
123 error = getaddrinfo(nodename, servname, hints, &aihead);
127 /* traverse the addrinfo list */
129 for(ai = aihead; ai != NULL; ai = ai->ai_next) {
131 /* ignore elements with unsupported address family, */
132 /* settle family-specific sockaddr structure size. */
133 if(ai->ai_family == AF_INET)
134 ss_size = sizeof(struct sockaddr_in);
136 else if(ai->ai_family == AF_INET6)
137 ss_size = sizeof(struct sockaddr_in6);
142 /* ignore elements without required address info */
143 if((ai->ai_addr == NULL) || !(ai->ai_addrlen > 0))
146 /* ignore elements with bogus address size */
147 if((size_t)ai->ai_addrlen < ss_size)
150 if((ca = malloc(sizeof(Curl_addrinfo))) == NULL) {
155 /* copy each structure member individually, member ordering, */
156 /* size, or padding might be different for each platform. */
158 ca->ai_flags = ai->ai_flags;
159 ca->ai_family = ai->ai_family;
160 ca->ai_socktype = ai->ai_socktype;
161 ca->ai_protocol = ai->ai_protocol;
162 ca->ai_addrlen = (curl_socklen_t)ss_size;
164 ca->ai_canonname = NULL;
167 if((ca->ai_addr = malloc(ss_size)) == NULL) {
172 memcpy(ca->ai_addr, ai->ai_addr, ss_size);
174 if(ai->ai_canonname != NULL) {
175 if((ca->ai_canonname = strdup(ai->ai_canonname)) == NULL) {
183 /* if the return list is empty, this becomes the first element */
187 /* add this element last in the return list */
189 calast->ai_next = ca;
194 /* destroy the addrinfo list */
196 freeaddrinfo(aihead);
198 /* if we failed, also destroy the Curl_addrinfo list */
200 Curl_freeaddrinfo(cafirst);
205 /* rfc3493 conformant */
208 /* rfc3493 obsoleted */
212 SET_SOCKERRNO(error);
218 /* This is not a CURLcode */
221 #endif /* HAVE_GETADDRINFO */
227 * This function returns a pointer to the first element of a newly allocated
228 * Curl_addrinfo struct linked list filled with the data of a given hostent.
229 * Curl_addrinfo is meant to work like the addrinfo struct does for a IPv6
230 * stack, but usable also for IPv4, all hosts and environments.
232 * The memory allocated by this function *MUST* be free'd later on calling
233 * Curl_freeaddrinfo(). For each successful call to this function there
234 * must be an associated call later to Curl_freeaddrinfo().
236 * Curl_addrinfo defined in "lib/curl_addrinfo.h"
238 * struct Curl_addrinfo {
243 * curl_socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
244 * char *ai_canonname;
245 * struct sockaddr *ai_addr;
246 * struct Curl_addrinfo *ai_next;
248 * typedef struct Curl_addrinfo Curl_addrinfo;
250 * hostent defined in <netdb.h>
257 * char **h_addr_list;
260 * for backward compatibility:
262 * #define h_addr h_addr_list[0]
266 Curl_he2ai(const struct hostent *he, int port)
269 Curl_addrinfo *prevai = NULL;
270 Curl_addrinfo *firstai = NULL;
271 struct sockaddr_in *addr;
273 struct sockaddr_in6 *addr6;
275 CURLcode result = CURLE_OK;
280 /* no input == no output! */
283 DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL));
285 for(i=0; (curr = he->h_addr_list[i]) != NULL; i++) {
289 if(he->h_addrtype == AF_INET6)
290 ss_size = sizeof (struct sockaddr_in6);
293 ss_size = sizeof (struct sockaddr_in);
295 if((ai = calloc(1, sizeof(Curl_addrinfo))) == NULL) {
296 result = CURLE_OUT_OF_MEMORY;
299 if((ai->ai_canonname = strdup(he->h_name)) == NULL) {
300 result = CURLE_OUT_OF_MEMORY;
304 if((ai->ai_addr = calloc(1, ss_size)) == NULL) {
305 result = CURLE_OUT_OF_MEMORY;
306 free(ai->ai_canonname);
312 /* store the pointer we want to return from this function */
316 /* make the previous entry point to this */
317 prevai->ai_next = ai;
319 ai->ai_family = he->h_addrtype;
321 /* we return all names as STREAM, so when using this address for TFTP
322 the type must be ignored and conn->socktype be used instead! */
323 ai->ai_socktype = SOCK_STREAM;
325 ai->ai_addrlen = (curl_socklen_t)ss_size;
327 /* leave the rest of the struct filled with zero */
329 switch (ai->ai_family) {
331 addr = (void *)ai->ai_addr; /* storage area for this info */
333 memcpy(&addr->sin_addr, curr, sizeof(struct in_addr));
334 addr->sin_family = (unsigned short)(he->h_addrtype);
335 addr->sin_port = htons((unsigned short)port);
340 addr6 = (void *)ai->ai_addr; /* storage area for this info */
342 memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr));
343 addr6->sin6_family = (unsigned short)(he->h_addrtype);
344 addr6->sin6_port = htons((unsigned short)port);
353 Curl_freeaddrinfo(firstai);
362 struct hostent hostentry;
366 struct in6_addr ina6;
369 char *h_addr_list[2];
376 * This function takes an internet address, in binary form, as input parameter
377 * along with its address family and the string version of the address, and it
378 * returns a Curl_addrinfo chain filled in correctly with information for the
383 Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port)
387 #if defined(__VMS) && \
388 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
389 #pragma pointer_size save
390 #pragma pointer_size short
391 #pragma message disable PTRMISMATCH
395 struct namebuff *buf;
400 DEBUGASSERT(inaddr && hostname);
402 buf = malloc(sizeof(struct namebuff));
406 hoststr = strdup(hostname);
414 addrsize = sizeof(struct in_addr);
415 addrentry = (void *)&buf->addrentry.ina4;
416 memcpy(addrentry, inaddr, sizeof(struct in_addr));
420 addrsize = sizeof(struct in6_addr);
421 addrentry = (void *)&buf->addrentry.ina6;
422 memcpy(addrentry, inaddr, sizeof(struct in6_addr));
434 h->h_addrtype = (short)af;
435 h->h_length = (short)addrsize;
436 h->h_addr_list = &buf->h_addr_list[0];
437 h->h_addr_list[0] = addrentry;
438 h->h_addr_list[1] = NULL; /* terminate list of entries */
440 #if defined(__VMS) && \
441 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
442 #pragma pointer_size restore
443 #pragma message enable PTRMISMATCH
446 ai = Curl_he2ai(h, port);
455 * Given an IPv4 or IPv6 dotted string address, this converts it to a proper
456 * allocated Curl_addrinfo struct and returns it.
458 Curl_addrinfo *Curl_str2addr(char *address, int port)
461 if(Curl_inet_pton(AF_INET, address, &in) > 0)
462 /* This is a dotted IP address 123.123.123.123-style */
463 return Curl_ip2addr(AF_INET, &in, address, port);
467 if(Curl_inet_pton(AF_INET6, address, &in6) > 0)
468 /* This is a dotted IPv6 address ::1-style */
469 return Curl_ip2addr(AF_INET6, &in6, address, port);
472 return NULL; /* bad input format */
475 #ifdef USE_UNIX_SOCKETS
477 * Given a path to a Unix domain socket, return a newly allocated Curl_addrinfo
478 * struct initialized with this path.
480 Curl_addrinfo *Curl_unix2addr(const char *path)
483 struct sockaddr_un *sa_un;
486 ai = calloc(1, sizeof(Curl_addrinfo));
489 if((ai->ai_addr = calloc(1, sizeof(struct sockaddr_un))) == NULL) {
493 /* sun_path must be able to store the NUL-terminated path */
494 path_len = strlen(path);
495 if(path_len >= sizeof(sa_un->sun_path)) {
501 ai->ai_family = AF_UNIX;
502 ai->ai_socktype = SOCK_STREAM; /* assume reliable transport for HTTP */
503 ai->ai_addrlen = (curl_socklen_t) sizeof(struct sockaddr_un);
504 sa_un = (void *) ai->ai_addr;
505 sa_un->sun_family = AF_UNIX;
506 memcpy(sa_un->sun_path, path, path_len + 1); /* copy NUL byte */
511 #if defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO)
513 * curl_dofreeaddrinfo()
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_dofreeaddrinfo(struct addrinfo *freethis,
522 int line, const char *source)
524 (freeaddrinfo)(freethis);
525 curl_memlog("ADDR %s:%d freeaddrinfo(%p)\n",
526 source, line, (void *)freethis);
528 #endif /* defined(CURLDEBUG) && defined(HAVE_FREEADDRINFO) */
531 #if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO)
533 * curl_dogetaddrinfo()
535 * This is strictly for memory tracing and are using the same style as the
536 * family otherwise present in memdebug.c. I put these ones here since they
537 * require a bunch of structs I didn't want to include in memdebug.c
541 curl_dogetaddrinfo(const char *hostname,
543 const struct addrinfo *hints,
544 struct addrinfo **result,
545 int line, const char *source)
547 int res=(getaddrinfo)(hostname, service, hints, result);
550 curl_memlog("ADDR %s:%d getaddrinfo() = %p\n",
551 source, line, (void *)*result);
553 curl_memlog("ADDR %s:%d getaddrinfo() failed\n",
557 #endif /* defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) */