Coverage Report

Created: 2026-09-14 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vdns/asyn-thrdd.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
/***********************************************************************
27
 * Only for threaded name resolves builds
28
 **********************************************************************/
29
#ifdef USE_RESOLV_THREADED
30
31
#ifdef HAVE_NETINET_IN_H
32
#include <netinet/in.h>
33
#endif
34
#ifdef HAVE_NETDB_H
35
#include <netdb.h>
36
#endif
37
#ifdef HAVE_ARPA_INET_H
38
#include <arpa/inet.h>
39
#endif
40
#ifdef __VMS
41
#include <in.h>
42
#include <inet.h>
43
#endif
44
45
#ifdef HAVE_GETADDRINFO
46
0
#define RESOLVER_ENOMEM  EAI_MEMORY  /* = WSA_NOT_ENOUGH_MEMORY on Windows */
47
#else
48
#define RESOLVER_ENOMEM  SOCKENOMEM
49
#endif
50
51
#include "urldata.h"
52
#include "cfilters.h"
53
#include "curl_addrinfo.h"
54
#include "curl_trc.h"
55
#include "url.h"
56
#include "multiif.h"
57
#include "curl_threads.h"
58
#include "progress.h"
59
#include "rand.h"
60
#include "select.h"
61
#include "thrdqueue.h"
62
#include "vdns/hostip.h"
63
#include "vdns/httpsrr.h"
64
#include "curlx/strparse.h"
65
#include "curlx/wait.h"
66
67
#ifdef USE_ARES
68
#include <ares.h>
69
#ifdef USE_HTTPSRR
70
#define USE_HTTPSRR_ARES  /* the combo */
71
#endif
72
#endif
73
74
75
/*
76
 * Curl_async_global_init()
77
 * Called from curl_global_init() to initialize global resolver environment.
78
 * Does nothing here.
79
 */
80
int Curl_async_global_init(void)
81
1
{
82
#if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
83
  if(ares_library_init(ARES_LIB_INIT_ALL)) {
84
    return CURLE_FAILED_INIT;
85
  }
86
#endif
87
1
  return CURLE_OK;
88
1
}
89
90
/*
91
 * Curl_async_global_cleanup()
92
 * Called from curl_global_cleanup() to destroy global resolver environment.
93
 * Does nothing here.
94
 */
95
void Curl_async_global_cleanup(void)
96
0
{
97
#if defined(USE_ARES) && defined(CARES_HAVE_ARES_LIBRARY_INIT)
98
  ares_library_cleanup();
99
#endif
100
0
}
101
102
#ifdef CURLVERBOSE
103
#define CURL_ASYN_ITEM_DESC_LEN   64
104
0
#define async_item_description(x)   (x)->description
105
#else
106
#define async_item_description(x)   NULL
107
#endif
108
109
struct async_thrdd_item {
110
  struct Curl_addrinfo *res;
111
#ifdef CURLVERBOSE
112
  char description[CURL_ASYN_ITEM_DESC_LEN];
113
#endif
114
  int sockerr;
115
  uint32_t mid;
116
  uint32_t resolv_id;
117
  uint16_t port;
118
  uint8_t transport;
119
  uint8_t dns_queries;
120
  BIT(negative); /* resolver answered that the name does not exist */
121
#ifdef DEBUGBUILD
122
  uint32_t delay_ms;
123
  uint32_t delay_fail_ms;
124
  BIT(dbg_negative);
125
#endif
126
  char hostname[1];
127
};
128
129
/* Give up reference to add_ctx */
130
static void async_thrdd_item_destroy(struct async_thrdd_item *item)
131
0
{
132
0
  if(item) {
133
0
    if(item->res)
134
0
      Curl_freeaddrinfo(item->res);
135
0
    curlx_free(item);
136
0
  }
137
0
}
138
139
/* Initialize context for threaded resolver */
140
static struct async_thrdd_item *async_thrdd_item_create(
141
  struct Curl_easy *data,
142
  uint32_t resolv_id, uint8_t dns_queries,
143
  const char *hostname, uint16_t port,
144
  uint8_t transport)
