/src/CMake/Utilities/cmcurl/lib/vdns/cf-dns.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 | | #include "urldata.h" |
27 | | #include "curl_addrinfo.h" |
28 | | #include "cfilters.h" |
29 | | #include "connect.h" |
30 | | #include "curl_trc.h" |
31 | | #include "multiif.h" |
32 | | #include "progress.h" |
33 | | #include "url.h" |
34 | | #include "vdns/cf-dns.h" |
35 | | #include "vdns/dnscache.h" |
36 | | #include "vdns/httpsrr.h" |
37 | | #include "curlx/strparse.h" |
38 | | |
39 | | /* Max time to wait for AAAA before connecting sub-filters, e.g. |
40 | | * letting cf-ip-happy.c do its work. */ |
41 | 0 | #define CURL_HE_AAAA_AWAIT_MS 25 |
42 | | |
43 | | struct cf_dns_ctx { |
44 | | struct Curl_dns_entry *dns; |
45 | | struct Curl_peer *peer; |
46 | | CURLcode resolv_result; |
47 | | timediff_t he_aaaa_await_ms; |
48 | | uint32_t resolv_id; |
49 | | uint8_t dns_queries; |
50 | | uint8_t transport; |
51 | | BIT(started); |
52 | | BIT(announced); |
53 | | BIT(for_proxy); |
54 | | }; |
55 | | |
56 | | static struct cf_dns_ctx *cf_dns_ctx_create(struct Curl_easy *data, |
57 | | struct Curl_peer *peer, |
58 | | uint8_t dns_queries, |
59 | | uint8_t transport, |
60 | | bool for_proxy) |
61 | 0 | { |
62 | 0 | struct cf_dns_ctx *ctx; |
63 | |
|
64 | 0 | ctx = curlx_calloc(1, sizeof(*ctx)); |
65 | 0 | if(!ctx) |
66 | 0 | return NULL; |
67 | | |
68 | 0 | Curl_peer_link(&ctx->peer, peer); |
69 | 0 | ctx->dns_queries = dns_queries; |
70 | 0 | ctx->transport = transport; |
71 | 0 | ctx->for_proxy = for_proxy; |
72 | 0 | ctx->he_aaaa_await_ms = CURL_HE_AAAA_AWAIT_MS; |
73 | | #ifdef DEBUGBUILD |
74 | | { |
75 | | const char *p = getenv("CURL_DBG_HE_AAAA_AWAIT_MS"); |
76 | | if(p) { |
77 | | curl_off_t l; |
78 | | if(!curlx_str_number(&p, &l, UINT32_MAX)) { |
79 | | ctx->he_aaaa_await_ms = (uint32_t)l; |
80 | | } |
81 | | } |
82 | | } |
83 | | #endif |
84 | |
|
85 | 0 | CURL_TRC_DNS(data, "[%s] created DNS filter for %s:%u, transport=%x", |
86 | 0 | Curl_resolv_query_str(ctx->dns_queries), |
87 | 0 | peer->hostname, peer->port, ctx->transport); |
88 | 0 | return ctx; |
89 | 0 | } |
90 | | |
91 | | static void cf_dns_ctx_destroy(struct Curl_easy *data, |
92 | | struct cf_dns_ctx *ctx) |
93 | 0 | { |
94 | 0 | if(ctx) { |
95 | 0 | Curl_peer_unlink(&ctx->peer); |
96 | 0 | Curl_dns_entry_unlink(data, &ctx->dns); |
97 | 0 | curlx_free(ctx); |
98 | 0 | } |
99 | 0 | } |
100 | | |
101 | | #ifdef CURLVERBOSE |
102 | | static void cf_dns_report_addr(struct Curl_easy *data, |
103 | | struct dynbuf *tmp, |
104 | | const char *label, |
105 | | int ai_family, |
106 | | const struct Curl_addrinfo *ai) |
107 | 0 | { |
108 | 0 | char buf[MAX_IPADR_LEN]; |
109 | 0 | const char *sep = ""; |
110 | 0 | CURLcode result; |
111 | |
|
112 | 0 | curlx_dyn_reset(tmp); |
113 | 0 | for(; ai; ai = ai->ai_next) { |
114 | 0 | if(ai->ai_family == ai_family) { |
115 | 0 | Curl_printable_address(ai, buf, sizeof(buf)); |
116 | 0 | result = curlx_dyn_addf(tmp, "%s%s", sep, buf); |
117 | 0 | if(result) { |
118 | 0 | infof(data, "too many IP, cannot show"); |
119 | 0 | return; |
120 | 0 | } |
121 | 0 | sep = ", "; |
122 | 0 | } |
123 | 0 | } |
124 | | |
125 | 0 | infof(data, "%s%s", label, |
126 | 0 | (curlx_dyn_len(tmp) ? curlx_dyn_ptr(tmp) : "(none)")); |
127 | 0 | } |
128 | | |
129 | | static void cf_dns_report(struct Curl_cfilter *cf, |
130 | | struct Curl_easy *data, |
131 | | struct Curl_dns_entry *dns) |
132 | 0 | { |
133 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
134 | 0 | struct dynbuf tmp; |
135 | |
|
136 | 0 | if(!Curl_trc_is_verbose(data) || |
137 | | /* ignore no name or numerical IP addresses */ |
138 | 0 | !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname)) |
139 | 0 | return; |
140 | | |
141 | 0 | if(ctx->peer->unix_socket) { |
142 | | #ifdef USE_UNIX_SOCKETS |
143 | | CURL_TRC_CF(data, cf, "resolved unix://%s", ctx->peer->hostname); |
144 | | #else |
145 | 0 | DEBUGASSERT(0); |
146 | 0 | #endif |
147 | 0 | } |
148 | 0 | else { |
149 | 0 | curlx_dyn_init(&tmp, 1024); |
150 | 0 | if(CURL_DNSQ_IS_ADDR(ctx->dns_queries)) { |
151 | 0 | infof(data, "Host %s:%u was resolved.", dns->hostname, dns->port); |
152 | 0 | #ifdef CURLRES_IPV6 |
153 | 0 | cf_dns_report_addr(data, &tmp, "IPv6: ", AF_INET6, dns->addr); |
154 | 0 | #endif |
155 | 0 | cf_dns_report_addr(data, &tmp, "IPv4: ", AF_INET, dns->addr); |
156 | 0 | } |
157 | | #ifdef USE_HTTPSRR |
158 | | else if(ctx->dns_queries & CURL_DNSQ_HTTPS) { |
159 | | if(!dns->hinfo) |
160 | | infof(data, "HTTPS-RR %s:%u: -", dns->hostname, dns->port); |
161 | | else if(!Curl_httpsrr_applicable(data, dns->hinfo)) |
162 | | infof(data, "HTTPS-RR %s:%u: not applicable", |
163 | | dns->hostname, dns->port); |
164 | | else { |
165 | | CURLcode result = Curl_httpsrr_print(&tmp, dns->hinfo); |
166 | | if(!result) |
167 | | infof(data, "HTTPS-RR %s:%u: %s", |
168 | | dns->hostname, dns->port, curlx_dyn_ptr(&tmp)); |
169 | | else |
170 | | infof(data, "Error printing HTTPS-RR information"); |
171 | | } |
172 | | } |
173 | | #endif |
174 | 0 | curlx_dyn_free(&tmp); |
175 | 0 | } |
176 | 0 | } |
177 | | #else |
178 | | #define cf_dns_report(x, y, z) Curl_nop_stmt |
179 | | #endif |
180 | | |
181 | | /************************************************************* |
182 | | * Resolve the address of the server or proxy |
183 | | *************************************************************/ |
184 | | static CURLcode cf_dns_start(struct Curl_cfilter *cf, |
185 | | struct Curl_easy *data, |
186 | | struct Curl_dns_entry **pdns) |
187 | 0 | { |
188 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
189 | 0 | timediff_t timeout_ms = Curl_timeleft_ms(data); |
190 | 0 | CURLcode result = CURLE_OK; |
191 | |
|
192 | 0 | *pdns = NULL; |
193 | |
|
194 | 0 | CURL_TRC_CF(data, cf, "[%s] cf_dns_start %s %s:%u", |
195 | 0 | Curl_resolv_query_str(ctx->dns_queries), |
196 | 0 | ctx->peer->unix_socket ? "unix-domain-socket" : "host", |
197 | 0 | ctx->peer->hostname, ctx->peer->port); |
198 | 0 | if(ctx->peer->unix_socket) |
199 | 0 | ctx->dns_queries = CURL_DNSQ_A; /* treat it like an A resolve */ |
200 | |
|
201 | 0 | if(CURL_DNSQ_IS_ADDR(ctx->dns_queries)) { |
202 | 0 | if(Curl_is_ipv4addr(ctx->peer->hostname)) |
203 | 0 | ctx->dns_queries |= CURL_DNSQ_A; |
204 | 0 | #ifdef USE_IPV6 |
205 | 0 | else if(ctx->peer->ipv6) |
206 | 0 | ctx->dns_queries |= CURL_DNSQ_AAAA; |
207 | 0 | #endif |
208 | |
|
209 | 0 | result = Curl_resolv(data, ctx->peer, ctx->dns_queries, ctx->transport, |
210 | 0 | (bool)ctx->for_proxy, timeout_ms, |
211 | 0 | &ctx->resolv_id, pdns); |
212 | 0 | } |
213 | | #ifdef USE_HTTPSRR |
214 | | else if(ctx->dns_queries == CURL_DNSQ_HTTPS) { |
215 | | result = Curl_resolv_https(data, ctx->peer, (bool)ctx->for_proxy, |
216 | | timeout_ms, &ctx->resolv_id, pdns); |
217 | | } |
218 | | #endif |
219 | 0 | else { |
220 | 0 | failf(data, "unsupported DNS queries %x", ctx->dns_queries); |
221 | 0 | return CURLE_FAILED_INIT; |
222 | 0 | } |
223 | | |
224 | 0 | DEBUGASSERT(!result || !*pdns); |
225 | 0 | if(!result) { /* resolved right away, either sync or from dnscache */ |
226 | 0 | DEBUGASSERT(*pdns); |
227 | 0 | return CURLE_OK; |
228 | 0 | } |
229 | 0 | else if(result == CURLE_AGAIN) { /* async resolv in progress */ |
230 | 0 | return CURLE_OK; |
231 | 0 | } |
232 | 0 | else if(result == CURLE_OPERATION_TIMEDOUT) { /* took too long */ |
233 | 0 | failf(data, "Failed to resolve '%s' with timeout after %" |
234 | 0 | FMT_TIMEDIFF_T " ms", ctx->peer->hostname, |
235 | 0 | Curl_pgrs_since_ms(data, NULL, TIMER_STARTSINGLE)); |
236 | 0 | return CURLE_OPERATION_TIMEDOUT; |
237 | 0 | } |
238 | 0 | else { |
239 | 0 | DEBUGASSERT(result); |
240 | 0 | if(ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA)) |
241 | 0 | failf(data, "Could not resolve: %s", ctx->peer->hostname); |
242 | 0 | return result; |
243 | 0 | } |
244 | 0 | } |
245 | | |
246 | | static bool cf_dns_ready_to_connect(struct Curl_cfilter *cf, |
247 | | struct Curl_easy *data) |
248 | 0 | { |
249 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
250 | |
|
251 | 0 | if(ctx->resolv_result) |
252 | 0 | return TRUE; |
253 | 0 | else if(ctx->dns) |
254 | 0 | return TRUE; |
255 | 0 | #ifdef USE_CURL_ASYNC |
256 | 0 | else if(CURL_DNSQ_IS_ADDR(ctx->dns_queries)) { |
257 | 0 | const struct curltime *pnow; |
258 | 0 | timediff_t remain_ms; |
259 | | /* For Happy Eyeballing, we can start on either A or AAAA resolves, |
260 | | * but AAAA is preferred. We enforce a small delay for missing |
261 | | * AAAA to arrive, then we let the connect continue. |
262 | | * Note: if AAAA was never started (-4), it is considered to have |
263 | | * an answer (e.g. a negative one). */ |
264 | 0 | if(Curl_resolv_has_answers(data, ctx->resolv_id, CURL_DNSQ_AAAA)) |
265 | 0 | return TRUE; |
266 | 0 | pnow = Curl_pgrs_now(data); |
267 | 0 | remain_ms = ctx->he_aaaa_await_ms - |
268 | 0 | Curl_resolv_elapsed_ms(data, ctx->resolv_id, pnow); |
269 | 0 | if(remain_ms <= 0) |
270 | 0 | return TRUE; |
271 | 0 | CURL_TRC_CF(data, cf, "[%s] still waiting %" FMT_TIMEDIFF_T |
272 | 0 | "ms for AAAA result", |
273 | 0 | Curl_resolv_query_str(ctx->dns_queries), remain_ms); |
274 | 0 | Curl_expire_set(data, EXPIRE_HAPPY_EYEBALLS, remain_ms, pnow); |
275 | 0 | return FALSE; |
276 | 0 | } |
277 | 0 | else { |
278 | 0 | return TRUE; |
279 | 0 | } |
280 | | #else |
281 | | (void)data; |
282 | | DEBUGASSERT(0); /* We should not come here */ |
283 | | return FALSE; |
284 | | #endif /* USE_CURL_ASYNC */ |
285 | 0 | } |
286 | | |
287 | | static CURLcode cf_dns_connect(struct Curl_cfilter *cf, |
288 | | struct Curl_easy *data, |
289 | | bool *done) |
290 | 0 | { |
291 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
292 | 0 | bool ip_query = (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA)); |
293 | |
|
294 | 0 | if(cf->connected) { |
295 | 0 | *done = TRUE; |
296 | 0 | return CURLE_OK; |
297 | 0 | } |
298 | | |
299 | 0 | *done = FALSE; |
300 | 0 | if(!ctx->started) { |
301 | 0 | ctx->started = TRUE; |
302 | 0 | ctx->resolv_result = cf_dns_start(cf, data, &ctx->dns); |
303 | 0 | } |
304 | |
|
305 | 0 | if(!ctx->dns && !ctx->resolv_result) { |
306 | 0 | ctx->resolv_result = |
307 | 0 | Curl_resolv_take_result(data, ctx->resolv_id, &ctx->dns); |
308 | 0 | } |
309 | |
|
310 | 0 | if(ctx->resolv_result && ip_query) { |
311 | | /* failing A|AAAA resolves is a hard failure. */ |
312 | 0 | CURL_TRC_CF(data, cf, "[%s] error resolving: %d", |
313 | 0 | Curl_resolv_query_str(ctx->dns_queries), |
314 | 0 | (int)ctx->resolv_result); |
315 | 0 | return ctx->resolv_result; |
316 | 0 | } |
317 | | |
318 | 0 | if(ctx->dns && !ctx->announced) { |
319 | 0 | ctx->announced = TRUE; |
320 | 0 | if((cf->sockindex == FIRSTSOCKET) && ip_query) { |
321 | 0 | cf->conn->bits.dns_resolved = TRUE; |
322 | 0 | Curl_pgrsTime(data, TIMER_NAMELOOKUP); |
323 | 0 | } |
324 | 0 | cf_dns_report(cf, data, ctx->dns); |
325 | 0 | } |
326 | | |
327 | | /* Delay connection sub-filters when we are still waiting for AAAA */ |
328 | 0 | if(!cf_dns_ready_to_connect(cf, data)) { |
329 | 0 | return CURLE_OK; |
330 | 0 | } |
331 | | |
332 | 0 | if(cf->next && !cf->next->connected) { |
333 | 0 | bool sub_done; |
334 | 0 | CURLcode result = Curl_conn_cf_connect(cf->next, data, &sub_done); |
335 | 0 | if(result || !sub_done) |
336 | 0 | return result; |
337 | 0 | DEBUGASSERT(sub_done); |
338 | 0 | } |
339 | | |
340 | | /* sub filter chain is connected, this means the filter has done |
341 | | * its work and is connected as well, if it has results or not. */ |
342 | 0 | CURL_TRC_CF(data, cf, "connected filter chain below"); |
343 | 0 | *done = TRUE; |
344 | 0 | cf->connected = TRUE; |
345 | 0 | Curl_resolv_destroy(data, ctx->resolv_id); |
346 | 0 | return CURLE_OK; |
347 | 0 | } |
348 | | |
349 | | static void cf_dns_destroy(struct Curl_cfilter *cf, struct Curl_easy *data) |
350 | 0 | { |
351 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
352 | |
|
353 | 0 | CURL_TRC_CF(data, cf, "destroy"); |
354 | 0 | cf_dns_ctx_destroy(data, ctx); |
355 | 0 | } |
356 | | |
357 | | static CURLcode cf_dns_adjust_pollset(struct Curl_cfilter *cf, |
358 | | struct Curl_easy *data, |
359 | | struct easy_pollset *ps) |
360 | 0 | { |
361 | 0 | #ifdef USE_CURL_ASYNC |
362 | 0 | if(!cf->connected) { |
363 | | /* The pollset only works when we have started the resolving. |
364 | | * Otherwise we might wait on the WAKEUP socketpair forever. |
365 | | * Since DNS filters can be added in the middle of a connect |
366 | | * attempt, they are not always started that way. */ |
367 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
368 | 0 | if(!ctx->started) { |
369 | 0 | CURL_TRC_CF(data, cf, "adjust_pollset, starting %s:%u queries=%s", |
370 | 0 | ctx->peer->hostname, ctx->peer->port, |
371 | 0 | Curl_resolv_query_str(ctx->dns_queries)); |
372 | 0 | ctx->started = TRUE; |
373 | 0 | ctx->resolv_result = cf_dns_start(cf, data, &ctx->dns); |
374 | 0 | if(ctx->resolv_result || ctx->dns) { |
375 | 0 | Curl_multi_mark_dirty(data); |
376 | 0 | } |
377 | 0 | } |
378 | 0 | return Curl_resolv_pollset(data, ps); |
379 | 0 | } |
380 | | #else |
381 | | (void)cf; |
382 | | (void)data; |
383 | | (void)ps; |
384 | | #endif |
385 | 0 | return CURLE_OK; |
386 | 0 | } |
387 | | |
388 | | static CURLcode cf_dns_cntrl(struct Curl_cfilter *cf, |
389 | | struct Curl_easy *data, |
390 | | int event, int arg1, void *arg2) |
391 | 0 | { |
392 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
393 | 0 | CURLcode result = CURLE_OK; |
394 | |
|
395 | 0 | (void)arg1; |
396 | 0 | (void)arg2; |
397 | 0 | switch(event) { |
398 | 0 | case CF_CTRL_DATA_DONE: |
399 | 0 | if(ctx->dns) { |
400 | | /* Should only come here when the connect attempt failed and |
401 | | * `data` is giving up on it. On a successful connect, we already |
402 | | * unlinked the DNS entry. */ |
403 | 0 | Curl_dns_entry_unlink(data, &ctx->dns); |
404 | 0 | } |
405 | 0 | break; |
406 | 0 | default: |
407 | 0 | break; |
408 | 0 | } |
409 | 0 | return result; |
410 | 0 | } |
411 | | |
412 | | struct Curl_cftype Curl_cft_dns = { |
413 | | "DNS", |
414 | | CF_TYPE_SETUP | CF_TYPE_DNS, |
415 | | CURL_LOG_LVL_NONE, |
416 | | cf_dns_destroy, |
417 | | cf_dns_connect, |
418 | | Curl_cf_def_shutdown, |
419 | | cf_dns_adjust_pollset, |
420 | | Curl_cf_def_data_pending, |
421 | | Curl_cf_def_send, |
422 | | Curl_cf_def_recv, |
423 | | cf_dns_cntrl, |
424 | | Curl_cf_def_conn_is_alive, |
425 | | Curl_cf_def_conn_keep_alive, |
426 | | Curl_cf_def_query, |
427 | | }; |
428 | | |
429 | | static CURLcode cf_dns_create(struct Curl_cfilter **pcf, |
430 | | struct Curl_easy *data, |
431 | | struct Curl_peer *peer, |
432 | | uint8_t dns_queries, |
433 | | uint8_t transport, |
434 | | bool for_proxy) |
435 | 0 | { |
436 | 0 | struct Curl_cfilter *cf = NULL; |
437 | 0 | struct cf_dns_ctx *ctx; |
438 | 0 | CURLcode result = CURLE_OK; |
439 | |
|
440 | 0 | (void)data; |
441 | 0 | ctx = cf_dns_ctx_create(data, peer, dns_queries, transport, for_proxy); |
442 | 0 | if(!ctx) { |
443 | 0 | result = CURLE_OUT_OF_MEMORY; |
444 | 0 | goto out; |
445 | 0 | } |
446 | | |
447 | 0 | result = Curl_cf_create(&cf, &Curl_cft_dns, ctx); |
448 | |
|
449 | 0 | out: |
450 | 0 | *pcf = result ? NULL : cf; |
451 | 0 | if(result) |
452 | 0 | cf_dns_ctx_destroy(data, ctx); |
453 | 0 | return result; |
454 | 0 | } |
455 | | |
456 | | /* Adds a "resolv" filter at the top of the connection's filter chain. |
457 | | * The filter will resolve the peer on the first connect attempt. */ |
458 | | static CURLcode cf_dns_add(struct Curl_easy *data, |
459 | | struct connectdata *conn, |
460 | | int8_t sockindex, |
461 | | struct Curl_peer *peer, |
462 | | uint8_t dns_queries, |
463 | | uint8_t transport) |
464 | 0 | { |
465 | 0 | struct Curl_cfilter *cf = NULL; |
466 | 0 | bool for_proxy = FALSE; |
467 | 0 | CURLcode result; |
468 | |
|
469 | 0 | if(!peer) |
470 | 0 | return CURLE_FAILED_INIT; |
471 | 0 | #ifndef CURL_DISABLE_PROXY |
472 | 0 | for_proxy = (peer == conn->socks_proxy.peer) || |
473 | 0 | (peer == conn->http_proxy.peer); |
474 | 0 | #endif |
475 | |
|
476 | 0 | result = cf_dns_create(&cf, data, peer, dns_queries, transport, for_proxy); |
477 | 0 | if(result) |
478 | 0 | goto out; |
479 | 0 | Curl_conn_cf_add(data, conn, sockindex, cf); |
480 | 0 | out: |
481 | 0 | return result; |
482 | 0 | } |
483 | | |
484 | | /* Insert a new "resolv" filter directly after `cf`. It will |
485 | | * start a DNS resolve for the given peer on the |
486 | | * first connect attempt. |
487 | | * See socks.c on how this is used to make a non-blocking DNS |
488 | | * resolve during connect. |
489 | | */ |
490 | | static CURLcode cf_dns_insert_after(struct Curl_cfilter *cf_at, |
491 | | struct Curl_easy *data, |
492 | | struct Curl_peer *peer, |
493 | | uint8_t dns_queries, |
494 | | uint8_t transport) |
495 | 0 | { |
496 | 0 | struct Curl_cfilter *cf; |
497 | 0 | CURLcode result; |
498 | |
|
499 | 0 | result = cf_dns_create(&cf, data, peer, dns_queries, transport, FALSE); |
500 | 0 | if(result) |
501 | 0 | return result; |
502 | | |
503 | 0 | Curl_conn_cf_insert_after(cf_at, cf); |
504 | 0 | return CURLE_OK; |
505 | 0 | } |
506 | | |
507 | | static CURLcode cf_dns_add_resolve(struct Curl_easy *data, |
508 | | struct connectdata *conn, |
509 | | int8_t sockindex, |
510 | | struct Curl_peer *peer, |
511 | | uint8_t dns_queries, |
512 | | uint8_t transport) |
513 | 0 | { |
514 | 0 | struct Curl_cfilter *cf = data->conn->cfilter[sockindex]; |
515 | 0 | struct Curl_cfilter *cf_dns = NULL; |
516 | 0 | bool is_addr = CURL_DNSQ_IS_ADDR(dns_queries); |
517 | |
|
518 | 0 | if((dns_queries & CURL_DNSQ_HTTPS) && |
519 | 0 | Curl_is_ipaddr(peer->hostname)) { |
520 | 0 | dns_queries = (uint8_t)(dns_queries & ~CURL_DNSQ_HTTPS); |
521 | 0 | } |
522 | |
|
523 | 0 | for(; cf && dns_queries; cf = cf->next) { |
524 | 0 | if(cf->cft == &Curl_cft_dns) { |
525 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
526 | 0 | cf_dns = cf; |
527 | 0 | if(Curl_peer_same_destination(ctx->peer, peer)) { |
528 | | /* subtract queries already being scheduled/ongoing */ |
529 | 0 | dns_queries = (uint8_t)(~ctx->dns_queries & dns_queries); |
530 | 0 | if(!dns_queries) { /* already there */ |
531 | 0 | return CURLE_OK; |
532 | 0 | } |
533 | 0 | else if(is_addr && !ctx->started && |
534 | 0 | CURL_DNSQ_IS_ADDR(ctx->dns_queries)) { |
535 | 0 | ctx->dns_queries |= dns_queries; |
536 | 0 | CURL_TRC_DNS(data, "[%s] added queries=%s for %s:%u", |
537 | 0 | Curl_resolv_query_str(ctx->dns_queries), |
538 | 0 | Curl_resolv_query_str(dns_queries), |
539 | 0 | peer->hostname, peer->port); |
540 | 0 | return CURLE_OK; |
541 | 0 | } |
542 | 0 | } |
543 | 0 | } |
544 | 0 | } |
545 | | |
546 | 0 | if(dns_queries) { |
547 | | /* No existing filter is handling these, add a new DNS filter |
548 | | * (after the last one if there was one already. FCFS. */ |
549 | 0 | if(cf_dns) |
550 | 0 | return cf_dns_insert_after(cf_dns, data, peer, dns_queries, transport); |
551 | 0 | else |
552 | 0 | return cf_dns_add(data, conn, sockindex, peer, dns_queries, transport); |
553 | 0 | } |
554 | 0 | return CURLE_OK; |
555 | 0 | } |
556 | | |
557 | | CURLcode Curl_conn_dns_add_addr_resolve(struct Curl_easy *data, |
558 | | struct connectdata *conn, |
559 | | int8_t sockindex, |
560 | | struct Curl_peer *peer, |
561 | | uint8_t dns_queries, |
562 | | uint8_t transport) |
563 | 0 | { |
564 | | /* should only have address query bits */ |
565 | 0 | DEBUGASSERT(!(dns_queries & ~CURL_DNSQ_ADDR)); |
566 | 0 | return cf_dns_add_resolve(data, conn, sockindex, peer, |
567 | 0 | (dns_queries & CURL_DNSQ_ADDR), transport); |
568 | 0 | } |
569 | | |
570 | | /* Return the result of the DNS address resolution for peer. |
571 | | * Searches for a DNS filter from the top of the filter chain down. Returns |
572 | | * - CURLE_AGAIN when not done yet |
573 | | * - CURLE_OK when DNS was successfully resolved |
574 | | * - CURLR_FAILED_INIT when no resolv filter was found |
575 | | * - error returned by the DNS resolv |
576 | | */ |
577 | | CURLcode Curl_conn_dns_addr_result(struct connectdata *conn, |
578 | | int8_t sockindex, |
579 | | struct Curl_peer *peer) |
580 | 0 | { |
581 | 0 | struct Curl_cfilter *cf = conn->cfilter[sockindex]; |
582 | 0 | for(; cf; cf = cf->next) { |
583 | 0 | if(cf->cft == &Curl_cft_dns) { |
584 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
585 | 0 | if(Curl_peer_same_destination(ctx->peer, peer) && |
586 | 0 | (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))) { |
587 | 0 | if(ctx->dns || ctx->resolv_result) |
588 | 0 | return ctx->resolv_result; |
589 | 0 | return CURLE_AGAIN; |
590 | 0 | } |
591 | 0 | } |
592 | 0 | } |
593 | 0 | return CURLE_FAILED_INIT; /* no one is resolving */ |
594 | 0 | } |
595 | | |
596 | | /* Return the addrinfo at `index` for the given `family` from the |
597 | | * first "resolve" filter at the connection. If the DNS resolving is |
598 | | * not done yet or if no address for the family exists, returns NULL. |
599 | | */ |
600 | | const struct Curl_addrinfo *Curl_conn_dns_get_ai(struct Curl_easy *data, |
601 | | struct Curl_peer *peer, |
602 | | int8_t sockindex, |
603 | | int ai_family, |
604 | | unsigned int index) |
605 | 0 | { |
606 | 0 | struct connectdata *conn = data->conn; |
607 | 0 | struct Curl_cfilter *cf = conn->cfilter[sockindex]; |
608 | |
|
609 | 0 | for(; cf; cf = cf->next) { |
610 | 0 | if(cf->cft == &Curl_cft_dns) { |
611 | 0 | struct cf_dns_ctx *ctx = cf->ctx; |
612 | 0 | if(Curl_peer_same_destination(ctx->peer, peer) && |
613 | 0 | (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))) { |
614 | 0 | CURL_TRC_CF(data, cf, "get %uth result for %s:%u, family=%d, dns=%d", |
615 | 0 | index, peer->hostname, peer->port, ai_family, !!ctx->dns); |
616 | 0 | if(ctx->resolv_result) |
617 | 0 | return NULL; |
618 | 0 | else if(ctx->dns) { |
619 | | /* A cached DNS entry may contain address families that we |
620 | | * here never queried for. We want to give no results for those. */ |
621 | 0 | if((ai_family == AF_INET) && !(ctx->dns_queries & CURL_DNSQ_A)) |
622 | 0 | return NULL; |
623 | 0 | #ifdef USE_IPV6 |
624 | 0 | if((ai_family == AF_INET6) && !(ctx->dns_queries & CURL_DNSQ_AAAA)) |
625 | 0 | return NULL; |
626 | 0 | #endif |
627 | 0 | return Curl_addrinfo_get(ctx->dns->addr, ai_family, index); |
628 | 0 | } |
629 | 0 | else |
630 | 0 | return Curl_resolv_get_ai(data, ctx->resolv_id, ai_family, index); |
631 | 0 | } |
632 | 0 | } |
633 | 0 | } |
634 | 0 | return NULL; |
635 | 0 | } |
636 | | |
637 | | #ifdef USE_HTTPSRR |
638 | | CURLcode Curl_conn_dns_add_https_resolve(struct Curl_easy *data, |
639 | | struct connectdata *conn, |
640 | | int8_t sockindex, |
641 | | struct Curl_peer *peer) |
642 | | { |
643 | | return cf_dns_add_resolve(data, conn, sockindex, peer, |
644 | | CURL_DNSQ_HTTPS, conn->transport_wanted); |
645 | | } |
646 | | |
647 | | /* Return the HTTPS-RR info from the first "resolve" filter at the |
648 | | * connection. If the DNS resolving is not done yet or if there |
649 | | * is no HTTPS-RR info, returns NULL. |
650 | | */ |
651 | | const struct Curl_https_rrinfo *Curl_conn_dns_get_https(struct Curl_easy *data, |
652 | | int8_t sockindex, |
653 | | struct Curl_peer *peer) |
654 | | { |
655 | | struct Curl_cfilter *cf = data->conn->cfilter[sockindex]; |
656 | | for(; cf; cf = cf->next) { |
657 | | if(cf->cft == &Curl_cft_dns) { |
658 | | struct cf_dns_ctx *ctx = cf->ctx; |
659 | | if(Curl_peer_same_destination(ctx->peer, peer) && |
660 | | (ctx->dns_queries & CURL_DNSQ_HTTPS)) { |
661 | | if(ctx->dns) |
662 | | return ctx->dns->hinfo; |
663 | | else |
664 | | return Curl_resolv_get_https(data, ctx->resolv_id); |
665 | | } |
666 | | } |
667 | | } |
668 | | return NULL; |
669 | | } |
670 | | |
671 | | bool Curl_conn_dns_resolved_https(struct Curl_easy *data, |
672 | | int8_t sockindex, |
673 | | struct Curl_peer *peer) |
674 | | { |
675 | | struct Curl_cfilter *cf = data->conn->cfilter[sockindex]; |
676 | | for(; cf; cf = cf->next) { |
677 | | if(cf->cft == &Curl_cft_dns) { |
678 | | struct cf_dns_ctx *ctx = cf->ctx; |
679 | | if(Curl_peer_same_destination(ctx->peer, peer) && |
680 | | (ctx->dns_queries & CURL_DNSQ_HTTPS)) { |
681 | | if(ctx->dns) |
682 | | return TRUE; |
683 | | else |
684 | | return Curl_resolv_knows_https(data, ctx->resolv_id); |
685 | | } |
686 | | } |
687 | | } |
688 | | return TRUE; |
689 | | } |
690 | | |
691 | | #endif /* USE_HTTPSRR */ |