/src/CMake/Utilities/cmcurl/lib/hostip.c
Line | Count | Source |
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 | | #include "curl_setup.h" |
25 | | |
26 | | #ifdef HAVE_NETINET_IN_H |
27 | | #include <netinet/in.h> |
28 | | #endif |
29 | | #ifdef HAVE_NETINET_IN6_H |
30 | | #include <netinet/in6.h> |
31 | | #endif |
32 | | #ifdef HAVE_NETDB_H |
33 | | #include <netdb.h> |
34 | | #endif |
35 | | #ifdef HAVE_ARPA_INET_H |
36 | | #include <arpa/inet.h> |
37 | | #endif |
38 | | #ifdef __VMS |
39 | | #include <in.h> |
40 | | #include <inet.h> |
41 | | #endif |
42 | | |
43 | | #include <setjmp.h> /* for sigjmp_buf, sigsetjmp() */ |
44 | | #include <signal.h> |
45 | | |
46 | | #include "urldata.h" |
47 | | #include "curl_addrinfo.h" |
48 | | #include "curl_trc.h" |
49 | | #include "dnscache.h" |
50 | | #include "hostip.h" |
51 | | #include "httpsrr.h" |
52 | | #include "url.h" |
53 | | #include "multiif.h" |
54 | | #include "progress.h" |
55 | | #include "doh.h" |
56 | | #include "select.h" |
57 | | #include "strcase.h" |
58 | | #include "easy_lock.h" |
59 | | #include "curlx/inet_ntop.h" |
60 | | #include "curlx/inet_pton.h" |
61 | | #include "curlx/strcopy.h" |
62 | | #include "curlx/strparse.h" |
63 | | |
64 | | #if defined(CURLRES_SYNCH) && \ |
65 | | defined(HAVE_ALARM) && \ |
66 | | defined(SIGALRM) && \ |
67 | | defined(HAVE_SIGSETJMP) && \ |
68 | | defined(GLOBAL_INIT_IS_THREADSAFE) |
69 | | /* alarm-based timeouts can only be used with all the dependencies satisfied */ |
70 | | #define USE_ALARM_TIMEOUT |
71 | | #endif |
72 | | |
73 | | #define RESOLV_FAIL(for_proxy) \ |
74 | 0 | ((for_proxy) ? CURLE_COULDNT_RESOLVE_PROXY : CURLE_COULDNT_RESOLVE_HOST) |
75 | | |
76 | | #define IS_RESOLV_FAIL(result) \ |
77 | 0 | (((result) == CURLE_COULDNT_RESOLVE_HOST) || \ |
78 | 0 | ((result) == CURLE_COULDNT_RESOLVE_PROXY)) |
79 | | /* |
80 | | * ipv6works() returns TRUE if IPv6 seems to work. |
81 | | */ |
82 | | #ifdef USE_IPV6 |
83 | | static bool ipv6works(struct Curl_easy *data); |
84 | | #else |
85 | | #define ipv6works(x) FALSE |
86 | | #endif |
87 | | |
88 | | /* |
89 | | * hostip.c explained |
90 | | * ================== |
91 | | * |
92 | | * The main COMPILE-TIME DEFINES to keep in mind when reading the host*.c |
93 | | * source file are these: |
94 | | * |
95 | | * CURLRES_IPV6 - this host has getaddrinfo() and family, and thus we use |
96 | | * that. The host may not be able to resolve IPv6, but we do not really have to |
97 | | * take that into account. Hosts that are not IPv6-enabled have CURLRES_IPV4 |
98 | | * defined. |
99 | | * |
100 | | * USE_RESOLV_ARES - is defined if libcurl is built to use c-ares for |
101 | | * asynchronous name resolves. This can be Windows or *nix. |
102 | | * |
103 | | * USE_RESOLV_THREADED - is defined if libcurl is built to run under (native) |
104 | | * Windows, and then the name resolve will be done in a new thread, and the |
105 | | * supported API will be the same as for ares-builds. |
106 | | * |
107 | | * If any of the two previous are defined, CURLRES_ASYNCH is defined too. If |
108 | | * libcurl is not built to use an asynchronous resolver, CURLRES_SYNCH is |
109 | | * defined. |
110 | | * |
111 | | * The host*.c sources files are split up like this: |
112 | | * |
113 | | * hostip.c - method-independent resolver functions and utility functions |
114 | | * hostip4.c - IPv4 specific functions |
115 | | * hostip6.c - IPv6 specific functions |
116 | | * asyn.h - common functions for all async resolvers |
117 | | * The two asynchronous name resolver backends are implemented in: |
118 | | * asyn-ares.c - async resolver using c-ares |
119 | | * asyn-thread.c - async resolver using POSIX threads |
120 | | * |
121 | | * The hostip.h is the united header file for all this. It defines the |
122 | | * CURLRES_* defines based on the config*.h and curl_setup.h defines. |
123 | | */ |
124 | | |
125 | | uint8_t Curl_resolv_dns_queries(struct Curl_easy *data, uint8_t ip_version) |
126 | 0 | { |
127 | 0 | (void)data; |
128 | 0 | switch(ip_version) { |
129 | 0 | case CURL_IPRESOLVE_V6: |
130 | 0 | return CURL_DNSQ_AAAA; |
131 | 0 | case CURL_IPRESOLVE_V4: |
132 | 0 | return CURL_DNSQ_A; |
133 | 0 | default: |
134 | 0 | if(ipv6works(data)) |
135 | 0 | return (CURL_DNSQ_A | CURL_DNSQ_AAAA); |
136 | 0 | else |
137 | 0 | return CURL_DNSQ_A; |
138 | 0 | } |
139 | 0 | } |
140 | | |
141 | | #ifdef CURLVERBOSE |
142 | | const char *Curl_resolv_query_str(uint8_t dns_queries) |
143 | 0 | { |
144 | 0 | switch(dns_queries) { |
145 | 0 | case (CURL_DNSQ_A | CURL_DNSQ_AAAA | CURL_DNSQ_HTTPS): |
146 | 0 | return "A+AAAA+HTTPS"; |
147 | 0 | case (CURL_DNSQ_A | CURL_DNSQ_AAAA): |
148 | 0 | return "A+AAAA"; |
149 | 0 | case (CURL_DNSQ_AAAA | CURL_DNSQ_HTTPS): |
150 | 0 | return "AAAA+HTTPS"; |
151 | 0 | case (CURL_DNSQ_AAAA): |
152 | 0 | return "AAAA"; |
153 | 0 | case (CURL_DNSQ_A | CURL_DNSQ_HTTPS): |
154 | 0 | return "A+HTTPS"; |
155 | 0 | case (CURL_DNSQ_A): |
156 | 0 | return "A"; |
157 | 0 | case (CURL_DNSQ_HTTPS): |
158 | 0 | return "HTTPS"; |
159 | 0 | case 0: |
160 | 0 | return "-"; |
161 | 0 | default: |
162 | 0 | DEBUGASSERT(0); |
163 | 0 | return "???"; |
164 | 0 | } |
165 | 0 | } |
166 | | #endif |
167 | | |
168 | | /* |
169 | | * Curl_printable_address() stores a printable version of the 1st address |
170 | | * given in the 'ai' argument. The result will be stored in the buf that is |
171 | | * bufsize bytes big. |
172 | | * |
173 | | * If the conversion fails, the target buffer is empty. |
174 | | */ |
175 | | void Curl_printable_address(const struct Curl_addrinfo *ai, char *buf, |
176 | | size_t bufsize) |
177 | 0 | { |
178 | 0 | DEBUGASSERT(bufsize); |
179 | 0 | buf[0] = 0; |
180 | |
|
181 | 0 | switch(ai->ai_family) { |
182 | 0 | case AF_INET: { |
183 | 0 | const struct sockaddr_in *sa4 = (const void *)ai->ai_addr; |
184 | 0 | const struct in_addr *ipaddr4 = &sa4->sin_addr; |
185 | 0 | (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr4, buf, bufsize); |
186 | 0 | break; |
187 | 0 | } |
188 | 0 | #ifdef USE_IPV6 |
189 | 0 | case AF_INET6: { |
190 | 0 | const struct sockaddr_in6 *sa6 = (const void *)ai->ai_addr; |
191 | 0 | const struct in6_addr *ipaddr6 = &sa6->sin6_addr; |
192 | 0 | (void)curlx_inet_ntop(ai->ai_family, (const void *)ipaddr6, buf, bufsize); |
193 | 0 | break; |
194 | 0 | } |
195 | 0 | #endif |
196 | 0 | default: |
197 | 0 | break; |
198 | 0 | } |
199 | 0 | } |
200 | | |
201 | | #ifdef USE_ALARM_TIMEOUT |
202 | | /* Beware this is a global and unique instance. This is used to store the |
203 | | return address that we can jump back to from inside a signal handler. This |
204 | | is not thread-safe stuff. */ |
205 | | static sigjmp_buf curl_jmpenv; |
206 | | static curl_simple_lock curl_jmpenv_lock = CURL_SIMPLE_LOCK_INIT; |
207 | | #endif |
208 | | |
209 | | #ifdef USE_IPV6 |
210 | | /* return a static IPv6 ::1 for the name */ |
211 | | static struct Curl_addrinfo *get_localhost6(uint16_t port, const char *name) |
212 | 0 | { |
213 | 0 | struct Curl_addrinfo *ca; |
214 | 0 | const size_t ss_size = sizeof(struct sockaddr_in6); |
215 | 0 | const size_t hostlen = strlen(name); |
216 | 0 | struct sockaddr_in6 sa6; |
217 | 0 | unsigned char ipv6[16]; |
218 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
219 | 0 | ca = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1); |
220 | 0 | if(!ca) |
221 | 0 | return NULL; |
222 | | |
223 | 0 | memset(&sa6, 0, sizeof(sa6)); |
224 | 0 | sa6.sin6_family = AF_INET6; |
225 | 0 | sa6.sin6_port = htons(port16); |
226 | |
|
227 | 0 | (void)curlx_inet_pton(AF_INET6, "::1", ipv6); |
228 | 0 | memcpy(&sa6.sin6_addr, ipv6, sizeof(ipv6)); |
229 | |
|
230 | 0 | ca->ai_flags = 0; |
231 | 0 | ca->ai_family = AF_INET6; |
232 | 0 | ca->ai_socktype = SOCK_STREAM; |
233 | 0 | ca->ai_protocol = IPPROTO_TCP; |
234 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
235 | 0 | ca->ai_next = NULL; |
236 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
237 | 0 | memcpy(ca->ai_addr, &sa6, ss_size); |
238 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
239 | 0 | curlx_strcopy(ca->ai_canonname, hostlen + 1, name, hostlen); |
240 | 0 | return ca; |
241 | 0 | } |
242 | | #else |
243 | | #define get_localhost6(x, y) NULL |
244 | | #endif |
245 | | |
246 | | /* return a static IPv4 127.0.0.1 for the given name */ |
247 | | static struct Curl_addrinfo *get_localhost(uint16_t port, const char *name) |
248 | 0 | { |
249 | 0 | struct Curl_addrinfo *ca; |
250 | 0 | struct Curl_addrinfo *ca6; |
251 | 0 | const size_t ss_size = sizeof(struct sockaddr_in); |
252 | 0 | const size_t hostlen = strlen(name); |
253 | 0 | struct sockaddr_in sa; |
254 | 0 | unsigned int ipv4; |
255 | 0 | unsigned short port16 = (unsigned short)(port & 0xffff); |
256 | | |
257 | | /* memset to clear the sa.sin_zero field */ |
258 | 0 | memset(&sa, 0, sizeof(sa)); |
259 | 0 | sa.sin_family = AF_INET; |
260 | 0 | sa.sin_port = htons(port16); |
261 | 0 | if(curlx_inet_pton(AF_INET, "127.0.0.1", (char *)&ipv4) < 1) |
262 | 0 | return NULL; |
263 | 0 | memcpy(&sa.sin_addr, &ipv4, sizeof(ipv4)); |
264 | |
|
265 | 0 | ca = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen + 1); |
266 | 0 | if(!ca) |
267 | 0 | return NULL; |
268 | 0 | ca->ai_flags = 0; |
269 | 0 | ca->ai_family = AF_INET; |
270 | 0 | ca->ai_socktype = SOCK_STREAM; |
271 | 0 | ca->ai_protocol = IPPROTO_TCP; |
272 | 0 | ca->ai_addrlen = (curl_socklen_t)ss_size; |
273 | 0 | ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
274 | 0 | memcpy(ca->ai_addr, &sa, ss_size); |
275 | 0 | ca->ai_canonname = (char *)ca->ai_addr + ss_size; |
276 | 0 | curlx_strcopy(ca->ai_canonname, hostlen + 1, name, hostlen); |
277 | |
|
278 | 0 | ca6 = get_localhost6(port, name); |
279 | 0 | if(!ca6) |
280 | 0 | return ca; |
281 | 0 | ca6->ai_next = ca; |
282 | 0 | return ca6; |
283 | 0 | } |
284 | | |
285 | | #ifdef USE_IPV6 |
286 | | /* the nature of most systems is that IPv6 status does not come and go during a |
287 | | program's lifetime so we only probe the first time and then we have the |
288 | | info kept for fast reuse */ |
289 | | CURLcode Curl_probeipv6(struct Curl_multi *multi) |
290 | 0 | { |
291 | | /* probe to see if we have a working IPv6 stack */ |
292 | 0 | curl_socket_t s = CURL_SOCKET(PF_INET6, SOCK_DGRAM, 0); |
293 | 0 | multi->ipv6_works = FALSE; |
294 | 0 | if(s == CURL_SOCKET_BAD) { |
295 | 0 | if(SOCKERRNO == SOCKENOMEM) |
296 | 0 | return CURLE_OUT_OF_MEMORY; |
297 | 0 | } |
298 | 0 | else { |
299 | 0 | multi->ipv6_works = TRUE; |
300 | 0 | sclose(s); |
301 | 0 | } |
302 | 0 | return CURLE_OK; |
303 | 0 | } |
304 | | |
305 | | /* |
306 | | * ipv6works() returns TRUE if IPv6 seems to work. |
307 | | */ |
308 | | static bool ipv6works(struct Curl_easy *data) |
309 | 0 | { |
310 | 0 | DEBUGASSERT(data); |
311 | 0 | DEBUGASSERT(data->multi); |
312 | 0 | return data ? data->multi->ipv6_works : FALSE; |
313 | 0 | } |
314 | | #endif /* USE_IPV6 */ |
315 | | |
316 | | /* |
317 | | * Curl_host_is_ipnum() returns TRUE if the given string is a numerical IPv4 |
318 | | * (or IPv6 if supported) address. |
319 | | */ |
320 | | bool Curl_host_is_ipnum(const char *hostname) |
321 | 0 | { |
322 | 0 | struct in_addr in; |
323 | 0 | #ifdef USE_IPV6 |
324 | 0 | struct in6_addr in6; |
325 | 0 | #endif |
326 | 0 | if(curlx_inet_pton(AF_INET, hostname, &in) > 0 |
327 | 0 | #ifdef USE_IPV6 |
328 | 0 | || curlx_inet_pton(AF_INET6, hostname, &in6) > 0 |
329 | 0 | #endif |
330 | 0 | ) |
331 | 0 | return TRUE; |
332 | 0 | return FALSE; |
333 | 0 | } |
334 | | |
335 | | /* return TRUE if 'part' is a case insensitive tail of 'full' */ |
336 | | static bool tailmatch(const char *full, size_t flen, |
337 | | const char *part, size_t plen) |
338 | 0 | { |
339 | 0 | if(plen > flen) |
340 | 0 | return FALSE; |
341 | 0 | return curl_strnequal(part, &full[flen - plen], plen); |
342 | 0 | } |
343 | | |
344 | | static CURLcode hostip_resolv_failed(struct Curl_easy *data, |
345 | | const char *hostname, |
346 | | bool for_proxy) |
347 | 0 | { |
348 | 0 | failf(data, "Could not resolve %s: %s", |
349 | 0 | for_proxy ? "proxy" : "host", hostname); |
350 | 0 | return RESOLV_FAIL(for_proxy); |
351 | 0 | } |
352 | | |
353 | | static bool can_resolve_dns_queries(struct Curl_easy *data, |
354 | | uint8_t dns_queries) |
355 | 0 | { |
356 | 0 | (void)data; |
357 | 0 | if((CURL_DNSQ_IP(dns_queries) == CURL_DNSQ_AAAA) && !ipv6works(data)) |
358 | 0 | return FALSE; |
359 | 0 | return TRUE; |
360 | 0 | } |
361 | | |
362 | | CURLcode Curl_resolv_announce_start(struct Curl_easy *data, |
363 | | void *resolver) |
364 | 0 | { |
365 | 0 | if(data->set.resolver_start) { |
366 | 0 | int rc; |
367 | |
|
368 | 0 | CURL_TRC_DNS(data, "announcing resolve to application"); |
369 | 0 | Curl_set_in_callback(data, TRUE); |
370 | 0 | rc = data->set.resolver_start(resolver, NULL, |
371 | 0 | data->set.resolver_start_client); |
372 | 0 | Curl_set_in_callback(data, FALSE); |
373 | 0 | if(rc) { |
374 | 0 | CURL_TRC_DNS(data, "application aborted resolve"); |
375 | 0 | return CURLE_ABORTED_BY_CALLBACK; |
376 | 0 | } |
377 | 0 | } |
378 | 0 | return CURLE_OK; |
379 | 0 | } |
380 | | |
381 | | #ifdef USE_CURL_ASYNC |
382 | | |
383 | | static struct Curl_resolv_async *hostip_async_new(struct Curl_easy *data, |
384 | | uint8_t dns_queries, |
385 | | const char *hostname, |
386 | | uint16_t port, |
387 | | uint8_t transport, |
388 | | bool for_proxy, |
389 | | timediff_t timeout_ms) |
390 | 0 | { |
391 | 0 | struct Curl_resolv_async *async; |
392 | 0 | size_t hostlen = strlen(hostname); |
393 | |
|
394 | 0 | if(!data->multi) { |
395 | 0 | DEBUGASSERT(0); |
396 | 0 | return NULL; |
397 | 0 | } |
398 | | |
399 | | /* struct size already includes the NUL for hostname */ |
400 | 0 | async = curlx_calloc(1, sizeof(*async) + hostlen); |
401 | 0 | if(!async) |
402 | 0 | return NULL; |
403 | | |
404 | | /* Give every async resolve operation a "unique" id. This may |
405 | | * wrap around after a long time, making collisions highly unlikely. |
406 | | * As we keep the async structs at the easy handle, chances of |
407 | | * easy `mid plus resolv->id` colliding should be astronomical. |
408 | | * `resolv_id == 0` is never used. */ |
409 | 0 | if(data->multi->last_resolv_id == UINT32_MAX) |
410 | 0 | data->multi->last_resolv_id = 1; /* wrap around */ |
411 | 0 | else |
412 | 0 | data->multi->last_resolv_id++; |
413 | 0 | async->id = data->multi->last_resolv_id; |
414 | 0 | async->dns_queries = dns_queries; |
415 | 0 | async->port = port; |
416 | 0 | async->transport = transport; |
417 | 0 | async->for_proxy = for_proxy; |
418 | 0 | async->start = *Curl_pgrs_now(data); |
419 | 0 | async->timeout_ms = timeout_ms; |
420 | 0 | if(hostlen) { |
421 | 0 | memcpy(async->hostname, hostname, hostlen); |
422 | 0 | async->is_ipaddr = Curl_is_ipaddr(async->hostname); |
423 | 0 | if(async->is_ipaddr) |
424 | 0 | async->is_ipv4addr = Curl_is_ipv4addr(async->hostname); |
425 | 0 | } |
426 | |
|
427 | 0 | return async; |
428 | 0 | } |
429 | | |
430 | | static CURLcode hostip_resolv_take_result(struct Curl_easy *data, |
431 | | struct Curl_resolv_async *async, |
432 | | struct Curl_dns_entry **pdns) |
433 | 0 | { |
434 | 0 | CURLcode result; |
435 | | |
436 | | /* If async resolving is ongoing, this must be set */ |
437 | 0 | if(!async) |
438 | 0 | return CURLE_FAILED_INIT; |
439 | | |
440 | 0 | #ifndef CURL_DISABLE_DOH |
441 | 0 | if(async->doh) |
442 | 0 | result = Curl_doh_take_result(data, async, pdns); |
443 | 0 | else |
444 | 0 | #endif |
445 | 0 | result = Curl_async_take_result(data, async, pdns); |
446 | |
|
447 | 0 | if(result == CURLE_AGAIN) { |
448 | 0 | CURL_TRC_DNS(data, "resolve incomplete, queries=%s, responses=%s, " |
449 | 0 | "ongoing=%d for %s:%d", |
450 | 0 | Curl_resolv_query_str(async->dns_queries), |
451 | 0 | Curl_resolv_query_str(async->dns_responses), |
452 | 0 | async->queries_ongoing, async->hostname, async->port); |
453 | 0 | result = CURLE_OK; |
454 | 0 | } |
455 | 0 | else if(result) { |
456 | 0 | result = Curl_async_failed(data, async, NULL); |
457 | 0 | } |
458 | 0 | else { |
459 | 0 | CURL_TRC_DNS(data, "resolve complete for %s:%u", |
460 | 0 | async->hostname, async->port); |
461 | 0 | DEBUGASSERT(*pdns); |
462 | 0 | } |
463 | |
|
464 | 0 | return result; |
465 | 0 | } |
466 | | |
467 | | timediff_t Curl_resolv_elapsed_ms(struct Curl_easy *data, |
468 | | uint32_t resolv_id) |
469 | 0 | { |
470 | 0 | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
471 | 0 | if(!async) |
472 | 0 | return CURL_TIMEOUT_RESOLVE_MS; |
473 | 0 | return curlx_ptimediff_ms(Curl_pgrs_now(data), &async->start); |
474 | 0 | } |
475 | | |
476 | | bool Curl_resolv_has_answers(struct Curl_easy *data, |
477 | | uint32_t resolv_id, uint8_t dns_queries) |
478 | 0 | { |
479 | 0 | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
480 | 0 | uint8_t check_queries; |
481 | | /* a no longer existing/running resolve has all answers. */ |
482 | 0 | if(!async || async->done) |
483 | 0 | return TRUE; |
484 | | /* Relevant are only queries undertaken. Others are considered answered. */ |
485 | 0 | check_queries = (dns_queries & async->dns_queries); |
486 | 0 | if((check_queries & async->dns_responses) != check_queries) { |
487 | 0 | return FALSE; |
488 | 0 | } |
489 | 0 | return TRUE; |
490 | 0 | } |
491 | | |
492 | | const struct Curl_addrinfo *Curl_resolv_get_ai(struct Curl_easy *data, |
493 | | uint32_t resolv_id, |
494 | | int ai_family, |
495 | | unsigned int index) |
496 | 0 | { |
497 | 0 | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
498 | 0 | (void)index; |
499 | 0 | if(!async) |
500 | 0 | return NULL; |
501 | 0 | if((ai_family == AF_INET) && !(async->dns_queries & CURL_DNSQ_A)) |
502 | 0 | return NULL; |
503 | 0 | #ifdef USE_IPV6 |
504 | 0 | if((ai_family == AF_INET6) && !(async->dns_queries & CURL_DNSQ_AAAA)) |
505 | 0 | return NULL; |
506 | 0 | #endif |
507 | 0 | return Curl_async_get_ai(data, async, ai_family, index); |
508 | 0 | } |
509 | | |
510 | | #ifdef USE_HTTPSRR |
511 | | const struct Curl_https_rrinfo * |
512 | | Curl_resolv_get_https(struct Curl_easy *data, uint32_t resolv_id) |
513 | | { |
514 | | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
515 | | if(!async) |
516 | | return NULL; |
517 | | return Curl_async_get_https(data, async); |
518 | | } |
519 | | |
520 | | bool Curl_resolv_knows_https(struct Curl_easy *data, uint32_t resolv_id) |
521 | | { |
522 | | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
523 | | if(!async) |
524 | | return TRUE; |
525 | | return Curl_async_knows_https(data, async); |
526 | | } |
527 | | #endif /* USE_HTTPSRR */ |
528 | | |
529 | | #endif /* USE_CURL_ASYNC */ |
530 | | |
531 | | static CURLcode hostip_resolv_start(struct Curl_easy *data, |
532 | | uint8_t dns_queries, |
533 | | const char *hostname, |
534 | | uint16_t port, |
535 | | uint8_t transport, |
536 | | bool for_proxy, |
537 | | timediff_t timeout_ms, |
538 | | bool allowDOH, |
539 | | uint32_t *presolv_id, |
540 | | struct Curl_dns_entry **pdns) |
541 | 0 | { |
542 | 0 | #ifdef USE_CURL_ASYNC |
543 | 0 | struct Curl_resolv_async *async = NULL; |
544 | 0 | #endif |
545 | 0 | struct Curl_addrinfo *addr = NULL; |
546 | 0 | size_t hostname_len; |
547 | 0 | CURLcode result = CURLE_OK; |
548 | |
|
549 | 0 | (void)timeout_ms; /* not in all ifdefs */ |
550 | 0 | *presolv_id = 0; |
551 | 0 | *pdns = NULL; |
552 | | |
553 | | /* Check for "known" things to resolve ourselves. */ |
554 | 0 | #ifndef USE_RESOLVE_ON_IPS |
555 | 0 | if(Curl_is_ipaddr(hostname)) { |
556 | | /* test655 verifies that the announce is done, even though there |
557 | | * is no real resolving. So, keep doing this. */ |
558 | 0 | result = Curl_resolv_announce_start(data, NULL); |
559 | 0 | if(result) |
560 | 0 | goto out; |
561 | | /* shortcut literal IP addresses, if we are not told to resolve them. */ |
562 | 0 | result = Curl_str2addr(hostname, port, &addr); |
563 | 0 | goto out; |
564 | 0 | } |
565 | 0 | #endif |
566 | | |
567 | 0 | hostname_len = strlen(hostname); |
568 | 0 | if(curl_strequal(hostname, "localhost") || |
569 | 0 | curl_strequal(hostname, "localhost.") || |
570 | 0 | tailmatch(hostname, hostname_len, STRCONST(".localhost")) || |
571 | 0 | tailmatch(hostname, hostname_len, STRCONST(".localhost."))) { |
572 | 0 | result = Curl_resolv_announce_start(data, NULL); |
573 | 0 | if(result) |
574 | 0 | goto out; |
575 | 0 | addr = get_localhost(port, hostname); |
576 | 0 | if(!addr) |
577 | 0 | result = CURLE_OUT_OF_MEMORY; |
578 | 0 | goto out; |
579 | 0 | } |
580 | | |
581 | 0 | #ifndef CURL_DISABLE_DOH |
582 | 0 | if(!Curl_is_ipaddr(hostname) && allowDOH && data->set.doh) { |
583 | 0 | result = Curl_resolv_announce_start(data, NULL); |
584 | 0 | if(result) |
585 | 0 | goto out; |
586 | 0 | if(!async) { |
587 | 0 | async = hostip_async_new(data, dns_queries, hostname, port, |
588 | 0 | transport, for_proxy, timeout_ms); |
589 | 0 | if(!async) { |
590 | 0 | result = CURLE_OUT_OF_MEMORY; |
591 | 0 | goto out; |
592 | 0 | } |
593 | 0 | } |
594 | 0 | result = Curl_doh(data, async); |
595 | 0 | goto out; |
596 | 0 | } |
597 | | #else |
598 | | (void)allowDOH; |
599 | | #endif |
600 | | |
601 | | /* Can we provide the requested IP specifics in resolving? */ |
602 | 0 | if(!can_resolve_dns_queries(data, dns_queries)) { |
603 | 0 | result = RESOLV_FAIL(for_proxy); |
604 | 0 | goto out; |
605 | 0 | } |
606 | | |
607 | | #ifdef CURLRES_ASYNCH |
608 | | (void)addr; |
609 | | if(!async) { |
610 | | async = hostip_async_new(data, dns_queries, hostname, port, |
611 | | transport, for_proxy, timeout_ms); |
612 | | if(!async) { |
613 | | result = CURLE_OUT_OF_MEMORY; |
614 | | goto out; |
615 | | } |
616 | | } |
617 | | result = Curl_async_getaddrinfo(data, async); |
618 | | if(result == CURLE_AGAIN) { |
619 | | /* the answer might be there already. Check. */ |
620 | | CURLcode r2 = hostip_resolv_take_result(data, async, pdns); |
621 | | if(r2) |
622 | | result = r2; |
623 | | else if(*pdns) |
624 | | result = CURLE_OK; |
625 | | } |
626 | | #else |
627 | 0 | result = Curl_resolv_announce_start(data, NULL); |
628 | 0 | if(result) |
629 | 0 | goto out; |
630 | 0 | addr = Curl_sync_getaddrinfo(data, dns_queries, hostname, port, transport); |
631 | 0 | if(!addr) |
632 | 0 | result = RESOLV_FAIL(for_proxy); |
633 | 0 | #endif |
634 | |
|
635 | 0 | out: |
636 | 0 | if(!result) { |
637 | 0 | if(addr) { |
638 | | /* we got a response, create a dns entry, add to cache, return */ |
639 | 0 | DEBUGASSERT(!*pdns); |
640 | 0 | *pdns = Curl_dnscache_mk_entry(data, dns_queries, &addr, hostname, port); |
641 | 0 | if(!*pdns) |
642 | 0 | result = CURLE_OUT_OF_MEMORY; |
643 | 0 | } |
644 | 0 | else if(!*pdns) |
645 | 0 | result = CURLE_AGAIN; |
646 | 0 | } |
647 | 0 | else if(*pdns) |
648 | 0 | Curl_dns_entry_unlink(data, pdns); |
649 | 0 | else if(addr) |
650 | 0 | Curl_freeaddrinfo(addr); |
651 | |
|
652 | 0 | #ifdef USE_CURL_ASYNC |
653 | 0 | if(async) { |
654 | 0 | if(result == CURLE_AGAIN) { /* still need it, link, return id. */ |
655 | 0 | *presolv_id = async->id; |
656 | 0 | async->next = data->state.async; |
657 | 0 | data->state.async = async; |
658 | 0 | } |
659 | 0 | else { |
660 | 0 | Curl_async_destroy(data, async); |
661 | 0 | } |
662 | 0 | } |
663 | 0 | #endif |
664 | 0 | return result; |
665 | 0 | } |
666 | | |
667 | | static CURLcode hostip_resolv(struct Curl_easy *data, |
668 | | uint8_t dns_queries, |
669 | | const char *hostname, |
670 | | uint16_t port, |
671 | | uint8_t transport, |
672 | | bool for_proxy, |
673 | | timediff_t timeout_ms, |
674 | | bool allowDOH, |
675 | | uint32_t *presolv_id, |
676 | | struct Curl_dns_entry **pdns) |
677 | 0 | { |
678 | 0 | size_t hostname_len; |
679 | 0 | CURLcode result = RESOLV_FAIL(for_proxy); |
680 | 0 | bool cache_dns = FALSE; |
681 | |
|
682 | 0 | (void)timeout_ms; /* not used in all ifdefs */ |
683 | 0 | *presolv_id = 0; |
684 | 0 | *pdns = NULL; |
685 | |
|
686 | | #ifdef CURL_DISABLE_DOH |
687 | | (void)allowDOH; |
688 | | #endif |
689 | | |
690 | | /* We should intentionally error and not resolve .onion TLDs */ |
691 | 0 | hostname_len = strlen(hostname); |
692 | 0 | DEBUGASSERT(hostname_len); |
693 | 0 | if(hostname_len >= 7 && |
694 | 0 | (curl_strequal(&hostname[hostname_len - 6], ".onion") || |
695 | 0 | curl_strequal(&hostname[hostname_len - 7], ".onion."))) { |
696 | 0 | failf(data, "Not resolving .onion address (RFC 7686)"); |
697 | 0 | goto out; |
698 | 0 | } |
699 | | |
700 | | #ifdef DEBUGBUILD |
701 | | CURL_TRC_DNS(data, "hostip_resolv(%s:%u, queries=%s)", |
702 | | hostname, port, Curl_resolv_query_str(dns_queries)); |
703 | | if((CURL_DNSQ_IP(dns_queries) == CURL_DNSQ_AAAA) && |
704 | | getenv("CURL_DBG_RESOLV_FAIL_IPV6")) { |
705 | | infof(data, "DEBUG fail ipv6 resolve"); |
706 | | result = hostip_resolv_failed(data, hostname, for_proxy); |
707 | | goto out; |
708 | | } |
709 | | #endif |
710 | | /* Let's check our DNS cache first */ |
711 | 0 | result = Curl_dnscache_get(data, dns_queries, hostname, port, pdns); |
712 | 0 | if(*pdns) { |
713 | 0 | infof(data, "Hostname %s was found in DNS cache", hostname); |
714 | 0 | result = CURLE_OK; |
715 | 0 | } |
716 | 0 | else if(result) { |
717 | 0 | infof(data, "Negative DNS entry"); |
718 | 0 | result = hostip_resolv_failed(data, hostname, for_proxy); |
719 | 0 | } |
720 | 0 | else { |
721 | | /* No luck, we need to start resolving. */ |
722 | 0 | cache_dns = TRUE; |
723 | 0 | result = hostip_resolv_start(data, dns_queries, hostname, port, |
724 | 0 | transport, for_proxy, timeout_ms, allowDOH, |
725 | 0 | presolv_id, pdns); |
726 | 0 | } |
727 | |
|
728 | 0 | out: |
729 | 0 | if(result && (result != CURLE_AGAIN)) { |
730 | 0 | Curl_dns_entry_unlink(data, pdns); |
731 | 0 | if(IS_RESOLV_FAIL(result)) { |
732 | 0 | if(cache_dns) |
733 | 0 | Curl_dnscache_add_negative(data, dns_queries, hostname, port); |
734 | 0 | failf(data, "Could not resolve: %s:%u", hostname, port); |
735 | 0 | } |
736 | 0 | else { |
737 | 0 | failf(data, "Error %d resolving %s:%u", (int)result, hostname, port); |
738 | 0 | } |
739 | 0 | } |
740 | 0 | else if(cache_dns && *pdns) { |
741 | 0 | result = Curl_dnscache_add(data, *pdns); |
742 | 0 | if(result) |
743 | 0 | Curl_dns_entry_unlink(data, pdns); |
744 | 0 | } |
745 | |
|
746 | 0 | return result; |
747 | 0 | } |
748 | | |
749 | | CURLcode Curl_resolv_blocking(struct Curl_easy *data, |
750 | | uint8_t dns_queries, |
751 | | const char *hostname, |
752 | | uint16_t port, |
753 | | uint8_t transport, |
754 | | struct Curl_dns_entry **pdns) |
755 | 0 | { |
756 | 0 | CURLcode result; |
757 | 0 | uint32_t resolv_id; |
758 | 0 | DEBUGASSERT(hostname && *hostname); |
759 | 0 | *pdns = NULL; |
760 | | /* We cannot do a blocking resolve using DoH currently */ |
761 | 0 | result = hostip_resolv(data, dns_queries, |
762 | 0 | hostname, port, transport, FALSE, 0, FALSE, |
763 | 0 | &resolv_id, pdns); |
764 | 0 | switch(result) { |
765 | 0 | case CURLE_OK: |
766 | 0 | DEBUGASSERT(*pdns); |
767 | 0 | break; |
768 | 0 | #ifdef USE_CURL_ASYNC |
769 | 0 | case CURLE_AGAIN: |
770 | 0 | DEBUGASSERT(!*pdns); |
771 | 0 | result = Curl_async_await(data, resolv_id, pdns); |
772 | 0 | Curl_resolv_destroy(data, resolv_id); |
773 | 0 | break; |
774 | 0 | #endif |
775 | 0 | default: |
776 | 0 | break; |
777 | 0 | } |
778 | 0 | return result; |
779 | 0 | } |
780 | | |
781 | | #ifdef USE_ALARM_TIMEOUT |
782 | | /* |
783 | | * This signal handler jumps back into the main libcurl code and continues |
784 | | * execution. This effectively causes the remainder of the application to run |
785 | | * within a signal handler which is nonportable and could lead to problems. |
786 | | */ |
787 | | CURL_NORETURN static void alarmfunc(int sig) |
788 | 0 | { |
789 | 0 | (void)sig; |
790 | 0 | siglongjmp(curl_jmpenv, 1); |
791 | 0 | } |
792 | | |
793 | | static CURLcode resolv_alarm_timeout(struct Curl_easy *data, |
794 | | uint8_t dns_queries, |
795 | | const char *hostname, |
796 | | uint16_t port, |
797 | | uint8_t transport, |
798 | | bool for_proxy, |
799 | | timediff_t timeout_ms, |
800 | | uint32_t *presolv_id, |
801 | | struct Curl_dns_entry **entry) |
802 | 0 | { |
803 | 0 | #ifdef HAVE_SIGACTION |
804 | 0 | struct sigaction keep_sigact; /* store the old struct here */ |
805 | 0 | volatile bool keep_copysig = FALSE; /* whether old sigact has been saved */ |
806 | 0 | struct sigaction sigact; |
807 | | #else |
808 | | #ifdef HAVE_SIGNAL |
809 | | void (*keep_sigact)(int); /* store the old handler here */ |
810 | | #endif /* HAVE_SIGNAL */ |
811 | | #endif /* HAVE_SIGACTION */ |
812 | 0 | volatile long timeout; |
813 | 0 | volatile unsigned int prev_alarm = 0; |
814 | 0 | CURLcode result; |
815 | |
|
816 | 0 | DEBUGASSERT(hostname && *hostname); |
817 | 0 | DEBUGASSERT(timeout_ms > 0); |
818 | 0 | DEBUGASSERT(!data->set.no_signal); |
819 | 0 | #ifndef CURL_DISABLE_DOH |
820 | 0 | DEBUGASSERT(!data->set.doh); |
821 | 0 | #endif |
822 | |
|
823 | 0 | *entry = NULL; |
824 | 0 | timeout = (timeout_ms > LONG_MAX) ? LONG_MAX : (long)timeout_ms; |
825 | 0 | if(timeout < 1000) { |
826 | | /* The alarm() function only provides integer second resolution, so if |
827 | | we want to wait less than one second we must bail out already now. */ |
828 | 0 | failf(data, |
829 | 0 | "remaining timeout of %ld too small to resolve via SIGALRM method", |
830 | 0 | timeout); |
831 | 0 | return CURLE_OPERATION_TIMEDOUT; |
832 | 0 | } |
833 | | /* This allows us to time-out from the name resolver, as the timeout |
834 | | will generate a signal and we will siglongjmp() from that here. |
835 | | This technique has problems (see alarmfunc). |
836 | | This should be the last thing we do before calling Curl_resolv(), |
837 | | as otherwise we would have to worry about variables that get modified |
838 | | before we invoke Curl_resolv() (and thus use "volatile"). */ |
839 | 0 | curl_simple_lock_lock(&curl_jmpenv_lock); |
840 | |
|
841 | 0 | if(sigsetjmp(curl_jmpenv, 1)) { |
842 | | /* this is coming from a siglongjmp() after an alarm signal */ |
843 | 0 | failf(data, "name lookup timed out"); |
844 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
845 | 0 | goto clean_up; |
846 | 0 | } |
847 | 0 | else { |
848 | | /************************************************************* |
849 | | * Set signal handler to catch SIGALRM |
850 | | * Store the old value to be able to set it back later! |
851 | | *************************************************************/ |
852 | 0 | #ifdef HAVE_SIGACTION |
853 | 0 | sigaction(SIGALRM, NULL, &sigact); |
854 | 0 | keep_sigact = sigact; |
855 | 0 | keep_copysig = TRUE; /* yes, we have a copy */ |
856 | 0 | sigact.sa_handler = alarmfunc; |
857 | 0 | #ifdef SA_RESTART |
858 | | /* HP-UX does not have SA_RESTART but defaults to that behavior! */ |
859 | 0 | sigact.sa_flags &= ~SA_RESTART; |
860 | 0 | #endif |
861 | | /* now set the new struct */ |
862 | 0 | sigaction(SIGALRM, &sigact, NULL); |
863 | | #else /* HAVE_SIGACTION */ |
864 | | /* no sigaction(), revert to the much lamer signal() */ |
865 | | #ifdef HAVE_SIGNAL |
866 | | keep_sigact = signal(SIGALRM, alarmfunc); |
867 | | #endif |
868 | | #endif /* HAVE_SIGACTION */ |
869 | | |
870 | | /* alarm() makes a signal get sent when the timeout fires off, and that |
871 | | will abort system calls */ |
872 | 0 | prev_alarm = alarm(curlx_sltoui(timeout / 1000L)); |
873 | 0 | } |
874 | | |
875 | | /* Perform the actual name resolution. This might be interrupted by an |
876 | | * alarm if it takes too long. */ |
877 | 0 | result = hostip_resolv(data, dns_queries, hostname, port, transport, |
878 | 0 | for_proxy, timeout_ms, FALSE, presolv_id, entry); |
879 | |
|
880 | 0 | clean_up: |
881 | 0 | if(!prev_alarm) |
882 | | /* deactivate a possibly active alarm before uninstalling the handler */ |
883 | 0 | alarm(0); |
884 | |
|
885 | 0 | #ifdef HAVE_SIGACTION |
886 | 0 | if(keep_copysig) { |
887 | | /* we got a struct as it looked before, now put that one back nice |
888 | | and clean */ |
889 | 0 | sigaction(SIGALRM, &keep_sigact, NULL); /* put it back */ |
890 | 0 | } |
891 | | #else |
892 | | #ifdef HAVE_SIGNAL |
893 | | /* restore the previous SIGALRM handler */ |
894 | | signal(SIGALRM, keep_sigact); |
895 | | #endif |
896 | | #endif /* HAVE_SIGACTION */ |
897 | |
|
898 | 0 | curl_simple_lock_unlock(&curl_jmpenv_lock); |
899 | | |
900 | | /* switch back the alarm() to either zero or to what it was before minus |
901 | | the time we spent until now! */ |
902 | 0 | if(prev_alarm) { |
903 | | /* there was an alarm() set before us, now put it back */ |
904 | 0 | timediff_t elapsed_secs = curlx_ptimediff_ms(Curl_pgrs_now(data), |
905 | 0 | &data->conn->created) / 1000; |
906 | | |
907 | | /* the alarm period is counted in even number of seconds */ |
908 | 0 | unsigned long alarm_set = (unsigned long)(prev_alarm - elapsed_secs); |
909 | |
|
910 | 0 | if(!alarm_set || |
911 | 0 | ((alarm_set >= 0x80000000) && (prev_alarm < 0x80000000))) { |
912 | | /* if the alarm time-left reached zero or turned "negative" (counted |
913 | | with unsigned values), we should fire off a SIGALRM here, but we |
914 | | will not, and zero would be to switch it off so we never set it to |
915 | | less than 1! */ |
916 | 0 | alarm(1); |
917 | 0 | result = CURLE_OPERATION_TIMEDOUT; |
918 | 0 | failf(data, "Previous alarm fired off"); |
919 | 0 | } |
920 | 0 | else |
921 | 0 | alarm((unsigned int)alarm_set); |
922 | 0 | } |
923 | |
|
924 | 0 | return result; |
925 | 0 | } |
926 | | |
927 | | #endif /* USE_ALARM_TIMEOUT */ |
928 | | |
929 | | #ifdef USE_UNIX_SOCKETS |
930 | | static CURLcode resolv_unix(struct Curl_easy *data, |
931 | | const char *unix_path, |
932 | | bool abstract_path, |
933 | | struct Curl_dns_entry **pdns) |
934 | | { |
935 | | struct Curl_addrinfo *addr; |
936 | | CURLcode result; |
937 | | |
938 | | DEBUGASSERT(unix_path); |
939 | | *pdns = NULL; |
940 | | |
941 | | result = Curl_unix2addr(unix_path, abstract_path, &addr); |
942 | | if(result) { |
943 | | if(result == CURLE_TOO_LARGE) { |
944 | | /* Long paths are not supported for now */ |
945 | | failf(data, "Unix socket path too long: '%s'", unix_path); |
946 | | result = CURLE_COULDNT_RESOLVE_HOST; |
947 | | } |
948 | | return result; |
949 | | } |
950 | | |
951 | | *pdns = Curl_dnscache_mk_entry(data, 0, &addr, NULL, 0); |
952 | | return *pdns ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
953 | | } |
954 | | #endif /* USE_UNIX_SOCKETS */ |
955 | | |
956 | | /* |
957 | | * Curl_resolv() is the main name resolve function within libcurl. It resolves |
958 | | * a name and returns a pointer to the entry in the 'entry' argument. This |
959 | | * function might return immediately if we are using asynch resolves. See the |
960 | | * return codes. |
961 | | * |
962 | | * The cache entry we return will get its 'inuse' counter increased when this |
963 | | * function is used. You MUST call Curl_dns_entry_unlink() later (when you are |
964 | | * done using this struct) to decrease the reference counter again. |
965 | | * |
966 | | * If built with a synchronous resolver and use of signals is not |
967 | | * disabled by the application, then a nonzero timeout will cause a |
968 | | * timeout after the specified number of milliseconds. Otherwise, timeout |
969 | | * is ignored. |
970 | | * |
971 | | * Return codes: |
972 | | * CURLE_OK = success, *pdns set to non-NULL |
973 | | * CURLE_AGAIN = resolving in progress, *pdns == NULL |
974 | | * any other CURLcode error, *pdns == NULL |
975 | | */ |
976 | | CURLcode Curl_resolv(struct Curl_easy *data, |
977 | | struct Curl_peer *peer, |
978 | | uint8_t dns_queries, |
979 | | uint8_t transport, |
980 | | bool for_proxy, |
981 | | timediff_t timeout_ms, |
982 | | uint32_t *presolv_id, |
983 | | struct Curl_dns_entry **pdns) |
984 | 0 | { |
985 | 0 | *presolv_id = 0; |
986 | 0 | *pdns = NULL; |
987 | |
|
988 | 0 | if(timeout_ms < 0) |
989 | | /* got an already expired timeout */ |
990 | 0 | return CURLE_OPERATION_TIMEDOUT; |
991 | 0 | else if(!timeout_ms) |
992 | 0 | timeout_ms = CURL_TIMEOUT_RESOLVE_MS; |
993 | | |
994 | | #ifdef USE_UNIX_SOCKETS |
995 | | if(peer->unix_socket) |
996 | | return resolv_unix(data, peer->hostname, (bool)peer->abstract_uds, pdns); |
997 | | #else |
998 | 0 | if(peer->unix_socket) |
999 | 0 | return hostip_resolv_failed(data, peer->hostname, for_proxy); |
1000 | 0 | #endif |
1001 | | |
1002 | 0 | #ifdef USE_ALARM_TIMEOUT |
1003 | 0 | if(timeout_ms && data->set.no_signal) { |
1004 | | /* Cannot use ALARM when signals are disabled */ |
1005 | 0 | timeout_ms = 0; |
1006 | 0 | } |
1007 | 0 | if(timeout_ms && !Curl_doh_wanted(data)) { |
1008 | 0 | return resolv_alarm_timeout(data, dns_queries, peer->hostname, peer->port, |
1009 | 0 | transport, for_proxy, timeout_ms, presolv_id, |
1010 | 0 | pdns); |
1011 | 0 | } |
1012 | 0 | #endif /* !USE_ALARM_TIMEOUT */ |
1013 | | |
1014 | 0 | #ifndef CURLRES_ASYNCH |
1015 | 0 | if(timeout_ms) |
1016 | 0 | infof(data, "timeout on name lookup is not supported"); |
1017 | 0 | #endif |
1018 | |
|
1019 | 0 | return hostip_resolv(data, dns_queries, peer->hostname, peer->port, |
1020 | 0 | transport, for_proxy, timeout_ms, TRUE, presolv_id, |
1021 | 0 | pdns); |
1022 | 0 | } |
1023 | | |
1024 | | #ifdef USE_CURL_ASYNC |
1025 | | |
1026 | | struct Curl_resolv_async *Curl_async_get(struct Curl_easy *data, |
1027 | | uint32_t resolv_id) |
1028 | 0 | { |
1029 | 0 | struct Curl_resolv_async *async = data->state.async; |
1030 | 0 | for(; async; async = async->next) { |
1031 | 0 | if(async->id == resolv_id) |
1032 | 0 | return async; |
1033 | 0 | } |
1034 | 0 | return NULL; |
1035 | 0 | } |
1036 | | |
1037 | | CURLcode Curl_resolv_take_result(struct Curl_easy *data, uint32_t resolv_id, |
1038 | | struct Curl_dns_entry **pdns) |
1039 | 0 | { |
1040 | 0 | struct Curl_resolv_async *async = Curl_async_get(data, resolv_id); |
1041 | 0 | CURLcode result; |
1042 | | |
1043 | | /* If async resolving is ongoing, this must be set */ |
1044 | 0 | if(!async) |
1045 | 0 | return CURLE_FAILED_INIT; |
1046 | | |
1047 | | /* check if we have the name resolved by now (from someone else) */ |
1048 | 0 | result = Curl_dnscache_get(data, async->dns_queries, |
1049 | 0 | async->hostname, async->port, pdns); |
1050 | 0 | if(*pdns) { |
1051 | | /* Tell a possibly async resolver we no longer need the results. */ |
1052 | 0 | infof(data, "Hostname '%s' was found in DNS cache", async->hostname); |
1053 | 0 | Curl_async_shutdown(data, async); |
1054 | 0 | return CURLE_OK; |
1055 | 0 | } |
1056 | 0 | else if(result) { |
1057 | 0 | Curl_async_shutdown(data, async); |
1058 | 0 | return Curl_async_failed(data, async, NULL); |
1059 | 0 | } |
1060 | | |
1061 | 0 | result = hostip_resolv_take_result(data, async, pdns); |
1062 | |
|
1063 | 0 | if(*pdns) { |
1064 | | /* Add to cache */ |
1065 | 0 | result = Curl_dnscache_add(data, *pdns); |
1066 | 0 | if(result) |
1067 | 0 | Curl_dns_entry_unlink(data, pdns); |
1068 | 0 | } |
1069 | 0 | else if(IS_RESOLV_FAIL(result)) { |
1070 | 0 | Curl_dnscache_add_negative(data, async->dns_queries, |
1071 | 0 | async->hostname, async->port); |
1072 | 0 | failf(data, "Could not resolve: %s:%u", async->hostname, async->port); |
1073 | 0 | } |
1074 | 0 | else if(result) { |
1075 | 0 | failf(data, "Error %d resolving %s:%u", |
1076 | 0 | (int)result, async->hostname, async->port); |
1077 | 0 | } |
1078 | 0 | return result; |
1079 | 0 | } |
1080 | | |
1081 | | CURLcode Curl_resolv_pollset(struct Curl_easy *data, |
1082 | | struct easy_pollset *ps) |
1083 | 0 | { |
1084 | 0 | struct Curl_resolv_async *async = data->state.async; |
1085 | 0 | CURLcode result = CURLE_OK; |
1086 | |
|
1087 | 0 | (void)ps; |
1088 | 0 | for(; async && !result; async = async->next) { |
1089 | 0 | #ifndef CURL_DISABLE_DOH |
1090 | 0 | if(async->doh) /* DoH has nothing for the pollset */ |
1091 | 0 | continue; |
1092 | 0 | #endif |
1093 | 0 | result = Curl_async_pollset(data, async, ps); |
1094 | 0 | } |
1095 | 0 | return result; |
1096 | 0 | } |
1097 | | |
1098 | | void Curl_resolv_destroy(struct Curl_easy *data, uint32_t resolv_id) |
1099 | 0 | { |
1100 | 0 | struct Curl_resolv_async **panchor = &data->state.async; |
1101 | |
|
1102 | 0 | for(; *panchor; panchor = &(*panchor)->next) { |
1103 | 0 | struct Curl_resolv_async *async = *panchor; |
1104 | 0 | if(async->id == resolv_id) { |
1105 | 0 | *panchor = async->next; |
1106 | 0 | Curl_async_destroy(data, async); |
1107 | 0 | break; |
1108 | 0 | } |
1109 | 0 | } |
1110 | 0 | } |
1111 | | |
1112 | | void Curl_resolv_shutdown_all(struct Curl_easy *data) |
1113 | 0 | { |
1114 | 0 | struct Curl_resolv_async *async = data->state.async; |
1115 | 0 | for(; async; async = async->next) { |
1116 | 0 | Curl_async_shutdown(data, async); |
1117 | 0 | } |
1118 | 0 | } |
1119 | | |
1120 | | void Curl_resolv_destroy_all(struct Curl_easy *data) |
1121 | 0 | { |
1122 | 0 | while(data->state.async) { |
1123 | 0 | struct Curl_resolv_async *async = data->state.async; |
1124 | 0 | data->state.async = async->next; |
1125 | 0 | Curl_async_destroy(data, async); |
1126 | 0 | } |
1127 | 0 | } |
1128 | | |
1129 | | #endif /* USE_CURL_ASYNC */ |