share.c 6.2 KB

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