share.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2015, 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 http://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. CURLSH *
  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(CURLSH *sh, CURLSHoption option, ...)
  46. {
  47. struct Curl_share *share = (struct Curl_share *)sh;
  48. va_list param;
  49. int type;
  50. curl_lock_function lockfunc;
  51. curl_unlock_function unlockfunc;
  52. void *ptr;
  53. CURLSHcode res = CURLSHE_OK;
  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. share->specifier |= (1<<type);
  64. switch( type ) {
  65. case CURL_LOCK_DATA_DNS:
  66. break;
  67. case CURL_LOCK_DATA_COOKIE:
  68. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  69. if(!share->cookies) {
  70. share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
  71. if(!share->cookies)
  72. res = CURLSHE_NOMEM;
  73. }
  74. #else /* CURL_DISABLE_HTTP */
  75. res = CURLSHE_NOT_BUILT_IN;
  76. #endif
  77. break;
  78. case CURL_LOCK_DATA_SSL_SESSION:
  79. #ifdef USE_SSL
  80. if(!share->sslsession) {
  81. share->max_ssl_sessions = 8;
  82. share->sslsession = calloc(share->max_ssl_sessions,
  83. sizeof(struct curl_ssl_session));
  84. share->sessionage = 0;
  85. if(!share->sslsession)
  86. res = CURLSHE_NOMEM;
  87. }
  88. #else
  89. res = CURLSHE_NOT_BUILT_IN;
  90. #endif
  91. break;
  92. case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
  93. break;
  94. default:
  95. res = CURLSHE_BAD_OPTION;
  96. }
  97. break;
  98. case CURLSHOPT_UNSHARE:
  99. /* this is a type this share will no longer share */
  100. type = va_arg(param, int);
  101. share->specifier &= ~(1<<type);
  102. switch( type ) {
  103. case CURL_LOCK_DATA_DNS:
  104. break;
  105. case CURL_LOCK_DATA_COOKIE:
  106. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  107. if(share->cookies) {
  108. Curl_cookie_cleanup(share->cookies);
  109. share->cookies = NULL;
  110. }
  111. #else /* CURL_DISABLE_HTTP */
  112. res = CURLSHE_NOT_BUILT_IN;
  113. #endif
  114. break;
  115. case CURL_LOCK_DATA_SSL_SESSION:
  116. #ifdef USE_SSL
  117. Curl_safefree(share->sslsession);
  118. #else
  119. res = CURLSHE_NOT_BUILT_IN;
  120. #endif
  121. break;
  122. case CURL_LOCK_DATA_CONNECT:
  123. break;
  124. default:
  125. res = CURLSHE_BAD_OPTION;
  126. break;
  127. }
  128. break;
  129. case CURLSHOPT_LOCKFUNC:
  130. lockfunc = va_arg(param, curl_lock_function);
  131. share->lockfunc = lockfunc;
  132. break;
  133. case CURLSHOPT_UNLOCKFUNC:
  134. unlockfunc = va_arg(param, curl_unlock_function);
  135. share->unlockfunc = unlockfunc;
  136. break;
  137. case CURLSHOPT_USERDATA:
  138. ptr = va_arg(param, void *);
  139. share->clientdata = ptr;
  140. break;
  141. default:
  142. res = CURLSHE_BAD_OPTION;
  143. break;
  144. }
  145. va_end(param);
  146. return res;
  147. }
  148. CURLSHcode
  149. curl_share_cleanup(CURLSH *sh)
  150. {
  151. struct Curl_share *share = (struct Curl_share *)sh;
  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_hash_destroy(&share->hostcache);
  163. #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
  164. Curl_cookie_cleanup(share->cookies);
  165. #endif
  166. #ifdef USE_SSL
  167. if(share->sslsession) {
  168. size_t i;
  169. for(i = 0; i < share->max_ssl_sessions; i++)
  170. Curl_ssl_kill_session(&(share->sslsession[i]));
  171. free(share->sslsession);
  172. }
  173. #endif
  174. if(share->unlockfunc)
  175. share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
  176. free(share);
  177. return CURLSHE_OK;
  178. }
  179. CURLSHcode
  180. Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
  181. curl_lock_access accesstype)
  182. {
  183. struct Curl_share *share = data->share;
  184. if(share == NULL)
  185. return CURLSHE_INVALID;
  186. if(share->specifier & (1<<type)) {
  187. if(share->lockfunc) /* only call this if set! */
  188. share->lockfunc(data, type, accesstype, share->clientdata);
  189. }
  190. /* else if we don't share this, pretend successful lock */
  191. return CURLSHE_OK;
  192. }
  193. CURLSHcode
  194. Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
  195. {
  196. struct Curl_share *share = data->share;
  197. if(share == NULL)
  198. return CURLSHE_INVALID;
  199. if(share->specifier & (1<<type)) {
  200. if(share->unlockfunc) /* only call this if set! */
  201. share->unlockfunc (data, type, share->clientdata);
  202. }
  203. return CURLSHE_OK;
  204. }