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