145
0
{
146
0
  size_t hostlen = strlen(hostname);
147
0
  struct async_thrdd_item *item;
148
149
0
  item = curlx_calloc(1, sizeof(*item) + hostlen);
150
0
  if(!item)
151
0
    return NULL;
152
153
0
  if(hostlen) /* NUL byte of name already in struct size */
154
0
    memcpy(item->hostname, hostname, hostlen);
155
0
  item->mid = data->mid;
156
0
  item->resolv_id = resolv_id;
157
0
  item->dns_queries = dns_queries;
158
0
  item->port = port;
159
0
  item->transport = transport;
160
161
0
#ifdef CURLVERBOSE
162
0
  curl_msnprintf(item->description, sizeof(item->description),
163
0
                 "[%" FMT_OFF_T "/%u] %s %s:%u",
164
0
                 data->id, item->resolv_id,
165
0
                 Curl_resolv_query_str(dns_queries),
166
0
                 item->hostname, item->port);
167
0
#endif
168
169
0
#ifdef DEBUGBUILD
170
0
  {
171
0
    const char *p = getenv("CURL_DBG_RESOLV_DELAY");
172
0
    if(p) {
173
0
      curl_off_t l;
174
0
      if(!curlx_str_number(&p, &l, UINT32_MAX)) {
175
0
        item->delay_ms = (uint32_t)l;
176
0
      }
177
0
    }
178
0
    p = getenv("CURL_DBG_RESOLV_FAIL_DELAY");
179
0
    if(p) {
180
0
      curl_off_t l;
181
0
      if(!curlx_str_number(&p, &l, UINT32_MAX)) {
182
0
        unsigned char c = 0;
183
0
        Curl_rand_bytes(data, FALSE, &c, 1);
184
0
        item->delay_fail_ms = (uint32_t)l + c;
185
0
      }
186
0
    }
187
0
    if(getenv("CURL_DBG_RESOLV_FAIL_NEGATIVE"))
188
0
      item->dbg_negative = TRUE;
189
0
  }
190
0
#endif
191
192
0
  return item;
193
0
}
194
195
#ifdef USE_HTTPSRR_ARES
196
197
static void async_thrdd_rr_done(void *user_data, ares_status_t status,
198
                                size_t timeouts,
199
                                const ares_dns_record_t *dnsrec)
200
{
201
  struct Curl_resolv_async *async = user_data;
202
  struct async_thrdd_ctx *thrdd = async ? &async->thrdd : NULL;
203
204
  (void)timeouts;
205
  if(!thrdd)
206
    return;
207
208
  async->dns_responses |= CURL_DNSQ_HTTPS;
209
  async->queries_ongoing--;
210
  async->done = !async->queries_ongoing;
211
  if((ARES_SUCCESS == status) && dnsrec)
212
    async->result = Curl_httpsrr_from_ares(dnsrec, &async->httpsrr);
213
}
214
215
static CURLcode async_rr_start(struct Curl_easy *data,
216
                               struct Curl_resolv_async *async)
217
{
218
  struct async_thrdd_ctx *thrdd = &async->thrdd;
219
  char *https_name = NULL;
220
  int status;
221
  CURLcode result = CURLE_OK;
222
223
  DEBUGASSERT(!thrdd->rr.channel);
224
  if(async->peer->port != 443) {
225
    https_name = curl_maprintf("_%u._https.%s",
226
                               async->peer->port, async->peer->hostname);
227
    if(!https_name) {
228
      result = CURLE_OUT_OF_MEMORY;
229
      goto out;
230
    }
231
  }
232
  status = ares_init_options(&thrdd->rr.channel, NULL, 0);
233
  if(status != ARES_SUCCESS) {
234
    thrdd->rr.channel = NULL;
235
    result = CURLE_FAILED_INIT;
236
    goto out;
237
  }
238
#ifdef DEBUGBUILD
239
  if(getenv("CURL_DNS_SERVER")) {
240
    const char *servers = getenv("CURL_DNS_SERVER");
241
    status = ares_set_servers_ports_csv(thrdd->rr.channel, servers);
242
    if(status) {
243
      result = CURLE_FAILED_INIT;
244
      goto out;
245
    }
246
  }
247
#endif
248
249
  async->queries_ongoing++;
250
  ares_query_dnsrec(thrdd->rr.channel,
251
                    https_name ? https_name : async->peer->hostname,
252
                    ARES_CLASS_IN, ARES_REC_TYPE_HTTPS,
253
                    async_thrdd_rr_done, async, NULL);
254
  CURL_TRC_DNS(data, "[HTTPS] query records for %s",
255
               https_name ? https_name : async->peer->hostname);
256
257
out:
258
  curlx_free(https_name);
259
  return result;
260
}
261
#endif
262
263
void Curl_async_thrdd_shutdown(struct Curl_easy *data,
264
                               struct Curl_resolv_async *async)
265
0
{
266
0
  Curl_async_thrdd_destroy(data, async);
267
0
}
268
269
struct async_thrdd_match_ctx {
270
  uint32_t mid;
271
  uint32_t resolv_id;
272
};
273
274
static bool async_thrdd_match_item(void *qitem, void *match_data)
275
0
{
276
0
  const struct async_thrdd_match_ctx *ctx = match_data;
277
0
  struct async_thrdd_item *item = qitem;
278
0
  return (item->mid == ctx->mid) && (item->resolv_id == ctx->resolv_id);
279
0
}
280
281
void Curl_async_thrdd_destroy(struct Curl_easy *data,
282
                              struct Curl_resolv_async *async)
