Coverage Report

Created: 2026-05-30 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/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 "dnscache.h"
31
#include "httpsrr.h"
32
#include "curl_trc.h"
33
#include "progress.h"
34
#include "url.h"
35
#include "cf-dns.h"
36
37
38
struct cf_dns_ctx {
39
  struct Curl_dns_entry *dns;
40
  struct Curl_peer *peer;
41
  CURLcode resolv_result;
42
  uint32_t resolv_id;
43
  uint8_t dns_queries;
44
  uint8_t transport;
45
  BIT(started);
46
  BIT(announced);
47
  BIT(complete_resolve);
48
  BIT(for_proxy);
49
};
50
51
static struct cf_dns_ctx *cf_dns_ctx_create(struct Curl_easy *data,
52
                                            struct Curl_peer *peer,
53
                                            uint8_t dns_queries,
54
                                            uint8_t transport,
55
                                            bool for_proxy,
56
                                            bool complete_resolve)
57
0
{
58
0
  struct cf_dns_ctx *ctx;
59
60
0
  ctx = curlx_calloc(1, sizeof(*ctx));
61
0
  if(!ctx)
62
0
    return NULL;
63
64
0
  Curl_peer_link(&ctx->peer, peer);
65
0
  ctx->dns_queries = dns_queries;
66
0
  ctx->transport = transport;
67
0
  ctx->for_proxy = for_proxy;
68
0
  ctx->complete_resolve = complete_resolve;
69
70
0
  CURL_TRC_DNS(data, "created DNS filter for %s:%u, transport=%x, queries=%x",
71
0
               peer->hostname, peer->port, ctx->transport, ctx->dns_queries);
72
0
  return ctx;
73
0
}
74
75
static void cf_dns_ctx_destroy(struct Curl_easy *data,
76
                               struct cf_dns_ctx *ctx)
77
0
{
78
0
  if(ctx) {
79
0
    Curl_peer_unlink(&ctx->peer);
80
0
    Curl_dns_entry_unlink(data, &ctx->dns);
81
0
    curlx_free(ctx);
82
0
  }
83
0
}
84
85
#ifdef CURLVERBOSE
86
static void cf_dns_report_addr(struct Curl_easy *data,
87
                               struct dynbuf *tmp,
88
                               const char *label,
89
                               int ai_family,
90
                               const struct Curl_addrinfo *ai)
91
0
{
92
0
  char buf[MAX_IPADR_LEN];
93
0
  const char *sep = "";
94
0
  CURLcode result;
95
96
0
  curlx_dyn_reset(tmp);
97
0
  for(; ai; ai = ai->ai_next) {
98
0
    if(ai->ai_family == ai_family) {
99
0
      Curl_printable_address(ai, buf, sizeof(buf));
100
0
      result = curlx_dyn_addf(tmp, "%s%s", sep, buf);
101
0
      if(result) {
102
0
        infof(data, "too many IP, cannot show");
103
0
        return;
104
0
      }
105
0
      sep = ", ";
106
0
    }
107
0
  }
108
109
0
  infof(data, "%s%s", label,
110
0
        (curlx_dyn_len(tmp) ? curlx_dyn_ptr(tmp) : "(none)"));
111
0
}
112
113
static void cf_dns_report(struct Curl_cfilter *cf,
114
                          struct Curl_easy *data,
115
                          struct Curl_dns_entry *dns)
