share.c 7.1 KB

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