1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2009, 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 ***************************************************************************/
27 #include <curl/curl.h>
30 #include "curl_memory.h"
32 /* The last #include file should be: */
38 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
40 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
45 #undef curl_share_setopt
47 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
49 struct Curl_share *share = (struct Curl_share *)sh;
52 curl_lock_function lockfunc;
53 curl_unlock_function unlockfunc;
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 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
78 case CURL_LOCK_DATA_COOKIE:
80 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
85 #endif /* CURL_DISABLE_HTTP */
87 case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
88 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
91 return CURLSHE_BAD_OPTION;
95 case CURLSHOPT_UNSHARE:
96 /* this is a type this share will no longer share */
97 type = va_arg(param, int);
98 share->specifier &= ~(1<<type);
101 case CURL_LOCK_DATA_DNS:
102 if(share->hostcache) {
103 Curl_hash_destroy(share->hostcache);
104 share->hostcache = NULL;
108 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
109 case CURL_LOCK_DATA_COOKIE:
111 Curl_cookie_cleanup(share->cookies);
112 share->cookies = NULL;
115 #endif /* CURL_DISABLE_HTTP */
117 case CURL_LOCK_DATA_SSL_SESSION:
120 case CURL_LOCK_DATA_CONNECT:
124 return CURLSHE_BAD_OPTION;
128 case CURLSHOPT_LOCKFUNC:
129 lockfunc = va_arg(param, curl_lock_function);
130 share->lockfunc = lockfunc;
133 case CURLSHOPT_UNLOCKFUNC:
134 unlockfunc = va_arg(param, curl_unlock_function);
135 share->unlockfunc = unlockfunc;
138 case CURLSHOPT_USERDATA:
139 ptr = va_arg(param, void *);
140 share->clientdata = ptr;
144 return CURLSHE_BAD_OPTION;
151 curl_share_cleanup(CURLSH *sh)
153 struct Curl_share *share = (struct Curl_share *)sh;
156 return CURLSHE_INVALID;
159 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
163 if(share->unlockfunc)
164 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
165 return CURLSHE_IN_USE;
168 if(share->hostcache) {
169 Curl_hash_destroy(share->hostcache);
170 share->hostcache = NULL;
173 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
175 Curl_cookie_cleanup(share->cookies);
176 #endif /* CURL_DISABLE_HTTP */
178 if(share->unlockfunc)
179 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
187 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
188 curl_lock_access accesstype)
190 struct Curl_share *share = data->share;
193 return CURLSHE_INVALID;
195 if(share->specifier & (1<<type)) {
196 if(share->lockfunc) /* only call this if set! */
197 share->lockfunc(data, type, accesstype, share->clientdata);
199 /* else if we don't share this, pretend successful lock */
205 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
207 struct Curl_share *share = data->share;
210 return CURLSHE_INVALID;
212 if(share->specifier & (1<<type)) {
213 if(share->unlockfunc) /* only call this if set! */
214 share->unlockfunc (data, type, share->clientdata);