share.c 6.2 KB

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