share.c 6.3 KB

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