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