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