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