easy.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 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: easy.c,v 1.49 2004/03/15 16:28:36 bagder Exp $
  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
  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. #define _MPRINTF_REPLACE /* use our functions only */
  71. #include <curl/mprintf.h>
  72. /* The last #include file should be: */
  73. #ifdef CURLDEBUG
  74. #include "memdebug.h"
  75. #endif
  76. /* Silly win32 socket initialization functions */
  77. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  78. static void win32_cleanup(void)
  79. {
  80. WSACleanup();
  81. }
  82. static CURLcode win32_init(void)
  83. {
  84. WORD wVersionRequested;
  85. WSADATA wsaData;
  86. int err;
  87. #ifdef ENABLE_IPV6
  88. wVersionRequested = MAKEWORD(2, 0);
  89. #else
  90. wVersionRequested = MAKEWORD(1, 1);
  91. #endif
  92. err = WSAStartup(wVersionRequested, &wsaData);
  93. if (err != 0)
  94. /* Tell the user that we couldn't find a useable */
  95. /* winsock.dll. */
  96. return CURLE_FAILED_INIT;
  97. /* Confirm that the Windows Sockets DLL supports what we need.*/
  98. /* Note that if the DLL supports versions greater */
  99. /* than wVersionRequested, it will still return */
  100. /* wVersionRequested in wVersion. wHighVersion contains the */
  101. /* highest supported version. */
  102. if ( LOBYTE( wsaData.wVersion ) != LOBYTE(wVersionRequested) ||
  103. HIBYTE( wsaData.wVersion ) != HIBYTE(wVersionRequested) ) {
  104. /* Tell the user that we couldn't find a useable */
  105. /* winsock.dll. */
  106. WSACleanup();
  107. return CURLE_FAILED_INIT;
  108. }
  109. return CURLE_OK;
  110. }
  111. /* The Windows Sockets DLL is acceptable. Proceed. */
  112. #else
  113. /* These functions exist merely to prevent compiler warnings */
  114. static CURLcode win32_init(void) { return CURLE_OK; }
  115. static void win32_cleanup(void) { }
  116. #endif
  117. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  118. static unsigned int initialized = 0;
  119. static long init_flags = 0;
  120. /**
  121. * Globally initializes cURL given a bitwise set of
  122. * the different features to initialize.
  123. */
  124. CURLcode curl_global_init(long flags)
  125. {
  126. if (initialized)
  127. return CURLE_OK;
  128. if (flags & CURL_GLOBAL_SSL)
  129. Curl_SSL_init();
  130. if (flags & CURL_GLOBAL_WIN32)
  131. if (win32_init() != CURLE_OK)
  132. return CURLE_FAILED_INIT;
  133. #ifdef _AMIGASF
  134. if(!amiga_init())
  135. return CURLE_FAILED_INIT;
  136. #endif
  137. initialized = 1;
  138. init_flags = flags;
  139. return CURLE_OK;
  140. }
  141. /**
  142. * Globally cleanup cURL, uses the value of "init_flags" to determine
  143. * what needs to be cleaned up and what doesn't
  144. */
  145. void curl_global_cleanup(void)
  146. {
  147. if (!initialized)
  148. return;
  149. Curl_global_host_cache_dtor();
  150. if (init_flags & CURL_GLOBAL_SSL)
  151. Curl_SSL_cleanup();
  152. if (init_flags & CURL_GLOBAL_WIN32)
  153. win32_cleanup();
  154. #ifdef _AMIGASF
  155. amiga_cleanup();
  156. #endif
  157. initialized = 0;
  158. init_flags = 0;
  159. }
  160. CURL *curl_easy_init(void)
  161. {
  162. CURLcode res;
  163. struct SessionHandle *data;
  164. /* Make sure we inited the global SSL stuff */
  165. if (!initialized) {
  166. res = curl_global_init(CURL_GLOBAL_DEFAULT);
  167. if(res)
  168. /* something in the global init failed, return nothing */
  169. return NULL;
  170. }
  171. /* We use curl_open() with undefined URL so far */
  172. res = Curl_open(&data);
  173. if(res != CURLE_OK)
  174. return NULL;
  175. return data;
  176. }
  177. typedef int (*func_T)(void);
  178. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  179. {
  180. va_list arg;
  181. func_T param_func = (func_T)0;
  182. long param_long = 0;
  183. void *param_obj = NULL;
  184. curl_off_t param_offset = 0;
  185. struct SessionHandle *data = curl;
  186. CURLcode ret=CURLE_FAILED_INIT;
  187. va_start(arg, tag);
  188. /* PORTING NOTE:
  189. Object pointers can't necessarily be casted to function pointers and
  190. therefore we need to know what type it is and read the correct type
  191. at once. This should also correct problems with different sizes of
  192. the types.
  193. */
  194. if(tag < CURLOPTTYPE_OBJECTPOINT) {
  195. /* This is a LONG type */
  196. param_long = va_arg(arg, long);
  197. ret = Curl_setopt(data, tag, param_long);
  198. }
  199. else if(tag < CURLOPTTYPE_FUNCTIONPOINT) {
  200. /* This is a object pointer type */
  201. param_obj = va_arg(arg, void *);
  202. ret = Curl_setopt(data, tag, param_obj);
  203. }
  204. else if(tag < CURLOPTTYPE_OFF_T) {
  205. /* This is a function pointer type */
  206. param_func = va_arg(arg, func_T );
  207. ret = Curl_setopt(data, tag, param_func);
  208. } else {
  209. /* This is a curl_off_t type */
  210. param_offset = va_arg(arg, curl_off_t);
  211. ret = Curl_setopt(data, tag, param_offset);
  212. }
  213. va_end(arg);
  214. return ret;
  215. }
  216. CURLcode curl_easy_perform(CURL *curl)
  217. {
  218. struct SessionHandle *data = (struct SessionHandle *)curl;
  219. if ( ! (data->share && data->share->hostcache) ) {
  220. if (Curl_global_host_cache_use(data) &&
  221. data->hostcache != Curl_global_host_cache_get()) {
  222. if (data->hostcache)
  223. Curl_hash_destroy(data->hostcache);
  224. data->hostcache = Curl_global_host_cache_get();
  225. }
  226. if (!data->hostcache) {
  227. data->hostcache = Curl_hash_alloc(7, Curl_freednsinfo);
  228. if(!data->hostcache)
  229. /* While we possibly could survive and do good without a host cache,
  230. the fact that creating it failed indicates that things are truly
  231. screwed up and we should bail out! */
  232. return CURLE_OUT_OF_MEMORY;
  233. }
  234. }
  235. return Curl_perform(data);
  236. }
  237. void curl_easy_cleanup(CURL *curl)
  238. {
  239. struct SessionHandle *data = (struct SessionHandle *)curl;
  240. if ( ! (data->share && data->share->hostcache) ) {
  241. if ( !Curl_global_host_cache_use(data)) {
  242. Curl_hash_destroy(data->hostcache);
  243. }
  244. }
  245. Curl_close(data);
  246. }
  247. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  248. {
  249. va_list arg;
  250. void *paramp;
  251. struct SessionHandle *data = (struct SessionHandle *)curl;
  252. va_start(arg, info);
  253. paramp = va_arg(arg, void *);
  254. return Curl_getinfo(data, info, paramp);
  255. }
  256. CURL *curl_easy_duphandle(CURL *incurl)
  257. {
  258. struct SessionHandle *data=(struct SessionHandle *)incurl;
  259. struct SessionHandle *outcurl = (struct SessionHandle *)
  260. malloc(sizeof(struct SessionHandle));
  261. if(NULL == outcurl)
  262. return NULL; /* failure */
  263. /* start with clearing the entire new struct */
  264. memset(outcurl, 0, sizeof(struct SessionHandle));
  265. /*
  266. * We setup a few buffers we need. We should probably make them
  267. * get setup on-demand in the code, as that would probably decrease
  268. * the likeliness of us forgetting to init a buffer here in the future.
  269. */
  270. outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
  271. if(!outcurl->state.headerbuff) {
  272. free(outcurl); /* free the memory again */
  273. return NULL;
  274. }
  275. outcurl->state.headersize=HEADERSIZE;
  276. /* copy all userdefined values */
  277. outcurl->set = data->set;
  278. outcurl->state.numconnects = data->state.numconnects;
  279. outcurl->state.connects = (struct connectdata **)
  280. malloc(sizeof(struct connectdata *) * outcurl->state.numconnects);
  281. if(!outcurl->state.connects) {
  282. free(outcurl->state.headerbuff);
  283. free(outcurl);
  284. return NULL;
  285. }
  286. memset(outcurl->state.connects, 0,
  287. sizeof(struct connectdata *)*outcurl->state.numconnects);
  288. outcurl->progress.flags = data->progress.flags;
  289. outcurl->progress.callback = data->progress.callback;
  290. if(data->cookies)
  291. /* If cookies are enabled in the parent handle, we enable them
  292. in the clone as well! */
  293. outcurl->cookies = Curl_cookie_init(data,
  294. data->cookies->filename,
  295. outcurl->cookies,
  296. data->set.cookiesession);
  297. /* duplicate all values in 'change' */
  298. if(data->change.url) {
  299. outcurl->change.url = strdup(data->change.url);
  300. outcurl->change.url_alloc = TRUE;
  301. }
  302. if(data->change.proxy) {
  303. outcurl->change.proxy = strdup(data->change.proxy);
  304. outcurl->change.proxy_alloc = TRUE;
  305. }
  306. if(data->change.referer) {
  307. outcurl->change.referer = strdup(data->change.referer);
  308. outcurl->change.referer_alloc = TRUE;
  309. }
  310. return outcurl;
  311. }