Coverage Report

Created: 2025-11-15 08:43

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