116
0
{
117
0
  struct cf_dns_ctx *ctx = cf->ctx;
118
0
  struct dynbuf tmp;
119
120
0
  if(!Curl_trc_is_verbose(data) ||
121
     /* ignore no name or numerical IP addresses */
122
0
     !dns->hostname[0] || Curl_host_is_ipnum(dns->hostname))
123
0
    return;
124
125
0
  if(ctx->peer->unix_socket) {
126
0
#ifdef USE_UNIX_SOCKETS
127
0
    CURL_TRC_CF(data, cf, "resolved unix://%s", ctx->peer->hostname);
128
#else
129
    DEBUGASSERT(0);
130
#endif
131
0
  }
132
0
  else {
133
0
    curlx_dyn_init(&tmp, 1024);
134
0
    infof(data, "Host %s:%u was resolved.", dns->hostname, dns->port);
135
0
#ifdef CURLRES_IPV6
136
0
    cf_dns_report_addr(data, &tmp, "IPv6: ", AF_INET6, dns->addr);
137
0
#endif
138
0
    cf_dns_report_addr(data, &tmp, "IPv4: ", AF_INET, dns->addr);
139
#ifdef USE_HTTPSRR
140
    if(!dns->hinfo)
141
      infof(data, "HTTPS-RR: -");
142
    else if(!Curl_httpsrr_applicable(data, dns->hinfo))
143
      infof(data, "HTTPS-RR: not applicable");
144
    else {
145
      CURLcode result = Curl_httpsrr_print(&tmp, dns->hinfo);
146
      if(!result)
147
        infof(data, "HTTPS-RR: %s", curlx_dyn_ptr(&tmp));
148
      else
149
        infof(data, "Error printing HTTPS-RR information");
150
    }
151
#endif
152
0
    curlx_dyn_free(&tmp);
153
0
  }
154
0
}
155
#else
156
#define cf_dns_report(x, y, z) Curl_nop_stmt
157
#endif
158
159
/*************************************************************
160
 * Resolve the address of the server or proxy
161
 *************************************************************/
162
static CURLcode cf_dns_start(struct Curl_cfilter *cf,
163
                             struct Curl_easy *data,
164
                             struct Curl_dns_entry **pdns)
165
0
{
166
0
  struct cf_dns_ctx *ctx = cf->ctx;
167
0
  timediff_t timeout_ms = Curl_timeleft_ms(data);
168
0
  CURLcode result;
169
170
0
  *pdns = NULL;
171
172
0
  CURL_TRC_CF(data, cf, "cf_dns_start %s %s:%u",
173
0
              ctx->peer->unix_socket ? "unix-domain-socket" : "host",
174
0
              ctx->peer->hostname, ctx->peer->port);
175
0
  if(ctx->peer->unix_socket)
176
0
    ctx->dns_queries = 0;
177
0
  else if(Curl_is_ipv4addr(ctx->peer->hostname))
178
0
    ctx->dns_queries |= CURL_DNSQ_A;
179
0
#ifdef USE_IPV6
180
0
  else if(ctx->peer->ipv6)
181
0
    ctx->dns_queries |= CURL_DNSQ_AAAA;
182
0
#endif
183
184
0
  result = Curl_resolv(data, ctx->peer, ctx->dns_queries, ctx->transport,
185
0
                       (bool)ctx->for_proxy, timeout_ms,
186
0
                       &ctx->resolv_id, pdns);
187
0
  DEBUGASSERT(!result || !*pdns);
188
0
  if(!result) { /* resolved right away, either sync or from dnscache */
189
0
    DEBUGASSERT(*pdns);
190
0
    return CURLE_OK;
191
0
  }
192
0
  else if(result == CURLE_AGAIN) { /* async resolv in progress */
193
0
    return CURLE_OK;
194
0
  }
195
0
  else if(result == CURLE_OPERATION_TIMEDOUT) { /* took too long */
196
0
    failf(data, "Failed to resolve '%s' with timeout after %"
197
0
          FMT_TIMEDIFF_T " ms", ctx->peer->hostname,
198
0
          curlx_ptimediff_ms(Curl_pgrs_now(data),
199
0
                             &data->progress.t_startsingle));
200
0
    return CURLE_OPERATION_TIMEDOUT;
201
0
  }
202
0
  else {
203
0
    DEBUGASSERT(result);
204
0
    failf(data, "Could not resolve: %s", ctx->peer->hostname);
205
0
    return result;
206
0
  }
207
0
}
208
209
0
#define CURL_HEV3_RESOLVE_DELAY_MS    50
210
211
static bool cf_dns_ready_to_connect(struct Curl_cfilter *cf,
212
                                    struct Curl_easy *data)
