1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2004, 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>
33 /* The last #include file should be: */
39 struct Curl_share *share =
40 (struct Curl_share *)malloc(sizeof(struct Curl_share));
42 memset (share, 0, sizeof(struct Curl_share));
43 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
50 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
52 struct Curl_share *share = (struct Curl_share *)sh;
55 curl_lock_function lockfunc;
56 curl_unlock_function unlockfunc;
60 /* don't allow setting options while one or more handles are already
62 return CURLSHE_IN_USE;
64 va_start(param, option);
68 /* this is a type this share will share */
69 type = va_arg(param, int);
70 share->specifier |= (1<<type);
72 case CURL_LOCK_DATA_DNS:
73 if (!share->hostcache) {
74 share->hostcache = Curl_mk_dnscache();
80 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
81 case CURL_LOCK_DATA_COOKIE:
82 if (!share->cookies) {
83 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
88 #endif /* CURL_DISABLE_HTTP */
90 case CURL_LOCK_DATA_SSL_SESSION: /* not supported (yet) */
91 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
94 return CURLSHE_BAD_OPTION;
98 case CURLSHOPT_UNSHARE:
99 /* this is a type this share will no longer share */
100 type = va_arg(param, int);
101 share->specifier &= ~(1<<type);
104 case CURL_LOCK_DATA_DNS:
105 if (share->hostcache) {
106 Curl_hash_destroy(share->hostcache);
107 share->hostcache = NULL;
111 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
112 case CURL_LOCK_DATA_COOKIE:
113 if (share->cookies) {
114 Curl_cookie_cleanup(share->cookies);
115 share->cookies = NULL;
118 #endif /* CURL_DISABLE_HTTP */
120 case CURL_LOCK_DATA_SSL_SESSION:
123 case CURL_LOCK_DATA_CONNECT:
127 return CURLSHE_BAD_OPTION;
131 case CURLSHOPT_LOCKFUNC:
132 lockfunc = va_arg(param, curl_lock_function);
133 share->lockfunc = lockfunc;
136 case CURLSHOPT_UNLOCKFUNC:
137 unlockfunc = va_arg(param, curl_unlock_function);
138 share->unlockfunc = unlockfunc;
141 case CURLSHOPT_USERDATA:
142 ptr = va_arg(param, void *);
143 share->clientdata = ptr;
147 return CURLSHE_BAD_OPTION;
154 curl_share_cleanup(CURLSH *sh)
156 struct Curl_share *share = (struct Curl_share *)sh;
159 return CURLSHE_INVALID;
162 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
166 if(share->unlockfunc)
167 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
168 return CURLSHE_IN_USE;
172 Curl_hash_destroy(share->hostcache);
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);