1 /* Host name resolution and matching.
2 Copyright (C) 1995, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 GNU Wget is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
44 #include <sys/types.h>
48 # define SET_H_ERRNO(err) WSASetLastError (err)
50 # include <sys/socket.h>
51 # include <netinet/in.h>
53 # include <arpa/inet.h>
56 # define SET_H_ERRNO(err) ((void)(h_errno = (err)))
60 # define NO_ADDRESS NO_DATA
70 #include "connect.h" /* for socket_has_inet6 */
82 /* Lists of IP addresses that result from running DNS queries. See
83 lookup_host for details. */
86 int count; /* number of adrresses */
87 ip_address *addresses; /* pointer to the string of addresses */
89 int faulty; /* number of addresses known not to work. */
90 int connected; /* whether we were able to connect to
91 one of the addresses in the list,
94 int refcount; /* reference count; when it drops to
95 0, the entry is freed. */
98 /* Get the bounds of the address list. */
101 address_list_get_bounds (const struct address_list *al, int *start, int *end)
107 /* Return a pointer to the address at position POS. */
110 address_list_address_at (const struct address_list *al, int pos)
112 assert (pos >= al->faulty && pos < al->count);
113 return al->addresses + pos;
116 /* Return non-zero if AL contains IP, zero otherwise. */
119 address_list_contains (const struct address_list *al, const ip_address *ip)
125 for (i = 0; i < al->count; i++)
127 ip_address *cur = al->addresses + i;
128 if (cur->type == IPV4_ADDRESS
129 && (ADDRESS_IPV4_IN_ADDR (cur).s_addr
131 ADDRESS_IPV4_IN_ADDR (ip).s_addr))
137 for (i = 0; i < al->count; i++)
139 ip_address *cur = al->addresses + i;
140 if (cur->type == IPV6_ADDRESS
141 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
142 && ADDRESS_IPV6_SCOPE (cur) == ADDRESS_IPV6_SCOPE (ip)
144 && IN6_ARE_ADDR_EQUAL (&ADDRESS_IPV6_IN6_ADDR (cur),
145 &ADDRESS_IPV6_IN6_ADDR (ip)))
149 #endif /* ENABLE_IPV6 */
156 /* Mark the INDEXth element of AL as faulty, so that the next time
157 this address list is used, the faulty element will be skipped. */
160 address_list_set_faulty (struct address_list *al, int index)
162 /* We assume that the address list is traversed in order, so that a
163 "faulty" attempt is always preceded with all-faulty addresses,
164 and this is how Wget uses it. */
165 assert (index == al->faulty);
168 if (al->faulty >= al->count)
169 /* All addresses have been proven faulty. Since there's not much
170 sense in returning the user an empty address list the next
171 time, we'll rather make them all clean, so that they can be
176 /* Set the "connected" flag to true. This flag used by connect.c to
177 see if the host perhaps needs to be resolved again. */
180 address_list_set_connected (struct address_list *al)
185 /* Return the value of the "connected" flag. */
188 address_list_connected_p (const struct address_list *al)
190 return al->connected;
195 /* Create an address_list from the addresses in the given struct
198 static struct address_list *
199 address_list_from_addrinfo (const struct addrinfo *ai)
201 struct address_list *al;
202 const struct addrinfo *ptr;
207 for (ptr = ai; ptr != NULL ; ptr = ptr->ai_next)
208 if (ptr->ai_family == AF_INET || ptr->ai_family == AF_INET6)
213 al = xnew0 (struct address_list);
214 al->addresses = xnew_array (ip_address, cnt);
219 for (ptr = ai; ptr != NULL; ptr = ptr->ai_next)
220 if (ptr->ai_family == AF_INET6)
222 const struct sockaddr_in6 *sin6 =
223 (const struct sockaddr_in6 *)ptr->ai_addr;
224 ip->type = IPV6_ADDRESS;
225 ADDRESS_IPV6_IN6_ADDR (ip) = sin6->sin6_addr;
226 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
227 ADDRESS_IPV6_SCOPE (ip) = sin6->sin6_scope_id;
231 else if (ptr->ai_family == AF_INET)
233 const struct sockaddr_in *sin =
234 (const struct sockaddr_in *)ptr->ai_addr;
235 ip->type = IPV4_ADDRESS;
236 ADDRESS_IPV4_IN_ADDR (ip) = sin->sin_addr;
239 assert (ip - al->addresses == cnt);
243 #else /* not ENABLE_IPV6 */
245 /* Create an address_list from a NULL-terminated vector of IPv4
246 addresses. This kind of vector is returned by gethostbyname. */
248 static struct address_list *
249 address_list_from_ipv4_addresses (char **vec)
252 struct address_list *al = xnew0 (struct address_list);
259 al->addresses = xnew_array (ip_address, count);
263 for (i = 0; i < count; i++)
265 ip_address *ip = &al->addresses[i];
266 ip->type = IPV4_ADDRESS;
267 memcpy (ADDRESS_IPV4_DATA (ip), vec[i], 4);
273 #endif /* not ENABLE_IPV6 */
276 address_list_delete (struct address_list *al)
278 xfree (al->addresses);
282 /* Mark the address list as being no longer in use. This will reduce
283 its reference count which will cause the list to be freed when the
287 address_list_release (struct address_list *al)
290 DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
291 if (al->refcount <= 0)
293 DEBUGP (("Deleting unused %p.\n", al));
294 address_list_delete (al);
298 /* Versions of gethostbyname and getaddrinfo that support timeout. */
302 struct ghbnwt_context {
303 const char *host_name;
304 struct hostent *hptr;
308 gethostbyname_with_timeout_callback (void *arg)
310 struct ghbnwt_context *ctx = (struct ghbnwt_context *)arg;
311 ctx->hptr = gethostbyname (ctx->host_name);
314 /* Just like gethostbyname, except it times out after TIMEOUT seconds.
315 In case of timeout, NULL is returned and errno is set to ETIMEDOUT.
316 The function makes sure that when NULL is returned for reasons
317 other than timeout, errno is reset. */
319 static struct hostent *
320 gethostbyname_with_timeout (const char *host_name, double timeout)
322 struct ghbnwt_context ctx;
323 ctx.host_name = host_name;
324 if (run_with_timeout (timeout, gethostbyname_with_timeout_callback, &ctx))
326 SET_H_ERRNO (HOST_NOT_FOUND);
335 /* Print error messages for host errors. */
337 host_errstr (int error)
339 /* Can't use switch since some of these constants can be equal,
340 which makes the compiler complain about duplicate case
342 if (error == HOST_NOT_FOUND
343 || error == NO_RECOVERY
345 || error == NO_ADDRESS)
346 return _("Unknown host");
347 else if (error == TRY_AGAIN)
348 /* Message modeled after what gai_strerror returns in similar
350 return _("Temporary failure in name resolution");
352 return _("Unknown error");
355 #else /* ENABLE_IPV6 */
357 struct gaiwt_context {
360 const struct addrinfo *hints;
361 struct addrinfo **res;
366 getaddrinfo_with_timeout_callback (void *arg)
368 struct gaiwt_context *ctx = (struct gaiwt_context *)arg;
369 ctx->exit_code = getaddrinfo (ctx->node, ctx->service, ctx->hints, ctx->res);
372 /* Just like getaddrinfo, except it times out after TIMEOUT seconds.
373 In case of timeout, the EAI_SYSTEM error code is returned and errno
374 is set to ETIMEDOUT. */
377 getaddrinfo_with_timeout (const char *node, const char *service,
378 const struct addrinfo *hints, struct addrinfo **res,
381 struct gaiwt_context ctx;
383 ctx.service = service;
387 if (run_with_timeout (timeout, getaddrinfo_with_timeout_callback, &ctx))
392 return ctx.exit_code;
395 #endif /* ENABLE_IPV6 */
397 /* Pretty-print ADDR. When compiled without IPv6, this is the same as
398 inet_ntoa. With IPv6, it either prints an IPv6 address or an IPv4
402 pretty_print_address (const ip_address *addr)
407 return inet_ntoa (ADDRESS_IPV4_IN_ADDR (addr));
411 static char buf[128];
412 inet_ntop (AF_INET6, &ADDRESS_IPV6_IN6_ADDR (addr), buf, sizeof (buf));
414 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
416 /* append "%SCOPE_ID" for all ?non-global? addresses */
417 char *p = buf + strlen (buf);
419 number_to_string (p, ADDRESS_IPV6_SCOPE (addr));
423 buf[sizeof (buf) - 1] = '\0';
432 /* Simple host cache, used by lookup_host to speed up resolving. The
433 cache doesn't handle TTL because Wget is a fairly short-lived
434 application. Refreshing is attempted when connect fails, though --
435 see connect_to_host. */
437 /* Mapping between known hosts and to lists of their addresses. */
438 static struct hash_table *host_name_addresses_map;
441 /* Return the host's resolved addresses from the cache, if
444 static struct address_list *
445 cache_query (const char *host)
447 struct address_list *al;
448 if (!host_name_addresses_map)
450 al = hash_table_get (host_name_addresses_map, host);
453 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
460 /* Cache the DNS lookup of HOST. Subsequent invocations of
461 lookup_host will return the cached value. */
464 cache_store (const char *host, struct address_list *al)
466 if (!host_name_addresses_map)
467 host_name_addresses_map = make_nocase_string_hash_table (0);
470 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
476 debug_logprintf ("Caching %s =>", host);
477 for (i = 0; i < al->count; i++)
478 debug_logprintf (" %s", pretty_print_address (al->addresses + i));
479 debug_logprintf ("\n");
484 /* Remove HOST from the DNS cache. Does nothing is HOST is not in
488 cache_remove (const char *host)
490 struct address_list *al;
491 if (!host_name_addresses_map)
493 al = hash_table_get (host_name_addresses_map, host);
496 address_list_release (al);
497 hash_table_remove (host_name_addresses_map, host);
501 /* Look up HOST in DNS and return a list of IP addresses. The
502 addresses in the list are in the same order in which
503 gethostbyname/getaddrinfo returned them.
505 This function caches its result so that, if the same host is passed
506 the second time, the addresses are returned without DNS lookup.
507 (Use LH_REFRESH to force lookup, or set opt.dns_cache to 0 to
508 globally disable caching.)
510 FLAGS can be a combination of:
511 LH_SILENT - don't print the "resolving ... done" messages.
512 LH_BIND - resolve addresses for use with bind, which under
513 IPv6 means to use AI_PASSIVE flag to getaddrinfo.
514 Passive lookups are not cached under IPv6.
515 LH_REFRESH - if HOST is cached, remove the entry from the cache
516 and resolve it anew. */
518 struct address_list *
519 lookup_host (const char *host, int flags)
521 struct address_list *al;
522 int silent = flags & LH_SILENT;
526 /* If we're not using getaddrinfo, first check if HOST specifies a
527 numeric IPv4 address. gethostbyname is not required to accept
528 dotted-decimal IPv4 addresses, and some implementations (e.g. the
529 Ultrix one and possibly Winsock) indeed don't. */
531 uint32_t addr_ipv4 = (uint32_t)inet_addr (host);
532 if (addr_ipv4 != (uint32_t) -1)
534 /* No need to cache host->addr relation, just return the
537 vec[0] = (char *)&addr_ipv4;
539 return address_list_from_ipv4_addresses (vec);
544 /* Cache is normally on, but can be turned off with --no-dns-cache.
545 Don't cache passive lookups under IPv6. */
546 use_cache = opt.dns_cache;
552 /* Try to find the host in the cache so we don't need to talk to the
553 resolver. If LH_REFRESH is requested, remove HOST from the cache
557 if (!(flags & LH_REFRESH))
559 al = cache_query (host);
567 /* No luck with the cache; resolve HOST. */
570 logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
575 struct addrinfo hints, *res;
578 hints.ai_socktype = SOCK_STREAM;
580 hints.ai_family = AF_INET;
581 else if (opt.ipv6_only)
582 hints.ai_family = AF_INET6;
585 hints.ai_family = AF_UNSPEC;
586 #ifdef HAVE_GETADDRINFO_AI_ADDRCONFIG
587 hints.ai_flags |= AI_ADDRCONFIG;
589 /* On systems without AI_ADDRCONFIG, emulate it by manually
590 checking whether the system supports IPv6 sockets. */
591 if (!socket_has_inet6 ())
592 hints.ai_family = AF_INET;
596 hints.ai_flags |= AI_PASSIVE;
598 err = getaddrinfo_with_timeout (host, NULL, &hints, &res, opt.dns_timeout);
599 if (err != 0 || res == NULL)
602 logprintf (LOG_VERBOSE, _("failed: %s.\n"),
603 err != EAI_SYSTEM ? gai_strerror (err) : strerror (errno));
606 al = address_list_from_addrinfo (res);
610 logprintf (LOG_VERBOSE,
611 _("failed: No IPv4/IPv6 addresses for host.\n"));
617 struct hostent *hptr = gethostbyname_with_timeout (host, opt.dns_timeout);
622 if (errno != ETIMEDOUT)
623 logprintf (LOG_VERBOSE, _("failed: %s.\n"),
624 host_errstr (h_errno));
626 logputs (LOG_VERBOSE, _("failed: timed out.\n"));
630 /* Do older systems have h_addr_list? */
631 al = address_list_from_ipv4_addresses (hptr->h_addr_list);
635 /* Print the addresses determined by DNS lookup, but no more than
640 int printmax = al->count <= 3 ? al->count : 3;
641 for (i = 0; i < printmax; i++)
643 logprintf (LOG_VERBOSE, "%s",
644 pretty_print_address (al->addresses + i));
645 if (i < printmax - 1)
646 logputs (LOG_VERBOSE, ", ");
648 if (printmax != al->count)
649 logputs (LOG_VERBOSE, ", ...");
650 logputs (LOG_VERBOSE, "\n");
653 /* Cache the lookup information. */
655 cache_store (host, al);
660 /* Determine whether a URL is acceptable to be followed, according to
661 a list of domains to accept. */
663 accept_domain (struct url *u)
665 assert (u->host != NULL);
668 if (!sufmatch ((const char **)opt.domains, u->host))
671 if (opt.exclude_domains)
673 if (sufmatch ((const char **)opt.exclude_domains, u->host))
679 /* Check whether WHAT is matched in LIST, each element of LIST being a
680 pattern to match WHAT against, using backward matching (see
681 match_backwards() in utils.c).
683 If an element of LIST matched, 1 is returned, 0 otherwise. */
685 sufmatch (const char **list, const char *what)
690 for (i = 0; list[i]; i++)
692 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
693 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
695 /* The domain must be first to reach to beginning. */
703 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
705 struct address_list *al;
707 xfree (key); /* host */
709 al = (struct address_list *)value;
710 assert (al->refcount == 1);
711 address_list_delete (al);
719 if (host_name_addresses_map)
721 hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
722 hash_table_destroy (host_name_addresses_map);
723 host_name_addresses_map = NULL;