1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2007, 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.1.1.1 2008-09-23 16:32:05 hoffman Exp $
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
);
49 #undef curl_share_setopt
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
);
73 case CURL_LOCK_DATA_DNS
:
74 if(!share
->hostcache
) {
75 share
->hostcache
= Curl_mk_dnscache();
81 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
82 case CURL_LOCK_DATA_COOKIE
:
84 share
->cookies
= Curl_cookie_init(NULL
, NULL
, NULL
, TRUE
);
89 #endif /* CURL_DISABLE_HTTP */
91 case CURL_LOCK_DATA_SSL_SESSION
: /* not supported (yet) */
92 case CURL_LOCK_DATA_CONNECT
: /* not supported (yet) */
95 return CURLSHE_BAD_OPTION
;
99 case CURLSHOPT_UNSHARE
:
100 /* this is a type this share will no longer share */
101 type
= va_arg(param
, int);
102 share
->specifier
&= ~(1<<type
);
105 case CURL_LOCK_DATA_DNS
:
106 if(share
->hostcache
) {
107 Curl_hash_destroy(share
->hostcache
);
108 share
->hostcache
= NULL
;
112 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
113 case CURL_LOCK_DATA_COOKIE
:
115 Curl_cookie_cleanup(share
->cookies
);
116 share
->cookies
= NULL
;
119 #endif /* CURL_DISABLE_HTTP */
121 case CURL_LOCK_DATA_SSL_SESSION
:
124 case CURL_LOCK_DATA_CONNECT
:
128 return CURLSHE_BAD_OPTION
;
132 case CURLSHOPT_LOCKFUNC
:
133 lockfunc
= va_arg(param
, curl_lock_function
);
134 share
->lockfunc
= lockfunc
;
137 case CURLSHOPT_UNLOCKFUNC
:
138 unlockfunc
= va_arg(param
, curl_unlock_function
);
139 share
->unlockfunc
= unlockfunc
;
142 case CURLSHOPT_USERDATA
:
143 ptr
= va_arg(param
, void *);
144 share
->clientdata
= ptr
;
148 return CURLSHE_BAD_OPTION
;
155 curl_share_cleanup(CURLSH
*sh
)
157 struct Curl_share
*share
= (struct Curl_share
*)sh
;
160 return CURLSHE_INVALID
;
163 share
->lockfunc(NULL
, CURL_LOCK_DATA_SHARE
, CURL_LOCK_ACCESS_SINGLE
,
167 if(share
->unlockfunc
)
168 share
->unlockfunc(NULL
, CURL_LOCK_DATA_SHARE
, share
->clientdata
);
169 return CURLSHE_IN_USE
;
173 Curl_hash_destroy(share
->hostcache
);
175 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
177 Curl_cookie_cleanup(share
->cookies
);
178 #endif /* CURL_DISABLE_HTTP */
180 if(share
->unlockfunc
)
181 share
->unlockfunc(NULL
, CURL_LOCK_DATA_SHARE
, share
->clientdata
);
189 Curl_share_lock(struct SessionHandle
*data
, curl_lock_data type
,
190 curl_lock_access accesstype
)
192 struct Curl_share
*share
= data
->share
;
195 return CURLSHE_INVALID
;
197 if(share
->specifier
& (1<<type
)) {
198 if(share
->lockfunc
) /* only call this if set! */
199 share
->lockfunc(data
, type
, accesstype
, share
->clientdata
);
201 /* else if we don't share this, pretend successful lock */
207 Curl_share_unlock(struct SessionHandle
*data
, curl_lock_data type
)
209 struct Curl_share
*share
= data
->share
;
212 return CURLSHE_INVALID
;
214 if(share
->specifier
& (1<<type
)) {
215 if(share
->unlockfunc
) /* only call this if set! */
216 share
->unlockfunc (data
, type
, share
->clientdata
);