283
0
{
284
0
  (void)data;
285
0
  if(async->queries_ongoing && !async->done &&
286
0
     data->multi && data->multi->resolv_thrdq) {
287
    /* Remove any resolve items still queued */
288
0
    struct async_thrdd_match_ctx mctx;
289
0
    mctx.mid = data->mid;
290
0
    mctx.resolv_id = async->id;
291
0
    Curl_thrdq_clear(data->multi->resolv_thrdq,
292
0
                     async_thrdd_match_item, &mctx);
293
0
  }
294
#ifdef USE_HTTPSRR_ARES
295
  if(async->thrdd.rr.channel) {
296
    ares_destroy(async->thrdd.rr.channel);
297
    async->thrdd.rr.channel = NULL;
298
  }
299
#endif
300
0
  async_thrdd_item_destroy(async->thrdd.res_A);
301
0
  async->thrdd.res_A = NULL;
302
0
  async_thrdd_item_destroy(async->thrdd.res_AAAA);
303
0
  async->thrdd.res_AAAA = NULL;
304
0
}
305
306
/*
307
 * Waits for a resolve to finish. This function should be avoided since using
308
 * this risk getting the multi interface to "hang".
309
 */
310
CURLcode Curl_async_await(struct Curl_easy *data, uint32_t resolv_id,
311
                          struct Curl_dns_entry **pdns)
312
0
{
313
0
  struct Curl_resolv_async *async = Curl_async_get(data, resolv_id);
314
0
  struct async_thrdd_ctx *thrdd = async ? &async->thrdd : NULL;
315
0
  timediff_t milli, ms;
316
0
  CURLcode result = CURLE_AGAIN;
317
318
0
  if(!thrdd)
319
0
    return CURLE_FAILED_INIT;
320
321
0
  while(result == CURLE_AGAIN) {
322
0
    while(async->queries_ongoing && !async->done) {
323
0
      Curl_async_thrdd_multi_process(data->multi);
324
0
      if(async->done)
325
0
        break;
326
327
0
      ms = curlx_ptimediff_ms(Curl_pgrs_now(data), &async->start);
328
0
      if(ms < 3)
329
0
        milli = 0;
330
0
      else if(ms <= 50)
331
0
        milli = ms / 3;
332
0
      else if(ms <= 250)
333
0
        milli = 50;
334
0
      else
335
0
        milli = 200;
336
0
      CURL_TRC_DNS(data, "await, waiting %" FMT_TIMEDIFF_T "ms", milli);
337
0
      curlx_wait_ms(milli);
338
0
    }
339
0
    result = Curl_async_take_result(data, async, pdns);
340
0
  }
341
0
  return result;
342
0
}
343
344
#ifdef HAVE_GETADDRINFO
345
346
/* Was the getaddrinfo() failure an authoritative negative answer,
347
   i.e. the resolver responded that the name (or its data) does not
348
   exist? Transient failures like EAI_AGAIN and local troubles must
349
   not count as negative answers. */
