Coverage Report

Created: 2026-09-04 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
12.3k
#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
12.3k
{
62
12.3k
  struct cf_dns_ctx *ctx;
63
64
12.3k
  ctx = curlx_calloc(1, sizeof(*ctx));
65
12.3k
  if(!ctx)
66
0
    return NULL;
67
68
12.3k
  Curl_peer_link(&ctx->peer, peer);
69
12.3k
  ctx->dns_queries = dns_queries;
70
12.3k
  ctx->transport = transport;
71
12.3k
  ctx->for_proxy = for_proxy;
72
12.3k
  ctx->he_aaaa_await_ms = CURL_HE_AAAA_AWAIT_MS;
73
12.3k
#ifdef DEBUGBUILD
74
12.3k
  {
75
12.3k
    const char *p = getenv("CURL_DBG_HE_AAAA_AWAIT_MS");
76
12.3k
    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
12.3k
  }
83
12.3k
#endif
84
85
12.3k
  CURL_TRC_DNS(data, "[%s] created DNS filter for %s:%u, transport=%x",
86
12.3k
               Curl_resolv_query_str(ctx->dns_queries),
87
12.3k
               peer->hostname, peer->port, ctx->transport);
88
12.3k
  return ctx;
89
12.3k
}
90
91
static void cf_dns_ctx_destroy(struct Curl_easy *data,
92
                               struct cf_dns_ctx *ctx)
93
12.3k
{
94
12.3k
  if(ctx) {
95
12.3k
    Curl_peer_unlink(&ctx->peer);
96
12.3k
    Curl_dns_entry_unlink(data, &ctx->dns);
97
12.3k
    curlx_free(ctx);
98
12.3k
  }
99
12.3k
}
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
12.2k
{
133
12.2k
  struct cf_dns_ctx *ctx = cf->ctx;
134
12.2k
  struct dynbuf tmp;
135
136
12.2k
  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
12.2k
    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
0
#ifdef USE_HTTPSRR
158
0
    else if(ctx->dns_queries & CURL_DNSQ_HTTPS) {
159
0
      if(!dns->hinfo)
160
0
        infof(data, "HTTPS-RR %s:%u: -", dns->hostname, dns->port);
161
0
      else if(!Curl_httpsrr_applicable(data, dns->hinfo))
162
0
        infof(data, "HTTPS-RR %s:%u: not applicable",
163
0
              dns->hostname, dns->port);
164
0
      else {
165
0
        CURLcode result = Curl_httpsrr_print(&tmp, dns->hinfo);
166
0
        if(!result)
167
0
          infof(data, "HTTPS-RR %s:%u: %s",
168
0
                dns->hostname, dns->port, curlx_dyn_ptr(&tmp));
169
0
        else
170
0
          infof(data, "Error printing HTTPS-RR information");
171
0
      }
172
0
    }
173
0
#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
12.2k
{
188
12.2k
  struct cf_dns_ctx *ctx = cf->ctx;
189
12.2k
  timediff_t timeout_ms = Curl_timeleft_ms(data);
190
12.2k
  CURLcode result = CURLE_OK;
191
192
12.2k
  *pdns = NULL;
193
194
12.2k
  CURL_TRC_CF(data, cf, "[%s] cf_dns_start %s %s:%u",
195
12.2k
              Curl_resolv_query_str(ctx->dns_queries),
196
12.2k
              ctx->peer->unix_socket ? "unix-domain-socket" : "host",
197
12.2k
              ctx->peer->hostname, ctx->peer->port);
198
12.2k
  if(ctx->peer->unix_socket)
199
554
    ctx->dns_queries = CURL_DNSQ_A; /* treat it like an A resolve */
200
201
12.2k
  if(CURL_DNSQ_IS_ADDR(ctx->dns_queries)) {
202
12.2k
    if(Curl_is_ipv4addr(ctx->peer->hostname))
203
11.7k
      ctx->dns_queries |= CURL_DNSQ_A;
204
553
#ifdef USE_IPV6
205
553
    else if(ctx->peer->ipv6)
206
0
      ctx->dns_queries |= CURL_DNSQ_AAAA;
207
12.2k
#endif
208
209
12.2k
    result = Curl_resolv(data, ctx->peer, ctx->dns_queries, ctx->transport,
210
12.2k
                         (bool)ctx->for_proxy, timeout_ms,
211
12.2k
                         &ctx->resolv_id, pdns);
212
12.2k
  }
213
0
#ifdef USE_HTTPSRR
214
0
  else if(ctx->dns_queries == CURL_DNSQ_HTTPS) {
215
0
    result = Curl_resolv_https(data, ctx->peer, (bool)ctx->for_proxy,
216
0
                               timeout_ms, &ctx->resolv_id, pdns);
217
0
  }
218
0
#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
12.2k
  DEBUGASSERT(!result || !*pdns);
225
12.2k
  if(!result) { /* resolved right away, either sync or from dnscache */
226
12.2k
    DEBUGASSERT(*pdns);
227
12.2k
    return CURLE_OK;
228
12.2k
  }
229
24
  else if(result == CURLE_AGAIN) { /* async resolv in progress */
230
0
    return CURLE_OK;
231
0
  }
232
24
  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
24
  else {
239
24
    DEBUGASSERT(result);
240
24
    if(ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))
241
24
      failf(data, "Could not resolve: %s", ctx->peer->hostname);
242
24
    return result;
243
24
  }
244
12.2k
}
245
246
static bool cf_dns_ready_to_connect(struct Curl_cfilter *cf,
247
                                    struct Curl_easy *data)
