Coverage Report

Created: 2026-09-04 07:15

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