1 /* Dealing with host names.
2 Copyright (C) 1995, 1996, 1997, 2000 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>
62 /* Mapping between all known hosts to their addresses (n.n.n.n). */
64 /* #### We should map to *lists* of IP addresses. */
66 struct hash_table *host_name_address_map;
70 /* This function is no longer used. */
72 /* The same as gethostbyname, but supports internet addresses of the
73 form `N.N.N.N'. On some systems gethostbyname() knows how to do
74 this automatically. */
76 ngethostbyname (const char *name)
81 addr = (unsigned long)inet_addr (name);
83 hp = gethostbyaddr ((char *)&addr, sizeof (addr), AF_INET);
85 hp = gethostbyname (name);
90 /* Add host name HOST with the address ADDR_TEXT to the cache. */
93 add_host_to_cache (const char *host, const char *addr_text)
95 DEBUGP (("Caching %s => %s\n", host, addr_text));
97 if (!host_name_address_map)
98 host_name_address_map = make_nocase_string_hash_table (0);
100 hash_table_put (host_name_address_map,
101 xstrdup_lower (host), xstrdup (addr_text));
104 /* Store the address of HOSTNAME, internet-style (four octets in
105 network order), to WHERE. First try to get the address from the
106 cache; if it is not available, call the DNS functions and update
109 Return 1 on successful finding of the hostname, 0 otherwise. */
111 lookup_host (const char *hostname, unsigned char *store_ip)
114 char *addr_text = NULL;
115 struct hostent *hptr;
119 /* If the address is of the form d.d.d.d, no further lookup is
121 addr = (unsigned long)inet_addr (hostname);
124 /* ADDR is defined to be in network byte order, which is what
125 this returns, so we can just copy it to STORE_IP. However,
126 on big endian 64-bit architectures the value will be stored
127 in the *last*, not first four bytes. OFFSET makes sure that
128 we copy the correct four bytes. */
131 #ifdef WORDS_BIGENDIAN
132 offset = sizeof (unsigned long) - 4;
136 memcpy (store_ip, (char *)&addr + offset, 4);
140 /* By now we know that the host name we got is not of the form
141 d.d.d.d. Try to find it in our cache of host names. */
142 if (host_name_address_map)
143 addr_text = hash_table_get (host_name_address_map, hostname);
147 DEBUGP (("Found %s in host_name_address_map: %s\n",
148 hostname, addr_text));
149 addr = (unsigned long)inet_addr (addr_text);
153 /* Look up the host using gethostbyname(). Note that we use
154 gethostbyname() rather than ngethostbyname(), because we already
155 know that the address is not numerical. */
156 hptr = gethostbyname (hostname);
160 /* Store the IP address to the desired location. */
161 assert (hptr->h_length == 4);
162 memcpy (store_ip, hptr->h_addr_list[0], hptr->h_length);
164 /* Now that we've successfully looked up the host, store this
165 information in the cache. */
167 /* Originally, we copied to in.s_addr, but it appears to be missing
169 memcpy (&in, *hptr->h_addr_list, sizeof (in));
170 inet_s = inet_ntoa (in);
171 add_host_to_cache (hostname, inet_s);
176 /* Determine whether a URL is acceptable to be followed, according to
177 a list of domains to accept. */
179 accept_domain (struct url *u)
181 assert (u->host != NULL);
184 if (!sufmatch ((const char **)opt.domains, u->host))
187 if (opt.exclude_domains)
189 if (sufmatch ((const char **)opt.exclude_domains, u->host))
195 /* Check whether WHAT is matched in LIST, each element of LIST being a
196 pattern to match WHAT against, using backward matching (see
197 match_backwards() in utils.c).
199 If an element of LIST matched, 1 is returned, 0 otherwise. */
201 sufmatch (const char **list, const char *what)
206 for (i = 0; list[i]; i++)
208 for (j = strlen (list[i]), k = lw; j >= 0 && k >= 0; j--, k--)
209 if (TOLOWER (list[i][j]) != TOLOWER (what[k]))
211 /* The domain must be first to reach to beginning. */
218 /* Print error messages for host errors. */
222 /* Can't use switch since some constants are equal (at least on my
223 system), and the compiler signals "duplicate case value". */
224 if (error == HOST_NOT_FOUND
225 || error == NO_RECOVERY
227 || error == NO_ADDRESS
228 || error == TRY_AGAIN)
229 return _("Host not found");
231 return _("Unknown error");
237 free_keys_and_values (host_name_address_map);
238 hash_table_destroy (host_name_address_map);