213
0
{
214
0
  struct cf_dns_ctx *ctx = cf->ctx;
215
216
0
  if(ctx->resolv_result)
217
0
    return TRUE;
218
0
  else if(ctx->dns)
219
0
    return TRUE;
220
0
#ifdef USE_CURL_ASYNC
221
0
  else {
222
    /* We want AAAA answer as we prefer ipv6. If a sub-filter desires
223
    * HTTPS-RR, we check for that query as well. */
224
0
    uint8_t wanted_answers = CURL_DNSQ_AAAA;
225
0
    if(Curl_conn_cf_wants_httpsrr(cf, data))
226
0
      wanted_answers |= CURL_DNSQ_HTTPS;
227
228
    /* Note: if a query was never started, it is considered to have
229
     * an answer (e.g. a negative one). */
230
0
    if(Curl_resolv_has_answers(data, ctx->resolv_id, wanted_answers))
231
0
      return TRUE;
232
    /* If the wanted answers are not available after a delay,
233
     * we let the connect attempts start anyway. */
234
0
    return Curl_resolv_elapsed_ms(data, ctx->resolv_id) >=
235
0
           CURL_HEV3_RESOLVE_DELAY_MS;
236
0
  }
237
#else
238
  (void)data;
239
  DEBUGASSERT(0); /* We should not come here */
240
  return FALSE;
241
#endif /* USE_CURL_ASYNC */
242
0
}
243
244
static CURLcode cf_dns_connect(struct Curl_cfilter *cf,
245
                               struct Curl_easy *data,
246
                               bool *done)
247
0
{
248
0
  struct cf_dns_ctx *ctx = cf->ctx;
249
250
0
  if(cf->connected) {
251
0
    *done = TRUE;
252
0
    return CURLE_OK;
253
0
  }
254
255
0
  *done = FALSE;
256
0
  if(!ctx->started) {
257
0
    ctx->started = TRUE;
258
0
    ctx->resolv_result = cf_dns_start(cf, data, &ctx->dns);
259
0
  }
260
261
0
  if(!ctx->dns && !ctx->resolv_result) {
262
0
    ctx->resolv_result =
263
0
      Curl_resolv_take_result(data, ctx->resolv_id, &ctx->dns);
264
0
  }
265
266
0
  if(ctx->resolv_result) {
267
0
    CURL_TRC_CF(data, cf, "error resolving: %d", ctx->resolv_result);
268
0
    return ctx->resolv_result;
269
0
  }
270
271
0
  if(ctx->dns && !ctx->announced) {
272
0
    ctx->announced = TRUE;
273
0
    if(cf->sockindex == FIRSTSOCKET) {
274
0
      cf->conn->bits.dns_resolved = TRUE;
275
0
      Curl_pgrsTime(data, TIMER_NAMELOOKUP);
276
0
    }
277
0
    cf_dns_report(cf, data, ctx->dns);
278
0
  }
279
280
0
  if(!cf_dns_ready_to_connect(cf, data)) {
281
0
    return CURLE_OK;
282
0
  }
283
284
0
  if(cf->next && !cf->next->connected) {
285
0
    bool sub_done;
286
0
    CURLcode result = Curl_conn_cf_connect(cf->next, data, &sub_done);
287
0
    if(result || !sub_done)
288
0
      return result;
289
0
    DEBUGASSERT(sub_done);
290
0
  }
291
292
  /* sub filter chain is connected */
293
0
  CURL_TRC_CF(data, cf, "connected filter chain below");
294
0
  if(ctx->complete_resolve && !ctx->dns && !ctx->resolv_result) {
295
    /* This filter only connects when it has resolved everything. */
296
0
    CURL_TRC_CF(data, cf, "delay connect until resolve complete");
297
0
    return CURLE_OK;
298
0
  }
299
0
  *done = TRUE;
300
0
  cf->connected = TRUE;
301
0
  Curl_resolv_destroy(data, ctx->resolv_id);
302
0
  return CURLE_OK;
303
0
}
304
305
static void cf_dns_destroy(struct Curl_cfilter *cf, struct Curl_easy *data)
306
0
{
307
0
  struct cf_dns_ctx *ctx = cf->ctx;
308
309
0
  CURL_TRC_CF(data, cf, "destroy");
310
0
  cf_dns_ctx_destroy(data, ctx);
311
0
}
312
313
static void cf_dns_close(struct Curl_cfilter *cf, struct Curl_easy *data)
314
0
{
315
0
  cf->connected = FALSE;
316
0
  if(cf->next)
317
0
    cf->next->cft->do_close(cf->next, data);
318
0
}
319
320
static CURLcode cf_dns_adjust_pollset(struct Curl_cfilter *cf,
321
                                      struct Curl_easy *data,
322
                                      struct easy_pollset *ps)
