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 | | #include "urldata.h" |
27 | | #include "transfer.h" |
28 | | #include "url.h" |
29 | | #include "cfilters.h" |
30 | | #include "connect.h" |
31 | | #include "progress.h" |
32 | | #include "curl_share.h" |
33 | | #include "psl.h" |
34 | | #include "multiif.h" |
35 | | #include "multi_ev.h" |
36 | | #include "sendf.h" |
37 | | #include "curl_trc.h" |
38 | | #include "http.h" |
39 | | #include "select.h" |
40 | | #include "curlx/wait.h" |
41 | | #include "conncache.h" |
42 | | #include "multihandle.h" |
43 | | #include "sigpipe.h" |
44 | | #include "vtls/vtls.h" |
45 | | #include "vtls/vtls_scache.h" |
46 | | #include "http_proxy.h" |
47 | | #include "http2.h" |
48 | | #include "socketpair.h" |
49 | | #include "bufref.h" |
50 | | #include "curlx/strparse.h" |
51 | | |
52 | | /* initial multi->xfers table size for a full multi */ |
53 | 53.3k | #define CURL_XFER_TABLE_SIZE 128 |
54 | | |
55 | | /* CURL_SOCKET_HASH_TABLE_SIZE should be a prime number. Increasing it from 97 |
56 | | to 911 takes on a 32-bit machine 4 x 804 = 3211 more bytes. Still, every |
57 | | curl handle takes 6K memory, therefore this 3K are not significant. */ |
58 | | #ifndef CURL_SOCKET_HASH_TABLE_SIZE |
59 | 25.8k | #define CURL_SOCKET_HASH_TABLE_SIZE 911 |
60 | | #endif |
61 | | |
62 | | #ifndef CURL_CONNECTION_HASH_SIZE |
63 | 25.8k | #define CURL_CONNECTION_HASH_SIZE 97 |
64 | | #endif |
65 | | |
66 | | #ifndef CURL_DNS_HASH_SIZE |
67 | 25.8k | #define CURL_DNS_HASH_SIZE 71 |
68 | | #endif |
69 | | |
70 | | #ifndef CURL_TLS_SESSION_SIZE |
71 | 25.8k | #define CURL_TLS_SESSION_SIZE 25 |
72 | | #endif |
73 | | |
74 | | static void move_pending_to_connect(struct Curl_multi *multi, |
75 | | struct Curl_easy *data); |
76 | | static CURLMcode add_next_timeout(const struct curltime *pnow, |
77 | | struct Curl_multi *multi, |
78 | | struct Curl_easy *data); |
79 | | static void multi_timeout(struct Curl_multi *multi, |
80 | | timediff_t *pexire_offset_us, |
81 | | int *timeout_ms); |
82 | | static void multi_schedule_pending(struct Curl_multi *multi); |
83 | | static void multi_xfer_bufs_free(struct Curl_multi *multi); |
84 | | #ifdef DEBUGBUILD |
85 | | static void multi_xfer_tbl_dump(struct Curl_multi *multi); |
86 | | #endif |
87 | | /* Get the # of transfers current in process/pending. */ |
88 | | static uint32_t multi_xfers_running(struct Curl_multi *multi); |
89 | | |
90 | | /* function pointer called once when entering a state */ |
91 | | typedef void (*mstate_enter_func)(struct Curl_easy *data, |
92 | | CURLMstate from_state); |
93 | | |
94 | | static void mstate_enter_connect(struct Curl_easy *data, |
95 | | CURLMstate from_state) |
96 | 39.5k | { |
97 | 39.5k | (void)from_state; |
98 | 39.5k | Curl_init_CONNECT(data); |
99 | 39.5k | } |
100 | | |
101 | | static void mstate_enter_did(struct Curl_easy *data, |
102 | | CURLMstate from_state) |
103 | 34.8k | { |
104 | 34.8k | (void)from_state; |
105 | 34.8k | data->req.chunk = FALSE; |
106 | 34.8k | Curl_pgrsTime(data, TIMER_PRETRANSFER); |
107 | 34.8k | if(!CURL_REQ_WANT_SEND(data)) |
108 | 30.5k | Curl_pgrsTime(data, TIMER_POSTRANSFER); |
109 | 34.8k | } |
110 | | |
111 | | static void mstate_enter_done(struct Curl_easy *data, |
112 | | CURLMstate from_state) |
113 | 13.8k | { |
114 | 13.8k | (void)from_state; |
115 | 13.8k | CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE); |
116 | 13.8k | } |
117 | | |
118 | | static void mstate_enter_completed(struct Curl_easy *data, |
119 | | CURLMstate from_state) |
120 | 27.2k | { |
121 | | /* we sometimes directly jump to COMPLETED, trigger things |
122 | | * we then missed. Not when coming from before DO: the transfer |
123 | | * failed to resolve/connect and never sent any request. */ |
124 | 27.2k | if((from_state >= MSTATE_DO) && (from_state < MSTATE_DID)) { |
125 | 591 | Curl_pgrsTime(data, TIMER_PRETRANSFER); |
126 | 591 | Curl_pgrsTime(data, TIMER_POSTRANSFER); |
127 | 591 | Curl_pgrsTime(data, TIMER_STARTTRANSFER); |
128 | 591 | } |
129 | | /* multi_done() should have run and detached any connection already */ |
130 | 27.2k | DEBUGASSERT(!data->conn); |
131 | 27.2k | Curl_pgrsCompleted(data); |
132 | 27.2k | if(from_state < MSTATE_DONE) |
133 | 13.3k | CURLM_NTFY(data, CURLMNOTIFY_EASY_DONE); |
134 | | /* changing to COMPLETED means it is in process and needs to go */ |
135 | 27.2k | DEBUGASSERT(Curl_uint32_bset_contains(&data->multi->process, data->mid)); |
136 | 27.2k | Curl_uint32_bset_remove(&data->multi->process, data->mid); |
137 | 27.2k | Curl_uint32_bset_remove(&data->multi->pending, data->mid); /* to be sure */ |
138 | | |
139 | 27.2k | if(Curl_uint32_bset_empty(&data->multi->process)) { |
140 | | /* free the transfer buffer when we have no more active transfers */ |
141 | 0 | multi_xfer_bufs_free(data->multi); |
142 | 0 | } |
143 | | /* Important: reset the conn pointer so that we do not point to memory |
144 | | that could be freed anytime */ |
145 | 27.2k | Curl_detach_connection(data); |
146 | 27.2k | Curl_expire_clear_all(data); /* stop all timers */ |
147 | 27.2k | } |
148 | | |
149 | | /* always use this function to change state, to make debugging easier */ |
150 | | static void mstate(struct Curl_easy *data, uint8_t state |
151 | | #ifdef DEBUGBUILD |
152 | | , int lineno |
153 | | #endif |
154 | | ) |
155 | 353k | { |
156 | 353k | uint8_t oldstate = data->mstate; |
157 | 353k | static const mstate_enter_func state_enter[MSTATE_LAST] = { |
158 | 353k | NULL, /* INIT */ |
159 | 353k | NULL, /* PENDING */ |
160 | 353k | NULL, /* SETUP */ |
161 | 353k | mstate_enter_connect, /* CONNECT */ |
162 | 353k | NULL, /* CONNECTING */ |
163 | 353k | NULL, /* PROTOCONNECT */ |
164 | 353k | NULL, /* PROTOCONNECTING */ |
165 | 353k | NULL, /* DO */ |
166 | 353k | NULL, /* DOING */ |
167 | 353k | NULL, /* DOING_MORE */ |
168 | 353k | mstate_enter_did, /* DID */ |
169 | 353k | NULL, /* PERFORMING */ |
170 | 353k | NULL, /* RATELIMITING */ |
171 | 353k | mstate_enter_done, /* DONE */ |
172 | 353k | mstate_enter_completed, /* COMPLETED */ |
173 | | NULL /* MSGSENT */ |
174 | 353k | }; |
175 | | |
176 | 353k | if(oldstate == state) |
177 | | /* do not bother when the new state is the same as the old state */ |
178 | 25.8k | return; |
179 | | |
180 | 327k | #ifdef DEBUGBUILD |
181 | 327k | NOVERBOSE((void)lineno); |
182 | 327k | CURL_TRC_M(data, "-> [%s] (line %d)", CURL_MSTATE_NAME(state), lineno); |
183 | | #else |
184 | | CURL_TRC_M(data, "-> [%s]", CURL_MSTATE_NAME(state)); |
185 | | #endif |
186 | | |
187 | | /* really switching state */ |
188 | 327k | data->mstate = (uint8_t)state; |
189 | 327k | if(state_enter[state]) |
190 | 115k | state_enter[state](data, (CURLMstate)oldstate); |
191 | 327k | } |
192 | | |
193 | | #ifndef DEBUGBUILD |
194 | | #define multistate(x, y) mstate(x, y) |
195 | | #else |
196 | 353k | #define multistate(x, y) mstate(x, y, __LINE__) |
197 | | #endif |
198 | | |
199 | | /* multi->proto_hash destructor. Should never be called as elements |
200 | | * MUST be added with their own destructor */ |
201 | | static void ph_freeentry(void *p) |
202 | 0 | { |
203 | 0 | (void)p; |
204 | | /* Always FALSE. Cannot use a 0 assert here since compilers |
205 | | * are not in agreement if they then want a NORETURN attribute or |
206 | | * not. *sigh* */ |
207 | 0 | DEBUGASSERT(!p); |
208 | 0 | } |
209 | | |
210 | | /* |
211 | | * multi_addmsg() |
212 | | * |
213 | | * Called when a transfer is completed. Marks its message as unread. |
214 | | */ |
215 | | static void multi_addmsg(struct Curl_multi *multi, struct Curl_easy *data) |
216 | 27.2k | { |
217 | 27.2k | if(Curl_uint32_bset_empty(&multi->msgsent)) |
218 | 27.2k | CURLM_NTFY(multi->admin, CURLMNOTIFY_INFO_READ); |
219 | 27.2k | Curl_uint32_bset_add(&multi->msgsent, data->mid); |
220 | 27.2k | } |
221 | | |
222 | | static void multi_timeouts_init(struct Curl_easy *data); |
223 | | |
224 | | struct Curl_multi *Curl_multi_handle(uint32_t xfer_table_size, |
225 | | size_t ev_hashsize, /* event hash */ |
226 | | size_t chashsize, /* connection hash */ |
227 | | size_t dnssize, /* dns hash */ |
228 | | size_t sesssize) /* TLS session cache */ |
229 | 27.6k | { |
230 | 27.6k | struct curltime created; |
231 | 27.6k | struct Curl_multi *multi = curlx_calloc(1, sizeof(struct Curl_multi)); |
232 | | |
233 | 27.6k | if(!multi) |
234 | 31 | return NULL; |
235 | | |
236 | 27.5k | multi->magic = CURLMULTI_MAGIC_NUMBER; |
237 | | |
238 | | /* Initialisation order is important here! |
239 | | * easy_init() does a lazy check on curl_global_init() which sets |
240 | | * up platform specific things we need. For example calling curlx_pnow() |
241 | | * before this is not safe. */ |
242 | 27.5k | multi->admin = curl_easy_init(); |
243 | 27.5k | if(!multi->admin) { |
244 | 20 | curlx_free(multi); |
245 | 20 | return NULL; |
246 | 20 | } |
247 | 27.5k | multi->admin->multi = multi; |
248 | 27.5k | multi->admin->state.internal = TRUE; |
249 | | |
250 | | /* Now we can use curlx_* things safely */ |
251 | 27.5k | curlx_pnow(&created); |
252 | 27.5k | Curl_timeouts_init(&multi->timeouts, &created); |
253 | 27.5k | multi_timeouts_init(multi->admin); |
254 | | |
255 | 27.5k | Curl_dnscache_init(&multi->dnscache, dnssize); |
256 | 27.5k | Curl_mntfy_init(multi); |
257 | 27.5k | Curl_multi_ev_init(multi, ev_hashsize); |
258 | 27.5k | Curl_uint32_tbl_init(&multi->xfers); |
259 | 27.5k | Curl_uint32_bset_init(&multi->process); |
260 | 27.5k | Curl_uint32_bset_init(&multi->dirty); |
261 | 27.5k | Curl_uint32_bset_init(&multi->pending); |
262 | 27.5k | Curl_uint32_bset_init(&multi->msgsent); |
263 | 27.5k | Curl_hash_init(&multi->proto_hash, 23, CURL_HASH_TYPE_BYTES, ph_freeentry); |
264 | 27.5k | Curl_cshutdn_init(&multi->cshutdn); |
265 | | |
266 | 27.5k | multi->multiplexing = TRUE; |
267 | 27.5k | multi->max_concurrent_streams = 100; |
268 | 27.5k | #ifdef ENABLE_WAKEUP |
269 | 27.5k | multi->wakeup_pair[0] = CURL_SOCKET_BAD; |
270 | 27.5k | multi->wakeup_pair[1] = CURL_SOCKET_BAD; |
271 | 27.5k | #endif |
272 | 27.5k | #ifdef ENABLE_INTERNAL_WAKEUP |
273 | 27.5k | multi->wakeup_internal[0] = CURL_SOCKET_BAD; |
274 | 27.5k | multi->wakeup_internal[1] = CURL_SOCKET_BAD; |
275 | 27.5k | #endif |
276 | | |
277 | 27.5k | if(Curl_uint32_bset_resize(&multi->process, xfer_table_size) || |
278 | 27.5k | Curl_uint32_bset_resize(&multi->pending, xfer_table_size) || |
279 | 27.5k | Curl_uint32_bset_resize(&multi->dirty, xfer_table_size) || |
280 | 27.5k | Curl_uint32_bset_resize(&multi->msgsent, xfer_table_size) || |
281 | 27.5k | Curl_uint32_tbl_resize(&multi->xfers, xfer_table_size)) |
282 | 51 | goto error; |
283 | | |
284 | 27.5k | #ifdef DEBUGBUILD |
285 | 27.5k | { |
286 | 27.5k | const char *p; |
287 | 27.5k | if(getenv("CURL_DEBUG")) |
288 | 0 | multi->admin->set.verbose = TRUE; |
289 | 27.5k | p = getenv("CURL_DBG_MAX_TOTAL_CONNECTIONS"); |
290 | 27.5k | if(p) { |
291 | 0 | curl_off_t l; |
292 | 0 | if(!curlx_str_number(&p, &l, UINT32_MAX)) { |
293 | 0 | multi->max_total_connections = (uint32_t)l; |
294 | 0 | } |
295 | 0 | } |
296 | 27.5k | } |
297 | 27.5k | #endif |
298 | 27.5k | Curl_uint32_tbl_add(&multi->xfers, multi->admin, &multi->admin->mid); |
299 | 27.5k | Curl_uint32_bset_add(&multi->process, multi->admin->mid); |
300 | | |
301 | 27.5k | Curl_cpool_init(&multi->cpool, NULL, chashsize); |
302 | | |
303 | 27.5k | #ifdef USE_SSL |
304 | 27.5k | if(Curl_ssl_scache_create(sesssize, 2, &multi->ssl_scache)) |
305 | 24 | goto error; |
306 | | #else |
307 | | (void)sesssize; |
308 | | #endif |
309 | | |
310 | | #ifdef USE_WINSOCK |
311 | | multi->wsa_event = WSACreateEvent(); |
312 | | if(multi->wsa_event == WSA_INVALID_EVENT) |
313 | | goto error; |
314 | | #endif |
315 | 27.4k | #ifdef ENABLE_WAKEUP |
316 | | /* When enabled, rely on this to work. We ignore this in previous |
317 | | * versions, but that seems an unnecessary complication. */ |
318 | 27.4k | if(Curl_wakeup_init(multi->wakeup_pair, TRUE) < 0) |
319 | 0 | goto error; |
320 | 27.4k | #endif |
321 | 27.4k | #ifdef ENABLE_INTERNAL_WAKEUP |
322 | 27.4k | if(Curl_wakeup_init(multi->wakeup_internal, TRUE) < 0) |
323 | 0 | goto error; |
324 | 27.4k | #endif |
325 | | |
326 | 27.4k | if(Curl_probeipv6(multi)) |
327 | 0 | goto error; |
328 | | |
329 | 27.4k | #ifdef USE_RESOLV_THREADED |
330 | 27.4k | if(xfer_table_size < CURL_XFER_TABLE_SIZE) { /* easy multi */ |
331 | 1.71k | if(Curl_async_thrdd_multi_init(multi, 0, 2, 10)) |
332 | 1 | goto error; |
333 | 1.71k | } |
334 | 25.7k | else { /* real multi handle */ |
335 | 25.7k | if(Curl_async_thrdd_multi_init(multi, 0, 20, 2000)) |
336 | 14 | goto error; |
337 | 25.7k | } |
338 | 27.4k | #endif |
339 | | |
340 | 27.4k | return multi; |
341 | | |
342 | 90 | error: |
343 | | |
344 | 90 | #ifdef USE_RESOLV_THREADED |
345 | 90 | Curl_async_thrdd_multi_destroy(multi, TRUE); |
346 | 90 | #endif |
347 | 90 | Curl_multi_ev_cleanup(multi); |
348 | 90 | Curl_hash_destroy(&multi->proto_hash); |
349 | 90 | Curl_dnscache_destroy(&multi->dnscache); |
350 | 90 | Curl_cpool_destroy(&multi->cpool, multi->admin); |
351 | 90 | Curl_cshutdn_destroy(&multi->cshutdn, multi->admin); |
352 | 90 | #ifdef USE_SSL |
353 | 90 | Curl_ssl_scache_destroy(multi->ssl_scache); |
354 | 90 | #endif |
355 | 90 | if(multi->admin) { |
356 | 90 | Curl_multi_ev_xfer_done(multi, multi->admin); |
357 | 90 | multi->admin->multi = NULL; |
358 | 90 | Curl_close(&multi->admin); |
359 | 90 | } |
360 | 90 | Curl_mntfy_cleanup(multi); |
361 | | |
362 | 90 | Curl_uint32_bset_destroy(&multi->process); |
363 | 90 | Curl_uint32_bset_destroy(&multi->dirty); |
364 | 90 | Curl_uint32_bset_destroy(&multi->pending); |
365 | 90 | Curl_uint32_bset_destroy(&multi->msgsent); |
366 | 90 | Curl_uint32_tbl_destroy(&multi->xfers); |
367 | 90 | #ifdef ENABLE_WAKEUP |
368 | 90 | Curl_wakeup_destroy(multi->wakeup_pair); |
369 | 90 | #endif |
370 | 90 | #ifdef ENABLE_INTERNAL_WAKEUP |
371 | 90 | Curl_wakeup_destroy(multi->wakeup_internal); |
372 | 90 | #endif |
373 | | |
374 | 90 | curlx_free(multi); |
375 | 90 | return NULL; |
376 | 27.4k | } |
377 | | |
378 | | CURLM *curl_multi_init(void) |
379 | 25.8k | { |
380 | 25.8k | return Curl_multi_handle(CURL_XFER_TABLE_SIZE, |
381 | 25.8k | CURL_SOCKET_HASH_TABLE_SIZE, |
382 | 25.8k | CURL_CONNECTION_HASH_SIZE, |
383 | 25.8k | CURL_DNS_HASH_SIZE, |
384 | 25.8k | CURL_TLS_SESSION_SIZE); |
385 | 25.8k | } |
386 | | |
387 | | #if defined(DEBUGBUILD) && defined(CURLVERBOSE) |
388 | | static void multi_warn_debug(struct Curl_multi *multi, struct Curl_easy *data) |
389 | 637k | { |
390 | 637k | if(!multi->warned) { |
391 | 27.4k | infof(data, "!!! WARNING !!!"); |
392 | 27.4k | infof(data, "This is a debug build of libcurl, " |
393 | 27.4k | "do not use in production."); |
394 | 27.4k | multi->warned = TRUE; |
395 | 27.4k | } |
396 | 637k | } |
397 | | #else |
398 | | #define multi_warn_debug(x, y) Curl_nop_stmt |
399 | | #endif |
400 | | |
401 | | bool Curl_is_connecting(struct Curl_easy *data) |
402 | 1.12M | { |
403 | 1.12M | return (data->mstate < MSTATE_DO) || data->state.still_connecting; |
404 | 1.12M | } |
405 | | |
406 | | static CURLMcode multi_assess_wakeup(struct Curl_multi *multi) |
407 | 141k | { |
408 | 141k | #ifdef ENABLE_INTERNAL_WAKEUP |
409 | 141k | if(multi->socket_cb) |
410 | 35.5k | return Curl_multi_ev_assess_xfer(multi, multi->admin); |
411 | | #else |
412 | | (void)multi; |
413 | | #endif |
414 | 106k | return CURLM_OK; |
415 | 141k | } |
416 | | |
417 | | static CURLMcode multi_xfers_add(struct Curl_multi *multi, |
418 | | struct Curl_easy *data) |
419 | 27.4k | { |
420 | 27.4k | uint32_t capacity = Curl_uint32_tbl_capacity(&multi->xfers); |
421 | 27.4k | uint32_t new_size = 0; |
422 | | /* Prepare to make this into a CURLMOPT_MAX_TRANSFERS, because some |
423 | | * applications may want to prevent a run-away of their memory use. */ |
424 | | /* UINT_MAX is our "invalid" id, do not let the table grow up to that. */ |
425 | 27.4k | const uint32_t max_capacity = UINT_MAX - 1; |
426 | | |
427 | 27.4k | if(capacity < max_capacity) { |
428 | | /* We want `multi->xfers` to have "sufficient" free rows, so that we do |
429 | | * not have to reuse the `mid` from a removed easy right away. |
430 | | * Check if an 8th of the capacity is still free */ |
431 | 27.4k | uint32_t used = Curl_uint32_tbl_count(&multi->xfers); |
432 | 27.4k | uint32_t unused = capacity - used; |
433 | 27.4k | uint32_t min_unused = CURLMAX(capacity >> 3, 4); |
434 | 27.4k | if(unused < min_unused) { |
435 | | /* Grow by 50% of current capacity, in range of [128, 2048], |
436 | | * which means the table grows max by 16kb on 64-bit arch. */ |
437 | 0 | uint32_t growth = CURLMIN(CURLMAX(capacity >> 1, 128), 2048); |
438 | | /* Make sure the uint arithmetic here works on the corner |
439 | | * cases where we are close to max_capacity or UINT_MAX */ |
440 | 0 | if((max_capacity - growth) <= capacity) |
441 | 0 | new_size = max_capacity; |
442 | 0 | else |
443 | 0 | new_size = capacity + growth; |
444 | 0 | } |
445 | 27.4k | } |
446 | | |
447 | 27.4k | if(new_size > capacity) { |
448 | | /* Grow the bitsets first. Should one fail, we do not need |
449 | | * to downsize the already resized ones. The sets continue |
450 | | * to work properly when larger than the table, but not |
451 | | * the other way around. */ |
452 | 0 | CURL_TRC_M(data, "increasing xfer table size to %u", new_size); |
453 | 0 | if(Curl_uint32_bset_resize(&multi->process, new_size) || |
454 | 0 | Curl_uint32_bset_resize(&multi->dirty, new_size) || |
455 | 0 | Curl_uint32_bset_resize(&multi->pending, new_size) || |
456 | 0 | Curl_uint32_bset_resize(&multi->msgsent, new_size) || |
457 | 0 | Curl_uint32_tbl_resize(&multi->xfers, new_size)) |
458 | 0 | return CURLM_OUT_OF_MEMORY; |
459 | 0 | } |
460 | | |
461 | | /* Insert the easy into the table now */ |
462 | 27.4k | if(!Curl_uint32_tbl_add(&multi->xfers, data, &data->mid)) { |
463 | | /* MUST only happen when table is full */ |
464 | 0 | DEBUGASSERT(Curl_uint32_tbl_capacity(&multi->xfers) <= |
465 | 0 | Curl_uint32_tbl_count(&multi->xfers)); |
466 | 0 | return CURLM_OUT_OF_MEMORY; |
467 | 0 | } |
468 | 27.4k | return CURLM_OK; |
469 | 27.4k | } |
470 | | |
471 | | CURLMcode Curl_multi_add_handle(struct Curl_multi *multi, |
472 | | struct Curl_easy *data) |
473 | 27.4k | { |
474 | 27.4k | CURLMcode mresult; |
475 | | |
476 | | /* Prevent users from adding same easy handle more than once and prevent |
477 | | adding to more than one multi stack */ |
478 | 27.4k | if(data->multi) |
479 | 0 | return CURLM_ADDED_ALREADY; |
480 | | |
481 | 27.4k | if(multi->dead) { |
482 | | /* a "dead" handle cannot get added transfers while any existing easy |
483 | | handles are still alive - but if there are none alive anymore, it is |
484 | | fine to start over and unmark the "deadness" of this handle. |
485 | | This means only the admin handle MUST be present. */ |
486 | 0 | if((Curl_uint32_tbl_count(&multi->xfers) != 1) || |
487 | 0 | !Curl_uint32_tbl_contains(&multi->xfers, 0)) |
488 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
489 | 0 | multi->dead = FALSE; |
490 | 0 | Curl_uint32_bset_clear(&multi->process); |
491 | 0 | Curl_uint32_bset_clear(&multi->dirty); |
492 | 0 | Curl_uint32_bset_clear(&multi->pending); |
493 | 0 | Curl_uint32_bset_clear(&multi->msgsent); |
494 | 0 | } |
495 | | |
496 | | /* if the handle has a connection still attached (it is/was a connect-only |
497 | | handle used with curl_easy_send/recv) then disconnect now */ |
498 | 27.4k | if(data->conn) { |
499 | 0 | struct connectdata *conn = data->conn; |
500 | 0 | Curl_detach_connection(data); |
501 | 0 | Curl_conn_close(data, conn, TRUE); |
502 | 0 | } |
503 | | |
504 | 27.4k | if(data->multi_easy) { |
505 | | /* if this easy handle was previously used for curl_easy_perform(), there |
506 | | is a private multi handle here that we can kill */ |
507 | 0 | curl_multi_cleanup(data->multi_easy); |
508 | 0 | data->multi_easy = NULL; |
509 | 0 | } |
510 | | |
511 | | /* Insert the easy into the multi->xfers table, assigning it a `mid`. */ |
512 | 27.4k | if(multi_xfers_add(multi, data)) |
513 | 0 | return CURLM_OUT_OF_MEMORY; |
514 | | |
515 | | /* Initialize timeouts for this handle */ |
516 | 27.4k | multi_timeouts_init(data); |
517 | | |
518 | | /* |
519 | | * No failure allowed in this function beyond this point. No modification |
520 | | * of easy nor multi handle allowed before this except for potential |
521 | | * multi's connection pool growing which will not be undone in this |
522 | | * function no matter what. |
523 | | */ |
524 | 27.4k | if(data->set.errorbuffer) |
525 | 0 | data->set.errorbuffer[0] = 0; |
526 | | |
527 | 27.4k | data->state.os_errno = 0; |
528 | | |
529 | | /* make the Curl_easy refer back to this multi handle - before |
530 | | Curl_expire() is called. */ |
531 | 27.4k | data->multi = multi; |
532 | | |
533 | | /* set the easy handle */ |
534 | 27.4k | multistate(data, MSTATE_INIT); |
535 | | /* not yet passed INIT state */ |
536 | 27.4k | data->state.really_alive = FALSE; |
537 | | |
538 | | #ifdef USE_LIBPSL |
539 | | /* Do the same for PSL. */ |
540 | | if(data->share && (data->share->specifier & (1 << CURL_LOCK_DATA_PSL))) |
541 | | data->psl = &data->share->psl; |
542 | | else |
543 | | data->psl = &multi->psl; |
544 | | #endif |
545 | | |
546 | | /* add the easy handle to the process set */ |
547 | 27.4k | Curl_uint32_bset_add(&multi->process, data->mid); |
548 | 27.4k | ++multi->xfers_alive; |
549 | 27.4k | ++multi->xfers_total_ever; |
550 | | |
551 | 27.4k | Curl_cpool_xfer_init(data); |
552 | 27.4k | multi_warn_debug(multi, data); |
553 | | |
554 | | /* Make sure the new handle will run */ |
555 | 27.4k | Curl_multi_mark_dirty(data); |
556 | | |
557 | | /* Necessary in event based processing, where dirty handles trigger |
558 | | * a timeout callback invocation. */ |
559 | 27.4k | mresult = Curl_update_timer(multi); |
560 | 27.4k | if(mresult) { |
561 | | /* undo the additions above */ |
562 | 0 | Curl_uint32_bset_remove(&multi->process, data->mid); |
563 | 0 | Curl_uint32_bset_remove(&multi->dirty, data->mid); |
564 | 0 | --multi->xfers_alive; |
565 | 0 | data->multi = NULL; /* not anymore */ |
566 | 0 | Curl_uint32_tbl_remove(&multi->xfers, data->mid); |
567 | 0 | data->mid = UINT32_MAX; |
568 | 0 | return mresult; |
569 | 0 | } |
570 | | |
571 | | /* The admin handle only ever has default timeouts set. To improve the |
572 | | state somewhat we clone the timeouts from each added handle so that the |
573 | | admin handle always has the same timeouts as the most recently added |
574 | | easy handle. */ |
575 | 27.4k | multi->admin->set.timeout = data->set.timeout; |
576 | 27.4k | multi->admin->set.server_response_timeout = |
577 | 27.4k | data->set.server_response_timeout; |
578 | 27.4k | multi->admin->set.no_signal = data->set.no_signal; |
579 | | |
580 | 27.4k | CURL_TRC_M(data, "added to multi, mid=%u, running=%u, total=%u", |
581 | 27.4k | data->mid, multi_xfers_running(multi), |
582 | 27.4k | Curl_uint32_tbl_count(&multi->xfers)); |
583 | 27.4k | return CURLM_OK; |
584 | 27.4k | } |
585 | | |
586 | | CURLMcode curl_multi_add_handle(CURLM *m, CURL *curl) |
587 | 25.7k | { |
588 | 25.7k | struct Curl_mapi_guard guard; |
589 | 25.7k | CURLMcode mresult; |
590 | | |
591 | 25.7k | if(CURL_MAPI_ENTER(&guard, m, multi_add_handle, &mresult)) { |
592 | 25.7k | struct Curl_easy *data = curl; |
593 | | /* Verify that we got a somewhat good easy handle too */ |
594 | 25.7k | if(!GOOD_EASY_HANDLE(data) || data->state.internal) |
595 | 0 | mresult = CURLM_BAD_EASY_HANDLE; |
596 | 25.7k | else |
597 | 25.7k | mresult = Curl_multi_add_handle(m, data); |
598 | 25.7k | } |
599 | 25.7k | CURL_MAPI_LEAVE(&guard); |
600 | 25.7k | return mresult; |
601 | 25.7k | } |
602 | | |
603 | | struct multi_done_ctx { |
604 | | BIT(premature); |
605 | | }; |
606 | | |
607 | | static bool multi_conn_should_close(struct connectdata *conn, |
608 | | struct Curl_easy *data, |
609 | | bool premature) |
610 | 36.9k | { |
611 | | /* if conn->bits.close is TRUE, it means that the connection should be |
612 | | closed in spite of everything else. */ |
613 | 36.9k | if(conn->bits.close) |
614 | 29.5k | return TRUE; |
615 | | |
616 | | /* if data->set.reuse_forbid is TRUE, it means the libcurl client has forced |
617 | | us to close this connection. This is ignored for requests taking place in |
618 | | a NEGOTIATE authentication handshake. */ |
619 | 7.38k | if(data->set.reuse_forbid |
620 | | #ifdef USE_SPNEGO |
621 | | && !(conn->http_negotiate_state == CURL_NEGO_AUTH_RECV || |
622 | | conn->proxy_negotiate_state == CURL_NEGO_AUTH_RECV) |
623 | | #endif |
624 | 7.38k | ) |
625 | 67 | return TRUE; |
626 | | |
627 | | /* Unless this connection is for a "connect-only" transfer, it |
628 | | * needs to be closed if the protocol handler does not support reuse. */ |
629 | 7.31k | if(!data->set.connect_only && conn->scheme && |
630 | 6.80k | !(conn->scheme->flags & PROTOPT_CONN_REUSE)) |
631 | 0 | return TRUE; |
632 | | |
633 | | /* if premature is TRUE, it means this connection was said to be DONE before |
634 | | the entire request operation is complete and thus we cannot know in what |
635 | | state it is for reusing, so we are forced to close it. In a perfect world |
636 | | we can add code that keep track of if we really must close it here or not, |
637 | | but currently we have no such detail knowledge. */ |
638 | 7.31k | if(premature && !Curl_conn_is_multiplex(conn)) |
639 | 1.13k | return TRUE; |
640 | | |
641 | 6.17k | return FALSE; |
642 | 7.31k | } |
643 | | |
644 | | static cpool_do_result multi_done_locked(struct Curl_easy *data, |
645 | | struct connectdata *conn, |
646 | | void *userdata, |
647 | | const struct curltime *pnow) |
648 | 36.9k | { |
649 | 36.9k | struct multi_done_ctx *mdctx = userdata; |
650 | | |
651 | 36.9k | (void)pnow; |
652 | 36.9k | CURL_TRC_M(data, "multi_done_locked, in use=%u", conn->attached_xfers); |
653 | 36.9k | if(CONN_INUSE(conn)) { |
654 | | /* Stop if still used. */ |
655 | 0 | CURL_TRC_M(data, "Connection still in use %u, no more multi_done now!", |
656 | 0 | conn->attached_xfers); |
657 | 0 | return CPOOL_DO_KEEP; |
658 | 0 | } |
659 | | |
660 | 36.9k | if(multi_conn_should_close(conn, data, (bool)mdctx->premature)) { |
661 | 30.7k | CURL_TRC_M(data, "multi_done, terminating conn #%" FMT_OFF_T " to %s:%u, " |
662 | 30.7k | "forbid=%d, close=%d, premature=%d, conn_multiplex=%d", |
663 | 30.7k | conn->connection_id, conn->origin->user_hostname, |
664 | 30.7k | conn->origin->port, |
665 | 30.7k | data->set.reuse_forbid, conn->bits.close, mdctx->premature, |
666 | 30.7k | Curl_conn_is_multiplex(conn)); |
667 | 30.7k | connclose(conn); |
668 | 30.7k | return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE; |
669 | 30.7k | } |
670 | 6.17k | else if(!Curl_conn_get_max_concurrent(data, conn)) { |
671 | 235 | CURL_TRC_M(data, "multi_done, conn #%" FMT_OFF_T " to %s:%u was shutdown" |
672 | 235 | " by server, not reusing", conn->connection_id, |
673 | 235 | conn->origin->user_hostname, conn->origin->port); |
674 | 235 | connclose(conn); |
675 | 235 | return mdctx->premature ? CPOOL_DO_TERMINATE : CPOOL_DO_CLOSE; |
676 | 235 | } |
677 | 5.94k | else { |
678 | 5.94k | return CPOOL_DO_IDLE; |
679 | 5.94k | } |
680 | 36.9k | } |
681 | | |
682 | | static CURLcode multi_done(struct Curl_easy *data, |
683 | | CURLcode status, /* an error if this is called |
684 | | after an error was detected */ |
685 | | bool premature) |
686 | 36.9k | { |
687 | 36.9k | CURLcode result; |
688 | 36.9k | struct connectdata *conn = data->conn; |
689 | 36.9k | const struct curltime *pnow = Curl_pgrs_now(data); |
690 | | |
691 | 36.9k | CURL_TRC_M(data, "multi_done: status: %d prem: %d done: %d", |
692 | 36.9k | (int)status, (int)premature, data->state.done); |
693 | | |
694 | 36.9k | if(data->state.done) { |
695 | | /* Stop if multi_done() has already been called */ |
696 | 0 | return CURLE_OK; |
697 | 0 | } |
698 | 36.9k | data->state.done = TRUE; /* called now! */ |
699 | | |
700 | | /* Shut down any ongoing async resolver operation. */ |
701 | 36.9k | Curl_resolv_shutdown_all(data); |
702 | | |
703 | | /* Cleanup possible redirect junk */ |
704 | 36.9k | curlx_safefree(data->req.newurl); |
705 | 36.9k | curlx_safefree(data->req.location); |
706 | | |
707 | 36.9k | switch(status) { |
708 | 5 | case CURLE_ABORTED_BY_CALLBACK: |
709 | 135 | case CURLE_READ_ERROR: |
710 | 194 | case CURLE_WRITE_ERROR: |
711 | | /* When we are aborted due to a callback return code it has to be counted |
712 | | as premature as there is trouble ahead if we do not. We have many |
713 | | callbacks and protocols work differently, we could potentially do this |
714 | | more fine-grained in the future. */ |
715 | 194 | premature = TRUE; |
716 | 194 | FALLTHROUGH(); |
717 | 36.9k | default: |
718 | 36.9k | break; |
719 | 36.9k | } |
720 | | |
721 | | /* this calls the protocol-specific function pointer previously set */ |
722 | 36.9k | if(conn && conn->scheme->run->done && (data->mstate >= MSTATE_PROTOCONNECT)) |
723 | 35.7k | result = conn->scheme->run->done(data, status, premature); |
724 | 1.14k | else |
725 | 1.14k | result = status; |
726 | | |
727 | 36.9k | if(data->mstate > MSTATE_CONNECTING && |
728 | 35.7k | (result != CURLE_ABORTED_BY_CALLBACK)) { |
729 | | /* avoid this if |
730 | | * - the transfer has not connected |
731 | | * - we already aborted by callback to avoid this calling another callback |
732 | | */ |
733 | 35.7k | int rc = Curl_pgrsDone(data, pnow); |
734 | 35.7k | if(!result && rc) |
735 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
736 | 35.7k | } |
737 | | |
738 | | /* Make sure that transfer client writes are really done now. */ |
739 | 36.9k | result = Curl_1st_fatal(result, Curl_xfer_write_done(data, premature)); |
740 | | |
741 | 36.9k | if(!result) |
742 | 17.4k | result = Curl_req_done(&data->req, data, premature); |
743 | | |
744 | 36.9k | if(conn) { |
745 | | /* Detach connection and decide what to do with it. */ |
746 | 36.9k | struct multi_done_ctx mdctx; |
747 | | |
748 | | /* Inform connection filters that this transfer is done and they |
749 | | * need to give up any references to `data`. */ |
750 | 36.9k | Curl_conn_ev_data_done(data, premature); |
751 | 36.9k | Curl_detach_connection(data); |
752 | 36.9k | Curl_dnscache_prune(data, pnow); |
753 | | |
754 | 36.9k | memset(&mdctx, 0, sizeof(mdctx)); |
755 | 36.9k | mdctx.premature = premature; |
756 | 36.9k | Curl_cpool_return(data, conn, multi_done_locked, &mdctx, pnow); |
757 | | |
758 | 36.9k | if(data->multi) |
759 | 36.9k | multi_schedule_pending(data->multi); |
760 | 36.9k | } |
761 | | |
762 | | /* flush the netrc cache */ |
763 | 36.9k | Curl_netrc_cleanup(&data->state.netrc); |
764 | 36.9k | return result; |
765 | 36.9k | } |
766 | | |
767 | | CURLMcode Curl_multi_remove_handle(struct Curl_multi *multi, |
768 | | struct Curl_easy *data) |
769 | 27.4k | { |
770 | 27.4k | CURLMcode mresult; |
771 | 27.4k | uint32_t mid; |
772 | | |
773 | | /* Prevent users from trying to remove same easy handle more than once */ |
774 | 27.4k | if(!data->multi) |
775 | 0 | return CURLM_OK; /* it is already removed so let's say it is fine! */ |
776 | | |
777 | | /* Prevent users from trying to remove an easy handle from the wrong multi */ |
778 | 27.4k | if(data->multi != multi) |
779 | 0 | return CURLM_BAD_EASY_HANDLE; |
780 | | |
781 | 27.4k | if(data->mid == UINT32_MAX) { |
782 | 0 | DEBUGASSERT(0); |
783 | 0 | return CURLM_INTERNAL_ERROR; |
784 | 0 | } |
785 | 27.4k | if(Curl_uint32_tbl_get(&multi->xfers, data->mid) != data) { |
786 | 0 | DEBUGASSERT(0); |
787 | 0 | return CURLM_INTERNAL_ERROR; |
788 | 0 | } |
789 | | |
790 | 27.4k | if(data->conn) { |
791 | 229 | bool premature = (data->mstate < MSTATE_COMPLETED); |
792 | | |
793 | | /* If the 'state' is not INIT or COMPLETED, we might need to do something |
794 | | nice to put the easy_handle in a good known state when this returns. */ |
795 | 229 | if(premature && (data->mstate > MSTATE_DO)) |
796 | 52 | streamclose(data->conn); |
797 | | |
798 | | /* multi_done() clears the association between the easy handle and the |
799 | | connection. |
800 | | Note that this ignores the return code because there is |
801 | | nothing really useful to do with it anyway! */ |
802 | 229 | (void)multi_done(data, (CURLcode)data->done, premature); |
803 | 229 | } |
804 | | |
805 | | /* The timer must be shut down before data->multi is set to NULL, else |
806 | | data's splaynode would remain in the splay tree after curl_easy_cleanup is |
807 | | called. Do it after multi_done() in case that sets another time! */ |
808 | 27.4k | Curl_expire_clear_all(data); |
809 | | |
810 | | /* In MSGSENT, it was deducted from `multi->xfers_alive` already. */ |
811 | 27.4k | if(data->mstate != MSTATE_MSGSENT) |
812 | 232 | --multi->xfers_alive; |
813 | | |
814 | 27.4k | if(data->state.really_alive) { |
815 | 232 | data->state.really_alive = FALSE; |
816 | 232 | --multi->xfers_really_alive; |
817 | 232 | if(!multi->xfers_really_alive) |
818 | 232 | (void)multi_assess_wakeup(multi); |
819 | 232 | } |
820 | | |
821 | 27.4k | Curl_wildcard_dtor(&data->wildcard); |
822 | | |
823 | 27.4k | data->mstate = MSTATE_COMPLETED; |
824 | | |
825 | | /* Remove the association between the connection and the handle */ |
826 | 27.4k | Curl_detach_connection(data); |
827 | | |
828 | | /* Tell event handling that this transfer is definitely going away */ |
829 | 27.4k | Curl_multi_ev_xfer_done(multi, data); |
830 | | /* Remove it from any outstanding notifications */ |
831 | 27.4k | Curl_mntfy_remove(data); |
832 | | |
833 | 27.4k | if(data->set.connect_only) { |
834 | 503 | if(data->multi_easy) { |
835 | 283 | if(data->state.last_conn_id != -1) { |
836 | | /* Mark any connect-only connection for closure */ |
837 | 265 | struct connectdata *conn; |
838 | 265 | (void)Curl_getconnectinfo(data, &conn); |
839 | 265 | if(conn && conn->bits.connect_only) |
840 | 243 | connclose(conn); |
841 | 265 | } |
842 | 283 | } |
843 | 220 | else { |
844 | | /* This removes a handle that was part the multi interface that used |
845 | | CONNECT_ONLY, that connection is now left alive but since this handle |
846 | | has bits.close set nothing can use that connection anymore and it is |
847 | | forbidden from reuse. This easy handle cannot find the connection |
848 | | anymore once removed from the multi handle |
849 | | |
850 | | Better close the connection here, at once. */ |
851 | 220 | struct connectdata *conn; |
852 | 220 | (void)Curl_getconnectinfo(data, &conn); |
853 | 220 | if(conn) |
854 | 181 | Curl_conn_close(data, conn, TRUE); |
855 | 220 | } |
856 | 503 | } |
857 | | |
858 | | #ifdef USE_LIBPSL |
859 | | /* Remove the PSL association. */ |
860 | | if(data->psl == &multi->psl) |
861 | | data->psl = NULL; |
862 | | #endif |
863 | | |
864 | | /* clear the association to this multi handle */ |
865 | 27.4k | mid = data->mid; |
866 | 27.4k | DEBUGASSERT(Curl_uint32_tbl_contains(&multi->xfers, mid)); |
867 | 27.4k | Curl_uint32_tbl_remove(&multi->xfers, mid); |
868 | 27.4k | Curl_uint32_bset_remove(&multi->process, mid); |
869 | 27.4k | Curl_uint32_bset_remove(&multi->dirty, mid); |
870 | 27.4k | Curl_uint32_bset_remove(&multi->pending, mid); |
871 | 27.4k | Curl_uint32_bset_remove(&multi->msgsent, mid); |
872 | 27.4k | data->multi = NULL; |
873 | 27.4k | data->mid = UINT32_MAX; |
874 | 27.4k | data->master_mid = UINT32_MAX; |
875 | | |
876 | | /* A pending transfer *might* be able to run now. */ |
877 | 27.4k | multi_schedule_pending(multi); |
878 | 27.4k | mresult = Curl_update_timer(multi); |
879 | 27.4k | if(mresult) |
880 | 0 | return mresult; |
881 | | |
882 | 27.4k | mresult = multi_assess_wakeup(multi); |
883 | 27.4k | if(mresult) { |
884 | 0 | failf(data, "error enabling wakeup listening: %d", mresult); |
885 | 0 | return mresult; |
886 | 0 | } |
887 | | |
888 | 27.4k | CURL_TRC_M(data, "removed from multi, mid=%u, running=%u, total=%u", |
889 | 27.4k | mid, multi_xfers_running(multi), |
890 | 27.4k | Curl_uint32_tbl_count(&multi->xfers)); |
891 | 27.4k | return CURLM_OK; |
892 | 27.4k | } |
893 | | |
894 | | CURLMcode curl_multi_remove_handle(CURLM *m, CURL *curl) |
895 | 25.7k | { |
896 | 25.7k | struct Curl_mapi_guard guard; |
897 | 25.7k | CURLMcode mresult; |
898 | | |
899 | 25.7k | if(CURL_MAPI_ENTER(&guard, m, multi_remove_handle, &mresult)) { |
900 | 25.7k | struct Curl_easy *data = curl; |
901 | 25.7k | if(!GOOD_EASY_HANDLE(data) || data->state.internal) |
902 | 0 | mresult = CURLM_BAD_EASY_HANDLE; |
903 | 25.7k | else |
904 | 25.7k | mresult = Curl_multi_remove_handle(m, data); |
905 | 25.7k | } |
906 | 25.7k | CURL_MAPI_LEAVE(&guard); |
907 | 25.7k | return mresult; |
908 | 25.7k | } |
909 | | |
910 | | /* Return TRUE if the application asked for multiplexing */ |
911 | | bool Curl_multiplex_wanted(const struct Curl_multi *multi) |
912 | 36.9k | { |
913 | 36.9k | return multi && multi->multiplexing; |
914 | 36.9k | } |
915 | | |
916 | | /* |
917 | | * Curl_detach_connection() removes the given transfer from the connection. |
918 | | * |
919 | | * This is the only function that should clear data->conn. This will |
920 | | * occasionally be called with the data->conn pointer already cleared. |
921 | | */ |
922 | | void Curl_detach_connection(struct Curl_easy *data) |
923 | 211k | { |
924 | 211k | struct connectdata *conn = data->conn; |
925 | 211k | if(conn) { |
926 | | /* this should never happen, prevent underflow */ |
927 | 99.4k | DEBUGASSERT(conn->attached_xfers); |
928 | 99.4k | if(conn->attached_xfers) { |
929 | 99.4k | conn->attached_xfers--; |
930 | 99.4k | if(!conn->attached_xfers) |
931 | 99.4k | conn->attached_multi = NULL; |
932 | 99.4k | } |
933 | 99.4k | } |
934 | 211k | data->conn = NULL; |
935 | 211k | } |
936 | | |
937 | | /* |
938 | | * Curl_attach_connection() attaches this transfer to this connection. |
939 | | * |
940 | | * This is the only function that should assign data->conn. |
941 | | * `matched == TRUE` means the transfer's properties match this |
942 | | * connection and it is not a temporary attach for maintenance. |
943 | | */ |
944 | | void Curl_attach_connection(struct Curl_easy *data, |
945 | | struct connectdata *conn, |
946 | | bool matched) |
947 | 99.4k | { |
948 | 99.4k | DEBUGASSERT(data); |
949 | 99.4k | DEBUGASSERT(!data->conn); |
950 | 99.4k | DEBUGASSERT(conn); |
951 | 99.4k | DEBUGASSERT(conn->attached_xfers < UINT32_MAX); |
952 | 99.4k | data->conn = conn; |
953 | 99.4k | if(matched) { |
954 | 37.2k | data->state.last_conn_id = conn->connection_id; |
955 | 37.2k | data->state.last_cpid = conn->cpid; |
956 | 37.2k | } |
957 | 62.2k | else |
958 | 99.4k | DEBUGASSERT(!data->mid); /* admin handle */ |
959 | 99.4k | conn->attached_xfers++; |
960 | | /* all attached transfers must be from the same multi */ |
961 | 99.4k | if(!conn->attached_multi) |
962 | 99.4k | conn->attached_multi = data->multi; |
963 | 99.4k | DEBUGASSERT(conn->attached_multi == data->multi); |
964 | | |
965 | 99.4k | if(conn->scheme && conn->scheme->run->attach) |
966 | 0 | conn->scheme->run->attach(data, conn); |
967 | 99.4k | } |
968 | | |
969 | | static CURLcode multi_cfchain_adjust_ps(struct Curl_easy *data, |
970 | | struct Curl_cfchain *chain, |
971 | | bool recv_blocked, |
972 | | bool send_blocked, |
973 | | struct easy_pollset *ps) |
974 | 84 | { |
975 | 84 | CURLcode result; |
976 | 84 | if(chain->sock != CURL_SOCKET_BAD) { |
977 | 84 | if(recv_blocked && Curl_pollset_want_recv(data, ps, chain->sock) && |
978 | 83 | !Curl_cfchain_wants_maintenance(chain)) { |
979 | 83 | result = Curl_pollset_remove_in(data, ps, chain->sock); |
980 | 83 | if(result) |
981 | 0 | return result; |
982 | 83 | } |
983 | 84 | if(send_blocked && Curl_pollset_want_send(data, ps, chain->sock)) { |
984 | 0 | result = Curl_pollset_remove_out(data, ps, chain->sock); |
985 | 0 | if(result) |
986 | 0 | return result; |
987 | 0 | } |
988 | 84 | } |
989 | 84 | return CURLE_OK; |
990 | 84 | } |
991 | | |
992 | | /* adjust pollset for rate limits/pauses */ |
993 | | static CURLcode multi_adjust_pollset(struct Curl_easy *data, |
994 | | struct easy_pollset *ps) |
995 | 262k | { |
996 | 262k | struct connectdata *conn = data->conn; |
997 | 262k | bool send_blocked, recv_blocked; |
998 | 262k | CURLcode result = CURLE_OK; |
999 | | |
1000 | 262k | if(!ps->n) |
1001 | 12.8k | return CURLE_OK; |
1002 | | |
1003 | 249k | recv_blocked = (Curl_rlimit_avail(&data->progress.dl.rlimit, NULL) <= 0); |
1004 | 249k | send_blocked = (Curl_rlimit_avail(&data->progress.ul.rlimit, NULL) <= 0); |
1005 | 249k | if(send_blocked || recv_blocked) { |
1006 | 84 | result = multi_cfchain_adjust_ps( |
1007 | 84 | data, &conn->chain, recv_blocked, send_blocked, ps); |
1008 | 84 | } |
1009 | | |
1010 | | /* Not blocked and wanting to receive. If there is data pending |
1011 | | * in the connection filters, make transfer run again. */ |
1012 | 249k | if(!recv_blocked && (data->mstate != MSTATE_RATELIMITING) && |
1013 | 249k | Curl_pollset_want_recv(data, ps, conn->chain.sock) && |
1014 | 11.0k | Curl_conn_data_pending(data)) { |
1015 | 3 | CURL_TRC_M(data, "pollset[] has POLLIN, but there is still " |
1016 | 3 | "buffered input -> mark as dirty"); |
1017 | 3 | Curl_multi_mark_dirty(data); |
1018 | 3 | } |
1019 | 249k | return result; |
1020 | 262k | } |
1021 | | |
1022 | | static CURLcode mstate_connecting_pollset(struct Curl_easy *data, |
1023 | | struct easy_pollset *ps) |
1024 | 17.4k | { |
1025 | 17.4k | struct connectdata *conn = data->conn; |
1026 | 17.4k | CURLcode result = CURLE_OK; |
1027 | | |
1028 | 17.4k | if(Curl_xfer_recv_is_paused(data)) |
1029 | 0 | return CURLE_OK; |
1030 | | /* If a socket is set, receiving is default. If the socket |
1031 | | * has not been determined yet (eyeballing), always ask the |
1032 | | * connection filters for what to monitor. */ |
1033 | 17.4k | if(conn->chain.sock != CURL_SOCKET_BAD) { |
1034 | 0 | result = Curl_pollset_change(data, ps, conn->chain.sock, CURL_POLL_IN, 0); |
1035 | 0 | if(!result) |
1036 | 0 | result = multi_adjust_pollset(data, ps); |
1037 | 0 | } |
1038 | 17.4k | if(!result) |
1039 | 17.4k | result = Curl_conn_adjust_pollset(data, conn, ps); |
1040 | 17.4k | return result; |
1041 | 17.4k | } |
1042 | | |
1043 | | static CURLcode mstate_protocol_pollset(struct Curl_easy *data, |
1044 | | struct easy_pollset *ps) |
1045 | 0 | { |
1046 | 0 | struct connectdata *conn = data->conn; |
1047 | 0 | CURLcode result = CURLE_OK; |
1048 | |
|
1049 | 0 | if(conn->scheme->run->proto_pollset) |
1050 | 0 | result = conn->scheme->run->proto_pollset(data, ps); |
1051 | 0 | else { |
1052 | 0 | curl_socket_t sockfd = conn->chain.sock; |
1053 | 0 | if(sockfd != CURL_SOCKET_BAD) { |
1054 | | /* Default is to wait to something from the server */ |
1055 | 0 | result = Curl_pollset_change(data, ps, sockfd, CURL_POLL_IN, 0); |
1056 | 0 | } |
1057 | 0 | } |
1058 | 0 | if(!result) |
1059 | 0 | result = multi_adjust_pollset(data, ps); |
1060 | 0 | if(!result) |
1061 | 0 | result = Curl_conn_adjust_pollset(data, conn, ps); |
1062 | 0 | return result; |
1063 | 0 | } |
1064 | | |
1065 | | static CURLcode mstate_do_pollset(struct Curl_easy *data, |
1066 | | struct easy_pollset *ps) |
1067 | 0 | { |
1068 | 0 | struct connectdata *conn = data->conn; |
1069 | 0 | CURLcode result = CURLE_OK; |
1070 | |
|
1071 | 0 | if(conn->scheme->run->doing_pollset) |
1072 | 0 | result = conn->scheme->run->doing_pollset(data, ps); |
1073 | 0 | else if(conn->bits.want_send) { |
1074 | | /* Default is that we want to send something to the server */ |
1075 | 0 | result = Curl_pollset_add_out(data, ps, conn->chain.sock); |
1076 | 0 | } |
1077 | 0 | if(!result) |
1078 | 0 | result = multi_adjust_pollset(data, ps); |
1079 | 0 | if(!result) |
1080 | 0 | result = Curl_conn_adjust_pollset(data, conn, ps); |
1081 | 0 | return result; |
1082 | 0 | } |
1083 | | |
1084 | | static CURLcode mstate_domore_pollset(struct Curl_easy *data, |
1085 | | struct easy_pollset *ps) |
1086 | 0 | { |
1087 | 0 | struct connectdata *conn = data->conn; |
1088 | 0 | CURLcode result = CURLE_OK; |
1089 | |
|
1090 | 0 | if(conn->scheme->run->domore_pollset) |
1091 | 0 | result = conn->scheme->run->domore_pollset(data, ps); |
1092 | 0 | else if(conn->bits.want_send) { |
1093 | | /* Default is that we want to send something to the server */ |
1094 | 0 | result = Curl_pollset_add_out(data, ps, conn->chain.sock); |
1095 | 0 | } |
1096 | 0 | if(!result) |
1097 | 0 | result = multi_adjust_pollset(data, ps); |
1098 | 0 | if(!result) |
1099 | 0 | result = Curl_conn_adjust_pollset(data, conn, ps); |
1100 | 0 | return result; |
1101 | 0 | } |
1102 | | |
1103 | | static CURLcode mstate_perform_pollset(struct Curl_easy *data, |
1104 | | struct easy_pollset *ps) |
1105 | 262k | { |
1106 | 262k | struct connectdata *conn = data->conn; |
1107 | 262k | CURLcode result = CURLE_OK; |
1108 | | |
1109 | 262k | if(conn->scheme->run->perform_pollset) |
1110 | 262k | result = conn->scheme->run->perform_pollset(data, ps); |
1111 | 0 | else { |
1112 | | /* Default is to obey the request flags for send/recv */ |
1113 | 0 | if(Curl_req_want_recv(data) && conn->bits.want_recv) { |
1114 | 0 | result = Curl_pollset_add_in(data, ps, conn->chain.sock); |
1115 | 0 | } |
1116 | 0 | if(!result && Curl_req_want_send(data) && conn->bits.want_send) { |
1117 | 0 | result = Curl_pollset_add_out(data, ps, conn->chain.sock); |
1118 | 0 | } |
1119 | 0 | } |
1120 | 262k | if(!result) |
1121 | 262k | result = multi_adjust_pollset(data, ps); |
1122 | 262k | if(!result) |
1123 | 262k | result = Curl_conn_adjust_pollset(data, conn, ps); |
1124 | 262k | return result; |
1125 | 262k | } |
1126 | | |
1127 | | static CURLcode mstate_ratelimit_pollset(struct Curl_easy *data, |
1128 | | struct easy_pollset *ps) |
1129 | 0 | { |
1130 | 0 | struct connectdata *conn = data->conn; |
1131 | 0 | CURLcode result = CURLE_OK; |
1132 | | |
1133 | | /* Normally, we do not want to poll sockets for a ratelimited transfer, |
1134 | | * but if any filter wants maintenance, add the socket for input. */ |
1135 | 0 | if(conn && Curl_cfchain_wants_maintenance(&conn->chain)) { |
1136 | 0 | if(conn->chain.sock != CURL_SOCKET_BAD) |
1137 | 0 | result = Curl_pollset_add_in(data, ps, conn->chain.sock); |
1138 | 0 | } |
1139 | 0 | if(!result) |
1140 | 0 | result = multi_adjust_pollset(data, ps); |
1141 | 0 | if(!result) |
1142 | 0 | result = Curl_conn_adjust_pollset(data, conn, ps); |
1143 | 0 | return result; |
1144 | 0 | } |
1145 | | |
1146 | | #ifdef CURLVERBOSE |
1147 | | static size_t multi_timeouts_count(struct expire_timers *timeouts) |
1148 | 24.0k | { |
1149 | 24.0k | size_t n = 0; |
1150 | 24.0k | uint8_t eid = timeouts->first; |
1151 | 79.6k | for(; eid < EXPIRE_LAST; eid = timeouts->next[eid]) |
1152 | 55.6k | ++n; |
1153 | 24.0k | return n; |
1154 | 24.0k | } |
1155 | | #endif |
1156 | | |
1157 | | /* Initializes `poll_set` with the current socket poll actions needed |
1158 | | * for transfer `data`. */ |
1159 | | CURLMcode Curl_multi_pollset(struct Curl_easy *data, |
1160 | | struct easy_pollset *ps) |
1161 | 451k | { |
1162 | 451k | CURLcode result = CURLE_OK; |
1163 | | |
1164 | 451k | Curl_pollset_reset(ps); |
1165 | 451k | #ifdef ENABLE_INTERNAL_WAKEUP |
1166 | | /* The admin handle always listens on the wakeup socket when there |
1167 | | * are transfers alive. */ |
1168 | 451k | if(data->multi && (data == data->multi->admin) && |
1169 | 168k | data->multi->xfers_really_alive) { |
1170 | 163k | CURL_TRC_M(data, "adding wakeup, %u xfers really alive", |
1171 | 163k | data->multi->xfers_really_alive); |
1172 | 163k | result = Curl_pollset_add_in(data, ps, data->multi->wakeup_internal[0]); |
1173 | 163k | } |
1174 | 451k | #endif |
1175 | | /* If the transfer has no connection, this is fine. Happens when |
1176 | | called via curl_multi_remove_handle() => Curl_multi_ev_assess() => |
1177 | | Curl_multi_pollset(). */ |
1178 | 451k | if(!result && data->conn) { |
1179 | 279k | switch(data->mstate) { |
1180 | 0 | case MSTATE_INIT: |
1181 | 0 | case MSTATE_PENDING: |
1182 | 0 | case MSTATE_SETUP: |
1183 | 0 | case MSTATE_CONNECT: |
1184 | | /* nothing to poll for yet */ |
1185 | 0 | break; |
1186 | | |
1187 | 17.4k | case MSTATE_CONNECTING: |
1188 | 17.4k | result = mstate_connecting_pollset(data, ps); |
1189 | 17.4k | break; |
1190 | | |
1191 | 0 | case MSTATE_PROTOCONNECT: |
1192 | 0 | case MSTATE_PROTOCONNECTING: |
1193 | 0 | result = mstate_protocol_pollset(data, ps); |
1194 | 0 | break; |
1195 | | |
1196 | 0 | case MSTATE_DO: |
1197 | 0 | case MSTATE_DOING: |
1198 | 0 | result = mstate_do_pollset(data, ps); |
1199 | 0 | break; |
1200 | | |
1201 | 0 | case MSTATE_DOING_MORE: |
1202 | 0 | result = mstate_domore_pollset(data, ps); |
1203 | 0 | break; |
1204 | | |
1205 | 0 | case MSTATE_DID: /* same as PERFORMING in regard to polling */ |
1206 | 262k | case MSTATE_PERFORMING: |
1207 | 262k | result = mstate_perform_pollset(data, ps); |
1208 | 262k | break; |
1209 | | |
1210 | 0 | case MSTATE_RATELIMITING: |
1211 | 0 | result = mstate_ratelimit_pollset(data, ps); |
1212 | 0 | break; |
1213 | | |
1214 | 0 | case MSTATE_DONE: |
1215 | 0 | case MSTATE_COMPLETED: |
1216 | 0 | case MSTATE_MSGSENT: |
1217 | | /* nothing more to poll for */ |
1218 | 0 | break; |
1219 | | |
1220 | 0 | default: |
1221 | 0 | failf(data, "multi_getsock: unexpected multi state %d", |
1222 | 0 | (int)data->mstate); |
1223 | 0 | DEBUGASSERT(0); |
1224 | 0 | break; |
1225 | 279k | } |
1226 | 279k | } |
1227 | | |
1228 | 451k | if(result) { |
1229 | 0 | if(result == CURLE_OUT_OF_MEMORY) |
1230 | 0 | return CURLM_OUT_OF_MEMORY; |
1231 | 0 | failf(data, "error determining pollset: %d", (int)result); |
1232 | 0 | return CURLM_INTERNAL_ERROR; |
1233 | 0 | } |
1234 | | |
1235 | 451k | #ifdef CURLVERBOSE |
1236 | 451k | if(CURL_TRC_M_is_verbose(data)) { |
1237 | 24.0k | size_t timeout_count = multi_timeouts_count(&data->state.timeouts); |
1238 | 24.0k | switch(ps->n) { |
1239 | 5.69k | case 0: |
1240 | 5.69k | CURL_TRC_M(data, "pollset[], timeouts=%zu, paused %d/%d (r/w)", |
1241 | 5.69k | timeout_count, |
1242 | 5.69k | Curl_xfer_send_is_paused(data), |
1243 | 5.69k | Curl_xfer_recv_is_paused(data)); |
1244 | 5.69k | break; |
1245 | 18.3k | case 1: |
1246 | 18.3k | CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu", |
1247 | 18.3k | ps->sockets[0], |
1248 | 18.3k | (ps->actions[0] & CURL_POLL_IN) ? "IN" : "", |
1249 | 18.3k | (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "", |
1250 | 18.3k | timeout_count); |
1251 | 18.3k | break; |
1252 | 0 | case 2: |
1253 | 0 | CURL_TRC_M(data, "pollset[fd=%" FMT_SOCKET_T " %s%s, " |
1254 | 0 | "fd=%" FMT_SOCKET_T " %s%s], timeouts=%zu", |
1255 | 0 | ps->sockets[0], |
1256 | 0 | (ps->actions[0] & CURL_POLL_IN) ? "IN" : "", |
1257 | 0 | (ps->actions[0] & CURL_POLL_OUT) ? "OUT" : "", |
1258 | 0 | ps->sockets[1], |
1259 | 0 | (ps->actions[1] & CURL_POLL_IN) ? "IN" : "", |
1260 | 0 | (ps->actions[1] & CURL_POLL_OUT) ? "OUT" : "", |
1261 | 0 | timeout_count); |
1262 | 0 | break; |
1263 | 0 | default: |
1264 | 0 | CURL_TRC_M(data, "pollset[fds=%u], timeouts=%zu", ps->n, timeout_count); |
1265 | 0 | break; |
1266 | 24.0k | } |
1267 | 24.0k | CURL_TRC_EASY_TIMERS(data); |
1268 | 24.0k | } |
1269 | 451k | #endif |
1270 | | |
1271 | 451k | return CURLM_OK; |
1272 | 451k | } |
1273 | | |
1274 | | CURLMcode curl_multi_fdset(CURLM *m, |
1275 | | fd_set *read_fd_set, fd_set *write_fd_set, |
1276 | | fd_set *exc_fd_set, int *max_fd) |
1277 | 0 | { |
1278 | 0 | struct Curl_mapi_guard guard; |
1279 | 0 | CURLMcode mresult; |
1280 | |
|
1281 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_fdset, &mresult)) { |
1282 | | /* Scan through all the easy handles to get the file descriptors set. |
1283 | | Some easy handles may not have connected to the remote host yet, |
1284 | | and then we must make sure that is done. */ |
1285 | 0 | struct Curl_multi *multi = m; |
1286 | 0 | struct easy_pollset ps; |
1287 | 0 | int this_max_fd = -1; |
1288 | 0 | unsigned int i; |
1289 | 0 | uint32_t mid; |
1290 | 0 | (void)exc_fd_set; |
1291 | |
|
1292 | 0 | Curl_pollset_init(&ps); |
1293 | 0 | if(Curl_uint32_bset_first(&multi->process, &mid)) { |
1294 | 0 | do { |
1295 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
1296 | |
|
1297 | 0 | if(!data) { |
1298 | 0 | DEBUGASSERT(0); |
1299 | 0 | continue; |
1300 | 0 | } |
1301 | | |
1302 | 0 | Curl_multi_pollset(data, &ps); |
1303 | 0 | for(i = 0; i < ps.n; i++) { |
1304 | 0 | if(!FDSET_SOCK(ps.sockets[i])) |
1305 | | /* pretend it does not exist */ |
1306 | 0 | continue; |
1307 | 0 | if(ps.actions[i] & CURL_POLL_IN) |
1308 | 0 | FD_SET(ps.sockets[i], read_fd_set); |
1309 | 0 | if(ps.actions[i] & CURL_POLL_OUT) |
1310 | 0 | FD_SET(ps.sockets[i], write_fd_set); |
1311 | 0 | if((int)ps.sockets[i] > this_max_fd) |
1312 | 0 | this_max_fd = (int)ps.sockets[i]; |
1313 | 0 | } |
1314 | 0 | } while(Curl_uint32_bset_next(&multi->process, mid, &mid)); |
1315 | 0 | } |
1316 | | |
1317 | 0 | Curl_cshutdn_setfds(&multi->cshutdn, multi->admin, |
1318 | 0 | read_fd_set, write_fd_set, &this_max_fd); |
1319 | |
|
1320 | 0 | *max_fd = this_max_fd; |
1321 | 0 | Curl_pollset_cleanup(&ps); |
1322 | |
|
1323 | 0 | mresult = CURLM_OK; |
1324 | 0 | } |
1325 | 0 | CURL_MAPI_LEAVE(&guard); |
1326 | 0 | return mresult; |
1327 | 0 | } |
1328 | | |
1329 | | CURLMcode curl_multi_waitfds(CURLM *m, |
1330 | | struct curl_waitfd *ufds, |
1331 | | unsigned int size, |
1332 | | unsigned int *fd_count) |
1333 | 0 | { |
1334 | 0 | struct Curl_mapi_guard guard; |
1335 | 0 | CURLMcode mresult; |
1336 | |
|
1337 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_waitfds, &mresult)) { |
1338 | 0 | struct Curl_waitfds cwfds; |
1339 | 0 | struct Curl_multi *multi = m; |
1340 | 0 | struct easy_pollset ps; |
1341 | 0 | unsigned int need = 0; |
1342 | 0 | uint32_t mid; |
1343 | |
|
1344 | 0 | if(!ufds && (size || !fd_count)) { |
1345 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
1346 | 0 | goto out; |
1347 | 0 | } |
1348 | | |
1349 | 0 | Curl_pollset_init(&ps); |
1350 | 0 | Curl_waitfds_init(&cwfds, ufds, size); |
1351 | 0 | mresult = CURLM_OK; |
1352 | 0 | if(Curl_uint32_bset_first(&multi->process, &mid)) { |
1353 | 0 | do { |
1354 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
1355 | 0 | if(!data) { |
1356 | 0 | DEBUGASSERT(0); |
1357 | 0 | Curl_uint32_bset_remove(&multi->process, mid); |
1358 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
1359 | 0 | continue; |
1360 | 0 | } |
1361 | 0 | Curl_multi_pollset(data, &ps); |
1362 | 0 | need += Curl_waitfds_add_ps(&cwfds, &ps); |
1363 | 0 | } while(Curl_uint32_bset_next(&multi->process, mid, &mid)); |
1364 | 0 | } |
1365 | | |
1366 | 0 | need += Curl_cshutdn_add_waitfds(&multi->cshutdn, multi->admin, &cwfds); |
1367 | |
|
1368 | 0 | if(need != cwfds.n && ufds) |
1369 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
1370 | |
|
1371 | 0 | if(fd_count) |
1372 | 0 | *fd_count = need; |
1373 | 0 | Curl_pollset_cleanup(&ps); |
1374 | 0 | } |
1375 | 0 | out: |
1376 | 0 | CURL_MAPI_LEAVE(&guard); |
1377 | 0 | return mresult; |
1378 | 0 | } |
1379 | | |
1380 | | #ifdef USE_WINSOCK |
1381 | | /* Reset FD_WRITE for TCP sockets. Nothing is actually sent. UDP sockets cannot |
1382 | | * be reset this way because an empty datagram would be sent. #9203 |
1383 | | * |
1384 | | * "On Windows the internal state of FD_WRITE as returned from |
1385 | | * WSAEnumNetworkEvents is only reset after successful send()." |
1386 | | */ |
1387 | | static void reset_socket_fdwrite(curl_socket_t s) |
1388 | | { |
1389 | | int t; |
1390 | | int l = (int)sizeof(t); |
1391 | | if(!getsockopt(s, SOL_SOCKET, SO_TYPE, (char *)&t, &l) && t == SOCK_STREAM) |
1392 | | swrite(s, NULL, 0); |
1393 | | } |
1394 | | |
1395 | | static CURLMcode multi_winsock_select(struct Curl_multi *multi, |
1396 | | struct curl_pollfds *cpfds, |
1397 | | unsigned int curl_nfds, |
1398 | | struct curl_waitfd extra_fds[], |
1399 | | unsigned int extra_nfds, |
1400 | | int timeout_ms, |
1401 | | bool extrawait, |
1402 | | int *pnevents) |
1403 | | { |
1404 | | CURLMcode mresult = CURLM_OK; |
1405 | | WSANETWORKEVENTS wsa_events; |
1406 | | int nevents = 0; |
1407 | | size_t i; |
1408 | | |
1409 | | DEBUGASSERT(multi->wsa_event != WSA_INVALID_EVENT); |
1410 | | |
1411 | | /* Set the WSA events based on the collected pollds */ |
1412 | | for(i = 0; i < cpfds->n; i++) { |
1413 | | long mask = 0; |
1414 | | if(cpfds->pfds[i].events & POLLIN) |
1415 | | mask |= FD_READ | FD_ACCEPT | FD_CLOSE; |
1416 | | if(cpfds->pfds[i].events & POLLPRI) |
1417 | | mask |= FD_OOB; |
1418 | | if(cpfds->pfds[i].events & POLLOUT) { |
1419 | | mask |= FD_WRITE | FD_CONNECT | FD_CLOSE; |
1420 | | reset_socket_fdwrite(cpfds->pfds[i].fd); |
1421 | | } |
1422 | | if(mask && WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, mask)) { |
1423 | | mresult = CURLM_OUT_OF_MEMORY; |
1424 | | goto out; |
1425 | | } |
1426 | | } |
1427 | | |
1428 | | if(cpfds->n || extrawait) { |
1429 | | int pollrc = 0; |
1430 | | |
1431 | | if(cpfds->n) { /* pre-check with Winsock */ |
1432 | | pollrc = Curl_poll(cpfds->pfds, cpfds->n, 0); |
1433 | | if(pollrc < 0) { |
1434 | | mresult = CURLM_UNRECOVERABLE_POLL; |
1435 | | goto out; |
1436 | | } |
1437 | | nevents = pollrc; |
1438 | | } |
1439 | | |
1440 | | if(!nevents) { |
1441 | | /* now wait... if not ready during the pre-check (pollrc == 0) */ |
1442 | | WSAWaitForMultipleEvents(1, &multi->wsa_event, FALSE, (DWORD)timeout_ms, |
1443 | | FALSE); |
1444 | | } |
1445 | | |
1446 | | /* With Winsock, we have to run the following section unconditionally |
1447 | | to call WSAEventSelect(fd, event, 0) on all the sockets */ |
1448 | | /* copy revents results from the poll to the curl_multi_wait poll |
1449 | | struct, the bit values of the actual underlying poll() implementation |
1450 | | may not be the same as the ones in the public libcurl API! */ |
1451 | | for(i = 0; i < extra_nfds; i++) { |
1452 | | unsigned short mask = 0; |
1453 | | curl_socket_t s = extra_fds[i].fd; |
1454 | | |
1455 | | wsa_events.lNetworkEvents = 0; |
1456 | | if(WSAEnumNetworkEvents(s, NULL, &wsa_events) == 0) { |
1457 | | if(wsa_events.lNetworkEvents & (FD_READ | FD_ACCEPT | FD_CLOSE)) |
1458 | | mask |= CURL_WAIT_POLLIN; |
1459 | | if(wsa_events.lNetworkEvents & (FD_WRITE | FD_CONNECT | FD_CLOSE)) |
1460 | | mask |= CURL_WAIT_POLLOUT; |
1461 | | if(wsa_events.lNetworkEvents & FD_OOB) |
1462 | | mask |= CURL_WAIT_POLLPRI; |
1463 | | if(!pollrc && wsa_events.lNetworkEvents) |
1464 | | nevents++; |
1465 | | } |
1466 | | WSAEventSelect(s, multi->wsa_event, 0); |
1467 | | if(!pollrc) { |
1468 | | extra_fds[i].revents = (short)mask; |
1469 | | continue; |
1470 | | } |
1471 | | else { |
1472 | | unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents; |
1473 | | if(r & POLLIN) |
1474 | | mask |= CURL_WAIT_POLLIN; |
1475 | | if(r & POLLOUT) |
1476 | | mask |= CURL_WAIT_POLLOUT; |
1477 | | if(r & POLLPRI) |
1478 | | mask |= CURL_WAIT_POLLPRI; |
1479 | | extra_fds[i].revents = (short)mask; |
1480 | | } |
1481 | | } |
1482 | | |
1483 | | /* Count up all our own sockets that had activity, |
1484 | | and remove them from the event. */ |
1485 | | for(i = 0; i < curl_nfds; ++i) { |
1486 | | wsa_events.lNetworkEvents = 0; |
1487 | | if(WSAEnumNetworkEvents(cpfds->pfds[i].fd, NULL, &wsa_events) == 0) { |
1488 | | if(!pollrc && wsa_events.lNetworkEvents) |
1489 | | nevents++; |
1490 | | } |
1491 | | WSAEventSelect(cpfds->pfds[i].fd, multi->wsa_event, 0); |
1492 | | } |
1493 | | WSAResetEvent(multi->wsa_event); |
1494 | | } |
1495 | | |
1496 | | out: |
1497 | | *pnevents = nevents; |
1498 | | return mresult; |
1499 | | } |
1500 | | |
1501 | | #else /* USE_WINSOCK */ |
1502 | | |
1503 | | static CURLMcode multi_posix_poll(struct Curl_multi *multi, |
1504 | | struct curl_pollfds *cpfds, |
1505 | | unsigned int curl_nfds, |
1506 | | struct curl_waitfd extra_fds[], |
1507 | | unsigned int extra_nfds, |
1508 | | int timeout_ms, |
1509 | | bool extrawait, |
1510 | | int *pnevents) |
1511 | 132k | { |
1512 | 132k | CURLMcode mresult = CURLM_OK; |
1513 | 132k | int nevents = 0; |
1514 | 132k | size_t i; |
1515 | | |
1516 | 132k | (void)multi; |
1517 | 132k | if(cpfds->n) { |
1518 | 132k | int pollrc = Curl_poll(cpfds->pfds, cpfds->n, timeout_ms); /* wait... */ |
1519 | 132k | if(pollrc < 0) { |
1520 | 0 | mresult = CURLM_UNRECOVERABLE_POLL; |
1521 | 0 | goto out; |
1522 | 0 | } |
1523 | 132k | nevents = pollrc; |
1524 | | |
1525 | | /* copy revents results from the poll to the curl_multi_wait poll |
1526 | | struct, the bit values of the actual underlying poll() implementation |
1527 | | may not be the same as the ones in the public libcurl API! */ |
1528 | 132k | for(i = 0; i < extra_nfds; i++) { |
1529 | 0 | unsigned r = (unsigned)cpfds->pfds[curl_nfds + i].revents; |
1530 | 0 | unsigned short mask = 0; |
1531 | 0 | if(r & POLLIN) |
1532 | 0 | mask |= CURL_WAIT_POLLIN; |
1533 | 0 | if(r & POLLOUT) |
1534 | 0 | mask |= CURL_WAIT_POLLOUT; |
1535 | 0 | if(r & POLLPRI) |
1536 | 0 | mask |= CURL_WAIT_POLLPRI; |
1537 | 0 | extra_fds[i].revents = (short)mask; |
1538 | 0 | } |
1539 | 132k | } |
1540 | 0 | else if(extrawait) { |
1541 | | /* No fds to poll, but asked to obey timeout_ms anyway. We cannot |
1542 | | * use Curl_poll() as it, on some platforms, returns immediately |
1543 | | * without fds. */ |
1544 | 0 | curlx_wait_ms(timeout_ms); |
1545 | 0 | } |
1546 | | |
1547 | 132k | out: |
1548 | 132k | *pnevents = nevents; |
1549 | 132k | return mresult; |
1550 | 132k | } |
1551 | | |
1552 | | #endif /* !USE_WINSOCK */ |
1553 | | |
1554 | 132k | #define NUM_POLLS_ON_STACK 10 |
1555 | | |
1556 | | static CURLMcode multi_wait(struct Curl_multi *multi, |
1557 | | struct curl_waitfd extra_fds[], |
1558 | | unsigned int extra_nfds, |
1559 | | int timeout_ms, |
1560 | | int *ret, |
1561 | | bool extrawait) /* when no socket, wait */ |
1562 | 132k | { |
1563 | 132k | size_t i; |
1564 | 132k | int timeout_internal; |
1565 | 132k | int nevents = 0; |
1566 | 132k | struct easy_pollset ps; |
1567 | 132k | struct pollfd a_few_on_stack[NUM_POLLS_ON_STACK]; |
1568 | 132k | struct curl_pollfds cpfds; |
1569 | 132k | unsigned int curl_nfds = 0; /* how many pfds are for curl transfers */ |
1570 | 132k | struct Curl_easy *data = NULL; |
1571 | 132k | CURLMcode mresult = CURLM_OK; |
1572 | 132k | uint32_t mid; |
1573 | 132k | #ifdef ENABLE_WAKEUP |
1574 | 132k | int wakeup_idx = -1; |
1575 | 132k | #endif |
1576 | | |
1577 | 132k | if(timeout_ms < 0) |
1578 | 0 | return CURLM_BAD_FUNCTION_ARGUMENT; |
1579 | | |
1580 | 132k | Curl_pollset_init(&ps); |
1581 | 132k | Curl_pollfds_init(&cpfds, a_few_on_stack, NUM_POLLS_ON_STACK); |
1582 | | |
1583 | | /* Add the curl handles to our pollfds first */ |
1584 | 132k | if(Curl_uint32_bset_first(&multi->process, &mid)) { |
1585 | 265k | do { |
1586 | 265k | data = Curl_multi_get_easy(multi, mid); |
1587 | 265k | if(!data) { |
1588 | 0 | DEBUGASSERT(0); |
1589 | 0 | Curl_uint32_bset_remove(&multi->process, mid); |
1590 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
1591 | 0 | continue; |
1592 | 0 | } |
1593 | 265k | Curl_multi_pollset(data, &ps); |
1594 | 265k | if(Curl_pollfds_add_ps(&cpfds, &ps)) { |
1595 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
1596 | 0 | goto out; |
1597 | 0 | } |
1598 | 265k | } while(Curl_uint32_bset_next(&multi->process, mid, &mid)); |
1599 | 132k | } |
1600 | | |
1601 | 132k | if(Curl_cshutdn_add_pollfds(&multi->cshutdn, multi->admin, &cpfds)) { |
1602 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
1603 | 0 | goto out; |
1604 | 0 | } |
1605 | | |
1606 | 132k | #ifdef ENABLE_WAKEUP |
1607 | | /* If `extrawait` is TRUE *or* we have `extra_fds`to poll *or* we |
1608 | | * have transfer sockets to poll, we obey `timeout_ms`. |
1609 | | * Then we need to also monitor the multi's wakeup |
1610 | | * socket to catch calls to `curl_multi_wakeup()` during the wait. */ |
1611 | 132k | if(extrawait || cpfds.n || extra_nfds) { |
1612 | 132k | wakeup_idx = cpfds.n; |
1613 | 132k | if(Curl_pollfds_add_sock(&cpfds, multi->wakeup_pair[0], POLLIN)) { |
1614 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
1615 | 0 | goto out; |
1616 | 0 | } |
1617 | 132k | } |
1618 | 132k | #endif |
1619 | | |
1620 | 132k | curl_nfds = cpfds.n; /* what curl internally uses in cpfds */ |
1621 | | /* Add external file descriptions from poll-like struct curl_waitfd */ |
1622 | 132k | for(i = 0; i < extra_nfds; i++) { |
1623 | 0 | unsigned short events = 0; |
1624 | 0 | if(extra_fds[i].events & CURL_WAIT_POLLIN) |
1625 | 0 | events |= POLLIN; |
1626 | 0 | if(extra_fds[i].events & CURL_WAIT_POLLPRI) |
1627 | 0 | events |= POLLPRI; |
1628 | 0 | if(extra_fds[i].events & CURL_WAIT_POLLOUT) |
1629 | 0 | events |= POLLOUT; |
1630 | 0 | if(Curl_pollfds_add_sock(&cpfds, extra_fds[i].fd, events)) { |
1631 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
1632 | 0 | goto out; |
1633 | 0 | } |
1634 | 0 | } |
1635 | | |
1636 | | /* We check the internal timeout *AFTER* we collected all sockets to |
1637 | | * poll. Collecting the sockets may install new timers by protocols |
1638 | | * and connection filters. |
1639 | | * Use the shorter one of the internal and the caller requested timeout. |
1640 | | * If we are called with `!extrawait` and multi_timeout() reports no |
1641 | | * timeouts exist, do not wait. */ |
1642 | 132k | multi_timeout(multi, NULL, &timeout_internal); |
1643 | 132k | if((timeout_internal >= 0) && (timeout_internal < timeout_ms)) |
1644 | 132k | timeout_ms = timeout_internal; |
1645 | | |
1646 | 132k | if(data) |
1647 | 132k | CURL_TRC_M(data, "multi_wait(fds=%u, timeout=%d) tinternal=%d", |
1648 | 132k | cpfds.n, timeout_ms, timeout_internal); |
1649 | | |
1650 | | #ifdef USE_WINSOCK |
1651 | | mresult = multi_winsock_select(multi, &cpfds, curl_nfds, |
1652 | | extra_fds, extra_nfds, |
1653 | | timeout_ms, extrawait, &nevents); |
1654 | | #else |
1655 | 132k | mresult = multi_posix_poll(multi, &cpfds, curl_nfds, |
1656 | 132k | extra_fds, extra_nfds, |
1657 | 132k | timeout_ms, extrawait, &nevents); |
1658 | 132k | #endif |
1659 | | |
1660 | 132k | #ifdef ENABLE_WAKEUP |
1661 | 132k | if(nevents && (wakeup_idx >= 0)) { |
1662 | 131k | if(cpfds.pfds[wakeup_idx].revents & POLLIN) { |
1663 | 0 | (void)Curl_wakeup_consume(multi->wakeup_pair, TRUE); |
1664 | | /* do not count the wakeup socket into the returned value */ |
1665 | 0 | nevents--; |
1666 | 0 | } |
1667 | 131k | } |
1668 | 132k | #endif |
1669 | | |
1670 | 132k | out: |
1671 | 132k | Curl_pollset_cleanup(&ps); |
1672 | 132k | Curl_pollfds_cleanup(&cpfds); |
1673 | 132k | if(ret) |
1674 | 0 | *ret = nevents; |
1675 | 132k | return mresult; |
1676 | 132k | } |
1677 | | |
1678 | | CURLMcode curl_multi_wait(CURLM *m, |
1679 | | struct curl_waitfd extra_fds[], |
1680 | | unsigned int extra_nfds, |
1681 | | int timeout_ms, |
1682 | | int *ret) |
1683 | 21 | { |
1684 | 21 | struct Curl_mapi_guard guard; |
1685 | 21 | CURLMcode mresult; |
1686 | | |
1687 | 21 | if(CURL_MAPI_ENTER(&guard, m, multi_wait, &mresult)) { |
1688 | 0 | mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, FALSE); |
1689 | 0 | } |
1690 | 21 | CURL_MAPI_LEAVE(&guard); |
1691 | 21 | return mresult; |
1692 | 21 | } |
1693 | | |
1694 | | CURLMcode curl_multi_poll(CURLM *m, |
1695 | | struct curl_waitfd extra_fds[], |
1696 | | unsigned int extra_nfds, |
1697 | | int timeout_ms, |
1698 | | int *ret) |
1699 | 132k | { |
1700 | 132k | struct Curl_mapi_guard guard; |
1701 | 132k | CURLMcode mresult; |
1702 | | |
1703 | 132k | if(CURL_MAPI_ENTER(&guard, m, multi_poll, &mresult)) { |
1704 | 132k | mresult = multi_wait(m, extra_fds, extra_nfds, timeout_ms, ret, TRUE); |
1705 | 132k | } |
1706 | 132k | CURL_MAPI_LEAVE(&guard); |
1707 | 132k | return mresult; |
1708 | 132k | } |
1709 | | |
1710 | | CURLMcode curl_multi_wakeup(CURLM *m) |
1711 | 122 | { |
1712 | | /* this function is usually called from another thread, |
1713 | | it has to be careful only to access parts of the |
1714 | | Curl_multi struct that are constant */ |
1715 | 122 | struct Curl_multi *multi = m; |
1716 | 122 | CURLMcode mresult = CURLM_WAKEUP_FAILURE; |
1717 | | |
1718 | | /* GOOD_MULTI_HANDLE can be safely called */ |
1719 | 122 | if(!GOOD_MULTI_HANDLE(multi)) |
1720 | 0 | return CURLM_BAD_HANDLE; |
1721 | | |
1722 | 122 | #ifdef ENABLE_WAKEUP |
1723 | | /* the wakeup_pair variable is only written during init and cleanup, |
1724 | | making it safe to access from another thread after the init part |
1725 | | and before cleanup */ |
1726 | 122 | if(!Curl_wakeup_signal(multi->wakeup_pair)) |
1727 | 122 | mresult = CURLM_OK; |
1728 | 122 | #endif |
1729 | | #ifdef USE_WINSOCK |
1730 | | if(WSASetEvent(multi->wsa_event)) |
1731 | | mresult = CURLM_OK; |
1732 | | #endif |
1733 | 122 | return mresult; |
1734 | 122 | } |
1735 | | |
1736 | | #ifdef ENABLE_INTERNAL_WAKEUP |
1737 | | void Curl_multi_wakeup_internal(struct Curl_multi *multi) |
1738 | 101 | { |
1739 | | /* This is expected to be invocable from another thread which |
1740 | | * does NOT outlive the multi handle. Check for sanity. */ |
1741 | 101 | if(GOOD_MULTI_HANDLE(multi)) |
1742 | 101 | Curl_wakeup_signal(multi->wakeup_internal); |
1743 | 0 | else |
1744 | 101 | DEBUGASSERT(0); |
1745 | 101 | } |
1746 | | #endif |
1747 | | |
1748 | | /* |
1749 | | * multi_ischanged() is called |
1750 | | * |
1751 | | * Returns TRUE/FALSE whether the state is changed to trigger a CONNECT_PEND |
1752 | | * => CONNECT action. |
1753 | | * |
1754 | | * Set 'clear' to TRUE to have it also clear the state variable. |
1755 | | */ |
1756 | | static bool multi_ischanged(struct Curl_multi *multi, bool clear) |
1757 | 1.42M | { |
1758 | 1.42M | bool retval = FALSE; |
1759 | 1.42M | DEBUGASSERT(multi); |
1760 | 1.42M | if(multi) { |
1761 | 1.42M | retval = (bool)multi->recheckstate; |
1762 | 1.42M | if(clear) |
1763 | 1.03M | multi->recheckstate = FALSE; |
1764 | 1.42M | } |
1765 | 1.42M | return retval; |
1766 | 1.42M | } |
1767 | | |
1768 | | /* |
1769 | | * Curl_multi_connchanged() is called to tell that there is a connection in |
1770 | | * this multi handle that has changed state (multiplexing become possible, the |
1771 | | * number of allowed streams changed or similar), and a subsequent use of this |
1772 | | * multi handle should move CONNECT_PEND handles back to CONNECT to have them |
1773 | | * retry. |
1774 | | */ |
1775 | | void Curl_multi_connchanged(struct Curl_multi *multi) |
1776 | 70.5k | { |
1777 | | /* May be invoked for a `data->multi` when no multi is present. */ |
1778 | 70.5k | if(multi) |
1779 | 70.4k | multi->recheckstate = TRUE; |
1780 | 70.5k | } |
1781 | | |
1782 | | CURLMcode Curl_multi_add_perform(struct Curl_multi *multi, |
1783 | | struct Curl_easy *data, |
1784 | | struct connectdata *conn) |
1785 | 0 | { |
1786 | 0 | CURLMcode mresult; |
1787 | |
|
1788 | 0 | mresult = Curl_multi_add_handle(multi, data); |
1789 | 0 | if(!mresult) { |
1790 | 0 | CURLcode result; |
1791 | | |
1792 | | /* pass in NULL for 'conn' here since we do not want to init the |
1793 | | connection, only this transfer */ |
1794 | 0 | result = Curl_init_transfer(data, NULL); |
1795 | 0 | if(result) { |
1796 | 0 | Curl_multi_remove_handle(multi, data); |
1797 | 0 | return CURLM_INTERNAL_ERROR; |
1798 | 0 | } |
1799 | | |
1800 | | /* take this handle to the perform state right away */ |
1801 | 0 | multistate(data, MSTATE_PERFORMING); |
1802 | 0 | Curl_attach_connection(data, conn, TRUE); |
1803 | 0 | CURL_REQ_SET_RECV(data); |
1804 | 0 | } |
1805 | 0 | return mresult; |
1806 | 0 | } |
1807 | | |
1808 | | static CURLcode multi_do(struct Curl_easy *data, bool *done) |
1809 | 35.5k | { |
1810 | 35.5k | CURLcode result = CURLE_OK; |
1811 | 35.5k | struct connectdata *conn = data->conn; |
1812 | | |
1813 | 35.5k | DEBUGASSERT(conn); |
1814 | 35.5k | DEBUGASSERT(conn->scheme); |
1815 | | |
1816 | 35.5k | if(conn->scheme->run->do_it) |
1817 | 35.5k | result = conn->scheme->run->do_it(data, done); |
1818 | | |
1819 | 35.5k | return result; |
1820 | 35.5k | } |
1821 | | |
1822 | | /* |
1823 | | * multi_do_more() is called during the DO_MORE multi state. It is a second |
1824 | | * stage DO state which (wrongly) was introduced to support FTP's second |
1825 | | * connection. |
1826 | | * |
1827 | | * 'complete' can return DOMORE_INCOMPLETE, DOMORE_DONE or DOMORE_GOBACK |
1828 | | * (to DOING state when there is more work to do) |
1829 | | */ |
1830 | | |
1831 | | static CURLcode multi_do_more(struct Curl_easy *data, domore *complete) |
1832 | 0 | { |
1833 | 0 | CURLcode result = CURLE_OK; |
1834 | 0 | struct connectdata *conn = data->conn; |
1835 | |
|
1836 | 0 | *complete = DOMORE_INCOMPLETE; |
1837 | |
|
1838 | 0 | if(conn->scheme->run->do_more) |
1839 | 0 | result = conn->scheme->run->do_more(data, complete); |
1840 | |
|
1841 | 0 | return result; |
1842 | 0 | } |
1843 | | |
1844 | | /* |
1845 | | * Check whether a timeout occurred, and handle it if it did |
1846 | | */ |
1847 | | static bool multi_handle_timeout(struct Curl_easy *data, |
1848 | | const struct curltime *pnow, |
1849 | | bool *stream_error, |
1850 | | CURLcode *result, |
1851 | | bool connect_timeout) |
1852 | 617k | { |
1853 | 617k | timediff_t timeout_ms; |
1854 | | |
1855 | 617k | timeout_ms = connect_timeout ? Curl_timeleft_now_ms(data, pnow) : |
1856 | 617k | Curl_timeleft_total_now_ms(data, pnow); |
1857 | 617k | if(timeout_ms < 0) { |
1858 | | /* Handle timed out */ |
1859 | 48 | timerid base_timer = Curl_is_connecting(data) ? |
1860 | 40 | TIMER_STARTSINGLE : TIMER_STARTOP; |
1861 | 48 | timediff_t elapsed_ms = Curl_pgrs_since_ms(data, NULL, base_timer); |
1862 | 48 | if(data->mstate == MSTATE_CONNECTING) { |
1863 | 8 | failf(data, "%s timed out after %" FMT_TIMEDIFF_T " milliseconds", |
1864 | 8 | data->conn->bits.dns_resolved ? "Connection" : "Resolving", |
1865 | 8 | elapsed_ms); |
1866 | | /* keep what we know about a partly done connect, e.g. TCP done |
1867 | | * but TLS handshake still ongoing */ |
1868 | 8 | Curl_cfchain_report_failed(data, &data->conn->chain); |
1869 | 8 | } |
1870 | 40 | else { |
1871 | 40 | struct SingleRequest *k = &data->req; |
1872 | 40 | if(k->size != -1) { |
1873 | 40 | failf(data, "Operation timed out after %" FMT_TIMEDIFF_T |
1874 | 40 | " milliseconds with %" FMT_OFF_T " out of %" |
1875 | 40 | FMT_OFF_T " bytes received", |
1876 | 40 | elapsed_ms, k->bytecount, k->size); |
1877 | 40 | } |
1878 | 0 | else { |
1879 | 0 | failf(data, "Operation timed out after %" FMT_TIMEDIFF_T |
1880 | 0 | " milliseconds with %" FMT_OFF_T " bytes received", |
1881 | 0 | elapsed_ms, k->bytecount); |
1882 | 0 | } |
1883 | 40 | } |
1884 | 48 | *result = CURLE_OPERATION_TIMEDOUT; |
1885 | 48 | if(data->conn) { |
1886 | | /* Force connection closed if the connection has indeed been used */ |
1887 | 48 | if(data->mstate > MSTATE_DO) { |
1888 | 40 | streamclose(data->conn); |
1889 | 40 | *stream_error = TRUE; |
1890 | 40 | } |
1891 | 48 | (void)multi_done(data, *result, TRUE); |
1892 | 48 | } |
1893 | 48 | return TRUE; |
1894 | 48 | } |
1895 | | |
1896 | 617k | return FALSE; |
1897 | 617k | } |
1898 | | |
1899 | | /* |
1900 | | * We are doing protocol-specific connecting and this is being called over and |
1901 | | * over from the multi interface until the connection phase is done on |
1902 | | * protocol layer. |
1903 | | */ |
1904 | | |
1905 | | static CURLcode protocol_connecting(struct Curl_easy *data, bool *done) |
1906 | 0 | { |
1907 | 0 | CURLcode result = CURLE_OK; |
1908 | 0 | struct connectdata *conn = data->conn; |
1909 | |
|
1910 | 0 | if(conn && conn->scheme->run->connecting) { |
1911 | 0 | *done = FALSE; |
1912 | 0 | result = conn->scheme->run->connecting(data, done); |
1913 | 0 | } |
1914 | 0 | else |
1915 | 0 | *done = TRUE; |
1916 | |
|
1917 | 0 | return result; |
1918 | 0 | } |
1919 | | |
1920 | | /* |
1921 | | * We are DOING this is being called over and over from the multi interface |
1922 | | * until the DOING phase is done on protocol layer. |
1923 | | */ |
1924 | | |
1925 | | static CURLcode protocol_doing(struct Curl_easy *data, bool *done) |
1926 | 0 | { |
1927 | 0 | CURLcode result = CURLE_OK; |
1928 | 0 | struct connectdata *conn = data->conn; |
1929 | |
|
1930 | 0 | if(conn && conn->scheme->run->doing) { |
1931 | 0 | *done = FALSE; |
1932 | 0 | result = conn->scheme->run->doing(data, done); |
1933 | 0 | } |
1934 | 0 | else |
1935 | 0 | *done = TRUE; |
1936 | |
|
1937 | 0 | return result; |
1938 | 0 | } |
1939 | | |
1940 | | /* |
1941 | | * We have discovered that the TCP connection has been successful, we can now |
1942 | | * proceed with some action. |
1943 | | * |
1944 | | */ |
1945 | | static CURLcode protocol_connect(struct Curl_easy *data, bool *protocol_done) |
1946 | 34.2k | { |
1947 | 34.2k | struct connectdata *conn = data->conn; |
1948 | 34.2k | CURLcode result = CURLE_OK; |
1949 | | |
1950 | 34.2k | DEBUGASSERT(conn); |
1951 | 34.2k | DEBUGASSERT(protocol_done); |
1952 | 34.2k | DEBUGASSERT(Curl_conn_is_connected(conn)); |
1953 | | |
1954 | 34.2k | *protocol_done = FALSE; |
1955 | 34.2k | if(!conn->bits.protoconnstart) { |
1956 | 34.2k | if(conn->scheme->run->connect_it) { |
1957 | | /* Call the protocol-specific connect function */ |
1958 | 0 | result = conn->scheme->run->connect_it(data, protocol_done); |
1959 | 0 | if(result) |
1960 | 0 | return result; |
1961 | 0 | } |
1962 | 34.2k | conn->bits.protoconnstart = TRUE; |
1963 | 34.2k | } |
1964 | | |
1965 | | /* Unless this protocol does not have any protocol-connect callback, as |
1966 | | then we know we are done. */ |
1967 | 34.2k | if(!conn->scheme->run->connecting) |
1968 | 34.2k | *protocol_done = TRUE; |
1969 | 34.2k | return CURLE_OK; |
1970 | 34.2k | } |
1971 | | |
1972 | | /* |
1973 | | * posttransfer() is called immediately after a transfer ends |
1974 | | */ |
1975 | | static void multi_posttransfer(struct Curl_easy *data) |
1976 | 39.0k | { |
1977 | | #if defined(HAVE_SIGNAL) && defined(SIGPIPE) && !defined(MSG_NOSIGNAL) |
1978 | | /* restore the signal handler for SIGPIPE before we get back */ |
1979 | | if(!data->set.no_signal) |
1980 | | signal(SIGPIPE, data->state.prev_signal); |
1981 | | #else |
1982 | 39.0k | (void)data; |
1983 | 39.0k | #endif |
1984 | 39.0k | } |
1985 | | |
1986 | | /* |
1987 | | * multi_follow() handles the URL redirect magic. Pass in the 'newurl' string |
1988 | | * as given by the remote server and set up the new URL to request. |
1989 | | * |
1990 | | * This function DOES NOT FREE the given URL. |
1991 | | */ |
1992 | | static CURLcode multi_follow(struct Curl_easy *data, |
1993 | | const struct Curl_scheme *handler, |
1994 | | const char *newurl, /* the Location: string */ |
1995 | | followtype type) /* see transfer.h */ |
1996 | 12.8k | { |
1997 | 12.8k | if(handler && handler->run->follow) |
1998 | 12.8k | return handler->run->follow(data, newurl, type); |
1999 | | |
2000 | 0 | if(type == FOLLOW_RETRY) |
2001 | | /* Retries are generic and do not require protocol-specific redirect |
2002 | | handling. */ |
2003 | 0 | return CURLE_OK; |
2004 | | |
2005 | 0 | return CURLE_TOO_MANY_REDIRECTS; |
2006 | 0 | } |
2007 | | |
2008 | | static CURLcode mspeed_check(struct Curl_easy *data) |
2009 | 727k | { |
2010 | 727k | if(Curl_rlimit_active(&data->progress.dl.rlimit) || |
2011 | 727k | Curl_rlimit_active(&data->progress.ul.rlimit)) { |
2012 | | /* check if our send/recv limits require idle waits */ |
2013 | 305 | const struct curltime *pnow = Curl_pgrs_now(data); |
2014 | 305 | timediff_t recv_ms, send_ms; |
2015 | | |
2016 | 305 | send_ms = Curl_rlimit_wait_ms(&data->progress.ul.rlimit, pnow); |
2017 | 305 | recv_ms = Curl_rlimit_wait_ms(&data->progress.dl.rlimit, pnow); |
2018 | | |
2019 | 305 | if(send_ms || recv_ms) { |
2020 | 0 | if(data->mstate != MSTATE_RATELIMITING) { |
2021 | 0 | multistate(data, MSTATE_RATELIMITING); |
2022 | 0 | } |
2023 | 0 | Curl_expire_set(data, EXPIRE_TOOFAST, CURLMAX(send_ms, recv_ms), pnow); |
2024 | 0 | Curl_multi_clear_dirty(data); |
2025 | 0 | CURL_TRC_M(data, "[RLIMIT] waiting %" FMT_TIMEDIFF_T "ms", |
2026 | 0 | CURLMAX(send_ms, recv_ms)); |
2027 | 0 | return CURLE_AGAIN; |
2028 | 0 | } |
2029 | 305 | else { |
2030 | | /* when will the rate limits increase next? The transfer needs |
2031 | | * to run again at that time or it may stall. */ |
2032 | 305 | send_ms = Curl_rlimit_next_step_ms(&data->progress.ul.rlimit, pnow); |
2033 | 305 | recv_ms = Curl_rlimit_next_step_ms(&data->progress.dl.rlimit, pnow); |
2034 | 305 | if(send_ms || recv_ms) { |
2035 | 0 | timediff_t next_ms = CURLMIN(send_ms, recv_ms); |
2036 | 0 | if(!next_ms) |
2037 | 0 | next_ms = CURLMAX(send_ms, recv_ms); |
2038 | 0 | Curl_expire_set(data, EXPIRE_TOOFAST, next_ms, pnow); |
2039 | 0 | CURL_TRC_M(data, "[RLIMIT] next token update in %" FMT_TIMEDIFF_T "ms", |
2040 | 0 | next_ms); |
2041 | 0 | } |
2042 | 305 | } |
2043 | 305 | } |
2044 | | |
2045 | 727k | if(data->mstate != MSTATE_PERFORMING) { |
2046 | 0 | CURL_TRC_M(data, "[RLIMIT] wait over, continue"); |
2047 | 0 | multistate(data, MSTATE_PERFORMING); |
2048 | 0 | } |
2049 | 727k | return CURLE_OK; |
2050 | 727k | } |
2051 | | |
2052 | | static CURLMcode multistate_performing(struct Curl_easy *data, |
2053 | | bool *stream_errorp, |
2054 | | CURLcode *resultp) |
2055 | 381k | { |
2056 | 381k | char *newurl = NULL; |
2057 | 381k | bool retry = FALSE; |
2058 | 381k | CURLMcode mresult = CURLM_OK; |
2059 | 381k | CURLcode result = *resultp = CURLE_OK; |
2060 | 381k | *stream_errorp = FALSE; |
2061 | | |
2062 | 381k | if(mspeed_check(data) == CURLE_AGAIN) |
2063 | 0 | return CURLM_OK; |
2064 | | |
2065 | | /* read/write data if it is ready to do so */ |
2066 | 381k | result = Curl_sendrecv(data); |
2067 | | |
2068 | 381k | if(data->req.done || (result == CURLE_RECV_ERROR)) { |
2069 | | /* If CURLE_RECV_ERROR happens early enough, we assume it was a race |
2070 | | * condition and the server closed the reused connection exactly when we |
2071 | | * wanted to use it, so figure out if that is indeed the case. |
2072 | | */ |
2073 | 25.6k | CURLcode ret = Curl_retry_request(data, &newurl); |
2074 | 25.6k | if(!ret) |
2075 | 25.6k | retry = !!newurl; |
2076 | 0 | else if(!result) |
2077 | 0 | result = ret; |
2078 | | |
2079 | 25.6k | if(retry) { |
2080 | | /* if we are to retry, set the result to OK and consider the |
2081 | | request as done */ |
2082 | 9.29k | result = CURLE_OK; |
2083 | 9.29k | data->req.done = TRUE; |
2084 | 9.29k | } |
2085 | 25.6k | } |
2086 | 355k | #ifndef CURL_DISABLE_HTTP |
2087 | 355k | else if(!data->set.never_retry && |
2088 | 355k | (result == CURLE_HTTP2_STREAM) && |
2089 | 607 | Curl_h2_http_1_1_error(data)) { |
2090 | 244 | CURLcode ret = Curl_retry_request(data, &newurl); |
2091 | | |
2092 | 244 | if(!ret) { |
2093 | 244 | infof(data, "Downgrades to HTTP/1.1"); |
2094 | 244 | streamclose(data->conn); |
2095 | 244 | data->state.http_neg.wanted = CURL_HTTP_V1x; |
2096 | 244 | data->state.http_neg.allowed = CURL_HTTP_V1x; |
2097 | | /* clear the error message bit too as we ignore the one we got */ |
2098 | 244 | data->state.errorbuf = FALSE; |
2099 | 244 | if(!newurl) |
2100 | | /* typically for HTTP_1_1_REQUIRED error on first flight */ |
2101 | 243 | newurl = Curl_bufref_dup(&data->state.url); |
2102 | 244 | if(!newurl) { |
2103 | 0 | result = CURLE_OUT_OF_MEMORY; |
2104 | 0 | } |
2105 | 244 | else { |
2106 | | /* if we are to retry, set the result to OK and consider the request |
2107 | | as done */ |
2108 | 244 | retry = TRUE; |
2109 | 244 | result = CURLE_OK; |
2110 | 244 | data->req.done = TRUE; |
2111 | 244 | } |
2112 | 244 | } |
2113 | 0 | else |
2114 | 0 | result = ret; |
2115 | 244 | } |
2116 | 381k | #endif |
2117 | | |
2118 | 381k | if(result) { |
2119 | | /* |
2120 | | * The transfer phase returned error, we mark the connection to get closed |
2121 | | * to prevent being reused. This is because we cannot possibly know if the |
2122 | | * connection is in a good shape or not now. Unless it is a protocol which |
2123 | | * uses two "channels" like FTP, as then the error happened in the data |
2124 | | * connection. |
2125 | | */ |
2126 | | |
2127 | 8.93k | if(!(data->conn->scheme->flags & PROTOPT_DUAL) && |
2128 | 8.93k | result != CURLE_HTTP2_STREAM) |
2129 | 8.57k | streamclose(data->conn); |
2130 | | |
2131 | 8.93k | multi_posttransfer(data); |
2132 | 8.93k | multi_done(data, result, TRUE); |
2133 | 8.93k | } |
2134 | 372k | else if(data->req.done && !Curl_cwriter_is_paused(data)) { |
2135 | 25.7k | const struct Curl_scheme *handler = data->conn->scheme; |
2136 | | |
2137 | | /* call this even if the readwrite function returned error */ |
2138 | 25.7k | multi_posttransfer(data); |
2139 | | |
2140 | | /* When we follow redirects or is set to retry the connection, we must to |
2141 | | go back to the CONNECT state */ |
2142 | 25.7k | if(data->req.newurl || retry) { |
2143 | 12.2k | followtype follow = FOLLOW_NONE; |
2144 | 12.2k | if(!retry) { |
2145 | | /* if the URL is a follow-location and not a retried request then |
2146 | | figure out the URL here */ |
2147 | 2.68k | curlx_free(newurl); |
2148 | 2.68k | newurl = data->req.newurl; |
2149 | 2.68k | data->req.newurl = NULL; |
2150 | 2.68k | follow = FOLLOW_REDIR; |
2151 | 2.68k | } |
2152 | 9.54k | else |
2153 | 9.54k | follow = FOLLOW_RETRY; |
2154 | 12.2k | (void)multi_done(data, CURLE_OK, FALSE); |
2155 | | /* multi_done() might return CURLE_GOT_NOTHING */ |
2156 | 12.2k | result = multi_follow(data, handler, newurl, follow); |
2157 | 12.2k | if(!result) { |
2158 | 12.1k | multistate(data, MSTATE_SETUP); |
2159 | 12.1k | mresult = CURLM_CALL_MULTI_PERFORM; |
2160 | 12.1k | } |
2161 | 12.2k | } |
2162 | 13.5k | else { |
2163 | | /* after the transfer is done, go DONE */ |
2164 | | |
2165 | | /* but first check to see if we got a location info even though we are |
2166 | | not following redirects */ |
2167 | 13.5k | if(data->req.location) { |
2168 | 515 | curlx_free(newurl); |
2169 | 515 | newurl = data->req.location; |
2170 | 515 | data->req.location = NULL; |
2171 | 515 | result = multi_follow(data, handler, newurl, FOLLOW_FAKE); |
2172 | 515 | if(result) { |
2173 | 2 | *stream_errorp = TRUE; |
2174 | 2 | result = multi_done(data, result, TRUE); |
2175 | 2 | } |
2176 | 515 | } |
2177 | | |
2178 | 13.5k | if(!result) { |
2179 | 13.5k | multistate(data, MSTATE_DONE); |
2180 | 13.5k | mresult = CURLM_CALL_MULTI_PERFORM; |
2181 | 13.5k | } |
2182 | 13.5k | } |
2183 | 25.7k | } |
2184 | 346k | else { /* not errored, not done */ |
2185 | 346k | mspeed_check(data); |
2186 | 346k | } |
2187 | 381k | curlx_free(newurl); |
2188 | 381k | *resultp = result; |
2189 | 381k | return mresult; |
2190 | 381k | } |
2191 | | |
2192 | | static CURLMcode multistate_do(struct Curl_easy *data, |
2193 | | bool *stream_errorp, |
2194 | | CURLcode *resultp) |
2195 | 35.7k | { |
2196 | 35.7k | CURLMcode mresult = CURLM_OK; |
2197 | 35.7k | CURLcode result = CURLE_OK; |
2198 | 35.7k | if(data->set.fprereq) { |
2199 | 0 | struct Curl_mapi_guard guard; |
2200 | 0 | int prereq_rc; |
2201 | | |
2202 | | /* call the prerequest callback function */ |
2203 | 0 | CURL_CBAPI_START(&guard, data, easy_fprereq); |
2204 | 0 | prereq_rc = data->set.fprereq(data->set.prereq_userp, |
2205 | 0 | data->info.primary.remote_ip, |
2206 | 0 | data->info.primary.local_ip, |
2207 | 0 | data->info.primary.remote_port, |
2208 | 0 | data->info.primary.local_port); |
2209 | 0 | CURL_CBAPI_END(&guard); |
2210 | 0 | if(prereq_rc != CURL_PREREQFUNC_OK) { |
2211 | 0 | failf(data, "operation aborted by pre-request callback"); |
2212 | | /* failure in pre-request callback - do not do any other processing */ |
2213 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
2214 | 0 | multi_posttransfer(data); |
2215 | 0 | multi_done(data, result, FALSE); |
2216 | 0 | *stream_errorp = TRUE; |
2217 | 0 | goto end; |
2218 | 0 | } |
2219 | 0 | } |
2220 | | |
2221 | 35.7k | if(data->set.connect_only && !data->set.connect_only_ws) { |
2222 | 288 | multistate(data, MSTATE_DONE); |
2223 | 288 | mresult = CURLM_CALL_MULTI_PERFORM; |
2224 | 288 | } |
2225 | 35.5k | else { |
2226 | 35.5k | bool dophase_done = FALSE; |
2227 | | /* Perform the protocol's DO action */ |
2228 | 35.5k | result = multi_do(data, &dophase_done); |
2229 | | |
2230 | 35.5k | if(!result) { |
2231 | 34.8k | if(!dophase_done) { |
2232 | 0 | #ifndef CURL_DISABLE_FTP |
2233 | | /* some steps needed for wildcard matching */ |
2234 | 0 | if(data->state.wildcardmatch) { |
2235 | 0 | struct WildcardData *wc = data->wildcard; |
2236 | 0 | if(wc->state == CURLWC_DONE || wc->state == CURLWC_SKIP) { |
2237 | | /* skip some states if it is important */ |
2238 | 0 | multi_done(data, CURLE_OK, FALSE); |
2239 | | |
2240 | | /* if there is no connection left, skip the DONE state */ |
2241 | 0 | multistate(data, data->conn ? MSTATE_DONE : MSTATE_COMPLETED); |
2242 | 0 | mresult = CURLM_CALL_MULTI_PERFORM; |
2243 | 0 | goto end; |
2244 | 0 | } |
2245 | 0 | } |
2246 | 0 | #endif |
2247 | | /* DO was not completed in one function call, we must continue |
2248 | | DOING... */ |
2249 | 0 | multistate(data, MSTATE_DOING); |
2250 | 0 | mresult = CURLM_CALL_MULTI_PERFORM; |
2251 | 0 | } |
2252 | | |
2253 | | /* after DO, go DO_DONE... or DO_MORE */ |
2254 | 34.8k | else if(data->conn->bits.do_more) { |
2255 | | /* we are supposed to do more, but we need to sit down, relax and wait |
2256 | | a little while first */ |
2257 | 0 | multistate(data, MSTATE_DOING_MORE); |
2258 | 0 | mresult = CURLM_CALL_MULTI_PERFORM; |
2259 | 0 | } |
2260 | 34.8k | else { |
2261 | | /* we are done with the DO, now DID */ |
2262 | 34.8k | multistate(data, MSTATE_DID); |
2263 | 34.8k | mresult = CURLM_CALL_MULTI_PERFORM; |
2264 | 34.8k | } |
2265 | 34.8k | } |
2266 | 681 | else if((result == CURLE_SEND_ERROR) && |
2267 | 134 | data->conn->bits.reuse) { |
2268 | | /* |
2269 | | * In this situation, a connection that we were trying to use may have |
2270 | | * unexpectedly died. If possible, send the connection back to the |
2271 | | * CONNECT phase so we can try again. |
2272 | | */ |
2273 | 91 | const struct Curl_scheme *handler = data->conn->scheme; |
2274 | 91 | char *newurl = NULL; |
2275 | 91 | followtype follow = FOLLOW_NONE; |
2276 | 91 | CURLcode drc; |
2277 | | |
2278 | 91 | drc = Curl_retry_request(data, &newurl); |
2279 | 91 | if(drc) { |
2280 | | /* a failure here pretty much implies an out of memory */ |
2281 | 1 | result = drc; |
2282 | 1 | *stream_errorp = TRUE; |
2283 | 1 | } |
2284 | | |
2285 | 91 | multi_posttransfer(data); |
2286 | 91 | drc = multi_done(data, result, FALSE); |
2287 | | |
2288 | | /* When set to retry the connection, we must go back to the CONNECT |
2289 | | * state */ |
2290 | 91 | if(newurl) { |
2291 | 90 | if(!drc || (drc == CURLE_SEND_ERROR)) { |
2292 | 90 | follow = FOLLOW_RETRY; |
2293 | 90 | drc = multi_follow(data, handler, newurl, follow); |
2294 | 90 | if(!drc) { |
2295 | 90 | multistate(data, MSTATE_SETUP); |
2296 | 90 | mresult = CURLM_CALL_MULTI_PERFORM; |
2297 | 90 | result = CURLE_OK; |
2298 | 90 | } |
2299 | 0 | else { |
2300 | | /* Follow failed */ |
2301 | 0 | result = drc; |
2302 | 0 | } |
2303 | 90 | } |
2304 | 0 | else { |
2305 | | /* done did not return OK or SEND_ERROR */ |
2306 | 0 | result = drc; |
2307 | 0 | } |
2308 | 90 | } |
2309 | 1 | else { |
2310 | | /* Have error handler disconnect conn if we cannot retry */ |
2311 | 1 | *stream_errorp = TRUE; |
2312 | 1 | } |
2313 | 91 | curlx_free(newurl); |
2314 | 91 | } |
2315 | 590 | else { |
2316 | | /* failure detected */ |
2317 | 590 | multi_posttransfer(data); |
2318 | 590 | if(data->conn) |
2319 | 590 | multi_done(data, result, FALSE); |
2320 | 590 | *stream_errorp = TRUE; |
2321 | 590 | } |
2322 | 35.5k | } |
2323 | 35.7k | end: |
2324 | 35.7k | *resultp = result; |
2325 | 35.7k | return mresult; |
2326 | 35.7k | } |
2327 | | |
2328 | | static CURLMcode multistate_ratelimiting(struct Curl_easy *data, |
2329 | | CURLcode *resultp) |
2330 | 0 | { |
2331 | 0 | CURLcode result = CURLE_OK; |
2332 | 0 | CURLMcode mresult = CURLM_OK; |
2333 | 0 | DEBUGASSERT(data->conn); |
2334 | | /* if both rates are within spec, resume transfer */ |
2335 | 0 | result = Curl_pgrsCheck(data); |
2336 | |
|
2337 | 0 | if(result) { |
2338 | 0 | if(!(data->conn->scheme->flags & PROTOPT_DUAL) && |
2339 | 0 | result != CURLE_HTTP2_STREAM) |
2340 | 0 | streamclose(data->conn); |
2341 | |
|
2342 | 0 | multi_posttransfer(data); |
2343 | 0 | multi_done(data, result, TRUE); |
2344 | 0 | } |
2345 | 0 | else { |
2346 | 0 | if(!mspeed_check(data)) |
2347 | 0 | mresult = CURLM_CALL_MULTI_PERFORM; |
2348 | |
|
2349 | 0 | if(data->mstate == MSTATE_RATELIMITING) { |
2350 | 0 | result = Curl_conn_do_maintenance(data); |
2351 | 0 | if(result) { |
2352 | 0 | if(!(data->conn->scheme->flags & PROTOPT_DUAL) && |
2353 | 0 | result != CURLE_HTTP2_STREAM) |
2354 | 0 | streamclose(data->conn); |
2355 | |
|
2356 | 0 | multi_posttransfer(data); |
2357 | 0 | multi_done(data, result, TRUE); |
2358 | 0 | } |
2359 | 0 | } |
2360 | 0 | } |
2361 | 0 | *resultp = result; |
2362 | 0 | return mresult; |
2363 | 0 | } |
2364 | | |
2365 | | static CURLMcode multistate_connect(struct Curl_multi *multi, |
2366 | | struct Curl_easy *data, |
2367 | | CURLcode *resultp) |
2368 | 39.5k | { |
2369 | | /* Connect. We want to get a connection identifier filled in. This state can |
2370 | | be entered from SETUP and from PENDING. */ |
2371 | 39.5k | bool connected; |
2372 | 39.5k | CURLMcode mresult = CURLM_OK; |
2373 | 39.5k | CURLcode result = Curl_connect(data, &connected); |
2374 | 39.5k | if(result == CURLE_NO_CONNECTION_AVAILABLE) { |
2375 | | /* There was no connection available. We will go to the pending state and |
2376 | | wait for an available connection. */ |
2377 | 0 | multistate(data, MSTATE_PENDING); |
2378 | | /* move from process to pending set */ |
2379 | 0 | Curl_uint32_bset_remove(&multi->process, data->mid); |
2380 | 0 | Curl_uint32_bset_remove(&multi->dirty, data->mid); |
2381 | 0 | Curl_uint32_bset_add(&multi->pending, data->mid); |
2382 | 0 | *resultp = CURLE_OK; |
2383 | 0 | return mresult; |
2384 | 0 | } |
2385 | 39.5k | else |
2386 | 39.5k | multi_schedule_pending(data->multi); |
2387 | | |
2388 | 39.5k | if(!result) { |
2389 | | /* after the connect has been sent off, go WAITCONNECT unless the |
2390 | | protocol connect is already done and we can go directly to WAITDO or |
2391 | | DO! */ |
2392 | 36.9k | mresult = CURLM_CALL_MULTI_PERFORM; |
2393 | | |
2394 | 36.9k | if(connected) { |
2395 | 0 | if(!data->conn->bits.reuse && Curl_conn_is_multiplex(data->conn)) { |
2396 | | /* new connection, can multiplex, wake pending handles */ |
2397 | 0 | multi_schedule_pending(data->multi); |
2398 | 0 | } |
2399 | 0 | multistate(data, MSTATE_PROTOCONNECT); |
2400 | 0 | } |
2401 | 36.9k | else { |
2402 | 36.9k | multistate(data, MSTATE_CONNECTING); |
2403 | 36.9k | } |
2404 | 36.9k | } |
2405 | 39.5k | *resultp = result; |
2406 | 39.5k | return mresult; |
2407 | 39.5k | } |
2408 | | |
2409 | | /* returns the possibly updated result */ |
2410 | | static CURLcode is_finished(struct Curl_multi *multi, |
2411 | | struct Curl_easy *data, |
2412 | | const struct curltime *pnow, |
2413 | | bool stream_error, |
2414 | | CURLcode result) |
2415 | 663k | { |
2416 | 663k | if(data->mstate < MSTATE_COMPLETED) { |
2417 | 649k | if(result) { |
2418 | | /* |
2419 | | * If an error was returned, and we are not in completed state now, |
2420 | | * then we go to completed and consider this transfer aborted. |
2421 | | */ |
2422 | | |
2423 | | /* No attempt to disconnect connections must be made before this - |
2424 | | connection detach and termination happens only here */ |
2425 | | |
2426 | | /* Check if we can move pending requests to send pipe */ |
2427 | 13.3k | multi_schedule_pending(multi); /* connection */ |
2428 | | |
2429 | 13.3k | if(data->conn) { |
2430 | 0 | if(stream_error) { |
2431 | | /* Do not attempt to send data over a connection that timed out */ |
2432 | 0 | bool dead_connection = result == CURLE_OPERATION_TIMEDOUT; |
2433 | 0 | struct connectdata *conn = data->conn; |
2434 | | |
2435 | | /* This is where we make sure that the conn pointer is reset. |
2436 | | We do not have to do this in every case block above where a |
2437 | | failure is detected */ |
2438 | 0 | Curl_detach_connection(data); |
2439 | 0 | Curl_conn_close(data, conn, dead_connection); |
2440 | 0 | } |
2441 | 0 | } |
2442 | 13.3k | else if(data->mstate == MSTATE_CONNECT) { |
2443 | | /* Curl_connect() failed */ |
2444 | 2.66k | multi_posttransfer(data); |
2445 | 2.66k | Curl_pgrsUpdate_nometer(data); |
2446 | 2.66k | } |
2447 | | |
2448 | 13.3k | multistate(data, MSTATE_COMPLETED); |
2449 | 13.3k | return result; |
2450 | 13.3k | } |
2451 | | /* if there is still a connection to use, call the progress function */ |
2452 | 636k | else if(data->conn && Curl_conn_is_connected(data->conn)) { |
2453 | 503k | result = Curl_pgrsUpdateX(data, pnow); |
2454 | 503k | if(result) { |
2455 | | /* aborted due to progress callback return code must close the |
2456 | | connection */ |
2457 | 0 | streamclose(data->conn); |
2458 | | |
2459 | | /* if not yet in DONE state, go there. */ |
2460 | 0 | if(data->mstate < MSTATE_DONE) |
2461 | 0 | multistate(data, MSTATE_DONE); |
2462 | 0 | return result; |
2463 | 0 | } |
2464 | 503k | } |
2465 | 649k | } |
2466 | 650k | return result; |
2467 | 663k | } |
2468 | | |
2469 | | static void handle_completed(struct Curl_multi *multi, |
2470 | | struct Curl_easy *data, |
2471 | | CURLcode result) |
2472 | 27.2k | { |
2473 | 27.2k | bool msg_to_app = data->master_mid == UINT32_MAX; |
2474 | | |
2475 | 27.2k | if(!msg_to_app) { |
2476 | | /* A sub transfer, not reported to the application. Is anyone still |
2477 | | * interested in processing its results? */ |
2478 | 0 | if(data->sub_xfer_done) { |
2479 | 0 | struct Curl_easy *master = Curl_multi_get_easy(multi, data->master_mid); |
2480 | |
|
2481 | 0 | CURL_TRC_M(data, "sub xfer done for master %u", data->master_mid); |
2482 | 0 | if(master) |
2483 | 0 | data->sub_xfer_done(data, master, result); |
2484 | 0 | else |
2485 | 0 | CURL_TRC_M(data, "master easy %u already gone.", data->master_mid); |
2486 | 0 | } |
2487 | 0 | } |
2488 | 27.2k | else { |
2489 | | /* now fill in the CURLMsg with this info */ |
2490 | 27.2k | struct CURLMsg *msg = &data->msg; |
2491 | | |
2492 | 27.2k | msg->msg = CURLMSG_DONE; |
2493 | 27.2k | msg->easy_handle = data; |
2494 | 27.2k | msg->data.result = result; |
2495 | | |
2496 | 27.2k | DEBUGASSERT(!data->conn); |
2497 | 27.2k | } |
2498 | 27.2k | multistate(data, MSTATE_MSGSENT); |
2499 | | |
2500 | | /* remove from the other sets */ |
2501 | 27.2k | Curl_uint32_bset_remove(&multi->process, data->mid); |
2502 | 27.2k | Curl_uint32_bset_remove(&multi->dirty, data->mid); |
2503 | 27.2k | Curl_uint32_bset_remove(&multi->pending, data->mid); |
2504 | 27.2k | if(msg_to_app) |
2505 | 27.2k | multi_addmsg(multi, data); |
2506 | 27.2k | if(data->state.really_alive) { |
2507 | 27.2k | data->state.really_alive = FALSE; |
2508 | 27.2k | --multi->xfers_really_alive; |
2509 | 27.2k | if(!multi->xfers_really_alive) |
2510 | 27.2k | (void)multi_assess_wakeup(multi); |
2511 | 27.2k | } |
2512 | 27.2k | --multi->xfers_alive; |
2513 | 27.2k | if(!multi->xfers_alive) |
2514 | 27.2k | multi_assess_wakeup(multi); |
2515 | 27.2k | } |
2516 | | |
2517 | | static CURLMcode multistate_init(struct Curl_easy *data, CURLcode *result) |
2518 | 27.4k | { |
2519 | 27.4k | if(!data->state.really_alive) { |
2520 | 27.4k | data->state.really_alive = TRUE; |
2521 | 27.4k | ++data->multi->xfers_really_alive; |
2522 | 27.4k | if(data->multi->xfers_really_alive == 1) { |
2523 | 27.4k | CURLMcode mresult = multi_assess_wakeup(data->multi); |
2524 | 27.4k | if(mresult) { |
2525 | 3 | failf(data, "error enabling wakeup listening: %d", mresult); |
2526 | 3 | return mresult; |
2527 | 3 | } |
2528 | 27.4k | } |
2529 | 27.4k | } |
2530 | | |
2531 | 27.4k | *result = Curl_pretransfer(data); |
2532 | 27.4k | if(*result) |
2533 | 124 | return CURLM_OK; |
2534 | | |
2535 | | /* after init, go SETUP */ |
2536 | 27.3k | multistate(data, MSTATE_SETUP); |
2537 | 27.3k | Curl_pgrsTime(data, TIMER_STARTOP); |
2538 | 27.3k | return CURLM_CALL_MULTI_PERFORM; |
2539 | 27.4k | } |
2540 | | |
2541 | | static CURLMcode multistate_setup(struct Curl_easy *data) |
2542 | 39.5k | { |
2543 | 39.5k | const struct curltime *pnow = Curl_pgrs_now(data); |
2544 | 39.5k | timediff_t ctimeout_ms = (data->set.connecttimeout > 0) ? |
2545 | 39.5k | data->set.connecttimeout : DEFAULT_CONNECT_TIMEOUT; |
2546 | | |
2547 | 39.5k | Curl_pgrsTimeWas(data, TIMER_STARTSINGLE, *pnow); |
2548 | 39.5k | if(data->set.timeout) |
2549 | 39.5k | Curl_expire_set(data, EXPIRE_TIMEOUT, data->set.timeout, pnow); |
2550 | | /* Since a connection might go to pending and back to CONNECT several |
2551 | | times before it actually takes off, we need to set the timeout once |
2552 | | in SETUP before we enter CONNECT the first time. */ |
2553 | 39.5k | Curl_expire_set(data, EXPIRE_CONNECTTIMEOUT, ctimeout_ms, pnow); |
2554 | | |
2555 | 39.5k | multistate(data, MSTATE_CONNECT); |
2556 | 39.5k | return CURLM_CALL_MULTI_PERFORM; |
2557 | 39.5k | } |
2558 | | |
2559 | | static CURLMcode multistate_connecting(struct Curl_easy *data, |
2560 | | bool *stream_error, |
2561 | | CURLcode *result) |
2562 | 55.2k | { |
2563 | 55.2k | bool connected; |
2564 | | |
2565 | 55.2k | if(!data->conn) { |
2566 | 0 | DEBUGASSERT(0); |
2567 | 0 | *result = CURLE_FAILED_INIT; |
2568 | 0 | return CURLM_OK; |
2569 | 0 | } |
2570 | 55.2k | if(!Curl_xfer_recv_is_paused(data)) { |
2571 | 55.2k | *result = Curl_conn_connect(data, FALSE, &connected); |
2572 | 55.2k | if(connected && !*result) { |
2573 | 35.7k | if(!data->conn->bits.reuse && Curl_conn_is_multiplex(data->conn)) { |
2574 | | /* new connection, can multiplex, wake pending handles */ |
2575 | 18.4k | multi_schedule_pending(data->multi); |
2576 | 18.4k | } |
2577 | 35.7k | multistate(data, MSTATE_PROTOCONNECT); |
2578 | 35.7k | return CURLM_CALL_MULTI_PERFORM; |
2579 | 35.7k | } |
2580 | 19.4k | else if(*result) { |
2581 | | /* failure detected */ |
2582 | 955 | CURL_TRC_M(data, "connect failed -> %d", (int)*result); |
2583 | 955 | multi_posttransfer(data); |
2584 | 955 | multi_done(data, *result, TRUE); |
2585 | 955 | *stream_error = TRUE; |
2586 | 955 | return CURLM_OK; |
2587 | 955 | } |
2588 | 55.2k | } |
2589 | 18.5k | return CURLM_OK; |
2590 | 55.2k | } |
2591 | | |
2592 | | static CURLMcode multistate_protoconnect(struct Curl_easy *data, |
2593 | | bool *stream_error, |
2594 | | CURLcode *result) |
2595 | 35.7k | { |
2596 | 35.7k | bool protocol_connected = FALSE; |
2597 | | |
2598 | 35.7k | if(!*result && data->conn->bits.reuse) { |
2599 | | /* ftp seems to hang when protoconnect on reused connection since we |
2600 | | * handle PROTOCONNECT in general inside the filters, it seems wrong to |
2601 | | * restart this on a reused connection. |
2602 | | */ |
2603 | 1.53k | multistate(data, MSTATE_DO); |
2604 | 1.53k | return CURLM_CALL_MULTI_PERFORM; |
2605 | 1.53k | } |
2606 | 34.2k | if(!*result) |
2607 | 34.2k | *result = protocol_connect(data, &protocol_connected); |
2608 | 34.2k | if(!*result && !protocol_connected) { |
2609 | | /* switch to waiting state */ |
2610 | 0 | multistate(data, MSTATE_PROTOCONNECTING); |
2611 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2612 | 0 | } |
2613 | 34.2k | else if(!*result) { |
2614 | | /* protocol connect has completed, go WAITDO or DO */ |
2615 | 34.2k | multistate(data, MSTATE_DO); |
2616 | 34.2k | return CURLM_CALL_MULTI_PERFORM; |
2617 | 34.2k | } |
2618 | | |
2619 | | /* failure detected */ |
2620 | 0 | multi_posttransfer(data); |
2621 | 0 | multi_done(data, *result, TRUE); |
2622 | 0 | *stream_error = TRUE; |
2623 | 0 | return CURLM_OK; |
2624 | 34.2k | } |
2625 | | |
2626 | | static CURLMcode multistate_protoconnecting(struct Curl_easy *data, |
2627 | | bool *stream_error, |
2628 | | CURLcode *result) |
2629 | 0 | { |
2630 | 0 | bool protocol_connected = FALSE; |
2631 | | |
2632 | | /* protocol-specific connect phase */ |
2633 | 0 | *result = protocol_connecting(data, &protocol_connected); |
2634 | 0 | if(!*result && protocol_connected) { |
2635 | | /* after the connect has completed, go WAITDO or DO */ |
2636 | 0 | multistate(data, MSTATE_DO); |
2637 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2638 | 0 | } |
2639 | 0 | else if(*result) { |
2640 | | /* failure detected */ |
2641 | 0 | multi_posttransfer(data); |
2642 | 0 | multi_done(data, *result, TRUE); |
2643 | 0 | *stream_error = TRUE; |
2644 | 0 | } |
2645 | 0 | return CURLM_OK; |
2646 | 0 | } |
2647 | | |
2648 | | static CURLMcode multistate_doing(struct Curl_easy *data, |
2649 | | bool *stream_error, |
2650 | | CURLcode *result) |
2651 | 0 | { |
2652 | 0 | bool dophase_done = FALSE; |
2653 | | |
2654 | | /* we continue DOING until the DO phase is complete */ |
2655 | 0 | DEBUGASSERT(data->conn); |
2656 | 0 | *result = protocol_doing(data, &dophase_done); |
2657 | 0 | if(!*result) { |
2658 | 0 | if(dophase_done) { |
2659 | | /* after DO, go DO_DONE or DO_MORE */ |
2660 | 0 | multistate(data, data->conn->bits.do_more ? |
2661 | 0 | MSTATE_DOING_MORE : MSTATE_DID); |
2662 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2663 | 0 | } /* dophase_done */ |
2664 | 0 | } |
2665 | 0 | else { |
2666 | | /* failure detected */ |
2667 | 0 | multi_posttransfer(data); |
2668 | 0 | multi_done(data, *result, FALSE); |
2669 | 0 | *stream_error = TRUE; |
2670 | 0 | } |
2671 | 0 | return CURLM_OK; |
2672 | 0 | } |
2673 | | |
2674 | | static CURLMcode multistate_doing_more(struct Curl_easy *data, |
2675 | | bool *stream_error, |
2676 | | CURLcode *result) |
2677 | 0 | { |
2678 | 0 | domore control; |
2679 | | |
2680 | | /* |
2681 | | * When we are connected, DOING MORE and then go DID |
2682 | | */ |
2683 | 0 | DEBUGASSERT(data->conn); |
2684 | 0 | *result = multi_do_more(data, &control); |
2685 | |
|
2686 | 0 | if(!*result) { |
2687 | 0 | if(control != DOMORE_INCOMPLETE) { |
2688 | | /* if DONE, advance to DO_DONE |
2689 | | if GOBACK, go back to DOING */ |
2690 | 0 | multistate(data, control == DOMORE_DONE ? MSTATE_DID : MSTATE_DOING); |
2691 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2692 | 0 | } |
2693 | | /* else |
2694 | | stay in DO_MORE */ |
2695 | 0 | } |
2696 | 0 | else { |
2697 | | /* failure detected */ |
2698 | 0 | multi_posttransfer(data); |
2699 | 0 | multi_done(data, *result, FALSE); |
2700 | 0 | *stream_error = TRUE; |
2701 | 0 | } |
2702 | 0 | return CURLM_OK; |
2703 | 0 | } |
2704 | | |
2705 | | static CURLMcode multistate_did(struct Curl_multi *multi, |
2706 | | struct Curl_easy *data) |
2707 | 34.8k | { |
2708 | 34.8k | DEBUGASSERT(data->conn); |
2709 | 34.8k | if(data->conn->bits.multiplex) |
2710 | | /* Check if we can move pending requests to send pipe */ |
2711 | 19.1k | multi_schedule_pending(multi); /* multiplexed */ |
2712 | | |
2713 | | /* Only perform the transfer if there is a good socket to work with. |
2714 | | Having both BAD is a signal to skip immediately to DONE */ |
2715 | 34.8k | if(data->conn->bits.want_recv || data->conn->bits.want_send) { |
2716 | 34.8k | multistate(data, MSTATE_PERFORMING); |
2717 | | /* Do not return CURLM_CALL_MULTI_PERFORM to give other transfers |
2718 | | * a chance to send off their requests. |
2719 | | * Note: Some SFTP handlers do not seem to like this. |
2720 | | * Restrict it to HTTP families. */ |
2721 | 34.8k | return ((multi->xfers_alive > 1) && |
2722 | 0 | (data->conn->scheme->protocol & PROTO_FAMILY_HTTP)) ? |
2723 | 34.8k | CURLM_OK : CURLM_CALL_MULTI_PERFORM; |
2724 | 34.8k | } |
2725 | 0 | else { |
2726 | 0 | #ifndef CURL_DISABLE_FTP |
2727 | 0 | if(data->state.wildcardmatch && |
2728 | 0 | ((data->conn->scheme->flags & PROTOPT_WILDCARD) == 0)) { |
2729 | 0 | data->wildcard->state = CURLWC_DONE; |
2730 | 0 | } |
2731 | 0 | #endif |
2732 | 0 | multistate(data, MSTATE_DONE); |
2733 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2734 | 0 | } |
2735 | 34.8k | } |
2736 | | |
2737 | | static CURLMcode multistate_done(struct Curl_easy *data, CURLcode *presult) |
2738 | 13.8k | { |
2739 | 13.8k | if(data->conn) { |
2740 | 13.8k | CURLcode result; |
2741 | | |
2742 | | /* post-transfer command */ |
2743 | 13.8k | result = multi_done(data, *presult, FALSE); |
2744 | | |
2745 | | /* allow a previously set error code take precedence */ |
2746 | 13.8k | if(!(*presult)) |
2747 | 13.8k | *presult = result; |
2748 | 13.8k | } |
2749 | | |
2750 | 13.8k | #ifndef CURL_DISABLE_FTP |
2751 | 13.8k | if(data->state.wildcardmatch) { |
2752 | 0 | if(data->wildcard->state != CURLWC_DONE) { |
2753 | | /* if a wildcard is set and we are not ending -> lets start again |
2754 | | with MSTATE_INIT */ |
2755 | 0 | multistate(data, MSTATE_INIT); |
2756 | 0 | return CURLM_CALL_MULTI_PERFORM; |
2757 | 0 | } |
2758 | 0 | } |
2759 | 13.8k | #endif |
2760 | | /* after we have DONE what we are supposed to do, go COMPLETED, and |
2761 | | it does not matter what the multi_done() returned! */ |
2762 | 13.8k | multistate(data, MSTATE_COMPLETED); |
2763 | 13.8k | return CURLM_CALL_MULTI_PERFORM; |
2764 | 13.8k | } |
2765 | | |
2766 | | static CURLMcode multi_runsingle(struct Curl_multi *multi, |
2767 | | struct Curl_easy *data, |
2768 | | struct Curl_sigpipe_ctx *sigpipe_ctx) |
2769 | 609k | { |
2770 | 609k | CURLMcode mresult = CURLM_OK; |
2771 | 609k | CURLcode result = CURLE_OK; |
2772 | 609k | const struct curltime *pnow = NULL; |
2773 | | |
2774 | 609k | if(multi->dead) { |
2775 | | /* a multi-level callback returned error before, meaning every individual |
2776 | | transfer now has failed */ |
2777 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
2778 | 0 | multi_posttransfer(data); |
2779 | 0 | multi_done(data, result, FALSE); |
2780 | 0 | multistate(data, MSTATE_COMPLETED); |
2781 | 0 | } |
2782 | | |
2783 | 609k | multi_warn_debug(multi, data); |
2784 | | |
2785 | | /* transfer runs now, clear the dirty bit. This may be set |
2786 | | * again during processing, triggering a re-run later. */ |
2787 | 609k | Curl_uint32_bset_remove(&multi->dirty, data->mid); |
2788 | | |
2789 | 609k | if(data == multi->admin) { |
2790 | 230k | #ifdef ENABLE_INTERNAL_WAKEUP |
2791 | | /* Consume any pending wakeup signals before processing. |
2792 | | * This is necessary for event based processing. See #21547 */ |
2793 | 230k | (void)Curl_wakeup_consume(multi->wakeup_internal, TRUE); |
2794 | 230k | #endif |
2795 | 230k | #ifdef USE_RESOLV_THREADED |
2796 | 230k | Curl_async_thrdd_multi_process(multi); |
2797 | 230k | #endif |
2798 | 230k | Curl_cshutdn_perform(&multi->cshutdn, multi->admin, sigpipe_ctx); |
2799 | 230k | goto out; |
2800 | 230k | } |
2801 | | |
2802 | 379k | sigpipe_apply(data, sigpipe_ctx); |
2803 | 663k | do { |
2804 | | /* A "stream" here is a logical stream if the protocol can handle that |
2805 | | (HTTP/2), or the full connection for older protocols */ |
2806 | 663k | bool stream_error = FALSE; |
2807 | 663k | mresult = CURLM_OK; |
2808 | 663k | pnow = NULL; |
2809 | | |
2810 | 663k | if(multi_ischanged(multi, TRUE)) { |
2811 | 41.7k | CURL_TRC_M(data, "multi changed, check CONNECT_PEND queue"); |
2812 | 41.7k | multi_schedule_pending(multi); /* multiplexed */ |
2813 | 41.7k | } |
2814 | | |
2815 | 663k | if(data->mstate > MSTATE_CONNECT && |
2816 | 556k | data->mstate < MSTATE_COMPLETED) { |
2817 | | /* Make sure we set the connection's current owner */ |
2818 | 556k | DEBUGASSERT(data->conn); |
2819 | 556k | if(!data->conn) { |
2820 | 0 | mresult = CURLM_INTERNAL_ERROR; |
2821 | 0 | goto out; |
2822 | 0 | } |
2823 | 556k | } |
2824 | | |
2825 | | /* Wait for the connect state as only then is the start time stored, but |
2826 | | we must not check already completed handles. |
2827 | | |
2828 | | Check the overall timeout here but defer the connect timeout to after |
2829 | | the state machine ran (below): the connection may have completed in |
2830 | | the window since the application last called us, and the state |
2831 | | machine must get to see the connected socket before we declare the |
2832 | | connect timed out. Otherwise an application that is slow to call |
2833 | | curl_multi_perform() gets CURLE_OPERATION_TIMEDOUT on a connection |
2834 | | that is already up. |
2835 | | |
2836 | | In MSTATE_CONNECT no connect is underway yet, so the connect timeout |
2837 | | is still checked here: a transfer queued by a connection limit past |
2838 | | its connect timeout must fail instead of going back to PENDING. */ |
2839 | 663k | if((data->mstate >= MSTATE_CONNECT) && (data->mstate < MSTATE_COMPLETED)) { |
2840 | 596k | pnow = Curl_pgrs_now(data); |
2841 | 596k | if(multi_handle_timeout(data, pnow, &stream_error, &result, |
2842 | 596k | data->mstate == MSTATE_CONNECT)) |
2843 | | /* Skip the statemachine and go directly to error handling section. */ |
2844 | 48 | goto statemachine_end; |
2845 | 596k | pnow = NULL; |
2846 | 596k | } |
2847 | | |
2848 | 663k | switch(data->mstate) { |
2849 | 27.4k | case MSTATE_INIT: |
2850 | | /* Transitional state. init this transfer. A handle never comes back to |
2851 | | this state. */ |
2852 | 27.4k | mresult = multistate_init(data, &result); |
2853 | 27.4k | break; |
2854 | | |
2855 | 39.5k | case MSTATE_SETUP: |
2856 | | /* Transitional state. Setup things for a new transfer. The handle |
2857 | | can come back to this state on a redirect. */ |
2858 | 39.5k | mresult = multistate_setup(data); |
2859 | 39.5k | break; |
2860 | | |
2861 | 39.5k | case MSTATE_CONNECT: |
2862 | 39.5k | mresult = multistate_connect(multi, data, &result); |
2863 | 39.5k | break; |
2864 | | |
2865 | 55.2k | case MSTATE_CONNECTING: |
2866 | | /* awaiting a completion of an asynch TCP connect */ |
2867 | 55.2k | mresult = multistate_connecting(data, &stream_error, &result); |
2868 | 55.2k | break; |
2869 | | |
2870 | 35.7k | case MSTATE_PROTOCONNECT: |
2871 | 35.7k | mresult = multistate_protoconnect(data, &stream_error, &result); |
2872 | 35.7k | break; |
2873 | | |
2874 | 0 | case MSTATE_PROTOCONNECTING: |
2875 | | /* protocol-specific connect phase */ |
2876 | 0 | mresult = multistate_protoconnecting(data, &stream_error, &result); |
2877 | 0 | break; |
2878 | | |
2879 | 35.7k | case MSTATE_DO: |
2880 | 35.7k | mresult = multistate_do(data, &stream_error, &result); |
2881 | 35.7k | break; |
2882 | | |
2883 | 0 | case MSTATE_DOING: |
2884 | | /* we continue DOING until the DO phase is complete */ |
2885 | 0 | mresult = multistate_doing(data, &stream_error, &result); |
2886 | 0 | break; |
2887 | | |
2888 | 0 | case MSTATE_DOING_MORE: |
2889 | | /* |
2890 | | * When we are connected, DOING MORE and then go DID |
2891 | | */ |
2892 | 0 | mresult = multistate_doing_more(data, &stream_error, &result); |
2893 | 0 | break; |
2894 | | |
2895 | 34.8k | case MSTATE_DID: |
2896 | 34.8k | mresult = multistate_did(multi, data); |
2897 | 34.8k | break; |
2898 | | |
2899 | 0 | case MSTATE_RATELIMITING: /* limit-rate exceeded in either direction */ |
2900 | 0 | mresult = multistate_ratelimiting(data, &result); |
2901 | 0 | break; |
2902 | | |
2903 | 381k | case MSTATE_PERFORMING: |
2904 | 381k | mresult = multistate_performing(data, &stream_error, &result); |
2905 | 381k | break; |
2906 | | |
2907 | 13.8k | case MSTATE_DONE: |
2908 | 13.8k | mresult = multistate_done(data, &result); |
2909 | 13.8k | break; |
2910 | | |
2911 | 0 | case MSTATE_COMPLETED: |
2912 | 0 | break; |
2913 | | |
2914 | 0 | case MSTATE_PENDING: |
2915 | 0 | case MSTATE_MSGSENT: |
2916 | | /* handles in these states should NOT be in this list */ |
2917 | 0 | break; |
2918 | | |
2919 | 0 | default: |
2920 | 0 | mresult = CURLM_INTERNAL_ERROR; |
2921 | 0 | goto out; |
2922 | 663k | } |
2923 | | |
2924 | 663k | if(data->mstate >= MSTATE_CONNECT && |
2925 | 623k | data->mstate < MSTATE_COMPLETED && |
2926 | 609k | (Curl_is_connecting(data) || |
2927 | 475k | (data->conn && data->conn->connect_start_ms >= 0)) && |
2928 | 134k | mresult != CURLM_CALL_MULTI_PERFORM && |
2929 | 22.1k | !multi_ischanged(multi, FALSE)) { |
2930 | | /* We now handle stream timeouts if and only if this will be the last |
2931 | | * loop iteration. We only check this on the last iteration to ensure |
2932 | | * that if we know we have additional work to do immediately |
2933 | | * (i.e. CURLM_CALL_MULTI_PERFORM == TRUE) then we should do that before |
2934 | | * declaring the connection timed out as we may almost have a completed |
2935 | | * connection. This covers the connect of the transfer's connection, |
2936 | | * a handshake still connecting in a later state (WebSocket Upgrade) |
2937 | | * and a connect of a secondary connection (`connect_start_ms` set), |
2938 | | * like the FTP data connection. */ |
2939 | 21.1k | pnow = Curl_pgrs_now(data); |
2940 | 21.1k | multi_handle_timeout(data, pnow, &stream_error, &result, TRUE); |
2941 | 21.1k | } |
2942 | | |
2943 | 663k | statemachine_end: |
2944 | 663k | if(!pnow) |
2945 | 642k | pnow = Curl_pgrs_now(data); |
2946 | 663k | result = is_finished(multi, data, pnow, stream_error, result); |
2947 | 663k | if(result) |
2948 | 21.9k | mresult = CURLM_CALL_MULTI_PERFORM; |
2949 | | |
2950 | 663k | if(MSTATE_COMPLETED == data->mstate) { |
2951 | 27.2k | handle_completed(multi, data, result); |
2952 | 27.2k | mresult = CURLM_OK; |
2953 | 27.2k | goto out; |
2954 | 27.2k | } |
2955 | 663k | } while((mresult == CURLM_CALL_MULTI_PERFORM) || |
2956 | 365k | multi_ischanged(multi, FALSE)); |
2957 | | |
2958 | 609k | out: |
2959 | 609k | data->done = (uint8_t)result; |
2960 | 609k | return mresult; |
2961 | 379k | } |
2962 | | |
2963 | | static CURLMcode multi_perform(struct Curl_multi *multi, |
2964 | | int *running_handles) |
2965 | 230k | { |
2966 | 230k | CURLMcode returncode = CURLM_OK; |
2967 | 230k | struct curltime start; |
2968 | 230k | uint32_t mid; |
2969 | 230k | struct Curl_sigpipe_ctx sigpipe_ctx; |
2970 | | |
2971 | 230k | curlx_pnow(&start); |
2972 | 230k | sigpipe_init(&sigpipe_ctx); |
2973 | | |
2974 | 230k | if(Curl_uint32_bset_first(&multi->process, &mid)) { |
2975 | 230k | CURL_TRC_M(multi->admin, "multi_perform(running=%u)", |
2976 | 230k | multi_xfers_running(multi)); |
2977 | 460k | do { |
2978 | 460k | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
2979 | 460k | CURLMcode mresult; |
2980 | 460k | if(!data) { |
2981 | 0 | DEBUGASSERT(0); |
2982 | 0 | Curl_uint32_bset_remove(&multi->process, mid); |
2983 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
2984 | 0 | continue; |
2985 | 0 | } |
2986 | 460k | mresult = multi_runsingle(multi, data, &sigpipe_ctx); |
2987 | 460k | if(mresult) |
2988 | 0 | returncode = mresult; |
2989 | 460k | } while(Curl_uint32_bset_next(&multi->process, mid, &mid)); |
2990 | 230k | } |
2991 | 230k | sigpipe_restore(&sigpipe_ctx); |
2992 | | |
2993 | 230k | if(multi_ischanged(multi, TRUE)) |
2994 | 20.1k | multi_schedule_pending(multi); |
2995 | | |
2996 | 230k | if(!returncode && CURL_MNTFY_HAS_ENTRIES(multi)) |
2997 | 0 | returncode = Curl_mntfy_dispatch_all(multi); |
2998 | | |
2999 | | /* |
3000 | | * Remove all expired timers from the splay since handles are dealt |
3001 | | * with unconditionally by this function and curl_multi_timeout() requires |
3002 | | * that already passed/handled expire times are removed from the splay. |
3003 | | * |
3004 | | * It is important that the 'now' value is set at the entry of this function |
3005 | | * and not for the current time as it may have ticked a little while since |
3006 | | * then and then we risk this loop to remove timers that actually have not |
3007 | | * been handled! |
3008 | | */ |
3009 | 230k | while(Curl_timeouts_remove_expired(&multi->timeouts, &start, &mid)) { |
3010 | | /* the removed may have another timeout in queue */ |
3011 | 16 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3012 | 16 | if(!data) { |
3013 | 0 | DEBUGASSERT(0); |
3014 | 0 | continue; |
3015 | 0 | } |
3016 | 16 | (void)add_next_timeout(&start, multi, data); |
3017 | 16 | if(data->mstate == MSTATE_PENDING) { |
3018 | 0 | bool stream_unused; |
3019 | 0 | CURLcode result_unused; |
3020 | 0 | if(multi_handle_timeout(data, Curl_pgrs_now(data), |
3021 | 0 | &stream_unused, &result_unused, TRUE)) { |
3022 | 0 | infof(data, "PENDING handle timeout"); |
3023 | 0 | move_pending_to_connect(multi, data); |
3024 | 0 | } |
3025 | 0 | } |
3026 | 16 | } |
3027 | | |
3028 | 230k | if(running_handles) { |
3029 | 230k | uint32_t running = multi_xfers_running(multi); |
3030 | 230k | *running_handles = (running < INT_MAX) ? (int)running : INT_MAX; |
3031 | 230k | } |
3032 | | |
3033 | 230k | if(CURLM_OK >= returncode) |
3034 | 230k | returncode = Curl_update_timer(multi); |
3035 | | |
3036 | 230k | return returncode; |
3037 | 230k | } |
3038 | | |
3039 | | CURLMcode curl_multi_perform(CURLM *m, int *running_handles) |
3040 | 230k | { |
3041 | 230k | struct Curl_mapi_guard guard; |
3042 | 230k | CURLMcode mresult; |
3043 | | |
3044 | 230k | if(CURL_MAPI_ENTER(&guard, m, multi_perform, &mresult)) { |
3045 | 230k | mresult = multi_perform(m, running_handles); |
3046 | 230k | } |
3047 | 230k | CURL_MAPI_LEAVE(&guard); |
3048 | 230k | return mresult; |
3049 | 230k | } |
3050 | | |
3051 | | CURLMcode curl_multi_cleanup(CURLM *m) |
3052 | 27.4k | { |
3053 | 27.4k | struct Curl_mapi_guard guard; |
3054 | 27.4k | CURLMcode mresult; |
3055 | | |
3056 | 27.4k | if(CURL_MAPI_ENTER(&guard, m, multi_cleanup, &mresult)) { |
3057 | 27.4k | struct Curl_multi *multi = m; |
3058 | 27.4k | void *entry; |
3059 | 27.4k | uint32_t mid; |
3060 | | |
3061 | | /* First remove all remaining easy handles, |
3062 | | * close internal ones. admin handle is special */ |
3063 | 27.4k | if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) { |
3064 | 27.4k | do { |
3065 | 27.4k | struct Curl_easy *data = entry; |
3066 | 27.4k | if(!GOOD_EASY_HANDLE(data)) { |
3067 | 0 | mresult = CURLM_BAD_HANDLE; |
3068 | 0 | goto out; |
3069 | 0 | } |
3070 | | |
3071 | 27.4k | #ifdef DEBUGBUILD |
3072 | 27.4k | if(mid != data->mid) { |
3073 | 0 | CURL_TRC_M(data, "multi_cleanup: still present with mid=%u, " |
3074 | 0 | "but unexpected data->mid=%u\n", mid, data->mid); |
3075 | 0 | DEBUGASSERT(0); |
3076 | 0 | } |
3077 | 27.4k | #endif |
3078 | | |
3079 | 27.4k | if(data == multi->admin) |
3080 | 27.4k | continue; |
3081 | | |
3082 | 0 | if(!data->state.done && data->conn) |
3083 | | /* if DONE was never called for this handle */ |
3084 | 0 | (void)multi_done(data, CURLE_OK, TRUE); |
3085 | |
|
3086 | 0 | data->multi = NULL; /* clear the association */ |
3087 | 0 | Curl_uint32_tbl_remove(&multi->xfers, mid); |
3088 | 0 | data->mid = UINT32_MAX; |
3089 | |
|
3090 | | #ifdef USE_LIBPSL |
3091 | | if(data->psl == &multi->psl) |
3092 | | data->psl = NULL; |
3093 | | #endif |
3094 | 0 | if(data->state.internal) |
3095 | 0 | Curl_close(&data); |
3096 | 27.4k | } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry)); |
3097 | 27.4k | } |
3098 | | |
3099 | 27.4k | #ifdef USE_RESOLV_THREADED |
3100 | 27.4k | Curl_async_thrdd_multi_destroy(multi, !multi->quick_exit); |
3101 | 27.4k | #endif |
3102 | 27.4k | Curl_cpool_destroy(&multi->cpool, multi->admin); |
3103 | 27.4k | Curl_cshutdn_destroy(&multi->cshutdn, multi->admin); |
3104 | 27.4k | if(multi->admin) { |
3105 | 27.4k | CURL_TRC_M(multi->admin, "multi_cleanup, closing admin handle, done"); |
3106 | 27.4k | multi->admin->multi = NULL; |
3107 | 27.4k | Curl_uint32_tbl_remove(&multi->xfers, multi->admin->mid); |
3108 | 27.4k | Curl_close(&multi->admin); |
3109 | 27.4k | } |
3110 | | |
3111 | 27.4k | multi->magic = 0; /* not good anymore */ |
3112 | | |
3113 | 27.4k | Curl_multi_ev_cleanup(multi); |
3114 | 27.4k | Curl_hash_destroy(&multi->proto_hash); |
3115 | 27.4k | Curl_dnscache_destroy(&multi->dnscache); |
3116 | 27.4k | Curl_psl_destroy(&multi->psl); |
3117 | 27.4k | #ifdef USE_SSL |
3118 | 27.4k | Curl_ssl_scache_destroy(multi->ssl_scache); |
3119 | 27.4k | #endif |
3120 | | |
3121 | | #ifdef USE_WINSOCK |
3122 | | WSACloseEvent(multi->wsa_event); |
3123 | | #endif |
3124 | 27.4k | #ifdef ENABLE_WAKEUP |
3125 | 27.4k | Curl_wakeup_destroy(multi->wakeup_pair); |
3126 | 27.4k | #endif |
3127 | 27.4k | #ifdef ENABLE_INTERNAL_WAKEUP |
3128 | 27.4k | Curl_wakeup_destroy(multi->wakeup_internal); |
3129 | 27.4k | #endif |
3130 | | |
3131 | 27.4k | multi_xfer_bufs_free(multi); |
3132 | 27.4k | Curl_mntfy_cleanup(multi); |
3133 | 27.4k | #ifdef DEBUGBUILD |
3134 | 27.4k | if(Curl_uint32_tbl_count(&multi->xfers)) { |
3135 | 0 | multi_xfer_tbl_dump(multi); |
3136 | 0 | DEBUGASSERT(0); |
3137 | 0 | } |
3138 | 27.4k | #endif |
3139 | 27.4k | Curl_uint32_bset_destroy(&multi->process); |
3140 | 27.4k | Curl_uint32_bset_destroy(&multi->dirty); |
3141 | 27.4k | Curl_uint32_bset_destroy(&multi->pending); |
3142 | 27.4k | Curl_uint32_bset_destroy(&multi->msgsent); |
3143 | 27.4k | Curl_uint32_tbl_destroy(&multi->xfers); |
3144 | 27.4k | curlx_memzero(multi, sizeof(*multi)); |
3145 | 27.4k | curlx_free(multi); |
3146 | | |
3147 | 27.4k | mresult = CURLM_OK; |
3148 | 27.4k | } |
3149 | 27.4k | out: |
3150 | 27.4k | CURL_MAPI_LEAVE(&guard); |
3151 | 27.4k | return mresult; |
3152 | 27.4k | } |
3153 | | |
3154 | | /* |
3155 | | * curl_multi_info_read() |
3156 | | * |
3157 | | * This function is the primary way for a multi/multi_socket application to |
3158 | | * figure out if a transfer has ended. |
3159 | | */ |
3160 | | |
3161 | | CURLMsg *curl_multi_info_read(CURLM *m, int *msgs_in_queue) |
3162 | 157k | { |
3163 | 157k | struct Curl_mapi_guard guard; |
3164 | 157k | CURLMsg *msg_result = NULL; |
3165 | | |
3166 | 157k | *msgs_in_queue = 0; /* default to none */ |
3167 | 157k | if(CURL_MAPI_ENTER(&guard, m, multi_info_read, NULL)) { |
3168 | 157k | struct Curl_multi *multi = m; |
3169 | 157k | uint32_t mid; |
3170 | 157k | if(Curl_uint32_bset_first(&multi->msgsent, &mid)) { |
3171 | 27.2k | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3172 | | |
3173 | 27.2k | DEBUGASSERT(data); |
3174 | 27.2k | Curl_uint32_bset_remove(&multi->msgsent, mid); |
3175 | 27.2k | *msgs_in_queue = |
3176 | 27.2k | curlx_uztosi(Curl_uint32_bset_count(&multi->msgsent)); |
3177 | 27.2k | if(data) |
3178 | 27.2k | msg_result = &data->msg; |
3179 | 27.2k | } |
3180 | 157k | } |
3181 | 157k | CURL_MAPI_LEAVE(&guard); |
3182 | 157k | return msg_result; |
3183 | 157k | } |
3184 | | |
3185 | | void Curl_multi_will_close(struct Curl_easy *data, curl_socket_t s) |
3186 | 34.3k | { |
3187 | 34.3k | if(data) { |
3188 | 34.3k | struct Curl_multi *multi = data->multi; |
3189 | 34.3k | if(multi) { |
3190 | 34.2k | CURL_TRC_M(data, "Curl_multi_will_close fd=%" FMT_SOCKET_T, s); |
3191 | 34.2k | Curl_multi_ev_socket_done(multi, data, s); |
3192 | 34.2k | } |
3193 | 34.3k | } |
3194 | 34.3k | } |
3195 | | |
3196 | | static void multi_timeouts_init(struct Curl_easy *data) |
3197 | 82.3k | { |
3198 | 82.3k | data->state.timeouts.first = EXPIRE_LAST; |
3199 | 82.3k | data->state.timeouts.splaynode.registered = FALSE; |
3200 | 82.3k | } |
3201 | | |
3202 | | /* |
3203 | | * Each Curl_easy has a list of timeouts. The add_next_timeout() is called |
3204 | | * when it has been removed from the splay tree because the timeout has |
3205 | | * expired. This function is then to advance in the list to pick the next |
3206 | | * timeout to use (skip the already expired ones) and add this node back to |
3207 | | * the splay tree again. |
3208 | | * |
3209 | | * The splay tree only has each Curl_easy as a single node and the nearest |
3210 | | * timeout is used to sort it on. |
3211 | | */ |
3212 | | static CURLMcode add_next_timeout(const struct curltime *pnow, |
3213 | | struct Curl_multi *multi, |
3214 | | struct Curl_easy *data) |
3215 | 79 | { |
3216 | 79 | struct expire_timers *timeouts = &data->state.timeouts; |
3217 | 79 | timediff_t now_us = Curl_timeouts_offset_us(&multi->timeouts, pnow); |
3218 | | |
3219 | 180 | while(timeouts->first < EXPIRE_LAST) { |
3220 | 165 | if(timeouts->offset_us[timeouts->first] <= now_us) /* already expired */ |
3221 | 101 | timeouts->first = timeouts->next[timeouts->first]; |
3222 | 64 | else /* timeouts are sorted, first is first in the future now */ |
3223 | 64 | break; |
3224 | 165 | } |
3225 | | |
3226 | 79 | if(timeouts->first < EXPIRE_LAST) { |
3227 | | /* Insert this node again into the splay. Keep the timer in the list in |
3228 | | case we need to recompute future timers. */ |
3229 | 64 | Curl_timeouts_add(&multi->timeouts, data, |
3230 | 64 | timeouts->offset_us[timeouts->first]); |
3231 | 64 | } |
3232 | 79 | return CURLM_OK; |
3233 | 79 | } |
3234 | | |
3235 | | static void multi_mark_expired_as_dirty(struct Curl_multi *multi, |
3236 | | const struct curltime *ts) |
3237 | 291k | { |
3238 | 291k | struct Curl_easy *data = NULL; |
3239 | 291k | uint32_t mid; |
3240 | | |
3241 | | /* |
3242 | | * The loop following here will go on as long as there are expire-times left |
3243 | | * to process (compared to `ts`) in the splay and 'data' will be |
3244 | | * re-assigned for every expired handle we deal with. |
3245 | | */ |
3246 | 291k | while(Curl_timeouts_remove_expired(&multi->timeouts, ts, &mid)) { |
3247 | | /* Check if there is one (more) expired timer to deal with! This function |
3248 | | extracts a matching node if there is one */ |
3249 | 63 | data = Curl_multi_get_easy(multi, mid); |
3250 | 63 | if(!data) { |
3251 | 0 | DEBUGASSERT(0); |
3252 | 0 | continue; |
3253 | 0 | } |
3254 | 63 | #ifdef CURLVERBOSE |
3255 | 63 | if(CURL_TRC_TIMER_is_verbose(data)) { |
3256 | 12 | if(data->state.timeouts.first < EXPIRE_LAST) { |
3257 | 12 | CURL_TRC_TIMER(data, data->state.timeouts.first, "has expired"); |
3258 | 12 | } |
3259 | 12 | } |
3260 | 63 | #endif |
3261 | 63 | (void)add_next_timeout(ts, multi, data); |
3262 | 63 | Curl_multi_mark_dirty(data); |
3263 | 63 | } |
3264 | 291k | } |
3265 | | |
3266 | | static CURLMcode multi_run_dirty(struct Curl_multi *multi, |
3267 | | struct Curl_sigpipe_ctx *sigpipe_ctx, |
3268 | | uint32_t *pnum) |
3269 | 291k | { |
3270 | 291k | CURLMcode mresult = CURLM_OK; |
3271 | 291k | uint32_t mid; |
3272 | | |
3273 | 291k | *pnum = 0; |
3274 | 291k | if(Curl_uint32_bset_first(&multi->dirty, &mid)) { |
3275 | 149k | do { |
3276 | 149k | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3277 | 149k | if(data) { |
3278 | 149k | CURL_TRC_M(data, "multi_run_dirty"); |
3279 | | |
3280 | 149k | if(!Curl_uint32_bset_contains(&multi->process, mid)) { |
3281 | 0 | if(Curl_uint32_bset_contains(&multi->pending, mid)) { |
3282 | | /* Something happened on the pending transfer, act on it. */ |
3283 | 0 | move_pending_to_connect(multi, data); |
3284 | 0 | } |
3285 | 0 | else { |
3286 | | /* We are no longer processing this transfer */ |
3287 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
3288 | 0 | continue; |
3289 | 0 | } |
3290 | 0 | } |
3291 | | |
3292 | 149k | (*pnum)++; |
3293 | | /* runsingle() clears the dirty mid */ |
3294 | 149k | mresult = multi_runsingle(multi, data, sigpipe_ctx); |
3295 | | |
3296 | 149k | if(CURLM_OK >= mresult) { |
3297 | | /* reassess event handling of data */ |
3298 | 149k | mresult = Curl_multi_ev_assess_xfer(multi, data); |
3299 | 149k | if(mresult) |
3300 | 6 | goto out; |
3301 | 149k | } |
3302 | 149k | } |
3303 | 0 | else { |
3304 | 0 | CURL_TRC_M(multi->admin, "multi_run_dirty, %u no longer found", mid); |
3305 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
3306 | 0 | } |
3307 | 149k | } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid)); |
3308 | 149k | } |
3309 | | |
3310 | 291k | out: |
3311 | 291k | return mresult; |
3312 | 291k | } |
3313 | | |
3314 | | static CURLMcode multi_socket(struct Curl_multi *multi, |
3315 | | bool checkall, |
3316 | | curl_socket_t s, |
3317 | | int ev_bitmask, |
3318 | | int *running_handles) |
3319 | 145k | { |
3320 | 145k | CURLMcode mresult = CURLM_OK; |
3321 | 145k | struct Curl_sigpipe_ctx pipe_ctx; |
3322 | 145k | struct curltime now; |
3323 | 145k | uint32_t run_xfers; |
3324 | | |
3325 | 145k | (void)ev_bitmask; |
3326 | 145k | sigpipe_init(&pipe_ctx); |
3327 | | |
3328 | 145k | if(checkall) { |
3329 | | /* *perform() deals with running_handles on its own */ |
3330 | 0 | mresult = multi_perform(multi, running_handles); |
3331 | |
|
3332 | 0 | if(mresult != CURLM_BAD_HANDLE) { |
3333 | | /* Reassess event status of all active transfers, but keep any error |
3334 | | from multi_perform() */ |
3335 | 0 | CURLMcode mresult2 = |
3336 | 0 | Curl_multi_ev_assess_xfer_bset(multi, &multi->process); |
3337 | 0 | if(!mresult) |
3338 | 0 | mresult = mresult2; |
3339 | 0 | } |
3340 | 0 | goto out; |
3341 | 0 | } |
3342 | | |
3343 | 145k | if(s != CURL_SOCKET_TIMEOUT) { |
3344 | | /* Mark all transfers of that socket as dirty */ |
3345 | 113k | Curl_multi_ev_dirty_xfers(multi, s); |
3346 | 113k | } |
3347 | 32.1k | else { |
3348 | | /* Asked to run due to time-out. Clear the 'last_expire_ts' variable to |
3349 | | force Curl_update_timer() to trigger a callback to the app again even |
3350 | | if the same timeout is still the one to run after this call. That |
3351 | | handles the case when the application asks libcurl to run the timeout |
3352 | | prematurely. */ |
3353 | 32.1k | multi->last_expire_offset_us = 0; |
3354 | | |
3355 | | /* Applications may set `socket_cb` *after* having added transfers |
3356 | | * first. *Then* kick off processing with a |
3357 | | * curl_multi_socket_action(TIMEOUT) afterwards. Make sure our |
3358 | | * admin handle registers its pollset with the callbacks present. */ |
3359 | 32.1k | mresult = multi_assess_wakeup(multi); |
3360 | 32.1k | if(mresult) |
3361 | 0 | goto out; |
3362 | 32.1k | } |
3363 | | |
3364 | 145k | curlx_pnow(&now); |
3365 | 145k | multi_mark_expired_as_dirty(multi, &now); |
3366 | 145k | mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers); |
3367 | 145k | if(mresult) |
3368 | 9 | goto out; |
3369 | | |
3370 | 145k | if(run_xfers) { |
3371 | | /* Running transfers takes time. With a new timestamp, we might catch |
3372 | | * other expires which are due now. Instead of telling the application |
3373 | | * to set a 0 timeout and call us again, we run them here. |
3374 | | * Do that only once or it might be unfair to transfers on other |
3375 | | * sockets. */ |
3376 | 145k | curlx_pnow(&now); |
3377 | 145k | multi_mark_expired_as_dirty(multi, &now); |
3378 | 145k | mresult = multi_run_dirty(multi, &pipe_ctx, &run_xfers); |
3379 | 145k | } |
3380 | | |
3381 | 145k | out: |
3382 | 145k | sigpipe_restore(&pipe_ctx); |
3383 | | |
3384 | 145k | if(multi_ischanged(multi, TRUE)) |
3385 | 683 | multi_schedule_pending(multi); |
3386 | | |
3387 | 145k | if(!mresult && CURL_MNTFY_HAS_ENTRIES(multi)) |
3388 | 0 | mresult = Curl_mntfy_dispatch_all(multi); |
3389 | | |
3390 | 145k | if(running_handles) { |
3391 | 145k | uint32_t running = multi_xfers_running(multi); |
3392 | 145k | *running_handles = (running < INT_MAX) ? (int)running : INT_MAX; |
3393 | 145k | } |
3394 | | |
3395 | 145k | if(CURLM_OK >= mresult) |
3396 | 145k | mresult = Curl_update_timer(multi); |
3397 | 145k | return mresult; |
3398 | 145k | } |
3399 | | |
3400 | | #undef curl_multi_setopt |
3401 | | CURLMcode curl_multi_setopt(CURLM *m, CURLMoption option, ...) |
3402 | 6.96k | { |
3403 | 6.96k | struct Curl_mapi_guard guard; |
3404 | 6.96k | CURLMcode mresult = CURLM_OK; |
3405 | | |
3406 | 6.96k | if(CURL_MAPI_ENTER(&guard, m, multi_setopt, &mresult)) { |
3407 | 6.91k | struct Curl_multi *multi = m; |
3408 | 6.91k | va_list param; |
3409 | 6.91k | unsigned long uarg; |
3410 | 6.91k | size_t szarg; |
3411 | | |
3412 | 6.91k | va_start(param, option); |
3413 | | |
3414 | 6.91k | switch(option) { |
3415 | 870 | case CURLMOPT_SOCKETFUNCTION: |
3416 | 870 | multi->socket_cb = va_arg(param, curl_socket_callback); |
3417 | 870 | break; |
3418 | 870 | case CURLMOPT_SOCKETDATA: |
3419 | 870 | multi->socket_userp = va_arg(param, void *); |
3420 | 870 | break; |
3421 | 0 | case CURLMOPT_PUSHFUNCTION: |
3422 | 0 | multi->push_cb = va_arg(param, curl_push_callback); |
3423 | 0 | break; |
3424 | 0 | case CURLMOPT_PUSHDATA: |
3425 | 0 | multi->push_userp = va_arg(param, void *); |
3426 | 0 | break; |
3427 | 0 | case CURLMOPT_PIPELINING: |
3428 | 0 | multi->multiplexing = va_arg(param, long) & CURLPIPE_MULTIPLEX ? 1 : 0; |
3429 | 0 | break; |
3430 | 870 | case CURLMOPT_TIMERFUNCTION: |
3431 | 870 | multi->timer_cb = va_arg(param, curl_multi_timer_callback); |
3432 | 870 | break; |
3433 | 870 | case CURLMOPT_TIMERDATA: |
3434 | 870 | multi->timer_userp = va_arg(param, void *); |
3435 | 870 | break; |
3436 | 1.71k | case CURLMOPT_MAXCONNECTS: |
3437 | 1.71k | uarg = va_arg(param, unsigned long); |
3438 | 1.71k | if(uarg <= UINT32_MAX) |
3439 | 1.71k | multi->maxconnects = (uint32_t)uarg; |
3440 | 1.71k | break; |
3441 | 0 | case CURLMOPT_MAX_HOST_CONNECTIONS: |
3442 | 0 | if(!curlx_sltouz(va_arg(param, long), &szarg)) |
3443 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
3444 | 0 | multi->max_host_connections = (szarg < UINT32_MAX) ? |
3445 | 0 | (uint32_t)szarg : UINT32_MAX; |
3446 | 0 | break; |
3447 | 0 | case CURLMOPT_MAX_TOTAL_CONNECTIONS: |
3448 | 0 | if(!curlx_sltouz(va_arg(param, long), &szarg)) |
3449 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
3450 | 0 | multi->max_total_connections = (szarg < UINT32_MAX) ? |
3451 | 0 | (uint32_t)szarg : UINT32_MAX; |
3452 | 0 | break; |
3453 | | /* options formerly used for pipelining */ |
3454 | 0 | case CURLMOPT_MAX_PIPELINE_LENGTH: |
3455 | 0 | break; |
3456 | 0 | case CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE: |
3457 | 0 | break; |
3458 | 0 | case CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE: |
3459 | 0 | break; |
3460 | 0 | case CURLMOPT_PIPELINING_SITE_BL: |
3461 | 0 | break; |
3462 | 0 | case CURLMOPT_PIPELINING_SERVER_BL: |
3463 | 0 | break; |
3464 | 0 | case CURLMOPT_MAX_CONCURRENT_STREAMS: { |
3465 | 0 | if(!curlx_sltouz(va_arg(param, long), &szarg) || |
3466 | 0 | !szarg || (szarg > (size_t)INT_MAX)) /* preserve previous cutoff */ |
3467 | 0 | multi->max_concurrent_streams = 100; |
3468 | 0 | else |
3469 | 0 | multi->max_concurrent_streams = (szarg < UINT32_MAX) ? |
3470 | 0 | (uint32_t)szarg : UINT32_MAX; |
3471 | 0 | break; |
3472 | 0 | } |
3473 | 0 | case CURLMOPT_NETWORK_CHANGED: { |
3474 | 0 | long val = va_arg(param, long); |
3475 | 0 | if(val & CURLMNWC_CLEAR_ALL) |
3476 | | /* In the beginning, all values available to set were 1 by mistake. We |
3477 | | converted this to mean "all", thus setting all the bits |
3478 | | automatically */ |
3479 | 0 | val = CURLMNWC_CLEAR_DNS | CURLMNWC_CLEAR_CONNS; |
3480 | 0 | if(val & CURLMNWC_CLEAR_DNS) { |
3481 | 0 | Curl_dnscache_clear(multi->admin); |
3482 | 0 | } |
3483 | 0 | if(val & CURLMNWC_CLEAR_CONNS) { |
3484 | 0 | Curl_cpool_nw_changed(&multi->cpool, multi->admin); |
3485 | 0 | } |
3486 | 0 | break; |
3487 | 0 | } |
3488 | 0 | case CURLMOPT_NOTIFYFUNCTION: |
3489 | 0 | multi->ntfy.ntfy_cb = va_arg(param, curl_notify_callback); |
3490 | 0 | break; |
3491 | 0 | case CURLMOPT_NOTIFYDATA: |
3492 | 0 | multi->ntfy.ntfy_cb_data = va_arg(param, void *); |
3493 | 0 | break; |
3494 | 0 | case CURLMOPT_RESOLVE_THREADS_MAX: |
3495 | 0 | #ifdef USE_RESOLV_THREADED |
3496 | 0 | uarg = va_arg(param, long); |
3497 | 0 | if((uarg <= 0) || (uarg > UINT32_MAX)) |
3498 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
3499 | 0 | else { |
3500 | 0 | CURLcode result = Curl_async_thrdd_multi_set_props( |
3501 | 0 | multi, 0, (uint32_t)uarg, 2000); |
3502 | 0 | switch(result) { |
3503 | 0 | case CURLE_OK: |
3504 | 0 | mresult = CURLM_OK; |
3505 | 0 | break; |
3506 | 0 | case CURLE_BAD_FUNCTION_ARGUMENT: |
3507 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
3508 | 0 | break; |
3509 | 0 | case CURLE_OUT_OF_MEMORY: |
3510 | 0 | mresult = CURLM_OUT_OF_MEMORY; |
3511 | 0 | break; |
3512 | 0 | default: |
3513 | 0 | mresult = CURLM_INTERNAL_ERROR; |
3514 | 0 | break; |
3515 | 0 | } |
3516 | 0 | } |
3517 | 0 | #endif |
3518 | 0 | break; |
3519 | 1.71k | case CURLMOPT_QUICK_EXIT: |
3520 | 1.71k | multi->quick_exit = va_arg(param, long) ? 1 : 0; |
3521 | 1.71k | break; |
3522 | 0 | default: |
3523 | 0 | mresult = CURLM_UNKNOWN_OPTION; |
3524 | 0 | break; |
3525 | 6.91k | } |
3526 | 6.91k | va_end(param); |
3527 | 6.91k | } |
3528 | 6.96k | CURL_MAPI_LEAVE(&guard); |
3529 | 6.96k | return mresult; |
3530 | 6.96k | } |
3531 | | |
3532 | | /* we define curl_multi_socket() in the public multi.h header */ |
3533 | | #undef curl_multi_socket |
3534 | | |
3535 | | CURLMcode curl_multi_socket(CURLM *m, curl_socket_t s, int *running_handles) |
3536 | 0 | { |
3537 | 0 | struct Curl_mapi_guard guard; |
3538 | 0 | CURLMcode mresult; |
3539 | |
|
3540 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_socket, &mresult)) { |
3541 | 0 | mresult = multi_socket(m, FALSE, s, 0, running_handles); |
3542 | 0 | } |
3543 | 0 | CURL_MAPI_LEAVE(&guard); |
3544 | 0 | return mresult; |
3545 | 0 | } |
3546 | | |
3547 | | CURLMcode curl_multi_socket_action(CURLM *m, curl_socket_t s, |
3548 | | int ev_bitmask, int *running_handles) |
3549 | 145k | { |
3550 | 145k | struct Curl_mapi_guard guard; |
3551 | 145k | CURLMcode mresult; |
3552 | | |
3553 | 145k | if(CURL_MAPI_ENTER(&guard, m, multi_socket_action, &mresult)) { |
3554 | 145k | mresult = multi_socket(m, FALSE, s, ev_bitmask, running_handles); |
3555 | 145k | } |
3556 | 145k | CURL_MAPI_LEAVE(&guard); |
3557 | 145k | return mresult; |
3558 | 145k | } |
3559 | | |
3560 | | CURLMcode curl_multi_socket_all(CURLM *m, int *running_handles) |
3561 | 0 | { |
3562 | 0 | struct Curl_mapi_guard guard; |
3563 | 0 | CURLMcode mresult; |
3564 | |
|
3565 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_socket_all, &mresult)) { |
3566 | 0 | mresult = multi_socket(m, TRUE, CURL_SOCKET_BAD, 0, running_handles); |
3567 | 0 | } |
3568 | 0 | CURL_MAPI_LEAVE(&guard); |
3569 | 0 | return mresult; |
3570 | 0 | } |
3571 | | |
3572 | | static bool multi_has_dirties(struct Curl_multi *multi) |
3573 | 320k | { |
3574 | 320k | uint32_t mid; |
3575 | 320k | if(Curl_uint32_bset_first(&multi->dirty, &mid)) { |
3576 | 33.4k | do { |
3577 | 33.4k | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3578 | 33.4k | if(data) { |
3579 | 33.4k | if(Curl_uint32_bset_contains(&multi->process, mid)) |
3580 | 33.4k | return TRUE; |
3581 | | /* We are no longer processing this transfer */ |
3582 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
3583 | 0 | } |
3584 | 0 | else { |
3585 | 0 | CURL_TRC_M(multi->admin, "dirty transfer %u no longer found", mid); |
3586 | 0 | Curl_uint32_bset_remove(&multi->dirty, mid); |
3587 | 0 | } |
3588 | 33.4k | } while(Curl_uint32_bset_next(&multi->dirty, mid, &mid)); |
3589 | 33.4k | } |
3590 | 286k | return FALSE; |
3591 | 320k | } |
3592 | | |
3593 | | static void multi_timeout(struct Curl_multi *multi, |
3594 | | timediff_t *pexire_offset_us, |
3595 | | int *timeout_ms) |
3596 | 320k | { |
3597 | 320k | if(multi->dead) { |
3598 | 0 | if(pexire_offset_us) |
3599 | 0 | *pexire_offset_us = 0; |
3600 | 0 | *timeout_ms = 0; |
3601 | 0 | return; |
3602 | 0 | } |
3603 | | |
3604 | 320k | if(multi_has_dirties(multi)) { |
3605 | 33.4k | if(pexire_offset_us) { |
3606 | 633 | struct curltime now; |
3607 | 633 | curlx_pnow(&now); |
3608 | 633 | *pexire_offset_us = Curl_timeouts_offset_us(&multi->timeouts, &now); |
3609 | 633 | } |
3610 | 33.4k | *timeout_ms = 0; |
3611 | 33.4k | return; |
3612 | 33.4k | } |
3613 | 286k | else { |
3614 | 286k | struct curltime now; |
3615 | 286k | uint32_t mid; |
3616 | | |
3617 | 286k | curlx_pnow(&now); |
3618 | 286k | *timeout_ms = Curl_timeouts_next_ms(&multi->timeouts, &now, |
3619 | 286k | pexire_offset_us, &mid); |
3620 | 286k | #ifdef CURLVERBOSE |
3621 | 286k | if(mid != UINT32_MAX) { |
3622 | 285k | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3623 | 285k | if(data && CURL_TRC_TIMER_is_verbose(data) && |
3624 | 24.8k | (data->state.timeouts.first < EXPIRE_LAST)) { |
3625 | 24.8k | CURL_TRC_TIMER(data, data->state.timeouts.first, |
3626 | 24.8k | "gives multi timeout in %dms", *timeout_ms); |
3627 | 24.8k | } |
3628 | 285k | } |
3629 | 286k | #endif |
3630 | 286k | } |
3631 | 320k | } |
3632 | | |
3633 | | CURLMcode curl_multi_timeout(CURLM *m, |
3634 | | long *timeout_ms) |
3635 | 40.0k | { |
3636 | 40.0k | struct Curl_mapi_guard guard; |
3637 | 40.0k | CURLMcode mresult; |
3638 | | |
3639 | 40.0k | if(CURL_MAPI_ENTER(&guard, m, multi_timeout, &mresult)) { |
3640 | 40.0k | int itimeout_ms; |
3641 | | |
3642 | 40.0k | multi_timeout(m, NULL, &itimeout_ms); |
3643 | 40.0k | *timeout_ms = (long)itimeout_ms; |
3644 | 40.0k | mresult = CURLM_OK; |
3645 | 40.0k | } |
3646 | 40.0k | CURL_MAPI_LEAVE(&guard); |
3647 | 40.0k | return mresult; |
3648 | 40.0k | } |
3649 | | |
3650 | | /* |
3651 | | * Tell the application it should update its timers, if it subscribes to the |
3652 | | * update timer callback. |
3653 | | */ |
3654 | | CURLMcode Curl_update_timer(struct Curl_multi *multi) |
3655 | 431k | { |
3656 | 431k | timediff_t timeouts_offset_us = 0; |
3657 | 431k | int timeout_ms; |
3658 | 431k | int rc; |
3659 | 431k | bool set_value = FALSE; |
3660 | | |
3661 | 431k | if(!multi->timer_cb || multi->dead) |
3662 | 283k | return CURLM_OK; |
3663 | 147k | multi_timeout(multi, &timeouts_offset_us, &timeout_ms); |
3664 | | |
3665 | 147k | if(timeout_ms < 0 && !multi->last_timeout_set) { |
3666 | | /* nothing to do */ |
3667 | 1.07k | } |
3668 | 146k | else if(timeout_ms < 0) { |
3669 | | /* there is no timeout now but there was one previously */ |
3670 | 581 | CURL_TRC_M(multi->admin, "[TIMER] clear"); |
3671 | 581 | timeout_ms = -1; /* normalize */ |
3672 | 581 | set_value = TRUE; |
3673 | 581 | } |
3674 | 145k | else if(!multi->last_timeout_set) { |
3675 | 581 | CURL_TRC_M(multi->admin, "[TIMER] set %dms, none before", timeout_ms); |
3676 | 581 | set_value = TRUE; |
3677 | 581 | } |
3678 | 145k | else if(multi->last_expire_offset_us != timeouts_offset_us) { |
3679 | | /* We had a timeout before and have one now, the absolute timestamp |
3680 | | * differs. The relative timeout_ms may be the same, but the starting |
3681 | | * point differs. Let the application restart its timer. */ |
3682 | 32.2k | CURL_TRC_M(multi->admin, "[TIMER] set %dms, replace previous", |
3683 | 32.2k | timeout_ms); |
3684 | 32.2k | set_value = TRUE; |
3685 | 32.2k | } |
3686 | 112k | else { |
3687 | | /* We have same expire time as previously. Our relative 'timeout_ms' |
3688 | | * may be different now, but the application has the timer running |
3689 | | * and we do not to tell it to start this again. */ |
3690 | 112k | } |
3691 | | |
3692 | 147k | if(set_value) { |
3693 | 33.4k | struct Curl_mapi_guard guard; |
3694 | | |
3695 | 33.4k | multi->last_expire_offset_us = timeouts_offset_us; |
3696 | 33.4k | multi->last_timeout_set = timeout_ms >= 0; |
3697 | 33.4k | CURL_CBAPI_MULTI_START(&guard, multi, multi_timer_cb); |
3698 | 33.4k | rc = multi->timer_cb(multi, timeout_ms, multi->timer_userp); |
3699 | 33.4k | CURL_CBAPI_MULTI_END(&guard); |
3700 | 33.4k | if(rc == -1) { |
3701 | 0 | multi->dead = TRUE; |
3702 | 0 | return CURLM_ABORTED_BY_CALLBACK; |
3703 | 0 | } |
3704 | 33.4k | } |
3705 | 147k | return CURLM_OK; |
3706 | 147k | } |
3707 | | |
3708 | | #ifdef DEBUGBUILD |
3709 | | static bool multi_timeouts_check(struct Curl_easy *data) |
3710 | 266k | { |
3711 | 266k | struct expire_timers *timeouts = &data->state.timeouts; |
3712 | 266k | uint8_t id; |
3713 | 266k | int i = 0; |
3714 | 771k | for(id = timeouts->first; id < EXPIRE_LAST; id = timeouts->next[id]) { |
3715 | 504k | if(++i >= EXPIRE_LAST) { |
3716 | 0 | failf(data, "expire timeouts looped: %d iterations and no end", i); |
3717 | 0 | return FALSE; |
3718 | 0 | } |
3719 | 504k | if(id == timeouts->next[id]) { |
3720 | 0 | failf(data, "expire timeouts wrong: %d points to itself", (int)id); |
3721 | 0 | return FALSE; |
3722 | 0 | } |
3723 | 504k | if((timeouts->next[id] < EXPIRE_LAST) && |
3724 | 265k | (timeouts->offset_us[id] > timeouts->offset_us[timeouts->next[id]])) { |
3725 | 0 | failf(data, "expire timeouts not sorted: %d happens after %d but " |
3726 | 0 | "is listed before", (int)id, (int)timeouts->next[id]); |
3727 | 0 | return FALSE; |
3728 | 0 | } |
3729 | 504k | } |
3730 | 266k | return TRUE; |
3731 | 266k | } |
3732 | | #endif |
3733 | | |
3734 | | /* |
3735 | | * Remove a given timestamp from the list of timeouts. |
3736 | | */ |
3737 | | static void multi_clear_timeout(struct Curl_easy *data, expire_id eid) |
3738 | 150k | { |
3739 | 150k | struct expire_timers *timeouts = &data->state.timeouts; |
3740 | 150k | uint8_t orig_first = timeouts->first; |
3741 | 150k | uint8_t *anchor = &timeouts->first; |
3742 | 150k | uint8_t id = (uint8_t)eid; |
3743 | | |
3744 | 150k | if((unsigned)eid >= EXPIRE_LAST) { |
3745 | 0 | DEBUGASSERT(0); |
3746 | 0 | return; |
3747 | 0 | } |
3748 | | |
3749 | 302k | while(*anchor < EXPIRE_LAST) { |
3750 | 211k | if(*anchor == id) { |
3751 | 60.5k | *anchor = timeouts->next[id]; |
3752 | 60.5k | break; |
3753 | 60.5k | } |
3754 | 151k | anchor = &timeouts->next[*anchor]; |
3755 | 151k | } |
3756 | 150k | DEBUGASSERT(multi_timeouts_check(data)); |
3757 | 150k | if(Curl_timeouts_has(data)) { |
3758 | 123k | struct Curl_multi *multi = data->multi; |
3759 | | |
3760 | 123k | if(!multi) { |
3761 | 0 | DEBUGASSERT(0); |
3762 | 0 | return; |
3763 | 0 | } |
3764 | 123k | if((timeouts->first >= EXPIRE_LAST) || /* no more timeouts */ |
3765 | 123k | (timeouts->first != orig_first)) { /* active timeout changed */ |
3766 | 42.7k | Curl_timeouts_remove(&multi->timeouts, data); |
3767 | 42.7k | } |
3768 | 123k | if((timeouts->first < EXPIRE_LAST) && !Curl_timeouts_has(data)) { |
3769 | 42.7k | Curl_timeouts_add(&multi->timeouts, data, |
3770 | 42.7k | timeouts->offset_us[timeouts->first]); |
3771 | 42.7k | } |
3772 | 123k | } |
3773 | 150k | } |
3774 | | |
3775 | | /* |
3776 | | * Add a timestamp to the list of timeouts. Keep the list sorted so that head |
3777 | | * of list is always the timeout nearest in time. |
3778 | | */ |
3779 | | static CURLMcode multi_set_timeout(struct Curl_easy *data, |
3780 | | const struct curltime *stamp, |
3781 | | expire_id eid) |
3782 | 115k | { |
3783 | 115k | struct expire_timers *timeouts = &data->state.timeouts; |
3784 | 115k | uint8_t *anchor = &timeouts->first; |
3785 | 115k | uint8_t id = (uint8_t)eid; |
3786 | | |
3787 | 115k | if((unsigned)eid >= EXPIRE_LAST) { |
3788 | 0 | DEBUGASSERT(0); |
3789 | 0 | return CURLM_BAD_FUNCTION_ARGUMENT; |
3790 | 0 | } |
3791 | | /* remove from list, store time and re-insert */ |
3792 | 115k | multi_clear_timeout(data, eid); |
3793 | 115k | timeouts->offset_us[id] = |
3794 | 115k | Curl_timeouts_offset_us(&data->multi->timeouts, stamp); |
3795 | | |
3796 | 220k | while(*anchor < EXPIRE_LAST) { |
3797 | 123k | if(timeouts->offset_us[*anchor] > timeouts->offset_us[id]) |
3798 | 18.5k | break; |
3799 | 105k | anchor = &timeouts->next[*anchor]; |
3800 | 105k | } |
3801 | 115k | timeouts->next[eid] = *anchor; |
3802 | 115k | *anchor = id; |
3803 | 115k | DEBUGASSERT(multi_timeouts_check(data)); |
3804 | 115k | CURL_TRC_TIMER(data, eid, "set for %" FMT_TIMEDIFF_T "us", |
3805 | 115k | curlx_ptimediff_us(stamp, Curl_pgrs_now(data))); |
3806 | 115k | return CURLM_OK; |
3807 | 115k | } |
3808 | | |
3809 | | /* |
3810 | | * given a number of milliseconds from now to use to set the 'act before |
3811 | | * this'-time for the transfer, to be extracted by curl_multi_timeout() |
3812 | | * |
3813 | | * The timeout will be added to a queue of timeouts if it defines a moment in |
3814 | | * time that is later than the current head of queue. |
3815 | | * |
3816 | | * Expire replaces a former timeout using the same id if already set. |
3817 | | */ |
3818 | | void Curl_expire_set(struct Curl_easy *data, |
3819 | | expire_id eid, timediff_t ms, |
3820 | | const struct curltime *pnow) |
3821 | 115k | { |
3822 | 115k | struct Curl_multi *multi = data->multi; |
3823 | 115k | struct expire_timers *timeouts = &data->state.timeouts; |
3824 | 115k | uint8_t prev_id = timeouts->first; |
3825 | 115k | struct curltime set; |
3826 | | |
3827 | | /* this is only interesting while there is still an associated multi struct |
3828 | | remaining! */ |
3829 | 115k | if(!multi) |
3830 | 0 | return; |
3831 | 115k | DEBUGASSERT(eid < EXPIRE_LAST); |
3832 | 115k | if(ms > INT_MAX) |
3833 | | /* Cap ridiculous timeouts, 31-bit ms is still 3.5 weeks. When the time |
3834 | | goes to the user, it must fit in this size. */ |
3835 | 0 | ms = INT_MAX; |
3836 | | |
3837 | 115k | set = *pnow; |
3838 | 115k | set.tv_sec += (time_t)(ms / 1000); /* may be a 64 to 32-bit conversion */ |
3839 | 115k | set.tv_usec += (int)(ms % 1000) * 1000; |
3840 | 115k | if(set.tv_usec >= 1000000) { |
3841 | 18.9k | set.tv_sec++; |
3842 | 18.9k | set.tv_usec -= 1000000; |
3843 | 18.9k | } |
3844 | | |
3845 | | /* Add the timeout, will replace any previous value for this timer. */ |
3846 | 115k | multi_set_timeout(data, &set, eid); |
3847 | 115k | DEBUGASSERT(timeouts->first < EXPIRE_LAST); |
3848 | | |
3849 | 115k | if(Curl_timeouts_has(data)) { |
3850 | | /* data has already a timeout registered. If the first timer |
3851 | | * was NOT the one we just set AND is still the first one, |
3852 | | * nothing changed from the timeouts point of view. The |
3853 | | * set timer triggers after the one already registered. Leave. */ |
3854 | 88.2k | if((prev_id != eid) && (prev_id == timeouts->first)) |
3855 | 45.2k | return; |
3856 | | |
3857 | | /* Since this is an updated time, we must remove data from |
3858 | | * timeouts and then add it again. */ |
3859 | 43.0k | Curl_timeouts_remove(&multi->timeouts, data); |
3860 | 43.0k | } |
3861 | | |
3862 | | /* Insert the new timer expiry since it is our local minimum. */ |
3863 | 70.3k | Curl_timeouts_add(&multi->timeouts, data, |
3864 | 70.3k | timeouts->offset_us[timeouts->first]); |
3865 | 70.3k | } |
3866 | | |
3867 | | /* Set the timer expiry if not an earlier one is already set */ |
3868 | | void Curl_expire_latest(struct Curl_easy *data, |
3869 | | expire_id eid, timediff_t ms, |
3870 | | const struct curltime *pnow) |
3871 | 0 | { |
3872 | 0 | struct expire_timers *timeouts = &data->state.timeouts; |
3873 | 0 | uint8_t id, nid = (uint8_t)eid; |
3874 | |
|
3875 | 0 | if(!data->multi) |
3876 | 0 | return; |
3877 | | |
3878 | 0 | for(id = timeouts->first; id < EXPIRE_LAST; id = timeouts->next[id]) { |
3879 | 0 | if(id == nid) { /* expire timer is set */ |
3880 | 0 | struct curltime newset = *pnow; |
3881 | 0 | timediff_t now_us = |
3882 | 0 | Curl_timeouts_offset_us(&data->multi->timeouts, &newset); |
3883 | |
|
3884 | 0 | if(now_us >= timeouts->offset_us[nid]) /* already expired, set again */ |
3885 | 0 | break; |
3886 | 0 | if(((timeouts->offset_us[nid] - now_us) / 1000) < ms) |
3887 | 0 | return; /* existing timeout is earlier */ |
3888 | 0 | break; |
3889 | 0 | } |
3890 | 0 | } |
3891 | 0 | Curl_expire_set(data, eid, ms, pnow); |
3892 | 0 | } |
3893 | | |
3894 | | void Curl_expire(struct Curl_easy *data, |
3895 | | timediff_t milli, expire_id eid) |
3896 | 0 | { |
3897 | 0 | Curl_expire_set(data, eid, milli, Curl_pgrs_now(data)); |
3898 | 0 | } |
3899 | | |
3900 | | /* |
3901 | | * Removes the expire timer. Marks it as done. |
3902 | | */ |
3903 | | void Curl_expire_clear(struct Curl_easy *data, expire_id eid) |
3904 | 35.1k | { |
3905 | | /* remove the timer, if there */ |
3906 | 35.1k | multi_clear_timeout(data, eid); |
3907 | 35.1k | CURL_TRC_TIMER(data, eid, "cleared"); |
3908 | 35.1k | } |
3909 | | |
3910 | | /* |
3911 | | * Clear ALL timeout values for this handle. |
3912 | | */ |
3913 | | void Curl_expire_clear_all(struct Curl_easy *data) |
3914 | 112k | { |
3915 | 112k | struct Curl_multi *multi = data->multi; |
3916 | | |
3917 | | /* this is only interesting while there is still an associated multi struct |
3918 | | remaining! */ |
3919 | 112k | if(!multi) |
3920 | 57.4k | return; |
3921 | | |
3922 | 54.7k | if(Curl_timeouts_remove(&multi->timeouts, data)) { |
3923 | | /* Since this is an cleared time, we must remove the previous entry from |
3924 | | the splay tree */ |
3925 | 27.3k | multi_timeouts_init(data); |
3926 | | |
3927 | 27.3k | if(data->id >= 0) |
3928 | 27.3k | CURL_TRC_M(data, "[TIMEOUT] all cleared"); |
3929 | 27.3k | } |
3930 | 54.7k | } |
3931 | | |
3932 | | CURLMcode curl_multi_assign(CURLM *m, curl_socket_t sockfd, |
3933 | | void *sockp) |
3934 | 2.74k | { |
3935 | 2.74k | struct Curl_mapi_guard guard; |
3936 | 2.74k | CURLMcode mresult; |
3937 | | |
3938 | 2.74k | if(CURL_MAPI_ENTER(&guard, m, multi_assign, &mresult)) { |
3939 | 2.74k | mresult = Curl_multi_ev_assign(m, sockfd, sockp); |
3940 | 2.74k | } |
3941 | 2.74k | CURL_MAPI_LEAVE(&guard); |
3942 | 2.74k | return mresult; |
3943 | 2.74k | } |
3944 | | |
3945 | | static void move_pending_to_connect(struct Curl_multi *multi, |
3946 | | struct Curl_easy *data) |
3947 | 0 | { |
3948 | 0 | DEBUGASSERT(data->mstate == MSTATE_PENDING); |
3949 | | |
3950 | | /* Remove this node from the pending set, add into process set */ |
3951 | 0 | Curl_uint32_bset_remove(&multi->pending, data->mid); |
3952 | 0 | Curl_uint32_bset_add(&multi->process, data->mid); |
3953 | |
|
3954 | 0 | multistate(data, MSTATE_CONNECT); |
3955 | 0 | Curl_multi_mark_dirty(data); /* make it run */ |
3956 | 0 | } |
3957 | | |
3958 | | /* multi_schedule_pending() moves a handle from PENDING back into the process |
3959 | | list and change state to CONNECT. |
3960 | | |
3961 | | We do not move all transfers because that can be a significant amount. |
3962 | | Since this is tried every now and then doing too many too often becomes a |
3963 | | performance problem. |
3964 | | |
3965 | | When there is a change for connection limits like max host connections etc, |
3966 | | this likely only allows one new transfer. When there is a pipewait change, |
3967 | | it can potentially allow hundreds of new transfers. |
3968 | | |
3969 | | We could consider an improvement where we store the queue reason and allow |
3970 | | more pipewait rechecks than others. */ |
3971 | | static void multi_schedule_pending(struct Curl_multi *multi) |
3972 | 217k | { |
3973 | 217k | uint32_t mid = multi->last_pending_mid; |
3974 | | |
3975 | 217k | if(mid) { |
3976 | 0 | while(Curl_uint32_bset_next(&multi->pending, mid, &mid)) { |
3977 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3978 | 0 | if(data) { |
3979 | 0 | move_pending_to_connect(multi, data); |
3980 | 0 | multi->last_pending_mid = mid; |
3981 | 0 | return; |
3982 | 0 | } |
3983 | | /* transfer no longer known, should not happen */ |
3984 | 0 | Curl_uint32_bset_remove(&multi->pending, mid); |
3985 | 0 | DEBUGASSERT(0); |
3986 | 0 | } |
3987 | | /* found no pending transfers with `mid` larger than `last_pending_mid`. |
3988 | | * Start at the beginning of the pending set again. */ |
3989 | 0 | multi->last_pending_mid = 0; |
3990 | 0 | } |
3991 | | |
3992 | 217k | if(Curl_uint32_bset_first(&multi->pending, &mid)) { |
3993 | 0 | do { |
3994 | 0 | struct Curl_easy *data = Curl_multi_get_easy(multi, mid); |
3995 | 0 | if(data) { |
3996 | 0 | move_pending_to_connect(multi, data); |
3997 | 0 | multi->last_pending_mid = mid; |
3998 | 0 | return; |
3999 | 0 | } |
4000 | | /* transfer no longer known, should not happen */ |
4001 | 0 | Curl_uint32_bset_remove(&multi->pending, mid); |
4002 | 0 | DEBUGASSERT(0); |
4003 | 0 | } while(Curl_uint32_bset_next(&multi->pending, mid, &mid)); |
4004 | 0 | } |
4005 | 217k | } |
4006 | | |
4007 | | uint32_t Curl_multi_max_concurrent_streams(struct Curl_multi *multi) |
4008 | 37.0k | { |
4009 | 37.0k | DEBUGASSERT(multi); |
4010 | 37.0k | return multi->max_concurrent_streams; |
4011 | 37.0k | } |
4012 | | |
4013 | | CURL **curl_multi_get_handles(CURLM *m) |
4014 | 25.7k | { |
4015 | 25.7k | struct Curl_mapi_guard guard; |
4016 | 25.7k | CURL **a = NULL; |
4017 | | |
4018 | 25.7k | if(CURL_MAPI_ENTER(&guard, m, multi_get_handles, NULL)) { |
4019 | 25.7k | struct Curl_multi *multi = m; |
4020 | 25.7k | void *entry; |
4021 | 25.7k | size_t count = Curl_uint32_tbl_count(&multi->xfers); |
4022 | | |
4023 | 25.7k | a = curlx_malloc(sizeof(struct Curl_easy *) * (count + 1)); |
4024 | 25.7k | if(a) { |
4025 | 25.7k | unsigned int i = 0; |
4026 | 25.7k | uint32_t mid; |
4027 | | |
4028 | 25.7k | if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) { |
4029 | 51.5k | do { |
4030 | 51.5k | struct Curl_easy *data = entry; |
4031 | 51.5k | DEBUGASSERT(i < count); |
4032 | 51.5k | if(!data->state.internal) |
4033 | 25.7k | a[i++] = data; |
4034 | 51.5k | } while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry)); |
4035 | 25.7k | } |
4036 | 25.7k | a[i] = NULL; /* last entry is a NULL */ |
4037 | 25.7k | } |
4038 | 25.7k | } |
4039 | 25.7k | CURL_MAPI_LEAVE(&guard); |
4040 | 25.7k | return a; |
4041 | 25.7k | } |
4042 | | |
4043 | | CURLMcode curl_multi_get_offt(CURLM *m, |
4044 | | CURLMinfo_offt info, |
4045 | | curl_off_t *pvalue) |
4046 | 0 | { |
4047 | 0 | struct Curl_mapi_guard guard; |
4048 | 0 | CURLMcode mresult = CURLM_OK; |
4049 | |
|
4050 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_get_offt, &mresult)) { |
4051 | 0 | struct Curl_multi *multi = m; |
4052 | 0 | uint32_t n; |
4053 | |
|
4054 | 0 | if(!pvalue) { |
4055 | 0 | mresult = CURLM_BAD_FUNCTION_ARGUMENT; |
4056 | 0 | goto out; |
4057 | 0 | } |
4058 | | |
4059 | 0 | switch(info) { |
4060 | 0 | case CURLMINFO_XFERS_CURRENT: |
4061 | 0 | n = Curl_uint32_tbl_count(&multi->xfers); |
4062 | 0 | if(n && multi->admin) |
4063 | 0 | --n; |
4064 | 0 | *pvalue = (curl_off_t)n; |
4065 | 0 | break; |
4066 | 0 | case CURLMINFO_XFERS_RUNNING: |
4067 | 0 | n = Curl_uint32_bset_count(&multi->process); |
4068 | 0 | if(n && Curl_uint32_bset_contains(&multi->process, multi->admin->mid)) |
4069 | 0 | --n; |
4070 | 0 | *pvalue = (curl_off_t)n; |
4071 | 0 | break; |
4072 | 0 | case CURLMINFO_XFERS_PENDING: |
4073 | 0 | *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->pending); |
4074 | 0 | break; |
4075 | 0 | case CURLMINFO_XFERS_DONE: |
4076 | 0 | *pvalue = (curl_off_t)Curl_uint32_bset_count(&multi->msgsent); |
4077 | 0 | break; |
4078 | 0 | case CURLMINFO_XFERS_ADDED: |
4079 | 0 | *pvalue = multi->xfers_total_ever; |
4080 | 0 | break; |
4081 | 0 | default: |
4082 | 0 | *pvalue = -1; |
4083 | 0 | mresult = CURLM_UNKNOWN_OPTION; |
4084 | 0 | break; |
4085 | 0 | } |
4086 | 0 | } |
4087 | 0 | out: |
4088 | 0 | CURL_MAPI_LEAVE(&guard); |
4089 | 0 | return mresult; |
4090 | 0 | } |
4091 | | |
4092 | | static struct Curl_fixed_buf *fixed_buf_create(size_t len) |
4093 | 23.5k | { |
4094 | 23.5k | struct Curl_fixed_buf *fbuf; |
4095 | 23.5k | if((SIZE_MAX - sizeof(*fbuf)) < len) |
4096 | 0 | return NULL; /* too large */ |
4097 | 23.5k | fbuf = curlx_malloc(len + sizeof(*fbuf)); |
4098 | 23.5k | if(fbuf) |
4099 | 23.5k | fbuf->len = len; |
4100 | 23.5k | return fbuf; |
4101 | 23.5k | } |
4102 | | |
4103 | | CURLcode Curl_multi_xfer_buf_borrow(struct Curl_easy *data, |
4104 | | char **pbuf, size_t *pbuflen) |
4105 | 127k | { |
4106 | 127k | DEBUGASSERT(data); |
4107 | 127k | DEBUGASSERT(data->multi); |
4108 | 127k | *pbuf = NULL; |
4109 | 127k | *pbuflen = 0; |
4110 | 127k | if(!data->multi) { |
4111 | 0 | failf(data, "transfer has no multi handle"); |
4112 | 0 | return CURLE_FAILED_INIT; |
4113 | 0 | } |
4114 | 127k | if(!data->set.buffer_size) { |
4115 | 0 | failf(data, "transfer buffer size is 0"); |
4116 | 0 | return CURLE_FAILED_INIT; |
4117 | 0 | } |
4118 | 127k | if(data->multi->xfer_buf_borrowed) { |
4119 | 0 | failf(data, "attempt to borrow xfer_buf when already borrowed"); |
4120 | 0 | return CURLE_AGAIN; |
4121 | 0 | } |
4122 | | |
4123 | 127k | if(data->multi->xfer_buf && |
4124 | 103k | data->set.buffer_size > data->multi->xfer_buf->len) { |
4125 | | /* not large enough, get a new one */ |
4126 | 0 | curlx_safefree(data->multi->xfer_buf); |
4127 | 0 | } |
4128 | | |
4129 | 127k | if(!data->multi->xfer_buf) { |
4130 | 23.5k | data->multi->xfer_buf = |
4131 | 23.5k | fixed_buf_create(curlx_uitouz(data->set.buffer_size)); |
4132 | 23.5k | if(!data->multi->xfer_buf) { |
4133 | 12 | failf(data, "could not allocate xfer_buf of %u bytes", |
4134 | 12 | data->set.buffer_size); |
4135 | 12 | return CURLE_OUT_OF_MEMORY; |
4136 | 12 | } |
4137 | 23.5k | } |
4138 | | |
4139 | 127k | data->multi->xfer_buf_borrowed = TRUE; |
4140 | 127k | *pbuf = data->multi->xfer_buf->data; |
4141 | 127k | *pbuflen = data->multi->xfer_buf->len; |
4142 | 127k | return CURLE_OK; |
4143 | 127k | } |
4144 | | |
4145 | | void Curl_multi_xfer_buf_release(struct Curl_easy *data, char *buf) |
4146 | 127k | { |
4147 | 127k | (void)buf; |
4148 | 127k | DEBUGASSERT(data); |
4149 | 127k | DEBUGASSERT(data->multi); |
4150 | 127k | DEBUGASSERT(!buf || (data->multi->xfer_buf && |
4151 | 127k | data->multi->xfer_buf->data == buf)); |
4152 | 127k | data->multi->xfer_buf_borrowed = FALSE; |
4153 | 127k | } |
4154 | | |
4155 | | CURLcode Curl_multi_xfer_ulbuf_borrow(struct Curl_easy *data, |
4156 | | char **pbuf, size_t *pbuflen) |
4157 | 0 | { |
4158 | 0 | DEBUGASSERT(data); |
4159 | 0 | DEBUGASSERT(data->multi); |
4160 | 0 | *pbuf = NULL; |
4161 | 0 | *pbuflen = 0; |
4162 | 0 | if(!data->multi) { |
4163 | 0 | failf(data, "transfer has no multi handle"); |
4164 | 0 | return CURLE_FAILED_INIT; |
4165 | 0 | } |
4166 | 0 | if(data->multi->xfer_ulbuf_borrowed) { |
4167 | 0 | failf(data, "attempt to borrow xfer_ulbuf when already borrowed"); |
4168 | 0 | return CURLE_AGAIN; |
4169 | 0 | } |
4170 | | |
4171 | 0 | if(data->multi->xfer_ulbuf && |
4172 | 0 | data->set.upload_buffer_size > data->multi->xfer_ulbuf->len) { |
4173 | | /* not large enough, get a new one */ |
4174 | 0 | curlx_safefree(data->multi->xfer_ulbuf); |
4175 | 0 | } |
4176 | |
|
4177 | 0 | if(!data->multi->xfer_ulbuf) { |
4178 | 0 | data->multi->xfer_ulbuf = |
4179 | 0 | fixed_buf_create(curlx_uitouz(data->set.upload_buffer_size)); |
4180 | 0 | if(!data->multi->xfer_ulbuf) { |
4181 | 0 | failf(data, "could not allocate xfer_ulbuf of %u bytes", |
4182 | 0 | data->set.upload_buffer_size); |
4183 | 0 | return CURLE_OUT_OF_MEMORY; |
4184 | 0 | } |
4185 | 0 | } |
4186 | | |
4187 | 0 | data->multi->xfer_ulbuf_borrowed = TRUE; |
4188 | 0 | *pbuf = data->multi->xfer_ulbuf->data; |
4189 | 0 | *pbuflen = data->multi->xfer_ulbuf->len; |
4190 | 0 | return CURLE_OK; |
4191 | 0 | } |
4192 | | |
4193 | | void Curl_multi_xfer_ulbuf_release(struct Curl_easy *data, char *buf) |
4194 | 0 | { |
4195 | 0 | (void)buf; |
4196 | 0 | DEBUGASSERT(data); |
4197 | 0 | DEBUGASSERT(data->multi); |
4198 | 0 | DEBUGASSERT(!buf || (data->multi->xfer_ulbuf && |
4199 | 0 | data->multi->xfer_ulbuf->data == buf)); |
4200 | 0 | data->multi->xfer_ulbuf_borrowed = FALSE; |
4201 | 0 | } |
4202 | | |
4203 | | CURLcode Curl_multi_xfer_sockbuf_borrow(struct Curl_easy *data, |
4204 | | size_t blen, char **pbuf) |
4205 | 0 | { |
4206 | 0 | DEBUGASSERT(data); |
4207 | 0 | *pbuf = NULL; |
4208 | 0 | if(!data->multi) { |
4209 | | /* When a SHARE gets destroyed and has a connection pool, we get |
4210 | | * call with share->admin which does not have a multi handle. */ |
4211 | 0 | *pbuf = curlx_malloc(blen); |
4212 | 0 | return *pbuf ? CURLE_OK : CURLE_OUT_OF_MEMORY; |
4213 | 0 | } |
4214 | 0 | if(data->multi->xfer_sockbuf_borrowed) { |
4215 | 0 | failf(data, "attempt to borrow xfer_sockbuf when already borrowed"); |
4216 | 0 | return CURLE_AGAIN; |
4217 | 0 | } |
4218 | | |
4219 | 0 | if(data->multi->xfer_sockbuf && blen > data->multi->xfer_sockbuf->len) { |
4220 | | /* not large enough, get a new one */ |
4221 | 0 | curlx_safefree(data->multi->xfer_sockbuf); |
4222 | 0 | } |
4223 | |
|
4224 | 0 | if(!data->multi->xfer_sockbuf) { |
4225 | 0 | data->multi->xfer_sockbuf = fixed_buf_create(blen); |
4226 | 0 | if(!data->multi->xfer_sockbuf) { |
4227 | 0 | failf(data, "could not allocate xfer_sockbuf of %zu bytes", blen); |
4228 | 0 | return CURLE_OUT_OF_MEMORY; |
4229 | 0 | } |
4230 | 0 | } |
4231 | | |
4232 | 0 | data->multi->xfer_sockbuf_borrowed = TRUE; |
4233 | 0 | *pbuf = data->multi->xfer_sockbuf->data; |
4234 | 0 | return CURLE_OK; |
4235 | 0 | } |
4236 | | |
4237 | | void Curl_multi_xfer_sockbuf_release(struct Curl_easy *data, char *buf) |
4238 | 0 | { |
4239 | 0 | DEBUGASSERT(data); |
4240 | 0 | if(!data->multi) { |
4241 | | /* When a SHARE gets destroyed and has a connection pool, we get |
4242 | | * call with share->admin which does not have a multi handle. */ |
4243 | 0 | curlx_free(buf); |
4244 | 0 | } |
4245 | 0 | else { |
4246 | 0 | DEBUGASSERT(!buf || (data->multi->xfer_sockbuf && |
4247 | 0 | data->multi->xfer_sockbuf->data == buf)); |
4248 | 0 | data->multi->xfer_sockbuf_borrowed = FALSE; |
4249 | 0 | } |
4250 | 0 | } |
4251 | | |
4252 | | static void multi_xfer_bufs_free(struct Curl_multi *multi) |
4253 | 27.4k | { |
4254 | 27.4k | DEBUGASSERT(multi); |
4255 | 27.4k | curlx_safefree(multi->xfer_buf); |
4256 | 27.4k | multi->xfer_buf_borrowed = FALSE; |
4257 | 27.4k | curlx_safefree(multi->xfer_ulbuf); |
4258 | 27.4k | multi->xfer_ulbuf_borrowed = FALSE; |
4259 | 27.4k | curlx_safefree(multi->xfer_sockbuf); |
4260 | 27.4k | multi->xfer_sockbuf_borrowed = FALSE; |
4261 | 27.4k | } |
4262 | | |
4263 | | struct Curl_easy *Curl_multi_get_easy(struct Curl_multi *multi, |
4264 | | uint32_t mid) |
4265 | 1.62M | { |
4266 | 1.62M | struct Curl_easy *data = Curl_uint32_tbl_get(&multi->xfers, mid); |
4267 | 1.62M | if(GOOD_EASY_HANDLE(data)) |
4268 | 1.62M | return data; |
4269 | 0 | CURL_TRC_M(multi->admin, "invalid easy handle in xfer table for mid=%u", |
4270 | 0 | mid); |
4271 | 0 | Curl_uint32_tbl_remove(&multi->xfers, mid); |
4272 | 0 | return NULL; |
4273 | 1.62M | } |
4274 | | |
4275 | | bool Curl_multi_knows_easy(struct Curl_multi *multi, struct Curl_easy *data) |
4276 | 38.4k | { |
4277 | 38.4k | return Curl_uint32_tbl_get(&multi->xfers, data->mid) == data; |
4278 | 38.4k | } |
4279 | | |
4280 | | static uint32_t multi_xfers_running(struct Curl_multi *multi) |
4281 | 377k | { |
4282 | 377k | if(!multi) { |
4283 | 0 | DEBUGASSERT(0); |
4284 | 0 | return 0; |
4285 | 0 | } |
4286 | 377k | return multi->xfers_alive; |
4287 | 377k | } |
4288 | | |
4289 | | uint32_t Curl_multi_xfers_attached(struct Curl_multi *multi) |
4290 | 5.55k | { |
4291 | 5.55k | if(!multi || !multi->admin) { |
4292 | 0 | DEBUGASSERT(0); |
4293 | 0 | return 0; |
4294 | 0 | } |
4295 | | /* Discount the admin handle */ |
4296 | 5.55k | return Curl_uint32_tbl_count(&multi->xfers) - 1; |
4297 | 5.55k | } |
4298 | | |
4299 | | void Curl_multi_mark_dirty(struct Curl_easy *data) |
4300 | 221k | { |
4301 | 221k | if(data->multi && data->mid != UINT32_MAX) |
4302 | 221k | Curl_uint32_bset_add(&data->multi->dirty, data->mid); |
4303 | 221k | } |
4304 | | |
4305 | | void Curl_multi_clear_dirty(struct Curl_easy *data) |
4306 | 0 | { |
4307 | 0 | if(data->multi && data->mid != UINT32_MAX) |
4308 | 0 | Curl_uint32_bset_remove(&data->multi->dirty, data->mid); |
4309 | 0 | } |
4310 | | |
4311 | | CURLMcode curl_multi_notify_enable(CURLM *m, unsigned int notification) |
4312 | 0 | { |
4313 | 0 | struct Curl_mapi_guard guard; |
4314 | 0 | CURLMcode mresult = CURLM_OK; |
4315 | |
|
4316 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_notify_enable, &mresult)) { |
4317 | 0 | mresult = Curl_mntfy_enable(m, notification); |
4318 | 0 | } |
4319 | 0 | CURL_MAPI_LEAVE(&guard); |
4320 | 0 | return mresult; |
4321 | 0 | } |
4322 | | |
4323 | | CURLMcode curl_multi_notify_disable(CURLM *m, unsigned int notification) |
4324 | 0 | { |
4325 | 0 | struct Curl_mapi_guard guard; |
4326 | 0 | CURLMcode mresult = CURLM_OK; |
4327 | |
|
4328 | 0 | if(CURL_MAPI_ENTER(&guard, m, multi_notify_disable, &mresult)) { |
4329 | 0 | mresult = Curl_mntfy_disable(m, notification); |
4330 | 0 | } |
4331 | 0 | CURL_MAPI_LEAVE(&guard); |
4332 | 0 | return mresult; |
4333 | 0 | } |
4334 | | |
4335 | | #ifdef DEBUGBUILD |
4336 | | static void multi_xfer_dump(struct Curl_multi *multi, uint32_t mid, |
4337 | | void *entry) |
4338 | 0 | { |
4339 | 0 | struct Curl_easy *data = entry; |
4340 | |
|
4341 | 0 | (void)multi; |
4342 | 0 | if(!data) { |
4343 | 0 | curl_mfprintf(stderr, "mid=%u, entry=NULL, bug in xfer table?\n", mid); |
4344 | 0 | } |
4345 | 0 | else { |
4346 | 0 | curl_mfprintf(stderr, "mid=%u, magic=%s, p=%p, id=%" FMT_OFF_T |
4347 | 0 | ", url=%s\n", |
4348 | 0 | mid, |
4349 | 0 | (data->magic == CURLEASY_MAGIC_NUMBER) ? "GOOD" : "BAD!", |
4350 | 0 | (void *)data, data->id, Curl_bufref_ptr(&data->state.url)); |
4351 | 0 | } |
4352 | 0 | } |
4353 | | |
4354 | | static void multi_xfer_tbl_dump(struct Curl_multi *multi) |
4355 | 0 | { |
4356 | 0 | uint32_t mid; |
4357 | 0 | void *entry; |
4358 | 0 | curl_mfprintf(stderr, "=== multi xfer table (count=%u, capacity=%u\n", |
4359 | 0 | Curl_uint32_tbl_count(&multi->xfers), |
4360 | 0 | Curl_uint32_tbl_capacity(&multi->xfers)); |
4361 | 0 | if(Curl_uint32_tbl_first(&multi->xfers, &mid, &entry)) { |
4362 | 0 | multi_xfer_dump(multi, mid, entry); |
4363 | 0 | while(Curl_uint32_tbl_next(&multi->xfers, mid, &mid, &entry)) |
4364 | 0 | multi_xfer_dump(multi, mid, entry); |
4365 | 0 | } |
4366 | 0 | curl_mfprintf(stderr, "===\n"); |
4367 | | fflush(stderr); |
4368 | 0 | } |
4369 | | #endif /* DEBUGBUILD */ |