350
static bool gai_negative(int rc)
351
0
{
352
0
  switch(rc) {
353
0
#ifdef EAI_NONAME
354
0
  case EAI_NONAME:
355
0
#endif
356
0
#if defined(EAI_NODATA) && \
357
0
  (!defined(EAI_NONAME) || (EAI_NODATA != EAI_NONAME))
358
0
  case EAI_NODATA:
359
0
#endif
360
0
    return TRUE;
361
0
  default:
362
0
    return FALSE;
363
0
  }
364
0
}
365
366
/* Process the item, using Curl_getaddrinfo_ex() */
367
static void async_thrdd_item_process(void *arg)
368
0
{
369
0
  struct async_thrdd_item *item = arg;
370
0
  struct addrinfo hints;
371
0
  char service[12];
372
0
  int pf = PF_INET;
373
0
  int rc;
374
375
0
#ifdef DEBUGBUILD
376
0
  if(item->delay_ms) {
377
0
    curlx_wait_ms(item->delay_ms);
378
0
  }
379
0
  if(item->delay_fail_ms) {
380
0
    curlx_wait_ms(item->delay_fail_ms);
381
0
    item->negative = item->dbg_negative;
382
0
    return;
383
0
  }
384
0
#endif
385
386
0
  memset(&hints, 0, sizeof(hints));
387
0
#ifdef CURLRES_IPV6
388
0
  if(item->dns_queries & CURL_DNSQ_AAAA) {
389
0
    pf = (item->dns_queries & CURL_DNSQ_A) ? PF_UNSPEC : PF_INET6;
390
0
  }
391
0
#endif
392
0
  hints.ai_family = pf;
393
0
  hints.ai_socktype = Curl_socktype_for_transport(item->transport);
394
0
  hints.ai_protocol = Curl_protocol_for_transport(item->transport);
395
#ifdef __APPLE__
396
  /* If we leave `ai_flags == 0` then macOS is looking for IPV4MAPPED
397
   * when doing AAAA queries. We do not want this "help". */
398
  hints.ai_flags = AI_ADDRCONFIG;
399
#endif
400
401
0
  curl_msnprintf(service, sizeof(service), "%u", item->port);
402
0
#ifdef AI_NUMERICSERV
403
0
  hints.ai_flags |= AI_NUMERICSERV;
404
0
#endif
405
406
0
  rc = Curl_getaddrinfo_ex(item->hostname, service, &hints, &item->res);
407
0
  if(rc) {
408
0
    item->sockerr = SOCKERRNO ? SOCKERRNO : rc;
409
0
    if(item->sockerr == 0)
410
0
      item->sockerr = RESOLVER_ENOMEM;
411
0
    item->negative = gai_negative(rc);
412
0
  }
413
0
  else {
414
0
    Curl_addrinfo_set_port(item->res, item->port);
415
0
  }
416
0
}
417
418
#else /* HAVE_GETADDRINFO */
419
420
/* Process the item, using Curl_ipv4_resolve_r() */
421
static void async_thrdd_item_process(void *arg)
422
{
423
  struct async_thrdd_item *item = arg;
424
425
#ifdef DEBUGBUILD
426
  if(item->delay_ms) {
427
    curlx_wait_ms(item->delay_ms);
428
  }
429
  if(item->delay_fail_ms) {
430
    curlx_wait_ms(item->delay_fail_ms);
431
    item->negative = item->dbg_negative;
432
    return;
433
  }
434
#endif
435
  item->res = Curl_ipv4_resolve_r(item->hostname, item->port);
436
  if(!item->res) {
437
    item->sockerr = SOCKERRNO;
438
    if(item->sockerr == 0)
439
      item->sockerr = RESOLVER_ENOMEM;
440
    /* this resolver cannot tell a transient failure from an
441
       authoritative negative answer, treat it as before */
442
    item->negative = TRUE;
443
  }
444
}
445
446
#endif /* HAVE_GETADDRINFO */
447
448
#ifdef ENABLE_INTERNAL_WAKEUP
449
static void async_thrdd_event(const struct curl_thrdq *tqueue,
450
                              Curl_thrdq_event ev,
451
                              void *user_data)
452
0
{
453
0
  struct Curl_multi *multi = user_data;
454
0
  (void)tqueue;
455
0
  switch(ev) {
456
0
  case CURL_THRDQ_EV_ITEM_DONE:
457
0
    Curl_multi_wakeup_internal(multi);
458
0
    break;
459
0
  default:
460
0
    break;
461
0
  }
462
0
}
463
#else
464
#define async_thrdd_event   NULL
465
#endif
466
467
static void async_thrdd_item_free(void *item)
468
0
{
469
0
  async_thrdd_item_destroy(item);
470
0
}
471
472
/* Create a thread queue for processing resolv items */
473
CURLcode Curl_async_thrdd_multi_init(struct Curl_multi *multi,
474
                                     uint32_t min_threads,
475
                                     uint32_t max_threads,
476
                                     uint32_t idle_time_ms)
477
12.4k
{
478
12.4k
  CURLcode result;
479
12.4k
  DEBUGASSERT(!multi->resolv_thrdq);
480
12.4k
  result = Curl_thrdq_create(&multi->resolv_thrdq, "DNS",
481
12.4k
                             min_threads, max_threads, idle_time_ms,
482
12.4k
                             async_thrdd_item_free,
483
12.4k
                             async_thrdd_item_process,
484
12.4k
                             async_thrdd_event,
485
12.4k
                             multi);
486
12.4k
#ifdef DEBUGBUILD
487
12.4k
  if(!result) {
488
12.4k
    const char *p = getenv("CURL_DBG_RESOLV_MAX_THREADS");
489
12.4k
    if(p) {
490
0
      curl_off_t l;
491
0
      if(!curlx_str_number(&p, &l, UINT32_MAX)) {
492
0
        result = Curl_async_thrdd_multi_set_props(
493
0
          multi, min_threads, (uint32_t)l, idle_time_ms);
494
0
      }
495
0
    }
496
12.4k
  }
497
12.4k
#endif
498
12.4k
  return result;
499
12.4k
}
500
501
/* Tear down the thread queue, joining active threads or detaching them */
502
void Curl_async_thrdd_multi_destroy(struct Curl_multi *multi, bool join)
503
12.4k
{
504
12.4k
  if(multi->resolv_thrdq) {
505
12.4k
#ifdef CURLVERBOSE
506
12.4k
    CURL_TRC_DNS(multi->admin, "destroy thread queue+pool, join=%d", join);
507
12.4k
    Curl_thrdq_trace(multi->resolv_thrdq, multi->admin);
508
12.4k
#endif
509
12.4k
    Curl_thrdq_destroy(multi->resolv_thrdq, join);
510
12.4k
    multi->resolv_thrdq = NULL;
511
12.4k
  }
512
12.4k
}
513
514
#ifdef CURLVERBOSE
515
static void async_thrdd_report_item(struct Curl_easy *data,
516
                                    struct async_thrdd_item *item)
