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