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 if(!share->sslsession)
99 return CURLSHE_NOT_BUILT_IN;
102 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
105 return CURLSHE_BAD_OPTION;
109 case CURLSHOPT_UNSHARE:
110 /* this is a type this share will no longer share */
111 type = va_arg(param, int);
112 share->specifier &= ~(1<<type);
114 case CURL_LOCK_DATA_DNS:
115 if(share->hostcache) {
116 Curl_hash_destroy(share->hostcache);
117 share->hostcache = NULL;
121 case CURL_LOCK_DATA_COOKIE:
122 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
124 Curl_cookie_cleanup(share->cookies);
125 share->cookies = NULL;
128 #else /* CURL_DISABLE_HTTP */
129 return CURLSHE_NOT_BUILT_IN;
132 case CURL_LOCK_DATA_SSL_SESSION:
134 if(share->sslsession) {
135 free(share->sslsession);
136 share->sslsession = NULL;
137 share->nsslsession = 0;
141 return CURLSHE_NOT_BUILT_IN;
144 case CURL_LOCK_DATA_CONNECT:
148 return 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 return CURLSHE_BAD_OPTION;
175 curl_share_cleanup(CURLSH *sh)
177 struct Curl_share *share = (struct Curl_share *)sh;
180 return CURLSHE_INVALID;
183 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
187 if(share->unlockfunc)
188 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
189 return CURLSHE_IN_USE;
192 if(share->hostcache) {
193 Curl_hash_destroy(share->hostcache);
194 share->hostcache = NULL;
197 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
199 Curl_cookie_cleanup(share->cookies);
203 if(share->sslsession) {
205 for(i = 0; i < share->nsslsession; ++i)
206 Curl_ssl_kill_session(&(share->sslsession[i]));
207 free(share->sslsession);
211 if(share->unlockfunc)
212 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
220 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
221 curl_lock_access accesstype)
223 struct Curl_share *share = data->share;
226 return CURLSHE_INVALID;
228 if(share->specifier & (1<<type)) {
229 if(share->lockfunc) /* only call this if set! */
230 share->lockfunc(data, type, accesstype, share->clientdata);
232 /* else if we don't share this, pretend successful lock */
238 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
240 struct Curl_share *share = data->share;
243 return CURLSHE_INVALID;
245 if(share->specifier & (1<<type)) {
246 if(share->unlockfunc) /* only call this if set! */
247 share->unlockfunc (data, type, share->clientdata);