| 1 |
/* Host name resolution and matching. |
| 2 |
Copyright (C) 1996-2012, 2015, 2018-2024 Free Software Foundation, |
| 3 |
Inc. |
| 4 |
|
| 5 |
This file is part of GNU Wget. |
| 6 |
|
| 7 |
GNU Wget is free software; you can redistribute it and/or modify |
| 8 |
it under the terms of the GNU General Public License as published by |
| 9 |
the Free Software Foundation; either version 3 of the License, or |
| 10 |
(at your option) any later version. |
| 11 |
|
| 12 |
GNU Wget is distributed in the hope that it will be useful, |
| 13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
GNU General Public License for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with Wget. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
Additional permission under GNU GPL version 3 section 7 |
| 21 |
|
| 22 |
If you modify this program, or any covered work, by linking or |
| 23 |
combining it with the OpenSSL project's OpenSSL library (or a |
| 24 |
modified version of that library), containing parts covered by the |
| 25 |
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation |
| 26 |
grants you additional permission to convey the resulting work. |
| 27 |
Corresponding Source for a non-source form of such a combination |
| 28 |
shall include the source code for the parts of OpenSSL used as well |
| 29 |
as that of the covered work. */ |
| 30 |
|
| 31 |
#include "wget.h" |
| 32 |
|
| 33 |
#include <stdio.h> |
| 34 |
#include <stdlib.h> |
| 35 |
#include <string.h> |
| 36 |
#include <assert.h> |
| 37 |
|
| 38 |
#ifndef WINDOWS |
| 39 |
# include <sys/types.h> |
| 40 |
# include <sys/socket.h> |
| 41 |
# include <netinet/in.h> |
| 42 |
# ifndef __BEOS__ |
| 43 |
# include <arpa/inet.h> |
| 44 |
# endif |
| 45 |
# ifdef __VMS |
| 46 |
# include "vms_ip.h" |
| 47 |
# else /* def __VMS */ |
| 48 |
# include <netdb.h> |
| 49 |
# endif /* def __VMS [else] */ |
| 50 |
# define SET_H_ERRNO(err) ((void)(h_errno = (err))) |
| 51 |
#else /* WINDOWS */ |
| 52 |
# include <winsock2.h> |
| 53 |
# include <ws2tcpip.h> |
| 54 |
# define SET_H_ERRNO(err) WSASetLastError (err) |
| 55 |
#endif /* WINDOWS */ |
| 56 |
|
| 57 |
#include <errno.h> |
| 58 |
|
| 59 |
#include "utils.h" |
| 60 |
#include "host.h" |
| 61 |
#include "url.h" |
| 62 |
#include "hash.h" |
| 63 |
#include "ptimer.h" |
| 64 |
|
| 65 |
#ifndef NO_ADDRESS |
| 66 |
# define NO_ADDRESS NO_DATA |
| 67 |
#endif |
| 68 |
|
| 69 |
#if !HAVE_DECL_H_ERRNO && !defined(WINDOWS) |
| 70 |
extern int h_errno; |
| 71 |
#endif |
| 72 |
|
| 73 |
|
| 74 |
/* Lists of IP addresses that result from running DNS queries. See |
| 75 |
lookup_host for details. */ |
| 76 |
|
| 77 |
struct address_list { |
| 78 |
int count; /* number of addresses */ |
| 79 |
ip_address *addresses; /* pointer to the string of addresses */ |
| 80 |
|
| 81 |
int faulty; /* number of addresses known not to work. */ |
| 82 |
bool connected; /* whether we were able to connect to |
| 83 |
one of the addresses in the list, |
| 84 |
at least once. */ |
| 85 |
|
| 86 |
int refcount; /* reference count; when it drops to |
| 87 |
0, the entry is freed. */ |
| 88 |
}; |
| 89 |
|
| 90 |
/* Get the bounds of the address list. */ |
| 91 |
|
| 92 |
void |
| 93 |
address_list_get_bounds (const struct address_list *al, int *start, int *end) |
| 94 |
{ |
| 95 |
*start = al->faulty; |
| 96 |
*end = al->count; |
| 97 |
} |
| 98 |
|
| 99 |
/* Return a pointer to the address at position POS. */ |
| 100 |
|
| 101 |
const ip_address * |
| 102 |
address_list_address_at (const struct address_list *al, int pos) |
| 103 |
{ |
| 104 |
assert (pos >= al->faulty && pos < al->count); |
| 105 |
return al->addresses + pos; |
| 106 |
} |
| 107 |
|
| 108 |
/* Return true if AL contains IP, false otherwise. */ |
| 109 |
|
| 110 |
bool |
| 111 |
address_list_contains (const struct address_list *al, const ip_address *ip) |
| 112 |
{ |
| 113 |
int i; |
| 114 |
switch (ip->family) |
| 115 |
{ |
| 116 |
case AF_INET: |
| 117 |
for (i = 0; i < al->count; i++) |
| 118 |
{ |
| 119 |
ip_address *cur = al->addresses + i; |
| 120 |
if (cur->family == AF_INET |
| 121 |
&& (cur->data.d4.s_addr == ip->data.d4.s_addr)) |
| 122 |
return true; |
| 123 |
} |
| 124 |
return false; |
| 125 |
#ifdef ENABLE_IPV6 |
| 126 |
case AF_INET6: |
| 127 |
for (i = 0; i < al->count; i++) |
| 128 |
{ |
| 129 |
ip_address *cur = al->addresses + i; |
| 130 |
if (cur->family == AF_INET6 |
| 131 |
#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID |
| 132 |
&& cur->ipv6_scope == ip->ipv6_scope |
| 133 |
#endif |
| 134 |
&& IN6_ARE_ADDR_EQUAL (&cur->data.d6, &ip->data.d6)) |
| 135 |
return true; |
| 136 |
} |
| 137 |
return false; |
| 138 |
#endif /* ENABLE_IPV6 */ |
| 139 |
default: |
| 140 |
abort (); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/* Mark the INDEXth element of AL as faulty, so that the next time |
| 145 |
this address list is used, the faulty element will be skipped. */ |
| 146 |
|
| 147 |
void |
| 148 |
address_list_set_faulty (struct address_list *al, int index) |
| 149 |
{ |
| 150 |
/* We assume that the address list is traversed in order, so that a |
| 151 |
"faulty" attempt is always preceded with all-faulty addresses, |
| 152 |
and this is how Wget uses it. */ |
| 153 |
assert (index == al->faulty); |
| 154 |
if (index != al->faulty) |
| 155 |
{ |
| 156 |
logprintf (LOG_ALWAYS, "index: %d\nal->faulty: %d\n", index, al->faulty); |
| 157 |
logprintf (LOG_ALWAYS, _("Error in handling the address list.\n")); |
| 158 |
logprintf (LOG_ALWAYS, _("Please report this issue to [email protected]\n")); |
| 159 |
abort(); |
| 160 |
} |
| 161 |
|
| 162 |
++al->faulty; |
| 163 |
if (al->faulty >= al->count) |
| 164 |
/* All addresses have been proven faulty. Since there's not much |
| 165 |
sense in returning the user an empty address list the next |
| 166 |
time, we'll rather make them all clean, so that they can be |
| 167 |
retried anew. */ |
| 168 |
al->faulty = 0; |
| 169 |
} |
| 170 |
|
| 171 |
/* Set the "connected" flag to true. This flag used by connect.c to |
| 172 |
see if the host perhaps needs to be resolved again. */ |
| 173 |
|
| 174 |
void |
| 175 |
address_list_set_connected (struct address_list *al) |
| 176 |
{ |
| 177 |
al->connected = true; |
| 178 |
} |
| 179 |
|
| 180 |
/* Return the value of the "connected" flag. */ |
| 181 |
|
| 182 |
bool |
| 183 |
address_list_connected_p (const struct address_list *al) |
| 184 |
{ |
| 185 |
return al->connected; |
| 186 |
} |
| 187 |
|
| 188 |
#ifdef ENABLE_IPV6 |
| 189 |
|
| 190 |
/* Create an address_list from the addresses in the given struct |
| 191 |
addrinfo. */ |
| 192 |
|
| 193 |
static struct address_list * |
| 194 |
address_list_from_addrinfo (const struct addrinfo *ai) |
| 195 |
{ |
| 196 |
struct address_list *al; |
| 197 |
const struct addrinfo *ptr; |
| 198 |
int cnt; |
| 199 |
ip_address *ip; |
| 200 |
|
| 201 |
cnt = 0; |
| 202 |
for (ptr = ai; ptr != NULL ; ptr = ptr->ai_next) |
| 203 |
if (ptr->ai_family == AF_INET || ptr->ai_family == AF_INET6) |
| 204 |
++cnt; |
| 205 |
if (cnt == 0) |
| 206 |
return NULL; |
| 207 |
|
| 208 |
al = xnew0 (struct address_list); |
| 209 |
al->addresses = xnew_array (ip_address, cnt); |
| 210 |
al->count = cnt; |
| 211 |
al->refcount = 1; |
| 212 |
|
| 213 |
ip = al->addresses; |
| 214 |
for (ptr = ai; ptr != NULL; ptr = ptr->ai_next) |
| 215 |
if (ptr->ai_family == AF_INET6) |
| 216 |
{ |
| 217 |
const struct sockaddr_in6 *sin6 = |
| 218 |
(const struct sockaddr_in6 *)ptr->ai_addr; |
| 219 |
ip->family = AF_INET6; |
| 220 |
ip->data.d6 = sin6->sin6_addr; |
| 221 |
#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID |
| 222 |
ip->ipv6_scope = sin6->sin6_scope_id; |
| 223 |
#endif |
| 224 |
++ip; |
| 225 |
} |
| 226 |
else if (ptr->ai_family == AF_INET) |
| 227 |
{ |
| 228 |
const struct sockaddr_in *sin = |
| 229 |
(const struct sockaddr_in *)ptr->ai_addr; |
| 230 |
ip->family = AF_INET; |
| 231 |
ip->data.d4 = sin->sin_addr; |
| 232 |
++ip; |
| 233 |
} |
| 234 |
assert (ip - al->addresses == cnt); |
| 235 |
return al; |
| 236 |
} |
| 237 |
|
| 238 |
#define IS_IPV4(addr) (((const ip_address *) addr)->family == AF_INET) |
| 239 |
|
| 240 |
/* Compare two IP addresses by family, giving preference to the IPv4 |
| 241 |
address (sorting it first). In other words, return -1 if ADDR1 is |
| 242 |
IPv4 and ADDR2 is IPv6, +1 if ADDR1 is IPv6 and ADDR2 is IPv4, and |
| 243 |
0 otherwise. |
| 244 |
|
| 245 |
This is intended to be used as the comparator arg to a qsort-like |
| 246 |
sorting function, which is why it accepts generic pointers. */ |
| 247 |
|
| 248 |
static int |
| 249 |
cmp_prefer_ipv4 (const void *addr1, const void *addr2) |
| 250 |
{ |
| 251 |
return !IS_IPV4 (addr1) - !IS_IPV4 (addr2); |
| 252 |
} |
| 253 |
|
| 254 |
#define IS_IPV6(addr) (((const ip_address *) addr)->family == AF_INET6) |
| 255 |
|
| 256 |
/* Like the above, but give preference to the IPv6 address. */ |
| 257 |
|
| 258 |
static int |
| 259 |
cmp_prefer_ipv6 (const void *addr1, const void *addr2) |
| 260 |
{ |
| 261 |
return !IS_IPV6 (addr1) - !IS_IPV6 (addr2); |
| 262 |
} |
| 263 |
|
| 264 |
#else /* not ENABLE_IPV6 */ |
| 265 |
|
| 266 |
/* Create an address_list from a NULL-terminated vector of IPv4 |
| 267 |
addresses. This kind of vector is returned by gethostbyname. */ |
| 268 |
|
| 269 |
static struct address_list * |
| 270 |
address_list_from_ipv4_addresses (char **vec) |
| 271 |
{ |
| 272 |
int count, i; |
| 273 |
struct address_list *al = xnew0 (struct address_list); |
| 274 |
|
| 275 |
count = 0; |
| 276 |
while (vec[count]) |
| 277 |
++count; |
| 278 |
assert (count > 0); |
| 279 |
|
| 280 |
al->addresses = xnew_array (ip_address, count); |
| 281 |
al->count = count; |
| 282 |
al->refcount = 1; |
| 283 |
|
| 284 |
for (i = 0; i < count; i++) |
| 285 |
{ |
| 286 |
ip_address *ip = &al->addresses[i]; |
| 287 |
ip->family = AF_INET; |
| 288 |
memcpy (IP_INADDR_DATA (ip), vec[i], 4); |
| 289 |
} |
| 290 |
|
| 291 |
return al; |
| 292 |
} |
| 293 |
|
| 294 |
#endif /* not ENABLE_IPV6 */ |
| 295 |
|
| 296 |
static void |
| 297 |
address_list_delete (struct address_list *al) |
| 298 |
{ |
| 299 |
xfree (al->addresses); |
| 300 |
xfree (al); |
| 301 |
} |
| 302 |
|
| 303 |
/* Mark the address list as being no longer in use. This will reduce |
| 304 |
its reference count which will cause the list to be freed when the |
| 305 |
count reaches 0. */ |
| 306 |
|
| 307 |
void |
| 308 |
address_list_release (struct address_list *al) |
| 309 |
{ |
| 310 |
--al->refcount; |
| 311 |
DEBUGP (("Releasing 0x%0*lx (new refcount %d).\n", PTR_FORMAT (al), |
| 312 |
al->refcount)); |
| 313 |
if (al->refcount <= 0) |
| 314 |
{ |
| 315 |
DEBUGP (("Deleting unused 0x%0*lx.\n", PTR_FORMAT (al))); |
| 316 |
address_list_delete (al); |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/* Versions of gethostbyname and getaddrinfo that support timeout. */ |
| 321 |
|
| 322 |
#ifndef ENABLE_IPV6 |
| 323 |
|
| 324 |
struct ghbnwt_context { |
| 325 |
const char *host_name; |
| 326 |
struct hostent *hptr; |
| 327 |
}; |
| 328 |
|
| 329 |
static void |
| 330 |
gethostbyname_with_timeout_callback (void *arg) |
| 331 |
{ |
| 332 |
struct ghbnwt_context *ctx = (struct ghbnwt_context *)arg; |
| 333 |
ctx->hptr = gethostbyname (ctx->host_name); |
| 334 |
} |
| 335 |
|
| 336 |
/* Just like gethostbyname, except it times out after TIMEOUT seconds. |
| 337 |
In case of timeout, NULL is returned and errno is set to ETIMEDOUT. |
| 338 |
The function makes sure that when NULL is returned for reasons |
| 339 |
other than timeout, errno is reset. */ |
| 340 |
|
| 341 |
static struct hostent * |
| 342 |
gethostbyname_with_timeout (const char *host_name, double timeout) |
| 343 |
{ |
| 344 |
struct ghbnwt_context ctx; |
| 345 |
ctx.host_name = host_name; |
| 346 |
if (run_with_timeout (timeout, gethostbyname_with_timeout_callback, &ctx)) |
| 347 |
{ |
| 348 |
SET_H_ERRNO (HOST_NOT_FOUND); |
| 349 |
errno = ETIMEDOUT; |
| 350 |
return NULL; |
| 351 |
} |
| 352 |
if (!ctx.hptr) |
| 353 |
errno = 0; |
| 354 |
return ctx.hptr; |
| 355 |
} |
| 356 |
|
| 357 |
/* Print error messages for host errors. */ |
| 358 |
static const char * |
| 359 |
host_errstr (int error) |
| 360 |
{ |
| 361 |
/* Can't use switch since some of these constants can be equal, |
| 362 |
which makes the compiler complain about duplicate case |
| 363 |
values. */ |
| 364 |
if (error == HOST_NOT_FOUND |
| 365 |
|| error == NO_RECOVERY |
| 366 |
|| error == NO_DATA |
| 367 |
|| error == NO_ADDRESS) |
| 368 |
return _("Unknown host"); |
| 369 |
else if (error == TRY_AGAIN) |
| 370 |
/* Message modeled after what gai_strerror returns in similar |
| 371 |
circumstances. */ |
| 372 |
return _("Temporary failure in name resolution"); |
| 373 |
else |
| 374 |
return _("Unknown error"); |
| 375 |
} |
| 376 |
|
| 377 |
#else /* ENABLE_IPV6 */ |
| 378 |
|
| 379 |
struct gaiwt_context { |
| 380 |
const char *node; |
| 381 |
const char *service; |
| 382 |
const struct addrinfo *hints; |
| 383 |
struct addrinfo **res; |
| 384 |
int exit_code; |
| 385 |
}; |
| 386 |
|
| 387 |
static void |
| 388 |
getaddrinfo_with_timeout_callback (void *arg) |
| 389 |
{ |
| 390 |
struct gaiwt_context *ctx = (struct gaiwt_context *)arg; |
| 391 |
ctx->exit_code = getaddrinfo (ctx->node, ctx->service, ctx->hints, ctx->res); |
| 392 |
} |
| 393 |
|
| 394 |
/* Just like getaddrinfo, except it times out after TIMEOUT seconds. |
| 395 |
In case of timeout, the EAI_SYSTEM error code is returned and errno |
| 396 |
is set to ETIMEDOUT. */ |
| 397 |
|
| 398 |
static int |
| 399 |
getaddrinfo_with_timeout (const char *node, const char *service, |
| 400 |
const struct addrinfo *hints, struct addrinfo **res, |
| 401 |
double timeout) |
| 402 |
{ |
| 403 |
struct gaiwt_context ctx; |
| 404 |
ctx.node = node; |
| 405 |
ctx.service = service; |
| 406 |
ctx.hints = hints; |
| 407 |
ctx.res = res; |
| 408 |
|
| 409 |
if (run_with_timeout (timeout, getaddrinfo_with_timeout_callback, &ctx)) |
| 410 |
{ |
| 411 |
errno = ETIMEDOUT; |
| 412 |
return EAI_SYSTEM; |
| 413 |
} |
| 414 |
return ctx.exit_code; |
| 415 |
} |
| 416 |
|
| 417 |
#endif /* ENABLE_IPV6 */ |
| 418 |
|
| 419 |
/* Return a textual representation of ADDR, i.e. the dotted quad for |
| 420 |
IPv4 addresses, and the colon-separated list of hex words (with all |
| 421 |
zeros omitted, etc.) for IPv6 addresses. */ |
| 422 |
|
| 423 |
const char * |
| 424 |
print_address (const ip_address *addr) |
| 425 |
{ |
| 426 |
static char buf[64]; |
| 427 |
|
| 428 |
if (!inet_ntop (addr->family, IP_INADDR_DATA (addr), buf, sizeof buf)) |
| 429 |
snprintf (buf, sizeof buf, "<error: %s>", strerror (errno)); |
| 430 |
|
| 431 |
return buf; |
| 432 |
} |
| 433 |
|
| 434 |
/* The following two functions were adapted from glibc's |
| 435 |
implementation of inet_pton, written by Paul Vixie. */ |
| 436 |
|
| 437 |
static bool |
| 438 |
is_valid_ipv4_address (const char *str, const char *end) |
| 439 |
{ |
| 440 |
bool saw_digit = false; |
| 441 |
int octets = 0; |
| 442 |
int val = 0; |
| 443 |
|
| 444 |
while (str < end) |
| 445 |
{ |
| 446 |
int ch = *str++; |
| 447 |
|
| 448 |
if (ch >= '0' && ch <= '9') |
| 449 |
{ |
| 450 |
val = val * 10 + (ch - '0'); |
| 451 |
|
| 452 |
if (val > 255) |
| 453 |
return false; |
| 454 |
if (!saw_digit) |
| 455 |
{ |
| 456 |
if (++octets > 4) |
| 457 |
return false; |
| 458 |
saw_digit = true; |
| 459 |
} |
| 460 |
} |
| 461 |
else if (ch == '.' && saw_digit) |
| 462 |
{ |
| 463 |
if (octets == 4) |
| 464 |
return false; |
| 465 |
val = 0; |
| 466 |
saw_digit = false; |
| 467 |
} |
| 468 |
else |
| 469 |
return false; |
| 470 |
} |
| 471 |
if (octets < 4) |
| 472 |
return false; |
| 473 |
|
| 474 |
return true; |
| 475 |
} |
| 476 |
|
| 477 |
bool |
| 478 |
is_valid_ipv6_address (const char *str, const char *end) |
| 479 |
{ |
| 480 |
/* Use lower-case for these to avoid clash with system headers. */ |
| 481 |
enum { |
| 482 |
ns_inaddrsz = 4, |
| 483 |
ns_in6addrsz = 16, |
| 484 |
ns_int16sz = 2 |
| 485 |
}; |
| 486 |
|
| 487 |
const char *curtok; |
| 488 |
int tp; |
| 489 |
const char *colonp; |
| 490 |
bool saw_xdigit; |
| 491 |
unsigned int val; |
| 492 |
|
| 493 |
tp = 0; |
| 494 |
colonp = NULL; |
| 495 |
|
| 496 |
if (str == end) |
| 497 |
return false; |
| 498 |
|
| 499 |
/* Leading :: requires some special handling. */ |
| 500 |
if (*str == ':') |
| 501 |
{ |
| 502 |
++str; |
| 503 |
if (str == end || *str != ':') |
| 504 |
return false; |
| 505 |
} |
| 506 |
|
| 507 |
curtok = str; |
| 508 |
saw_xdigit = false; |
| 509 |
val = 0; |
| 510 |
|
| 511 |
while (str < end) |
| 512 |
{ |
| 513 |
int ch = *str++; |
| 514 |
|
| 515 |
/* if ch is a number, add it to val. */ |
| 516 |
if (c_isxdigit (ch)) |
| 517 |
{ |
| 518 |
val <<= 4; |
| 519 |
val |= _unhex (ch); |
| 520 |
if (val > 0xffff) |
| 521 |
return false; |
| 522 |
saw_xdigit = true; |
| 523 |
continue; |
| 524 |
} |
| 525 |
|
| 526 |
/* if ch is a colon ... */ |
| 527 |
if (ch == ':') |
| 528 |
{ |
| 529 |
curtok = str; |
| 530 |
if (!saw_xdigit) |
| 531 |
{ |
| 532 |
if (colonp != NULL) |
| 533 |
return false; |
| 534 |
colonp = str + tp; |
| 535 |
continue; |
| 536 |
} |
| 537 |
else if (str == end) |
| 538 |
return false; |
| 539 |
if (tp > ns_in6addrsz - ns_int16sz) |
| 540 |
return false; |
| 541 |
tp += ns_int16sz; |
| 542 |
saw_xdigit = false; |
| 543 |
val = 0; |
| 544 |
continue; |
| 545 |
} |
| 546 |
|
| 547 |
/* if ch is a dot ... */ |
| 548 |
if (ch == '.' && (tp <= ns_in6addrsz - ns_inaddrsz) |
| 549 |
&& is_valid_ipv4_address (curtok, end) == 1) |
| 550 |
{ |
| 551 |
tp += ns_inaddrsz; |
| 552 |
saw_xdigit = false; |
| 553 |
break; |
| 554 |
} |
| 555 |
|
| 556 |
return false; |
| 557 |
} |
| 558 |
|
| 559 |
if (saw_xdigit) |
| 560 |
{ |
| 561 |
if (tp > ns_in6addrsz - ns_int16sz) |
| 562 |
return false; |
| 563 |
tp += ns_int16sz; |
| 564 |
} |
| 565 |
|
| 566 |
if (colonp != NULL) |
| 567 |
{ |
| 568 |
if (tp == ns_in6addrsz) |
| 569 |
return false; |
| 570 |
tp = ns_in6addrsz; |
| 571 |
} |
| 572 |
|
| 573 |
if (tp != ns_in6addrsz) |
| 574 |
return false; |
| 575 |
|
| 576 |
return true; |
| 577 |
} |
| 578 |
|
| 579 |
/* Simple host cache, used by lookup_host to speed up resolving. The |
| 580 |
cache doesn't handle TTL because Wget is a fairly short-lived |
| 581 |
application. Refreshing is attempted when connect fails, though -- |
| 582 |
see connect_to_host. */ |
| 583 |
|
| 584 |
/* Mapping between known hosts and to lists of their addresses. */ |
| 585 |
static struct hash_table *host_name_addresses_map; |
| 586 |
|
| 587 |
|
| 588 |
/* Return the host's resolved addresses from the cache, if |
| 589 |
available. */ |
| 590 |
|
| 591 |
static struct address_list * |
| 592 |
cache_query (const char *host) |
| 593 |
{ |
| 594 |
struct address_list *al; |
| 595 |
if (!host_name_addresses_map) |
| 596 |
return NULL; |
| 597 |
al = hash_table_get (host_name_addresses_map, host); |
| 598 |
if (al) |
| 599 |
{ |
| 600 |
DEBUGP (("Found %s in host_name_addresses_map (%p)\n", host, (void *) al)); |
| 601 |
++al->refcount; |
| 602 |
return al; |
| 603 |
} |
| 604 |
return NULL; |
| 605 |
} |
| 606 |
|
| 607 |
/* Cache the DNS lookup of HOST. Subsequent invocations of |
| 608 |
lookup_host will return the cached value. */ |
| 609 |
|
| 610 |
static void |
| 611 |
cache_store (const char *host, struct address_list *al) |
| 612 |
{ |
| 613 |
if (!host_name_addresses_map) |
| 614 |
host_name_addresses_map = make_nocase_string_hash_table (0); |
| 615 |
|
| 616 |
++al->refcount; |
| 617 |
hash_table_put (host_name_addresses_map, xstrdup_lower (host), al); |
| 618 |
|
| 619 |
IF_DEBUG |
| 620 |
{ |
| 621 |
int i; |
| 622 |
debug_logprintf ("Caching %s =>", host); |
| 623 |
for (i = 0; i < al->count; i++) |
| 624 |
debug_logprintf (" %s", print_address (al->addresses + i)); |
| 625 |
debug_logprintf ("\n"); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
/* Remove HOST from the DNS cache. Does nothing is HOST is not in |
| 630 |
the cache. */ |
| 631 |
|
| 632 |
static void |
| 633 |
cache_remove (const char *host) |
| 634 |
{ |
| 635 |
struct address_list *al; |
| 636 |
if (!host_name_addresses_map) |
| 637 |
return; |
| 638 |
al = hash_table_get (host_name_addresses_map, host); |
| 639 |
if (al) |
| 640 |
{ |
| 641 |
address_list_release (al); |
| 642 |
hash_table_remove (host_name_addresses_map, host); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
#ifdef HAVE_LIBCARES |
| 647 |
#include <sys/select.h> |
| 648 |
#include <ares.h> |
| 649 |
extern ares_channel ares; |
| 650 |
|
| 651 |
static struct address_list * |
| 652 |
merge_address_lists (struct address_list *al1, struct address_list *al2) |
| 653 |
{ |
| 654 |
int count = al1->count + al2->count; |
| 655 |
|
| 656 |
/* merge al2 into al1 */ |
| 657 |
al1->addresses = xrealloc (al1->addresses, sizeof (ip_address) * count); |
| 658 |
memcpy (al1->addresses + al1->count, al2->addresses, sizeof (ip_address) * al2->count); |
| 659 |
al1->count = count; |
| 660 |
|
| 661 |
address_list_delete (al2); |
| 662 |
|
| 663 |
return al1; |
| 664 |
} |
| 665 |
|
| 666 |
static struct address_list * |
| 667 |
address_list_from_hostent (struct hostent *host) |
| 668 |
{ |
| 669 |
int count, i; |
| 670 |
struct address_list *al = xnew0 (struct address_list); |
| 671 |
|
| 672 |
for (count = 0; host->h_addr_list[count]; count++) |
| 673 |
; |
| 674 |
|
| 675 |
assert (count > 0); |
| 676 |
|
| 677 |
al->addresses = xnew_array (ip_address, count); |
| 678 |
al->count = count; |
| 679 |
al->refcount = 1; |
| 680 |
|
| 681 |
for (i = 0; i < count; i++) |
| 682 |
{ |
| 683 |
ip_address *ip = &al->addresses[i]; |
| 684 |
ip->family = host->h_addrtype; |
| 685 |
memcpy (IP_INADDR_DATA (ip), host->h_addr_list[i], ip->family == AF_INET ? 4 : 16); |
| 686 |
} |
| 687 |
|
| 688 |
return al; |
| 689 |
} |
| 690 |
|
| 691 |
/* Since GnuLib's select() (i.e. rpl_select()) cannot handle socket-numbers |
| 692 |
* returned from C-ares, we must use the original select() from Winsock. |
| 693 |
*/ |
| 694 |
#ifdef WINDOWS |
| 695 |
#undef select |
| 696 |
#endif |
| 697 |
|
| 698 |
static void |
| 699 |
wait_ares (ares_channel channel) |
| 700 |
{ |
| 701 |
struct ptimer *timer = NULL; |
| 702 |
|
| 703 |
if (opt.dns_timeout) |
| 704 |
timer = ptimer_new (); |
| 705 |
|
| 706 |
for (;;) |
| 707 |
{ |
| 708 |
struct timeval *tvp, tv; |
| 709 |
fd_set read_fds, write_fds; |
| 710 |
int nfds, rc; |
| 711 |
|
| 712 |
FD_ZERO (&read_fds); |
| 713 |
FD_ZERO (&write_fds); |
| 714 |
nfds = ares_fds (channel, &read_fds, &write_fds); |
| 715 |
if (nfds == 0) |
| 716 |
break; |
| 717 |
|
| 718 |
if (timer) |
| 719 |
{ |
| 720 |
double max = opt.dns_timeout - ptimer_measure (timer); |
| 721 |
|
| 722 |
tv.tv_sec = (long) max; |
| 723 |
tv.tv_usec = 1000000 * (max - (long) max); |
| 724 |
tvp = ares_timeout (channel, &tv, &tv); |
| 725 |
} |
| 726 |
else |
| 727 |
tvp = ares_timeout (channel, NULL, &tv); |
| 728 |
|
| 729 |
rc = select (nfds, &read_fds, &write_fds, NULL, tvp); |
| 730 |
if (rc == 0 && timer && ptimer_measure (timer) >= opt.dns_timeout) |
| 731 |
ares_cancel (channel); |
| 732 |
else |
| 733 |
ares_process (channel, &read_fds, &write_fds); |
| 734 |
} |
| 735 |
if (timer) |
| 736 |
ptimer_destroy (timer); |
| 737 |
} |
| 738 |
|
| 739 |
static void |
| 740 |
callback (void *arg, int status, int timeouts _GL_UNUSED, struct hostent *host) |
| 741 |
{ |
| 742 |
struct address_list **al = (struct address_list **) arg; |
| 743 |
|
| 744 |
if (!host || status != ARES_SUCCESS) |
| 745 |
{ |
| 746 |
*al = NULL; |
| 747 |
return; |
| 748 |
} |
| 749 |
|
| 750 |
*al = address_list_from_hostent (host); |
| 751 |
} |
| 752 |
#endif |
| 753 |
|
| 754 |
/* Look up HOST in DNS and return a list of IP addresses. |
| 755 |
|
| 756 |
This function caches its result so that, if the same host is passed |
| 757 |
the second time, the addresses are returned without DNS lookup. |
| 758 |
(Use LH_REFRESH to force lookup, or set opt.dns_cache to 0 to |
| 759 |
globally disable caching.) |
| 760 |
|
| 761 |
The order of the returned addresses is affected by the setting of |
| 762 |
opt.prefer_family: if it is set to prefer_ipv4, IPv4 addresses are |
| 763 |
placed at the beginning; if it is prefer_ipv6, IPv6 ones are placed |
| 764 |
at the beginning; otherwise, the order is left intact. The |
| 765 |
relative order of addresses with the same family is left |
| 766 |
undisturbed in either case. |
| 767 |
|
| 768 |
FLAGS can be a combination of: |
| 769 |
LH_SILENT - don't print the "resolving ... done" messages. |
| 770 |
LH_BIND - resolve addresses for use with bind, which under |
| 771 |
IPv6 means to use AI_PASSIVE flag to getaddrinfo. |
| 772 |
Passive lookups are not cached under IPv6. |
| 773 |
LH_REFRESH - if HOST is cached, remove the entry from the cache |
| 774 |
and resolve it anew. */ |
| 775 |
|
| 776 |
struct address_list * |
| 777 |
lookup_host (const char *host, int flags) |
| 778 |
{ |
| 779 |
struct address_list *al; |
| 780 |
bool silent = !!(flags & LH_SILENT); |
| 781 |
bool use_cache; |
| 782 |
bool numeric_address = false; |
| 783 |
double timeout = opt.dns_timeout; |
| 784 |
|
| 785 |
#ifndef ENABLE_IPV6 |
| 786 |
/* If we're not using getaddrinfo, first check if HOST specifies a |
| 787 |
numeric IPv4 address. Some implementations of gethostbyname |
| 788 |
(e.g. the Ultrix one and possibly Winsock) don't accept |
| 789 |
dotted-decimal IPv4 addresses. */ |
| 790 |
{ |
| 791 |
uint32_t addr_ipv4 = (uint32_t)inet_addr (host); |
| 792 |
if (addr_ipv4 != (uint32_t) -1) |
| 793 |
{ |
| 794 |
/* No need to cache host->addr relation, just return the |
| 795 |
address. */ |
| 796 |
char *vec[2]; |
| 797 |
vec[0] = (char *)&addr_ipv4; |
| 798 |
vec[1] = NULL; |
| 799 |
return address_list_from_ipv4_addresses (vec); |
| 800 |
} |
| 801 |
} |
| 802 |
#else /* ENABLE_IPV6 */ |
| 803 |
/* If we're using getaddrinfo, at least check whether the address is |
| 804 |
already numeric, in which case there is no need to print the |
| 805 |
"Resolving..." output. (This comes at no additional cost since |
| 806 |
the is_valid_ipv*_address are already required for |
| 807 |
url_parse.) */ |
| 808 |
{ |
| 809 |
const char *end = host + strlen (host); |
| 810 |
if (is_valid_ipv4_address (host, end) || is_valid_ipv6_address (host, end)) |
| 811 |
numeric_address = true; |
| 812 |
} |
| 813 |
#endif |
| 814 |
|
| 815 |
/* Cache is normally on, but can be turned off with --no-dns-cache. |
| 816 |
Don't cache passive lookups under IPv6. */ |
| 817 |
use_cache = opt.dns_cache; |
| 818 |
#ifdef ENABLE_IPV6 |
| 819 |
if ((flags & LH_BIND) || numeric_address) |
| 820 |
use_cache = false; |
| 821 |
#endif |
| 822 |
|
| 823 |
/* Try to find the host in the cache so we don't need to talk to the |
| 824 |
resolver. If LH_REFRESH is requested, remove HOST from the cache |
| 825 |
instead. */ |
| 826 |
if (use_cache) |
| 827 |
{ |
| 828 |
if (!(flags & LH_REFRESH)) |
| 829 |
{ |
| 830 |
al = cache_query (host); |
| 831 |
if (al) |
| 832 |
return al; |
| 833 |
} |
| 834 |
else |
| 835 |
cache_remove (host); |
| 836 |
} |
| 837 |
|
| 838 |
/* No luck with the cache; resolve HOST. */ |
| 839 |
|
| 840 |
if (!silent && !numeric_address) |
| 841 |
{ |
| 842 |
char *str = NULL, *name; |
| 843 |
|
| 844 |
if (opt.enable_iri && (name = idn_decode ((char *) host)) != NULL) |
| 845 |
{ |
| 846 |
str = aprintf ("%s (%s)", name, host); |
| 847 |
xfree (name); |
| 848 |
} |
| 849 |
|
| 850 |
logprintf (LOG_VERBOSE, _("Resolving %s... "), |
| 851 |
quotearg_style (escape_quoting_style, str ? str : host)); |
| 852 |
|
| 853 |
xfree (str); |
| 854 |
} |
| 855 |
|
| 856 |
#ifdef ENABLE_IPV6 |
| 857 |
#ifdef HAVE_LIBCARES |
| 858 |
if (ares) |
| 859 |
{ |
| 860 |
struct address_list *al4 = NULL; |
| 861 |
struct address_list *al6 = NULL; |
| 862 |
|
| 863 |
if (opt.ipv4_only || !opt.ipv6_only) |
| 864 |
ares_gethostbyname (ares, host, AF_INET, callback, &al4); |
| 865 |
if (opt.ipv6_only || !opt.ipv4_only) |
| 866 |
ares_gethostbyname (ares, host, AF_INET6, callback, &al6); |
| 867 |
|
| 868 |
wait_ares (ares); |
| 869 |
|
| 870 |
if (al4 && al6) |
| 871 |
al = merge_address_lists (al4, al6); |
| 872 |
else if (al4) |
| 873 |
al = al4; |
| 874 |
else |
| 875 |
al = al6; |
| 876 |
} |
| 877 |
else |
| 878 |
#endif |
| 879 |
{ |
| 880 |
int err; |
| 881 |
struct addrinfo hints, *res; |
| 882 |
|
| 883 |
xzero (hints); |
| 884 |
hints.ai_socktype = SOCK_STREAM; |
| 885 |
if (opt.ipv4_only) |
| 886 |
hints.ai_family = AF_INET; |
| 887 |
else if (opt.ipv6_only) |
| 888 |
hints.ai_family = AF_INET6; |
| 889 |
else |
| 890 |
/* We tried using AI_ADDRCONFIG, but removed it because: it |
| 891 |
misinterprets IPv6 loopbacks, it is broken on AIX 5.1, and |
| 892 |
it's unneeded since we sort the addresses anyway. */ |
| 893 |
hints.ai_family = AF_UNSPEC; |
| 894 |
|
| 895 |
if (flags & LH_BIND) |
| 896 |
hints.ai_flags |= AI_PASSIVE; |
| 897 |
|
| 898 |
#ifdef AI_NUMERICHOST |
| 899 |
if (numeric_address) |
| 900 |
{ |
| 901 |
/* Where available, the AI_NUMERICHOST hint can prevent costly |
| 902 |
access to DNS servers. */ |
| 903 |
hints.ai_flags |= AI_NUMERICHOST; |
| 904 |
timeout = 0; /* no timeout needed when "resolving" |
| 905 |
numeric hosts -- avoid setting up |
| 906 |
signal handlers and such. */ |
| 907 |
} |
| 908 |
#endif |
| 909 |
|
| 910 |
err = getaddrinfo_with_timeout (host, NULL, &hints, &res, timeout); |
| 911 |
|
| 912 |
if (err != 0 || res == NULL) |
| 913 |
{ |
| 914 |
if (!silent) |
| 915 |
logprintf (LOG_VERBOSE, _ ("failed: %s.\n"), |
| 916 |
err != EAI_SYSTEM ? gai_strerror (err) : strerror (errno)); |
| 917 |
return NULL; |
| 918 |
} |
| 919 |
al = address_list_from_addrinfo (res); |
| 920 |
freeaddrinfo (res); |
| 921 |
} |
| 922 |
|
| 923 |
if (!al) |
| 924 |
{ |
| 925 |
logprintf (LOG_VERBOSE, |
| 926 |
_ ("failed: No IPv4/IPv6 addresses for host.\n")); |
| 927 |
return NULL; |
| 928 |
} |
| 929 |
|
| 930 |
/* Reorder addresses so that IPv4 ones (or IPv6 ones, as per |
| 931 |
--prefer-family) come first. Sorting is stable so the order of |
| 932 |
the addresses with the same family is undisturbed. */ |
| 933 |
if (al->count > 1 && opt.prefer_family != prefer_none) |
| 934 |
stable_sort (al->addresses, al->count, sizeof (ip_address), |
| 935 |
opt.prefer_family == prefer_ipv4 |
| 936 |
? cmp_prefer_ipv4 : cmp_prefer_ipv6); |
| 937 |
#else /* not ENABLE_IPV6 */ |
| 938 |
#ifdef HAVE_LIBCARES |
| 939 |
if (ares) |
| 940 |
{ |
| 941 |
ares_gethostbyname (ares, host, AF_INET, callback, &al); |
| 942 |
wait_ares (ares); |
| 943 |
} |
| 944 |
else |
| 945 |
#endif |
| 946 |
{ |
| 947 |
struct hostent *hptr = gethostbyname_with_timeout (host, timeout); |
| 948 |
if (!hptr) |
| 949 |
{ |
| 950 |
if (!silent) |
| 951 |
{ |
| 952 |
if (errno != ETIMEDOUT) |
| 953 |
logprintf (LOG_VERBOSE, _ ("failed: %s.\n"), |
| 954 |
host_errstr (h_errno)); |
| 955 |
else |
| 956 |
logputs (LOG_VERBOSE, _ ("failed: timed out.\n")); |
| 957 |
} |
| 958 |
return NULL; |
| 959 |
} |
| 960 |
/* Do older systems have h_addr_list? */ |
| 961 |
al = address_list_from_ipv4_addresses (hptr->h_addr_list); |
| 962 |
} |
| 963 |
#endif /* not ENABLE_IPV6 */ |
| 964 |
|
| 965 |
/* Print the addresses determined by DNS lookup, but no more than |
| 966 |
three if show_all_dns_entries is not specified. */ |
| 967 |
if (!silent && !numeric_address) |
| 968 |
{ |
| 969 |
int i; |
| 970 |
int printmax = al->count; |
| 971 |
|
| 972 |
if (!opt.show_all_dns_entries && printmax > 3) |
| 973 |
printmax = 3; |
| 974 |
|
| 975 |
for (i = 0; i < printmax; i++) |
| 976 |
{ |
| 977 |
logputs (LOG_VERBOSE, print_address (al->addresses + i)); |
| 978 |
if (i < printmax - 1) |
| 979 |
logputs (LOG_VERBOSE, ", "); |
| 980 |
} |
| 981 |
if (printmax != al->count) |
| 982 |
logputs (LOG_VERBOSE, ", ..."); |
| 983 |
logputs (LOG_VERBOSE, "\n"); |
| 984 |
} |
| 985 |
|
| 986 |
/* Cache the lookup information. */ |
| 987 |
if (use_cache) |
| 988 |
cache_store (host, al); |
| 989 |
|
| 990 |
return al; |
| 991 |
} |
| 992 |
|
| 993 |
/* Determine whether a URL is acceptable to be followed, according to |
| 994 |
a list of domains to accept. */ |
| 995 |
bool |
| 996 |
accept_domain (struct url *u) |
| 997 |
{ |
| 998 |
assert (u->host != NULL); |
| 999 |
if (opt.domains) |
| 1000 |
{ |
| 1001 |
if (!sufmatch ((const char **)opt.domains, u->host)) |
| 1002 |
return false; |
| 1003 |
} |
| 1004 |
if (opt.exclude_domains) |
| 1005 |
{ |
| 1006 |
if (sufmatch ((const char **)opt.exclude_domains, u->host)) |
| 1007 |
return false; |
| 1008 |
} |
| 1009 |
return true; |
| 1010 |
} |
| 1011 |
|
| 1012 |
/* Check whether WHAT is matched in LIST, each element of LIST being a |
| 1013 |
pattern to match WHAT against, using backward matching (see |
| 1014 |
match_backwards() in utils.c). |
| 1015 |
|
| 1016 |
If an element of LIST matched, 1 is returned, 0 otherwise. */ |
| 1017 |
bool |
| 1018 |
sufmatch (const char **list, const char *what) |
| 1019 |
{ |
| 1020 |
int i, j, k, lw; |
| 1021 |
|
| 1022 |
lw = strlen (what); |
| 1023 |
|
| 1024 |
for (i = 0; list[i]; i++) |
| 1025 |
{ |
| 1026 |
j = strlen (list[i]); |
| 1027 |
if (lw < j) |
| 1028 |
continue; /* what is no (sub)domain of list[i] */ |
| 1029 |
|
| 1030 |
for (k = lw; j >= 0 && k >= 0; j--, k--) |
| 1031 |
if (c_tolower (list[i][j]) != c_tolower (what[k])) |
| 1032 |
break; |
| 1033 |
|
| 1034 |
/* Domain or subdomain match |
| 1035 |
* k == -1: exact match |
| 1036 |
* k >= 0 && what[k] == '.': subdomain match |
| 1037 |
* k >= 0 && list[i][0] == '.': dot-prefixed subdomain match |
| 1038 |
*/ |
| 1039 |
if (j == -1 && (k == -1 || what[k] == '.' || list[i][0] == '.')) |
| 1040 |
return true; |
| 1041 |
} |
| 1042 |
|
| 1043 |
return false; |
| 1044 |
} |
| 1045 |
|
| 1046 |
#if defined DEBUG_MALLOC || defined TESTING |
| 1047 |
void |
| 1048 |
host_cleanup (void) |
| 1049 |
{ |
| 1050 |
if (host_name_addresses_map) |
| 1051 |
{ |
| 1052 |
hash_table_iterator iter; |
| 1053 |
for (hash_table_iterate (host_name_addresses_map, &iter); |
| 1054 |
hash_table_iter_next (&iter); |
| 1055 |
) |
| 1056 |
{ |
| 1057 |
char *host = iter.key; |
| 1058 |
struct address_list *al = iter.value; |
| 1059 |
xfree (host); |
| 1060 |
assert (al->refcount == 1); |
| 1061 |
address_list_delete (al); |
| 1062 |
} |
| 1063 |
hash_table_destroy (host_name_addresses_map); |
| 1064 |
host_name_addresses_map = NULL; |
| 1065 |
} |
| 1066 |
} |
| 1067 |
#endif |
| 1068 |
|
| 1069 |
bool |
| 1070 |
is_valid_ip_address (const char *name) |
| 1071 |
{ |
| 1072 |
const char *endp; |
| 1073 |
|
| 1074 |
endp = name + strlen(name); |
| 1075 |
if (is_valid_ipv4_address (name, endp)) |
| 1076 |
return true; |
| 1077 |
#ifdef ENABLE_IPV6 |
| 1078 |
if (is_valid_ipv6_address (name, endp)) |
| 1079 |
return true; |
| 1080 |
#endif |
| 1081 |
return false; |
| 1082 |
} |