517
0
{
518
0
  char buf[MAX_IPADR_LEN];
519
0
  struct dynbuf tmp;
520
0
  const char *sep = "";
521
0
  const struct Curl_addrinfo *ai = item->res;
522
0
  CURLcode result;
523
0
  int ai_family;
524
0
#ifdef USE_IPV6
525
0
  ai_family = (item->dns_queries & CURL_DNSQ_AAAA) ? AF_INET6 : AF_INET;
526
#else
527
  ai_family = AF_INET;
528
#endif
529
530
0
  if(!CURL_TRC_DNS_is_verbose(data))
531
0
    return;
532
533
0
  curlx_dyn_init(&tmp, 1024);
534
0
  for(; ai; ai = ai->ai_next) {
535
0
    if(ai->ai_family == ai_family) {
536
0
      Curl_printable_address(ai, buf, sizeof(buf));
537
0
      result = curlx_dyn_addf(&tmp, "%s%s", sep, buf);
538
0
      if(result) {
539
0
        CURL_TRC_DNS(data, "too many IP, cannot show");
540
0
        goto out;
541
0
      }
542
0
      sep = ", ";
543
0
    }
544
0
  }
545
546
0
  CURL_TRC_DNS(data, "Host %s:%u resolved IPv%c: %s",
547
0
               item->hostname, item->port,
548
0
               (item->dns_queries & CURL_DNSQ_AAAA) ? '6' : '4',
549
0
               (curlx_dyn_len(&tmp) ? curlx_dyn_ptr(&tmp) : "(none)"));
550
0
out:
551
0
  curlx_dyn_free(&tmp);
552
0
}
553
#endif /* CURLVERBOSE */
554
555
/* Process the receiving end of the thread queue, dispatching
556
 * processed items to their transfer when it can still be found
557
 * and has an `async` state present. Otherwise, destroy the item. */
558
void Curl_async_thrdd_multi_process(struct Curl_multi *multi)
559
13.2k
{
560
13.2k
  struct Curl_easy *data;
561
13.2k
  void *qitem;
562
563
13.2k
  while(!Curl_thrdq_recv(multi->resolv_thrdq, &qitem)) {
564
    /* dispatch resolve result */
565
0
    struct async_thrdd_item *item = qitem;
566
0
    struct Curl_resolv_async *async = NULL;
567
568
0
    data = Curl_multi_get_easy(multi, item->mid);
569
0
    if(data)
570
0
      async = Curl_async_get(data, item->resolv_id);
571
0
    if(async) {
572
0
      struct async_thrdd_item **pdest = &async->thrdd.res_A;
573
574
0
#ifdef CURLRES_IPV6
575
0
      if(item->dns_queries & CURL_DNSQ_AAAA)
576
0
        pdest = &async->thrdd.res_AAAA;
577
0
#endif
578
0
      if(!*pdest) {
579
0
        *pdest = item;
580
0
        item = NULL;
581
0
      }
582
0
      else
583
0
        DEBUGASSERT(0); /* should not receive duplicates here */
584
585
0
      --async->queries_ongoing;
586
0
      Curl_multi_mark_dirty(data);
587
0
    }
588
0
    async_thrdd_item_free(item);
589
0
  }
590
13.2k
  VERBOSE(Curl_thrdq_trace(multi->resolv_thrdq, multi->admin));
591
13.2k
}
592
593
CURLcode Curl_async_thrdd_multi_set_props(struct Curl_multi *multi,
594
                                          uint32_t min_threads,
595
                                          uint32_t max_threads,
596
                                          uint32_t idle_time_ms)
597
0
{
598
0
  return Curl_thrdq_set_props(multi->resolv_thrdq,
599
0
                              min_threads, max_threads, idle_time_ms);
600
0
}
601
602
static CURLcode async_thrdd_query(struct Curl_easy *data,
603
                                  struct Curl_resolv_async *async,
604
                                  uint8_t dns_queries)
