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
63 #ifdef HAVE_SYS_UTSNAME_H
64 # include <sys/utsname.h>
85 int ip_default_family = AF_INET6;
87 int ip_default_family = AF_INET;
90 /* Mapping between known hosts and to lists of their addresses. */
92 static struct hash_table *host_name_addresses_map;
94 /* Lists of addresses. This should eventually be extended to handle
98 int count; /* number of adrresses */
99 ip_address *addresses; /* pointer to the string of addresses */
101 int faulty; /* number of addresses known not to work. */
102 int refcount; /* so we know whether to free it or not. */
105 /* Get the bounds of the address list. */
108 address_list_get_bounds (struct address_list *al, int *start, int *end)
114 /* Copy address number INDEX to IP_STORE. */
117 address_list_copy_one (struct address_list *al, int index, ip_address *ip_store)
119 assert (index >= al->faulty && index < al->count);
120 memcpy (ip_store, al->addresses + index, sizeof (ip_address));
123 /* Check whether two address lists have all their IPs in common. */
126 address_list_match_all (struct address_list *al1, struct address_list *al2)
130 if (al1->count != al2->count)
132 return 0 == memcmp (al1->addresses, al2->addresses,
133 al1->count * sizeof (ip_address));
136 /* Mark the INDEXth element of AL as faulty, so that the next time
137 this address list is used, the faulty element will be skipped. */
140 address_list_set_faulty (struct address_list *al, int index)
142 /* We assume that the address list is traversed in order, so that a
143 "faulty" attempt is always preceded with all-faulty addresses,
144 and this is how Wget uses it. */
145 assert (index == al->faulty);
148 if (al->faulty >= al->count)
149 /* All addresses have been proven faulty. Since there's not much
150 sense in returning the user an empty address list the next
151 time, we'll rather make them all clean, so that they can be
156 #ifdef HAVE_GETADDRINFO
158 * address_list_from_addrinfo
160 * This function transform an addrinfo links list in and address_list.
163 * addrinfo* Linkt list of addrinfo
166 * address_list* New allocated address_list
168 static struct address_list *
169 address_list_from_addrinfo (struct addrinfo *ai)
171 struct address_list *al;
172 struct addrinfo *ai_head = ai;
176 for (ai = ai_head; ai; ai = ai->ai_next)
177 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
182 al = xmalloc (sizeof (struct address_list));
183 al->addresses = xmalloc (cnt * sizeof (ip_address));
188 for (i = 0, ai = ai_head; ai; ai = ai->ai_next)
189 if (ai->ai_family == AF_INET6)
191 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)ai->ai_addr;
192 memcpy (al->addresses + i, &sin6->sin6_addr, 16);
195 else if (ai->ai_family == AF_INET)
197 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
198 map_ipv4_to_ip ((ip4_address *)&sin->sin_addr, al->addresses + i);
205 /* Create an address_list out of a NULL-terminated list of addresses,
206 as returned by gethostbyname. */
207 static struct address_list *
208 address_list_new (char **h_addr_list)
212 struct address_list *al = xmalloc (sizeof (struct address_list));
214 while (h_addr_list[count])
219 al->addresses = xmalloc (count * sizeof (ip_address));
222 for (i = 0; i < count; i++)
223 map_ipv4_to_ip ((ip4_address *)h_addr_list[i], al->addresses + i);
229 /* Like address_list_new, but initialized with only one address. */
231 static struct address_list *
232 address_list_new_one (ip_address *addr)
234 struct address_list *al = xmalloc (sizeof (struct address_list));
237 al->addresses = xmalloc (sizeof (ip_address));
239 memcpy (al->addresses, addr, sizeof (ip_address));
245 address_list_delete (struct address_list *al)
247 xfree (al->addresses);
252 address_list_release (struct address_list *al)
255 DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
256 if (al->refcount <= 0)
258 DEBUGP (("Deleting unused %p.\n", al));
259 address_list_delete (al);
264 * wget_sockaddr_set_address
266 * This function takes an wget_sockaddr and fill in the protocol type,
267 * the port number and the address, there NULL in address means wildcard.
268 * Unsuported adress family will abort the whole programm.
271 * wget_sockaddr* The space to be filled
272 * int The wished protocol
273 * unsigned short The port
274 * const ip_address The Binary IP adress
277 * - Only modify 1. param
280 wget_sockaddr_set_address (wget_sockaddr *sa,
281 int ip_family, unsigned short port, ip_address *addr)
283 if (ip_family == AF_INET)
285 sa->sin.sin_family = ip_family;
286 sa->sin.sin_port = htons (port);
288 memset (&sa->sin.sin_addr, 0, sizeof(ip4_address));
292 if (!map_ip_to_ipv4 (addr, &addr4))
293 /* should the callers have prevented this? */
295 memcpy (&sa->sin.sin_addr, &addr4, sizeof(ip4_address));
300 if (ip_family == AF_INET6)
302 sa->sin6.sin6_family = ip_family;
303 sa->sin6.sin6_port = htons (port);
305 memset (&sa->sin6.sin6_addr, 0 , 16);
307 memcpy (&sa->sin6.sin6_addr, addr, 16);
315 * wget_sockaddr_set_port
317 * This funtion only fill the port of the socket information.
318 * If the protocol is not supported nothing is done.
319 * Unsuported adress family will abort the whole programm.
322 * that the IP-Protocol already is set.
325 * wget_sockaddr* The space there port should be entered
326 * unsigned int The port that should be entered in host order
329 * - Only modify 1. param
332 wget_sockaddr_set_port (wget_sockaddr *sa, unsigned short port)
334 if (sa->sa.sa_family == AF_INET)
336 sa->sin.sin_port = htons (port);
340 if (sa->sa.sa_family == AF_INET6)
342 sa->sin6.sin6_port = htons (port);
350 * wget_sockaddr_get_addr
352 * This function return the adress from an sockaddr as byte string.
353 * Unsuported adress family will abort the whole programm.
356 * that the IP-Protocol already is set.
359 * wget_sockaddr* Socket Information
362 * unsigned char * IP address as byte string.
365 wget_sockaddr_get_addr (wget_sockaddr *sa)
367 if (sa->sa.sa_family == AF_INET)
368 return &sa->sin.sin_addr;
370 if (sa->sa.sa_family == AF_INET6)
371 return &sa->sin6.sin6_addr;
379 * wget_sockaddr_get_port
381 * This function only return the port from the input structure
382 * Unsuported adress family will abort the whole programm.
385 * that the IP-Protocol already is set.
388 * wget_sockaddr* Information where to get the port
391 * unsigned short Port Number in host order.
394 wget_sockaddr_get_port (const wget_sockaddr *sa)
396 if (sa->sa.sa_family == AF_INET)
397 return htons (sa->sin.sin_port);
399 if (sa->sa.sa_family == AF_INET6)
400 return htons (sa->sin6.sin6_port);
403 /* do not complain about return nothing */
410 * This function return the length of the sockaddr corresponding to
411 * the acutall prefered protocol for (bind, connect etc...)
412 * Unsuported adress family will abort the whole programm.
415 * that the IP-Protocol already is set.
418 * - Public IP-Family Information
421 * int structure length for socket options
426 if (ip_default_family == AF_INET)
427 return sizeof (struct sockaddr_in);
429 if (ip_default_family == AF_INET6)
430 return sizeof (struct sockaddr_in6);
433 /* do not complain about return nothing */
438 * Map an IPv4 adress to the internal adress format.
441 map_ipv4_to_ip (ip4_address *ipv4, ip_address *ip)
444 static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
445 memcpy ((char *)ip + 12, ipv4 , 4);
446 memcpy ((char *)ip + 0, ipv64, 12);
448 if ((char *)ip != (char *)ipv4)
449 memcpy (ip, ipv4, 4);
453 /* Detect whether an IP adress represents an IPv4 address and, if so,
454 copy it to IPV4. 0 is returned on failure.
455 This operation always succeeds when Wget is compiled without IPv6.
456 If IPV4 is NULL, don't copy, just detect. */
459 map_ip_to_ipv4 (ip_address *ip, ip4_address *ipv4)
462 static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
463 if (0 != memcmp (ip, ipv64, 12))
466 memcpy (ipv4, (char *)ip + 12, 4);
469 memcpy (ipv4, (char *)ip, 4);
474 /* Versions of gethostbyname and getaddrinfo that support timeout. */
478 struct ghbnwt_context {
479 const char *host_name;
480 struct hostent *hptr;
484 gethostbyname_with_timeout_callback (void *arg)
486 struct ghbnwt_context *ctx = (struct ghbnwt_context *)arg;
487 ctx->hptr = gethostbyname (ctx->host_name);
490 /* Just like gethostbyname, except it times out after TIMEOUT seconds.
491 In case of timeout, NULL is returned and errno is set to ETIMEDOUT.
492 The function makes sure that when NULL is returned for reasons
493 other than timeout, errno is reset. */
495 static struct hostent *
496 gethostbyname_with_timeout (const char *host_name, int timeout)
498 struct ghbnwt_context ctx;
499 ctx.host_name = host_name;
500 if (run_with_timeout (timeout, gethostbyname_with_timeout_callback, &ctx))
502 SET_H_ERRNO (HOST_NOT_FOUND);
511 #else /* ENABLE_IPV6 */
513 struct gaiwt_context {
516 const struct addrinfo *hints;
517 struct addrinfo **res;
522 getaddrinfo_with_timeout_callback (void *arg)
524 struct gaiwt_context *ctx = (struct gaiwt_context *)arg;
525 ctx->exit_code = getaddrinfo (ctx->node, ctx->service, ctx->hints, ctx->res);
528 /* Just like getaddrinfo, except it times out after TIMEOUT seconds.
529 In case of timeout, the EAI_SYSTEM error code is returned and errno
530 is set to ETIMEDOUT. */
533 getaddrinfo_with_timeout (const char *node, const char *service,
534 const struct addrinfo *hints, struct addrinfo **res,
537 struct gaiwt_context ctx;
539 ctx.service = service;
543 if (run_with_timeout (timeout, getaddrinfo_with_timeout_callback, &ctx))
548 return ctx.exit_code;
551 #endif /* ENABLE_IPV6 */
553 /* Pretty-print ADDR. When compiled without IPv6, this is the same as
554 inet_ntoa. With IPv6, it either prints an IPv6 address or an IPv4
558 pretty_print_address (ip_address *addr)
562 static char buf[128];
564 if (map_ip_to_ipv4 (addr, &addr4))
565 return inet_ntoa (*(struct in_addr *)&addr4);
567 if (!inet_ntop (AF_INET6, addr, buf, sizeof (buf)))
571 return inet_ntoa (*(struct in_addr *)addr);
574 /* Add host name HOST with the address ADDR_TEXT to the cache.
575 ADDR_LIST is a NULL-terminated list of addresses, as in struct
579 cache_host_lookup (const char *host, struct address_list *al)
581 if (!host_name_addresses_map)
582 host_name_addresses_map = make_nocase_string_hash_table (0);
585 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
591 debug_logprintf ("Caching %s =>", host);
592 for (i = 0; i < al->count; i++)
593 debug_logprintf (" %s", pretty_print_address (al->addresses + i));
594 debug_logprintf ("\n");
599 struct address_list *
600 lookup_host (const char *host, int silent)
602 struct address_list *al = NULL;
603 unsigned long addr_ipv4; /* #### use a 32-bit type here. */
606 /* First, try to check whether the address is already a numeric
610 if (inet_pton (AF_INET6, host, &addr) > 0)
611 return address_list_new_one (&addr);
614 addr_ipv4 = (unsigned long)inet_addr (host);
615 if ((int)addr_ipv4 != -1)
617 /* ADDR is defined to be in network byte order, which is what
618 this returns, so we can just copy it to STORE_IP. However,
619 on big endian 64-bit architectures the value will be stored
620 in the *last*, not first four bytes. OFFSET makes sure that
621 we copy the correct four bytes. */
623 #ifdef WORDS_BIGENDIAN
624 offset = sizeof (unsigned long) - sizeof (ip4_address);
626 map_ipv4_to_ip ((ip4_address *)((char *)&addr_ipv4 + offset), &addr);
627 return address_list_new_one (&addr);
630 if (host_name_addresses_map)
632 al = hash_table_get (host_name_addresses_map, host);
636 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
643 logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
645 /* Host name lookup goes on below. */
647 #ifdef HAVE_GETADDRINFO
649 struct addrinfo hints, *ai;
652 memset (&hints, 0, sizeof (hints));
653 if (ip_default_family == AF_INET)
654 hints.ai_family = AF_INET;
656 hints.ai_family = PF_UNSPEC;
657 hints.ai_socktype = SOCK_STREAM;
658 err = getaddrinfo_with_timeout (host, NULL, &hints, &ai, opt.timeout);
660 if (err != 0 || ai == NULL)
663 logprintf (LOG_VERBOSE, _("failed: %s.\n"),
664 err != EAI_SYSTEM ? gai_strerror (err) : strerror (errno));
667 al = address_list_from_addrinfo (ai);
672 struct hostent *hptr = gethostbyname_with_timeout (host, opt.timeout);
677 if (errno != ETIMEDOUT)
678 logprintf (LOG_VERBOSE, _("failed: %s.\n"), herrmsg (h_errno));
680 logputs (LOG_VERBOSE, _("failed: timed out.\n"));
684 /* Do all systems have h_addr_list, or is it a newer thing? If
685 the latter, use address_list_new_one. */
686 al = address_list_new (hptr->h_addr_list);
691 logprintf (LOG_VERBOSE, _("done.\n"));
693 /* Cache the lookup information. */
695 cache_host_lookup (host, al);
700 /* Determine whether a URL is acceptable to be followed, according to
701 a list of domains to accept. */
703 accept_domain (struct url *u)
705 assert (u->host != NULL);
708 if (!sufmatch ((const char **)opt.domains, u->host))
711 if (opt.exclude_domains)
713 if (sufmatch ((const char **)opt.exclude_domains, u->host))
719 /* Check whether WHAT is matched in LIST, each element of LIST being a
720 pattern to match WHAT against, using backward matching (see
721 match_backwards() in utils.c).
723 If an element of LIST matched, 1 is returned, 0 otherwise. */
725 sufmatch (const char **list, const char *what)
730 for (i = 0; list[i]; i++)
732 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
733 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
735 /* The domain must be first to reach to beginning. */
742 /* Print error messages for host errors. */
746 /* Can't use switch since some constants are equal (at least on my
747 system), and the compiler signals "duplicate case value". */
748 if (error == HOST_NOT_FOUND
749 || error == NO_RECOVERY
751 || error == NO_ADDRESS
752 || error == TRY_AGAIN)
753 return _("Host not found");
755 return _("Unknown error");
759 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
761 struct address_list *al;
763 xfree (key); /* host */
765 al = (struct address_list *)value;
766 assert (al->refcount == 1);
767 address_list_delete (al);
775 if (host_name_addresses_map)
777 hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
778 hash_table_destroy (host_name_addresses_map);
779 host_name_addresses_map = NULL;