share.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "urldata.h"
  25. #include "share.h"
  26. #include "vtls/vtls.h"
  27. #include "curl_memory.h"
  28. /* The last #include file should be: */
  29. #include "memdebug.h"
  30. CURLSH *
  31. curl_share_init(void)
  32. {
  33. struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
  34. if(share)
  35. share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
  36. return share;
  37. }
  38. #undef curl_share_setopt
  39. CURLSHcode
  40. curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
  41. {
  42. struct Curl_share *share = (struct Curl_share *)sh;
  43. va_list param;
  44. int type;
  45. curl_lock_function lockfunc;
  46. curl_unlock_function unlockfunc;
  47. void *ptr;
  48. CURLSHcode res = CURLSHE_OK;
  49. if(share->dirty)
  50. /* don't allow setting options while one or more handles are already
  51. using this share */
  52. return CURLSHE_IN_USE;
  53. va_start(param, option);
  54. switch(option) {
  55. case CURLSHOPT_SHARE:
  56. /* this is a type this share will share */
  57. type = va_arg(param, int);
  58. share->specifier |= (1<<type);
  59. switch( type ) {
  60. case CURL_LOCK_DATA_DNS:
  61. if(!share->hostcache) {
  62. share->hostcache = Curl_mk_dnscache();
  63. if(!share->hostcache)
  64. res = CURLSHE_NOMEM;
  65. }
  66. break;
  67. case CURL_LOCK_DATA_COOKIE:
  68. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  69. if(!share->cookies) {
  70. share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
  71. if(!share->cookies)
  72. res = CURLSHE_NOMEM;
  73. }
  74. #else /* CURL_DISABLE_HTTP */
  75. res = CURLSHE_NOT_BUILT_IN;
  76. #endif
  77. break;
  78. case CURL_LOCK_DATA_SSL_SESSION:
  79. #ifdef USE_SSL
  80. if(!share->sslsession) {
  81. share->max_ssl_sessions = 8;
  82. share->sslsession = calloc(share->max_ssl_sessions,
  83. sizeof(struct curl_ssl_session));
  84. share->sessionage = 0;
  85. if(!share->sslsession)
  86. res = CURLSHE_NOMEM;
  87. }
  88. #else
  89. res = CURLSHE_NOT_BUILT_IN;
  90. #endif
  91. break;
  92. case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
  93. break;
  94. default:
  95. res = CURLSHE_BAD_OPTION;
  96. }
  97. break;
  98. case CURLSHOPT_UNSHARE:
  99. /* this is a type this share will no longer share */
  100. type = va_arg(param, int);
  101. share->specifier &= ~(1<<type);
  102. switch( type ) {
  103. case CURL_LOCK_DATA_DNS:
  104. if(share->hostcache) {
  105. Curl_hash_destroy(share->hostcache);
  106. share->hostcache = NULL;
  107. }
  108. break;
  109. case CURL_LOCK_DATA_COOKIE:
  110. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  111. if(share->cookies) {
  112. Curl_cookie_cleanup(share->cookies);
  113. share->cookies = NULL;
  114. }
  115. #else /* CURL_DISABLE_HTTP */
  116. res = CURLSHE_NOT_BUILT_IN;
  117. #endif
  118. break;
  119. case CURL_LOCK_DATA_SSL_SESSION:
  120. #ifdef USE_SSL
  121. Curl_safefree(share->sslsession);
  122. #else
  123. res = CURLSHE_NOT_BUILT_IN;
  124. #endif
  125. break;
  126. case CURL_LOCK_DATA_CONNECT:
  127. break;
  128. default:
  129. res = CURLSHE_BAD_OPTION;
  130. break;
  131. }
  132. break;
  133. case CURLSHOPT_LOCKFUNC:
  134. lockfunc = va_arg(param, curl_lock_function);
  135. share->lockfunc = lockfunc;
  136. break;
  137. case CURLSHOPT_UNLOCKFUNC:
  138. unlockfunc = va_arg(param, curl_unlock_function);
  139. share->unlockfunc = unlockfunc;
  140. break;
  141. case CURLSHOPT_USERDATA:
  142. ptr = va_arg(param, void *);
  143. share->clientdata = ptr;
  144. break;
  145. default:
  146. res = CURLSHE_BAD_OPTION;
  147. break;
  148. }
  149. va_end(param);
  150. return res;
  151. }
  152. CURLSHcode
  153. curl_share_cleanup(CURLSH *sh)
  154. {
  155. struct Curl_share *share = (struct Curl_share *)sh;
  156. if(share == NULL)
  157. return CURLSHE_INVALID;
  158. if(share->lockfunc)
  159. share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
  160. share->clientdata);
  161. if(share->dirty) {
  162. if(share->unlockfunc)
  163. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  164. return CURLSHE_IN_USE;
  165. }
  166. if(share->hostcache) {
  167. Curl_hash_destroy(share->hostcache);
  168. share->hostcache = NULL;
  169. }
  170. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  171. if(share->cookies)
  172. Curl_cookie_cleanup(share->cookies);
  173. #endif
  174. #ifdef USE_SSL
  175. if(share->sslsession) {
  176. size_t i;
  177. for(i = 0; i < share->max_ssl_sessions; i++)
  178. Curl_ssl_kill_session(&(share->sslsession[i]));
  179. free(share->sslsession);
  180. }
  181. #endif
  182. if(share->unlockfunc)
  183. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  184. free(share);
  185. return CURLSHE_OK;
  186. }
  187. CURLSHcode
  188. Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
  189. curl_lock_access accesstype)
  190. {
  191. struct Curl_share *share = data->share;
  192. if(share == NULL)
  193. return CURLSHE_INVALID;
  194. if(share->specifier & (1<<type)) {
  195. if(share->lockfunc) /* only call this if set! */
  196. share->lockfunc(data, type, accesstype, share->clientdata);
  197. }
  198. /* else if we don't share this, pretend successful lock */
  199. return CURLSHE_OK;
  200. }
  201. CURLSHcode
  202. Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
  203. {
  204. struct Curl_share *share = data->share;
  205. if(share == NULL)
  206. return CURLSHE_INVALID;
  207. if(share->specifier & (1<<type)) {
  208. if(share->unlockfunc) /* only call this if set! */
  209. share->unlockfunc (data, type, share->clientdata);
  210. }
  211. return CURLSHE_OK;
  212. }