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