248
12.9k
{
249
12.9k
  struct cf_dns_ctx *ctx = cf->ctx;
250
251
12.9k
  if(ctx->resolv_result)
252
0
    return TRUE;
253
12.9k
  else if(ctx->dns)
254
12.9k
    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
12.9k
}
286
287
static CURLcode cf_dns_connect(struct Curl_cfilter *cf,
288
                               struct Curl_easy *data,
289
                               bool *done)
290
12.9k
{
291
12.9k
  struct cf_dns_ctx *ctx = cf->ctx;
292
12.9k
  bool ip_query = (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA));
293
294
12.9k
  if(cf->connected) {
295
0
    *done = TRUE;
296
0
    return CURLE_OK;
297
0
  }
298
299
12.9k
  *done = FALSE;
300
12.9k
  if(!ctx->started) {
301
12.2k
    ctx->started = TRUE;
302
12.2k
    ctx->resolv_result = cf_dns_start(cf, data, &ctx->dns);
303
12.2k
  }
304
305
12.9k
  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
12.9k
  if(ctx->resolv_result && ip_query) {
311
    /* failing A|AAAA resolves is a hard failure. */
312
24
    CURL_TRC_CF(data, cf, "[%s] error resolving: %d",
313
24
                Curl_resolv_query_str(ctx->dns_queries),
314
24
                (int)ctx->resolv_result);
315
24
    return ctx->resolv_result;
316
24
  }
317
318
12.9k
  if(ctx->dns && !ctx->announced) {
319
12.2k
    ctx->announced = TRUE;
320
12.2k
    if((cf->sockindex == FIRSTSOCKET) && ip_query) {
321
12.2k
      cf->conn->bits.dns_resolved = TRUE;
322
12.2k
      Curl_pgrsTime(data, TIMER_NAMELOOKUP);
323
12.2k
    }
324
12.2k
    cf_dns_report(cf, data, ctx->dns);
325
12.2k
  }
326
327
  /* Delay connection sub-filters when we are still waiting for AAAA */
328
12.9k
  if(!cf_dns_ready_to_connect(cf, data)) {
329
0
    return CURLE_OK;
330
0
  }
331
332
12.9k
  if(cf->next && !cf->next->connected) {
333
12.9k
    bool sub_done;
334
12.9k
    CURLcode result = Curl_conn_cf_connect(cf->next, data, &sub_done);
335
12.9k
    if(result || !sub_done)
336
1.94k
      return result;
337
11.0k
    DEBUGASSERT(sub_done);
338
11.0k
  }
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
11.0k
  CURL_TRC_CF(data, cf, "connected filter chain below");
343
11.0k
  *done = TRUE;
344
11.0k
  cf->connected = TRUE;
345
11.0k
  Curl_resolv_destroy(data, ctx->resolv_id);
346
11.0k
  return CURLE_OK;
347
12.9k
}
348
349
static void cf_dns_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
350
12.3k
{
351
12.3k
  struct cf_dns_ctx *ctx = cf->ctx;
352
353
12.3k
  CURL_TRC_CF(data, cf, "destroy");
354
12.3k
  cf_dns_ctx_destroy(data, ctx);
355
12.3k
}
356
357
static CURLcode cf_dns_adjust_pollset(struct Curl_cfilter *cf,
358
                                      struct Curl_easy *data,
359
                                      struct easy_pollset *ps)