605
0
{
606
0
  struct async_thrdd_item *item;
607
0
  CURLcode result;
608
609
0
  item = async_thrdd_item_create(data, async->id, dns_queries,
610
0
                                 async->peer->hostname, async->peer->port,
611
0
                                 async->transport);
612
0
  if(!item) {
613
0
    result = CURLE_OUT_OF_MEMORY;
614
0
    goto out;
615
0
  }
616
0
  CURL_TRC_DNS(data, "queueing query %s", item->description);
617
0
  result = Curl_thrdq_send(data->multi->resolv_thrdq, item,
618
0
                           async_item_description(item), async->timeout_ms);
619
0
  if(result)
620
0
    goto out;
621
0
  item = NULL;
622
0
  async->queries_ongoing++;
623
624
0
out:
625
0
  if(item)
626
0
    async_thrdd_item_free(item);
627
0
  return result;
628
0
}
629
630
CURLcode Curl_async_getaddrinfo(struct Curl_easy *data,
631
                                struct Curl_resolv_async *async)
632
0
{
633
0
  CURLcode result = CURLE_FAILED_INIT;
634
0
  void *resolver = NULL;
635
636
0
  if(async->queries_ongoing || async->done)
637
0
    return CURLE_FAILED_INIT;
638
639
#ifdef USE_HTTPSRR_ARES
640
  DEBUGASSERT(!async->thrdd.rr.channel);
641
  if((async->dns_queries & CURL_DNSQ_HTTPS) && !async->is_ipaddr) {
642
    result = async_rr_start(data, async);
643
    if(result)
644
      goto out;
645
    resolver = async->thrdd.rr.channel;
646
  }
647
#endif
648
649
0
  result = Curl_resolv_announce_start(data, resolver);
650
0
  if(result)
651
0
    return result;
652
653
0
#ifdef CURLRES_IPV6
654
  /* Do not start an AAAA query for an IPv4 address when
655
   * we will start an A query for it. */
656
0
  if((async->dns_queries & CURL_DNSQ_AAAA) &&
657
0
     !(async->is_ipv4addr && (async->dns_queries & CURL_DNSQ_A))) {
658
0
    result = async_thrdd_query(data, async, CURL_DNSQ_AAAA);
659
0
    if(result)
660
0
      goto out;
661
0
  }
662
0
#endif
663
0
  if(async->dns_queries & CURL_DNSQ_A) {
664
0
    result = async_thrdd_query(data, async, CURL_DNSQ_A);
665
0
    if(result)
666
0
      goto out;
667
0
  }
668
669
0
#ifdef CURLVERBOSE
670
0
  Curl_thrdq_trace(data->multi->resolv_thrdq, data);
671
0
#endif
672
673
0
out:
674
0
  if(!async->queries_ongoing)
675
0
    async->done = TRUE;
676
677
0
  if(result)
678
0
    CURL_TRC_DNS(data, "error queueing query %s:%d -> %d",
679
0
                 async->peer->hostname, async->peer->port, (int)result);
680
0
  return result;
681
0
}
682
683
CURLcode Curl_async_pollset(struct Curl_easy *data,
684
                            struct Curl_resolv_async *async,
685
                            struct easy_pollset *ps)
686
0
{
687
0
  timediff_t timeout_ms;
688
689
0
  timeout_ms = Curl_async_timeleft_ms(data, async);
690
#ifdef USE_HTTPSRR_ARES
691
  if(async->thrdd.rr.channel) {
692
    CURLcode result = Curl_ares_pollset(data, async->thrdd.rr.channel, ps);
693
    if(result)
694
      return result;
695
    timeout_ms = Curl_ares_timeout_ms(data, async, async->thrdd.rr.channel);
696
  }
697
#else
698
0
  (void)ps;
699
0
#endif
700
701
0
  if(!async->done) {
702
0
    const struct curltime *pnow = Curl_pgrs_now(data);
703
#ifndef ENABLE_INTERNAL_WAKEUP
704
    timediff_t stutter_ms, elapsed_ms;
705
    elapsed_ms = curlx_ptimediff_ms(pnow, &async->start);
706
    if(elapsed_ms < 3)
707
      stutter_ms = 1;
708
    else if(elapsed_ms <= 50)
709
      stutter_ms = elapsed_ms / 3;
710
    else if(elapsed_ms <= 250)
711
      stutter_ms = 50;
712
    else
713
      stutter_ms = 200;
714
    timeout_ms = CURLMIN(stutter_ms, timeout_ms);
715
#else
716
0
    if(async->queries_ongoing &&
717
0
       !Curl_thrdq_check_started(data->multi->resolv_thrdq)) {
718
      /* The queue has items but starting a worker thread to process
719
         them just failed again; expire soon to check once more,
720
         instead of sleeping on the full resolve timeout. */
721
0
      CURL_TRC_DNS(data, "resolver thread start failed again, retrying");
722
0
      timeout_ms = CURLMIN(100, timeout_ms);
723
0
    }
724
0
#endif
725
0
    Curl_expire_set(data, EXPIRE_ASYNC_NAME, timeout_ms, pnow);
726
0
  }
727
0
  return CURLE_OK;
728
0
}
729
730
#if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
731
static bool async_thrdd_item_missing_scope(struct Curl_easy *data,
732
                                           struct async_thrdd_item *item)
