1 /* Host name resolution and matching.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
39 # include <sys/types.h>
40 # include <sys/socket.h>
41 # include <netinet/in.h>
43 # include <arpa/inet.h>
47 # else /* def __VMS */
49 # endif /* def __VMS [else] */
50 # define SET_H_ERRNO(err) ((void)(h_errno = (err)))
52 # define SET_H_ERRNO(err) WSASetLastError (err)
63 # define NO_ADDRESS NO_DATA
66 #if !HAVE_DECL_H_ERRNO
71 /* Lists of IP addresses that result from running DNS queries. See
72 lookup_host for details. */
75 int count; /* number of adrresses */
76 ip_address *addresses; /* pointer to the string of addresses */
78 int faulty; /* number of addresses known not to work. */
79 bool connected; /* whether we were able to connect to
80 one of the addresses in the list,
83 int refcount; /* reference count; when it drops to
84 0, the entry is freed. */
87 /* Get the bounds of the address list. */
90 address_list_get_bounds (const struct address_list *al, int *start, int *end)
96 /* Return a pointer to the address at position POS. */
99 address_list_address_at (const struct address_list *al, int pos)
101 assert (pos >= al->faulty && pos < al->count);
102 return al->addresses + pos;
105 /* Return true if AL contains IP, false otherwise. */
108 address_list_contains (const struct address_list *al, const ip_address *ip)
114 for (i = 0; i < al->count; i++)
116 ip_address *cur = al->addresses + i;
117 if (cur->family == AF_INET
118 && (cur->data.d4.s_addr == ip->data.d4.s_addr))
124 for (i = 0; i < al->count; i++)
126 ip_address *cur = al->addresses + i;
127 if (cur->family == AF_INET6
128 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
129 && cur->ipv6_scope == ip->ipv6_scope
131 && IN6_ARE_ADDR_EQUAL (&cur->data.d6, &ip->data.d6))
135 #endif /* ENABLE_IPV6 */
141 /* Mark the INDEXth element of AL as faulty, so that the next time
142 this address list is used, the faulty element will be skipped. */
145 address_list_set_faulty (struct address_list *al, int index)
147 /* We assume that the address list is traversed in order, so that a
148 "faulty" attempt is always preceded with all-faulty addresses,
149 and this is how Wget uses it. */
150 assert (index == al->faulty);
153 if (al->faulty >= al->count)
154 /* All addresses have been proven faulty. Since there's not much
155 sense in returning the user an empty address list the next
156 time, we'll rather make them all clean, so that they can be
161 /* Set the "connected" flag to true. This flag used by connect.c to
162 see if the host perhaps needs to be resolved again. */
165 address_list_set_connected (struct address_list *al)
167 al->connected = true;
170 /* Return the value of the "connected" flag. */
173 address_list_connected_p (const struct address_list *al)
175 return al->connected;
180 /* Create an address_list from the addresses in the given struct
183 static struct address_list *
184 address_list_from_addrinfo (const struct addrinfo *ai)
186 struct address_list *al;
187 const struct addrinfo *ptr;
192 for (ptr = ai; ptr != NULL ; ptr = ptr->ai_next)
193 if (ptr->ai_family == AF_INET || ptr->ai_family == AF_INET6)
198 al = xnew0 (struct address_list);
199 al->addresses = xnew_array (ip_address, cnt);
204 for (ptr = ai; ptr != NULL; ptr = ptr->ai_next)
205 if (ptr->ai_family == AF_INET6)
207 const struct sockaddr_in6 *sin6 =
208 (const struct sockaddr_in6 *)ptr->ai_addr;
209 ip->family = AF_INET6;
210 ip->data.d6 = sin6->sin6_addr;
211 #ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
212 ip->ipv6_scope = sin6->sin6_scope_id;
216 else if (ptr->ai_family == AF_INET)
218 const struct sockaddr_in *sin =
219 (const struct sockaddr_in *)ptr->ai_addr;
220 ip->family = AF_INET;
221 ip->data.d4 = sin->sin_addr;
224 assert (ip - al->addresses == cnt);
228 #define IS_IPV4(addr) (((const ip_address *) addr)->family == AF_INET)
230 /* Compare two IP addresses by family, giving preference to the IPv4
231 address (sorting it first). In other words, return -1 if ADDR1 is
232 IPv4 and ADDR2 is IPv6, +1 if ADDR1 is IPv6 and ADDR2 is IPv4, and
235 This is intended to be used as the comparator arg to a qsort-like
236 sorting function, which is why it accepts generic pointers. */
239 cmp_prefer_ipv4 (const void *addr1, const void *addr2)
241 return !IS_IPV4 (addr1) - !IS_IPV4 (addr2);
244 #define IS_IPV6(addr) (((const ip_address *) addr)->family == AF_INET6)
246 /* Like the above, but give preference to the IPv6 address. */
249 cmp_prefer_ipv6 (const void *addr1, const void *addr2)
251 return !IS_IPV6 (addr1) - !IS_IPV6 (addr2);
254 #else /* not ENABLE_IPV6 */
256 /* Create an address_list from a NULL-terminated vector of IPv4
257 addresses. This kind of vector is returned by gethostbyname. */
259 static struct address_list *
260 address_list_from_ipv4_addresses (char **vec)
263 struct address_list *al = xnew0 (struct address_list);
270 al->addresses = xnew_array (ip_address, count);
274 for (i = 0; i < count; i++)
276 ip_address *ip = &al->addresses[i];
277 ip->family = AF_INET;
278 memcpy (IP_INADDR_DATA (ip), vec[i], 4);
284 #endif /* not ENABLE_IPV6 */
287 address_list_delete (struct address_list *al)
289 xfree (al->addresses);
293 /* Mark the address list as being no longer in use. This will reduce
294 its reference count which will cause the list to be freed when the
298 address_list_release (struct address_list *al)
301 DEBUGP (("Releasing 0x%0*lx (new refcount %d).\n", PTR_FORMAT (al),
303 if (al->refcount <= 0)
305 DEBUGP (("Deleting unused 0x%0*lx.\n", PTR_FORMAT (al)));
306 address_list_delete (al);
310 /* Versions of gethostbyname and getaddrinfo that support timeout. */
314 struct ghbnwt_context {
315 const char *host_name;
316 struct hostent *hptr;
320 gethostbyname_with_timeout_callback (void *arg)
322 struct ghbnwt_context *ctx = (struct ghbnwt_context *)arg;
323 ctx->hptr = gethostbyname (ctx->host_name);
326 /* Just like gethostbyname, except it times out after TIMEOUT seconds.
327 In case of timeout, NULL is returned and errno is set to ETIMEDOUT.
328 The function makes sure that when NULL is returned for reasons
329 other than timeout, errno is reset. */
331 static struct hostent *
332 gethostbyname_with_timeout (const char *host_name, double timeout)
334 struct ghbnwt_context ctx;
335 ctx.host_name = host_name;
336 if (run_with_timeout (timeout, gethostbyname_with_timeout_callback, &ctx))
338 SET_H_ERRNO (HOST_NOT_FOUND);
347 /* Print error messages for host errors. */
349 host_errstr (int error)
351 /* Can't use switch since some of these constants can be equal,
352 which makes the compiler complain about duplicate case
354 if (error == HOST_NOT_FOUND
355 || error == NO_RECOVERY
357 || error == NO_ADDRESS)
358 return _("Unknown host");
359 else if (error == TRY_AGAIN)
360 /* Message modeled after what gai_strerror returns in similar
362 return _("Temporary failure in name resolution");
364 return _("Unknown error");
367 #else /* ENABLE_IPV6 */
369 struct gaiwt_context {
372 const struct addrinfo *hints;
373 struct addrinfo **res;
378 getaddrinfo_with_timeout_callback (void *arg)
380 struct gaiwt_context *ctx = (struct gaiwt_context *)arg;
381 ctx->exit_code = getaddrinfo (ctx->node, ctx->service, ctx->hints, ctx->res);
384 /* Just like getaddrinfo, except it times out after TIMEOUT seconds.
385 In case of timeout, the EAI_SYSTEM error code is returned and errno
386 is set to ETIMEDOUT. */
389 getaddrinfo_with_timeout (const char *node, const char *service,
390 const struct addrinfo *hints, struct addrinfo **res,
393 struct gaiwt_context ctx;
395 ctx.service = service;
399 if (run_with_timeout (timeout, getaddrinfo_with_timeout_callback, &ctx))
404 return ctx.exit_code;
407 #endif /* ENABLE_IPV6 */
409 /* Return a textual representation of ADDR, i.e. the dotted quad for
410 IPv4 addresses, and the colon-separated list of hex words (with all
411 zeros omitted, etc.) for IPv6 addresses. */
414 print_address (const ip_address *addr)
418 if (!inet_ntop (addr->family, IP_INADDR_DATA (addr), buf, sizeof buf))
419 snprintf (buf, sizeof buf, "<error: %s>", strerror (errno));
422 return inet_ntoa (addr->data.d4);
426 /* The following two functions were adapted from glibc's
427 implementation of inet_pton, written by Paul Vixie. */
430 is_valid_ipv4_address (const char *str, const char *end)
432 bool saw_digit = false;
440 if (ch >= '0' && ch <= '9')
442 val = val * 10 + (ch - '0');
453 else if (ch == '.' && saw_digit)
470 is_valid_ipv6_address (const char *str, const char *end)
472 /* Use lower-case for these to avoid clash with system headers. */
491 /* Leading :: requires some special handling. */
495 if (str == end || *str != ':')
507 /* if ch is a number, add it to val. */
511 val |= XDIGIT_TO_NUM (ch);
518 /* if ch is a colon ... */
531 if (tp > ns_in6addrsz - ns_int16sz)
539 /* if ch is a dot ... */
540 if (ch == '.' && (tp <= ns_in6addrsz - ns_inaddrsz)
541 && is_valid_ipv4_address (curtok, end) == 1)
553 if (tp > ns_in6addrsz - ns_int16sz)
560 if (tp == ns_in6addrsz)
565 if (tp != ns_in6addrsz)
571 /* Simple host cache, used by lookup_host to speed up resolving. The
572 cache doesn't handle TTL because Wget is a fairly short-lived
573 application. Refreshing is attempted when connect fails, though --
574 see connect_to_host. */
576 /* Mapping between known hosts and to lists of their addresses. */
577 static struct hash_table *host_name_addresses_map;
580 /* Return the host's resolved addresses from the cache, if
583 static struct address_list *
584 cache_query (const char *host)
586 struct address_list *al;
587 if (!host_name_addresses_map)
589 al = hash_table_get (host_name_addresses_map, host);
592 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
599 /* Cache the DNS lookup of HOST. Subsequent invocations of
600 lookup_host will return the cached value. */
603 cache_store (const char *host, struct address_list *al)
605 if (!host_name_addresses_map)
606 host_name_addresses_map = make_nocase_string_hash_table (0);
609 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
614 debug_logprintf ("Caching %s =>", host);
615 for (i = 0; i < al->count; i++)
616 debug_logprintf (" %s", print_address (al->addresses + i));
617 debug_logprintf ("\n");
621 /* Remove HOST from the DNS cache. Does nothing is HOST is not in
625 cache_remove (const char *host)
627 struct address_list *al;
628 if (!host_name_addresses_map)
630 al = hash_table_get (host_name_addresses_map, host);
633 address_list_release (al);
634 hash_table_remove (host_name_addresses_map, host);
638 /* Look up HOST in DNS and return a list of IP addresses.
640 This function caches its result so that, if the same host is passed
641 the second time, the addresses are returned without DNS lookup.
642 (Use LH_REFRESH to force lookup, or set opt.dns_cache to 0 to
643 globally disable caching.)
645 The order of the returned addresses is affected by the setting of
646 opt.prefer_family: if it is set to prefer_ipv4, IPv4 addresses are
647 placed at the beginning; if it is prefer_ipv6, IPv6 ones are placed
648 at the beginning; otherwise, the order is left intact. The
649 relative order of addresses with the same family is left
650 undisturbed in either case.
652 FLAGS can be a combination of:
653 LH_SILENT - don't print the "resolving ... done" messages.
654 LH_BIND - resolve addresses for use with bind, which under
655 IPv6 means to use AI_PASSIVE flag to getaddrinfo.
656 Passive lookups are not cached under IPv6.
657 LH_REFRESH - if HOST is cached, remove the entry from the cache
658 and resolve it anew. */
660 struct address_list *
661 lookup_host (const char *host, int flags)
663 struct address_list *al;
664 bool silent = !!(flags & LH_SILENT);
666 bool numeric_address = false;
667 double timeout = opt.dns_timeout;
670 /* If we're not using getaddrinfo, first check if HOST specifies a
671 numeric IPv4 address. Some implementations of gethostbyname
672 (e.g. the Ultrix one and possibly Winsock) don't accept
673 dotted-decimal IPv4 addresses. */
675 uint32_t addr_ipv4 = (uint32_t)inet_addr (host);
676 if (addr_ipv4 != (uint32_t) -1)
678 /* No need to cache host->addr relation, just return the
681 vec[0] = (char *)&addr_ipv4;
683 return address_list_from_ipv4_addresses (vec);
686 #else /* ENABLE_IPV6 */
687 /* If we're using getaddrinfo, at least check whether the address is
688 already numeric, in which case there is no need to print the
689 "Resolving..." output. (This comes at no additional cost since
690 the is_valid_ipv*_address are already required for
693 const char *end = host + strlen (host);
694 if (is_valid_ipv4_address (host, end) || is_valid_ipv6_address (host, end))
695 numeric_address = true;
699 /* Cache is normally on, but can be turned off with --no-dns-cache.
700 Don't cache passive lookups under IPv6. */
701 use_cache = opt.dns_cache;
703 if ((flags & LH_BIND) || numeric_address)
707 /* Try to find the host in the cache so we don't need to talk to the
708 resolver. If LH_REFRESH is requested, remove HOST from the cache
712 if (!(flags & LH_REFRESH))
714 al = cache_query (host);
722 /* No luck with the cache; resolve HOST. */
724 if (!silent && !numeric_address)
726 char *str = NULL, *name;
728 if (opt.enable_iri && (name = idn_decode ((char *) host)) != NULL)
730 int len = strlen (host) + strlen (name) + 4;
732 snprintf (str, len, "%s (%s)", name, host);
737 logprintf (LOG_VERBOSE, _("Resolving %s... "),
738 quotearg_style (escape_quoting_style, str ? str : host));
747 struct addrinfo hints, *res;
750 hints.ai_socktype = SOCK_STREAM;
752 hints.ai_family = AF_INET;
753 else if (opt.ipv6_only)
754 hints.ai_family = AF_INET6;
756 /* We tried using AI_ADDRCONFIG, but removed it because: it
757 misinterprets IPv6 loopbacks, it is broken on AIX 5.1, and
758 it's unneeded since we sort the addresses anyway. */
759 hints.ai_family = AF_UNSPEC;
762 hints.ai_flags |= AI_PASSIVE;
764 #ifdef AI_NUMERICHOST
767 /* Where available, the AI_NUMERICHOST hint can prevent costly
768 access to DNS servers. */
769 hints.ai_flags |= AI_NUMERICHOST;
770 timeout = 0; /* no timeout needed when "resolving"
771 numeric hosts -- avoid setting up
772 signal handlers and such. */
776 err = getaddrinfo_with_timeout (host, NULL, &hints, &res, timeout);
777 if (err != 0 || res == NULL)
780 logprintf (LOG_VERBOSE, _("failed: %s.\n"),
781 err != EAI_SYSTEM ? gai_strerror (err) : strerror (errno));
784 al = address_list_from_addrinfo (res);
788 logprintf (LOG_VERBOSE,
789 _("failed: No IPv4/IPv6 addresses for host.\n"));
793 /* Reorder addresses so that IPv4 ones (or IPv6 ones, as per
794 --prefer-family) come first. Sorting is stable so the order of
795 the addresses with the same family is undisturbed. */
796 if (al->count > 1 && opt.prefer_family != prefer_none)
797 stable_sort (al->addresses, al->count, sizeof (ip_address),
798 opt.prefer_family == prefer_ipv4
799 ? cmp_prefer_ipv4 : cmp_prefer_ipv6);
801 #else /* not ENABLE_IPV6 */
803 struct hostent *hptr = gethostbyname_with_timeout (host, timeout);
808 if (errno != ETIMEDOUT)
809 logprintf (LOG_VERBOSE, _("failed: %s.\n"),
810 host_errstr (h_errno));
812 logputs (LOG_VERBOSE, _("failed: timed out.\n"));
816 /* Do older systems have h_addr_list? */
817 al = address_list_from_ipv4_addresses (hptr->h_addr_list);
819 #endif /* not ENABLE_IPV6 */
821 /* Print the addresses determined by DNS lookup, but no more than
823 if (!silent && !numeric_address)
826 int printmax = al->count <= 3 ? al->count : 3;
827 for (i = 0; i < printmax; i++)
829 logputs (LOG_VERBOSE, print_address (al->addresses + i));
830 if (i < printmax - 1)
831 logputs (LOG_VERBOSE, ", ");
833 if (printmax != al->count)
834 logputs (LOG_VERBOSE, ", ...");
835 logputs (LOG_VERBOSE, "\n");
838 /* Cache the lookup information. */
840 cache_store (host, al);
845 /* Determine whether a URL is acceptable to be followed, according to
846 a list of domains to accept. */
848 accept_domain (struct url *u)
850 assert (u->host != NULL);
853 if (!sufmatch ((const char **)opt.domains, u->host))
856 if (opt.exclude_domains)
858 if (sufmatch ((const char **)opt.exclude_domains, u->host))
864 /* Check whether WHAT is matched in LIST, each element of LIST being a
865 pattern to match WHAT against, using backward matching (see
866 match_backwards() in utils.c).
868 If an element of LIST matched, 1 is returned, 0 otherwise. */
870 sufmatch (const char **list, const char *what)
875 for (i = 0; list[i]; i++)
877 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
878 if (c_tolower (list[i][j]) != c_tolower (what[k]))
880 /* The domain must be first to reach to beginning. */
890 if (host_name_addresses_map)
892 hash_table_iterator iter;
893 for (hash_table_iterate (host_name_addresses_map, &iter);
894 hash_table_iter_next (&iter);
897 char *host = iter.key;
898 struct address_list *al = iter.value;
900 assert (al->refcount == 1);
901 address_list_delete (al);
903 hash_table_destroy (host_name_addresses_map);
904 host_name_addresses_map = NULL;