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.
22 ***************************************************************************/
28 #include <curl/curl.h>
31 #include "curl_memory.h"
33 /* The last #include file should be: */
39 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
41 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
46 #undef curl_share_setopt
48 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
50 struct Curl_share *share = (struct Curl_share *)sh;
53 curl_lock_function lockfunc;
54 curl_unlock_function unlockfunc;
58 /* don't allow setting options while one or more handles are already
60 return CURLSHE_IN_USE;
62 va_start(param, option);
66 /* this is a type this share will share */
67 type = va_arg(param, int);
68 share->specifier |= (1<<type);
70 case CURL_LOCK_DATA_DNS:
71 if(!share->hostcache) {
72 share->hostcache = Curl_mk_dnscache();
78 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
79 case CURL_LOCK_DATA_COOKIE:
81 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
86 #endif /* CURL_DISABLE_HTTP */
88 case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
89 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
92 return CURLSHE_BAD_OPTION;
96 case CURLSHOPT_UNSHARE:
97 /* this is a type this share will no longer share */
98 type = va_arg(param, int);
99 share->specifier &= ~(1<<type);
102 case CURL_LOCK_DATA_DNS:
103 if(share->hostcache) {
104 Curl_hash_destroy(share->hostcache);
105 share->hostcache = NULL;
109 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
110 case CURL_LOCK_DATA_COOKIE:
112 Curl_cookie_cleanup(share->cookies);
113 share->cookies = NULL;
116 #endif /* CURL_DISABLE_HTTP */
118 case CURL_LOCK_DATA_SSL_SESSION:
121 case CURL_LOCK_DATA_CONNECT:
125 return CURLSHE_BAD_OPTION;
129 case CURLSHOPT_LOCKFUNC:
130 lockfunc = va_arg(param, curl_lock_function);
131 share->lockfunc = lockfunc;
134 case CURLSHOPT_UNLOCKFUNC:
135 unlockfunc = va_arg(param, curl_unlock_function);
136 share->unlockfunc = unlockfunc;
139 case CURLSHOPT_USERDATA:
140 ptr = va_arg(param, void *);
141 share->clientdata = ptr;
145 return CURLSHE_BAD_OPTION;
152 curl_share_cleanup(CURLSH *sh)
154 struct Curl_share *share = (struct Curl_share *)sh;
157 return CURLSHE_INVALID;
160 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
164 if(share->unlockfunc)
165 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
166 return CURLSHE_IN_USE;
169 if(share->hostcache) {
170 Curl_hash_destroy(share->hostcache);
171 share->hostcache = NULL;
174 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
176 Curl_cookie_cleanup(share->cookies);
177 #endif /* CURL_DISABLE_HTTP */
179 if(share->unlockfunc)
180 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
188 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
189 curl_lock_access accesstype)
191 struct Curl_share *share = data->share;
194 return CURLSHE_INVALID;
196 if(share->specifier & (1<<type)) {
197 if(share->lockfunc) /* only call this if set! */
198 share->lockfunc(data, type, accesstype, share->clientdata);
200 /* else if we don't share this, pretend successful lock */
206 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
208 struct Curl_share *share = data->share;
211 return CURLSHE_INVALID;
213 if(share->specifier & (1<<type)) {
214 if(share->unlockfunc) /* only call this if set! */
215 share->unlockfunc (data, type, share->clientdata);