360
803
{
361
803
#ifdef USE_CURL_ASYNC
362
803
  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
803
    struct cf_dns_ctx *ctx = cf->ctx;
368
803
    if(!ctx->started) {
369
60
      CURL_TRC_CF(data, cf, "adjust_pollset, starting %s:%u queries=%s",
370
60
                  ctx->peer->hostname, ctx->peer->port,
371
60
                  Curl_resolv_query_str(ctx->dns_queries));
372
60
      ctx->started = TRUE;
373
60
      ctx->resolv_result = cf_dns_start(cf, data, &ctx->dns);
374
60
      if(ctx->resolv_result || ctx->dns) {
375
60
        Curl_multi_mark_dirty(data);
376
60
      }
377
60
    }
378
803
    return Curl_resolv_pollset(data, ps);
379
803
  }
380
#else
381
  (void)cf;
382
  (void)data;
383
  (void)ps;
384
#endif
385
0
  return CURLE_OK;
386
803
}
387
388
static CURLcode cf_dns_cntrl(struct Curl_cfilter *cf,
389
                             struct Curl_easy *data,
390
                             int event, int arg1, void *arg2)
391
24.5k
{
392
24.5k
  struct cf_dns_ctx *ctx = cf->ctx;
393
24.5k
  CURLcode result = CURLE_OK;
394
395
24.5k
  (void)arg1;
396
24.5k
  (void)arg2;
397
24.5k
  switch(event) {
398
1.32k
  case CF_CTRL_DATA_DONE:
399
1.32k
    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
1.25k
      Curl_dns_entry_unlink(data, &ctx->dns);
404
1.25k
    }
405
1.32k
    break;
406
23.2k
  default:
407
23.2k
    break;
408
24.5k
  }
409
24.5k
  return result;
410
24.5k
}
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
12.3k
{
436
12.3k
  struct Curl_cfilter *cf = NULL;
437
12.3k
  struct cf_dns_ctx *ctx;
438
12.3k
  CURLcode result = CURLE_OK;
439
440
12.3k
  (void)data;
441
12.3k
  ctx = cf_dns_ctx_create(data, peer, dns_queries, transport, for_proxy);
442
12.3k
  if(!ctx) {
443
0
    result = CURLE_OUT_OF_MEMORY;
444
0
    goto out;
445
0
  }
446
447
12.3k
  result = Curl_cf_create(&cf, &Curl_cft_dns, ctx);
448
449
12.3k
out:
450
12.3k
  *pcf = result ? NULL : cf;
451
12.3k
  if(result)
452
0
    cf_dns_ctx_destroy(data, ctx);
453
12.3k
  return result;
454
12.3k
}
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
12.2k
{
465
12.2k
  struct Curl_cfilter *cf = NULL;
466
12.2k
  bool for_proxy = FALSE;
467
12.2k
  CURLcode result;
468
469
12.2k
  if(!peer)
470
0
    return CURLE_FAILED_INIT;
471
12.2k
#ifndef CURL_DISABLE_PROXY
472
12.2k
  for_proxy = (peer == conn->socks_proxy.peer) ||
473
12.1k
              (peer == conn->http_proxy.peer);
474
12.2k
#endif
475
476
12.2k
  result = cf_dns_create(&cf, data, peer, dns_queries, transport, for_proxy);
477
12.2k
  if(result)
478
0
    goto out;
479
12.2k
  Curl_conn_cf_add(data, conn, sockindex, cf);
480
12.2k
out:
481
12.2k
  return result;
482
12.2k
}
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
98
{
496
98
  struct Curl_cfilter *cf;
497
98
  CURLcode result;
498
499
98
  result = cf_dns_create(&cf, data, peer, dns_queries, transport, FALSE);
500
98
  if(result)
501
0
    return result;
502
503
98
  Curl_conn_cf_insert_after(cf_at, cf);
504
98
  return CURLE_OK;
505
98
}
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
12.3k
{
514
12.3k
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
515
12.3k
  struct Curl_cfilter *cf_dns = NULL;
516
12.3k
  bool is_addr = CURL_DNSQ_IS_ADDR(dns_queries);
517
518
12.3k
  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
25.0k
  for(; cf && dns_queries; cf = cf->next) {
524
12.7k
    if(cf->cft == &Curl_cft_dns) {
525
100
      struct cf_dns_ctx *ctx = cf->ctx;
526
100
      cf_dns = cf;
527
100
      if(Curl_peer_same_destination(ctx->peer, peer)) {
528
        /* subtract queries already being scheduled/ongoing */
529
2
        dns_queries = (uint8_t)(~ctx->dns_queries & dns_queries);
530
2
        if(!dns_queries) { /* already there */
531
2
          return CURLE_OK;
532
2
        }
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
2
      }
543
100
    }
544
12.7k
  }
545
546
12.3k
  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
12.3k
    if(cf_dns)
550
98
      return cf_dns_insert_after(cf_dns, data, peer, dns_queries, transport);
551
12.2k
    else
552
12.2k
      return cf_dns_add(data, conn, sockindex, peer, dns_queries, transport);
553
12.3k
  }
