share.c 7.1 KB

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