1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2015, 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 https://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 ***************************************************************************/
23 #include "curl_setup.h"
25 #include <curl/curl.h>
28 #include "vtls/vtls.h"
29 #include "curl_memory.h"
31 /* The last #include file should be: */
37 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
39 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
41 if(Curl_mk_dnscache(&share->hostcache)) {
50 #undef curl_share_setopt
52 curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
54 struct Curl_share *share = (struct Curl_share *)sh;
57 curl_lock_function lockfunc;
58 curl_unlock_function unlockfunc;
60 CURLSHcode res = CURLSHE_OK;
63 /* don't allow setting options while one or more handles are already
65 return CURLSHE_IN_USE;
67 va_start(param, option);
71 /* this is a type this share will share */
72 type = va_arg(param, int);
73 share->specifier |= (1<<type);
75 case CURL_LOCK_DATA_DNS:
78 case CURL_LOCK_DATA_COOKIE:
79 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
81 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE );
85 #else /* CURL_DISABLE_HTTP */
86 res = CURLSHE_NOT_BUILT_IN;
90 case CURL_LOCK_DATA_SSL_SESSION:
92 if(!share->sslsession) {
93 share->max_ssl_sessions = 8;
94 share->sslsession = calloc(share->max_ssl_sessions,
95 sizeof(struct curl_ssl_session));
96 share->sessionage = 0;
97 if(!share->sslsession)
101 res = CURLSHE_NOT_BUILT_IN;
105 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
109 res = CURLSHE_BAD_OPTION;
113 case CURLSHOPT_UNSHARE:
114 /* this is a type this share will no longer share */
115 type = va_arg(param, int);
116 share->specifier &= ~(1<<type);
118 case CURL_LOCK_DATA_DNS:
121 case CURL_LOCK_DATA_COOKIE:
122 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
124 Curl_cookie_cleanup(share->cookies);
125 share->cookies = NULL;
127 #else /* CURL_DISABLE_HTTP */
128 res = CURLSHE_NOT_BUILT_IN;
132 case CURL_LOCK_DATA_SSL_SESSION:
134 Curl_safefree(share->sslsession);
136 res = CURLSHE_NOT_BUILT_IN;
140 case CURL_LOCK_DATA_CONNECT:
144 res = CURLSHE_BAD_OPTION;
149 case CURLSHOPT_LOCKFUNC:
150 lockfunc = va_arg(param, curl_lock_function);
151 share->lockfunc = lockfunc;
154 case CURLSHOPT_UNLOCKFUNC:
155 unlockfunc = va_arg(param, curl_unlock_function);
156 share->unlockfunc = unlockfunc;
159 case CURLSHOPT_USERDATA:
160 ptr = va_arg(param, void *);
161 share->clientdata = ptr;
165 res = CURLSHE_BAD_OPTION;
175 curl_share_cleanup(CURLSH *sh)
177 struct Curl_share *share = (struct Curl_share *)sh;
180 return CURLSHE_INVALID;
183 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
187 if(share->unlockfunc)
188 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
189 return CURLSHE_IN_USE;
192 Curl_hash_destroy(&share->hostcache);
194 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
195 Curl_cookie_cleanup(share->cookies);
199 if(share->sslsession) {
201 for(i = 0; i < share->max_ssl_sessions; i++)
202 Curl_ssl_kill_session(&(share->sslsession[i]));
203 free(share->sslsession);
207 if(share->unlockfunc)
208 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
216 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
217 curl_lock_access accesstype)
219 struct Curl_share *share = data->share;
222 return CURLSHE_INVALID;
224 if(share->specifier & (1<<type)) {
225 if(share->lockfunc) /* only call this if set! */
226 share->lockfunc(data, type, accesstype, share->clientdata);
228 /* else if we don't share this, pretend successful lock */
234 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
236 struct Curl_share *share = data->share;
239 return CURLSHE_INVALID;
241 if(share->specifier & (1<<type)) {
242 if(share->unlockfunc) /* only call this if set! */
243 share->unlockfunc (data, type, share->clientdata);