Coverage Report

Created: 2026-09-01 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/vdns/asyn-base.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
#ifdef HAVE_NETINET_IN_H
27
#include <netinet/in.h>
28
#endif
29
#ifdef HAVE_NETDB_H
30
#include <netdb.h>
31
#endif
32
#ifdef HAVE_ARPA_INET_H
33
#include <arpa/inet.h>
34
#endif
35
#ifdef __VMS
36
#include <in.h>
37
#include <inet.h>
38
#endif
39
40
#ifdef USE_ARES
41
#include <ares.h>
42
#endif
43
44
#include "urldata.h"
45
#include "connect.h"
46
#include "curl_addrinfo.h"
47
#include "curl_trc.h"
48
#include "multiif.h"
49
#include "progress.h"
50
#include "select.h"
51
#include "url.h"
52
#include "vdns/hostip.h"
53
#include "vdns/httpsrr.h"
54
55
/***********************************************************************
56
 * Only for builds using asynchronous name resolves
57
 **********************************************************************/
58
#ifdef CURLRES_ASYNCH
59
60
timediff_t Curl_async_timeleft_ms(struct Curl_easy *data,
61
                                  struct Curl_resolv_async *async)
62
0
{
63
0
  if(async->timeout_ms) {
64
0
    timediff_t elapsed_ms =
65
0
      curlx_ptimediff_ms(Curl_pgrs_now(data), &async->start);
66
0
    return async->timeout_ms - elapsed_ms;
67
0
  }
68
0
  return Curl_timeleft_ms(data);
69
0
}
70
71
#ifdef USE_ARES
72
73
#if ARES_VERSION < 0x011000
74
#error "c-ares 1.16.0 or greater required"
75
#endif
76
77
/*
78
 * Curl_ares_pollset() is called when the outside world (using
79
 * curl_multi_fdset()) wants to get our fd_set setup and we are talking with
80
 * ares. The caller must make sure that this function is only called when we
81
 * have a working ares channel.
82
 *
83
 * Returns: sockets-in-use-bitmap
84
 */
85
CURLcode Curl_ares_pollset(struct Curl_easy *data,
86
                           ares_channel channel,
87
                           struct easy_pollset *ps)
88
{
89
  curl_socket_t sockets[16];  /* ARES documented limit */
90
  unsigned int bitmap, i;
91
  CURLcode result = CURLE_OK;
92
93
  DEBUGASSERT(channel);
94
  if(!channel)
95
    return CURLE_FAILED_INIT;
96
97
  bitmap = ares_getsock(channel, (ares_socket_t *)sockets,
98
                        CURL_ARRAYSIZE(sockets));
99
  for(i = 0; i < CURL_ARRAYSIZE(sockets); ++i) {
100
    int flags = 0;
101
    if(ARES_GETSOCK_READABLE(bitmap, i))
102
      flags |= CURL_POLL_IN;
103
    if(ARES_GETSOCK_WRITABLE(bitmap, i))
104
      flags |= CURL_POLL_OUT;
105
    if(!flags)
106
      break;
107
    result = Curl_pollset_change(data, ps, sockets[i], flags, 0);
108
    if(result)
109
      return result;
110
  }
111
  return result;
112
}
113
114
timediff_t Curl_ares_timeout_ms(struct Curl_easy *data,
115
                                struct Curl_resolv_async *async,
116
                                ares_channel channel)
117
{
118
  timediff_t async_timeout_ms;
119
120
  DEBUGASSERT(channel);
121
  if(!channel)
122
    return -1;
123
124
  async_timeout_ms = Curl_async_timeleft_ms(data, async);
125
  if((async_timeout_ms > 0) && (async_timeout_ms < INT_MAX)) {
126
    struct timeval timebuf;
127
    struct timeval *timeout;
128
    struct timeval end = { (int)async_timeout_ms / 1000,
129
                           ((int)async_timeout_ms % 1000) * 1000 };
130
131
    timeout = ares_timeout(channel, &end, &timebuf);
132
    if(timeout)
133
      return curlx_tvtoms(timeout);
134
  }
135
  return async_timeout_ms;
136
}
137
138
/*
139
 * Curl_ares_perform()
140
 *
141
 * 1) Ask ares what sockets it currently plays with, then
142
 * 2) wait for the timeout period to check for action on ares' sockets.
143
 * 3) tell ares to act on all the sockets marked as "with action"
144
 *
145
 * return number of sockets it worked on, or -1 on error
146
 */
