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 #define IP4_ADDRESS_LENGTH 4
70 /* Mapping between known hosts and to lists of their addresses. */
72 static struct hash_table *host_name_addresses_map;
74 /* Lists of addresses. This should eventually be extended to handle
78 int count; /* number of adrresses */
79 unsigned char *buffer; /* buffer which holds all of them. */
81 int faulty; /* number of addresses known not to
83 int refcount; /* so we know whether to free it or
87 #define ADDR_LOCATION(al, index) ((al)->buffer + index * IP4_ADDRESS_LENGTH)
89 /* Get the bounds of the address list. */
92 address_list_get_bounds (struct address_list *al, int *start, int *end)
98 /* Copy address number INDEX to IP_STORE. */
101 address_list_copy_one (struct address_list *al, int index,
102 unsigned char *ip_store)
104 assert (index >= al->faulty && index < al->count);
105 memcpy (ip_store, ADDR_LOCATION (al, index), IP4_ADDRESS_LENGTH);
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->buffer, al2->buffer,
118 al1->count * IP4_ADDRESS_LENGTH);
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)
128 /* Warning: INDEX is unused, so this assumes that the address list
129 is traversed in order. In the next release, either enable this
130 assert, or use INDEX. */
131 assert (index == al->faulty);
135 if (al->faulty >= al->count)
136 /* All addresses have been proven faulty. Since there's not much
137 sense in returning the user an empty address list the next
138 time, we'll rather make them all clean, so that they can be
143 /* Create an address_list out of a NULL-terminated list of addresses,
144 as returned by gethostbyname. */
146 static struct address_list *
147 address_list_new (char **h_addr_list)
151 struct address_list *al = xmalloc (sizeof (struct address_list));
153 while (h_addr_list[count])
158 al->buffer = xmalloc (count * IP4_ADDRESS_LENGTH);
161 for (i = 0; i < count; i++)
162 memcpy (ADDR_LOCATION (al, i), h_addr_list[i], IP4_ADDRESS_LENGTH);
167 /* Like address_list_new, but initialized with only one address. */
169 static struct address_list *
170 address_list_new_one (const char *addr)
172 struct address_list *al = xmalloc (sizeof (struct address_list));
175 al->buffer = xmalloc (IP4_ADDRESS_LENGTH);
177 memcpy (ADDR_LOCATION (al, 0), addr, IP4_ADDRESS_LENGTH);
183 address_list_delete (struct address_list *al)
190 address_list_release (struct address_list *al)
193 DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
194 if (al->refcount <= 0)
196 DEBUGP (("Deleting unused %p.\n", al));
197 address_list_delete (al);
201 /* The same as inet_ntoa, but without the need for a cast, or for
202 #including the netinet stuff. */
205 pretty_print_address (const unsigned char *addr)
207 return inet_ntoa (*(struct in_addr *)addr);
210 /* Add host name HOST with the address ADDR_TEXT to the cache.
211 ADDR_LIST is a NULL-terminated list of addresses, as in struct
215 cache_host_lookup (const char *host, struct address_list *al)
217 if (!host_name_addresses_map)
218 host_name_addresses_map = make_nocase_string_hash_table (0);
221 hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
227 debug_logprintf ("Caching %s =>", host);
228 for (i = 0; i < al->count; i++)
229 debug_logprintf (" %s",
230 pretty_print_address (ADDR_LOCATION (al, i)));
231 debug_logprintf ("\n");
236 struct address_list *
237 lookup_host (const char *host, int silent)
239 struct address_list *al = NULL;
241 struct hostent *hptr;
243 /* If the address is of the form d.d.d.d, no further lookup is
245 addr = (unsigned long)inet_addr (host);
248 /* ADDR is defined to be in network byte order, which is what
249 this returns, so we can just copy it to STORE_IP. However,
250 on big endian 64-bit architectures the value will be stored
251 in the *last*, not first four bytes. OFFSET makes sure that
252 we copy the correct four bytes. */
254 #ifdef WORDS_BIGENDIAN
255 offset = sizeof (unsigned long) - IP4_ADDRESS_LENGTH;
259 return address_list_new_one ((char *)&addr + offset);
262 /* By now we know that the host name we got is not of the form
263 d.d.d.d. Try to find it in our cache of host names. */
264 if (host_name_addresses_map)
265 al = hash_table_get (host_name_addresses_map, host);
269 DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
275 logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
277 /* Look up the host using gethostbyname(). */
278 hptr = gethostbyname (host);
282 logprintf (LOG_VERBOSE, _("failed: %s.\n"), herrmsg (h_errno));
287 logprintf (LOG_VERBOSE, _("done.\n"));
289 /* Do all systems have h_addr_list, or is it a newer thing? If the
290 latter, use address_list_new_one. */
291 al = address_list_new (hptr->h_addr_list);
293 /* Cache the lookup information. */
294 cache_host_lookup (host, al);
299 /* Determine whether a URL is acceptable to be followed, according to
300 a list of domains to accept. */
302 accept_domain (struct url *u)
304 assert (u->host != NULL);
307 if (!sufmatch ((const char **)opt.domains, u->host))
310 if (opt.exclude_domains)
312 if (sufmatch ((const char **)opt.exclude_domains, u->host))
318 /* Check whether WHAT is matched in LIST, each element of LIST being a
319 pattern to match WHAT against, using backward matching (see
320 match_backwards() in utils.c).
322 If an element of LIST matched, 1 is returned, 0 otherwise. */
324 sufmatch (const char **list, const char *what)
329 for (i = 0; list[i]; i++)
331 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
332 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
334 /* The domain must be first to reach to beginning. */
341 /* Print error messages for host errors. */
345 /* Can't use switch since some constants are equal (at least on my
346 system), and the compiler signals "duplicate case value". */
347 if (error == HOST_NOT_FOUND
348 || error == NO_RECOVERY
350 || error == NO_ADDRESS
351 || error == TRY_AGAIN)
352 return _("Host not found");
354 return _("Unknown error");
358 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
360 struct address_list *al;
362 xfree (key); /* host */
364 al = (struct address_list *)value;
365 assert (al->refcount == 1);
366 address_list_delete (al);
374 if (host_name_addresses_map)
376 hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
377 hash_table_destroy (host_name_addresses_map);
378 host_name_addresses_map = NULL;