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