/src/PROJ/curl/lib/hostip.c
Line | Count | Source (jump to first uncovered line) |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #ifdef HAVE_NETINET_IN_H |
28 | | #include <netinet/in.h> |
29 | | #endif |
30 | | #ifdef HAVE_NETINET_IN6_H |
31 | | #include <netinet/in6.h> |
32 | | #endif |
33 | | #ifdef HAVE_NETDB_H |
34 | | #include <netdb.h> |
35 | | #endif |
36 | | #ifdef HAVE_ARPA_INET_H |
37 | | #include <arpa/inet.h> |
38 | | #endif |
39 | | #ifdef __VMS |
40 | | #include <in.h> |
41 | | #include <inet.h> |
42 | | #endif |
43 | | |
44 | | #include <setjmp.h> |
45 | | #ifndef UNDER_CE |
46 | | #include <signal.h> |
47 | | #endif |
48 | | |
49 | | #include "urldata.h" |
50 | | #include "sendf.h" |
51 | | #include "connect.h" |
52 | | #include "hostip.h" |
53 | | #include "hash.h" |
54 | | #include "rand.h" |
55 | | #include "share.h" |
56 | | #include "url.h" |
57 | | #include "curlx/inet_ntop.h" |
58 | | #include "curlx/inet_pton.h" |
59 | | #include "multiif.h" |
60 | | #include "doh.h" |
61 | | #include "curlx/warnless.h" |
62 | | #include "select.h" |
63 | | #include "strcase.h" |
64 | | #include "easy_lock.h" |
65 | | #include "curlx/strparse.h" |
66 | | |
67 | | /* The last 3 #include files should be in this order */ |
68 | | #include "curl_printf.h" |
69 | | #include "curl_memory.h" |
70 | | #include "memdebug.h" |
71 | | |
72 | | #if defined(CURLRES_SYNCH) && \ |
73 | | defined(HAVE_ALARM) && \ |
74 | | defined(SIGALRM) && \ |
75 | | defined(HAVE_SIGSETJMP) && \ |
76 | | defined(GLOBAL_INIT_IS_THREADSAFE) |
77 | | /* alarm-based timeouts can only be used with all the dependencies satisfied */ |
78 | | #define USE_ALARM_TIMEOUT |
79 | | #endif |
80 | | |
81 | | #define MAX_HOSTCACHE_LEN (255 + 7) /* max FQDN + colon + port number + zero */ |
82 | | |
83 | 0 | #define MAX_DNS_CACHE_SIZE 29999 |
84 | | |
85 | | /* |
86 | | * hostip.c explained |
87 | | * ================== |
88 | | * |
89 | | * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c |
90 | | * source file are these: |
91 | | * |
92 | | * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use |
93 | | * that. The host may not be able to resolve IPv6, but we do not really have to |
94 | | * take that into account. Hosts that are not IPv6-enabled have CURLRES_IPV4 |
95 | | * defined. |
96 | | * |
97 | | * CURLRES_ARES - is defined if libcurl is built to use c-ares for |
98 | | * asynchronous name resolves. This can be Windows or *nix. |
99 | | * |
100 | | * CURLRES_THREADED - is defined if libcurl is built to run under (native) |
101 | | * Windows, and then the name resolve will be done in a new thread, and the |
102 | | * supported API will be the same as for ares-builds. |
103 | | * |
104 | | * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If |
105 | | * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is |
106 | | * defined. |
107 | | * |
108 | | * The host*.c sources files are split up like this: |
109 | | * |
110 | | * hostip.c - method-independent resolver functions and utility functions |
111 | | * hostip4.c - IPv4 specific functions |
112 | | * hostip6.c - IPv6 specific functions |
113 | | * asyn.h - common functions for all async resolvers |
114 | | * The two asynchronous name resolver backends are implemented in: |
115 | | * asyn-ares.c - async resolver using c-ares |
116 | | * asyn-thread.c - async resolver using POSIX threads |
117 | | * |
118 | | * The hostip.h is the united header file for all this. It defines the |
119 | | * CURLRES_* defines based on the config*.h and curl_setup.h defines. |
120 | | */ |
121 | | |
122 | | static void dnscache_entry_free(struct Curl_dns_entry *dns); |
123 | | |
124 | | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
125 | | static void show_resolve_info(struct Curl_easy *data, |
126 | | struct Curl_dns_entry *dns); |
127 | | #else |
128 | | #define show_resolve_info(x,y) Curl_nop_stmt |
129 | | #endif |
130 | | |
131 | | /* |
132 | | * Curl_printable_address() stores a printable version of the 1st address |
133 | | * given in the 'ai' argument. The result will be stored in the buf that is |
134 | | * bufsize bytes big. |
135 | | * |
136 | | * If the conversion fails, the target buffer is empty. |
137 | | */ |
138 | | void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf, |
139 | | size_t bufsize) |
140 | 0 | { |
141 | 0 | DEBUGASSERT(bufsize); |
142 | 0 | buf[0] = 0; |
143 | |
|
144 | 0 | switch(ai->ai_family) { |
145 | 0 | case AF_INET: { |
146 | 0 | const struct sockaddr_in *sa4 = (const void *)ai->ai_addr; |
147 | 0 | const struct in_addr *ipaddr4 = &sa4->sin_addr; |
148 | 0 | (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize); |
149 | 0 | break; |
150 | 0 | } |
151 | 0 | #ifdef USE_IPV6 |
152 | 0 | case AF_INET6: { |
153 | 0 | const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr; |
154 | 0 | const struct in6_addr *ipaddr6 = &sa6->sin6_addr; |
155 | 0 | (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize); |
156 | 0 | break; |
157 | 0 | } |
158 | 0 | #endif |
159 | 0 | default: |
160 | 0 | break; |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | /* |
165 | | * Create a hostcache id string for the provided host + port, to be used by |
166 | | * the DNS caching. Without alloc. Return length of the id string. |
167 | | */ |
168 | | static size_t |
169 | | create_dnscache_id(const char *name, |
170 | | size_t nlen, /* 0 or actual name length */ |
171 | | int port, char *ptr, size_t buflen) |
172 | 0 | { |
173 | 0 | size_t len = nlen ? nlen : strlen(name); |
174 | 0 | DEBUGASSERT(buflen >= MAX_HOSTCACHE_LEN); |
175 | 0 | if(len > (buflen - 7)) |
176 | 0 | len = buflen - 7; |
177 | | /* store and lower case the name */ |
178 | 0 | Curl_strntolower(ptr, name, len); |
179 | 0 | return msnprintf(&ptr[len], 7, ":%u", port) + len; |
180 | 0 | } |
181 | | |
182 | | struct dnscache_prune_data { |
183 | | struct curltime now; |
184 | | timediff_t oldest_ms; /* oldest time in cache not pruned. */ |
185 | | timediff_t max_age_ms; |
186 | | }; |
187 | | |
188 | | /* |
189 | | * This function is set as a callback to be called for every entry in the DNS |
190 | | * cache when we want to prune old unused entries. |
191 | | * |
192 | | * Returning non-zero means remove the entry, return 0 to keep it in the |
193 | | * cache. |
194 | | */ |
195 | | static int |
196 | | dnscache_entry_is_stale(void *datap, void *hc) |
197 | 0 | { |
198 | 0 | struct dnscache_prune_data *prune = |
199 | 0 | (struct dnscache_prune_data *) datap; |
200 | 0 | struct Curl_dns_entry *dns = (struct Curl_dns_entry *) hc; |
201 | |
|
202 | 0 | if(dns->timestamp.tv_sec || dns->timestamp.tv_usec) { |
203 | | /* get age in milliseconds */ |
204 | 0 | timediff_t age = curlx_timediff(prune->now, dns->timestamp); |
205 | 0 | if(!dns->addr) |
206 | 0 | age *= 2; /* negative entries age twice as fast */ |
207 | 0 | if(age >= prune->max_age_ms) |
208 | 0 | return TRUE; |
209 | 0 | if(age > prune->oldest_ms) |
210 | 0 | prune->oldest_ms = age; |
211 | 0 | } |
212 | 0 | return FALSE; |
213 | 0 | } |
214 | | |
215 | | /* |
216 | | * Prune the DNS cache. This assumes that a lock has already been taken. |
217 | | * Returns the 'age' of the oldest still kept entry - in milliseconds. |
218 | | */ |
219 | | static timediff_t |
220 | | dnscache_prune(struct Curl_hash *hostcache, timediff_t cache_timeout_ms, |
221 | | struct curltime now) |
222 | 0 | { |
223 | 0 | struct dnscache_prune_data user; |
224 | |
|
225 | 0 | user.max_age_ms = cache_timeout_ms; |
226 | 0 | user.now = now; |
227 | 0 | user.oldest_ms = 0; |
228 | |
|
229 | 0 | Curl_hash_clean_with_criterium(hostcache, |
230 | 0 | (void *) &user, |
231 | 0 | dnscache_entry_is_stale); |
232 | |
|
233 | 0 | return user.oldest_ms; |
234 | 0 | } |
235 | | |
236 | | static struct Curl_dnscache *dnscache_get(struct Curl_easy *data) |
237 | 0 | { |
238 | 0 | if(data->share && data->share->specifier & (1 << CURL_LOCK_DATA_DNS)) |
239 | 0 | return &data->share->dnscache; |
240 | 0 | if(data->multi) |
241 | 0 | return &data->multi->dnscache; |
242 | 0 | return NULL; |
243 | 0 | } |
244 | | |
245 | | static void dnscache_lock(struct Curl_easy *data, |
246 | | struct Curl_dnscache *dnscache) |
247 | 0 | { |
248 | 0 | if(data->share && dnscache == &data->share->dnscache) |
249 | 0 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
250 | 0 | } |
251 | | |
252 | | static void dnscache_unlock(struct Curl_easy *data, |
253 | | struct Curl_dnscache *dnscache) |
254 | 0 | { |
255 | 0 | if(data->share && dnscache == &data->share->dnscache) |
256 | 0 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
257 | 0 | } |
258 | | |
259 | | /* |
260 | | * Library-wide function for pruning the DNS cache. This function takes and |
261 | | * returns the appropriate locks. |
262 | | */ |
263 | | void Curl_dnscache_prune(struct Curl_easy *data) |
264 | 0 | { |
265 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
266 | 0 | struct curltime now; |
267 | | /* the timeout may be set -1 (forever) */ |
268 | 0 | timediff_t timeout_ms = data->set.dns_cache_timeout_ms; |
269 | |
|
270 | 0 | if(!dnscache || (timeout_ms == -1)) |
271 | | /* NULL hostcache means we cannot do it */ |
272 | 0 | return; |
273 | | |
274 | 0 | dnscache_lock(data, dnscache); |
275 | |
|
276 | 0 | now = curlx_now(); |
277 | |
|
278 | 0 | do { |
279 | | /* Remove outdated and unused entries from the hostcache */ |
280 | 0 | timediff_t oldest_ms = dnscache_prune(&dnscache->entries, timeout_ms, now); |
281 | |
|
282 | 0 | if(Curl_hash_count(&dnscache->entries) > MAX_DNS_CACHE_SIZE) { |
283 | 0 | if(oldest_ms < INT_MAX) |
284 | | /* prune the ones over half this age */ |
285 | 0 | timeout_ms = (int)oldest_ms / 2; |
286 | 0 | else |
287 | 0 | timeout_ms = INT_MAX/2; |
288 | 0 | } |
289 | 0 | else |
290 | 0 | break; |
291 | | |
292 | | /* if the cache size is still too big, use the oldest age as new prune |
293 | | limit */ |
294 | 0 | } while(timeout_ms); |
295 | | |
296 | 0 | dnscache_unlock(data, dnscache); |
297 | 0 | } |
298 | | |
299 | | void Curl_dnscache_clear(struct Curl_easy *data) |
300 | 0 | { |
301 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
302 | 0 | if(dnscache) { |
303 | 0 | dnscache_lock(data, dnscache); |
304 | 0 | Curl_hash_clean(&dnscache->entries); |
305 | 0 | dnscache_unlock(data, dnscache); |
306 | 0 | } |
307 | 0 | } |
308 | | |
309 | | #ifdef USE_ALARM_TIMEOUT |
310 | | /* Beware this is a global and unique instance. This is used to store the |
311 | | return address that we can jump back to from inside a signal handler. This |
312 | | is not thread-safe stuff. */ |
313 | | static sigjmp_buf curl_jmpenv; |
314 | | static curl_simple_lock curl_jmpenv_lock; |
315 | | #endif |
316 | | |
317 | | /* lookup address, returns entry if found and not stale */ |
318 | | static struct Curl_dns_entry *fetch_addr(struct Curl_easy *data, |
319 | | struct Curl_dnscache *dnscache, |
320 | | const char *hostname, |
321 | | int port, |
322 | | int ip_version) |
323 | 0 | { |
324 | 0 | struct Curl_dns_entry *dns = NULL; |
325 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
326 | 0 | size_t entry_len; |
327 | |
|
328 | 0 | if(!dnscache) |
329 | 0 | return NULL; |
330 | | |
331 | | /* Create an entry id, based upon the hostname and port */ |
332 | 0 | entry_len = create_dnscache_id(hostname, 0, port, |
333 | 0 | entry_id, sizeof(entry_id)); |
334 | | |
335 | | /* See if it is already in our dns cache */ |
336 | 0 | dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1); |
337 | | |
338 | | /* No entry found in cache, check if we might have a wildcard entry */ |
339 | 0 | if(!dns && data->state.wildcard_resolve) { |
340 | 0 | entry_len = create_dnscache_id("*", 1, port, entry_id, sizeof(entry_id)); |
341 | | |
342 | | /* See if it is already in our dns cache */ |
343 | 0 | dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1); |
344 | 0 | } |
345 | |
|
346 | 0 | if(dns && (data->set.dns_cache_timeout_ms != -1)) { |
347 | | /* See whether the returned entry is stale. Done before we release lock */ |
348 | 0 | struct dnscache_prune_data user; |
349 | |
|
350 | 0 | user.now = curlx_now(); |
351 | 0 | user.max_age_ms = data->set.dns_cache_timeout_ms; |
352 | 0 | user.oldest_ms = 0; |
353 | |
|
354 | 0 | if(dnscache_entry_is_stale(&user, dns)) { |
355 | 0 | infof(data, "Hostname in DNS cache was stale, zapped"); |
356 | 0 | dns = NULL; /* the memory deallocation is being handled by the hash */ |
357 | 0 | Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1); |
358 | 0 | } |
359 | 0 | } |
360 | | |
361 | | /* See if the returned entry matches the required resolve mode */ |
362 | 0 | if(dns && ip_version != CURL_IPRESOLVE_WHATEVER) { |
363 | 0 | int pf = PF_INET; |
364 | 0 | bool found = FALSE; |
365 | 0 | struct Curl_addrinfo *addr = dns->addr; |
366 | |
|
367 | 0 | #ifdef PF_INET6 |
368 | 0 | if(ip_version == CURL_IPRESOLVE_V6) |
369 | 0 | pf = PF_INET6; |
370 | 0 | #endif |
371 | |
|
372 | 0 | while(addr) { |
373 | 0 | if(addr->ai_family == pf) { |
374 | 0 | found = TRUE; |
375 | 0 | break; |
376 | 0 | } |
377 | 0 | addr = addr->ai_next; |
378 | 0 | } |
379 | |
|
380 | 0 | if(!found) { |
381 | 0 | infof(data, "Hostname in DNS cache does not have needed family, zapped"); |
382 | 0 | dns = NULL; /* the memory deallocation is being handled by the hash */ |
383 | 0 | Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1); |
384 | 0 | } |
385 | 0 | } |
386 | 0 | return dns; |
387 | 0 | } |
388 | | |
389 | | /* |
390 | | * Curl_dnscache_get() fetches a 'Curl_dns_entry' already in the DNS cache. |
391 | | * |
392 | | * Curl_resolv() checks initially and multi_runsingle() checks each time |
393 | | * it discovers the handle in the state WAITRESOLVE whether the hostname |
394 | | * has already been resolved and the address has already been stored in |
395 | | * the DNS cache. This short circuits waiting for a lot of pending |
396 | | * lookups for the same hostname requested by different handles. |
397 | | * |
398 | | * Returns the Curl_dns_entry entry pointer or NULL if not in the cache. |
399 | | * |
400 | | * The returned data *MUST* be "released" with Curl_resolv_unlink() after |
401 | | * use, or we will leak memory! |
402 | | */ |
403 | | struct Curl_dns_entry * |
404 | | Curl_dnscache_get(struct Curl_easy *data, |
405 | | const char *hostname, |
406 | | int port, |
407 | | int ip_version) |
408 | 0 | { |
409 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
410 | 0 | struct Curl_dns_entry *dns = NULL; |
411 | |
|
412 | 0 | dnscache_lock(data, dnscache); |
413 | |
|
414 | 0 | dns = fetch_addr(data, dnscache, hostname, port, ip_version); |
415 | 0 | if(dns) |
416 | 0 | dns->refcount++; /* we use it! */ |
417 | |
|
418 | 0 | dnscache_unlock(data, dnscache); |
419 | |
|
420 | 0 | return dns; |
421 | 0 | } |
422 | | |
423 | | #ifndef CURL_DISABLE_SHUFFLE_DNS |
424 | | /* |
425 | | * Return # of addresses in a Curl_addrinfo struct |
426 | | */ |
427 | | static int num_addresses(const struct Curl_addrinfo *addr) |
428 | 0 | { |
429 | 0 | int i = 0; |
430 | 0 | while(addr) { |
431 | 0 | addr = addr->ai_next; |
432 | 0 | i++; |
433 | 0 | } |
434 | 0 | return i; |
435 | 0 | } |
436 | | |
437 | | UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data, |
438 | | struct Curl_addrinfo **addr); |
439 | | /* |
440 | | * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo' |
441 | | * struct by re-linking its linked list. |
442 | | * |
443 | | * The addr argument should be the address of a pointer to the head node of a |
444 | | * `Curl_addrinfo` list and it will be modified to point to the new head after |
445 | | * shuffling. |
446 | | * |
447 | | * Not declared static only to make it easy to use in a unit test! |
448 | | * |
449 | | * @unittest: 1608 |
450 | | */ |
451 | | UNITTEST CURLcode Curl_shuffle_addr(struct Curl_easy *data, |
452 | | struct Curl_addrinfo **addr) |
453 | 0 | { |
454 | 0 | CURLcode result = CURLE_OK; |
455 | 0 | const int num_addrs = num_addresses(*addr); |
456 | |
|
457 | 0 | if(num_addrs > 1) { |
458 | 0 | struct Curl_addrinfo **nodes; |
459 | 0 | infof(data, "Shuffling %i addresses", num_addrs); |
460 | |
|
461 | 0 | nodes = malloc(num_addrs*sizeof(*nodes)); |
462 | 0 | if(nodes) { |
463 | 0 | int i; |
464 | 0 | unsigned int *rnd; |
465 | 0 | const size_t rnd_size = num_addrs * sizeof(*rnd); |
466 | | |
467 | | /* build a plain array of Curl_addrinfo pointers */ |
468 | 0 | nodes[0] = *addr; |
469 | 0 | for(i = 1; i < num_addrs; i++) { |
470 | 0 | nodes[i] = nodes[i-1]->ai_next; |
471 | 0 | } |
472 | |
|
473 | 0 | rnd = malloc(rnd_size); |
474 | 0 | if(rnd) { |
475 | | /* Fisher-Yates shuffle */ |
476 | 0 | if(Curl_rand(data, (unsigned char *)rnd, rnd_size) == CURLE_OK) { |
477 | 0 | struct Curl_addrinfo *swap_tmp; |
478 | 0 | for(i = num_addrs - 1; i > 0; i--) { |
479 | 0 | swap_tmp = nodes[rnd[i] % (unsigned int)(i + 1)]; |
480 | 0 | nodes[rnd[i] % (unsigned int)(i + 1)] = nodes[i]; |
481 | 0 | nodes[i] = swap_tmp; |
482 | 0 | } |
483 | | |
484 | | /* relink list in the new order */ |
485 | 0 | for(i = 1; i < num_addrs; i++) { |
486 | 0 | nodes[i-1]->ai_next = nodes[i]; |
487 | 0 | } |
488 | |
|
489 | 0 | nodes[num_addrs-1]->ai_next = NULL; |
490 | 0 | *addr = nodes[0]; |
491 | 0 | } |
492 | 0 | free(rnd); |
493 | 0 | } |
494 | 0 | else |
495 | 0 | result = CURLE_OUT_OF_MEMORY; |
496 | 0 | free(nodes); |
497 | 0 | } |
498 | 0 | else |
499 | 0 | result = CURLE_OUT_OF_MEMORY; |
500 | 0 | } |
501 | 0 | return result; |
502 | 0 | } |
503 | | #endif |
504 | | |
505 | | struct Curl_dns_entry * |
506 | | Curl_dnscache_mk_entry(struct Curl_easy *data, |
507 | | struct Curl_addrinfo *addr, |
508 | | const char *hostname, |
509 | | size_t hostlen, /* length or zero */ |
510 | | int port, |
511 | | bool permanent) |
512 | 0 | { |
513 | 0 | struct Curl_dns_entry *dns; |
514 | |
|
515 | 0 | #ifndef CURL_DISABLE_SHUFFLE_DNS |
516 | | /* shuffle addresses if requested */ |
517 | 0 | if(data->set.dns_shuffle_addresses) { |
518 | 0 | CURLcode result = Curl_shuffle_addr(data, &addr); |
519 | 0 | if(result) { |
520 | 0 | Curl_freeaddrinfo(addr); |
521 | 0 | return NULL; |
522 | 0 | } |
523 | 0 | } |
524 | | #else |
525 | | (void)data; |
526 | | #endif |
527 | 0 | if(!hostlen) |
528 | 0 | hostlen = strlen(hostname); |
529 | | |
530 | | /* Create a new cache entry */ |
531 | 0 | dns = calloc(1, sizeof(struct Curl_dns_entry) + hostlen); |
532 | 0 | if(!dns) { |
533 | 0 | Curl_freeaddrinfo(addr); |
534 | 0 | return NULL; |
535 | 0 | } |
536 | | |
537 | 0 | dns->refcount = 1; /* the cache has the first reference */ |
538 | 0 | dns->addr = addr; /* this is the address(es) */ |
539 | 0 | if(permanent) { |
540 | 0 | dns->timestamp.tv_sec = 0; /* an entry that never goes stale */ |
541 | 0 | dns->timestamp.tv_usec = 0; /* an entry that never goes stale */ |
542 | 0 | } |
543 | 0 | else { |
544 | 0 | dns->timestamp = curlx_now(); |
545 | 0 | } |
546 | 0 | dns->hostport = port; |
547 | 0 | if(hostlen) |
548 | 0 | memcpy(dns->hostname, hostname, hostlen); |
549 | |
|
550 | 0 | return dns; |
551 | 0 | } |
552 | | |
553 | | static struct Curl_dns_entry * |
554 | | dnscache_add_addr(struct Curl_easy *data, |
555 | | struct Curl_dnscache *dnscache, |
556 | | struct Curl_addrinfo *addr, |
557 | | const char *hostname, |
558 | | size_t hlen, /* length or zero */ |
559 | | int port, |
560 | | bool permanent) |
561 | 0 | { |
562 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
563 | 0 | size_t entry_len; |
564 | 0 | struct Curl_dns_entry *dns; |
565 | 0 | struct Curl_dns_entry *dns2; |
566 | |
|
567 | 0 | dns = Curl_dnscache_mk_entry(data, addr, hostname, hlen, port, permanent); |
568 | 0 | if(!dns) |
569 | 0 | return NULL; |
570 | | |
571 | | /* Create an entry id, based upon the hostname and port */ |
572 | 0 | entry_len = create_dnscache_id(hostname, hlen, port, |
573 | 0 | entry_id, sizeof(entry_id)); |
574 | | |
575 | | /* Store the resolved data in our DNS cache. */ |
576 | 0 | dns2 = Curl_hash_add(&dnscache->entries, entry_id, entry_len + 1, |
577 | 0 | (void *)dns); |
578 | 0 | if(!dns2) { |
579 | 0 | dnscache_entry_free(dns); |
580 | 0 | return NULL; |
581 | 0 | } |
582 | | |
583 | 0 | dns = dns2; |
584 | 0 | dns->refcount++; /* mark entry as in-use */ |
585 | 0 | return dns; |
586 | 0 | } |
587 | | |
588 | | CURLcode Curl_dnscache_add(struct Curl_easy *data, |
589 | | struct Curl_dns_entry *entry) |
590 | 0 | { |
591 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
592 | 0 | char id[MAX_HOSTCACHE_LEN]; |
593 | 0 | size_t idlen; |
594 | |
|
595 | 0 | if(!dnscache) |
596 | 0 | return CURLE_FAILED_INIT; |
597 | | /* Create an entry id, based upon the hostname and port */ |
598 | 0 | idlen = create_dnscache_id(entry->hostname, 0, entry->hostport, |
599 | 0 | id, sizeof(id)); |
600 | | |
601 | | /* Store the resolved data in our DNS cache and up ref count */ |
602 | 0 | dnscache_lock(data, dnscache); |
603 | 0 | if(!Curl_hash_add(&dnscache->entries, id, idlen + 1, (void *)entry)) { |
604 | 0 | dnscache_unlock(data, dnscache); |
605 | 0 | return CURLE_OUT_OF_MEMORY; |
606 | 0 | } |
607 | 0 | entry->refcount++; |
608 | 0 | dnscache_unlock(data, dnscache); |
609 | 0 | return CURLE_OK; |
610 | 0 | } |
611 | | |
612 | | #ifdef USE_IPV6 |
613 | | /* return a static IPv6 ::1 for the name */ |
614 | | static struct Curl_addrinfo *get_localhost6(int port, const char *name) |
615 | 0 | { |
616 | 0 | struct Curl_addrinfo *ca; |
617 | 0 | const size_t ss_size = sizeof(struct sockaddr_in6); |
618 | 0 | const size_t hostlen = strlen(name); |
619 | 0 | struct sockaddr_in6 sa6; |
620 | 0 | unsigned char ipv6[16]; |
621 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
622 | 0 | ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1); |
623 | 0 | if(!ca) |
624 | 0 | return NULL; |
625 | | |
626 | 0 | sa6.sin6_family = AF_INET6; |
627 | 0 | sa6.sin6_port = htons(port16); |
628 | 0 | sa6.sin6_flowinfo = 0; |
629 | 0 | #ifdef HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID |
630 | 0 | sa6.sin6_scope_id = 0; |
631 | 0 | #endif |
632 | |
|
633 | 0 | (void)curlx_inet_pton(AF_INET6, "::1", ipv6); |
634 | 0 | memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6)); |
635 | |
|
636 | 0 | ca->ai_flags = 0; |
637 | 0 | ca->ai_family = AF_INET6; |
638 | 0 | ca->ai_socktype = SOCK_STREAM; |
639 | 0 | ca->ai_protocol = IPPROTO_TCP; |
640 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
641 | 0 | ca->ai_next = NULL; |
642 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
643 | 0 | memcpy(ca->ai_addr, &sa6, ss_size); |
644 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
645 | 0 | strcpy(ca->ai_canonname, name); |
646 | 0 | return ca; |
647 | 0 | } |
648 | | #else |
649 | | #define get_localhost6(x,y) NULL |
650 | | #endif |
651 | | |
652 | | /* return a static IPv4 127.0.0.1 for the given name */ |
653 | | static struct Curl_addrinfo *get_localhost(int port, const char *name) |
654 | 0 | { |
655 | 0 | struct Curl_addrinfo *ca; |
656 | 0 | struct Curl_addrinfo *ca6; |
657 | 0 | const size_t ss_size = sizeof(struct sockaddr_in); |
658 | 0 | const size_t hostlen = strlen(name); |
659 | 0 | struct sockaddr_in sa; |
660 | 0 | unsigned int ipv4; |
661 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
662 | | |
663 | | /* memset to clear the sa.sin_zero field */ |
664 | 0 | memset(&sa, 0, sizeof(sa)); |
665 | 0 | sa.sin_family = AF_INET; |
666 | 0 | sa.sin_port = htons(port16); |
667 | 0 | if(curlx_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1) |
668 | 0 | return NULL; |
669 | 0 | memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4)); |
670 | |
|
671 | 0 | ca = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1); |
672 | 0 | if(!ca) |
673 | 0 | return NULL; |
674 | 0 | ca->ai_flags = 0; |
675 | 0 | ca->ai_family = AF_INET; |
676 | 0 | ca->ai_socktype = SOCK_STREAM; |
677 | 0 | ca->ai_protocol = IPPROTO_TCP; |
678 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
679 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
680 | 0 | memcpy(ca->ai_addr, &sa, ss_size); |
681 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
682 | 0 | strcpy(ca->ai_canonname, name); |
683 | |
|
684 | 0 | ca6 = get_localhost6(port, name); |
685 | 0 | if(!ca6) |
686 | 0 | return ca; |
687 | 0 | ca6->ai_next = ca; |
688 | 0 | return ca6; |
689 | 0 | } |
690 | | |
691 | | #ifdef USE_IPV6 |
692 | | /* |
693 | | * Curl_ipv6works() returns TRUE if IPv6 seems to work. |
694 | | */ |
695 | | bool Curl_ipv6works(struct Curl_easy *data) |
696 | 0 | { |
697 | 0 | if(data) { |
698 | | /* the nature of most system is that IPv6 status does not come and go |
699 | | during a program's lifetime so we only probe the first time and then we |
700 | | have the info kept for fast reuse */ |
701 | 0 | DEBUGASSERT(data); |
702 | 0 | DEBUGASSERT(data->multi); |
703 | 0 | if(data->multi->ipv6_up == IPV6_UNKNOWN) { |
704 | 0 | bool works = Curl_ipv6works(NULL); |
705 | 0 | data->multi->ipv6_up = works ? IPV6_WORKS : IPV6_DEAD; |
706 | 0 | } |
707 | 0 | return data->multi->ipv6_up == IPV6_WORKS; |
708 | 0 | } |
709 | 0 | else { |
710 | 0 | int ipv6_works = -1; |
711 | | /* probe to see if we have a working IPv6 stack */ |
712 | 0 | curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0); |
713 | 0 | if(s == CURL_SOCKET_BAD) |
714 | | /* an IPv6 address was requested but we cannot get/use one */ |
715 | 0 | ipv6_works = 0; |
716 | 0 | else { |
717 | 0 | ipv6_works = 1; |
718 | 0 | sclose(s); |
719 | 0 | } |
720 | 0 | return ipv6_works > 0; |
721 | 0 | } |
722 | 0 | } |
723 | | #endif /* USE_IPV6 */ |
724 | | |
725 | | /* |
726 | | * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4 |
727 | | * (or IPv6 if supported) address. |
728 | | */ |
729 | | bool Curl_host_is_ipnum(const char *hostname) |
730 | 0 | { |
731 | 0 | struct in_addr in; |
732 | 0 | #ifdef USE_IPV6 |
733 | 0 | struct in6_addr in6; |
734 | 0 | #endif |
735 | 0 | if(curlx_inet_pton(AF_INET, hostname, &in) > 0 |
736 | 0 | #ifdef USE_IPV6 |
737 | 0 | || curlx_inet_pton(AF_INET6, hostname, &in6) > 0 |
738 | 0 | #endif |
739 | 0 | ) |
740 | 0 | return TRUE; |
741 | 0 | return FALSE; |
742 | 0 | } |
743 | | |
744 | | |
745 | | /* return TRUE if 'part' is a case insensitive tail of 'full' */ |
746 | | static bool tailmatch(const char *full, size_t flen, |
747 | | const char *part, size_t plen) |
748 | 0 | { |
749 | 0 | if(plen > flen) |
750 | 0 | return FALSE; |
751 | 0 | return curl_strnequal(part, &full[flen - plen], plen); |
752 | 0 | } |
753 | | |
754 | | static struct Curl_addrinfo * |
755 | | convert_ipaddr_direct(const char *hostname, int port, bool *is_ipaddr) |
756 | 0 | { |
757 | 0 | struct in_addr in; |
758 | 0 | *is_ipaddr = FALSE; |
759 | | /* First check if this is an IPv4 address string */ |
760 | 0 | if(curlx_inet_pton(AF_INET, hostname, &in) > 0) { |
761 | | /* This is a dotted IP address 123.123.123.123-style */ |
762 | 0 | *is_ipaddr = TRUE; |
763 | | #ifdef USE_RESOLVE_ON_IPS |
764 | | (void)port; |
765 | | return NULL; |
766 | | #else |
767 | 0 | return Curl_ip2addr(AF_INET, &in, hostname, port); |
768 | 0 | #endif |
769 | 0 | } |
770 | 0 | #ifdef USE_IPV6 |
771 | 0 | else { |
772 | 0 | struct in6_addr in6; |
773 | | /* check if this is an IPv6 address string */ |
774 | 0 | if(curlx_inet_pton(AF_INET6, hostname, &in6) > 0) { |
775 | | /* This is an IPv6 address literal */ |
776 | 0 | *is_ipaddr = TRUE; |
777 | | #ifdef USE_RESOLVE_ON_IPS |
778 | | return NULL; |
779 | | #else |
780 | 0 | return Curl_ip2addr(AF_INET6, &in6, hostname, port); |
781 | 0 | #endif |
782 | 0 | } |
783 | 0 | } |
784 | 0 | #endif /* USE_IPV6 */ |
785 | 0 | return NULL; |
786 | 0 | } |
787 | | |
788 | | static bool can_resolve_ip_version(struct Curl_easy *data, int ip_version) |
789 | 0 | { |
790 | 0 | #ifdef CURLRES_IPV6 |
791 | 0 | if(ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data)) |
792 | 0 | return FALSE; |
793 | | #elif defined(CURLRES_IPV4) |
794 | | (void)data; |
795 | | if(ip_version == CURL_IPRESOLVE_V6) |
796 | | return FALSE; |
797 | | #else |
798 | | #error either CURLRES_IPV6 or CURLRES_IPV4 need to be defined |
799 | | #endif |
800 | 0 | return TRUE; |
801 | 0 | } |
802 | | |
803 | | static CURLcode store_negative_resolve(struct Curl_easy *data, |
804 | | const char *host, |
805 | | int port) |
806 | 0 | { |
807 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
808 | 0 | struct Curl_dns_entry *dns; |
809 | 0 | DEBUGASSERT(dnscache); |
810 | 0 | if(!dnscache) |
811 | 0 | return CURLE_FAILED_INIT; |
812 | | |
813 | | /* put this new host in the cache */ |
814 | 0 | dns = dnscache_add_addr(data, dnscache, NULL, host, 0, port, FALSE); |
815 | 0 | if(dns) { |
816 | | /* release the returned reference; the cache itself will keep the |
817 | | * entry alive: */ |
818 | 0 | dns->refcount--; |
819 | 0 | infof(data, "Store negative name resolve for %s:%d", host, port); |
820 | 0 | return CURLE_OK; |
821 | 0 | } |
822 | 0 | return CURLE_OUT_OF_MEMORY; |
823 | 0 | } |
824 | | |
825 | | /* |
826 | | * Curl_resolv() is the main name resolve function within libcurl. It resolves |
827 | | * a name and returns a pointer to the entry in the 'entry' argument (if one |
828 | | * is provided). This function might return immediately if we are using asynch |
829 | | * resolves. See the return codes. |
830 | | * |
831 | | * The cache entry we return will get its 'inuse' counter increased when this |
832 | | * function is used. You MUST call Curl_resolv_unlink() later (when you are |
833 | | * done using this struct) to decrease the reference counter again. |
834 | | * |
835 | | * Return codes: |
836 | | * CURLE_OK = success, *entry set to non-NULL |
837 | | * CURLE_AGAIN = resolving in progress, *entry == NULL |
838 | | * CURLE_COULDNT_RESOLVE_HOST = error, *entry == NULL |
839 | | * CURLE_OPERATION_TIMEDOUT = timeout expired, *entry == NULL |
840 | | */ |
841 | | CURLcode Curl_resolv(struct Curl_easy *data, |
842 | | const char *hostname, |
843 | | int port, |
844 | | int ip_version, |
845 | | bool allowDOH, |
846 | | struct Curl_dns_entry **entry) |
847 | 0 | { |
848 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
849 | 0 | struct Curl_dns_entry *dns = NULL; |
850 | 0 | struct Curl_addrinfo *addr = NULL; |
851 | 0 | int respwait = 0; |
852 | 0 | bool is_ipaddr; |
853 | 0 | size_t hostname_len; |
854 | |
|
855 | 0 | #ifndef CURL_DISABLE_DOH |
856 | 0 | data->conn->bits.doh = FALSE; /* default is not */ |
857 | | #else |
858 | | (void)allowDOH; |
859 | | #endif |
860 | 0 | if(!dnscache) |
861 | 0 | goto error; |
862 | | |
863 | | /* We should intentionally error and not resolve .onion TLDs */ |
864 | 0 | hostname_len = strlen(hostname); |
865 | 0 | if(hostname_len >= 7 && |
866 | 0 | (curl_strequal(&hostname[hostname_len - 6], ".onion") || |
867 | 0 | curl_strequal(&hostname[hostname_len - 7], ".onion."))) { |
868 | 0 | failf(data, "Not resolving .onion address (RFC 7686)"); |
869 | 0 | goto error; |
870 | 0 | } |
871 | | |
872 | | /* Let's check our DNS cache first */ |
873 | 0 | dnscache_lock(data, dnscache); |
874 | 0 | dns = fetch_addr(data, dnscache, hostname, port, ip_version); |
875 | 0 | if(dns) |
876 | 0 | dns->refcount++; /* we pass out the reference. */ |
877 | 0 | dnscache_unlock(data, dnscache); |
878 | 0 | if(dns) { |
879 | 0 | infof(data, "Hostname %s was found in DNS cache", hostname); |
880 | 0 | goto out; |
881 | 0 | } |
882 | | |
883 | | /* No luck, we need to resolve hostname. Notify user callback. */ |
884 | 0 | if(data->set.resolver_start) { |
885 | 0 | void *resolver = NULL; |
886 | 0 | int st; |
887 | 0 | #ifdef CURLRES_ASYNCH |
888 | 0 | if(Curl_async_get_impl(data, &resolver)) |
889 | 0 | goto error; |
890 | 0 | #endif |
891 | 0 | Curl_set_in_callback(data, TRUE); |
892 | 0 | st = data->set.resolver_start(resolver, NULL, |
893 | 0 | data->set.resolver_start_client); |
894 | 0 | Curl_set_in_callback(data, FALSE); |
895 | 0 | if(st) |
896 | 0 | goto error; |
897 | 0 | } |
898 | | |
899 | | /* shortcut literal IP addresses, if we are not told to resolve them. */ |
900 | 0 | addr = convert_ipaddr_direct(hostname, port, &is_ipaddr); |
901 | 0 | if(addr) |
902 | 0 | goto out; |
903 | | |
904 | 0 | #ifndef USE_RESOLVE_ON_IPS |
905 | | /* allowed to convert, hostname is IP address, then NULL means error */ |
906 | 0 | if(is_ipaddr) |
907 | 0 | goto error; |
908 | 0 | #endif |
909 | | |
910 | | /* Really need a resolver for hostname. */ |
911 | 0 | if(ip_version == CURL_IPRESOLVE_V6 && !Curl_ipv6works(data)) |
912 | 0 | goto error; |
913 | | |
914 | 0 | if(!is_ipaddr && |
915 | 0 | (curl_strequal(hostname, "localhost") || |
916 | 0 | curl_strequal(hostname, "localhost.") || |
917 | 0 | tailmatch(hostname, hostname_len, STRCONST(".localhost")) || |
918 | 0 | tailmatch(hostname, hostname_len, STRCONST(".localhost.")))) { |
919 | 0 | addr = get_localhost(port, hostname); |
920 | 0 | } |
921 | 0 | #ifndef CURL_DISABLE_DOH |
922 | 0 | else if(!is_ipaddr && allowDOH && data->set.doh) { |
923 | 0 | addr = Curl_doh(data, hostname, port, ip_version, &respwait); |
924 | 0 | } |
925 | 0 | #endif |
926 | 0 | else { |
927 | | /* Can we provide the requested IP specifics in resolving? */ |
928 | 0 | if(!can_resolve_ip_version(data, ip_version)) |
929 | 0 | goto error; |
930 | | |
931 | 0 | #ifdef CURLRES_ASYNCH |
932 | 0 | addr = Curl_async_getaddrinfo(data, hostname, port, ip_version, &respwait); |
933 | | #else |
934 | | respwait = 0; /* no async waiting here */ |
935 | | addr = Curl_sync_getaddrinfo(data, hostname, port, ip_version); |
936 | | #endif |
937 | 0 | } |
938 | | |
939 | 0 | out: |
940 | | /* We either have found a `dns` or looked up the `addr` |
941 | | * or `respwait` is set for an async operation. |
942 | | * Everything else is a failure to resolve. */ |
943 | 0 | if(dns) { |
944 | 0 | if(!dns->addr) { |
945 | 0 | infof(data, "Negative DNS entry"); |
946 | 0 | dns->refcount--; |
947 | 0 | return CURLE_COULDNT_RESOLVE_HOST; |
948 | 0 | } |
949 | 0 | *entry = dns; |
950 | 0 | return CURLE_OK; |
951 | 0 | } |
952 | 0 | else if(addr) { |
953 | | /* we got a response, create a dns entry, add to cache, return */ |
954 | 0 | dns = Curl_dnscache_mk_entry(data, addr, hostname, 0, port, FALSE); |
955 | 0 | if(!dns) |
956 | 0 | goto error; |
957 | 0 | if(Curl_dnscache_add(data, dns)) |
958 | 0 | goto error; |
959 | 0 | show_resolve_info(data, dns); |
960 | 0 | *entry = dns; |
961 | 0 | return CURLE_OK; |
962 | 0 | } |
963 | 0 | else if(respwait) { |
964 | 0 | if(!Curl_resolv_check(data, &dns)) { |
965 | 0 | *entry = dns; |
966 | 0 | return dns ? CURLE_OK : CURLE_AGAIN; |
967 | 0 | } |
968 | 0 | } |
969 | 0 | error: |
970 | 0 | if(dns) |
971 | 0 | Curl_resolv_unlink(data, &dns); |
972 | 0 | *entry = NULL; |
973 | 0 | Curl_async_shutdown(data); |
974 | 0 | store_negative_resolve(data, hostname, port); |
975 | 0 | return CURLE_COULDNT_RESOLVE_HOST; |
976 | 0 | } |
977 | | |
978 | | CURLcode Curl_resolv_blocking(struct Curl_easy *data, |
979 | | const char *hostname, |
980 | | int port, |
981 | | int ip_version, |
982 | | struct Curl_dns_entry **dnsentry) |
983 | 0 | { |
984 | 0 | CURLcode result; |
985 | |
|
986 | 0 | *dnsentry = NULL; |
987 | 0 | result = Curl_resolv(data, hostname, port, ip_version, FALSE, dnsentry); |
988 | 0 | switch(result) { |
989 | 0 | case CURLE_OK: |
990 | 0 | DEBUGASSERT(*dnsentry); |
991 | 0 | return CURLE_OK; |
992 | 0 | case CURLE_AGAIN: |
993 | 0 | DEBUGASSERT(!*dnsentry); |
994 | 0 | result = Curl_async_await(data, dnsentry); |
995 | 0 | if(result || !*dnsentry) { |
996 | | /* close the connection, since we cannot return failure here without |
997 | | cleaning up this connection properly. */ |
998 | 0 | connclose(data->conn, "async resolve failed"); |
999 | 0 | } |
1000 | 0 | return result; |
1001 | 0 | default: |
1002 | 0 | return result; |
1003 | 0 | } |
1004 | 0 | } |
1005 | | |
1006 | | #ifdef USE_ALARM_TIMEOUT |
1007 | | /* |
1008 | | * This signal handler jumps back into the main libcurl code and continues |
1009 | | * execution. This effectively causes the remainder of the application to run |
1010 | | * within a signal handler which is nonportable and could lead to problems. |
1011 | | */ |
1012 | | CURL_NORETURN static |
1013 | | void alarmfunc(int sig) |
1014 | | { |
1015 | | (void)sig; |
1016 | | siglongjmp(curl_jmpenv, 1); |
1017 | | } |
1018 | | #endif /* USE_ALARM_TIMEOUT */ |
1019 | | |
1020 | | /* |
1021 | | * Curl_resolv_timeout() is the same as Curl_resolv() but specifies a |
1022 | | * timeout. This function might return immediately if we are using asynch |
1023 | | * resolves. See the return codes. |
1024 | | * |
1025 | | * The cache entry we return will get its 'inuse' counter increased when this |
1026 | | * function is used. You MUST call Curl_resolv_unlink() later (when you are |
1027 | | * done using this struct) to decrease the reference counter again. |
1028 | | * |
1029 | | * If built with a synchronous resolver and use of signals is not |
1030 | | * disabled by the application, then a nonzero timeout will cause a |
1031 | | * timeout after the specified number of milliseconds. Otherwise, timeout |
1032 | | * is ignored. |
1033 | | * |
1034 | | * Return codes: |
1035 | | * CURLE_OK = success, *entry set to non-NULL |
1036 | | * CURLE_AGAIN = resolving in progress, *entry == NULL |
1037 | | * CURLE_COULDNT_RESOLVE_HOST = error, *entry == NULL |
1038 | | * CURLE_OPERATION_TIMEDOUT = timeout expired, *entry == NULL |
1039 | | */ |
1040 | | |
1041 | | CURLcode Curl_resolv_timeout(struct Curl_easy *data, |
1042 | | const char *hostname, |
1043 | | int port, |
1044 | | int ip_version, |
1045 | | struct Curl_dns_entry **entry, |
1046 | | timediff_t timeoutms) |
1047 | 0 | { |
1048 | | #ifdef USE_ALARM_TIMEOUT |
1049 | | #ifdef HAVE_SIGACTION |
1050 | | struct sigaction keep_sigact; /* store the old struct here */ |
1051 | | volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */ |
1052 | | struct sigaction sigact; |
1053 | | #else |
1054 | | #ifdef HAVE_SIGNAL |
1055 | | void (*keep_sigact)(int); /* store the old handler here */ |
1056 | | #endif /* HAVE_SIGNAL */ |
1057 | | #endif /* HAVE_SIGACTION */ |
1058 | | volatile long timeout; |
1059 | | volatile unsigned int prev_alarm = 0; |
1060 | | #endif /* USE_ALARM_TIMEOUT */ |
1061 | 0 | CURLcode result; |
1062 | |
|
1063 | 0 | *entry = NULL; |
1064 | |
|
1065 | 0 | if(timeoutms < 0) |
1066 | | /* got an already expired timeout */ |
1067 | 0 | return CURLE_OPERATION_TIMEDOUT; |
1068 | | |
1069 | | #ifdef USE_ALARM_TIMEOUT |
1070 | | if(data->set.no_signal) |
1071 | | /* Ignore the timeout when signals are disabled */ |
1072 | | timeout = 0; |
1073 | | else |
1074 | | timeout = (timeoutms > LONG_MAX) ? LONG_MAX : (long)timeoutms; |
1075 | | |
1076 | | if(!timeout |
1077 | | #ifndef CURL_DISABLE_DOH |
1078 | | || data->set.doh |
1079 | | #endif |
1080 | | ) |
1081 | | /* USE_ALARM_TIMEOUT defined, but no timeout actually requested or resolve |
1082 | | done using DoH */ |
1083 | | return Curl_resolv(data, hostname, port, ip_version, TRUE, entry); |
1084 | | |
1085 | | if(timeout < 1000) { |
1086 | | /* The alarm() function only provides integer second resolution, so if |
1087 | | we want to wait less than one second we must bail out already now. */ |
1088 | | failf(data, |
1089 | | "remaining timeout of %ld too small to resolve via SIGALRM method", |
1090 | | timeout); |
1091 | | return CURLE_OPERATION_TIMEDOUT; |
1092 | | } |
1093 | | /* This allows us to time-out from the name resolver, as the timeout |
1094 | | will generate a signal and we will siglongjmp() from that here. |
1095 | | This technique has problems (see alarmfunc). |
1096 | | This should be the last thing we do before calling Curl_resolv(), |
1097 | | as otherwise we would have to worry about variables that get modified |
1098 | | before we invoke Curl_resolv() (and thus use "volatile"). */ |
1099 | | curl_simple_lock_lock(&curl_jmpenv_lock); |
1100 | | |
1101 | | if(sigsetjmp(curl_jmpenv, 1)) { |
1102 | | /* this is coming from a siglongjmp() after an alarm signal */ |
1103 | | failf(data, "name lookup timed out"); |
1104 | | result = CURLE_OPERATION_TIMEDOUT; |
1105 | | goto clean_up; |
1106 | | } |
1107 | | else { |
1108 | | /************************************************************* |
1109 | | * Set signal handler to catch SIGALRM |
1110 | | * Store the old value to be able to set it back later! |
1111 | | *************************************************************/ |
1112 | | #ifdef HAVE_SIGACTION |
1113 | | sigaction(SIGALRM, NULL, &sigact); |
1114 | | keep_sigact = sigact; |
1115 | | keep_copysig = TRUE; /* yes, we have a copy */ |
1116 | | sigact.sa_handler = alarmfunc; |
1117 | | #ifdef SA_RESTART |
1118 | | /* HP-UX does not have SA_RESTART but defaults to that behavior! */ |
1119 | | sigact.sa_flags &= ~SA_RESTART; |
1120 | | #endif |
1121 | | /* now set the new struct */ |
1122 | | sigaction(SIGALRM, &sigact, NULL); |
1123 | | #else /* HAVE_SIGACTION */ |
1124 | | /* no sigaction(), revert to the much lamer signal() */ |
1125 | | #ifdef HAVE_SIGNAL |
1126 | | keep_sigact = signal(SIGALRM, alarmfunc); |
1127 | | #endif |
1128 | | #endif /* HAVE_SIGACTION */ |
1129 | | |
1130 | | /* alarm() makes a signal get sent when the timeout fires off, and that |
1131 | | will abort system calls */ |
1132 | | prev_alarm = alarm(curlx_sltoui(timeout/1000L)); |
1133 | | } |
1134 | | |
1135 | | #ifdef DEBUGBUILD |
1136 | | Curl_resolve_test_delay(); |
1137 | | #endif |
1138 | | |
1139 | | #else /* USE_ALARM_TIMEOUT */ |
1140 | | #ifndef CURLRES_ASYNCH |
1141 | | if(timeoutms) |
1142 | | infof(data, "timeout on name lookup is not supported"); |
1143 | | #else |
1144 | 0 | (void)timeoutms; /* timeoutms not used with an async resolver */ |
1145 | 0 | #endif |
1146 | 0 | #endif /* else USE_ALARM_TIMEOUT */ |
1147 | | |
1148 | | /* Perform the actual name resolution. This might be interrupted by an |
1149 | | * alarm if it takes too long. |
1150 | | */ |
1151 | 0 | result = Curl_resolv(data, hostname, port, ip_version, TRUE, entry); |
1152 | |
|
1153 | | #ifdef USE_ALARM_TIMEOUT |
1154 | | clean_up: |
1155 | | |
1156 | | if(!prev_alarm) |
1157 | | /* deactivate a possibly active alarm before uninstalling the handler */ |
1158 | | alarm(0); |
1159 | | |
1160 | | #ifdef HAVE_SIGACTION |
1161 | | if(keep_copysig) { |
1162 | | /* we got a struct as it looked before, now put that one back nice |
1163 | | and clean */ |
1164 | | sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */ |
1165 | | } |
1166 | | #else |
1167 | | #ifdef HAVE_SIGNAL |
1168 | | /* restore the previous SIGALRM handler */ |
1169 | | signal(SIGALRM, keep_sigact); |
1170 | | #endif |
1171 | | #endif /* HAVE_SIGACTION */ |
1172 | | |
1173 | | curl_simple_lock_unlock(&curl_jmpenv_lock); |
1174 | | |
1175 | | /* switch back the alarm() to either zero or to what it was before minus |
1176 | | the time we spent until now! */ |
1177 | | if(prev_alarm) { |
1178 | | /* there was an alarm() set before us, now put it back */ |
1179 | | timediff_t elapsed_secs = curlx_timediff(curlx_now(), |
1180 | | data->conn->created) / 1000; |
1181 | | |
1182 | | /* the alarm period is counted in even number of seconds */ |
1183 | | unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs); |
1184 | | |
1185 | | if(!alarm_set || |
1186 | | ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000)) ) { |
1187 | | /* if the alarm time-left reached zero or turned "negative" (counted |
1188 | | with unsigned values), we should fire off a SIGALRM here, but we |
1189 | | will not, and zero would be to switch it off so we never set it to |
1190 | | less than 1! */ |
1191 | | alarm(1); |
1192 | | result = CURLE_OPERATION_TIMEDOUT; |
1193 | | failf(data, "Previous alarm fired off"); |
1194 | | } |
1195 | | else |
1196 | | alarm((unsigned int)alarm_set); |
1197 | | } |
1198 | | #endif /* USE_ALARM_TIMEOUT */ |
1199 | |
|
1200 | 0 | return result; |
1201 | 0 | } |
1202 | | |
1203 | | static void dnscache_entry_free(struct Curl_dns_entry *dns) |
1204 | 0 | { |
1205 | 0 | Curl_freeaddrinfo(dns->addr); |
1206 | | #ifdef USE_HTTPSRR |
1207 | | if(dns->hinfo) { |
1208 | | Curl_httpsrr_cleanup(dns->hinfo); |
1209 | | free(dns->hinfo); |
1210 | | } |
1211 | | #endif |
1212 | 0 | free(dns); |
1213 | 0 | } |
1214 | | |
1215 | | /* |
1216 | | * Curl_resolv_unlink() releases a reference to the given cached DNS entry. |
1217 | | * When the reference count reaches 0, the entry is destroyed. It is important |
1218 | | * that only one unlink is made for each Curl_resolv() call. |
1219 | | * |
1220 | | * May be called with 'data' == NULL for global cache. |
1221 | | */ |
1222 | | void Curl_resolv_unlink(struct Curl_easy *data, struct Curl_dns_entry **pdns) |
1223 | 0 | { |
1224 | 0 | if(*pdns) { |
1225 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
1226 | 0 | struct Curl_dns_entry *dns = *pdns; |
1227 | 0 | *pdns = NULL; |
1228 | 0 | dnscache_lock(data, dnscache); |
1229 | 0 | dns->refcount--; |
1230 | 0 | if(dns->refcount == 0) |
1231 | 0 | dnscache_entry_free(dns); |
1232 | 0 | dnscache_unlock(data, dnscache); |
1233 | 0 | } |
1234 | 0 | } |
1235 | | |
1236 | | static void dnscache_entry_dtor(void *entry) |
1237 | 0 | { |
1238 | 0 | struct Curl_dns_entry *dns = (struct Curl_dns_entry *) entry; |
1239 | 0 | DEBUGASSERT(dns && (dns->refcount > 0)); |
1240 | 0 | dns->refcount--; |
1241 | 0 | if(dns->refcount == 0) |
1242 | 0 | dnscache_entry_free(dns); |
1243 | 0 | } |
1244 | | |
1245 | | /* |
1246 | | * Curl_dnscache_init() inits a new DNS cache. |
1247 | | */ |
1248 | | void Curl_dnscache_init(struct Curl_dnscache *dns, size_t size) |
1249 | 0 | { |
1250 | 0 | Curl_hash_init(&dns->entries, size, Curl_hash_str, curlx_str_key_compare, |
1251 | 0 | dnscache_entry_dtor); |
1252 | 0 | } |
1253 | | |
1254 | | void Curl_dnscache_destroy(struct Curl_dnscache *dns) |
1255 | 0 | { |
1256 | 0 | Curl_hash_destroy(&dns->entries); |
1257 | 0 | } |
1258 | | |
1259 | | CURLcode Curl_loadhostpairs(struct Curl_easy *data) |
1260 | 0 | { |
1261 | 0 | struct Curl_dnscache *dnscache = dnscache_get(data); |
1262 | 0 | struct curl_slist *hostp; |
1263 | |
|
1264 | 0 | if(!dnscache) |
1265 | 0 | return CURLE_FAILED_INIT; |
1266 | | |
1267 | | /* Default is no wildcard found */ |
1268 | 0 | data->state.wildcard_resolve = FALSE; |
1269 | |
|
1270 | 0 | for(hostp = data->state.resolve; hostp; hostp = hostp->next) { |
1271 | 0 | char entry_id[MAX_HOSTCACHE_LEN]; |
1272 | 0 | const char *host = hostp->data; |
1273 | 0 | struct Curl_str source; |
1274 | 0 | if(!host) |
1275 | 0 | continue; |
1276 | 0 | if(*host == '-') { |
1277 | 0 | curl_off_t num = 0; |
1278 | 0 | size_t entry_len; |
1279 | 0 | host++; |
1280 | 0 | if(!curlx_str_single(&host, '[')) { |
1281 | 0 | if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') || |
1282 | 0 | curlx_str_single(&host, ']') || |
1283 | 0 | curlx_str_single(&host, ':')) |
1284 | 0 | continue; |
1285 | 0 | } |
1286 | 0 | else { |
1287 | 0 | if(curlx_str_until(&host, &source, 4096, ':') || |
1288 | 0 | curlx_str_single(&host, ':')) { |
1289 | 0 | continue; |
1290 | 0 | } |
1291 | 0 | } |
1292 | | |
1293 | 0 | if(!curlx_str_number(&host, &num, 0xffff)) { |
1294 | | /* Create an entry id, based upon the hostname and port */ |
1295 | 0 | entry_len = create_dnscache_id(curlx_str(&source), |
1296 | 0 | curlx_strlen(&source), (int)num, |
1297 | 0 | entry_id, sizeof(entry_id)); |
1298 | 0 | dnscache_lock(data, dnscache); |
1299 | | /* delete entry, ignore if it did not exist */ |
1300 | 0 | Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1); |
1301 | 0 | dnscache_unlock(data, dnscache); |
1302 | 0 | } |
1303 | 0 | } |
1304 | 0 | else { |
1305 | 0 | struct Curl_dns_entry *dns; |
1306 | 0 | struct Curl_addrinfo *head = NULL, *tail = NULL; |
1307 | 0 | size_t entry_len; |
1308 | 0 | char address[64]; |
1309 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1310 | 0 | const char *addresses = NULL; |
1311 | 0 | #endif |
1312 | 0 | curl_off_t port = 0; |
1313 | 0 | bool permanent = TRUE; |
1314 | 0 | bool error = TRUE; |
1315 | |
|
1316 | 0 | if(*host == '+') { |
1317 | 0 | host++; |
1318 | 0 | permanent = FALSE; |
1319 | 0 | } |
1320 | 0 | if(!curlx_str_single(&host, '[')) { |
1321 | 0 | if(curlx_str_until(&host, &source, MAX_IPADR_LEN, ']') || |
1322 | 0 | curlx_str_single(&host, ']')) |
1323 | 0 | continue; |
1324 | 0 | } |
1325 | 0 | else { |
1326 | 0 | if(curlx_str_until(&host, &source, 4096, ':')) |
1327 | 0 | continue; |
1328 | 0 | } |
1329 | 0 | if(curlx_str_single(&host, ':') || |
1330 | 0 | curlx_str_number(&host, &port, 0xffff) || |
1331 | 0 | curlx_str_single(&host, ':')) |
1332 | 0 | goto err; |
1333 | | |
1334 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1335 | 0 | addresses = host; |
1336 | 0 | #endif |
1337 | | |
1338 | | /* start the address section */ |
1339 | 0 | while(*host) { |
1340 | 0 | struct Curl_str target; |
1341 | 0 | struct Curl_addrinfo *ai; |
1342 | |
|
1343 | 0 | if(!curlx_str_single(&host, '[')) { |
1344 | 0 | if(curlx_str_until(&host, &target, MAX_IPADR_LEN, ']') || |
1345 | 0 | curlx_str_single(&host, ']')) |
1346 | 0 | goto err; |
1347 | 0 | } |
1348 | 0 | else { |
1349 | 0 | if(curlx_str_until(&host, &target, 4096, ',')) { |
1350 | 0 | if(curlx_str_single(&host, ',')) |
1351 | 0 | goto err; |
1352 | | /* survive nothing but just a comma */ |
1353 | 0 | continue; |
1354 | 0 | } |
1355 | 0 | } |
1356 | | #ifndef USE_IPV6 |
1357 | | if(memchr(target.str, ':', target.len)) { |
1358 | | infof(data, "Ignoring resolve address '%s', missing IPv6 support.", |
1359 | | address); |
1360 | | if(curlx_str_single(&host, ',')) |
1361 | | goto err; |
1362 | | continue; |
1363 | | } |
1364 | | #endif |
1365 | | |
1366 | 0 | if(curlx_strlen(&target) >= sizeof(address)) |
1367 | 0 | goto err; |
1368 | | |
1369 | 0 | memcpy(address, curlx_str(&target), curlx_strlen(&target)); |
1370 | 0 | address[curlx_strlen(&target)] = '\0'; |
1371 | |
|
1372 | 0 | ai = Curl_str2addr(address, (int)port); |
1373 | 0 | if(!ai) { |
1374 | 0 | infof(data, "Resolve address '%s' found illegal", address); |
1375 | 0 | goto err; |
1376 | 0 | } |
1377 | | |
1378 | 0 | if(tail) { |
1379 | 0 | tail->ai_next = ai; |
1380 | 0 | tail = tail->ai_next; |
1381 | 0 | } |
1382 | 0 | else { |
1383 | 0 | head = tail = ai; |
1384 | 0 | } |
1385 | 0 | if(curlx_str_single(&host, ',')) |
1386 | 0 | break; |
1387 | 0 | } |
1388 | | |
1389 | 0 | if(!head) |
1390 | 0 | goto err; |
1391 | | |
1392 | 0 | error = FALSE; |
1393 | 0 | err: |
1394 | 0 | if(error) { |
1395 | 0 | failf(data, "Couldn't parse CURLOPT_RESOLVE entry '%s'", |
1396 | 0 | hostp->data); |
1397 | 0 | Curl_freeaddrinfo(head); |
1398 | 0 | return CURLE_SETOPT_OPTION_SYNTAX; |
1399 | 0 | } |
1400 | | |
1401 | | /* Create an entry id, based upon the hostname and port */ |
1402 | 0 | entry_len = create_dnscache_id(curlx_str(&source), curlx_strlen(&source), |
1403 | 0 | (int)port, |
1404 | 0 | entry_id, sizeof(entry_id)); |
1405 | |
|
1406 | 0 | dnscache_lock(data, dnscache); |
1407 | | |
1408 | | /* See if it is already in our dns cache */ |
1409 | 0 | dns = Curl_hash_pick(&dnscache->entries, entry_id, entry_len + 1); |
1410 | |
|
1411 | 0 | if(dns) { |
1412 | 0 | infof(data, "RESOLVE %.*s:%" CURL_FORMAT_CURL_OFF_T |
1413 | 0 | " - old addresses discarded", (int)curlx_strlen(&source), |
1414 | 0 | curlx_str(&source), port); |
1415 | | /* delete old entry, there are two reasons for this |
1416 | | 1. old entry may have different addresses. |
1417 | | 2. even if entry with correct addresses is already in the cache, |
1418 | | but if it is close to expire, then by the time next http |
1419 | | request is made, it can get expired and pruned because old |
1420 | | entry is not necessarily marked as permanent. |
1421 | | 3. when adding a non-permanent entry, we want it to remove and |
1422 | | replace an existing permanent entry. |
1423 | | 4. when adding a non-permanent entry, we want it to get a "fresh" |
1424 | | timeout that starts _now_. */ |
1425 | |
|
1426 | 0 | Curl_hash_delete(&dnscache->entries, entry_id, entry_len + 1); |
1427 | 0 | } |
1428 | | |
1429 | | /* put this new host in the cache */ |
1430 | 0 | dns = dnscache_add_addr(data, dnscache, head, curlx_str(&source), |
1431 | 0 | curlx_strlen(&source), (int)port, permanent); |
1432 | 0 | if(dns) { |
1433 | | /* release the returned reference; the cache itself will keep the |
1434 | | * entry alive: */ |
1435 | 0 | dns->refcount--; |
1436 | 0 | } |
1437 | |
|
1438 | 0 | dnscache_unlock(data, dnscache); |
1439 | |
|
1440 | 0 | if(!dns) |
1441 | 0 | return CURLE_OUT_OF_MEMORY; |
1442 | | |
1443 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1444 | 0 | infof(data, "Added %.*s:%" CURL_FORMAT_CURL_OFF_T ":%s to DNS cache%s", |
1445 | 0 | (int)curlx_strlen(&source), curlx_str(&source), port, addresses, |
1446 | 0 | permanent ? "" : " (non-permanent)"); |
1447 | 0 | #endif |
1448 | | |
1449 | | /* Wildcard hostname */ |
1450 | 0 | if(curlx_str_casecompare(&source, "*")) { |
1451 | 0 | infof(data, "RESOLVE *:%" CURL_FORMAT_CURL_OFF_T " using wildcard", |
1452 | 0 | port); |
1453 | 0 | data->state.wildcard_resolve = TRUE; |
1454 | 0 | } |
1455 | 0 | } |
1456 | 0 | } |
1457 | 0 | data->state.resolve = NULL; /* dealt with now */ |
1458 | |
|
1459 | 0 | return CURLE_OK; |
1460 | 0 | } |
1461 | | |
1462 | | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
1463 | | static void show_resolve_info(struct Curl_easy *data, |
1464 | | struct Curl_dns_entry *dns) |
1465 | 0 | { |
1466 | 0 | struct Curl_addrinfo *a; |
1467 | 0 | CURLcode result = CURLE_OK; |
1468 | 0 | #ifdef CURLRES_IPV6 |
1469 | 0 | struct dynbuf out[2]; |
1470 | | #else |
1471 | | struct dynbuf out[1]; |
1472 | | #endif |
1473 | 0 | DEBUGASSERT(data); |
1474 | 0 | DEBUGASSERT(dns); |
1475 | |
|
1476 | 0 | if(!data->set.verbose || |
1477 | | /* ignore no name or numerical IP addresses */ |
1478 | 0 | !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname)) |
1479 | 0 | return; |
1480 | | |
1481 | 0 | a = dns->addr; |
1482 | |
|
1483 | 0 | infof(data, "Host %s:%d was resolved.", |
1484 | 0 | (dns->hostname[0] ? dns->hostname : "(none)"), dns->hostport); |
1485 | |
|
1486 | 0 | curlx_dyn_init(&out[0], 1024); |
1487 | 0 | #ifdef CURLRES_IPV6 |
1488 | 0 | curlx_dyn_init(&out[1], 1024); |
1489 | 0 | #endif |
1490 | |
|
1491 | 0 | while(a) { |
1492 | 0 | if( |
1493 | 0 | #ifdef CURLRES_IPV6 |
1494 | 0 | a->ai_family == PF_INET6 || |
1495 | 0 | #endif |
1496 | 0 | a->ai_family == PF_INET) { |
1497 | 0 | char buf[MAX_IPADR_LEN]; |
1498 | 0 | struct dynbuf *d = &out[(a->ai_family != PF_INET)]; |
1499 | 0 | Curl_printable_address(a, buf, sizeof(buf)); |
1500 | 0 | if(curlx_dyn_len(d)) |
1501 | 0 | result = curlx_dyn_addn(d, ", ", 2); |
1502 | 0 | if(!result) |
1503 | 0 | result = curlx_dyn_add(d, buf); |
1504 | 0 | if(result) { |
1505 | 0 | infof(data, "too many IP, cannot show"); |
1506 | 0 | goto fail; |
1507 | 0 | } |
1508 | 0 | } |
1509 | 0 | a = a->ai_next; |
1510 | 0 | } |
1511 | | |
1512 | 0 | #ifdef CURLRES_IPV6 |
1513 | 0 | infof(data, "IPv6: %s", |
1514 | 0 | (curlx_dyn_len(&out[1]) ? curlx_dyn_ptr(&out[1]) : "(none)")); |
1515 | 0 | #endif |
1516 | 0 | infof(data, "IPv4: %s", |
1517 | 0 | (curlx_dyn_len(&out[0]) ? curlx_dyn_ptr(&out[0]) : "(none)")); |
1518 | |
|
1519 | 0 | fail: |
1520 | 0 | curlx_dyn_free(&out[0]); |
1521 | 0 | #ifdef CURLRES_IPV6 |
1522 | 0 | curlx_dyn_free(&out[1]); |
1523 | 0 | #endif |
1524 | 0 | } |
1525 | | #endif |
1526 | | |
1527 | | #ifdef USE_CURL_ASYNC |
1528 | | CURLcode Curl_resolv_check(struct Curl_easy *data, |
1529 | | struct Curl_dns_entry **dns) |
1530 | 0 | { |
1531 | 0 | CURLcode result; |
1532 | | |
1533 | | /* If async resolving is ongoing, this must be set */ |
1534 | 0 | if(!data->state.async.hostname) |
1535 | 0 | return CURLE_FAILED_INIT; |
1536 | | |
1537 | | /* check if we have the name resolved by now (from someone else) */ |
1538 | 0 | *dns = Curl_dnscache_get(data, data->state.async.hostname, |
1539 | 0 | data->state.async.port, |
1540 | 0 | data->state.async.ip_version); |
1541 | 0 | if(*dns) { |
1542 | | /* Tell a possibly async resolver we no longer need the results. */ |
1543 | 0 | infof(data, "Hostname '%s' was found in DNS cache", |
1544 | 0 | data->state.async.hostname); |
1545 | 0 | Curl_async_shutdown(data); |
1546 | 0 | data->state.async.dns = *dns; |
1547 | 0 | data->state.async.done = TRUE; |
1548 | 0 | return CURLE_OK; |
1549 | 0 | } |
1550 | | |
1551 | 0 | #ifndef CURL_DISABLE_DOH |
1552 | 0 | if(data->conn->bits.doh) { |
1553 | 0 | result = Curl_doh_is_resolved(data, dns); |
1554 | 0 | if(result) |
1555 | 0 | Curl_resolver_error(data, NULL); |
1556 | 0 | } |
1557 | 0 | else |
1558 | 0 | #endif |
1559 | 0 | result = Curl_async_is_resolved(data, dns); |
1560 | 0 | if(*dns) |
1561 | 0 | show_resolve_info(data, *dns); |
1562 | 0 | if(result) |
1563 | 0 | store_negative_resolve(data, data->state.async.hostname, |
1564 | 0 | data->state.async.port); |
1565 | 0 | return result; |
1566 | 0 | } |
1567 | | #endif |
1568 | | |
1569 | | CURLcode Curl_resolv_pollset(struct Curl_easy *data, |
1570 | | struct easy_pollset *ps) |
1571 | 0 | { |
1572 | 0 | #ifdef CURLRES_ASYNCH |
1573 | 0 | #ifndef CURL_DISABLE_DOH |
1574 | 0 | if(data->conn->bits.doh) |
1575 | | /* nothing to wait for during DoH resolve, those handles have their own |
1576 | | sockets */ |
1577 | 0 | return CURLE_OK; |
1578 | 0 | #endif |
1579 | 0 | return Curl_async_pollset(data, ps); |
1580 | | #else |
1581 | | (void)data; |
1582 | | (void)ps; |
1583 | | return CURLE_OK; |
1584 | | #endif |
1585 | 0 | } |
1586 | | |
1587 | | /* Call this function after Curl_connect() has returned async=TRUE and |
1588 | | then a successful name resolve has been received. |
1589 | | |
1590 | | Note: this function disconnects and frees the conn data in case of |
1591 | | resolve failure */ |
1592 | | CURLcode Curl_once_resolved(struct Curl_easy *data, |
1593 | | struct Curl_dns_entry *dns, |
1594 | | bool *protocol_done) |
1595 | 0 | { |
1596 | 0 | CURLcode result; |
1597 | 0 | struct connectdata *conn = data->conn; |
1598 | |
|
1599 | 0 | #ifdef USE_CURL_ASYNC |
1600 | 0 | if(data->state.async.dns) { |
1601 | 0 | DEBUGASSERT(data->state.async.dns == dns); |
1602 | 0 | data->state.async.dns = NULL; |
1603 | 0 | } |
1604 | 0 | #endif |
1605 | |
|
1606 | 0 | result = Curl_setup_conn(data, dns, protocol_done); |
1607 | |
|
1608 | 0 | if(result) { |
1609 | 0 | Curl_detach_connection(data); |
1610 | 0 | Curl_conn_terminate(data, conn, TRUE); |
1611 | 0 | } |
1612 | 0 | return result; |
1613 | 0 | } |
1614 | | |
1615 | | /* |
1616 | | * Curl_resolver_error() calls failf() with the appropriate message after a |
1617 | | * resolve error |
1618 | | */ |
1619 | | |
1620 | | #ifdef USE_CURL_ASYNC |
1621 | | CURLcode Curl_resolver_error(struct Curl_easy *data, const char *detail) |
1622 | 0 | { |
1623 | 0 | struct connectdata *conn = data->conn; |
1624 | 0 | const char *host_or_proxy = "host"; |
1625 | 0 | const char *name = conn->host.dispname; |
1626 | 0 | CURLcode result = CURLE_COULDNT_RESOLVE_HOST; |
1627 | |
|
1628 | 0 | #ifndef CURL_DISABLE_PROXY |
1629 | 0 | if(conn->bits.proxy) { |
1630 | 0 | host_or_proxy = "proxy"; |
1631 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
1632 | 0 | name = conn->socks_proxy.host.name ? conn->socks_proxy.host.dispname : |
1633 | 0 | conn->http_proxy.host.dispname; |
1634 | 0 | } |
1635 | 0 | #endif |
1636 | |
|
1637 | 0 | failf(data, "Could not resolve %s: %s%s%s%s", host_or_proxy, name, |
1638 | 0 | detail ? " (" : "", detail ? detail : "", detail ? ")" : ""); |
1639 | 0 | return result; |
1640 | 0 | } |
1641 | | #endif /* USE_CURL_ASYNC */ |
1642 | | |
1643 | | #ifdef DEBUGBUILD |
1644 | | #include "curlx/wait.h" |
1645 | | |
1646 | | void Curl_resolve_test_delay(void) |
1647 | | { |
1648 | | const char *p = getenv("CURL_DNS_DELAY_MS"); |
1649 | | if(p) { |
1650 | | curl_off_t l; |
1651 | | if(!curlx_str_number(&p, &l, TIME_T_MAX) && l) { |
1652 | | curlx_wait_ms((timediff_t)l); |
1653 | | } |
1654 | | } |
1655 | | } |
1656 | | #endif |