Coverage Report

Created: 2025-12-14 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/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
25
#include "curl_setup.h"
26
27
#ifdef HAVE_NETINET_IN_H
28
#include <netinet/in.h>
29
#endif
30
#ifdef HAVE_NETDB_H
31
#include <netdb.h>
32
#endif
33
#ifdef HAVE_ARPA_INET_H
34
#include <arpa/inet.h>
35
#endif
36
#ifdef __VMS
37
#include <in.h>
38
#include <inet.h>
39
#endif
40
41
#ifdef USE_ARES
42
#include <ares.h>
43
#include <ares_version.h> /* really old c-ares did not include this by
44
                             itself */
45
#endif
46
47
#include "urldata.h"
48
#include "asyn.h"
49
#include "sendf.h"
50
#include "hostip.h"
51
#include "hash.h"
52
#include "multiif.h"
53
#include "select.h"
54
#include "curl_share.h"
55
#include "url.h"
56
57
/***********************************************************************
58
 * Only for builds using asynchronous name resolves
59
 **********************************************************************/
60
#ifdef CURLRES_ASYNCH
61
62
#ifdef USE_ARES
63
64
#if ARES_VERSION < 0x010600
65
#error "requires c-ares 1.6.0 or newer"
66
#endif
67
68
/*
69
 * Curl_ares_pollset() is called when the outside world (using
70
 * curl_multi_fdset()) wants to get our fd_set setup and we are talking with
71
 * ares. The caller must make sure that this function is only called when we
72
 * have a working ares channel.
73
 *
74
 * Returns: sockets-in-use-bitmap
75
 */
76
CURLcode Curl_ares_pollset(struct Curl_easy *data,
77
                           ares_channel channel,
78
                           struct easy_pollset *ps)
79
{
80
  struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 };
81
  struct timeval timebuf;
82
  curl_socket_t sockets[16];  /* ARES documented limit */
83
  unsigned int bitmap, i;
84
  struct timeval *timeout;
85
  timediff_t milli;
86
  CURLcode result = CURLE_OK;
87
88
  DEBUGASSERT(channel);
89
  if(!channel)
90
    return CURLE_FAILED_INIT;
91
92
  bitmap = ares_getsock(channel, (ares_socket_t *)sockets,
93
                        CURL_ARRAYSIZE(sockets));
94
  for(i = 0; i < CURL_ARRAYSIZE(sockets); ++i) {
95
    int flags = 0;
96
    if(ARES_GETSOCK_READABLE(bitmap, i))
97
      flags |= CURL_POLL_IN;
98
    if(ARES_GETSOCK_WRITABLE(bitmap, i))
99
      flags |= CURL_POLL_OUT;
100
    if(!flags)
101
      break;
102
    result = Curl_pollset_change(data, ps, sockets[i], flags, 0);
103
    if(result)
104
      return result;
105
  }
106
107
  timeout = ares_timeout(channel, &maxtime, &timebuf);
108
  if(!timeout)
109
    timeout = &maxtime;
110
  milli = curlx_tvtoms(timeout);
111
  Curl_expire(data, milli, EXPIRE_ASYNC_NAME);
112
  return result;
113
}
114
115
/*
116
 * Curl_ares_perform()
117
 *
118
 * 1) Ask ares what sockets it currently plays with, then
119
 * 2) wait for the timeout period to check for action on ares' sockets.
120
 * 3) tell ares to act on all the sockets marked as "with action"
121
 *
122
 * return number of sockets it worked on, or -1 on error
123
 */
124
int Curl_ares_perform(ares_channel channel, timediff_t timeout_ms)
125
{
126
  int nfds;
127
  int bitmask;
128
  ares_socket_t socks[ARES_GETSOCK_MAXNUM];
129
  struct pollfd pfd[ARES_GETSOCK_MAXNUM];
130
  int i;
131
  int num = 0;
132
133
  if(!channel)
134
    return 0;
135
136
  bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM);
137
138
  for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
139
    pfd[i].events = 0;
140
    pfd[i].revents = 0;
141
    if(ARES_GETSOCK_READABLE(bitmask, i)) {
142
      pfd[i].fd = socks[i];
143
      pfd[i].events |= POLLRDNORM | POLLIN;
144
    }
145
    if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
146
      pfd[i].fd = socks[i];
147
      pfd[i].events |= POLLWRNORM | POLLOUT;
148
    }
149
    if(pfd[i].events)
150
      num++;
151
    else
152
      break;
153
  }
154
155
  if(num) {
156
    nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms);
157
    if(nfds < 0)
158
      return -1;
159
  }
160
  else
161
    nfds = 0;
162
163
  if(!nfds)
164
    /* Call ares_process() unconditionally here, even if we simply timed out
165
       above, as otherwise the ares name resolve will not timeout! */
166
    ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD);
167
  else {
168
    /* move through the descriptors and ask for processing on them */
169
    for(i = 0; i < num; i++)
170
      ares_process_fd(channel,
171
                      (pfd[i].revents & (POLLRDNORM | POLLIN)) ?
172
                      pfd[i].fd : ARES_SOCKET_BAD,
173
                      (pfd[i].revents & (POLLWRNORM | POLLOUT)) ?
174
                      pfd[i].fd : ARES_SOCKET_BAD);
175
  }
176
  return nfds;
177
}
178
179
#endif
180
181
#endif /* CURLRES_ASYNCH */
182
183
#ifdef USE_CURL_ASYNC
184
185
#include "doh.h"
186
187
void Curl_async_shutdown(struct Curl_easy *data)
188
0
{
189
#ifdef CURLRES_ARES
190
  Curl_async_ares_shutdown(data);
191
#endif
192
0
#ifdef CURLRES_THREADED
193
0
  Curl_async_thrdd_shutdown(data);
194
0
#endif
195
0
#ifndef CURL_DISABLE_DOH
196
0
  Curl_doh_cleanup(data);
197
0
#endif
198
0
  Curl_safefree(data->state.async.hostname);
199
0
}
200
201
void Curl_async_destroy(struct Curl_easy *data)
202
0
{
203
#ifdef CURLRES_ARES
204
  Curl_async_ares_destroy(data);
205
#endif
206
0
#ifdef CURLRES_THREADED
207
0
  Curl_async_thrdd_destroy(data);
208
0
#endif
209
0
#ifndef CURL_DISABLE_DOH
210
0
  Curl_doh_cleanup(data);
211
0
#endif
212
  Curl_safefree(data->state.async.hostname);
213
0
}
214
215
#endif /* USE_CURL_ASYNC */