]> sjero.net Git - wget/blob - src/host.c
[svn] Restore sanity check in address_list_set_faulty.
[wget] / src / host.c
1 /* Host name resolution and matching.
2    Copyright (C) 1995, 1996, 1997, 2000, 2001 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
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.
10
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.
15
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.  */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #ifdef HAVE_STRING_H
25 # include <string.h>
26 #else
27 # include <strings.h>
28 #endif
29 #include <assert.h>
30 #include <sys/types.h>
31
32 #ifdef WINDOWS
33 # include <winsock.h>
34 #else
35 # include <sys/socket.h>
36 # include <netinet/in.h>
37 # ifndef __BEOS__
38 #  include <arpa/inet.h>
39 # endif
40 # include <netdb.h>
41 #endif /* WINDOWS */
42
43 #ifndef NO_ADDRESS
44 #define NO_ADDRESS NO_DATA
45 #endif
46
47 #ifdef HAVE_SYS_UTSNAME_H
48 # include <sys/utsname.h>
49 #endif
50 #include <errno.h>
51
52 #include "wget.h"
53 #include "utils.h"
54 #include "host.h"
55 #include "url.h"
56 #include "hash.h"
57
58 #ifndef errno
59 extern int errno;
60 #endif
61
62 #ifndef h_errno
63 # ifndef __CYGWIN__
64 extern int h_errno;
65 # endif
66 #endif
67
68 #define IP4_ADDRESS_LENGTH 4
69
70 /* Mapping between known hosts and to lists of their addresses. */
71
72 static struct hash_table *host_name_addresses_map;
73 \f
74 /* Lists of addresses.  This should eventually be extended to handle
75    IPv6.  */
76
77 struct address_list {
78   int count;                    /* number of adrresses */
79   unsigned char *buffer;        /* buffer which holds all of them. */
80
81   int faulty;                   /* number of addresses known not to
82                                    work. */
83   int refcount;                 /* so we know whether to free it or
84                                    not. */
85 };
86
87 #define ADDR_LOCATION(al, index) ((al)->buffer + index * IP4_ADDRESS_LENGTH)
88
89 /* Get the bounds of the address list.  */
90
91 void
92 address_list_get_bounds (struct address_list *al, int *start, int *end)
93 {
94   *start = al->faulty;
95   *end   = al->count;
96 }
97
98 /* Copy address number INDEX to IP_STORE.  */
99
100 void
101 address_list_copy_one (struct address_list *al, int index,
102                        unsigned char *ip_store)
103 {
104   assert (index >= al->faulty && index < al->count);
105   memcpy (ip_store, ADDR_LOCATION (al, index), IP4_ADDRESS_LENGTH);
106 }
107
108 /* Check whether two address lists have all their IPs in common.  */
109
110 int
111 address_list_match_all (struct address_list *al1, struct address_list *al2)
112 {
113   if (al1 == al2)
114     return 1;
115   if (al1->count != al2->count)
116     return 0;
117   return 0 == memcmp (al1->buffer, al2->buffer,
118                       al1->count * IP4_ADDRESS_LENGTH);
119 }
120
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.  */
123
124 void
125 address_list_set_faulty (struct address_list *al, int index)
126 {
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);
131
132   ++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
137        retried anew.  */
138     al->faulty = 0;
139 }
140
141 /* Create an address_list out of a NULL-terminated list of addresses,
142    as returned by gethostbyname.  */
143
144 static struct address_list *
145 address_list_new (char **h_addr_list)
146 {
147   int count = 0, i;
148
149   struct address_list *al = xmalloc (sizeof (struct address_list));
150
151   while (h_addr_list[count])
152     ++count;
153   assert (count > 0);
154   al->count    = count;
155   al->faulty   = 0;
156   al->buffer   = xmalloc (count * IP4_ADDRESS_LENGTH);
157   al->refcount = 1;
158
159   for (i = 0; i < count; i++)
160     memcpy (ADDR_LOCATION (al, i), h_addr_list[i], IP4_ADDRESS_LENGTH);
161
162   return al;
163 }
164
165 /* Like address_list_new, but initialized with only one address. */
166
167 static struct address_list *
168 address_list_new_one (const char *addr)
169 {
170   struct address_list *al = xmalloc (sizeof (struct address_list));
171   al->count    = 1;
172   al->faulty   = 0;
173   al->buffer   = xmalloc (IP4_ADDRESS_LENGTH);
174   al->refcount = 1;
175   memcpy (ADDR_LOCATION (al, 0), addr, IP4_ADDRESS_LENGTH);
176
177   return al;
178 }
179
180 static void
181 address_list_delete (struct address_list *al)
182 {
183   xfree (al->buffer);
184   xfree (al);
185 }
186
187 void
188 address_list_release (struct address_list *al)
189 {
190   --al->refcount;
191   DEBUGP (("Releasing %p (new refcount %d).\n", al, al->refcount));
192   if (al->refcount <= 0)
193     {
194       DEBUGP (("Deleting unused %p.\n", al));
195       address_list_delete (al);
196     }
197 }
198 \f
199 /* The same as inet_ntoa, but without the need for a cast, or for
200    #including the netinet stuff.  */
201
202 char *
203 pretty_print_address (const unsigned char *addr)
204 {
205   return inet_ntoa (*(struct in_addr *)addr);
206 }
207
208 /* Add host name HOST with the address ADDR_TEXT to the cache.
209    ADDR_LIST is a NULL-terminated list of addresses, as in struct
210    hostent.  */
211
212 static void
213 cache_host_lookup (const char *host, struct address_list *al)
214 {
215   if (!host_name_addresses_map)
216     host_name_addresses_map = make_nocase_string_hash_table (0);
217
218   ++al->refcount;
219   hash_table_put (host_name_addresses_map, xstrdup_lower (host), al);
220
221 #ifdef DEBUG
222   if (opt.debug)
223     {
224       int i;
225       debug_logprintf ("Caching %s =>", host);
226       for (i = 0; i < al->count; i++)
227         debug_logprintf (" %s",
228                          pretty_print_address (ADDR_LOCATION (al, i)));
229       debug_logprintf ("\n");
230     }
231 #endif
232 }
233
234 struct address_list *
235 lookup_host (const char *host, int silent)
236 {
237   struct address_list *al = NULL;
238   unsigned long addr;
239   struct hostent *hptr;
240
241   /* If the address is of the form d.d.d.d, no further lookup is
242      needed.  */
243   addr = (unsigned long)inet_addr (host);
244   if ((int)addr != -1)
245     {
246       /* ADDR is defined to be in network byte order, which is what
247          this returns, so we can just copy it to STORE_IP.  However,
248          on big endian 64-bit architectures the value will be stored
249          in the *last*, not first four bytes.  OFFSET makes sure that
250          we copy the correct four bytes.  */
251       int offset;
252 #ifdef WORDS_BIGENDIAN
253       offset = sizeof (unsigned long) - IP4_ADDRESS_LENGTH;
254 #else
255       offset = 0;
256 #endif
257       return address_list_new_one ((char *)&addr + offset);
258     }
259
260   /* By now we know that the host name we got is not of the form
261      d.d.d.d.  Try to find it in our cache of host names.  */
262   if (host_name_addresses_map)
263     al = hash_table_get (host_name_addresses_map, host);
264
265   if (al)
266     {
267       DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, al));
268       ++al->refcount;
269       return al;
270     }
271
272   if (!silent)
273     logprintf (LOG_VERBOSE, _("Resolving %s... "), host);
274
275   /* Look up the host using gethostbyname().  */
276   hptr = gethostbyname (host);
277   if (!hptr)
278     {
279       if (!silent)
280         logprintf (LOG_VERBOSE, _("failed: %s.\n"), herrmsg (h_errno));
281       return NULL;
282     }
283
284   if (!silent)
285     logprintf (LOG_VERBOSE, _("done.\n"));
286
287   /* Do all systems have h_addr_list, or is it a newer thing?  If the
288      latter, use address_list_new_one.  */
289   al = address_list_new (hptr->h_addr_list);
290
291   /* Cache the lookup information. */
292   cache_host_lookup (host, al);
293
294   return al;
295 }
296 \f
297 /* Determine whether a URL is acceptable to be followed, according to
298    a list of domains to accept.  */
299 int
300 accept_domain (struct url *u)
301 {
302   assert (u->host != NULL);
303   if (opt.domains)
304     {
305       if (!sufmatch ((const char **)opt.domains, u->host))
306         return 0;
307     }
308   if (opt.exclude_domains)
309     {
310       if (sufmatch ((const char **)opt.exclude_domains, u->host))
311         return 0;
312     }
313   return 1;
314 }
315
316 /* Check whether WHAT is matched in LIST, each element of LIST being a
317    pattern to match WHAT against, using backward matching (see
318    match_backwards() in utils.c).
319
320    If an element of LIST matched, 1 is returned, 0 otherwise.  */
321 int
322 sufmatch (const char **list, const char *what)
323 {
324   int i, j, k, lw;
325
326   lw = strlen (what);
327   for (i = 0; list[i]; i++)
328     {
329       for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
330         if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
331           break;
332       /* The domain must be first to reach to beginning.  */
333       if (j == -1)
334         return 1;
335     }
336   return 0;
337 }
338
339 /* Print error messages for host errors.  */
340 char *
341 herrmsg (int error)
342 {
343   /* Can't use switch since some constants are equal (at least on my
344      system), and the compiler signals "duplicate case value".  */
345   if (error == HOST_NOT_FOUND
346       || error == NO_RECOVERY
347       || error == NO_DATA
348       || error == NO_ADDRESS
349       || error == TRY_AGAIN)
350     return _("Host not found");
351   else
352     return _("Unknown error");
353 }
354
355 static int
356 host_cleanup_mapper (void *key, void *value, void *arg_ignored)
357 {
358   struct address_list *al;
359
360   xfree (key);                  /* host */
361
362   al = (struct address_list *)value;
363   assert (al->refcount == 1);
364   address_list_delete (al);
365
366   return 0;
367 }
368
369 void
370 host_cleanup (void)
371 {
372   if (host_name_addresses_map)
373     {
374       hash_table_map (host_name_addresses_map, host_cleanup_mapper, NULL);
375       hash_table_destroy (host_name_addresses_map);
376       host_name_addresses_map = NULL;
377     }
378 }