1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2011, 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 ***************************************************************************/
25 #include <curl/curl.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;
56 /* don't allow setting options while one or more handles are already
58 return CURLSHE_IN_USE;
60 va_start(param, option);
64 /* this is a type this share will share */
65 type = va_arg(param, int);
66 share->specifier |= (1<<type);
68 case CURL_LOCK_DATA_DNS:
69 if(!share->hostcache) {
70 share->hostcache = Curl_mk_dnscache();
76 case CURL_LOCK_DATA_COOKIE:
77 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
79 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
84 #else /* CURL_DISABLE_HTTP */
85 return CURLSHE_NOT_BUILT_IN;
88 case CURL_LOCK_DATA_SSL_SESSION:
90 if(!share->sslsession) {
91 share->nsslsession = 8;
92 share->sslsession = calloc(share->nsslsession,
93 sizeof(struct curl_ssl_session));
94 share->sessionage = 0;
95 if(!share->sslsession)
100 return CURLSHE_NOT_BUILT_IN;
103 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
106 return CURLSHE_BAD_OPTION;
110 case CURLSHOPT_UNSHARE:
111 /* this is a type this share will no longer share */
112 type = va_arg(param, int);
113 share->specifier &= ~(1<<type);
115 case CURL_LOCK_DATA_DNS:
116 if(share->hostcache) {
117 Curl_hash_destroy(share->hostcache);
118 share->hostcache = NULL;
122 case CURL_LOCK_DATA_COOKIE:
123 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
125 Curl_cookie_cleanup(share->cookies);
126 share->cookies = NULL;
129 #else /* CURL_DISABLE_HTTP */
130 return CURLSHE_NOT_BUILT_IN;
133 case CURL_LOCK_DATA_SSL_SESSION:
135 if(share->sslsession) {
136 free(share->sslsession);
137 share->sslsession = NULL;
138 share->nsslsession = 0;
142 return CURLSHE_NOT_BUILT_IN;
145 case CURL_LOCK_DATA_CONNECT:
149 return CURLSHE_BAD_OPTION;
153 case CURLSHOPT_LOCKFUNC:
154 lockfunc = va_arg(param, curl_lock_function);
155 share->lockfunc = lockfunc;
158 case CURLSHOPT_UNLOCKFUNC:
159 unlockfunc = va_arg(param, curl_unlock_function);
160 share->unlockfunc = unlockfunc;
163 case CURLSHOPT_USERDATA:
164 ptr = va_arg(param, void *);
165 share->clientdata = ptr;
169 return CURLSHE_BAD_OPTION;
176 curl_share_cleanup(CURLSH *sh)
178 struct Curl_share *share = (struct Curl_share *)sh;
181 return CURLSHE_INVALID;
184 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
188 if(share->unlockfunc)
189 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
190 return CURLSHE_IN_USE;
193 if(share->hostcache) {
194 Curl_hash_destroy(share->hostcache);
195 share->hostcache = NULL;
198 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
200 Curl_cookie_cleanup(share->cookies);
204 if(share->sslsession) {
206 for(i = 0; i < share->nsslsession; ++i)
207 Curl_ssl_kill_session(&(share->sslsession[i]));
208 free(share->sslsession);
212 if(share->unlockfunc)
213 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
221 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
222 curl_lock_access accesstype)
224 struct Curl_share *share = data->share;
227 return CURLSHE_INVALID;
229 if(share->specifier & (1<<type)) {
230 if(share->lockfunc) /* only call this if set! */
231 share->lockfunc(data, type, accesstype, share->clientdata);
233 /* else if we don't share this, pretend successful lock */
239 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
241 struct Curl_share *share = data->share;
244 return CURLSHE_INVALID;
246 if(share->specifier & (1<<type)) {
247 if(share->unlockfunc) /* only call this if set! */
248 share->unlockfunc (data, type, share->clientdata);