1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2021, 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.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>
29 #include "vtls/vtls.h"
30 #include "curl_memory.h"
32 /* The last #include file should be: */
38 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
40 share->magic = CURL_GOOD_SHARE;
41 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
42 Curl_init_dnscache(&share->hostcache);
48 #undef curl_share_setopt
50 curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
54 curl_lock_function lockfunc;
55 curl_unlock_function unlockfunc;
57 CURLSHcode res = CURLSHE_OK;
59 if(!GOOD_SHARE_HANDLE(share))
60 return CURLSHE_INVALID;
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);
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:
106 if(Curl_conncache_init(&share->conn_cache, 103))
110 case CURL_LOCK_DATA_PSL:
112 res = CURLSHE_NOT_BUILT_IN;
117 res = CURLSHE_BAD_OPTION;
120 share->specifier |= (1<<type);
123 case CURLSHOPT_UNSHARE:
124 /* this is a type this share will no longer share */
125 type = va_arg(param, int);
126 share->specifier &= ~(1<<type);
128 case CURL_LOCK_DATA_DNS:
131 case CURL_LOCK_DATA_COOKIE:
132 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
134 Curl_cookie_cleanup(share->cookies);
135 share->cookies = NULL;
137 #else /* CURL_DISABLE_HTTP */
138 res = CURLSHE_NOT_BUILT_IN;
142 case CURL_LOCK_DATA_SSL_SESSION:
144 Curl_safefree(share->sslsession);
146 res = CURLSHE_NOT_BUILT_IN;
150 case CURL_LOCK_DATA_CONNECT:
154 res = CURLSHE_BAD_OPTION;
159 case CURLSHOPT_LOCKFUNC:
160 lockfunc = va_arg(param, curl_lock_function);
161 share->lockfunc = lockfunc;
164 case CURLSHOPT_UNLOCKFUNC:
165 unlockfunc = va_arg(param, curl_unlock_function);
166 share->unlockfunc = unlockfunc;
169 case CURLSHOPT_USERDATA:
170 ptr = va_arg(param, void *);
171 share->clientdata = ptr;
175 res = CURLSHE_BAD_OPTION;
185 curl_share_cleanup(struct Curl_share *share)
187 if(!GOOD_SHARE_HANDLE(share))
188 return CURLSHE_INVALID;
191 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
195 if(share->unlockfunc)
196 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
197 return CURLSHE_IN_USE;
200 Curl_conncache_close_all_connections(&share->conn_cache);
201 Curl_conncache_destroy(&share->conn_cache);
202 Curl_hash_destroy(&share->hostcache);
204 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
205 Curl_cookie_cleanup(share->cookies);
209 if(share->sslsession) {
211 for(i = 0; i < share->max_ssl_sessions; i++)
212 Curl_ssl_kill_session(&(share->sslsession[i]));
213 free(share->sslsession);
217 Curl_psl_destroy(&share->psl);
219 if(share->unlockfunc)
220 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
229 Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
230 curl_lock_access accesstype)
232 struct Curl_share *share = data->share;
235 return CURLSHE_INVALID;
237 if(share->specifier & (1<<type)) {
238 if(share->lockfunc) /* only call this if set! */
239 share->lockfunc(data, type, accesstype, share->clientdata);
241 /* else if we don't share this, pretend successful lock */
247 Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
249 struct Curl_share *share = data->share;
252 return CURLSHE_INVALID;
254 if(share->specifier & (1<<type)) {
255 if(share->unlockfunc) /* only call this if set! */
256 share->unlockfunc (data, type, share->clientdata);