Coverage Report

Created: 2026-09-01 06:58

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