733
0
{
734
0
  const struct Curl_addrinfo *ai;
735
736
  /* scope id already globally set */
737
0
  if(data->conn && data->conn->scope_id)
738
0
    return FALSE;
739
740
0
  for(ai = item->res; ai; ai = ai->ai_next) {
741
0
    if(ai->ai_family == AF_INET6) {
742
0
      struct sockaddr_in6 *sa6 = (void *)ai->ai_addr;
743
0
      if(IN6_IS_ADDR_LINKLOCAL(&sa6->sin6_addr) && !sa6->sin6_scope_id)
744
0
        return TRUE;
745
0
    }
746
0
  }
747
0
  return FALSE;
748
0
}
749
750
static void async_thrdd_item_strip_results(struct async_thrdd_item *item,
751
                                           int ai_family)
752
0
{
753
0
  struct Curl_addrinfo *ai = item->res, **panchor = &item->res;
754
0
  while(ai) {
755
0
    if(ai->ai_family == ai_family) {
756
0
      *panchor = ai->ai_next;
757
0
      ai->ai_next = NULL;
758
0
      Curl_freeaddrinfo(ai);
759
0
      ai = *panchor;
760
0
    }
761
0
    else {
762
0
      panchor = &ai->ai_next;
763
0
      ai = ai->ai_next;
764
0
    }
765
0
  }
766
0
}
767
768
#endif /* USE_IPV6 && HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID */
769
770
static CURLcode async_thrdd_check_done(struct Curl_easy *data,
771
                                       struct Curl_resolv_async *async)
772
0
{
773
0
  struct async_thrdd_ctx *thrdd = &async->thrdd;
774
775
0
  (void)data;
776
0
  if(thrdd->res_A && !thrdd->processed_A) {
777
0
    VERBOSE(async_thrdd_report_item(data, thrdd->res_A));
778
    /* move addrinfos to the async result member */
779
0
    async->dns_responses |= thrdd->res_A->dns_queries;
780
0
    async->ai_A = thrdd->res_A->res;
781
0
    thrdd->res_A->res = NULL;
782
0
    thrdd->processed_A = TRUE;
783
0
  }
784
785
0
  if(thrdd->res_AAAA && !thrdd->processed_AAAA) {
786
0
#if defined(USE_IPV6) && defined(HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID)
787
    /* do we accept the incoming AAAA response? */
788
0
    if(!(thrdd->res_AAAA->dns_queries & CURL_DNSQ_A) &&
789
0
       async_thrdd_item_missing_scope(data, thrdd->res_AAAA)) {
790
      /* We queried "only" AF_INET6. This may be problematic when the
791
       * result has ipv6 link-local addresses and did not give
792
       * any scope id for it. glibc has a long outstanding bug
793
       * <https://sourceware.org/bugzilla/show_bug.cgi?id=14413>
794
       * that gives scope ids only on AF_UNSPEC queries. */
795
0
      struct async_thrdd_item *item = thrdd->res_AAAA;
796
0
      CURLcode result;
797
798
      /* Reuse the item and queue it again, this time with added
799
       * CURL_DNSQ_A which resolves using AF_UNSPEC. */
800
0
      thrdd->res_AAAA = NULL;
801
0
      item->dns_queries |= CURL_DNSQ_A;
802
0
      if(item->res) {
803
0
        Curl_freeaddrinfo(item->res);
804
0
        item->res = NULL;
805
0
      }
806
807
0
      CURL_TRC_DNS(data, "re-queueing query %s for AF_UNSPEC resolve",
808
0
                   item->description);
809
0
      result = Curl_thrdq_send(data->multi->resolv_thrdq, item,
810
0
                               async_item_description(item),
811
0
                               async->timeout_ms);
812
0
      if(result) {
813
0
        async_thrdd_item_free(item);
814
0
        return result;
815
0
      }
816
0
      async->queries_ongoing++;
817
0
      return CURLE_AGAIN;
818
0
    }
819
    /* accepting the AAAA result, strip it of any AF_INET entries,
820
     * as we might have resolved it with AF_UNSPEC. */
821
0
    async_thrdd_item_strip_results(thrdd->res_AAAA, AF_INET);
822
0
#endif /* USE_IPV6 && HAVE_SOCKADDR_IN6_SIN6_SCOPE_ID */
823
0
    VERBOSE(async_thrdd_report_item(data, thrdd->res_AAAA));
824
    /* move addrinfos to the async result member */
825
0
    async->dns_responses |= thrdd->res_AAAA->dns_queries;
826
0
    async->ai_AAAA = thrdd->res_AAAA->res;
827
0
    thrdd->res_AAAA->res = NULL;
828
0
    thrdd->processed_AAAA = TRUE;
829
0
  }
830
831
0
  if(async->queries_ongoing)
832
0
    return CURLE_AGAIN;
833
0
  async->done = TRUE;
834
0
  return CURLE_OK;
835
0
}
836
837
/*
838
 * Curl_async_take_result() is called repeatedly to check if a previous
839
 * name resolve request has completed. It should also make sure to time-out if
840
 * the operation seems to take too long.
841
 */
