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. */
31 #include <sys/types.h>
36 # include <sys/socket.h>
37 # include <netinet/in.h>
39 # include <arpa/inet.h>
45 #define NO_ADDRESS NO_DATA
48 #ifdef HAVE_SYS_UTSNAME_H
49 # include <sys/utsname.h>
70 int ip_default_family = AF_INET6;
72 int ip_default_family = AF_INET;
75 /* Mapping between known hosts and to lists of their addresses. */
77 static struct hash_table *host_name_addresses_map;
79 /* Lists of addresses. This should eventually be extended to handle
83 int count; /* number of adrresses */
84 ip_address *addresses; /* pointer to the string of addresses */
86 int faulty; /* number of addresses known not to work. */
87 int refcount; /* so we know whether to free it or not. */
90 /* Get the bounds of the address list. */
93 address_list_get_bounds (struct address_list *al, int *start, int *end)
99 /* Copy address number INDEX to IP_STORE. */
102 address_list_copy_one (struct address_list *al, int index, ip_address *ip_store)
104 assert (index >= al->faulty && index < al->count);
105 memcpy (ip_store, al->addresses + index, sizeof (ip_address));
108 /* Check whether two address lists have all their IPs in common. */
111 address_list_match_all (struct address_list *al1, struct address_list *al2)
115 if (al1->count != al2->count)
117 return 0 == memcmp (al1->addresses, al2->addresses,
118 al1->count * sizeof (ip_address));
121 /* Mark the INDEXth element of AL as faulty, so that the next time
122 this address list is used, the faulty element will be skipped. */
125 address_list_set_faulty (struct address_list *al, int index)
127 /* We assume that the address list is traversed in order, so that a
128 "faulty" attempt is always preceded with all-faulty addresses,
129 and this is how Wget uses it. */
130 assert (index == al->faulty);
133 if (al->faulty >= al->count)
134 /* All addresses have been proven faulty. Since there's not much
135 sense in returning the user an empty address list the next
136 time, we'll rather make them all clean, so that they can be
143 * address_list_from_addrinfo
145 * This function transform an addrinfo links list in and address_list.
148 * addrinfo* Linkt list of addrinfo
151 * address_list* New allocated address_list
153 static struct address_list *
154 address_list_from_addrinfo (struct addrinfo *ai)
156 struct address_list *al;
157 struct addrinfo *ai_head = ai;
161 for (ai = ai_head; ai; ai = ai->ai_next)
162 if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
167 al = xmalloc (sizeof (struct address_list));
168 al->addresses = xmalloc (cnt * sizeof (ip_address));
173 for (i = 0, ai = ai_head; ai; ai = ai->ai_next)
174 if (ai->ai_family == AF_INET6)
176 struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)ai->ai_addr;
177 memcpy (al->addresses + i, &sin6->sin6_addr, 16);
180 else if (ai->ai_family == AF_INET)
182 struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;
183 map_ipv4_to_ip ((ip4_address *)&sin->sin_addr, al->addresses + i);
190 /* Create an address_list out of a NULL-terminated list of addresses,
191 as returned by gethostbyname. */
192 static struct address_list *
193 address_list_new (char **h_addr_list)
197 struct address_list *al = xmalloc (sizeof (struct address_list));
199 while (h_addr_list[count])
204 al->addresses = xmalloc (count * sizeof (ip_address));
207 for (i = 0; i < count; i++)
208 map_ipv4_to_ip ((ip4_address *)h_addr_list[i], al->addresses + i);
214 /* Like address_list_new, but initialized with only one address. */
216 static struct address_list *
217 address_list_new_one (ip_address *addr)
219 struct address_list *al = xmalloc (sizeof (struct address_list));
222 al->addresses = xmalloc (sizeof (ip_address));
224 memcpy (al->addresses, addr, sizeof (ip_address));
230 address_list_delete (struct address_list *al)
232 xfree (al->addresses);
237 address_list_release (struct address_list *al)
240 DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
241 if (al->refcount <= 0)
243 DEBUGP (("Deleting unused %p.\n", al));
244 address_list_delete (al);
249 * wget_sockaddr_set_address
251 * This function takes an wget_sockaddr and fill in the protocol type,
252 * the port number and the address, there NULL in address means wildcard.
253 * Unsuported adress family will abort the whole programm.
256 * wget_sockaddr* The space to be filled
257 * int The wished protocol
258 * unsigned short The port
259 * const ip_address The Binary IP adress
262 * - Only modify 1. param
265 wget_sockaddr_set_address (wget_sockaddr *sa,
266 int ip_family, unsigned short port, ip_address *addr)
268 if (ip_family == AF_INET)
270 sa->sin.sin_family = ip_family;
271 sa->sin.sin_port = htons (port);
273 memset (&sa->sin.sin_addr, 0, sizeof(ip4_address));
277 if (!map_ip_to_ipv4 (addr, &addr4))
278 /* should the callers have prevented this? */
280 memcpy (&sa->sin.sin_addr, &addr4, sizeof(ip4_address));
285 if (ip_family == AF_INET6)
287 sa->sin6.sin6_family = ip_family;
288 sa->sin6.sin6_port = htons (port);
290 memset (&sa->sin6.sin6_addr, 0 , 16);
292 memcpy (&sa->sin6.sin6_addr, addr, 16);
300 * wget_sockaddr_set_port
302 * This funtion only fill the port of the socket information.
303 * If the protocol is not supported nothing is done.
304 * Unsuported adress family will abort the whole programm.
307 * that the IP-Protocol already is set.
310 * wget_sockaddr* The space there port should be entered
311 * unsigned int The port that should be entered in host order
314 * - Only modify 1. param
317 wget_sockaddr_set_port (wget_sockaddr *sa, unsigned short port)
319 if (sa->sa.sa_family == AF_INET)
321 sa->sin.sin_port = htons (port);
325 if (sa->sa.sa_family == AF_INET6)
327 sa->sin6.sin6_port = htons (port);
335 * wget_sockaddr_get_addr
337 * This function return the adress from an sockaddr as byte string.
338 * Unsuported adress family will abort the whole programm.
341 * that the IP-Protocol already is set.
344 * wget_sockaddr* Socket Information
347 * unsigned char * IP address as byte string.
350 wget_sockaddr_get_addr (wget_sockaddr *sa)
352 if (sa->sa.sa_family == AF_INET)
353 return &sa->sin.sin_addr;
355 if (sa->sa.sa_family == AF_INET6)
356 return &sa->sin6.sin6_addr;
364 * wget_sockaddr_get_port
366 * This function only return the port from the input structure
367 * Unsuported adress family will abort the whole programm.
370 * that the IP-Protocol already is set.
373 * wget_sockaddr* Information where to get the port
376 * unsigned short Port Number in host order.
379 wget_sockaddr_get_port (const wget_sockaddr *sa)
381 if (sa->sa.sa_family == AF_INET)
382 return htons (sa->sin.sin_port);
384 if (sa->sa.sa_family == AF_INET6)
385 return htons (sa->sin6.sin6_port);
388 /* do not complain about return nothing */
395 * This function return the length of the sockaddr corresponding to
396 * the acutall prefered protocol for (bind, connect etc...)
397 * Unsuported adress family will abort the whole programm.
400 * that the IP-Protocol already is set.
403 * - Public IP-Family Information
406 * int structure length for socket options
411 if (ip_default_family == AF_INET)
412 return sizeof (struct sockaddr_in);
414 if (ip_default_family == AF_INET6)
415 return sizeof (struct sockaddr_in6);
418 /* do not complain about return nothing */
423 * Map an IPv4 adress to the internal adress format.
426 map_ipv4_to_ip (ip4_address *ipv4, ip_address *ip)
429 static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
430 memcpy ((char *)ip + 12, ipv4 , 4);
431 memcpy ((char *)ip + 0, ipv64, 12);
433 if ((char *)ip != (char *)ipv4)
434 memcpy (ip, ipv4, 4);
438 /* Detect whether an IP adress represents an IPv4 address and, if so,
439 copy it to IPV4. 0 is returned on failure.
440 This operation always succeeds when Wget is compiled without IPv6.
441 If IPV4 is NULL, don't copy, just detect. */
444 map_ip_to_ipv4 (ip_address *ip, ip4_address *ipv4)
447 static unsigned char ipv64[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff};
448 if (0 != memcmp (ip, ipv64, 12))
451 memcpy (ipv4, (char *)ip + 12, 4);
454 memcpy (ipv4, (char *)ip, 4);
459 /* Pretty-print ADDR. When compiled without IPv6, this is the same as
460 inet_ntoa. With IPv6, it either prints an IPv6 address or an IPv4
464 pretty_print_address (ip_address *addr)
468 static char buf[128];
470 if (map_ip_to_ipv4 (addr, &addr4))
471 return inet_ntoa (*(struct in_addr *)&addr4);
473 if (!inet_ntop (AF_INET6, addr, buf, sizeof (buf)))
477 return inet_ntoa (*(struct in_addr *)addr);
480 /* Add host name HOST with the address ADDR_TEXT to the cache.
481 ADDR_LIST is a NULL-terminated list of addresses, as in struct
485 cache_host_lookup (const char *host, struct address_list *al)
487 if (!host_name_addresses_map)
488 host_name_addresses_map = make_nocase_string_hash_table (0);
491 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
497 debug_logprintf ("Caching %s =>", host);
498 for (i = 0; i < al->count; i++)
499 debug_logprintf (" %s", pretty_print_address (al->addresses + i));
500 debug_logprintf ("\n");
505 struct address_list *
506 lookup_host (const char *host, int silent)
508 struct address_list *al = NULL;
509 unsigned long addr_ipv4; /* #### use a 32-bit type here. */
512 /* First, try to check whether the address is already a numeric
516 if (inet_pton (AF_INET6, host, &addr) > 0)
517 return address_list_new_one (&addr);
520 addr_ipv4 = (unsigned long)inet_addr (host);
521 if ((int)addr_ipv4 != -1)
523 /* ADDR is defined to be in network byte order, which is what
524 this returns, so we can just copy it to STORE_IP. However,
525 on big endian 64-bit architectures the value will be stored
526 in the *last*, not first four bytes. OFFSET makes sure that
527 we copy the correct four bytes. */
529 #ifdef WORDS_BIGENDIAN
530 offset = sizeof (unsigned long) - sizeof (ip4_address);
532 map_ipv4_to_ip ((ip4_address *)((char *)&addr_ipv4 + offset), &addr);
533 return address_list_new_one (&addr);
536 if (host_name_addresses_map)
538 al = hash_table_get (host_name_addresses_map, host);
542 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
549 logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
553 struct addrinfo hints, *ai;
556 memset (&hints, 0, sizeof (hints));
557 if (ip_default_family == AF_INET)
558 hints.ai_family = AF_INET;
560 hints.ai_family = PF_UNSPEC;
561 hints.ai_socktype = SOCK_STREAM;
562 err = getaddrinfo (host, NULL, &hints, &ai);
564 if (err != 0 || ai == NULL)
567 logprintf (LOG_VERBOSE, _("failed: %s.\n"), gai_strerror (err));
570 al = address_list_from_addrinfo (ai);
575 struct hostent *hptr = gethostbyname (host);
579 logprintf (LOG_VERBOSE, _("failed: %s.\n"), herrmsg (h_errno));
582 /* Do all systems have h_addr_list, or is it a newer thing? If
583 the latter, use address_list_new_one. */
584 al = address_list_new (hptr->h_addr_list);
589 logprintf (LOG_VERBOSE, _("done.\n"));
591 /* Cache the lookup information. */
592 cache_host_lookup (host, al);
597 /* Determine whether a URL is acceptable to be followed, according to
598 a list of domains to accept. */
600 accept_domain (struct url *u)
602 assert (u->host != NULL);
605 if (!sufmatch ((const char **)opt.domains, u->host))
608 if (opt.exclude_domains)
610 if (sufmatch ((const char **)opt.exclude_domains, u->host))
616 /* Check whether WHAT is matched in LIST, each element of LIST being a
617 pattern to match WHAT against, using backward matching (see
618 match_backwards() in utils.c).
620 If an element of LIST matched, 1 is returned, 0 otherwise. */
622 sufmatch (const char **list, const char *what)
627 for (i = 0; list[i]; i++)
629 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
630 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
632 /* The domain must be first to reach to beginning. */
639 /* Print error messages for host errors. */
643 /* Can't use switch since some constants are equal (at least on my
644 system), and the compiler signals "duplicate case value". */
645 if (error == HOST_NOT_FOUND
646 || error == NO_RECOVERY
648 || error == NO_ADDRESS
649 || error == TRY_AGAIN)
650 return _("Host not found");
652 return _("Unknown error");
656 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
658 struct address_list *al;
660 xfree (key); /* host */
662 al = (struct address_list *)value;
663 assert (al->refcount == 1);
664 address_list_delete (al);
672 if (host_name_addresses_map)
674 hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
675 hash_table_destroy (host_name_addresses_map);
676 host_name_addresses_map = NULL;