2
0

share.c 6.9 KB

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