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