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