323
0
{
324
0
#ifdef USE_CURL_ASYNC
325
0
  if(!cf->connected)
326
0
    return Curl_resolv_pollset(data, ps);
327
#else
328
  (void)cf;
329
  (void)data;
330
  (void)ps;
331
#endif
332
0
  return CURLE_OK;
333
0
}
334
335
static CURLcode cf_dns_cntrl(struct Curl_cfilter *cf,
336
                             struct Curl_easy *data,
337
                             int event, int arg1, void *arg2)
338
0
{
339
0
  struct cf_dns_ctx *ctx = cf->ctx;
340
0
  CURLcode result = CURLE_OK;
341
342
0
  (void)arg1;
343
0
  (void)arg2;
344
0
  switch(event) {
345
0
  case CF_CTRL_DATA_DONE:
346
0
    if(ctx->dns) {
347
      /* Should only come here when the connect attempt failed and
348
       * `data` is giving up on it. On a successful connect, we already
349
       * unlinked the DNS entry. */
350
0
      Curl_dns_entry_unlink(data, &ctx->dns);
351
0
    }
352
0
    break;
353
0
  default:
354
0
    break;
355
0
  }
356
0
  return result;
357
0
}
358
359
struct Curl_cftype Curl_cft_dns = {
360
  "DNS",
361
  CF_TYPE_SETUP,
362
  CURL_LOG_LVL_NONE,
363
  cf_dns_destroy,
364
  cf_dns_connect,
365
  cf_dns_close,
366
  Curl_cf_def_shutdown,
367
  cf_dns_adjust_pollset,
368
  Curl_cf_def_data_pending,
369
  Curl_cf_def_send,
370
  Curl_cf_def_recv,
371
  cf_dns_cntrl,
372
  Curl_cf_def_conn_is_alive,
373
  Curl_cf_def_conn_keep_alive,
374
  Curl_cf_def_query,
375
};
376
377
static CURLcode cf_dns_create(struct Curl_cfilter **pcf,
378
                              struct Curl_easy *data,
379
                              struct Curl_peer *peer,
380
                              uint8_t dns_queries,
381
                              uint8_t transport,
382
                              bool for_proxy,
383
                              bool complete_resolve)
384
0
{
385
0
  struct Curl_cfilter *cf = NULL;
386
0
  struct cf_dns_ctx *ctx;
387
0
  CURLcode result = CURLE_OK;
388
389
0
  (void)data;
390
0
  ctx = cf_dns_ctx_create(data, peer, dns_queries, transport,
391
0
                          for_proxy, complete_resolve);
392
0
  if(!ctx) {
393
0
    result = CURLE_OUT_OF_MEMORY;
394
0
    goto out;
395
0
  }
396
397
0
  result = Curl_cf_create(&cf, &Curl_cft_dns, ctx);
398
399
0
out:
400
0
  *pcf = result ? NULL : cf;
401
0
  if(result)
402
0
    cf_dns_ctx_destroy(data, ctx);
403
0
  return result;
404
0
}
405
406
/* Adds a "resolv" filter at the top of the connection's filter chain.
407
 * The filter will resolve the peer on the first connect attempt. */
