share.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.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. break;
  93. default:
  94. res = CURLSHE_BAD_OPTION;
  95. }
  96. break;
  97. case CURLSHOPT_UNSHARE:
  98. /* this is a type this share will no longer share */
  99. type = va_arg(param, int);
  100. share->specifier &= ~(1<<type);
  101. switch(type) {
  102. case CURL_LOCK_DATA_DNS:
  103. break;
  104. case CURL_LOCK_DATA_COOKIE:
  105. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  106. if(share->cookies) {
  107. Curl_cookie_cleanup(share->cookies);
  108. share->cookies = NULL;
  109. }
  110. #else /* CURL_DISABLE_HTTP */
  111. res = CURLSHE_NOT_BUILT_IN;
  112. #endif
  113. break;
  114. case CURL_LOCK_DATA_SSL_SESSION:
  115. #ifdef USE_SSL
  116. Curl_safefree(share->sslsession);
  117. #else
  118. res = CURLSHE_NOT_BUILT_IN;
  119. #endif
  120. break;
  121. case CURL_LOCK_DATA_CONNECT:
  122. break;
  123. default:
  124. res = CURLSHE_BAD_OPTION;
  125. break;
  126. }
  127. break;
  128. case CURLSHOPT_LOCKFUNC:
  129. lockfunc = va_arg(param, curl_lock_function);
  130. share->lockfunc = lockfunc;
  131. break;
  132. case CURLSHOPT_UNLOCKFUNC:
  133. unlockfunc = va_arg(param, curl_unlock_function);
  134. share->unlockfunc = unlockfunc;
  135. break;
  136. case CURLSHOPT_USERDATA:
  137. ptr = va_arg(param, void *);
  138. share->clientdata = ptr;
  139. break;
  140. default:
  141. res = CURLSHE_BAD_OPTION;
  142. break;
  143. }
  144. va_end(param);
  145. return res;
  146. }
  147. CURLSHcode
  148. curl_share_cleanup(struct Curl_share *share)
  149. {
  150. if(share == NULL)
  151. return CURLSHE_INVALID;
  152. if(share->lockfunc)
  153. share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
  154. share->clientdata);
  155. if(share->dirty) {
  156. if(share->unlockfunc)
  157. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  158. return CURLSHE_IN_USE;
  159. }
  160. Curl_hash_destroy(&share->hostcache);
  161. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  162. Curl_cookie_cleanup(share->cookies);
  163. #endif
  164. #ifdef USE_SSL
  165. if(share->sslsession) {
  166. size_t i;
  167. for(i = 0; i < share->max_ssl_sessions; i++)
  168. Curl_ssl_kill_session(&(share->sslsession[i]));
  169. free(share->sslsession);
  170. }
  171. #endif
  172. if(share->unlockfunc)
  173. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  174. free(share);
  175. return CURLSHE_OK;
  176. }
  177. CURLSHcode
  178. Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
  179. curl_lock_access accesstype)
  180. {
  181. struct Curl_share *share = data->share;
  182. if(share == NULL)
  183. return CURLSHE_INVALID;
  184. if(share->specifier & (1<<type)) {
  185. if(share->lockfunc) /* only call this if set! */
  186. share->lockfunc(data, type, accesstype, share->clientdata);
  187. }
  188. /* else if we don't share this, pretend successful lock */
  189. return CURLSHE_OK;
  190. }
  191. CURLSHcode
  192. Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
  193. {
  194. struct Curl_share *share = data->share;
  195. if(share == NULL)
  196. return CURLSHE_INVALID;
  197. if(share->specifier & (1<<type)) {
  198. if(share->unlockfunc) /* only call this if set! */
  199. share->unlockfunc (data, type, share->clientdata);
  200. }
  201. return CURLSHE_OK;
  202. }