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