408
CURLcode Curl_cf_dns_add(struct Curl_easy *data,
409
                         struct connectdata *conn,
410
                         int sockindex,
411
                         struct Curl_peer *peer,
412
                         uint8_t dns_queries,
413
                         uint8_t transport)
414
0
{
415
0
  struct Curl_cfilter *cf = NULL;
416
0
  bool for_proxy = FALSE;
417
0
  CURLcode result;
418
419
0
  if(!peer)
420
0
    return CURLE_FAILED_INIT;
421
0
#ifndef CURL_DISABLE_PROXY
422
0
  for_proxy = (peer == conn->socks_proxy.peer) ||
423
0
              (peer == conn->http_proxy.peer);
424
0
#endif
425
426
0
  result = cf_dns_create(&cf, data, peer, dns_queries, transport,
427
0
                         for_proxy, FALSE);
428
0
  if(result)
429
0
    goto out;
430
0
  Curl_conn_cf_add(data, conn, sockindex, cf);
431
0
out:
432
0
  return result;
433
0
}
434
435
/* Insert a new "resolv" filter directly after `cf`. It will
436
 * start a DNS resolve for the given peer on the
437
 * first connect attempt.
438
 * See socks.c on how this is used to make a non-blocking DNS
439
 * resolve during connect.
440
 */
441
CURLcode Curl_cf_dns_insert_after(struct Curl_cfilter *cf_at,
442
                                  struct Curl_easy *data,
443
                                  uint8_t dns_queries,
444
                                  struct Curl_peer *peer,
445
                                  uint8_t transport,
446
                                  bool complete_resolve)
447
0
{
448
0
  struct Curl_cfilter *cf;
449
0
  CURLcode result;
450
451
0
  result = cf_dns_create(&cf, data, peer, dns_queries, transport,
452
0
                         FALSE, complete_resolve);
453
0
  if(result)
454
0
    return result;
455
456
0
  Curl_conn_cf_insert_after(cf_at, cf);
457
0
  return CURLE_OK;
458
0
}
459
460
/* Return the resolv result from the first "resolv" filter, starting
461
 * the given filter `cf` downwards.
462
 */
463
static CURLcode cf_dns_result(struct Curl_cfilter *cf)
464
0
{
465
0
  for(; cf; cf = cf->next) {
466
0
    if(cf->cft == &Curl_cft_dns) {
467
0
      struct cf_dns_ctx *ctx = cf->ctx;
468
0
      if(ctx->dns || ctx->resolv_result)
469
0
        return ctx->resolv_result;
470
0
      return CURLE_AGAIN;
471
0
    }
472
0
  }
473
0
  return CURLE_FAILED_INIT;
474
0
}
475
476
/* Return the result of the DNS resolution. Searches for a "resolv"
477
 * filter from the top of the filter chain down. Returns
478
 * - CURLE_AGAIN when not done yet
479
 * - CURLE_OK when DNS was successfully resolved
480
 * - CURLR_FAILED_INIT when no resolv filter was found
481
 * - error returned by the DNS resolv
482
 */
483
CURLcode Curl_conn_dns_result(struct connectdata *conn, int sockindex)
484
0
{
485
0
  return cf_dns_result(conn->cfilter[sockindex]);
486
0
}
487
488
static const struct Curl_addrinfo *cf_dns_get_nth_ai(
489
  struct Curl_cfilter *cf,
490
  const struct Curl_addrinfo *ai,
491
  int ai_family, unsigned int index)
