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