1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2017, 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(struct Curl_share *share, CURLSHoption option, ...)
56 curl_lock_function lockfunc;
57 curl_unlock_function unlockfunc;
59 CURLSHcode res = CURLSHE_OK;
62 /* don't allow setting options while one or more handles are already
64 return CURLSHE_IN_USE;
66 va_start(param, option);
70 /* this is a type this share will share */
71 type = va_arg(param, int);
72 share->specifier |= (1<<type);
74 case CURL_LOCK_DATA_DNS:
77 case CURL_LOCK_DATA_COOKIE:
78 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
80 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE);
84 #else /* CURL_DISABLE_HTTP */
85 res = CURLSHE_NOT_BUILT_IN;
89 case CURL_LOCK_DATA_SSL_SESSION:
91 if(!share->sslsession) {
92 share->max_ssl_sessions = 8;
93 share->sslsession = calloc(share->max_ssl_sessions,
94 sizeof(struct curl_ssl_session));
95 share->sessionage = 0;
96 if(!share->sslsession)
100 res = CURLSHE_NOT_BUILT_IN;
104 case CURL_LOCK_DATA_CONNECT: /* not supported (yet) */
105 if(Curl_conncache_init(&share->conn_cache, 103))
110 res = CURLSHE_BAD_OPTION;
114 case CURLSHOPT_UNSHARE:
115 /* this is a type this share will no longer share */
116 type = va_arg(param, int);
117 share->specifier &= ~(1<<type);
119 case CURL_LOCK_DATA_DNS:
122 case CURL_LOCK_DATA_COOKIE:
123 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
125 Curl_cookie_cleanup(share->cookies);
126 share->cookies = NULL;
128 #else /* CURL_DISABLE_HTTP */
129 res = CURLSHE_NOT_BUILT_IN;
133 case CURL_LOCK_DATA_SSL_SESSION:
135 Curl_safefree(share->sslsession);
137 res = CURLSHE_NOT_BUILT_IN;
141 case CURL_LOCK_DATA_CONNECT:
145 res = CURLSHE_BAD_OPTION;
150 case CURLSHOPT_LOCKFUNC:
151 lockfunc = va_arg(param, curl_lock_function);
152 share->lockfunc = lockfunc;
155 case CURLSHOPT_UNLOCKFUNC:
156 unlockfunc = va_arg(param, curl_unlock_function);
157 share->unlockfunc = unlockfunc;
160 case CURLSHOPT_USERDATA:
161 ptr = va_arg(param, void *);
162 share->clientdata = ptr;
166 res = CURLSHE_BAD_OPTION;
176 curl_share_cleanup(struct Curl_share *share)
179 return CURLSHE_INVALID;
182 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
186 if(share->unlockfunc)
187 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
188 return CURLSHE_IN_USE;
191 Curl_conncache_close_all_connections(&share->conn_cache);
192 Curl_conncache_destroy(&share->conn_cache);
193 Curl_hash_destroy(&share->hostcache);
195 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
196 Curl_cookie_cleanup(share->cookies);
200 if(share->sslsession) {
202 for(i = 0; i < share->max_ssl_sessions; i++)
203 Curl_ssl_kill_session(&(share->sslsession[i]));
204 free(share->sslsession);
208 if(share->unlockfunc)
209 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
217 Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
218 curl_lock_access accesstype)
220 struct Curl_share *share = data->share;
223 return CURLSHE_INVALID;
225 if(share->specifier & (1<<type)) {
226 if(share->lockfunc) /* only call this if set! */
227 share->lockfunc(data, type, accesstype, share->clientdata);
229 /* else if we don't share this, pretend successful lock */
235 Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
237 struct Curl_share *share = data->share;
240 return CURLSHE_INVALID;
242 if(share->specifier & (1<<type)) {
243 if(share->unlockfunc) /* only call this if set! */
244 share->unlockfunc (data, type, share->clientdata);