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