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