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.
21 * $Id: share.c,v 1.16 2004/02/26 11:39:38 bagder Exp $
22 ***************************************************************************/
28 #include <curl/curl.h>
32 /* The last #include file should be: */
40 struct Curl_share *share =
41 (struct Curl_share *)malloc(sizeof(struct Curl_share));
43 memset (share, 0, sizeof(struct Curl_share));
44 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
51 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
53 struct Curl_share *share = (struct Curl_share *)sh;
56 curl_lock_function lockfunc;
57 curl_unlock_function unlockfunc;
61 /* don't allow setting options while one or more handles are already
63 return CURLSHE_IN_USE;
65 va_start(param, option);
69 /* this is a type this share will share */
70 type = va_arg(param, int);
71 share->specifier |= (1<<type);
74 case CURL_LOCK_DATA_DNS:
75 if (!share->hostcache) {
76 share->hostcache = Curl_hash_alloc(7, Curl_freednsinfo);
80 case CURL_LOCK_DATA_COOKIE:
81 if (!share->cookies) {
82 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
86 case CURL_LOCK_DATA_SSL_SESSION:
89 case CURL_LOCK_DATA_CONNECT:
93 return CURLSHE_BAD_OPTION;
97 case CURLSHOPT_UNSHARE:
98 /* this is a type this share will no longer share */
99 type = va_arg(param, int);
100 share->specifier &= ~(1<<type);
103 case CURL_LOCK_DATA_DNS:
104 if (share->hostcache) {
105 Curl_hash_destroy(share->hostcache);
106 share->hostcache = NULL;
110 case CURL_LOCK_DATA_COOKIE:
111 if (share->cookies) {
112 Curl_cookie_cleanup(share->cookies);
113 share->cookies = NULL;
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;
158 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
162 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
163 return CURLSHE_IN_USE;
167 Curl_hash_destroy(share->hostcache);
170 Curl_cookie_cleanup(share->cookies);
172 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
180 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
181 curl_lock_access accesstype)
183 struct Curl_share *share = data->share;
186 return CURLSHE_INVALID;
188 if(share->specifier & (1<<type)) {
189 if(share->lockfunc) /* only call this if set! */
190 share->lockfunc(data, type, accesstype, share->clientdata);
192 /* else if we don't share this, pretend successful lock */
198 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
200 struct Curl_share *share = data->share;
203 return CURLSHE_INVALID;
205 if(share->specifier & (1<<type)) {
206 if(share->unlockfunc) /* only call this if set! */
207 share->unlockfunc (data, type, share->clientdata);