1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2013, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
28 #include "vtls/vtls.h"
29 #include "curl_memory.h"
31 /* The last #include file should be: */
37 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
39 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
44 #undef curl_share_setopt
46 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
48 struct Curl_share *share = (struct Curl_share *)sh;
51 curl_lock_function lockfunc;
52 curl_unlock_function unlockfunc;
54 CURLSHcode res = CURLSHE_OK;
57 /* don't allow setting options while one or more handles are already
59 return CURLSHE_IN_USE;
61 va_start(param, option);
65 /* this is a type this share will share */
66 type = va_arg(param, int);
67 share->specifier |= (1<<type);
69 case CURL_LOCK_DATA_DNS:
70 if(!share->hostcache) {
71 share->hostcache = Curl_mk_dnscache();
77 case CURL_LOCK_DATA_COOKIE:
78 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
80 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
84 #else /* CURL_DISABLE_HTTP */
85 res = CURLSHE_NOT_BUILT_IN;
89 case CURL_LOCK_DATA_SSL_SESSION:
91 if(!share->sslsession) {
92 share->max_ssl_sessions = 8;
93 share->sslsession = calloc(share->max_ssl_sessions,
94 sizeof(struct curl_ssl_session));
95 share->sessionage = 0;
96 if(!share->sslsession)
100 res = CURLSHE_NOT_BUILT_IN;
104 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
108 res = CURLSHE_BAD_OPTION;
112 case CURLSHOPT_UNSHARE:
113 /* this is a type this share will no longer share */
114 type = va_arg(param, int);
115 share->specifier &= ~(1<<type);
117 case CURL_LOCK_DATA_DNS:
118 if(share->hostcache) {
119 Curl_hash_destroy(share->hostcache);
120 share->hostcache = NULL;
124 case CURL_LOCK_DATA_COOKIE:
125 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
127 Curl_cookie_cleanup(share->cookies);
128 share->cookies = NULL;
130 #else /* CURL_DISABLE_HTTP */
131 res = CURLSHE_NOT_BUILT_IN;
135 case CURL_LOCK_DATA_SSL_SESSION:
137 Curl_safefree(share->sslsession);
139 res = CURLSHE_NOT_BUILT_IN;
143 case CURL_LOCK_DATA_CONNECT:
147 res = CURLSHE_BAD_OPTION;
152 case CURLSHOPT_LOCKFUNC:
153 lockfunc = va_arg(param, curl_lock_function);
154 share->lockfunc = lockfunc;
157 case CURLSHOPT_UNLOCKFUNC:
158 unlockfunc = va_arg(param, curl_unlock_function);
159 share->unlockfunc = unlockfunc;
162 case CURLSHOPT_USERDATA:
163 ptr = va_arg(param, void *);
164 share->clientdata = ptr;
168 res = CURLSHE_BAD_OPTION;
178 curl_share_cleanup(CURLSH *sh)
180 struct Curl_share *share = (struct Curl_share *)sh;
183 return CURLSHE_INVALID;
186 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
190 if(share->unlockfunc)
191 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
192 return CURLSHE_IN_USE;
195 if(share->hostcache) {
196 Curl_hash_destroy(share->hostcache);
197 share->hostcache = NULL;
200 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
202 Curl_cookie_cleanup(share->cookies);
206 if(share->sslsession) {
208 for(i = 0; i < share->max_ssl_sessions; i++)
209 Curl_ssl_kill_session(&(share->sslsession[i]));
210 free(share->sslsession);
214 if(share->unlockfunc)
215 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
223 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
224 curl_lock_access accesstype)
226 struct Curl_share *share = data->share;
229 return CURLSHE_INVALID;
231 if(share->specifier & (1<<type)) {
232 if(share->lockfunc) /* only call this if set! */
233 share->lockfunc(data, type, accesstype, share->clientdata);
235 /* else if we don't share this, pretend successful lock */
241 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
243 struct Curl_share *share = data->share;
246 return CURLSHE_INVALID;
248 if(share->specifier & (1<<type)) {
249 if(share->unlockfunc) /* only call this if set! */
250 share->unlockfunc (data, type, share->clientdata);