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