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