share.c 6.0 KB

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