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