842
CURLcode Curl_async_take_result(struct Curl_easy *data,
843
                                struct Curl_resolv_async *async,
844
                                struct Curl_dns_entry **pdns)
845
0
{
846
0
  struct async_thrdd_ctx *thrdd = &async->thrdd;
847
0
  struct Curl_dns_entry *dns = NULL;
848
0
  CURLcode result = CURLE_OK;
849
850
0
  DEBUGASSERT(pdns);
851
0
  *pdns = NULL;
852
853
#ifdef USE_HTTPSRR_ARES
854
  /* best effort, ignore errors */
855
  if(thrdd->rr.channel)
856
    (void)Curl_ares_perform(thrdd->rr.channel, 0);
857
#endif
858
#ifndef ENABLE_INTERNAL_WAKEUP
859
  Curl_async_thrdd_multi_process(data->multi);
860
#endif
861
862
0
  result = async_thrdd_check_done(data, async);
863
0
  if(result)
864
0
    return result;
865
866
0
  Curl_expire_clear(data, EXPIRE_ASYNC_NAME);
867
868
  /* A failure is an authoritative negative answer, eligible for
869
     negative caching, only when every A/AAAA query performed came
870
     back answering that the name does not exist. A query that
871
     failed transiently or never returned is not an answer. */
872
0
  {
873
0
    const uint8_t ip_queries =
874
0
      async->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA);
875
0
    bool negative = (async->dns_responses & ip_queries) == ip_queries;
876
0
    if(thrdd->res_A && (async->ai_A || !thrdd->res_A->negative))
877
0
      negative = FALSE;
878
0
    if(thrdd->res_AAAA && (async->ai_AAAA || !thrdd->res_AAAA->negative))
879
0
      negative = FALSE;
880
0
    if(!thrdd->res_A && !thrdd->res_AAAA)
881
0
      negative = FALSE;
882
0
    async->negative_answer = negative;
883
0
  }
884
885
0
  if(async->result) {
886
0
    result = async->result;
887
0
    goto out;
888
0
  }
889
890
0
  if(async->ai_A || async->ai_AAAA) {
891
0
    dns = Curl_dnsc_mk_addr2(
892
0
      data, async->dns_queries, &async->ai_A, &async->ai_AAAA, async->peer);
893
0
    if(!dns) {
894
0
      result = CURLE_OUT_OF_MEMORY;
895
0
      goto out;
896
0
    }
897
0
  }
898
899
#ifdef USE_HTTPSRR_ARES
900
  if(!dns && thrdd->rr.channel) {
901
    Curl_httpsrr_trace(data, async->httpsrr);
902
    dns = Curl_dnsc_mk_https(data, &async->httpsrr, async->peer);
903
    if(!dns) {
904
      result = CURLE_OUT_OF_MEMORY;
905
      goto out;
906
    }
907
  }
908
#endif
909
910
0
  if(dns) {
911
0
    *pdns = dns;
912
0
    dns = NULL;
913
0
  }
914
0
#ifdef CURLVERBOSE
915
0
  Curl_thrdq_trace(data->multi->resolv_thrdq, data);
916
0
#endif
917
918
0
out:
919
0
  Curl_dns_entry_unlink(data, &dns);
920
0
  Curl_async_thrdd_shutdown(data, async);
921
0
  if(!result && !*pdns)
922
0
    result = Curl_async_failed(data, async, NULL);
923
0
  if(result &&
924
0
     (result != CURLE_COULDNT_RESOLVE_HOST) &&
925
0
     (result != CURLE_COULDNT_RESOLVE_PROXY)) {
926
0
    CURL_TRC_DNS(data, "Error %d resolving %s:%d",
927
0
                 (int)result, async->peer->hostname, async->peer->port);
928
0
  }
929
0
  return result;
930
0
}
931
932
#endif /* USE_RESOLV_THREADED */