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