/src/CMake/Utilities/cmcurl/lib/easy.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | #include "curl_setup.h" |
25 | | |
26 | | #ifdef HAVE_NETINET_IN_H |
27 | | #include <netinet/in.h> |
28 | | #endif |
29 | | #ifdef HAVE_NETDB_H |
30 | | #include <netdb.h> |
31 | | #endif |
32 | | #ifdef HAVE_ARPA_INET_H |
33 | | #include <arpa/inet.h> |
34 | | #endif |
35 | | #ifdef HAVE_NET_IF_H |
36 | | #include <net/if.h> |
37 | | #endif |
38 | | #ifdef HAVE_SYS_IOCTL_H |
39 | | #include <sys/ioctl.h> |
40 | | #endif |
41 | | |
42 | | #ifdef HAVE_SYS_PARAM_H |
43 | | #include <sys/param.h> |
44 | | #endif |
45 | | |
46 | | #include "urldata.h" |
47 | | #include "transfer.h" |
48 | | #include "vtls/vtls.h" |
49 | | #include "vtls/vtls_scache.h" |
50 | | #include "vquic/vquic.h" |
51 | | #include "url.h" |
52 | | #include "getinfo.h" |
53 | | #include "hostip.h" |
54 | | #include "curlx/strdup.h" |
55 | | #include "easyif.h" |
56 | | #include "multiif.h" |
57 | | #include "multi_ev.h" |
58 | | #include "select.h" |
59 | | #include "cfilters.h" |
60 | | #include "sendf.h" |
61 | | #include "curl_trc.h" |
62 | | #include "connect.h" /* for Curl_getconnectinfo */ |
63 | | #include "slist.h" |
64 | | #include "mime.h" |
65 | | #include "amigaos.h" |
66 | | #include "macos.h" |
67 | | #include "curlx/wait.h" |
68 | | #include "sigpipe.h" |
69 | | #include "vssh/ssh.h" |
70 | | #include "setopt.h" |
71 | | #include "http_digest.h" |
72 | | #include "system_win32.h" |
73 | | #include "curlx/dynbuf.h" |
74 | | #include "bufref.h" |
75 | | #include "altsvc.h" |
76 | | #include "hsts.h" |
77 | | |
78 | | #include "easy_lock.h" |
79 | | |
80 | | /* true globals -- for curl_global_init() and curl_global_cleanup() */ |
81 | | static unsigned int initialized; |
82 | | static long easy_init_flags; |
83 | | |
84 | | #ifdef GLOBAL_INIT_IS_THREADSAFE |
85 | | |
86 | | static curl_simple_lock s_lock = CURL_SIMPLE_LOCK_INIT; |
87 | | #define global_init_lock() curl_simple_lock_lock(&s_lock) |
88 | | #define global_init_unlock() curl_simple_lock_unlock(&s_lock) |
89 | | |
90 | | #else |
91 | | |
92 | | #define global_init_lock() |
93 | | #define global_init_unlock() |
94 | | |
95 | | #endif |
96 | | |
97 | | #if defined(_MSC_VER) && defined(_DLL) |
98 | | # pragma warning(push) |
99 | | # pragma warning(disable:4232) /* MSVC extension, dllimport identity */ |
100 | | #endif |
101 | | |
102 | | /* |
103 | | * If a memory-using function (like curl_getenv) is used before |
104 | | * curl_global_init() is called, we need to have these pointers set already. |
105 | | */ |
106 | | curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc; |
107 | | curl_free_callback Curl_cfree = (curl_free_callback)free; |
108 | | curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc; |
109 | | curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)CURLX_STRDUP_LOW; |
110 | | curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc; |
111 | | |
112 | | #if defined(_MSC_VER) && defined(_DLL) |
113 | | # pragma warning(pop) |
114 | | #endif |
115 | | |
116 | | #ifdef DEBUGBUILD |
117 | | static char *leakpointer; |
118 | | #endif |
119 | | |
120 | | /** |
121 | | * curl_global_init() globally initializes curl given a bitwise set of the |
122 | | * different features of what to initialize. |
123 | | */ |
124 | | static CURLcode global_init(long flags, bool memoryfuncs) |
125 | 0 | { |
126 | 0 | if(initialized++) |
127 | 0 | return CURLE_OK; |
128 | | |
129 | 0 | if(memoryfuncs) { |
130 | | /* Setup the default memory functions here (again) */ |
131 | 0 | Curl_cmalloc = (curl_malloc_callback)malloc; |
132 | 0 | Curl_cfree = (curl_free_callback)free; |
133 | 0 | Curl_crealloc = (curl_realloc_callback)realloc; |
134 | 0 | Curl_cstrdup = (curl_strdup_callback)CURLX_STRDUP_LOW; |
135 | 0 | Curl_ccalloc = (curl_calloc_callback)calloc; |
136 | 0 | } |
137 | |
|
138 | 0 | if(Curl_trc_init()) { |
139 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_trc_init failed\n")); |
140 | 0 | goto fail; |
141 | 0 | } |
142 | | |
143 | 0 | if(!Curl_ssl_init()) { |
144 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssl_init failed\n")); |
145 | 0 | goto fail; |
146 | 0 | } |
147 | | |
148 | 0 | if(!Curl_vquic_init()) { |
149 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_vquic_init failed\n")); |
150 | 0 | goto fail; |
151 | 0 | } |
152 | | |
153 | 0 | if(Curl_win32_init(flags)) { |
154 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: win32_init failed\n")); |
155 | 0 | goto fail; |
156 | 0 | } |
157 | | |
158 | 0 | if(Curl_amiga_init()) { |
159 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_amiga_init failed\n")); |
160 | 0 | goto fail; |
161 | 0 | } |
162 | | |
163 | 0 | if(Curl_macos_init()) { |
164 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_macos_init failed\n")); |
165 | 0 | goto fail; |
166 | 0 | } |
167 | | |
168 | 0 | if(Curl_async_global_init()) { |
169 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: resolver_global_init failed\n")); |
170 | 0 | goto fail; |
171 | 0 | } |
172 | | |
173 | 0 | if(Curl_ssh_init()) { |
174 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_ssh_init failed\n")); |
175 | 0 | goto fail; |
176 | 0 | } |
177 | | |
178 | 0 | easy_init_flags = flags; |
179 | |
|
180 | | #ifdef DEBUGBUILD |
181 | | if(getenv("CURL_GLOBAL_INIT")) |
182 | | /* alloc data that will leak if *cleanup() is not called! */ |
183 | | leakpointer = curlx_malloc(1); |
184 | | #endif |
185 | |
|
186 | 0 | return CURLE_OK; |
187 | | |
188 | 0 | fail: |
189 | 0 | initialized--; /* undo the increase */ |
190 | 0 | return CURLE_FAILED_INIT; |
191 | 0 | } |
192 | | |
193 | | /** |
194 | | * curl_global_init() globally initializes curl given a bitwise set of the |
195 | | * different features of what to initialize. |
196 | | */ |
197 | | CURLcode curl_global_init(long flags) |
198 | 0 | { |
199 | 0 | CURLcode result; |
200 | 0 | global_init_lock(); |
201 | |
|
202 | 0 | result = global_init(flags, TRUE); |
203 | |
|
204 | 0 | global_init_unlock(); |
205 | |
|
206 | 0 | return result; |
207 | 0 | } |
208 | | |
209 | | /* |
210 | | * curl_global_init_mem() globally initializes curl and also registers the |
211 | | * user provided callback routines. |
212 | | */ |
213 | | CURLcode curl_global_init_mem(long flags, curl_malloc_callback m, |
214 | | curl_free_callback f, curl_realloc_callback r, |
215 | | curl_strdup_callback s, curl_calloc_callback c) |
216 | 0 | { |
217 | 0 | CURLcode result; |
218 | | |
219 | | /* Invalid input, return immediately */ |
220 | 0 | if(!m || !f || !r || !s || !c) |
221 | 0 | return CURLE_FAILED_INIT; |
222 | | |
223 | 0 | global_init_lock(); |
224 | |
|
225 | 0 | if(initialized) { |
226 | | /* Already initialized, do not do it again, but bump the variable anyway to |
227 | | work like curl_global_init() and require the same amount of cleanup |
228 | | calls. */ |
229 | 0 | initialized++; |
230 | 0 | global_init_unlock(); |
231 | 0 | return CURLE_OK; |
232 | 0 | } |
233 | | |
234 | | /* set memory functions before global_init() in case it wants memory |
235 | | functions */ |
236 | 0 | Curl_cmalloc = m; |
237 | 0 | Curl_cfree = f; |
238 | 0 | Curl_cstrdup = s; |
239 | 0 | Curl_crealloc = r; |
240 | 0 | Curl_ccalloc = c; |
241 | | |
242 | | /* Call the actual init function, but without setting */ |
243 | 0 | result = global_init(flags, FALSE); |
244 | |
|
245 | 0 | global_init_unlock(); |
246 | |
|
247 | 0 | return result; |
248 | 0 | } |
249 | | |
250 | | /** |
251 | | * curl_global_cleanup() globally cleanups curl, uses the value of |
252 | | * "easy_init_flags" to determine what needs to be cleaned up and what does |
253 | | * not. |
254 | | */ |
255 | | void curl_global_cleanup(void) |
256 | 0 | { |
257 | 0 | global_init_lock(); |
258 | |
|
259 | 0 | if(!initialized) { |
260 | 0 | global_init_unlock(); |
261 | 0 | return; |
262 | 0 | } |
263 | | |
264 | 0 | if(--initialized) { |
265 | 0 | global_init_unlock(); |
266 | 0 | return; |
267 | 0 | } |
268 | | |
269 | 0 | Curl_ssl_cleanup(); |
270 | 0 | Curl_async_global_cleanup(); |
271 | |
|
272 | | #ifdef _WIN32 |
273 | | Curl_win32_cleanup(easy_init_flags); |
274 | | #endif |
275 | |
|
276 | 0 | Curl_amiga_cleanup(); |
277 | |
|
278 | 0 | Curl_ssh_cleanup(); |
279 | |
|
280 | | #ifdef DEBUGBUILD |
281 | | curlx_free(leakpointer); |
282 | | #endif |
283 | |
|
284 | 0 | easy_init_flags = 0; |
285 | |
|
286 | 0 | global_init_unlock(); |
287 | 0 | } |
288 | | |
289 | | /** |
290 | | * curl_global_trace() globally initializes curl logging. |
291 | | */ |
292 | | CURLcode curl_global_trace(const char *config) |
293 | 0 | { |
294 | 0 | #ifndef CURL_DISABLE_VERBOSE_STRINGS |
295 | 0 | CURLcode result; |
296 | 0 | global_init_lock(); |
297 | |
|
298 | 0 | result = Curl_trc_opt(config); |
299 | |
|
300 | 0 | global_init_unlock(); |
301 | |
|
302 | 0 | return result; |
303 | | #else |
304 | | (void)config; |
305 | | return CURLE_OK; |
306 | | #endif |
307 | 0 | } |
308 | | |
309 | | /* |
310 | | * curl_global_sslset() globally initializes the SSL backend to use. |
311 | | */ |
312 | | CURLsslset curl_global_sslset(curl_sslbackend id, const char *name, |
313 | | const curl_ssl_backend ***avail) |
314 | 0 | { |
315 | 0 | CURLsslset rc; |
316 | |
|
317 | 0 | global_init_lock(); |
318 | |
|
319 | 0 | rc = Curl_init_sslset_nolock(id, name, avail); |
320 | |
|
321 | 0 | global_init_unlock(); |
322 | |
|
323 | 0 | return rc; |
324 | 0 | } |
325 | | |
326 | | /* |
327 | | * curl_easy_init() is the external interface to alloc, setup and init an |
328 | | * easy handle that is returned. If anything goes wrong, NULL is returned. |
329 | | */ |
330 | | CURL *curl_easy_init(void) |
331 | 0 | { |
332 | 0 | CURLcode result; |
333 | 0 | struct Curl_easy *data; |
334 | | |
335 | | /* Make sure we inited the global SSL stuff */ |
336 | 0 | global_init_lock(); |
337 | |
|
338 | 0 | if(!initialized) { |
339 | 0 | result = global_init(CURL_GLOBAL_DEFAULT, TRUE); |
340 | 0 | if(result) { |
341 | | /* something in the global init failed, return nothing */ |
342 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: curl_global_init failed\n")); |
343 | 0 | global_init_unlock(); |
344 | 0 | return NULL; |
345 | 0 | } |
346 | 0 | } |
347 | 0 | global_init_unlock(); |
348 | | |
349 | | /* We use Curl_open() with undefined URL so far */ |
350 | 0 | result = Curl_open(&data); |
351 | 0 | if(result) { |
352 | 0 | DEBUGF(curl_mfprintf(stderr, "Error: Curl_open failed\n")); |
353 | 0 | return NULL; |
354 | 0 | } |
355 | | |
356 | 0 | return data; |
357 | 0 | } |
358 | | |
359 | | #ifdef DEBUGBUILD |
360 | | |
361 | | struct socketmonitor { |
362 | | struct socketmonitor *next; /* the next node in the list or NULL */ |
363 | | struct pollfd socket; /* socket info of what to monitor */ |
364 | | }; |
365 | | |
366 | | struct events { |
367 | | long ms; /* timeout, run the timeout function when reached */ |
368 | | bool msbump; /* set TRUE when timeout is set by callback */ |
369 | | int num_sockets; /* number of nodes in the monitor list */ |
370 | | struct socketmonitor *list; /* list of sockets to monitor */ |
371 | | int running_handles; /* store the returned number */ |
372 | | }; |
373 | | |
374 | | #define DEBUG_EV_POLL 0 |
375 | | |
376 | | /* events_timer |
377 | | * |
378 | | * Callback that gets called with a new value when the timeout should be |
379 | | * updated. |
380 | | */ |
381 | | static int events_timer(CURLM *multi, /* multi handle */ |
382 | | long timeout_ms, /* see above */ |
383 | | void *userp) /* private callback pointer */ |
384 | | { |
385 | | struct events *ev = userp; |
386 | | (void)multi; |
387 | | #if DEBUG_EV_POLL |
388 | | curl_mfprintf(stderr, "events_timer: set timeout %ldms\n", timeout_ms); |
389 | | #endif |
390 | | ev->ms = timeout_ms; |
391 | | ev->msbump = TRUE; |
392 | | return 0; |
393 | | } |
394 | | |
395 | | /* poll2cselect |
396 | | * |
397 | | * convert from poll() bit definitions to libcurl's CURL_CSELECT_* ones |
398 | | */ |
399 | | static int poll2cselect(int pollmask) |
400 | | { |
401 | | int omask = 0; |
402 | | if(pollmask & POLLIN) |
403 | | omask |= CURL_CSELECT_IN; |
404 | | if(pollmask & POLLOUT) |
405 | | omask |= CURL_CSELECT_OUT; |
406 | | if(pollmask & POLLERR) |
407 | | omask |= CURL_CSELECT_ERR; |
408 | | return omask; |
409 | | } |
410 | | |
411 | | /* socketcb2poll |
412 | | * |
413 | | * convert from libcurl' CURL_POLL_* bit definitions to poll()'s |
414 | | */ |
415 | | static short socketcb2poll(int pollmask) |
416 | | { |
417 | | short omask = 0; |
418 | | if(pollmask & CURL_POLL_IN) |
419 | | omask |= POLLIN; |
420 | | if(pollmask & CURL_POLL_OUT) |
421 | | omask |= POLLOUT; |
422 | | return omask; |
423 | | } |
424 | | |
425 | | /* events_socket |
426 | | * |
427 | | * Callback that gets called with information about socket activity to |
428 | | * monitor. |
429 | | */ |
430 | | static int events_socket(CURL *easy, /* easy handle */ |
431 | | curl_socket_t s, /* socket */ |
432 | | int what, /* see above */ |
433 | | void *userp, /* private callback |
434 | | pointer */ |
435 | | void *socketp) /* private socket |
436 | | pointer */ |
437 | | { |
438 | | struct events *ev = userp; |
439 | | struct socketmonitor *m; |
440 | | struct socketmonitor *prev = NULL; |
441 | | bool found = FALSE; |
442 | | struct Curl_easy *data = easy; |
443 | | |
444 | | #ifdef CURL_DISABLE_VERBOSE_STRINGS |
445 | | (void)easy; |
446 | | #endif |
447 | | (void)socketp; |
448 | | |
449 | | m = ev->list; |
450 | | while(m) { |
451 | | if(m->socket.fd == s) { |
452 | | found = TRUE; |
453 | | if(what == CURL_POLL_REMOVE) { |
454 | | struct socketmonitor *nxt = m->next; |
455 | | /* remove this node from the list of monitored sockets */ |
456 | | if(prev) |
457 | | prev->next = nxt; |
458 | | else |
459 | | ev->list = nxt; |
460 | | curlx_free(m); |
461 | | infof(data, "socket cb: socket %" FMT_SOCKET_T " REMOVED", s); |
462 | | } |
463 | | else { |
464 | | /* The socket 's' is already being monitored, update the activity |
465 | | mask. Convert from libcurl bitmask to the poll one. */ |
466 | | m->socket.events = socketcb2poll(what); |
467 | | infof(data, "socket cb: socket %" FMT_SOCKET_T " UPDATED as %s%s", s, |
468 | | (what & CURL_POLL_IN) ? "IN" : "", |
469 | | (what & CURL_POLL_OUT) ? "OUT" : ""); |
470 | | } |
471 | | break; |
472 | | } |
473 | | prev = m; |
474 | | m = m->next; /* move to next node */ |
475 | | } |
476 | | |
477 | | if(!found) { |
478 | | if(what == CURL_POLL_REMOVE) { |
479 | | /* should not happen if our logic is correct, but is no drama. */ |
480 | | DEBUGF(infof(data, "socket cb: asked to REMOVE socket %" |
481 | | FMT_SOCKET_T "but not present!", s)); |
482 | | DEBUGASSERT(0); |
483 | | } |
484 | | else { |
485 | | m = curlx_malloc(sizeof(struct socketmonitor)); |
486 | | if(m) { |
487 | | m->next = ev->list; |
488 | | m->socket.fd = s; |
489 | | m->socket.events = socketcb2poll(what); |
490 | | m->socket.revents = 0; |
491 | | ev->list = m; |
492 | | infof(data, "socket cb: socket %" FMT_SOCKET_T " ADDED as %s%s", s, |
493 | | (what & CURL_POLL_IN) ? "IN" : "", |
494 | | (what & CURL_POLL_OUT) ? "OUT" : ""); |
495 | | } |
496 | | else |
497 | | return CURLE_OUT_OF_MEMORY; |
498 | | } |
499 | | } |
500 | | |
501 | | return 0; |
502 | | } |
503 | | |
504 | | /* |
505 | | * events_setup() |
506 | | * |
507 | | * Do the multi handle setups that only event-based transfers need. |
508 | | */ |
509 | | static void events_setup(struct Curl_multi *multi, struct events *ev) |
510 | | { |
511 | | /* timer callback */ |
512 | | curl_multi_setopt(multi, CURLMOPT_TIMERFUNCTION, events_timer); |
513 | | curl_multi_setopt(multi, CURLMOPT_TIMERDATA, ev); |
514 | | |
515 | | /* socket callback */ |
516 | | curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, events_socket); |
517 | | curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, ev); |
518 | | } |
519 | | |
520 | | /* populate_fds() |
521 | | * |
522 | | * populate the fds[] array |
523 | | */ |
524 | | static unsigned int populate_fds(struct pollfd *fds, struct events *ev) |
525 | | { |
526 | | unsigned int numfds = 0; |
527 | | struct pollfd *f; |
528 | | struct socketmonitor *m; |
529 | | |
530 | | f = &fds[0]; |
531 | | for(m = ev->list; m; m = m->next) { |
532 | | f->fd = m->socket.fd; |
533 | | f->events = m->socket.events; |
534 | | f->revents = 0; |
535 | | #if DEBUG_EV_POLL |
536 | | curl_mfprintf(stderr, "poll() %d check socket %d\n", numfds, f->fd); |
537 | | #endif |
538 | | f++; |
539 | | numfds++; |
540 | | } |
541 | | return numfds; |
542 | | } |
543 | | |
544 | | /* poll_fds() |
545 | | * |
546 | | * poll the fds[] array |
547 | | */ |
548 | | static CURLcode poll_fds(struct events *ev, |
549 | | struct pollfd *fds, |
550 | | const unsigned int numfds, |
551 | | int *pollrc) |
552 | | { |
553 | | if(numfds) { |
554 | | /* wait for activity or timeout */ |
555 | | #if DEBUG_EV_POLL |
556 | | curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms)\n", numfds, ev->ms); |
557 | | #endif |
558 | | *pollrc = Curl_poll(fds, numfds, ev->ms); |
559 | | #if DEBUG_EV_POLL |
560 | | curl_mfprintf(stderr, "poll(numfds=%u, timeout=%ldms) -> %d\n", |
561 | | numfds, ev->ms, *pollrc); |
562 | | #endif |
563 | | if(*pollrc < 0) |
564 | | return CURLE_UNRECOVERABLE_POLL; |
565 | | } |
566 | | else { |
567 | | #if DEBUG_EV_POLL |
568 | | curl_mfprintf(stderr, "poll, but no fds, wait timeout=%ldms\n", ev->ms); |
569 | | #endif |
570 | | *pollrc = 0; |
571 | | if(ev->ms > 0) |
572 | | curlx_wait_ms(ev->ms); |
573 | | } |
574 | | return CURLE_OK; |
575 | | } |
576 | | |
577 | | /* wait_or_timeout() |
578 | | * |
579 | | * waits for activity on any of the given sockets, or the timeout to trigger. |
580 | | */ |
581 | | static CURLcode wait_or_timeout(struct Curl_multi *multi, struct events *ev) |
582 | | { |
583 | | bool done = FALSE; |
584 | | CURLMcode mresult = CURLM_OK; |
585 | | CURLcode result = CURLE_OK; |
586 | | |
587 | | while(!done) { |
588 | | CURLMsg *msg; |
589 | | struct pollfd fds[4]; |
590 | | int pollrc; |
591 | | struct curltime start; |
592 | | const unsigned int numfds = populate_fds(fds, ev); |
593 | | |
594 | | /* get the time stamp to use to figure out how long poll takes */ |
595 | | curlx_pnow(&start); |
596 | | |
597 | | result = poll_fds(ev, fds, numfds, &pollrc); |
598 | | if(result) |
599 | | return result; |
600 | | |
601 | | ev->msbump = FALSE; /* reset here */ |
602 | | |
603 | | if(!pollrc) { |
604 | | /* timeout! */ |
605 | | ev->ms = 0; |
606 | | #if 0 |
607 | | curl_mfprintf(stderr, "call curl_multi_socket_action(TIMEOUT)\n"); |
608 | | #endif |
609 | | mresult = curl_multi_socket_action(multi, CURL_SOCKET_TIMEOUT, 0, |
610 | | &ev->running_handles); |
611 | | } |
612 | | else { |
613 | | /* here pollrc is > 0 */ |
614 | | /* loop over the monitored sockets to see which ones had activity */ |
615 | | unsigned int i; |
616 | | for(i = 0; i < numfds; i++) { |
617 | | if(fds[i].revents) { |
618 | | /* socket activity, tell libcurl */ |
619 | | int act = poll2cselect(fds[i].revents); /* convert */ |
620 | | |
621 | | /* sending infof "randomly" to the first easy handle */ |
622 | | infof(multi->admin, "call curl_multi_socket_action(socket " |
623 | | "%" FMT_SOCKET_T ")", (curl_socket_t)fds[i].fd); |
624 | | mresult = curl_multi_socket_action(multi, fds[i].fd, act, |
625 | | &ev->running_handles); |
626 | | } |
627 | | } |
628 | | |
629 | | if(!ev->msbump && ev->ms >= 0) { |
630 | | /* If nothing updated the timeout, we decrease it by the spent time. |
631 | | * If it was updated, it has the new timeout time stored already. |
632 | | */ |
633 | | timediff_t spent_ms = curlx_timediff_ms(curlx_now(), start); |
634 | | if(spent_ms > 0) { |
635 | | #if DEBUG_EV_POLL |
636 | | curl_mfprintf(stderr, "poll timeout %ldms not updated, decrease by " |
637 | | "time spent %ldms\n", ev->ms, (long)spent_ms); |
638 | | #endif |
639 | | if(spent_ms > ev->ms) |
640 | | ev->ms = 0; |
641 | | else |
642 | | ev->ms -= (long)spent_ms; |
643 | | } |
644 | | } |
645 | | } |
646 | | |
647 | | if(mresult) |
648 | | return CURLE_URL_MALFORMAT; |
649 | | |
650 | | /* we do not really care about the "msgs_in_queue" value returned in the |
651 | | second argument */ |
652 | | msg = curl_multi_info_read(multi, &pollrc); |
653 | | if(msg) { |
654 | | result = msg->data.result; |
655 | | done = TRUE; |
656 | | } |
657 | | } |
658 | | |
659 | | return result; |
660 | | } |
661 | | |
662 | | /* easy_events() |
663 | | * |
664 | | * Runs a transfer in a blocking manner using the events-based API |
665 | | */ |
666 | | static CURLcode easy_events(struct Curl_multi *multi) |
667 | | { |
668 | | /* this struct is made static to allow it to be used after this function |
669 | | returns and curl_multi_remove_handle() is called */ |
670 | | static struct events evs = { -1, FALSE, 0, NULL, 0 }; |
671 | | |
672 | | /* if running event-based, do some further multi inits */ |
673 | | events_setup(multi, &evs); |
674 | | |
675 | | return wait_or_timeout(multi, &evs); |
676 | | } |
677 | | #else /* DEBUGBUILD */ |
678 | | /* when not built with debug, this function does not exist */ |
679 | 0 | #define easy_events(x) CURLE_NOT_BUILT_IN |
680 | | #endif |
681 | | |
682 | | static CURLcode easy_transfer(struct Curl_multi *multi) |
683 | 0 | { |
684 | 0 | bool done = FALSE; |
685 | 0 | CURLMcode mresult = CURLM_OK; |
686 | 0 | CURLcode result = CURLE_OK; |
687 | |
|
688 | 0 | while(!done && !mresult) { |
689 | 0 | int still_running = 0; |
690 | |
|
691 | 0 | mresult = curl_multi_poll(multi, NULL, 0, 1000, NULL); |
692 | |
|
693 | 0 | if(!mresult) |
694 | 0 | mresult = curl_multi_perform(multi, &still_running); |
695 | | |
696 | | /* only read 'still_running' if curl_multi_perform() return OK */ |
697 | 0 | if(!mresult && !still_running) { |
698 | 0 | int rc; |
699 | 0 | CURLMsg *msg = curl_multi_info_read(multi, &rc); |
700 | 0 | if(msg) { |
701 | 0 | result = msg->data.result; |
702 | 0 | done = TRUE; |
703 | 0 | } |
704 | 0 | } |
705 | 0 | } |
706 | | |
707 | | /* Make sure to return some kind of error if there was a multi problem */ |
708 | 0 | if(mresult) { |
709 | 0 | result = (mresult == CURLM_OUT_OF_MEMORY) ? CURLE_OUT_OF_MEMORY : |
710 | | /* The other multi errors should never happen, so return |
711 | | something suitably generic */ |
712 | 0 | CURLE_BAD_FUNCTION_ARGUMENT; |
713 | 0 | } |
714 | |
|
715 | 0 | return result; |
716 | 0 | } |
717 | | |
718 | | /* |
719 | | * easy_perform() is the internal interface that performs a blocking |
720 | | * transfer as previously setup. |
721 | | * |
722 | | * CONCEPT: This function creates a multi handle, adds the easy handle to it, |
723 | | * runs curl_multi_perform() until the transfer is done, then detaches the |
724 | | * easy handle, destroys the multi handle and returns the easy handle's return |
725 | | * code. |
726 | | * |
727 | | * REALITY: it cannot create and destroy the multi handle that easily. It |
728 | | * needs to keep it around since if this easy handle is used again by this |
729 | | * function, the same multi handle must be reused so that the same pools and |
730 | | * caches can be used. |
731 | | * |
732 | | * DEBUG: if 'events' is set TRUE, this function will use a replacement engine |
733 | | * instead of curl_multi_perform() and use curl_multi_socket_action(). |
734 | | */ |
735 | | static CURLcode easy_perform(struct Curl_easy *data, bool events) |
736 | 0 | { |
737 | 0 | struct Curl_multi *multi; |
738 | 0 | CURLMcode mresult; |
739 | 0 | CURLcode result = CURLE_OK; |
740 | 0 | struct Curl_sigpipe_ctx sigpipe_ctx; |
741 | |
|
742 | 0 | if(!data) |
743 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
744 | | |
745 | 0 | if(data->set.errorbuffer) |
746 | | /* clear this as early as possible */ |
747 | 0 | data->set.errorbuffer[0] = 0; |
748 | |
|
749 | 0 | data->state.os_errno = 0; |
750 | |
|
751 | 0 | if(data->multi) { |
752 | 0 | failf(data, "easy handle already used in multi handle"); |
753 | 0 | return CURLE_FAILED_INIT; |
754 | 0 | } |
755 | | |
756 | | /* if the handle has a connection still attached (it is/was a connect-only |
757 | | handle) then disconnect before performing */ |
758 | 0 | if(data->conn) { |
759 | 0 | struct connectdata *c; |
760 | 0 | curl_socket_t s; |
761 | 0 | Curl_detach_connection(data); |
762 | 0 | s = Curl_getconnectinfo(data, &c); |
763 | 0 | if((s != CURL_SOCKET_BAD) && c) { |
764 | 0 | Curl_conn_terminate(data, c, TRUE); |
765 | 0 | } |
766 | 0 | DEBUGASSERT(!data->conn); |
767 | 0 | } |
768 | |
|
769 | 0 | if(data->multi_easy) |
770 | 0 | multi = data->multi_easy; |
771 | 0 | else { |
772 | | /* this multi handle will only ever have a single easy handle attached to |
773 | | it, so make it use minimal hash sizes */ |
774 | 0 | multi = Curl_multi_handle(16, 1, 3, 7, 3); |
775 | 0 | if(!multi) |
776 | 0 | return CURLE_OUT_OF_MEMORY; |
777 | 0 | } |
778 | | |
779 | 0 | if(multi->in_callback) |
780 | 0 | return CURLE_RECURSIVE_API_CALL; |
781 | | |
782 | | /* Copy the MAXCONNECTS option to the multi handle */ |
783 | 0 | curl_multi_setopt(multi, CURLMOPT_MAXCONNECTS, (long)data->set.maxconnects); |
784 | |
|
785 | 0 | data->multi_easy = NULL; /* pretend it does not exist */ |
786 | 0 | mresult = curl_multi_add_handle(multi, data); |
787 | 0 | if(mresult) { |
788 | 0 | curl_multi_cleanup(multi); |
789 | 0 | if(mresult == CURLM_OUT_OF_MEMORY) |
790 | 0 | return CURLE_OUT_OF_MEMORY; |
791 | 0 | return CURLE_FAILED_INIT; |
792 | 0 | } |
793 | | |
794 | | /* assign this after curl_multi_add_handle() */ |
795 | 0 | data->multi_easy = multi; |
796 | |
|
797 | 0 | sigpipe_init(&sigpipe_ctx); |
798 | 0 | sigpipe_apply(data, &sigpipe_ctx); |
799 | | |
800 | | /* run the transfer */ |
801 | 0 | result = events ? easy_events(multi) : easy_transfer(multi); |
802 | | |
803 | | /* ignoring the return code is not nice, but atm we cannot really handle |
804 | | a failure here, room for future improvement! */ |
805 | 0 | (void)curl_multi_remove_handle(multi, data); |
806 | |
|
807 | 0 | sigpipe_restore(&sigpipe_ctx); |
808 | | |
809 | | /* The multi handle is kept alive, owned by the easy handle */ |
810 | 0 | return result; |
811 | 0 | } |
812 | | |
813 | | /* |
814 | | * curl_easy_perform() is the external interface that performs a blocking |
815 | | * transfer as previously setup. |
816 | | */ |
817 | | CURLcode curl_easy_perform(CURL *data) |
818 | 0 | { |
819 | 0 | return easy_perform(data, FALSE); |
820 | 0 | } |
821 | | |
822 | | #ifdef DEBUGBUILD |
823 | | /* |
824 | | * curl_easy_perform_ev() is the external interface that performs a blocking |
825 | | * transfer using the event-based API internally. |
826 | | */ |
827 | | CURLcode curl_easy_perform_ev(struct Curl_easy *data) |
828 | | { |
829 | | return easy_perform(data, TRUE); |
830 | | } |
831 | | #endif |
832 | | |
833 | | /* |
834 | | * curl_easy_cleanup() is the external interface to cleaning/freeing the given |
835 | | * easy handle. |
836 | | */ |
837 | | void curl_easy_cleanup(CURL *ptr) |
838 | 0 | { |
839 | 0 | struct Curl_easy *data = ptr; |
840 | 0 | if(GOOD_EASY_HANDLE(data)) { |
841 | 0 | struct Curl_sigpipe_ctx sigpipe_ctx; |
842 | 0 | sigpipe_ignore(data, &sigpipe_ctx); |
843 | 0 | Curl_close(&data); |
844 | 0 | sigpipe_restore(&sigpipe_ctx); |
845 | 0 | } |
846 | 0 | } |
847 | | |
848 | | /* |
849 | | * curl_easy_getinfo() is an external interface that allows an app to retrieve |
850 | | * information from a performed transfer and similar. |
851 | | */ |
852 | | #undef curl_easy_getinfo |
853 | | CURLcode curl_easy_getinfo(CURL *easy, CURLINFO info, ...) |
854 | 0 | { |
855 | 0 | struct Curl_easy *data = easy; |
856 | 0 | va_list arg; |
857 | 0 | void *paramp; |
858 | 0 | CURLcode result; |
859 | |
|
860 | 0 | if(!GOOD_EASY_HANDLE(data)) |
861 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
862 | | |
863 | 0 | va_start(arg, info); |
864 | 0 | paramp = va_arg(arg, void *); |
865 | |
|
866 | 0 | result = Curl_getinfo(data, info, paramp); |
867 | |
|
868 | 0 | va_end(arg); |
869 | 0 | return result; |
870 | 0 | } |
871 | | |
872 | | static CURLcode dupset(struct Curl_easy *dst, struct Curl_easy *src) |
873 | 0 | { |
874 | 0 | CURLcode result = CURLE_OK; |
875 | 0 | enum dupstring i; |
876 | 0 | enum dupblob j; |
877 | | |
878 | | /* Copy src->set into dst->set first, then deal with the strings |
879 | | afterwards */ |
880 | 0 | dst->set = src->set; |
881 | 0 | #if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API) |
882 | 0 | dst->set.mimepostp = NULL; |
883 | 0 | #endif |
884 | | /* clear all dest string and blob pointers first, in case we error out |
885 | | mid-function */ |
886 | 0 | memset(dst->set.str, 0, STRING_LAST * sizeof(char *)); |
887 | 0 | memset(dst->set.blobs, 0, BLOB_LAST * sizeof(struct curl_blob *)); |
888 | | |
889 | | /* duplicate all strings */ |
890 | 0 | for(i = (enum dupstring)0; i < STRING_LASTZEROTERMINATED; i++) { |
891 | 0 | result = Curl_setstropt(&dst->set.str[i], src->set.str[i]); |
892 | 0 | if(result) |
893 | 0 | return result; |
894 | 0 | } |
895 | | |
896 | | /* duplicate all blobs */ |
897 | 0 | for(j = (enum dupblob)0; j < BLOB_LAST; j++) { |
898 | 0 | result = Curl_setblobopt(&dst->set.blobs[j], src->set.blobs[j]); |
899 | 0 | if(result) |
900 | 0 | return result; |
901 | 0 | } |
902 | | |
903 | | /* duplicate memory areas pointed to */ |
904 | 0 | i = STRING_COPYPOSTFIELDS; |
905 | 0 | if(src->set.str[i]) { |
906 | 0 | if(src->set.postfieldsize == -1) |
907 | 0 | dst->set.str[i] = curlx_strdup(src->set.str[i]); |
908 | 0 | else |
909 | | /* postfieldsize is curl_off_t, curlx_memdup() takes a size_t ... */ |
910 | 0 | dst->set.str[i] = curlx_memdup(src->set.str[i], |
911 | 0 | curlx_sotouz(src->set.postfieldsize)); |
912 | 0 | if(!dst->set.str[i]) |
913 | 0 | return CURLE_OUT_OF_MEMORY; |
914 | | /* point to the new copy */ |
915 | 0 | dst->set.postfields = dst->set.str[i]; |
916 | 0 | } |
917 | | |
918 | 0 | #if !defined(CURL_DISABLE_MIME) || !defined(CURL_DISABLE_FORM_API) |
919 | 0 | if(src->set.mimepostp) { |
920 | | /* Duplicate mime data. Get a mimepost struct for the clone as well */ |
921 | 0 | dst->set.mimepostp = curlx_malloc(sizeof(*dst->set.mimepostp)); |
922 | 0 | if(!dst->set.mimepostp) |
923 | 0 | return CURLE_OUT_OF_MEMORY; |
924 | | |
925 | 0 | Curl_mime_initpart(dst->set.mimepostp); |
926 | 0 | result = Curl_mime_duppart(dst, dst->set.mimepostp, src->set.mimepostp); |
927 | 0 | if(result) |
928 | 0 | return result; |
929 | 0 | } |
930 | 0 | #endif |
931 | | |
932 | 0 | if(src->set.resolve) |
933 | 0 | dst->state.resolve = dst->set.resolve; |
934 | |
|
935 | 0 | return result; |
936 | 0 | } |
937 | | |
938 | | static void dupeasy_meta_freeentry(void *p) |
939 | 0 | { |
940 | 0 | (void)p; |
941 | | /* Will always be FALSE. Cannot use a 0 assert here since compilers |
942 | | * are not in agreement if they then want a NORETURN attribute or |
943 | | * not. *sigh* */ |
944 | 0 | DEBUGASSERT(p == NULL); |
945 | 0 | } |
946 | | |
947 | | /* |
948 | | * curl_easy_duphandle() is an external interface to allow duplication of a |
949 | | * given input easy handle. The returned handle will be a new working handle |
950 | | * with all options set exactly as the input source handle. |
951 | | */ |
952 | | CURL *curl_easy_duphandle(CURL *d) |
953 | 0 | { |
954 | 0 | struct Curl_easy *data = d; |
955 | 0 | struct Curl_easy *outcurl = NULL; |
956 | |
|
957 | 0 | if(!GOOD_EASY_HANDLE(data)) |
958 | 0 | goto fail; |
959 | 0 | outcurl = curlx_calloc(1, sizeof(struct Curl_easy)); |
960 | 0 | if(!outcurl) |
961 | 0 | goto fail; |
962 | | |
963 | | /* |
964 | | * We setup a few buffers we need. We should probably make them |
965 | | * get setup on-demand in the code, as that would probably decrease |
966 | | * the likeliness of us forgetting to init a buffer here in the future. |
967 | | */ |
968 | 0 | outcurl->set.buffer_size = data->set.buffer_size; |
969 | |
|
970 | 0 | Curl_hash_init(&outcurl->meta_hash, 23, |
971 | 0 | Curl_hash_str, curlx_str_key_compare, dupeasy_meta_freeentry); |
972 | 0 | curlx_dyn_init(&outcurl->state.headerb, CURL_MAX_HTTP_HEADER); |
973 | 0 | Curl_bufref_init(&outcurl->state.url); |
974 | 0 | Curl_bufref_init(&outcurl->state.referer); |
975 | 0 | Curl_netrc_init(&outcurl->state.netrc); |
976 | | |
977 | | /* the connection pool is setup on demand */ |
978 | 0 | outcurl->state.lastconnect_id = -1; |
979 | 0 | outcurl->state.recent_conn_id = -1; |
980 | 0 | outcurl->id = -1; |
981 | 0 | outcurl->mid = UINT32_MAX; |
982 | 0 | outcurl->master_mid = UINT32_MAX; |
983 | |
|
984 | 0 | #ifndef CURL_DISABLE_HTTP |
985 | 0 | Curl_llist_init(&outcurl->state.httphdrs, NULL); |
986 | 0 | #endif |
987 | 0 | Curl_initinfo(outcurl); |
988 | | |
989 | | /* copy all userdefined values */ |
990 | 0 | if(dupset(outcurl, data)) |
991 | 0 | goto fail; |
992 | | |
993 | 0 | outcurl->progress.hide = data->progress.hide; |
994 | 0 | outcurl->progress.callback = data->progress.callback; |
995 | |
|
996 | 0 | #ifndef CURL_DISABLE_COOKIES |
997 | 0 | outcurl->state.cookielist = NULL; |
998 | 0 | if(data->cookies && data->state.cookie_engine) { |
999 | | /* If cookies are enabled in the parent handle, we enable them |
1000 | | in the clone as well! */ |
1001 | 0 | outcurl->cookies = Curl_cookie_init(); |
1002 | 0 | if(!outcurl->cookies) |
1003 | 0 | goto fail; |
1004 | 0 | outcurl->state.cookie_engine = TRUE; |
1005 | 0 | } |
1006 | | |
1007 | 0 | if(data->state.cookielist) { |
1008 | 0 | outcurl->state.cookielist = Curl_slist_duplicate(data->state.cookielist); |
1009 | 0 | if(!outcurl->state.cookielist) |
1010 | 0 | goto fail; |
1011 | 0 | } |
1012 | 0 | #endif |
1013 | | |
1014 | 0 | if(Curl_bufref_ptr(&data->state.url)) { |
1015 | 0 | Curl_bufref_set(&outcurl->state.url, |
1016 | 0 | Curl_bufref_dup(&data->state.url), 0, |
1017 | 0 | curl_free); |
1018 | 0 | if(!Curl_bufref_ptr(&outcurl->state.url)) |
1019 | 0 | goto fail; |
1020 | 0 | } |
1021 | 0 | if(Curl_bufref_ptr(&data->state.referer)) { |
1022 | 0 | Curl_bufref_set(&outcurl->state.referer, |
1023 | 0 | Curl_bufref_dup(&data->state.referer), 0, |
1024 | 0 | curl_free); |
1025 | 0 | if(!Curl_bufref_ptr(&outcurl->state.referer)) |
1026 | 0 | goto fail; |
1027 | 0 | } |
1028 | | |
1029 | | /* Reinitialize an SSL engine for the new handle |
1030 | | * note: the engine name has already been copied by dupset */ |
1031 | 0 | if(outcurl->set.str[STRING_SSL_ENGINE]) { |
1032 | 0 | if(Curl_ssl_set_engine(outcurl, outcurl->set.str[STRING_SSL_ENGINE])) |
1033 | 0 | goto fail; |
1034 | 0 | } |
1035 | | |
1036 | | #ifndef CURL_DISABLE_ALTSVC |
1037 | | if(data->asi) { |
1038 | | outcurl->asi = Curl_altsvc_init(); |
1039 | | if(!outcurl->asi) |
1040 | | goto fail; |
1041 | | if(outcurl->set.str[STRING_ALTSVC]) |
1042 | | (void)Curl_altsvc_load(outcurl->asi, outcurl->set.str[STRING_ALTSVC]); |
1043 | | } |
1044 | | #endif |
1045 | | #ifndef CURL_DISABLE_HSTS |
1046 | | if(data->hsts) { |
1047 | | outcurl->hsts = Curl_hsts_init(); |
1048 | | if(!outcurl->hsts) |
1049 | | goto fail; |
1050 | | if(outcurl->set.str[STRING_HSTS]) |
1051 | | (void)Curl_hsts_loadfile(outcurl, |
1052 | | outcurl->hsts, outcurl->set.str[STRING_HSTS]); |
1053 | | (void)Curl_hsts_loadcb(outcurl, outcurl->hsts); |
1054 | | } |
1055 | | #endif |
1056 | | |
1057 | 0 | outcurl->magic = CURLEASY_MAGIC_NUMBER; |
1058 | | |
1059 | | /* we reach this point and thus we are OK */ |
1060 | |
|
1061 | 0 | return outcurl; |
1062 | | |
1063 | 0 | fail: |
1064 | |
|
1065 | 0 | if(outcurl) { |
1066 | 0 | #ifndef CURL_DISABLE_COOKIES |
1067 | 0 | curlx_free(outcurl->cookies); |
1068 | 0 | #endif |
1069 | 0 | curlx_dyn_free(&outcurl->state.headerb); |
1070 | 0 | Curl_altsvc_cleanup(&outcurl->asi); |
1071 | 0 | Curl_hsts_cleanup(&outcurl->hsts); |
1072 | 0 | Curl_freeset(outcurl); |
1073 | 0 | curlx_free(outcurl); |
1074 | 0 | } |
1075 | |
|
1076 | 0 | return NULL; |
1077 | 0 | } |
1078 | | |
1079 | | /* |
1080 | | * curl_easy_reset() is an external interface that allows an app to re- |
1081 | | * initialize a session handle to the default values. |
1082 | | */ |
1083 | | void curl_easy_reset(CURL *d) |
1084 | 0 | { |
1085 | 0 | struct Curl_easy *data = d; |
1086 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1087 | 0 | return; |
1088 | | |
1089 | 0 | Curl_req_hard_reset(&data->req, data); |
1090 | 0 | Curl_hash_clean(&data->meta_hash); |
1091 | | |
1092 | | /* clear all meta data */ |
1093 | 0 | Curl_meta_reset(data); |
1094 | | /* clear any resolve data */ |
1095 | 0 | Curl_async_shutdown(data); |
1096 | 0 | Curl_resolv_unlink(data, &data->state.dns[0]); |
1097 | 0 | Curl_resolv_unlink(data, &data->state.dns[1]); |
1098 | | /* zero out UserDefined data: */ |
1099 | 0 | Curl_freeset(data); |
1100 | 0 | memset(&data->set, 0, sizeof(struct UserDefined)); |
1101 | 0 | Curl_init_userdefined(data); |
1102 | | |
1103 | | /* zero out Progress data: */ |
1104 | 0 | memset(&data->progress, 0, sizeof(struct Progress)); |
1105 | | |
1106 | | /* zero out PureInfo data: */ |
1107 | 0 | Curl_initinfo(data); |
1108 | |
|
1109 | 0 | data->progress.hide = TRUE; |
1110 | 0 | data->state.current_speed = -1; /* init to negative == impossible */ |
1111 | 0 | data->state.recent_conn_id = -1; /* clear remembered connection id */ |
1112 | | |
1113 | | /* zero out authentication data: */ |
1114 | 0 | memset(&data->state.authhost, 0, sizeof(struct auth)); |
1115 | 0 | memset(&data->state.authproxy, 0, sizeof(struct auth)); |
1116 | |
|
1117 | 0 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_DIGEST_AUTH) |
1118 | 0 | Curl_http_auth_cleanup_digest(data); |
1119 | 0 | #endif |
1120 | 0 | data->master_mid = UINT32_MAX; |
1121 | 0 | } |
1122 | | |
1123 | | /* |
1124 | | * curl_easy_pause() allows an application to pause or unpause a specific |
1125 | | * transfer and direction. This function sets the full new state for the |
1126 | | * current connection this easy handle operates on. |
1127 | | * |
1128 | | * NOTE: if you have the receiving paused and you call this function to remove |
1129 | | * the pausing, you may get your write callback called at this point. |
1130 | | * |
1131 | | * Action is a bitmask consisting of CURLPAUSE_* bits in curl/curl.h |
1132 | | * |
1133 | | * NOTE: This is one of few API functions that are allowed to be called from |
1134 | | * within a callback. |
1135 | | */ |
1136 | | CURLcode curl_easy_pause(CURL *d, int action) |
1137 | 0 | { |
1138 | 0 | CURLcode result = CURLE_OK; |
1139 | 0 | bool recursive = FALSE; |
1140 | 0 | bool changed = FALSE; |
1141 | 0 | struct Curl_easy *data = d; |
1142 | 0 | bool recv_paused, recv_paused_new; |
1143 | 0 | bool send_paused, send_paused_new; |
1144 | |
|
1145 | 0 | if(!GOOD_EASY_HANDLE(data) || !data->conn) |
1146 | | /* crazy input, do not continue */ |
1147 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1148 | | |
1149 | 0 | if(Curl_is_in_callback(data)) |
1150 | 0 | recursive = TRUE; |
1151 | |
|
1152 | 0 | recv_paused = Curl_xfer_recv_is_paused(data); |
1153 | 0 | recv_paused_new = (action & CURLPAUSE_RECV); |
1154 | 0 | send_paused = Curl_xfer_send_is_paused(data); |
1155 | 0 | send_paused_new = (action & CURLPAUSE_SEND); |
1156 | |
|
1157 | 0 | if((send_paused != send_paused_new) || |
1158 | 0 | (send_paused_new != Curl_creader_is_paused(data))) { |
1159 | 0 | changed = TRUE; |
1160 | 0 | result = Curl_1st_err(result, Curl_xfer_pause_send(data, send_paused_new)); |
1161 | 0 | } |
1162 | |
|
1163 | 0 | if(recv_paused != recv_paused_new) { |
1164 | 0 | changed = TRUE; |
1165 | 0 | result = Curl_1st_err(result, Curl_xfer_pause_recv(data, recv_paused_new)); |
1166 | 0 | } |
1167 | | |
1168 | | /* If not completely pausing both directions now, run again in any case. */ |
1169 | 0 | if(!Curl_xfer_is_blocked(data)) { |
1170 | | /* reset the too-slow time keeper */ |
1171 | 0 | data->state.keeps_speed.tv_sec = 0; |
1172 | 0 | if(data->multi) { |
1173 | 0 | Curl_multi_mark_dirty(data); /* make it run */ |
1174 | | /* On changes, tell application to update its timers. */ |
1175 | 0 | if(changed) { |
1176 | 0 | if(Curl_update_timer(data->multi) && !result) |
1177 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
1178 | 0 | } |
1179 | 0 | } |
1180 | 0 | } |
1181 | |
|
1182 | 0 | if(!result && changed && !data->state.done && data->multi) |
1183 | | /* pause/unpausing may result in multi event changes */ |
1184 | 0 | if(Curl_multi_ev_assess_xfer(data->multi, data) && !result) |
1185 | 0 | result = CURLE_ABORTED_BY_CALLBACK; |
1186 | |
|
1187 | 0 | if(recursive) |
1188 | | /* this might have called a callback recursively which might have set this |
1189 | | to false again on exit */ |
1190 | 0 | Curl_set_in_callback(data, TRUE); |
1191 | |
|
1192 | 0 | return result; |
1193 | 0 | } |
1194 | | |
1195 | | static CURLcode easy_connection(struct Curl_easy *data, |
1196 | | struct connectdata **connp) |
1197 | 0 | { |
1198 | 0 | curl_socket_t sfd; |
1199 | |
|
1200 | 0 | if(!data) |
1201 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1202 | | |
1203 | | /* only allow these to be called on handles with CURLOPT_CONNECT_ONLY */ |
1204 | 0 | if(!data->set.connect_only) { |
1205 | 0 | failf(data, "CONNECT_ONLY is required"); |
1206 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
1207 | 0 | } |
1208 | | |
1209 | 0 | sfd = Curl_getconnectinfo(data, connp); |
1210 | |
|
1211 | 0 | if(sfd == CURL_SOCKET_BAD) { |
1212 | 0 | failf(data, "Failed to get recent socket"); |
1213 | 0 | return CURLE_UNSUPPORTED_PROTOCOL; |
1214 | 0 | } |
1215 | | |
1216 | 0 | return CURLE_OK; |
1217 | 0 | } |
1218 | | |
1219 | | /* |
1220 | | * Receives data from the connected socket. Use after successful |
1221 | | * curl_easy_perform() with CURLOPT_CONNECT_ONLY option. |
1222 | | * Returns CURLE_OK on success, error code on error. |
1223 | | */ |
1224 | | CURLcode curl_easy_recv(CURL *d, void *buffer, size_t buflen, size_t *n) |
1225 | 0 | { |
1226 | 0 | CURLcode result; |
1227 | 0 | struct connectdata *c; |
1228 | 0 | struct Curl_easy *data = d; |
1229 | |
|
1230 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1231 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1232 | 0 | if(Curl_is_in_callback(data)) |
1233 | 0 | return CURLE_RECURSIVE_API_CALL; |
1234 | | |
1235 | 0 | result = easy_connection(data, &c); |
1236 | 0 | if(result) |
1237 | 0 | return result; |
1238 | | |
1239 | 0 | if(!data->conn) |
1240 | | /* on first invoke, the transfer has been detached from the connection and |
1241 | | needs to be reattached */ |
1242 | 0 | Curl_attach_connection(data, c); |
1243 | |
|
1244 | 0 | *n = 0; |
1245 | 0 | return Curl_conn_recv(data, FIRSTSOCKET, buffer, buflen, n); |
1246 | 0 | } |
1247 | | |
1248 | | #ifndef CURL_DISABLE_WEBSOCKETS |
1249 | | CURLcode Curl_connect_only_attach(struct Curl_easy *data) |
1250 | | { |
1251 | | CURLcode result; |
1252 | | struct connectdata *c = NULL; |
1253 | | |
1254 | | result = easy_connection(data, &c); |
1255 | | if(result) |
1256 | | return result; |
1257 | | |
1258 | | if(!data->conn) |
1259 | | /* on first invoke, the transfer has been detached from the connection and |
1260 | | needs to be reattached */ |
1261 | | Curl_attach_connection(data, c); |
1262 | | |
1263 | | return CURLE_OK; |
1264 | | } |
1265 | | #endif /* !CURL_DISABLE_WEBSOCKETS */ |
1266 | | |
1267 | | /* |
1268 | | * Sends data over the connected socket. |
1269 | | * |
1270 | | * This is the private internal version of curl_easy_send() |
1271 | | */ |
1272 | | CURLcode Curl_senddata(struct Curl_easy *data, const void *buffer, |
1273 | | size_t buflen, size_t *n) |
1274 | 0 | { |
1275 | 0 | CURLcode result; |
1276 | 0 | struct connectdata *c = NULL; |
1277 | 0 | struct Curl_sigpipe_ctx sigpipe_ctx; |
1278 | |
|
1279 | 0 | *n = 0; |
1280 | 0 | result = easy_connection(data, &c); |
1281 | 0 | if(result) |
1282 | 0 | return result; |
1283 | | |
1284 | 0 | if(!data->conn) |
1285 | | /* on first invoke, the transfer has been detached from the connection and |
1286 | | needs to be reattached */ |
1287 | 0 | Curl_attach_connection(data, c); |
1288 | |
|
1289 | 0 | sigpipe_ignore(data, &sigpipe_ctx); |
1290 | 0 | result = Curl_conn_send(data, FIRSTSOCKET, buffer, buflen, FALSE, n); |
1291 | 0 | sigpipe_restore(&sigpipe_ctx); |
1292 | |
|
1293 | 0 | if(result && result != CURLE_AGAIN) |
1294 | 0 | return CURLE_SEND_ERROR; |
1295 | 0 | return result; |
1296 | 0 | } |
1297 | | |
1298 | | /* |
1299 | | * Sends data over the connected socket. Use after successful |
1300 | | * curl_easy_perform() with CURLOPT_CONNECT_ONLY option. |
1301 | | */ |
1302 | | CURLcode curl_easy_send(CURL *d, const void *buffer, size_t buflen, size_t *n) |
1303 | 0 | { |
1304 | 0 | size_t written = 0; |
1305 | 0 | CURLcode result; |
1306 | 0 | struct Curl_easy *data = d; |
1307 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1308 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1309 | 0 | if(Curl_is_in_callback(data)) |
1310 | 0 | return CURLE_RECURSIVE_API_CALL; |
1311 | | |
1312 | 0 | result = Curl_senddata(data, buffer, buflen, &written); |
1313 | 0 | *n = written; |
1314 | 0 | return result; |
1315 | 0 | } |
1316 | | |
1317 | | /* |
1318 | | * Performs connection upkeep for the given session handle. |
1319 | | */ |
1320 | | CURLcode curl_easy_upkeep(CURL *d) |
1321 | 0 | { |
1322 | 0 | struct Curl_easy *data = d; |
1323 | | /* Verify that we got an easy handle we can work with. */ |
1324 | 0 | if(!GOOD_EASY_HANDLE(data)) |
1325 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
1326 | | |
1327 | 0 | if(Curl_is_in_callback(data)) |
1328 | 0 | return CURLE_RECURSIVE_API_CALL; |
1329 | | |
1330 | | /* Use the common function to keep connections alive. */ |
1331 | 0 | return Curl_cpool_upkeep(data); |
1332 | 0 | } |
1333 | | |
1334 | | CURLcode curl_easy_ssls_import(CURL *d, const char *session_key, |
1335 | | const unsigned char *shmac, size_t shmac_len, |
1336 | | const unsigned char *sdata, size_t sdata_len) |
1337 | 0 | { |
1338 | | #if defined(USE_SSL) && defined(USE_SSLS_EXPORT) |
1339 | | struct Curl_easy *data = d; |
1340 | | if(!GOOD_EASY_HANDLE(data)) |
1341 | | return CURLE_BAD_FUNCTION_ARGUMENT; |
1342 | | return Curl_ssl_session_import(data, session_key, |
1343 | | shmac, shmac_len, sdata, sdata_len); |
1344 | | #else |
1345 | 0 | (void)d; |
1346 | 0 | (void)session_key; |
1347 | 0 | (void)shmac; |
1348 | 0 | (void)shmac_len; |
1349 | 0 | (void)sdata; |
1350 | 0 | (void)sdata_len; |
1351 | 0 | return CURLE_NOT_BUILT_IN; |
1352 | 0 | #endif |
1353 | 0 | } |
1354 | | |
1355 | | CURLcode curl_easy_ssls_export(CURL *d, |
1356 | | curl_ssls_export_cb *export_fn, |
1357 | | void *userptr) |
1358 | 0 | { |
1359 | | #if defined(USE_SSL) && defined(USE_SSLS_EXPORT) |
1360 | | struct Curl_easy *data = d; |
1361 | | if(!GOOD_EASY_HANDLE(data)) |
1362 | | return CURLE_BAD_FUNCTION_ARGUMENT; |
1363 | | return Curl_ssl_session_export(data, export_fn, userptr); |
1364 | | #else |
1365 | 0 | (void)d; |
1366 | 0 | (void)export_fn; |
1367 | 0 | (void)userptr; |
1368 | 0 | return CURLE_NOT_BUILT_IN; |
1369 | 0 | #endif |
1370 | 0 | } |
1371 | | |
1372 | | CURLcode Curl_meta_set(struct Curl_easy *data, const char *key, |
1373 | | void *meta_data, Curl_meta_dtor *meta_dtor) |
1374 | 0 | { |
1375 | 0 | DEBUGASSERT(meta_data); /* never set to NULL */ |
1376 | 0 | if(!Curl_hash_add2(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1, |
1377 | 0 | meta_data, meta_dtor)) { |
1378 | 0 | meta_dtor(CURL_UNCONST(key), strlen(key) + 1, meta_data); |
1379 | 0 | return CURLE_OUT_OF_MEMORY; |
1380 | 0 | } |
1381 | 0 | return CURLE_OK; |
1382 | 0 | } |
1383 | | |
1384 | | void Curl_meta_remove(struct Curl_easy *data, const char *key) |
1385 | 0 | { |
1386 | 0 | Curl_hash_delete(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1); |
1387 | 0 | } |
1388 | | |
1389 | | void *Curl_meta_get(struct Curl_easy *data, const char *key) |
1390 | 0 | { |
1391 | 0 | return Curl_hash_pick(&data->meta_hash, CURL_UNCONST(key), strlen(key) + 1); |
1392 | 0 | } |
1393 | | |
1394 | | void Curl_meta_reset(struct Curl_easy *data) |
1395 | 0 | { |
1396 | 0 | Curl_hash_clean(&data->meta_hash); |
1397 | 0 | } |