/src/httrack/src/htsdns.c
Line | Count | Source |
1 | | /* ------------------------------------------------------------ */ |
2 | | /* |
3 | | HTTrack Website Copier, Offline Browser for Windows and Unix |
4 | | Copyright (C) 1998 Xavier Roche and other contributors |
5 | | |
6 | | SPDX-License-Identifier: GPL-3.0-or-later |
7 | | |
8 | | This program is free software: you can redistribute it and/or modify |
9 | | it under the terms of the GNU General Public License as published by |
10 | | the Free Software Foundation, either version 3 of the License, or |
11 | | (at your option) any later version. |
12 | | |
13 | | This program is distributed in the hope that it will be useful, |
14 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | | GNU General Public License for more details. |
17 | | |
18 | | You should have received a copy of the GNU General Public License |
19 | | along with this program. If not, see <http://www.gnu.org/licenses/>. |
20 | | |
21 | | Ethical use: we kindly ask that you NOT use this software to harvest email |
22 | | addresses or to collect any other private information about people. Doing so |
23 | | would dishonor our work and waste the many hours we have spent on it. |
24 | | |
25 | | Please visit our Website: http://www.httrack.com |
26 | | */ |
27 | | |
28 | | /* ------------------------------------------------------------ */ |
29 | | /* File: htsdns.c subroutines: */ |
30 | | /* DNS cache and host name resolution, declared in htslib.h */ |
31 | | /* Author: Xavier Roche */ |
32 | | /* ------------------------------------------------------------ */ |
33 | | |
34 | | #include "htscore.h" |
35 | | #include "htsnet.h" |
36 | | #include "htsthread.h" |
37 | | #include "htscrashtest.h" |
38 | | |
39 | | #include <string.h> |
40 | | |
41 | | /* 0 = v4+v6, 1 = v4 only, 2 = v6 only (htscoremain -@i) */ |
42 | | int IPV6_resolver = 0; |
43 | | |
44 | | // Free a DNS cache record (coucal value handler). |
45 | 0 | static void hts_cache_value_free(coucal_opaque arg, coucal_value value) { |
46 | 0 | void *record = value.ptr; |
47 | |
|
48 | 0 | (void) arg; |
49 | 0 | freet(record); |
50 | 0 | } |
51 | | |
52 | | // opt's DNS cache hashtable, created on first use. Records (t_dnscache*) are |
53 | | // owned by the table and freed by hts_cache_value_free on coucal_delete. |
54 | 0 | static coucal hts_cache(httrackp *opt) { |
55 | 0 | assertf(opt != NULL); |
56 | 0 | if (opt->state.dns_cache == NULL) { |
57 | 0 | coucal cache = coucal_new(0); |
58 | |
|
59 | 0 | coucal_set_name(cache, "dns_cache"); |
60 | 0 | coucal_value_set_value_handler(cache, hts_cache_value_free, NULL); |
61 | 0 | opt->state.dns_cache = cache; |
62 | 0 | } |
63 | 0 | assertf(opt->state.dns_cache != NULL); |
64 | 0 | return opt->state.dns_cache; |
65 | 0 | } |
66 | | |
67 | | /* First lifetime of a negative DNS answer. Unlocked because only the |
68 | | self-test writes it, from the single thread that then resolves. */ |
69 | | static int hts_dns_negative_ttl_ms = HTS_DNS_NEGATIVE_TTL_MS; |
70 | | |
71 | 0 | void hts_dns_set_negative_ttl_ms(int ms) { |
72 | 0 | hts_dns_negative_ttl_ms = ms > 0 ? ms : 1; /* the doubling needs a floor */ |
73 | 0 | } |
74 | | |
75 | 0 | int hts_dns_negative_failures(httrackp *opt, const char *host) { |
76 | 0 | void *ptr; |
77 | 0 | int failures = 0; |
78 | |
|
79 | 0 | hts_mutexlock(&opt->state.lock); |
80 | 0 | if (coucal_read_pvoid(hts_cache(opt), host, &ptr)) |
81 | 0 | failures = ((const t_dnscache *) ptr)->failures; |
82 | 0 | hts_mutexrelease(&opt->state.lock); |
83 | 0 | return failures; |
84 | 0 | } |
85 | | |
86 | 0 | void hts_dns_test_move_clock(httrackp *opt, const char *host, TStamp ms) { |
87 | 0 | void *ptr; |
88 | |
|
89 | 0 | hts_mutexlock(&opt->state.lock); |
90 | 0 | if (coucal_read_pvoid(hts_cache(opt), host, &ptr)) { |
91 | 0 | t_dnscache *const record = (t_dnscache *) ptr; |
92 | |
|
93 | 0 | if (record->expiry != 0) { /* a positive record carries no stamps */ |
94 | 0 | record->expiry -= ms; |
95 | 0 | record->stored -= ms; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | hts_mutexrelease(&opt->state.lock); |
99 | 0 | } |
100 | | |
101 | | /* Double the wait per failure up to the ceiling, so dead hosts cost little. */ |
102 | 0 | int hts_dns_negative_wait_ms(int failures) { |
103 | 0 | int wait = hts_dns_negative_ttl_ms; |
104 | 0 | int i; |
105 | |
|
106 | 0 | for (i = 1; i < failures && wait < HTS_DNS_NEGATIVE_TTL_MAX_MS; i++) |
107 | 0 | wait *= 2; |
108 | 0 | return wait < HTS_DNS_NEGATIVE_TTL_MAX_MS ? wait |
109 | 0 | : HTS_DNS_NEGATIVE_TTL_MAX_MS; |
110 | 0 | } |
111 | | |
112 | | /* These texts are fixed because gai_strerror() may share one buffer between |
113 | | threads. A switch would not compile where two codes share one value. */ |
114 | | typedef struct dns_gai_text { |
115 | | int code; |
116 | | const char *text; |
117 | | const char *cached; |
118 | | } dns_gai_text; |
119 | | |
120 | | static const dns_gai_text dns_gai_texts[] = { |
121 | | #ifdef EAI_AGAIN |
122 | | {EAI_AGAIN, "temporary DNS failure", "temporary DNS failure (cached)"}, |
123 | | #endif |
124 | | #ifdef EAI_FAIL |
125 | | {EAI_FAIL, "permanent DNS failure", "permanent DNS failure (cached)"}, |
126 | | #endif |
127 | | #ifdef EAI_NODATA |
128 | | {EAI_NODATA, "no address for host name", |
129 | | "no address for host name (cached)"}, |
130 | | #endif |
131 | | #ifdef EAI_ADDRFAMILY |
132 | | {EAI_ADDRFAMILY, "no address in this family", |
133 | | "no address in this family (cached)"}, |
134 | | #endif |
135 | | #ifdef EAI_MEMORY |
136 | | {EAI_MEMORY, "resolver out of memory", "resolver out of memory (cached)"}, |
137 | | #endif |
138 | | #ifdef EAI_SYSTEM |
139 | | {EAI_SYSTEM, "resolver system error", "resolver system error (cached)"}, |
140 | | #endif |
141 | | {0, "host name resolution failed", "host name resolution failed (cached)"}, |
142 | | }; |
143 | | |
144 | 0 | const char *hts_dns_strerror(const hts_dns_result *res) { |
145 | 0 | size_t i; |
146 | |
|
147 | 0 | switch (res->status) { |
148 | 0 | case HTS_DNS_OK: |
149 | 0 | return "no error"; |
150 | 0 | case HTS_DNS_NONAME: |
151 | 0 | return res->cached ? "host name does not exist (cached)" |
152 | 0 | : "host name does not exist"; |
153 | 0 | case HTS_DNS_TEMPFAIL: |
154 | | /* the last row, code 0, also answers for a code with no text */ |
155 | 0 | for (i = 0; i + 1 < sizeof(dns_gai_texts) / sizeof(dns_gai_texts[0]); i++) |
156 | 0 | if (dns_gai_texts[i].code == res->gai_code) |
157 | 0 | break; |
158 | 0 | return res->cached ? dns_gai_texts[i].cached : dns_gai_texts[i].text; |
159 | 0 | case HTS_DNS_TIMEOUT: |
160 | 0 | return "host name resolution timed out"; |
161 | 0 | case HTS_DNS_CANCELLED: |
162 | 0 | return "host name resolution cancelled"; |
163 | 0 | } |
164 | 0 | return "unknown error"; |
165 | 0 | } |
166 | | |
167 | | /* Empty res, giving it status st and getaddrinfo() code gai. */ |
168 | 0 | static void dns_result_fail(hts_dns_result *res, hts_dns_status st, int gai) { |
169 | 0 | res->count = 0; |
170 | 0 | res->status = st; |
171 | 0 | res->gai_code = gai; |
172 | 0 | res->cached = HTS_FALSE; |
173 | 0 | } |
174 | | |
175 | | // MUST BE LOCKED (coucal is not internally serialized vs FTP/web threads) |
176 | | // Look up iadr in the DNS cache into res. Returns HTS_FALSE when it is not |
177 | | // cached, or is a negative answer past its expiry. |
178 | | static hts_boolean hts_ghbn_all(coucal cache, const char *const iadr, |
179 | 0 | hts_dns_result *const res) { |
180 | 0 | void *ptr; |
181 | |
|
182 | 0 | assertf(res != NULL); |
183 | 0 | assertf(iadr != NULL); |
184 | 0 | if (*iadr == '\0') { |
185 | 0 | return HTS_FALSE; |
186 | 0 | } |
187 | 0 | if (coucal_read_pvoid(cache, iadr, &ptr)) { // found |
188 | 0 | const t_dnscache *const record = (const t_dnscache *) ptr; |
189 | 0 | int i; |
190 | |
|
191 | 0 | assertf(record->host_count <= HTS_MAXADDRNUM); |
192 | | /* Also expire a negative record when the wall clock went back. */ |
193 | 0 | if (record->expiry != 0) { |
194 | 0 | const TStamp now = mtime_local(); |
195 | |
|
196 | 0 | if (now >= record->expiry || now < record->stored) { |
197 | 0 | return HTS_FALSE; |
198 | 0 | } |
199 | 0 | } |
200 | 0 | for (i = 0; i < record->host_count; i++) { |
201 | 0 | assertf(record->host_length[i] <= sizeof(record->host_addr[i])); |
202 | 0 | SOCaddr_copyaddr2(res->addr[i], record->host_addr[i], |
203 | 0 | record->host_length[i]); |
204 | 0 | } |
205 | 0 | res->count = record->host_count; |
206 | | /* a negative record without an expiry is the name's own answer */ |
207 | 0 | res->status = record->host_count > 0 ? HTS_DNS_OK |
208 | 0 | : record->expiry == 0 ? HTS_DNS_NONAME |
209 | 0 | : HTS_DNS_TEMPFAIL; |
210 | 0 | res->gai_code = record->gai_code; |
211 | 0 | res->cached = HTS_TRUE; |
212 | 0 | return HTS_TRUE; |
213 | 0 | } |
214 | 0 | return HTS_FALSE; |
215 | 0 | } |
216 | | |
217 | | #if HTS_INET6 != 0 |
218 | | /* Active resolver backend; defaults to the libc resolver. The self-test |
219 | | reroutes it to script DNS answers in-process (see |
220 | | hts_dns_set_resolver_backend). */ |
221 | | static const hts_resolver_backend hts_resolver_libc = {getaddrinfo, |
222 | | freeaddrinfo}; |
223 | | static const hts_resolver_backend *hts_resolver = &hts_resolver_libc; |
224 | | |
225 | 0 | void hts_dns_set_resolver_backend(const hts_resolver_backend *backend) { |
226 | 0 | hts_resolver = (backend != NULL) ? backend : &hts_resolver_libc; |
227 | 0 | } |
228 | | |
229 | | /* Test hook: HTTRACK_DEBUG_RESOLVE="host:ip[,ip...]" pins host like curl |
230 | | --resolve. The backend owns its chains, so both kinds free the same way. */ |
231 | | |
232 | | static void HTS_RESOLVER_CALL override_freeaddrinfo(struct addrinfo *res); |
233 | | |
234 | | /* Deep-copy a libc addrinfo chain into our own allocations, or NULL if one of |
235 | | them failed: the caller cannot hand a half-copied chain back as success. */ |
236 | 0 | static struct addrinfo *resolver_dup_chain(const struct addrinfo *src) { |
237 | 0 | struct addrinfo *head = NULL, *tail = NULL; |
238 | |
|
239 | 0 | for (; src != NULL; src = src->ai_next) { |
240 | 0 | struct addrinfo *ai = calloct(1, sizeof(*ai)); /* freet() needs an lvalue */ |
241 | |
|
242 | 0 | if (ai == NULL) { |
243 | 0 | override_freeaddrinfo(head); |
244 | 0 | return NULL; |
245 | 0 | } |
246 | 0 | ai->ai_family = src->ai_family; |
247 | 0 | ai->ai_socktype = src->ai_socktype; |
248 | 0 | ai->ai_protocol = src->ai_protocol; |
249 | 0 | ai->ai_addrlen = src->ai_addrlen; |
250 | 0 | ai->ai_addr = malloct(src->ai_addrlen); |
251 | 0 | if (ai->ai_addr == NULL) { |
252 | 0 | freet(ai); |
253 | 0 | override_freeaddrinfo(head); |
254 | 0 | return NULL; |
255 | 0 | } |
256 | 0 | memcpy(ai->ai_addr, src->ai_addr, src->ai_addrlen); |
257 | 0 | if (head == NULL) |
258 | 0 | head = ai; |
259 | 0 | else |
260 | 0 | tail->ai_next = ai; |
261 | 0 | tail = ai; |
262 | 0 | } |
263 | 0 | return head; |
264 | 0 | } |
265 | | |
266 | | /* Build one addrinfo node from an IPv4/IPv6 literal, or NULL if it does not |
267 | | parse or is filtered out by want_family (AF_INET/AF_INET6/PF_UNSPEC). */ |
268 | 0 | static struct addrinfo *resolver_make_ai(const char *ip, int want_family) { |
269 | 0 | struct addrinfo *ai; |
270 | |
|
271 | 0 | if (strchr(ip, ':') != NULL) { // IPv6 literal |
272 | 0 | struct sockaddr_in6 sa6; |
273 | 0 | void *copy6; |
274 | |
|
275 | 0 | if (want_family != PF_UNSPEC && want_family != AF_INET6) |
276 | 0 | return NULL; |
277 | 0 | memset(&sa6, 0, sizeof(sa6)); |
278 | 0 | if (inet_pton(AF_INET6, ip, &sa6.sin6_addr) != 1) |
279 | 0 | return NULL; |
280 | 0 | sa6.sin6_family = AF_INET6; |
281 | 0 | ai = calloct(1, sizeof(*ai)); |
282 | 0 | if (ai == NULL) |
283 | 0 | return NULL; |
284 | 0 | ai->ai_family = AF_INET6; |
285 | 0 | copy6 = malloct(sizeof(sa6)); |
286 | 0 | if (copy6 == NULL) { |
287 | 0 | freet(ai); |
288 | 0 | return NULL; |
289 | 0 | } |
290 | 0 | memcpy(copy6, &sa6, sizeof(sa6)); |
291 | 0 | ai->ai_addrlen = sizeof(sa6); |
292 | 0 | ai->ai_addr = copy6; |
293 | 0 | } else { // IPv4 literal |
294 | 0 | struct sockaddr_in sa; |
295 | 0 | void *copy4; |
296 | |
|
297 | 0 | if (want_family != PF_UNSPEC && want_family != AF_INET) |
298 | 0 | return NULL; |
299 | 0 | memset(&sa, 0, sizeof(sa)); |
300 | 0 | if (inet_pton(AF_INET, ip, &sa.sin_addr) != 1) |
301 | 0 | return NULL; |
302 | 0 | sa.sin_family = AF_INET; |
303 | 0 | ai = calloct(1, sizeof(*ai)); |
304 | 0 | if (ai == NULL) |
305 | 0 | return NULL; |
306 | 0 | ai->ai_family = AF_INET; |
307 | 0 | copy4 = malloct(sizeof(sa)); |
308 | 0 | if (copy4 == NULL) { |
309 | 0 | freet(ai); |
310 | 0 | return NULL; |
311 | 0 | } |
312 | 0 | memcpy(copy4, &sa, sizeof(sa)); |
313 | 0 | ai->ai_addrlen = sizeof(sa); |
314 | 0 | ai->ai_addr = copy4; |
315 | 0 | } |
316 | 0 | return ai; |
317 | 0 | } |
318 | | |
319 | 0 | static void HTS_RESOLVER_CALL override_freeaddrinfo(struct addrinfo *res) { |
320 | 0 | while (res != NULL) { |
321 | 0 | struct addrinfo *const next = res->ai_next; |
322 | |
|
323 | 0 | freet(res->ai_addr); |
324 | 0 | freet(res); |
325 | 0 | res = next; |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | static int HTS_RESOLVER_CALL override_getaddrinfo(const char *node, |
330 | | const char *service, |
331 | | const struct addrinfo *hints, |
332 | 0 | struct addrinfo **res) { |
333 | 0 | const char *const spec = getenv("HTTRACK_DEBUG_RESOLVE"); |
334 | 0 | const int want = (hints != NULL) ? hints->ai_family : PF_UNSPEC; |
335 | 0 | const char *colon; |
336 | |
|
337 | 0 | *res = NULL; |
338 | 0 | if (spec != NULL && node != NULL && (colon = strchr(spec, ':')) != NULL && |
339 | 0 | (size_t) (colon - spec) == strlen(node) && |
340 | 0 | strncmp(spec, node, colon - spec) == 0) { |
341 | 0 | struct addrinfo *head = NULL, *tail = NULL; |
342 | 0 | hts_span rest = hts_span_of(colon + 1); |
343 | 0 | hts_span item; |
344 | | |
345 | | /* reentrant: DNS worker threads run this */ |
346 | 0 | while (hts_span_next_in(&rest, ',', &item)) { |
347 | 0 | char ip[64]; |
348 | 0 | struct addrinfo *ai; |
349 | |
|
350 | 0 | if (!hts_buf_put_span(HTS_BUF(ip), item)) |
351 | 0 | continue; |
352 | 0 | ai = resolver_make_ai(ip, want); |
353 | |
|
354 | 0 | if (ai != NULL) { |
355 | 0 | if (head == NULL) |
356 | 0 | head = ai; |
357 | 0 | else |
358 | 0 | tail->ai_next = ai; |
359 | 0 | tail = ai; |
360 | 0 | } |
361 | 0 | } |
362 | 0 | if (head == NULL) |
363 | 0 | return EAI_NONAME; |
364 | 0 | *res = head; |
365 | 0 | return 0; |
366 | 0 | } |
367 | | |
368 | | /* not overridden: delegate to libc, copying into our owned format */ |
369 | 0 | { |
370 | 0 | struct addrinfo *sys = NULL; |
371 | 0 | int gerr = getaddrinfo(node, service, hints, &sys); |
372 | |
|
373 | 0 | if (gerr != 0) |
374 | 0 | return gerr; |
375 | 0 | *res = resolver_dup_chain(sys); |
376 | 0 | freeaddrinfo(sys); |
377 | 0 | return *res != NULL ? 0 : EAI_MEMORY; |
378 | 0 | } |
379 | 0 | } |
380 | | |
381 | | static const hts_resolver_backend hts_resolver_override = { |
382 | | override_getaddrinfo, override_freeaddrinfo}; |
383 | | |
384 | | /* Install the env override once, unless a backend was already set (self-test). |
385 | | */ |
386 | 0 | static void hts_resolver_check_env(void) { |
387 | 0 | static int checked = 0; |
388 | |
|
389 | 0 | if (!checked) { |
390 | 0 | checked = 1; |
391 | 0 | if (hts_resolver == &hts_resolver_libc && |
392 | 0 | getenv("HTTRACK_DEBUG_RESOLVE") != NULL) { |
393 | 0 | hts_resolver = &hts_resolver_override; |
394 | 0 | } |
395 | 0 | } |
396 | 0 | } |
397 | | #endif |
398 | | |
399 | | #if HTS_INET6 == 0 |
400 | | /* gethostbyname() answers into a shared static hostent, so the call and the |
401 | | copy out of it are one critical section, or two workers resolving at once |
402 | | read each other's addresses. Not getaddrinfo(), because configure also lands |
403 | | here when libc has none. */ |
404 | | static htsmutex dns_hostent_lock = HTSMUTEX_INIT; |
405 | | #endif |
406 | | |
407 | | // Resolve hostname into res (resolver/RFC 6724 order, at most |
408 | | // HTS_MAXADDRNUM), no cache. Returns res->status. |
409 | | static hts_dns_status hts_dns_resolve_nocache_list_(const char *const hostname, |
410 | 0 | hts_dns_result *const res) { |
411 | 0 | int count = 0; |
412 | | #if HTS_INET6 == 0 |
413 | | /* IPv4 resolver */ |
414 | | const struct hostent *hp; |
415 | | |
416 | | dns_result_fail(res, HTS_DNS_TEMPFAIL, 0); |
417 | | hts_mutexlock(&dns_hostent_lock); |
418 | | hp = gethostbyname(hostname); |
419 | | if (hp == NULL && h_errno == HOST_NOT_FOUND) |
420 | | res->status = HTS_DNS_NONAME; |
421 | | if (hp != NULL) { |
422 | | char *const *h; |
423 | | |
424 | | for (h = hp->h_addr_list; count < HTS_MAXADDRNUM && h != NULL && *h != NULL; |
425 | | h++) { |
426 | | SOCaddr_clear(res->addr[count]); |
427 | | SOCaddr_copyaddr2(res->addr[count], *h, hp->h_length); |
428 | | if (SOCaddr_is_valid(res->addr[count])) |
429 | | count++; |
430 | | } |
431 | | } |
432 | | hts_mutexrelease(&dns_hostent_lock); |
433 | | #else |
434 | | /* IPv6 resolver */ |
435 | 0 | struct addrinfo *res_ai = NULL, *cur; |
436 | 0 | struct addrinfo hints; |
437 | 0 | int gerr; |
438 | |
|
439 | 0 | dns_result_fail(res, HTS_DNS_TEMPFAIL, 0); |
440 | 0 | hts_resolver_check_env(); |
441 | 0 | memset(&hints, 0, sizeof(hints)); |
442 | 0 | if (IPV6_resolver == 1) // V4 only (for bogus V6 entries) |
443 | 0 | hints.ai_family = PF_INET; |
444 | 0 | else if (IPV6_resolver == 2) // V6 only (for testing V6 only) |
445 | 0 | hints.ai_family = PF_INET6; |
446 | 0 | else // V4 + V6 |
447 | 0 | hints.ai_family = PF_UNSPEC; |
448 | 0 | hints.ai_socktype = SOCK_STREAM; |
449 | 0 | hints.ai_protocol = IPPROTO_TCP; |
450 | 0 | if ((gerr = hts_resolver->getaddrinfo(hostname, NULL, &hints, &res_ai)) == |
451 | 0 | 0) { |
452 | 0 | for (cur = res_ai; cur != NULL && count < HTS_MAXADDRNUM; |
453 | 0 | cur = cur->ai_next) { |
454 | 0 | if (cur->ai_addr != NULL && cur->ai_addrlen != 0) { |
455 | 0 | SOCaddr_clear(res->addr[count]); |
456 | 0 | SOCaddr_copyaddr2(res->addr[count], cur->ai_addr, cur->ai_addrlen); |
457 | 0 | if (SOCaddr_is_valid(res->addr[count])) |
458 | 0 | count++; |
459 | 0 | } |
460 | 0 | } |
461 | 0 | } else { |
462 | 0 | res->gai_code = gerr; |
463 | | /* EAI_NONAME answers about the name; every other code says the resolver |
464 | | could not answer at all. */ |
465 | 0 | if (gerr == EAI_NONAME) |
466 | 0 | res->status = HTS_DNS_NONAME; |
467 | 0 | } |
468 | 0 | if (res_ai) { |
469 | 0 | hts_resolver->freeaddrinfo(res_ai); |
470 | 0 | } |
471 | 0 | #endif |
472 | |
|
473 | 0 | if (count > 0) { |
474 | 0 | res->count = count; |
475 | 0 | res->status = HTS_DNS_OK; |
476 | 0 | res->gai_code = 0; |
477 | 0 | } |
478 | 0 | return res->status; |
479 | 0 | } |
480 | | |
481 | | // Strip [] around a literal IPv6 ([3ffe:b80:1234:1::1]) the resolver won't |
482 | | // take, then resolve into res. |
483 | | static hts_dns_status hts_dns_resolve_nocache_list(const char *const hostname, |
484 | 0 | hts_dns_result *const res) { |
485 | 0 | if (!strnotempty(hostname)) { |
486 | 0 | dns_result_fail(res, HTS_DNS_NONAME, 0); |
487 | 0 | return res->status; |
488 | 0 | } |
489 | 0 | if ((hostname[0] == '[') && (hts_lastchar(hostname) == ']')) { |
490 | 0 | char *copy = hts_span_dup(hts_span_at(hostname + 1, strlen(hostname) - 2)); |
491 | 0 | hts_dns_status st; |
492 | |
|
493 | 0 | assertf(copy != NULL); |
494 | 0 | st = hts_dns_resolve_nocache_list_(copy, res); |
495 | 0 | freet(copy); |
496 | 0 | return st; |
497 | 0 | } else { |
498 | 0 | return hts_dns_resolve_nocache_list_(hostname, res); |
499 | 0 | } |
500 | 0 | } |
501 | | |
502 | | SOCaddr *hts_dns_resolve_nocache(const char *const hostname, |
503 | 0 | SOCaddr *const addr) { |
504 | 0 | hts_dns_result res; |
505 | |
|
506 | 0 | (void) hts_dns_resolve_nocache_list(hostname, &res); |
507 | 0 | SOCaddr_clear(*addr); |
508 | 0 | if (res.count > 0) |
509 | 0 | SOCaddr_copy_SOCaddr(*addr, res.addr[0]); |
510 | 0 | return SOCaddr_is_valid(*addr) ? addr : NULL; |
511 | 0 | } |
512 | | |
513 | | /* A timed-out resolve keeps running, so this job is refcounted and the last of |
514 | | caller and worker frees it. */ |
515 | | typedef struct dns_resolve_job { |
516 | | htsmutex lock; |
517 | | int refcount; |
518 | | hts_boolean done; |
519 | | char *hostname; |
520 | | hts_dns_result res; /* res.count is -1 when the worker gave no answer */ |
521 | | hts_boolean stored; /* hts_dns_poll() put the answer in the cache */ |
522 | | } dns_resolve_job; |
523 | | |
524 | 0 | static void dns_job_release(dns_resolve_job *job) { |
525 | 0 | hts_boolean last; |
526 | |
|
527 | 0 | hts_mutexlock(&job->lock); |
528 | 0 | last = (--job->refcount == 0) ? HTS_TRUE : HTS_FALSE; |
529 | 0 | hts_mutexrelease(&job->lock); |
530 | 0 | if (last) { |
531 | 0 | hts_mutexfree(&job->lock); |
532 | 0 | freet(job->hostname); |
533 | 0 | freet(job); |
534 | 0 | } |
535 | 0 | } |
536 | | |
537 | | /* Outlives a timed-out resolve, so it writes only the job: never opt nor the |
538 | | DNS cache, which the crawl keeps using without it. */ |
539 | 0 | static void dns_resolve_thread(void *arg) { |
540 | 0 | dns_resolve_job *const job = (dns_resolve_job *) arg; |
541 | 0 | hts_dns_result res; |
542 | |
|
543 | 0 | (void) hts_dns_resolve_nocache_list(job->hostname, &res); |
544 | 0 | hts_crash_test_worker(HTS_CRASH_WORKER_DNS); |
545 | 0 | hts_mutexlock(&job->lock); |
546 | 0 | job->res = res; |
547 | 0 | job->done = HTS_TRUE; /* published last: gates the caller's read of addr[] */ |
548 | 0 | hts_mutexrelease(&job->lock); |
549 | 0 | } |
550 | | |
551 | | /* Releases the job whatever the body did (see hts_newthread_tail). A -1 count |
552 | | is what the caller reads as "no answer", so it caches nothing. */ |
553 | 0 | static void dns_resolve_done(void *arg) { |
554 | 0 | dns_resolve_job *const job = (dns_resolve_job *) arg; |
555 | |
|
556 | 0 | hts_mutexlock(&job->lock); |
557 | 0 | if (!job->done) { |
558 | 0 | dns_result_fail(&job->res, HTS_DNS_TEMPFAIL, 0); |
559 | 0 | job->res.count = -1; |
560 | 0 | job->done = HTS_TRUE; |
561 | 0 | } |
562 | 0 | hts_mutexrelease(&job->lock); |
563 | 0 | dns_job_release(job); |
564 | 0 | } |
565 | | |
566 | | /* Start resolving hostname on a worker thread, holding one reference for the |
567 | | caller. NULL when no thread could start. */ |
568 | 0 | static dns_resolve_job *dns_job_start(const char *hostname) { |
569 | 0 | dns_resolve_job *const job = calloct(1, sizeof(*job)); |
570 | |
|
571 | 0 | assertf(job != NULL); |
572 | 0 | hts_mutexinit(&job->lock); |
573 | 0 | job->hostname = strdupt(hostname); |
574 | 0 | job->refcount = 2; /* this caller + the worker */ |
575 | 0 | if (hts_newthread_tail(dns_resolve_thread, job, dns_resolve_done) != 0) { |
576 | 0 | job->refcount = 1; |
577 | 0 | dns_job_release(job); |
578 | 0 | return NULL; |
579 | 0 | } |
580 | 0 | return job; |
581 | 0 | } |
582 | | |
583 | | /* Resolve hostname on a worker thread, giving up after timeout seconds or as |
584 | | soon as *cancel (optional) is raised. Returns HTS_FALSE when no answer came, |
585 | | with res saying why: a timeout, a cancel or a worker cut short, none of which |
586 | | may be cached. A zero count is a real answer and gets negative-cached. */ |
587 | | static hts_boolean hts_dns_resolve_nocache_list_bounded( |
588 | | const char *hostname, hts_dns_result *const res, const int timeout, |
589 | 0 | const volatile hts_boolean *cancel) { |
590 | 0 | dns_resolve_job *job; |
591 | 0 | hts_mono deadline; |
592 | 0 | hts_boolean answered = HTS_FALSE; |
593 | 0 | int poll_ms = 1; |
594 | | |
595 | | /* no bound asked for (--timeout 0), and nobody to cut it short either */ |
596 | 0 | if (timeout <= 0 && cancel == NULL) { |
597 | 0 | (void) hts_dns_resolve_nocache_list(hostname, res); |
598 | 0 | return HTS_TRUE; |
599 | 0 | } |
600 | | |
601 | 0 | job = dns_job_start(hostname); |
602 | 0 | if (job == NULL) { /* no worker: fall back to resolving inline */ |
603 | 0 | (void) hts_dns_resolve_nocache_list(hostname, res); |
604 | 0 | return HTS_TRUE; |
605 | 0 | } |
606 | | |
607 | | /* timeout <= 0 got here only for a cancellable resolve: no deadline then */ |
608 | 0 | deadline = |
609 | 0 | timeout > 0 ? hts_mono_add_s(hts_mono_now(), timeout) : hts_mono_never(); |
610 | 0 | for (;;) { |
611 | 0 | hts_boolean done; |
612 | |
|
613 | 0 | hts_mutexlock(&job->lock); |
614 | 0 | done = job->done; |
615 | 0 | if (done) { |
616 | 0 | *res = job->res; |
617 | 0 | answered = res->count >= 0 ? HTS_TRUE : HTS_FALSE; |
618 | 0 | if (!answered) |
619 | 0 | res->count = 0; |
620 | 0 | } |
621 | 0 | hts_mutexrelease(&job->lock); |
622 | 0 | if (done) |
623 | 0 | break; |
624 | 0 | if (cancel != NULL && hts_load_acquire_int(cancel)) { |
625 | 0 | dns_result_fail(res, HTS_DNS_CANCELLED, 0); |
626 | 0 | break; |
627 | 0 | } |
628 | 0 | if (!hts_mono_before(hts_mono_now(), deadline)) { |
629 | 0 | dns_result_fail(res, HTS_DNS_TIMEOUT, 0); |
630 | 0 | break; |
631 | 0 | } |
632 | 0 | Sleep(poll_ms); |
633 | 0 | if (poll_ms < 50) /* short first polls keep a fast resolve fast */ |
634 | 0 | poll_ms *= 2; |
635 | 0 | } |
636 | 0 | dns_job_release(job); |
637 | 0 | return answered; |
638 | 0 | } |
639 | | |
640 | | /* Cache key and resolver input: identification and any ":port" stripped. */ |
641 | 0 | static void dns_cache_key(hts_buf host, const char *iadr) { |
642 | 0 | char *a; |
643 | |
|
644 | 0 | hts_buf_cpy(host, jump_identification_const(iadr)); |
645 | 0 | if ((a = jump_toport(host.p))) |
646 | 0 | *a = '\0'; |
647 | 0 | } |
648 | | |
649 | 0 | static hts_boolean dns_same_addr(const void *a, size_t size, const SOCaddr *b) { |
650 | 0 | return size != 0 && size == SOCaddr_size(*b) && |
651 | 0 | memcmp(a, &SOCaddr_sockaddr(*b), size) == 0; |
652 | 0 | } |
653 | | |
654 | 0 | int hts_dns_addr_index(const SOCaddr *list, int count, const SOCaddr *addr) { |
655 | 0 | int i; |
656 | |
|
657 | 0 | for (i = 0; i < count; i++) { |
658 | 0 | if (dns_same_addr(&SOCaddr_sockaddr(list[i]), SOCaddr_size(list[i]), addr)) |
659 | 0 | return i; |
660 | 0 | } |
661 | 0 | return -1; |
662 | 0 | } |
663 | | |
664 | 0 | int hts_dns_preferred(httrackp *opt, const char *iadr) { |
665 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
666 | 0 | void *ptr; |
667 | 0 | int preferred = 0; |
668 | |
|
669 | 0 | assertf(opt != NULL); |
670 | 0 | if (!strnotempty(iadr)) |
671 | 0 | return 0; |
672 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
673 | 0 | hts_mutexlock(&opt->state.lock); |
674 | 0 | if (coucal_read_pvoid(hts_cache(opt), host, &ptr)) |
675 | 0 | preferred = ((const t_dnscache *) ptr)->preferred; |
676 | 0 | hts_mutexrelease(&opt->state.lock); |
677 | 0 | return preferred; |
678 | 0 | } |
679 | | |
680 | | void hts_dns_set_preferred(httrackp *opt, const char *iadr, |
681 | 0 | const SOCaddr *addr) { |
682 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
683 | 0 | void *ptr; |
684 | |
|
685 | 0 | assertf(opt != NULL); |
686 | 0 | if (!strnotempty(iadr)) |
687 | 0 | return; |
688 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
689 | 0 | hts_mutexlock(&opt->state.lock); |
690 | 0 | if (coucal_read_pvoid(hts_cache(opt), host, &ptr)) { |
691 | 0 | t_dnscache *const record = (t_dnscache *) ptr; |
692 | 0 | int i; |
693 | |
|
694 | 0 | for (i = 0; i < record->host_count; i++) { |
695 | 0 | if (dns_same_addr(record->host_addr[i], record->host_length[i], addr)) { |
696 | 0 | record->preferred = i; |
697 | 0 | break; |
698 | 0 | } |
699 | 0 | } |
700 | 0 | } |
701 | 0 | hts_mutexrelease(&opt->state.lock); |
702 | 0 | } |
703 | | |
704 | | /* Store a resolve's answer for host. Must hold no lock. */ |
705 | | static void dns_cache_store(httrackp *opt, const char *host, |
706 | 0 | const hts_dns_result *res) { |
707 | 0 | const int count = res->count; |
708 | 0 | coucal cache; |
709 | 0 | int i; |
710 | |
|
711 | 0 | hts_mutexlock(&opt->state.lock); |
712 | 0 | cache = hts_cache(opt); |
713 | 0 | { /* store the full list (coucal owns the record and dups the host key; a |
714 | | concurrent resolve of the same host replaces, and frees, this one) */ |
715 | 0 | t_dnscache *const record = malloct(sizeof(t_dnscache)); |
716 | 0 | void *previous; |
717 | 0 | const int failures = |
718 | 0 | (count == 0 && coucal_read_pvoid(cache, host, &previous)) |
719 | 0 | ? ((const t_dnscache *) previous)->failures + 1 |
720 | 0 | : 1; |
721 | |
|
722 | 0 | if (record != NULL) { |
723 | 0 | memset(record, 0, sizeof(*record)); |
724 | 0 | record->host_count = count; |
725 | 0 | record->failures = count == 0 ? failures : 0; |
726 | | /* The resolver saying the name does not exist stands for the crawl; |
727 | | it being unable to answer is only as good as the network it asked. */ |
728 | 0 | record->gai_code = res->gai_code; |
729 | 0 | if (count == 0 && res->status != HTS_DNS_NONAME) { |
730 | 0 | record->stored = mtime_local(); |
731 | 0 | record->expiry = record->stored + hts_dns_negative_wait_ms(failures); |
732 | 0 | } |
733 | 0 | for (i = 0; i < count; i++) { |
734 | 0 | record->host_length[i] = SOCaddr_size(res->addr[i]); |
735 | 0 | assertf(record->host_length[i] <= sizeof(record->host_addr[i])); |
736 | 0 | memcpy(record->host_addr[i], &SOCaddr_sockaddr(res->addr[i]), |
737 | 0 | record->host_length[i]); |
738 | 0 | } |
739 | 0 | coucal_add_pvoid(cache, host, record); |
740 | 0 | } |
741 | 0 | } |
742 | 0 | hts_mutexrelease(&opt->state.lock); |
743 | 0 | } |
744 | | |
745 | | hts_dns_status hts_dns_resolve(httrackp *opt, const char *iadr, int timeout, |
746 | | const volatile hts_boolean *cancel, |
747 | 0 | hts_dns_result *res) { |
748 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
749 | 0 | hts_boolean hit; |
750 | |
|
751 | 0 | assertf(opt != NULL); |
752 | 0 | assertf(res != NULL); |
753 | 0 | if (!strnotempty(iadr)) { |
754 | 0 | dns_result_fail(res, HTS_DNS_NONAME, 0); |
755 | 0 | return res->status; |
756 | 0 | } |
757 | | |
758 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
759 | |
|
760 | 0 | hts_mutexlock(&opt->state.lock); |
761 | 0 | #if HTS_INET6 != 0 |
762 | 0 | hts_resolver_check_env(); /* settle the backend before a worker reads it */ |
763 | 0 | #endif |
764 | 0 | hit = hts_ghbn_all(hts_cache(opt), host, res); |
765 | 0 | hts_mutexrelease(&opt->state.lock); |
766 | 0 | if (hit) |
767 | 0 | return res->status; |
768 | | |
769 | | /* Resolve with no lock held: getaddrinfo can block for a long time, and |
770 | | state.lock also gates the stop request (#606). */ |
771 | 0 | if (hts_dns_resolve_nocache_list_bounded(host, res, timeout, cancel)) |
772 | 0 | dns_cache_store(opt, host, res); |
773 | 0 | return res->status; |
774 | 0 | } |
775 | | |
776 | | int hts_dns_resolve_all(httrackp *opt, const char *iadr, SOCaddr *out, |
777 | 0 | int max) { |
778 | 0 | hts_dns_result res; |
779 | 0 | int i; |
780 | |
|
781 | 0 | assertf(opt != NULL); |
782 | 0 | assertf(out != NULL); |
783 | 0 | if (!strnotempty(iadr) || max <= 0) |
784 | 0 | return 0; |
785 | | /* the mirror's stop flag cancels: a resolve behind a black hole otherwise |
786 | | holds a stopped crawl for the whole --timeout (#1073) */ |
787 | 0 | (void) hts_dns_resolve(opt, iadr, opt->timeout, &opt->state.stop, &res); |
788 | 0 | for (i = 0; i < res.count && i < max; i++) |
789 | 0 | SOCaddr_copy_SOCaddr(out[i], res.addr[i]); |
790 | 0 | return res.count; |
791 | 0 | } |
792 | | |
793 | 0 | hts_boolean hts_dns_cached(httrackp *opt, const char *iadr) { |
794 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
795 | 0 | hts_dns_result scratch; |
796 | 0 | hts_boolean hit; |
797 | |
|
798 | 0 | assertf(opt != NULL); |
799 | 0 | if (!strnotempty(iadr)) |
800 | 0 | return HTS_TRUE; /* nothing to resolve */ |
801 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
802 | 0 | hts_mutexlock(&opt->state.lock); |
803 | 0 | hit = hts_ghbn_all(hts_cache(opt), host, &scratch); |
804 | 0 | hts_mutexrelease(&opt->state.lock); |
805 | 0 | return hit; |
806 | 0 | } |
807 | | |
808 | 0 | hts_dns_job *hts_dns_start(httrackp *opt, const char *iadr) { |
809 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
810 | |
|
811 | 0 | assertf(opt != NULL); |
812 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
813 | 0 | #if HTS_INET6 != 0 |
814 | 0 | hts_mutexlock(&opt->state.lock); |
815 | 0 | hts_resolver_check_env(); /* settle the backend before a worker reads it */ |
816 | 0 | hts_mutexrelease(&opt->state.lock); |
817 | 0 | #endif |
818 | 0 | return dns_job_start(host); |
819 | 0 | } |
820 | | |
821 | 0 | hts_boolean hts_dns_job_is(const hts_dns_job *job, const char *iadr) { |
822 | 0 | char BIGSTK host[HTS_URLMAXSIZE * 2]; |
823 | |
|
824 | 0 | dns_cache_key(HTS_BUF(host), iadr); |
825 | 0 | return strcmp(job->hostname, host) == 0 ? HTS_TRUE : HTS_FALSE; |
826 | 0 | } |
827 | | |
828 | 0 | hts_dns_job *hts_dns_job_ref(hts_dns_job *job) { |
829 | 0 | hts_mutexlock(&job->lock); |
830 | 0 | job->refcount++; |
831 | 0 | hts_mutexrelease(&job->lock); |
832 | 0 | return job; |
833 | 0 | } |
834 | | |
835 | 0 | void hts_dns_job_release(hts_dns_job *job) { dns_job_release(job); } |
836 | | |
837 | 0 | int hts_dns_poll(httrackp *opt, hts_dns_job *job, hts_dns_result *res) { |
838 | 0 | hts_dns_result answer; |
839 | 0 | hts_boolean store; |
840 | |
|
841 | 0 | hts_mutexlock(&job->lock); |
842 | 0 | if (!job->done) { |
843 | 0 | hts_mutexrelease(&job->lock); |
844 | 0 | return HTS_DNS_PENDING; |
845 | 0 | } |
846 | 0 | answer = job->res; |
847 | | /* the first poller of a shared job stores it, so failures count once */ |
848 | 0 | store = answer.count >= 0 && !job->stored; |
849 | 0 | job->stored = HTS_TRUE; |
850 | 0 | hts_mutexrelease(&job->lock); |
851 | 0 | if (store) |
852 | 0 | dns_cache_store(opt, job->hostname, &answer); |
853 | 0 | if (res != NULL) { |
854 | 0 | *res = answer; |
855 | 0 | if (res->count < 0) /* the worker was cut short */ |
856 | 0 | res->count = 0; |
857 | 0 | } |
858 | 0 | return answer.count; |
859 | 0 | } |