554
0
  return CURLE_OK;
555
12.3k
}
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
12.3k
{
564
  /* should only have address query bits */
565
12.3k
  DEBUGASSERT(!(dns_queries & ~CURL_DNSQ_ADDR));
566
12.3k
  return cf_dns_add_resolve(data, conn, sockindex, peer,
567
12.3k
                            (dns_queries & CURL_DNSQ_ADDR), transport);
568
12.3k
}
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
12.3k
{
581
12.3k
  struct Curl_cfilter *cf = conn->cfilter[sockindex];
582
12.4k
  for(; cf; cf = cf->next) {
583
12.4k
    if(cf->cft == &Curl_cft_dns) {
584
12.4k
      struct cf_dns_ctx *ctx = cf->ctx;
585
12.4k
      if(Curl_peer_same_destination(ctx->peer, peer) &&
586
12.3k
         (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))) {
587
12.3k
        if(ctx->dns || ctx->resolv_result)
588
12.2k
          return ctx->resolv_result;
589
50
        return CURLE_AGAIN;
590
12.3k
      }
591
12.4k
    }
592
12.4k
  }
593
0
  return CURLE_FAILED_INIT; /* no one is resolving */
594
12.3k
}
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
24.4k
{
606
24.4k
  struct connectdata *conn = data->conn;
607
24.4k
  struct Curl_cfilter *cf = conn->cfilter[sockindex];
608
609
24.5k
  for(; cf; cf = cf->next) {
610
24.5k
    if(cf->cft == &Curl_cft_dns) {
611
24.5k
      struct cf_dns_ctx *ctx = cf->ctx;
612
24.5k
      if(Curl_peer_same_destination(ctx->peer, peer) &&
613
24.4k
         (ctx->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))) {
614
24.4k
        CURL_TRC_CF(data, cf, "get %uth result for %s:%u, family=%d, dns=%d",
615
24.4k
                    index, peer->hostname, peer->port, ai_family, !!ctx->dns);
616
24.4k
        if(ctx->resolv_result)
617
0
          return NULL;
618
24.4k
        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
24.4k
          if((ai_family == AF_INET) && !(ctx->dns_queries & CURL_DNSQ_A))
622
0
            return NULL;
623
24.4k
#ifdef USE_IPV6
624
24.4k
          if((ai_family == AF_INET6) && !(ctx->dns_queries & CURL_DNSQ_AAAA))
625
21
            return NULL;
626
24.4k
#endif
627
24.4k
          return Curl_addrinfo_get(ctx->dns->addr, ai_family, index);
628
24.4k
        }
629
0
        else
630
0
          return Curl_resolv_get_ai(data, ctx->resolv_id, ai_family, index);
631
24.4k
      }
632
24.5k
    }
633
24.5k
  }
634
0
  return NULL;
635
24.4k
}
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
0
{
643
0
  return cf_dns_add_resolve(data, conn, sockindex, peer,
644
0
                            CURL_DNSQ_HTTPS, conn->transport_wanted);
645
0
}
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
0
{
655
0
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
656
0
  for(; cf; cf = cf->next) {
657
0
    if(cf->cft == &Curl_cft_dns) {
658
0
      struct cf_dns_ctx *ctx = cf->ctx;
659
0
      if(Curl_peer_same_destination(ctx->peer, peer) &&
660
0
         (ctx->dns_queries & CURL_DNSQ_HTTPS)) {
661
0
        if(ctx->dns)
662
0
          return ctx->dns->hinfo;
663
0
        else
664
0
          return Curl_resolv_get_https(data, ctx->resolv_id);
665
0
      }
666
0
    }
667
0
  }
668
0
  return NULL;
669
0
}
670
671
bool Curl_conn_dns_resolved_https(struct Curl_easy *data,
672
                                  int8_t sockindex,
673
                                  struct Curl_peer *peer)
674
0
{
675
0
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
676
0
  for(; cf; cf = cf->next) {
677
0
    if(cf->cft == &Curl_cft_dns) {
678
0
      struct cf_dns_ctx *ctx = cf->ctx;
679
0
      if(Curl_peer_same_destination(ctx->peer, peer) &&
680
0
         (ctx->dns_queries & CURL_DNSQ_HTTPS)) {
681
0
        if(ctx->dns)
682
0
          return TRUE;
683
0
        else
684
0
          return Curl_resolv_knows_https(data, ctx->resolv_id);
685
0
      }
686
0
    }
687
0
  }
688
0
  return TRUE;
689
0
}
690
691
#endif /* USE_HTTPSRR */