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. */
30 #include <sys/types.h>
35 # include <sys/socket.h>
36 # include <netinet/in.h>
38 # include <arpa/inet.h>
44 #define NO_ADDRESS NO_DATA
47 #ifdef HAVE_SYS_UTSNAME_H
48 # include <sys/utsname.h>
68 /* An IPv4 address is simply a 4-byte quantity. */
69 typedef unsigned char ipv4_address[4];
71 /* Mapping between known hosts and to lists of their addresses. */
73 static struct hash_table *host_name_addresses_map;
75 /* Lists of addresses. This should eventually be extended to handle
79 int count; /* number of adrresses */
80 ipv4_address *addresses; /* pointer to the string of addresses */
82 int faulty; /* number of addresses known not to
84 int refcount; /* so we know whether to free it or
88 /* Get the bounds of the address list. */
91 address_list_get_bounds (struct address_list *al, int *start, int *end)
97 /* Copy address number INDEX to IP_STORE. */
100 address_list_copy_one (struct address_list *al, int index,
101 unsigned char *ip_store)
103 assert (index >= al->faulty && index < al->count);
104 memcpy (ip_store, al->addresses + index, sizeof (ipv4_address));
107 /* Check whether two address lists have all their IPs in common. */
110 address_list_match_all (struct address_list *al1, struct address_list *al2)
114 if (al1->count != al2->count)
116 return 0 == memcmp (al1->addresses, al2->addresses,
117 al1->count * sizeof (ipv4_address));
120 /* Mark the INDEXth element of AL as faulty, so that the next time
121 this address list is used, the faulty element will be skipped. */
124 address_list_set_faulty (struct address_list *al, int index)
126 /* We assume that the address list is traversed in order, so that a
127 "faulty" attempt is always preceded with all-faulty addresses,
128 and this is how Wget uses it. */
129 assert (index == al->faulty);
132 if (al->faulty >= al->count)
133 /* All addresses have been proven faulty. Since there's not much
134 sense in returning the user an empty address list the next
135 time, we'll rather make them all clean, so that they can be
140 /* Create an address_list out of a NULL-terminated list of addresses,
141 as returned by gethostbyname. */
143 static struct address_list *
144 address_list_new (char **h_addr_list)
148 struct address_list *al = xmalloc (sizeof (struct address_list));
150 while (h_addr_list[count])
155 al->addresses = xmalloc (count * sizeof (ipv4_address));
158 for (i = 0; i < count; i++)
159 memcpy (al->addresses + i, h_addr_list[i], sizeof (ipv4_address));
164 /* Like address_list_new, but initialized with only one address. */
166 static struct address_list *
167 address_list_new_one (const char *addr)
169 struct address_list *al = xmalloc (sizeof (struct address_list));
172 al->addresses = xmalloc (sizeof (ipv4_address));
174 memcpy (al->addresses, addr, sizeof (ipv4_address));
180 address_list_delete (struct address_list *al)
182 xfree (al->addresses);
187 address_list_release (struct address_list *al)
190 DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
191 if (al->refcount <= 0)
193 DEBUGP (("Deleting unused %p.\n", al));
194 address_list_delete (al);
198 /* The same as inet_ntoa, but without the need for a cast, or for
199 #including the netinet stuff. */
202 pretty_print_address (const void *addr)
204 return inet_ntoa (*(struct in_addr *)addr);
207 /* Add host name HOST with the address ADDR_TEXT to the cache.
208 ADDR_LIST is a NULL-terminated list of addresses, as in struct
212 cache_host_lookup (const char *host, struct address_list *al)
214 if (!host_name_addresses_map)
215 host_name_addresses_map = make_nocase_string_hash_table (0);
218 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
224 debug_logprintf ("Caching %s =>", host);
225 for (i = 0; i < al->count; i++)
226 debug_logprintf (" %s", pretty_print_address (al->addresses + i));
227 debug_logprintf ("\n");
232 struct address_list *
233 lookup_host (const char *host, int silent)
235 struct address_list *al = NULL;
237 struct hostent *hptr;
239 /* If the address is of the form d.d.d.d, no further lookup is
241 addr = (unsigned long)inet_addr (host);
244 /* ADDR is defined to be in network byte order, which is what
245 this returns, so we can just copy it to STORE_IP. However,
246 on big endian 64-bit architectures the value will be stored
247 in the *last*, not first four bytes. OFFSET makes sure that
248 we copy the correct four bytes. */
250 #ifdef WORDS_BIGENDIAN
251 offset = sizeof (unsigned long) - sizeof (ipv4_address);
255 return address_list_new_one ((char *)&addr + offset);
258 /* By now we know that the host name we got is not of the form
259 d.d.d.d. Try to find it in our cache of host names. */
260 if (host_name_addresses_map)
261 al = hash_table_get (host_name_addresses_map, host);
265 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
271 logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
273 /* Look up the host using gethostbyname(). */
274 hptr = gethostbyname (host);
278 logprintf (LOG_VERBOSE, _("failed: %s.\n"), herrmsg (h_errno));
283 logprintf (LOG_VERBOSE, _("done.\n"));
285 /* Do all systems have h_addr_list, or is it a newer thing? If the
286 latter, use address_list_new_one. */
287 al = address_list_new (hptr->h_addr_list);
289 /* Cache the lookup information. */
290 cache_host_lookup (host, al);
295 /* Determine whether a URL is acceptable to be followed, according to
296 a list of domains to accept. */
298 accept_domain (struct url *u)
300 assert (u->host != NULL);
303 if (!sufmatch ((const char **)opt.domains, u->host))
306 if (opt.exclude_domains)
308 if (sufmatch ((const char **)opt.exclude_domains, u->host))
314 /* Check whether WHAT is matched in LIST, each element of LIST being a
315 pattern to match WHAT against, using backward matching (see
316 match_backwards() in utils.c).
318 If an element of LIST matched, 1 is returned, 0 otherwise. */
320 sufmatch (const char **list, const char *what)
325 for (i = 0; list[i]; i++)
327 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
328 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
330 /* The domain must be first to reach to beginning. */
337 /* Print error messages for host errors. */
341 /* Can't use switch since some constants are equal (at least on my
342 system), and the compiler signals "duplicate case value". */
343 if (error == HOST_NOT_FOUND
344 || error == NO_RECOVERY
346 || error == NO_ADDRESS
347 || error == TRY_AGAIN)
348 return _("Host not found");
350 return _("Unknown error");
354 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
356 struct address_list *al;
358 xfree (key); /* host */
360 al = (struct address_list *)value;
361 assert (al->refcount == 1);
362 address_list_delete (al);
370 if (host_name_addresses_map)
372 hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
373 hash_table_destroy (host_name_addresses_map);
374 host_name_addresses_map = NULL;