147
int Curl_ares_perform(ares_channel channel, timediff_t timeout_ms)
148
{
149
  int nfds;
150
  int bitmask;
151
  ares_socket_t socks[ARES_GETSOCK_MAXNUM];
152
  struct pollfd pfd[ARES_GETSOCK_MAXNUM];
153
  int i;
154
  int num = 0;
155
156
  if(!channel)
157
    return 0;
158
159
  bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM);
160
161
  for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
162
    pfd[i].events = 0;
163
    pfd[i].revents = 0;
164
    if(ARES_GETSOCK_READABLE(bitmask, i)) {
165
      pfd[i].fd = socks[i];
166
      pfd[i].events |= POLLRDNORM | POLLIN;
167
    }
168
    if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
169
      pfd[i].fd = socks[i];
170
      pfd[i].events |= POLLWRNORM | POLLOUT;
171
    }
172
    if(pfd[i].events)
173
      num++;
174
    else
175
      break;
176
  }
177
178
  if(num) {
179
    nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
180
    if(nfds < 0)
181
      return -1;
182
  }
183
  else
184
    nfds = 0;
185
186
  if(!nfds)
187
    /* Call ares_process() unconditionally here, even if we timed out
188
       above, as otherwise the ares name resolve will not timeout! */
189
    ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
190
  else {
191
    /* move through the descriptors and ask for processing on them */
192
    for(i = 0; i < num; i++)
193
      ares_process_fd(channel,
194
                      (pfd[i].revents & (POLLRDNORM | POLLIN)) ?
195
                      pfd[i].fd : ARES_SOCKET_BAD,
196
                      (pfd[i].revents & (POLLWRNORM | POLLOUT)) ?
197
                      pfd[i].fd : ARES_SOCKET_BAD);
198
  }
199
  return nfds;
200
}
201
202
#endif /* USE_ARES */
203
204
#endif /* CURLRES_ASYNCH */
205
206
#ifdef USE_CURL_ASYNC
207
208
#include "doh.h"
209
210
void Curl_async_shutdown(struct Curl_easy *data,
211
                         struct Curl_resolv_async *async)
212
0
{
213
0
  if(async) {
214
0
    CURL_TRC_DNS(data, "[%u] shutdown async", async->id);
215
0
    async->shutdown = TRUE;
216
#ifdef USE_RESOLV_ARES
217
    Curl_async_ares_shutdown(data, async);
218
#endif
219
0
#ifdef USE_RESOLV_THREADED
220
0
    Curl_async_thrdd_shutdown(data, async);
221
0
#endif
222
0
#ifndef CURL_DISABLE_DOH
223
0
    Curl_doh_cleanup(data, async);
224
0
#endif
225
0
  }
226
0
}
227
228
void Curl_async_destroy(struct Curl_easy *data,
229
                        struct Curl_resolv_async *async)
230
0
{
231
0
  if(async) {
232
0
    CURL_TRC_DNS(data, "[%u] destroy async", async->id);
233
0
    async->shutdown = TRUE;
234
#ifdef USE_RESOLV_ARES
235
    Curl_async_ares_destroy(data, async);
236
#endif
237
0
#ifdef USE_RESOLV_THREADED
238
0
    Curl_async_thrdd_destroy(data, async);
239
0
#endif
240
0
#ifndef CURL_DISABLE_DOH
241
0
    Curl_doh_cleanup(data, async);
242
0
#endif
243
0
    if(async->ai_A)
244
0
      Curl_freeaddrinfo(async->ai_A);
245
0
    if(async->ai_AAAA)
246
0
      Curl_freeaddrinfo(async->ai_AAAA);
247
#ifdef USE_HTTPSRR
248
    Curl_httpsrr_destroy(async->httpsrr);
249
#endif
250
0
    Curl_peer_unlink(&async->peer);
251
0
    curlx_safefree(async);
252
0
  }
253
0
}
254
255
CURLcode Curl_async_failed(struct Curl_easy *data,
256
                           struct Curl_resolv_async *async,
257
                           const char *detail)
258
0
{
259
0
  const char *host_or_proxy = "host";
260
0
  CURLcode result = CURLE_COULDNT_RESOLVE_HOST;
261
262
0
#ifndef CURL_DISABLE_PROXY
263
0
  if(async->for_proxy) {
264
0
    host_or_proxy = "proxy";
265
0
    result = CURLE_COULDNT_RESOLVE_PROXY;
266
0
  }
267
0
#endif
268
269
0
  if(async->dns_queries & (CURL_DNSQ_A | CURL_DNSQ_AAAA))
270
0
    failf(data, "Could not resolve %s: %s%s%s%s",
271
0
          host_or_proxy, async->peer->hostname,
272
0
          detail ? " (" : "", detail ? detail : "", detail ? ")" : "");
273
0
  return result;
274
0
}
275
276
#endif /* USE_CURL_ASYNC */