easy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 http://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. * $Id$
  22. ***************************************************************************/
  23. #include "setup.h"
  24. /* -- WIN32 approved -- */
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <stdarg.h>
  28. #include <stdlib.h>
  29. #include <ctype.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32. #include <errno.h>
  33. #include "strequal.h"
  34. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  35. #include <time.h>
  36. #include <io.h>
  37. #else
  38. #ifdef HAVE_SYS_SOCKET_H
  39. #include <sys/socket.h>
  40. #endif
  41. #include <netinet/in.h>
  42. #include <sys/time.h>
  43. #ifdef HAVE_UNISTD_H
  44. #include <unistd.h>
  45. #endif
  46. #include <netdb.h>
  47. #ifdef HAVE_ARPA_INET_H
  48. #include <arpa/inet.h>
  49. #endif
  50. #ifdef HAVE_NET_IF_H
  51. #include <net/if.h>
  52. #endif
  53. #include <sys/ioctl.h>
  54. #include <signal.h>
  55. #ifdef HAVE_SYS_PARAM_H
  56. #include <sys/param.h>
  57. #endif
  58. #ifdef HAVE_SYS_SELECT_H
  59. #include <sys/select.h>
  60. #endif
  61. #endif /* WIN32 ... */
  62. #include "urldata.h"
  63. #include <curl/curl.h>
  64. #include "transfer.h"
  65. #include "ssluse.h"
  66. #include "url.h"
  67. #include "getinfo.h"
  68. #include "hostip.h"
  69. #include "share.h"
  70. #include "curl_memory.h"
  71. #define _MPRINTF_REPLACE /* use our functions only */
  72. #include <curl/mprintf.h>
  73. /* The last #include file should be: */
  74. #include "memdebug.h"
  75. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  76. /* win32_cleanup() is for win32 socket cleanup functionality, the opposite
  77. of win32_init() */
  78. static void win32_cleanup(void)
  79. {
  80. WSACleanup();
  81. }
  82. /* win32_init() performs win32 socket initialization to properly setup the
  83. stack to allow networking */
  84. static CURLcode win32_init(void)
  85. {
  86. WORD wVersionRequested;
  87. WSADATA wsaData;
  88. int err;
  89. #ifdef ENABLE_IPV6
  90. wVersionRequested = MAKEWORD(2, 0);
  91. #else
  92. wVersionRequested = MAKEWORD(1, 1);
  93. #endif
  94. err = WSAStartup(wVersionRequested, &wsaData);
  95. if (err != 0)
  96. /* Tell the user that we couldn't find a useable */
  97. /* winsock.dll. */
  98. return CURLE_FAILED_INIT;
  99. /* Confirm that the Windows Sockets DLL supports what we need.*/
  100. /* Note that if the DLL supports versions greater */
  101. /* than wVersionRequested, it will still return */
  102. /* wVersionRequested in wVersion. wHighVersion contains the */
  103. /* highest supported version. */
  104. if ( LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
  105. HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
  106. /* Tell the user that we couldn't find a useable */
  107. /* winsock.dll. */
  108. WSACleanup();
  109. return CURLE_FAILED_INIT;
  110. }
  111. /* The Windows Sockets DLL is acceptable. Proceed. */
  112. return CURLE_OK;
  113. }
  114. #else
  115. /* These functions exist merely to prevent compiler warnings */
  116. static CURLcode win32_init(void) { return CURLE_OK; }
  117. static void win32_cleanup(void) { }
  118. #endif
  119. #ifdef USE_LIBIDN
  120. /*
  121. * Initialise use of IDNA library.
  122. * It falls back to ASCII if $CHARSET isn't defined. This doesn't work for
  123. * idna_to_ascii_lz().
  124. */
  125. static void idna_init (void)
  126. {
  127. #ifdef WIN32
  128. char buf[60];
  129. UINT cp = GetACP();
  130. if (!getenv("CHARSET") && cp > 0) {
  131. snprintf(buf, sizeof(buf), "CHARSET=cp%u", cp);
  132. putenv(buf);
  133. }
  134. #else
  135. /* to do? */
  136. #endif
  137. }
  138. #endif /* USE_LIBIDN */
  139. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  140. static unsigned int initialized = 0;
  141. static long init_flags = 0;
  142. /*
  143. * If a memory-using function (like curl_getenv) is used before
  144. * curl_global_init() is called, we need to have these pointers set already.
  145. */
  146. curl_malloc_callback Curl_cmalloc = (curl_malloc_callback)malloc;
  147. curl_free_callback Curl_cfree = (curl_free_callback)free;
  148. curl_realloc_callback Curl_crealloc = (curl_realloc_callback)realloc;
  149. curl_strdup_callback Curl_cstrdup = (curl_strdup_callback)strdup;
  150. curl_calloc_callback Curl_ccalloc = (curl_calloc_callback)calloc;
  151. /**
  152. * curl_global_init() globally initializes cURL given a bitwise set of the
  153. * different features of what to initialize.
  154. */
  155. CURLcode curl_global_init(long flags)
  156. {
  157. if (initialized)
  158. return CURLE_OK;
  159. /* Setup the default memory functions here (again) */
  160. Curl_cmalloc = (curl_malloc_callback)malloc;
  161. Curl_cfree = (curl_free_callback)free;
  162. Curl_crealloc = (curl_realloc_callback)realloc;
  163. Curl_cstrdup = (curl_strdup_callback)strdup;
  164. Curl_ccalloc = (curl_calloc_callback)calloc;
  165. if (flags & CURL_GLOBAL_SSL)
  166. Curl_SSL_init();
  167. if (flags & CURL_GLOBAL_WIN32)
  168. if (win32_init() != CURLE_OK)
  169. return CURLE_FAILED_INIT;
  170. #ifdef _AMIGASF
  171. if(!amiga_init())
  172. return CURLE_FAILED_INIT;
  173. #endif
  174. #ifdef USE_LIBIDN
  175. idna_init();
  176. #endif
  177. initialized = 1;
  178. init_flags = flags;
  179. return CURLE_OK;
  180. }
  181. /*
  182. * curl_global_init_mem() globally initializes cURL and also registers the
  183. * user provided callback routines.
  184. */
  185. CURLcode curl_global_init_mem(long flags, curl_malloc_callback m,
  186. curl_free_callback f, curl_realloc_callback r,
  187. curl_strdup_callback s, curl_calloc_callback c)
  188. {
  189. CURLcode code;
  190. /* Invalid input, return immediately */
  191. if (!m || !f || !r || !s || !c)
  192. return CURLE_FAILED_INIT;
  193. /* Already initialized, don't do it again */
  194. if ( initialized )
  195. return CURLE_OK;
  196. /* Call the actual init function first */
  197. code = curl_global_init(flags);
  198. if (code == CURLE_OK) {
  199. Curl_cmalloc = m;
  200. Curl_cfree = f;
  201. Curl_cstrdup = s;
  202. Curl_crealloc = r;
  203. Curl_ccalloc = c;
  204. }
  205. return code;
  206. }
  207. /**
  208. * curl_global_cleanup() globally cleanups cURL, uses the value of
  209. * "init_flags" to determine what needs to be cleaned up and what doesn't.
  210. */
  211. void curl_global_cleanup(void)
  212. {
  213. if (!initialized)
  214. return;
  215. Curl_global_host_cache_dtor();
  216. if (init_flags & CURL_GLOBAL_SSL)
  217. Curl_SSL_cleanup();
  218. if (init_flags & CURL_GLOBAL_WIN32)
  219. win32_cleanup();
  220. #ifdef _AMIGASF
  221. amiga_cleanup();
  222. #endif
  223. initialized = 0;
  224. init_flags = 0;
  225. }
  226. /*
  227. * curl_easy_init() is the external interface to alloc, setup and init an
  228. * easy handle that is returned. If anything goes wrong, NULL is returned.
  229. */
  230. CURL *curl_easy_init(void)
  231. {
  232. CURLcode res;
  233. struct SessionHandle *data;
  234. /* Make sure we inited the global SSL stuff */
  235. if (!initialized) {
  236. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  237. if(res)
  238. /* something in the global init failed, return nothing */
  239. return NULL;
  240. }
  241. /* We use curl_open() with undefined URL so far */
  242. res = Curl_open(&data);
  243. if(res != CURLE_OK)
  244. return NULL;
  245. return data;
  246. }
  247. /*
  248. * curl_easy_setopt() is the external interface for setting options on an
  249. * easy handle.
  250. */
  251. typedef int (*func_T)(void);
  252. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  253. {
  254. va_list arg;
  255. func_T param_func;
  256. long param_long;
  257. void *param_obj;
  258. curl_off_t param_offset;
  259. struct SessionHandle *data = curl;
  260. CURLcode ret;
  261. if(!curl)
  262. return CURLE_BAD_FUNCTION_ARGUMENT;
  263. va_start(arg, tag);
  264. /* PORTING NOTE:
  265. Object pointers can't necessarily be casted to function pointers and
  266. therefore we need to know what type it is and read the correct type
  267. at once. This should also correct problems with different sizes of
  268. the types.
  269. */
  270. if(tag < CURLOPTTYPE_OBJECTPOINT) {
  271. /* This is a LONG type */
  272. param_long = va_arg(arg, long);
  273. ret = Curl_setopt(data, tag, param_long);
  274. }
  275. else if(tag < CURLOPTTYPE_FUNCTIONPOINT) {
  276. /* This is a object pointer type */
  277. param_obj = va_arg(arg, void *);
  278. ret = Curl_setopt(data, tag, param_obj);
  279. }
  280. else if(tag < CURLOPTTYPE_OFF_T) {
  281. /* This is a function pointer type */
  282. param_func = va_arg(arg, func_T );
  283. ret = Curl_setopt(data, tag, param_func);
  284. }
  285. else {
  286. /* This is a curl_off_t type */
  287. param_offset = va_arg(arg, curl_off_t);
  288. ret = Curl_setopt(data, tag, param_offset);
  289. }
  290. va_end(arg);
  291. return ret;
  292. }
  293. /*
  294. * curl_easy_perform() is the external interface that performs a transfer
  295. * previously setup.
  296. */
  297. CURLcode curl_easy_perform(CURL *curl)
  298. {
  299. struct SessionHandle *data = (struct SessionHandle *)curl;
  300. if(!data)
  301. return CURLE_BAD_FUNCTION_ARGUMENT;
  302. if ( ! (data->share && data->share->hostcache) ) {
  303. if (Curl_global_host_cache_use(data) &&
  304. data->hostcache != Curl_global_host_cache_get()) {
  305. if (data->hostcache)
  306. Curl_hash_destroy(data->hostcache);
  307. data->hostcache = Curl_global_host_cache_get();
  308. }
  309. if (!data->hostcache) {
  310. data->hostcache = Curl_mk_dnscache();
  311. if(!data->hostcache)
  312. /* While we possibly could survive and do good without a host cache,
  313. the fact that creating it failed indicates that things are truly
  314. screwed up and we should bail out! */
  315. return CURLE_OUT_OF_MEMORY;
  316. }
  317. }
  318. return Curl_perform(data);
  319. }
  320. /*
  321. * curl_easy_cleanup() is the external interface to cleaning/freeing the given
  322. * easy handle.
  323. */
  324. void curl_easy_cleanup(CURL *curl)
  325. {
  326. struct SessionHandle *data = (struct SessionHandle *)curl;
  327. if(!data)
  328. return;
  329. if ( ! (data->share && data->share->hostcache) ) {
  330. if ( !Curl_global_host_cache_use(data)) {
  331. Curl_hash_destroy(data->hostcache);
  332. }
  333. }
  334. Curl_close(data);
  335. }
  336. /*
  337. * curl_easy_getinfo() is an external interface that allows an app to retrieve
  338. * information from a performed transfer and similar.
  339. */
  340. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  341. {
  342. va_list arg;
  343. void *paramp;
  344. struct SessionHandle *data = (struct SessionHandle *)curl;
  345. va_start(arg, info);
  346. paramp = va_arg(arg, void *);
  347. return Curl_getinfo(data, info, paramp);
  348. }
  349. /*
  350. * curl_easy_duphandle() is an external interface to allow duplication of a
  351. * given input easy handle. The returned handle will be a new working handle
  352. * with all options set exactly as the input source handle.
  353. */
  354. CURL *curl_easy_duphandle(CURL *incurl)
  355. {
  356. bool fail = TRUE;
  357. struct SessionHandle *data=(struct SessionHandle *)incurl;
  358. struct SessionHandle *outcurl = (struct SessionHandle *)
  359. calloc(sizeof(struct SessionHandle), 1);
  360. if(NULL == outcurl)
  361. return NULL; /* failure */
  362. do {
  363. /*
  364. * We setup a few buffers we need. We should probably make them
  365. * get setup on-demand in the code, as that would probably decrease
  366. * the likeliness of us forgetting to init a buffer here in the future.
  367. */
  368. outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
  369. if(!outcurl->state.headerbuff) {
  370. break;
  371. }
  372. outcurl->state.headersize=HEADERSIZE;
  373. /* copy all userdefined values */
  374. outcurl->set = data->set;
  375. outcurl->state.numconnects = data->state.numconnects;
  376. outcurl->state.connects = (struct connectdata **)
  377. malloc(sizeof(struct connectdata *) * outcurl->state.numconnects);
  378. if(!outcurl->state.connects) {
  379. break;
  380. }
  381. memset(outcurl->state.connects, 0,
  382. sizeof(struct connectdata *)*outcurl->state.numconnects);
  383. outcurl->progress.flags = data->progress.flags;
  384. outcurl->progress.callback = data->progress.callback;
  385. #ifndef CURL_DISABLE_HTTP
  386. if(data->cookies) {
  387. /* If cookies are enabled in the parent handle, we enable them
  388. in the clone as well! */
  389. outcurl->cookies = Curl_cookie_init(data,
  390. data->cookies->filename,
  391. outcurl->cookies,
  392. data->set.cookiesession);
  393. if(!outcurl->cookies) {
  394. break;
  395. }
  396. }
  397. #endif /* CURL_DISABLE_HTTP */
  398. /* duplicate all values in 'change' */
  399. if(data->change.url) {
  400. outcurl->change.url = strdup(data->change.url);
  401. if(!outcurl->change.url)
  402. break;
  403. outcurl->change.url_alloc = TRUE;
  404. }
  405. if(data->change.proxy) {
  406. outcurl->change.proxy = strdup(data->change.proxy);
  407. if(!outcurl->change.proxy)
  408. break;
  409. outcurl->change.proxy_alloc = TRUE;
  410. }
  411. if(data->change.referer) {
  412. outcurl->change.referer = strdup(data->change.referer);
  413. if(!outcurl->change.referer)
  414. break;
  415. outcurl->change.referer_alloc = TRUE;
  416. }
  417. #ifdef USE_ARES
  418. /* If we use ares, we setup a new ares channel for the new handle */
  419. if(ARES_SUCCESS != ares_init(&outcurl->state.areschannel))
  420. break;
  421. #endif
  422. fail = FALSE; /* we reach this point and thus we are OK */
  423. } while(0);
  424. if(fail) {
  425. if(outcurl) {
  426. if(outcurl->state.connects)
  427. free(outcurl->state.connects);
  428. if(outcurl->state.headerbuff)
  429. free(outcurl->state.headerbuff);
  430. if(outcurl->change.proxy)
  431. free(outcurl->change.proxy);
  432. if(outcurl->change.url)
  433. free(outcurl->change.url);
  434. if(outcurl->change.referer)
  435. free(outcurl->change.referer);
  436. free(outcurl); /* free the memory again */
  437. outcurl = NULL;
  438. }
  439. }
  440. return outcurl;
  441. }
  442. /*
  443. * curl_easy_reset() is an external interface that allows an app to re-
  444. * initialize a session handle to the default values.
  445. */
  446. void curl_easy_reset(CURL *curl)
  447. {
  448. struct SessionHandle *data = (struct SessionHandle *)curl;
  449. /* zero out UserDefined data: */
  450. memset(&data->set, 0, sizeof(struct UserDefined));
  451. /* zero out Progress data: */
  452. memset(&data->progress, 0, sizeof(struct Progress));
  453. /* The remainder of these calls have been taken from Curl_open() */
  454. data->set.out = stdout; /* default output to stdout */
  455. data->set.in = stdin; /* default input from stdin */
  456. data->set.err = stderr; /* default stderr to stderr */
  457. /* use fwrite as default function to store output */
  458. data->set.fwrite = (curl_write_callback)fwrite;
  459. /* use fread as default function to read input */
  460. data->set.fread = (curl_read_callback)fread;
  461. data->set.infilesize = -1; /* we don't know any size */
  462. data->state.current_speed = -1; /* init to negative == impossible */
  463. data->set.httpreq = HTTPREQ_GET; /* Default HTTP request */
  464. data->set.ftp_use_epsv = TRUE; /* FTP defaults to EPSV operations */
  465. data->set.ftp_use_eprt = TRUE; /* FTP defaults to EPRT operations */
  466. data->set.dns_cache_timeout = 60; /* Timeout every 60 seconds by default */
  467. /* make libcurl quiet by default: */
  468. data->set.hide_progress = TRUE; /* CURLOPT_NOPROGRESS changes these */
  469. /* Set the default size of the SSL session ID cache */
  470. data->set.ssl.numsessions = 5;
  471. data->set.proxyport = 1080;
  472. data->set.proxytype = CURLPROXY_HTTP; /* defaults to HTTP proxy */
  473. data->set.httpauth = CURLAUTH_BASIC; /* defaults to basic */
  474. data->set.proxyauth = CURLAUTH_BASIC; /* defaults to basic */
  475. /*
  476. * libcurl 7.10 introduced SSL verification *by default*! This needs to be
  477. * switched off unless wanted.
  478. */
  479. data->set.ssl.verifypeer = TRUE;
  480. data->set.ssl.verifyhost = 2;
  481. #ifdef CURL_CA_BUNDLE
  482. /* This is our prefered CA cert bundle since install time */
  483. data->set.ssl.CAfile = (char *)CURL_CA_BUNDLE;
  484. #endif
  485. }