1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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.
22 ***************************************************************************/
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
40 #ifdef HAVE_ARPA_INET_H
41 #include <arpa/inet.h>
44 #include <stdlib.h> /* required for free() prototypes */
47 #include <unistd.h> /* for the close() proto */
70 #include "inet_ntop.h"
72 #define _MPRINTF_REPLACE /* use our functions only */
73 #include <curl/mprintf.h>
75 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
76 #include "inet_ntoa_r.h"
80 /* The last #include file should be: */
87 * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c
88 * source file are these:
90 * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use
91 * that. The host may not be able to resolve IPv6, but we don't really have to
92 * take that into account. Hosts that aren't IPv6-enabled have CURLRES_IPV4
95 * CURLRES_ARES - is defined if libcurl is built to use c-ares for
96 * asynchronous name resolves. This can be Windows or *nix.
98 * CURLRES_THREADED - is defined if libcurl is built to run under (native)
99 * Windows, and then the name resolve will be done in a new thread, and the
100 * supported API will be the same as for ares-builds.
102 * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If
103 * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is
106 * The host*.c sources files are split up like this:
108 * hostip.c - method-independent resolver functions and utility functions
109 * hostasyn.c - functions for asynchronous name resolves
110 * hostsyn.c - functions for synchronous name resolves
111 * hostares.c - functions for ares-using name resolves
112 * hostthre.c - functions for threaded name resolves
113 * hostip4.c - ipv4-specific functions
114 * hostip6.c - ipv6-specific functions
116 * The hostip.h is the united header file for all this. It defines the
117 * CURLRES_* defines based on the config*.h and setup.h defines.
120 /* These two symbols are for the global DNS cache */
121 static struct curl_hash hostname_cache;
122 static int host_cache_initialized;
124 static void freednsentry(void *freethis);
127 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
128 * Global DNS cache is general badness. Do not use. This will be removed in
129 * a future version. Use the share interface instead!
131 void Curl_global_host_cache_init(void)
133 if(!host_cache_initialized) {
134 Curl_hash_init(&hostname_cache, 7, Curl_hash_str, Curl_str_key_compare,
136 host_cache_initialized = 1;
141 * Return a pointer to the global cache
143 struct curl_hash *Curl_global_host_cache_get(void)
145 return &hostname_cache;
149 * Destroy and cleanup the global DNS cache
151 void Curl_global_host_cache_dtor(void)
153 if(host_cache_initialized) {
154 Curl_hash_clean(&hostname_cache);
155 host_cache_initialized = 0;
160 * Return # of adresses in a Curl_addrinfo struct
162 int Curl_num_addresses(const Curl_addrinfo *addr)
165 for (i = 0; addr; addr = addr->ai_next, i++)
171 * Curl_printable_address() returns a printable version of the 1st address
172 * given in the 'ip' argument. The result will be stored in the buf that is
175 * If the conversion fails, it returns NULL.
177 const char *Curl_printable_address(const Curl_addrinfo *ip,
178 char *buf, size_t bufsize)
180 const void *ip4 = &((const struct sockaddr_in*)ip->ai_addr)->sin_addr;
181 int af = ip->ai_family;
183 const void *ip6 = &((const struct sockaddr_in6*)ip->ai_addr)->sin6_addr;
185 const void *ip6 = NULL;
188 return Curl_inet_ntop(af, af == AF_INET ? ip4 : ip6, buf, bufsize);
192 * Return a hostcache id string for the providing host + port, to be used by
196 create_hostcache_id(const char *server, int port)
198 /* create and return the new allocated entry */
199 return aprintf("%s:%d", server, port);
202 struct hostcache_prune_data {
208 * This function is set as a callback to be called for every entry in the DNS
209 * cache when we want to prune old unused entries.
211 * Returning non-zero means remove the entry, return 0 to keep it in the
215 hostcache_timestamp_remove(void *datap, void *hc)
217 struct hostcache_prune_data *data =
218 (struct hostcache_prune_data *) datap;
219 struct Curl_dns_entry *c = (struct Curl_dns_entry *) hc;
221 if((data->now - c->timestamp < data->cache_timeout) ||
223 /* please don't remove */
232 * Prune the DNS cache. This assumes that a lock has already been taken.
235 hostcache_prune(struct curl_hash *hostcache, long cache_timeout, time_t now)
237 struct hostcache_prune_data user;
239 user.cache_timeout = cache_timeout;
242 Curl_hash_clean_with_criterium(hostcache,
244 hostcache_timestamp_remove);
248 * Library-wide function for pruning the DNS cache. This function takes and
249 * returns the appropriate locks.
251 void Curl_hostcache_prune(struct SessionHandle *data)
255 if((data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
256 /* cache forever means never prune, and NULL hostcache means
261 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
265 /* Remove outdated and unused entries from the hostcache */
266 hostcache_prune(data->dns.hostcache,
267 data->set.dns_cache_timeout,
271 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
275 remove_entry_if_stale(struct SessionHandle *data, struct Curl_dns_entry *dns)
277 struct hostcache_prune_data user;
279 if( !dns || (data->set.dns_cache_timeout == -1) || !data->dns.hostcache)
280 /* cache forever means never prune, and NULL hostcache means
285 user.cache_timeout = data->set.dns_cache_timeout;
287 if( !hostcache_timestamp_remove(&user,dns) )
290 /* ok, we do need to clear the cache. although we need to remove just a
291 single entry we clean the entire hash, as no explicit delete function
294 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
296 Curl_hash_clean_with_criterium(data->dns.hostcache,
298 hostcache_timestamp_remove);
301 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
307 #ifdef HAVE_SIGSETJMP
308 /* Beware this is a global and unique instance. This is used to store the
309 return address that we can jump back to from inside a signal handler. This
310 is not thread-safe stuff. */
311 sigjmp_buf curl_jmpenv;
316 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
318 * When calling Curl_resolv() has resulted in a response with a returned
319 * address, we call this function to store the information in the dns
322 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
324 struct Curl_dns_entry *
325 Curl_cache_addr(struct SessionHandle *data,
327 const char *hostname,
332 struct Curl_dns_entry *dns;
333 struct Curl_dns_entry *dns2;
336 /* Create an entry id, based upon the hostname and port */
337 entry_id = create_hostcache_id(hostname, port);
338 /* If we can't create the entry id, fail */
341 entry_len = strlen(entry_id);
343 /* Create a new cache entry */
344 dns = (struct Curl_dns_entry *) calloc(sizeof(struct Curl_dns_entry), 1);
350 dns->inuse = 0; /* init to not used */
351 dns->addr = addr; /* this is the address(es) */
353 /* Store the resolved data in our DNS cache. This function may return a
354 pointer to an existing struct already present in the hash, and it may
355 return the same argument we pass in. Make no assumptions. */
356 dns2 = Curl_hash_add(data->dns.hostcache, entry_id, entry_len+1,
359 /* Major badness, run away. */
367 dns->timestamp = now; /* used now */
368 dns->inuse++; /* mark entry as in-use */
370 /* free the allocated entry_id again */
377 * Curl_resolv() is the main name resolve function within libcurl. It resolves
378 * a name and returns a pointer to the entry in the 'entry' argument (if one
379 * is provided). This function might return immediately if we're using asynch
380 * resolves. See the return codes.
382 * The cache entry we return will get its 'inuse' counter increased when this
383 * function is used. You MUST call Curl_resolv_unlock() later (when you're
384 * done using this struct) to decrease the counter again.
388 * CURLRESOLV_ERROR (-1) = error, no pointer
389 * CURLRESOLV_RESOLVED (0) = OK, pointer provided
390 * CURLRESOLV_PENDING (1) = waiting for response, no pointer
393 int Curl_resolv(struct connectdata *conn,
394 const char *hostname,
396 struct Curl_dns_entry **entry)
398 char *entry_id = NULL;
399 struct Curl_dns_entry *dns = NULL;
401 struct SessionHandle *data = conn->data;
406 #ifdef HAVE_SIGSETJMP
407 /* this allows us to time-out from the name resolver, as the timeout
408 will generate a signal and we will siglongjmp() from that here */
409 if(!data->set.no_signal) {
410 if(sigsetjmp(curl_jmpenv, 1)) {
411 /* this is coming from a siglongjmp() */
412 failf(data, "name lookup timed out");
413 return CURLRESOLV_ERROR;
418 /* Create an entry id, based upon the hostname and port */
419 entry_id = create_hostcache_id(hostname, port);
420 /* If we can't create the entry id, fail */
422 return CURLRESOLV_ERROR;
424 entry_len = strlen(entry_id);
427 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
429 /* See if its already in our dns cache */
430 dns = Curl_hash_pick(data->dns.hostcache, entry_id, entry_len+1);
433 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
435 /* free the allocated entry_id again */
438 /* See whether the returned entry is stale. Deliberately done after the
440 if( remove_entry_if_stale(data,dns) )
441 dns = NULL; /* the memory deallocation is being handled by the hash */
443 rc = CURLRESOLV_ERROR; /* default to failure */
446 /* The entry was not in the cache. Resolve it to IP address */
451 /* Check what IP specifics the app has requested and if we can provide it.
452 * If not, bail out. */
453 if(!Curl_ipvalid(data))
454 return CURLRESOLV_ERROR;
456 /* If Curl_getaddrinfo() returns NULL, 'respwait' might be set to a
457 non-zero value indicating that we need to wait for the response to the
459 addr = Curl_getaddrinfo(conn, hostname, port, &respwait);
463 /* the response to our resolve call will come asynchronously at
464 a later time, good or bad */
465 /* First, check that we haven't received the info by now */
466 result = Curl_is_resolved(conn, &dns);
467 if(result) /* error detected */
468 return CURLRESOLV_ERROR;
470 rc = CURLRESOLV_RESOLVED; /* pointer provided */
472 rc = CURLRESOLV_PENDING; /* no info yet */
477 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
479 /* we got a response, store it in the cache */
480 dns = Curl_cache_addr(data, addr, hostname, port);
483 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
486 /* returned failure, bail out nicely */
487 Curl_freeaddrinfo(addr);
489 rc = CURLRESOLV_RESOLVED;
494 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
495 dns->inuse++; /* we use it! */
497 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
498 rc = CURLRESOLV_RESOLVED;
507 * Curl_resolv_unlock() unlocks the given cached DNS entry. When this has been
508 * made, the struct may be destroyed due to pruning. It is important that only
509 * one unlock is made for each Curl_resolv() call.
511 void Curl_resolv_unlock(struct SessionHandle *data, struct Curl_dns_entry *dns)
513 DEBUGASSERT(dns && (dns->inuse>0));
516 Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
521 Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
525 * File-internal: free a cache dns entry.
527 static void freednsentry(void *freethis)
529 struct Curl_dns_entry *p = (struct Curl_dns_entry *) freethis;
531 Curl_freeaddrinfo(p->addr);
537 * Curl_mk_dnscache() creates a new DNS cache and returns the handle for it.
539 struct curl_hash *Curl_mk_dnscache(void)
541 return Curl_hash_alloc(7, Curl_hash_str, Curl_str_key_compare, freednsentry);
544 #ifdef CURLRES_ADDRINFO_COPY
546 /* align on even 64bit boundaries */
547 #define MEMALIGN(x) ((x)+(8-(((unsigned long)(x))&0x7)))
550 * Curl_addrinfo_copy() performs a "deep" copy of a hostent into a buffer and
551 * returns a pointer to the malloc()ed copy. You need to call free() on the
552 * returned buffer when you're done with it.
554 Curl_addrinfo *Curl_addrinfo_copy(const void *org, int port)
556 const struct hostent *orig = org;
558 return Curl_he2ai(orig, port);
560 #endif /* CURLRES_ADDRINFO_COPY */
562 /***********************************************************************
563 * Only for plain-ipv4 and c-ares builds
564 **********************************************************************/
566 #if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
568 * This is a function for freeing name information in a protocol independent
571 void Curl_freeaddrinfo(Curl_addrinfo *ai)
575 /* walk over the list and free all entries */
579 free(ai->ai_canonname);
586 struct hostent hostentry;
587 char *h_addr_list[2];
588 struct in_addr addrentry;
589 char h_name[16]; /* 123.123.123.123 = 15 letters is maximum */
593 * Curl_ip2addr() takes a 32bit ipv4 internet address as input parameter
594 * together with a pointer to the string version of the address, and it
595 * returns a Curl_addrinfo chain filled in correctly with information for this
598 * The input parameters ARE NOT checked for validity but they are expected
599 * to have been checked already when this is called.
601 Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port)
605 #if defined(VMS) && \
606 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
607 #pragma pointer_size save
608 #pragma pointer_size short
609 #pragma message disable PTRMISMATCH
613 struct in_addr *addrentry;
614 struct namebuf buffer;
615 struct namebuf *buf = &buffer;
618 h->h_addr_list = &buf->h_addr_list[0];
619 addrentry = &buf->addrentry;
621 /* On UNICOS, s_addr is a bit field and for some reason assigning to it
622 * doesn't work. There must be a better fix than this ugly hack.
624 memcpy(addrentry, &num, SIZEOF_in_addr);
626 addrentry->s_addr = num;
628 h->h_addr_list[0] = (char*)addrentry;
629 h->h_addr_list[1] = NULL;
630 h->h_addrtype = AF_INET;
631 h->h_length = sizeof(*addrentry);
632 h->h_name = &buf->h_name[0];
635 /* Now store the dotted version of the address */
636 snprintf((char *)h->h_name, 16, "%s", hostname);
638 #if defined(VMS) && \
639 defined(__INITIAL_POINTER_SIZE) && (__INITIAL_POINTER_SIZE == 64)
640 #pragma pointer_size restore
641 #pragma message enable PTRMISMATCH
644 ai = Curl_he2ai(h, port);
648 #endif /* CURLRES_IPV4 || CURLRES_ARES */