share.c 6.8 KB

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