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