Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmcurl/lib/asyn.h
5017 views
1
#ifndef HEADER_CURL_ASYN_H
2
#define HEADER_CURL_ASYN_H
3
/***************************************************************************
4
* _ _ ____ _
5
* Project ___| | | | _ \| |
6
* / __| | | | |_) | |
7
* | (__| |_| | _ <| |___
8
* \___|\___/|_| \_\_____|
9
*
10
* Copyright (C) Daniel Stenberg, <[email protected]>, et al.
11
*
12
* This software is licensed as described in the file COPYING, which
13
* you should have received as part of this distribution. The terms
14
* are also available at https://curl.se/docs/copyright.html.
15
*
16
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
17
* copies of the Software, and permit persons to whom the Software is
18
* furnished to do so, under the terms of the COPYING file.
19
*
20
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21
* KIND, either express or implied.
22
*
23
* SPDX-License-Identifier: curl
24
*
25
***************************************************************************/
26
#include "curl_setup.h"
27
28
struct Curl_easy;
29
struct Curl_dns_entry;
30
31
#ifdef CURLRES_ASYNCH
32
33
#include "curl_addrinfo.h"
34
#include "httpsrr.h"
35
36
struct addrinfo;
37
struct hostent;
38
struct connectdata;
39
struct easy_pollset;
40
41
#if defined(CURLRES_ARES) && defined(CURLRES_THREADED)
42
#error cannot have both CURLRES_ARES and CURLRES_THREADED defined
43
#endif
44
45
/*
46
* This header defines all functions in the internal asynch resolver interface.
47
* All asynch resolvers need to provide these functions.
48
* asyn-ares.c and asyn-thread.c are the current implementations of asynch
49
* resolver backends.
50
*/
51
52
/*
53
* Curl_async_global_init()
54
*
55
* Called from curl_global_init() to initialize global resolver environment.
56
* Returning anything else than CURLE_OK fails curl_global_init().
57
*/
58
int Curl_async_global_init(void);
59
60
/*
61
* Curl_async_global_cleanup()
62
* Called from curl_global_cleanup() to destroy global resolver environment.
63
*/
64
void Curl_async_global_cleanup(void);
65
66
/*
67
* Curl_async_get_impl()
68
* Get the resolver implementation instance (c-ares channel) or NULL
69
* for passing to application callback.
70
*/
71
CURLcode Curl_async_get_impl(struct Curl_easy *easy, void **impl);
72
73
/* Curl_async_pollset()
74
*
75
* This function is called from the Curl_multi_pollset() function. 'sock' is a
76
* pointer to an array to hold the file descriptors, with 'numsock' being the
77
* size of that array (in number of entries). This function is supposed to
78
* return bitmask indicating what file descriptors (referring to array indexes
79
* in the 'sock' array) to wait for, read/write.
80
*/
81
CURLcode Curl_async_pollset(struct Curl_easy *data, struct easy_pollset *ps);
82
83
/*
84
* Curl_async_is_resolved()
85
*
86
* Called repeatedly to check if a previous name resolve request has
87
* completed. It should also make sure to time-out if the operation seems to
88
* take too long.
89
*
90
* Returns normal CURLcode errors.
91
*/
92
CURLcode Curl_async_is_resolved(struct Curl_easy *data,
93
struct Curl_dns_entry **dns);
94
95
/*
96
* Curl_async_await()
97
*
98
* Waits for a resolve to finish. This function should be avoided since using
99
* this risk getting the multi interface to "hang".
100
*
101
* On return 'entry' is assigned the resolved dns (CURLE_OK or NULL otherwise.
102
*
103
* Returns CURLE_COULDNT_RESOLVE_HOST if the host was not resolved,
104
* CURLE_OPERATION_TIMEDOUT if a time-out occurred, or other errors.
105
*/
106
CURLcode Curl_async_await(struct Curl_easy *data,
107
struct Curl_dns_entry **dnsentry);
108
109
/*
110
* Curl_async_getaddrinfo() - when using this resolver
111
*
112
* Returns name information about the given hostname and port number. If
113
* successful, the 'hostent' is returned and the fourth argument will point to
114
* memory we need to free after use. That memory *MUST* be freed with
115
* Curl_freeaddrinfo(), nothing else.
116
*
117
* Each resolver backend must of course make sure to return data in the
118
* correct format to comply with this.
119
*/
120
CURLcode Curl_async_getaddrinfo(struct Curl_easy *data, const char *hostname,
121
int port, int ip_version);
122
123
#ifdef USE_ARES
124
/* common functions for c-ares and threaded resolver with HTTPSRR */
125
#include <ares.h>
126
127
CURLcode Curl_ares_pollset(struct Curl_easy *data,
128
ares_channel channel,
129
struct easy_pollset *ps);
130
131
int Curl_ares_perform(ares_channel channel, timediff_t timeout_ms);
132
#endif
133
134
#ifdef CURLRES_ARES
135
/* async resolving implementation using c-ares alone */
136
struct async_ares_ctx {
137
ares_channel channel;
138
int num_pending; /* number of outstanding c-ares requests */
139
struct Curl_addrinfo *temp_ai; /* intermediary result while fetching c-ares
140
parts */
141
int ares_status; /* ARES_SUCCESS, ARES_ENOTFOUND, etc. */
142
CURLcode result; /* CURLE_OK or error handling response */
143
#ifndef HAVE_CARES_GETADDRINFO
144
struct curltime happy_eyeballs_dns_time; /* when this timer started, or 0 */
145
#endif
146
#ifdef USE_HTTPSRR
147
struct Curl_https_rrinfo hinfo;
148
#endif
149
};
150
151
void Curl_async_ares_shutdown(struct Curl_easy *data);
152
void Curl_async_ares_destroy(struct Curl_easy *data);
153
154
/* Set the DNS server to use by ares, from `data` settings. */
155
CURLcode Curl_async_ares_set_dns_servers(struct Curl_easy *data);
156
157
/* Set the DNS interfacer to use by ares, from `data` settings. */
158
CURLcode Curl_async_ares_set_dns_interface(struct Curl_easy *data);
159
160
/* Set the local ipv4 address to use by ares, from `data` settings. */
161
CURLcode Curl_async_ares_set_dns_local_ip4(struct Curl_easy *data);
162
163
/* Set the local ipv6 address to use by ares, from `data` settings. */
164
CURLcode Curl_async_ares_set_dns_local_ip6(struct Curl_easy *data);
165
166
#endif /* CURLRES_ARES */
167
168
#ifdef CURLRES_THREADED
169
/* async resolving implementation using POSIX threads */
170
#include "curl_threads.h"
171
172
/* Context for threaded address resolver */
173
struct async_thrdd_addr_ctx {
174
curl_thread_t thread_hnd;
175
char *hostname; /* hostname to resolve, Curl_async.hostname
176
duplicate */
177
curl_mutex_t mutx;
178
#ifndef CURL_DISABLE_SOCKETPAIR
179
curl_socket_t sock_pair[2]; /* eventfd/pipes/socket pair */
180
#endif
181
struct Curl_addrinfo *res;
182
#ifdef HAVE_GETADDRINFO
183
struct addrinfo hints;
184
#endif
185
struct curltime start;
186
timediff_t interval_end;
187
unsigned int poll_interval;
188
int port;
189
int sock_error;
190
int ref_count;
191
BIT(thrd_done);
192
BIT(do_abort);
193
};
194
195
/* Context for threaded resolver */
196
struct async_thrdd_ctx {
197
/* `addr` is a pointer since this memory is shared with a started
198
* thread. Since threads cannot be killed, we use reference counting
199
* so that we can "release" our pointer to this memory while the
200
* thread is still running. */
201
struct async_thrdd_addr_ctx *addr;
202
#if defined(USE_HTTPSRR) && defined(USE_ARES)
203
struct {
204
ares_channel channel;
205
struct Curl_https_rrinfo hinfo;
206
CURLcode result;
207
BIT(done);
208
} rr;
209
#endif
210
};
211
212
void Curl_async_thrdd_shutdown(struct Curl_easy *data);
213
void Curl_async_thrdd_destroy(struct Curl_easy *data);
214
215
#endif /* CURLRES_THREADED */
216
217
#ifndef CURL_DISABLE_DOH
218
struct doh_probes;
219
#endif
220
221
#else /* CURLRES_ASYNCH */
222
223
/* convert these functions if an asynch resolver is not used */
224
#define Curl_async_get_impl(x, y) (*(y) = NULL, CURLE_OK)
225
#define Curl_async_is_resolved(x, y) CURLE_COULDNT_RESOLVE_HOST
226
#define Curl_async_await(x, y) CURLE_COULDNT_RESOLVE_HOST
227
#define Curl_async_global_init() CURLE_OK
228
#define Curl_async_global_cleanup() Curl_nop_stmt
229
230
#endif /* !CURLRES_ASYNCH */
231
232
#if defined(CURLRES_ASYNCH) || !defined(CURL_DISABLE_DOH)
233
#define USE_CURL_ASYNC
234
#endif
235
236
#ifdef USE_CURL_ASYNC
237
struct Curl_async {
238
#ifdef CURLRES_ARES
239
struct async_ares_ctx ares;
240
#elif defined(CURLRES_THREADED)
241
struct async_thrdd_ctx thrdd;
242
#endif
243
#ifndef CURL_DISABLE_DOH
244
struct doh_probes *doh; /* DoH specific data for this request */
245
#endif
246
struct Curl_dns_entry *dns; /* result of resolving on success */
247
char *hostname; /* copy of the params resolv started with */
248
int port;
249
int ip_version;
250
BIT(done);
251
};
252
253
/*
254
* Curl_async_shutdown().
255
*
256
* This shuts down all ongoing operations.
257
*/
258
void Curl_async_shutdown(struct Curl_easy *data);
259
260
/*
261
* Curl_async_destroy().
262
*
263
* This frees the resources of any async resolve.
264
*/
265
void Curl_async_destroy(struct Curl_easy *data);
266
#else /* !USE_CURL_ASYNC */
267
#define Curl_async_shutdown(x) Curl_nop_stmt
268
#define Curl_async_destroy(x) Curl_nop_stmt
269
#endif /* USE_CURL_ASYNC */
270
271
/********** end of generic resolver interface functions *****************/
272
#endif /* HEADER_CURL_ASYN_H */
273
274