easy.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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 <winsock.h>
  36. #include <time.h>
  37. #include <io.h>
  38. #else
  39. #ifdef HAVE_SYS_SOCKET_H
  40. #include <sys/socket.h>
  41. #endif
  42. #include <netinet/in.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #ifdef HAVE_UNISTD_H
  46. #include <unistd.h>
  47. #endif
  48. #include <netdb.h>
  49. #ifdef HAVE_ARPA_INET_H
  50. #include <arpa/inet.h>
  51. #endif
  52. #ifdef HAVE_NET_IF_H
  53. #include <net/if.h>
  54. #endif
  55. #include <sys/ioctl.h>
  56. #include <signal.h>
  57. #ifdef HAVE_SYS_PARAM_H
  58. #include <sys/param.h>
  59. #endif
  60. #ifdef HAVE_SYS_SELECT_H
  61. #include <sys/select.h>
  62. #endif
  63. #endif
  64. #include "urldata.h"
  65. #include <curl/curl.h>
  66. #include "transfer.h"
  67. #include "ssluse.h"
  68. #include "url.h"
  69. #include "getinfo.h"
  70. #include "hostip.h"
  71. #define _MPRINTF_REPLACE /* use our functions only */
  72. #include <curl/mprintf.h>
  73. /* Silly win32 socket initialization functions */
  74. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  75. static void win32_cleanup(void)
  76. {
  77. WSACleanup();
  78. }
  79. static CURLcode win32_init(void)
  80. {
  81. WORD wVersionRequested;
  82. WSADATA wsaData;
  83. int err;
  84. wVersionRequested = MAKEWORD(2, 0);
  85. err = WSAStartup(wVersionRequested, &wsaData);
  86. if (err != 0)
  87. /* Tell the user that we couldn't find a useable */
  88. /* winsock.dll. */
  89. return CURLE_FAILED_INIT;
  90. /* Confirm that the Windows Sockets DLL supports 2.0.*/
  91. /* Note that if the DLL supports versions greater */
  92. /* than 2.0 in addition to 2.0, it will still return */
  93. /* 2.0 in wVersion since that is the version we */
  94. /* requested. */
  95. if ( LOBYTE( wsaData.wVersion ) != 2 ||
  96. HIBYTE( wsaData.wVersion ) != 0 ) {
  97. /* Tell the user that we couldn't find a useable */
  98. /* winsock.dll. */
  99. WSACleanup();
  100. return CURLE_FAILED_INIT;
  101. }
  102. return CURLE_OK;
  103. }
  104. /* The Windows Sockets DLL is acceptable. Proceed. */
  105. #else
  106. /* These functions exist merely to prevent compiler warnings */
  107. static CURLcode win32_init(void) { return CURLE_OK; }
  108. static void win32_cleanup(void) { }
  109. #endif
  110. /* true globals -- for curl_global_init() and curl_global_cleanup() */
  111. static unsigned int initialized = 0;
  112. static long init_flags = 0;
  113. /**
  114. * Globally initializes cURL given a bitwise set of
  115. * the different features to initialize.
  116. */
  117. CURLcode curl_global_init(long flags)
  118. {
  119. if (initialized)
  120. return CURLE_OK;
  121. if (flags & CURL_GLOBAL_SSL)
  122. Curl_SSL_init();
  123. if (flags & CURL_GLOBAL_WIN32)
  124. if (win32_init() != CURLE_OK)
  125. return CURLE_FAILED_INIT;
  126. initialized = 1;
  127. init_flags = flags;
  128. return CURLE_OK;
  129. }
  130. /**
  131. * Globally cleanup cURL, uses the value of "init_flags" to determine
  132. * what needs to be cleaned up and what doesn't
  133. */
  134. void curl_global_cleanup(void)
  135. {
  136. if (!initialized)
  137. return;
  138. Curl_global_host_cache_dtor();
  139. if (init_flags & CURL_GLOBAL_SSL)
  140. Curl_SSL_cleanup();
  141. if (init_flags & CURL_GLOBAL_WIN32)
  142. win32_cleanup();
  143. initialized = 0;
  144. init_flags = 0;
  145. }
  146. CURL *curl_easy_init(void)
  147. {
  148. CURLcode res;
  149. struct SessionHandle *data;
  150. /* Make sure we inited the global SSL stuff */
  151. if (!initialized)
  152. curl_global_init(CURL_GLOBAL_DEFAULT);
  153. /* We use curl_open() with undefined URL so far */
  154. res = Curl_open(&data);
  155. if(res != CURLE_OK)
  156. return NULL;
  157. return data;
  158. }
  159. typedef int (*func_T)(void);
  160. CURLcode curl_easy_setopt(CURL *curl, CURLoption tag, ...)
  161. {
  162. va_list arg;
  163. func_T param_func = (func_T)0;
  164. long param_long = 0;
  165. void *param_obj = NULL;
  166. struct SessionHandle *data = curl;
  167. va_start(arg, tag);
  168. /* PORTING NOTE:
  169. Object pointers can't necessarily be casted to function pointers and
  170. therefore we need to know what type it is and read the correct type
  171. at once. This should also correct problems with different sizes of
  172. the types.
  173. */
  174. if(tag < CURLOPTTYPE_OBJECTPOINT) {
  175. /* This is a LONG type */
  176. param_long = va_arg(arg, long);
  177. Curl_setopt(data, tag, param_long);
  178. }
  179. else if(tag < CURLOPTTYPE_FUNCTIONPOINT) {
  180. /* This is a object pointer type */
  181. param_obj = va_arg(arg, void *);
  182. Curl_setopt(data, tag, param_obj);
  183. }
  184. else {
  185. param_func = va_arg(arg, func_T );
  186. Curl_setopt(data, tag, param_func);
  187. }
  188. va_end(arg);
  189. return CURLE_OK;
  190. }
  191. CURLcode curl_easy_perform(CURL *curl)
  192. {
  193. struct SessionHandle *data = (struct SessionHandle *)curl;
  194. if (Curl_global_host_cache_use(data) && data->hostcache != Curl_global_host_cache_get()) {
  195. if (data->hostcache) {
  196. Curl_hash_destroy(data->hostcache);
  197. }
  198. data->hostcache = Curl_global_host_cache_get();
  199. }
  200. if (!data->hostcache) {
  201. data->hostcache = Curl_hash_alloc(7, Curl_freednsinfo);
  202. }
  203. return Curl_perform(data);
  204. }
  205. void curl_easy_cleanup(CURL *curl)
  206. {
  207. struct SessionHandle *data = (struct SessionHandle *)curl;
  208. if (!Curl_global_host_cache_use(data)) {
  209. Curl_hash_destroy(data->hostcache);
  210. }
  211. Curl_close(data);
  212. }
  213. CURLcode curl_easy_getinfo(CURL *curl, CURLINFO info, ...)
  214. {
  215. va_list arg;
  216. void *paramp;
  217. struct SessionHandle *data = (struct SessionHandle *)curl;
  218. va_start(arg, info);
  219. paramp = va_arg(arg, void *);
  220. return Curl_getinfo(data, info, paramp);
  221. }
  222. CURL *curl_easy_duphandle(CURL *incurl)
  223. {
  224. struct SessionHandle *data=(struct SessionHandle *)incurl;
  225. struct SessionHandle *outcurl = (struct SessionHandle *)
  226. malloc(sizeof(struct SessionHandle));
  227. if(NULL == outcurl)
  228. return NULL; /* failure */
  229. /* start with clearing the entire new struct */
  230. memset(outcurl, 0, sizeof(struct SessionHandle));
  231. /*
  232. * We setup a few buffers we need. We should probably make them
  233. * get setup on-demand in the code, as that would probably decrease
  234. * the likeliness of us forgetting to init a buffer here in the future.
  235. */
  236. outcurl->state.headerbuff=(char*)malloc(HEADERSIZE);
  237. if(!outcurl->state.headerbuff) {
  238. free(outcurl); /* free the memory again */
  239. return NULL;
  240. }
  241. outcurl->state.headersize=HEADERSIZE;
  242. /* copy all userdefined values */
  243. outcurl->set = data->set;
  244. outcurl->state.numconnects = data->state.numconnects;
  245. outcurl->state.connects = (struct connectdata **)
  246. malloc(sizeof(struct connectdata *) * outcurl->state.numconnects);
  247. if(!outcurl->state.connects) {
  248. free(outcurl->state.headerbuff);
  249. free(outcurl);
  250. return NULL;
  251. }
  252. memset(outcurl->state.connects, 0,
  253. sizeof(struct connectdata *)*outcurl->state.numconnects);
  254. outcurl->progress.flags = data->progress.flags;
  255. outcurl->progress.callback = data->progress.callback;
  256. if(data->cookies)
  257. /* If cookies are enabled in the parent handle, we enable them
  258. in the clone as well! */
  259. outcurl->cookies = Curl_cookie_init(data->cookies->filename,
  260. outcurl->cookies,
  261. data->set.cookiesession);
  262. /* duplicate all values in 'change' */
  263. if(data->change.url) {
  264. outcurl->change.url = strdup(data->change.url);
  265. outcurl->change.url_alloc = TRUE;
  266. }
  267. if(data->change.proxy) {
  268. outcurl->change.proxy = strdup(data->change.proxy);
  269. outcurl->change.proxy_alloc = TRUE;
  270. }
  271. if(data->change.referer) {
  272. outcurl->change.referer = strdup(data->change.referer);
  273. outcurl->change.referer_alloc = TRUE;
  274. }
  275. return outcurl;
  276. }
  277. /*
  278. * local variables:
  279. * eval: (load-file "../curl-mode.el")
  280. * end:
  281. * vim600: fdm=marker
  282. * vim: et sw=2 ts=2 sts=2 tw=78
  283. */