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