/src/PROJ/curl/lib/asyn-base.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
81 | | |
82 | | CURLcode Curl_ares_pollset(struct Curl_easy *data, |
83 | | ares_channel channel, |
84 | | struct easy_pollset *ps) |
85 | | { |
86 | | struct timeval maxtime = { CURL_TIMEOUT_RESOLVE, 0 }; |
87 | | struct timeval timebuf; |
88 | | curl_socket_t sockets[16]; /* ARES documented limit */ |
89 | | unsigned int bitmap, i; |
90 | | struct timeval *timeout; |
91 | | timediff_t milli; |
92 | | CURLcode result = CURLE_OK; |
93 | | |
94 | | DEBUGASSERT(channel); |
95 | | if(!channel) |
96 | | return CURLE_FAILED_INIT; |
97 | | |
98 | | bitmap = ares_getsock(channel, (ares_socket_t *)sockets, |
99 | | CURL_ARRAYSIZE(sockets)); |
100 | | for(i = 0; i < CURL_ARRAYSIZE(sockets); ++i) { |
101 | | int flags = 0; |
102 | | if(ARES_GETSOCK_READABLE(bitmap, i)) |
103 | | flags |= CURL_POLL_IN; |
104 | | if(ARES_GETSOCK_WRITABLE(bitmap, i)) |
105 | | flags |= CURL_POLL_OUT; |
106 | | if(!flags) |
107 | | break; |
108 | | result = Curl_pollset_change(data, ps, sockets[i], flags, 0); |
109 | | if(result) |
110 | | return result; |
111 | | } |
112 | | |
113 | | timeout = ares_timeout(channel, &maxtime, &timebuf); |
114 | | if(!timeout) |
115 | | timeout = &maxtime; |
116 | | milli = curlx_tvtoms(timeout); |
117 | | Curl_expire(data, milli, EXPIRE_ASYNC_NAME); |
118 | | return result; |
119 | | } |
120 | | |
121 | | /* |
122 | | * Curl_ares_perform() |
123 | | * |
124 | | * 1) Ask ares what sockets it currently plays with, then |
125 | | * 2) wait for the timeout period to check for action on ares' sockets. |
126 | | * 3) tell ares to act on all the sockets marked as "with action" |
127 | | * |
128 | | * return number of sockets it worked on, or -1 on error |
129 | | */ |
130 | | int Curl_ares_perform(ares_channel channel, |
131 | | timediff_t timeout_ms) |
132 | | { |
133 | | int nfds; |
134 | | int bitmask; |
135 | | ares_socket_t socks[ARES_GETSOCK_MAXNUM]; |
136 | | struct pollfd pfd[ARES_GETSOCK_MAXNUM]; |
137 | | int i; |
138 | | int num = 0; |
139 | | |
140 | | if(!channel) |
141 | | return 0; |
142 | | |
143 | | bitmask = ares_getsock(channel, socks, ARES_GETSOCK_MAXNUM); |
144 | | |
145 | | for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) { |
146 | | pfd[i].events = 0; |
147 | | pfd[i].revents = 0; |
148 | | if(ARES_GETSOCK_READABLE(bitmask, i)) { |
149 | | pfd[i].fd = socks[i]; |
150 | | pfd[i].events |= POLLRDNORM|POLLIN; |
151 | | } |
152 | | if(ARES_GETSOCK_WRITABLE(bitmask, i)) { |
153 | | pfd[i].fd = socks[i]; |
154 | | pfd[i].events |= POLLWRNORM|POLLOUT; |
155 | | } |
156 | | if(pfd[i].events) |
157 | | num++; |
158 | | else |
159 | | break; |
160 | | } |
161 | | |
162 | | if(num) { |
163 | | nfds = Curl_poll(pfd, (unsigned int)num, timeout_ms); |
164 | | if(nfds < 0) |
165 | | return -1; |
166 | | } |
167 | | else |
168 | | nfds = 0; |
169 | | |
170 | | if(!nfds) |
171 | | /* Call ares_process() unconditionally here, even if we simply timed out |
172 | | above, as otherwise the ares name resolve will not timeout! */ |
173 | | ares_process_fd(channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD); |
174 | | else { |
175 | | /* move through the descriptors and ask for processing on them */ |
176 | | for(i = 0; i < num; i++) |
177 | | ares_process_fd(channel, |
178 | | (pfd[i].revents & (POLLRDNORM|POLLIN)) ? |
179 | | pfd[i].fd : ARES_SOCKET_BAD, |
180 | | (pfd[i].revents & (POLLWRNORM|POLLOUT)) ? |
181 | | pfd[i].fd : ARES_SOCKET_BAD); |
182 | | } |
183 | | return nfds; |
184 | | } |
185 | | |
186 | | #endif |
187 | | |
188 | | #endif /* CURLRES_ASYNCH */ |
189 | | |
190 | | #ifdef USE_CURL_ASYNC |
191 | | |
192 | | #include "doh.h" |
193 | | |
194 | | void Curl_async_shutdown(struct Curl_easy *data) |
195 | 0 | { |
196 | | #ifdef CURLRES_ARES |
197 | | Curl_async_ares_shutdown(data); |
198 | | #endif |
199 | 0 | #ifdef CURLRES_THREADED |
200 | 0 | Curl_async_thrdd_shutdown(data); |
201 | 0 | #endif |
202 | 0 | #ifndef CURL_DISABLE_DOH |
203 | 0 | Curl_doh_cleanup(data); |
204 | 0 | #endif |
205 | 0 | Curl_safefree(data->state.async.hostname); |
206 | 0 | } |
207 | | |
208 | | void Curl_async_destroy(struct Curl_easy *data) |
209 | 0 | { |
210 | | #ifdef CURLRES_ARES |
211 | | Curl_async_ares_destroy(data); |
212 | | #endif |
213 | 0 | #ifdef CURLRES_THREADED |
214 | 0 | Curl_async_thrdd_destroy(data); |
215 | 0 | #endif |
216 | 0 | #ifndef CURL_DISABLE_DOH |
217 | 0 | Curl_doh_cleanup(data); |
218 | 0 | #endif |
219 | 0 | Curl_safefree(data->state.async.hostname); |
220 | 0 | } |
221 | | |
222 | | #endif /* USE_CURL_ASYNC */ |