492
0
{
493
0
  struct cf_dns_ctx *ctx = cf->ctx;
494
0
  unsigned int i = 0;
495
496
0
  if((ai_family == AF_INET) && !(ctx->dns_queries & CURL_DNSQ_A))
497
0
    return NULL;
498
0
#ifdef USE_IPV6
499
0
  if((ai_family == AF_INET6) && !(ctx->dns_queries & CURL_DNSQ_AAAA))
500
0
    return NULL;
501
0
#endif
502
0
  for(i = 0; ai; ai = ai->ai_next) {
503
0
    if(ai->ai_family == ai_family) {
504
0
      if(i == index)
505
0
        return ai;
506
0
      ++i;
507
0
    }
508
0
  }
509
0
  return NULL;
510
0
}
511
512
/* Return the addrinfo at `index` for the given `family` from the
513
 * first "resolve" filter underneath `cf`. If the DNS resolving is
514
 * not done yet or if no address for the family exists, returns NULL.
515
 */
516
const struct Curl_addrinfo *Curl_cf_dns_get_ai(struct Curl_cfilter *cf,
517
                                               struct Curl_easy *data,
518
                                               int ai_family,
519
                                               unsigned int index)
520
0
{
521
0
  (void)data;
522
0
  for(; cf; cf = cf->next) {
523
0
    if(cf->cft == &Curl_cft_dns) {
524
0
      struct cf_dns_ctx *ctx = cf->ctx;
525
0
      if(ctx->resolv_result)
526
0
        return NULL;
527
0
      else if(ctx->dns)
528
0
        return cf_dns_get_nth_ai(cf, ctx->dns->addr, ai_family, index);
529
0
      else
530
0
        return Curl_resolv_get_ai(data, ctx->resolv_id, ai_family, index);
531
0
    }
532
0
  }
533
0
  return NULL;
534
0
}
535
536
/* Return the addrinfo at `index` for the given `family` from the
537
 * first "resolve" filter at the connection. If the DNS resolving is
538
 * not done yet or if no address for the family exists, returns NULL.
539
 */
540
const struct Curl_addrinfo *Curl_conn_dns_get_ai(struct Curl_easy *data,
541
                                                 int sockindex, int ai_family,
542
                                                 unsigned int index)
543
0
{
544
0
  struct connectdata *conn = data->conn;
545
0
  return Curl_cf_dns_get_ai(conn->cfilter[sockindex], data, ai_family, index);
546
0
}
547
548
#ifdef USE_HTTPSRR
549
/* Return the HTTPS-RR info from the first "resolve" filter at the
550
 * connection. If the DNS resolving is not done yet or if there
551
 * is no HTTPS-RR info, returns NULL.
552
 */
553
const struct Curl_https_rrinfo *Curl_conn_dns_get_https(struct Curl_easy *data,
554
                                                        int sockindex)
555
{
556
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
557
  for(; cf; cf = cf->next) {
558
    if(cf->cft == &Curl_cft_dns) {
559
      struct cf_dns_ctx *ctx = cf->ctx;
560
      if(ctx->dns)
561
        return ctx->dns->hinfo;
562
      else
563
        return Curl_resolv_get_https(data, ctx->resolv_id);
564
    }
565
  }
566
  return NULL;
567
}
568
569
bool Curl_conn_dns_resolved_https(struct Curl_easy *data, int sockindex)
570
{
571
  struct Curl_cfilter *cf = data->conn->cfilter[sockindex];
572
  for(; cf; cf = cf->next) {
573
    if(cf->cft == &Curl_cft_dns) {
574
      struct cf_dns_ctx *ctx = cf->ctx;
575
      if(ctx->dns)
576
        return TRUE;
577
      else
578
        return Curl_resolv_knows_https(data, ctx->resolv_id);
579
    }
580
  }
581
  return FALSE;
582
}
583
584
#endif /* USE_HTTPSRR */