share.c 6.3 KB

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