1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
|
---|
9 | *
|
---|
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.
|
---|
13 | *
|
---|
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.
|
---|
17 | *
|
---|
18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
19 | * KIND, either express or implied.
|
---|
20 | *
|
---|
21 | * SPDX-License-Identifier: curl
|
---|
22 | *
|
---|
23 | ***************************************************************************/
|
---|
24 |
|
---|
25 | #include "curl_setup.h"
|
---|
26 |
|
---|
27 | #include <curl/curl.h>
|
---|
28 | #include "urldata.h"
|
---|
29 | #include "connect.h"
|
---|
30 | #include "share.h"
|
---|
31 | #include "psl.h"
|
---|
32 | #include "vtls/vtls.h"
|
---|
33 | #include "hsts.h"
|
---|
34 | #include "url.h"
|
---|
35 |
|
---|
36 | /* The last 3 #include files should be in this order */
|
---|
37 | #include "curl_printf.h"
|
---|
38 | #include "curl_memory.h"
|
---|
39 | #include "memdebug.h"
|
---|
40 |
|
---|
41 | CURLSH *
|
---|
42 | curl_share_init(void)
|
---|
43 | {
|
---|
44 | struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
|
---|
45 | if(share) {
|
---|
46 | share->magic = CURL_GOOD_SHARE;
|
---|
47 | share->specifier |= (1 << CURL_LOCK_DATA_SHARE);
|
---|
48 | Curl_init_dnscache(&share->hostcache, 23);
|
---|
49 | }
|
---|
50 |
|
---|
51 | return share;
|
---|
52 | }
|
---|
53 |
|
---|
54 | #undef curl_share_setopt
|
---|
55 | CURLSHcode
|
---|
56 | curl_share_setopt(CURLSH *sh, CURLSHoption option, ...)
|
---|
57 | {
|
---|
58 | va_list param;
|
---|
59 | int type;
|
---|
60 | curl_lock_function lockfunc;
|
---|
61 | curl_unlock_function unlockfunc;
|
---|
62 | void *ptr;
|
---|
63 | CURLSHcode res = CURLSHE_OK;
|
---|
64 | struct Curl_share *share = sh;
|
---|
65 |
|
---|
66 | if(!GOOD_SHARE_HANDLE(share))
|
---|
67 | return CURLSHE_INVALID;
|
---|
68 |
|
---|
69 | if(share->dirty)
|
---|
70 | /* do not allow setting options while one or more handles are already
|
---|
71 | using this share */
|
---|
72 | return CURLSHE_IN_USE;
|
---|
73 |
|
---|
74 | va_start(param, option);
|
---|
75 |
|
---|
76 | switch(option) {
|
---|
77 | case CURLSHOPT_SHARE:
|
---|
78 | /* this is a type this share will share */
|
---|
79 | type = va_arg(param, int);
|
---|
80 |
|
---|
81 | switch(type) {
|
---|
82 | case CURL_LOCK_DATA_DNS:
|
---|
83 | break;
|
---|
84 |
|
---|
85 | case CURL_LOCK_DATA_COOKIE:
|
---|
86 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
---|
87 | if(!share->cookies) {
|
---|
88 | share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE);
|
---|
89 | if(!share->cookies)
|
---|
90 | res = CURLSHE_NOMEM;
|
---|
91 | }
|
---|
92 | #else /* CURL_DISABLE_HTTP */
|
---|
93 | res = CURLSHE_NOT_BUILT_IN;
|
---|
94 | #endif
|
---|
95 | break;
|
---|
96 |
|
---|
97 | case CURL_LOCK_DATA_HSTS:
|
---|
98 | #ifndef CURL_DISABLE_HSTS
|
---|
99 | if(!share->hsts) {
|
---|
100 | share->hsts = Curl_hsts_init();
|
---|
101 | if(!share->hsts)
|
---|
102 | res = CURLSHE_NOMEM;
|
---|
103 | }
|
---|
104 | #else /* CURL_DISABLE_HSTS */
|
---|
105 | res = CURLSHE_NOT_BUILT_IN;
|
---|
106 | #endif
|
---|
107 | break;
|
---|
108 |
|
---|
109 | case CURL_LOCK_DATA_SSL_SESSION:
|
---|
110 | #ifdef USE_SSL
|
---|
111 | if(!share->sslsession) {
|
---|
112 | share->max_ssl_sessions = 8;
|
---|
113 | share->sslsession = calloc(share->max_ssl_sessions,
|
---|
114 | sizeof(struct Curl_ssl_session));
|
---|
115 | share->sessionage = 0;
|
---|
116 | if(!share->sslsession)
|
---|
117 | res = CURLSHE_NOMEM;
|
---|
118 | }
|
---|
119 | #else
|
---|
120 | res = CURLSHE_NOT_BUILT_IN;
|
---|
121 | #endif
|
---|
122 | break;
|
---|
123 |
|
---|
124 | case CURL_LOCK_DATA_CONNECT:
|
---|
125 | /* It is safe to set this option several times on a share. */
|
---|
126 | if(!share->cpool.idata) {
|
---|
127 | if(Curl_cpool_init(&share->cpool, Curl_on_disconnect,
|
---|
128 | NULL, share, 103))
|
---|
129 | res = CURLSHE_NOMEM;
|
---|
130 | }
|
---|
131 | break;
|
---|
132 |
|
---|
133 | case CURL_LOCK_DATA_PSL:
|
---|
134 | #ifndef USE_LIBPSL
|
---|
135 | res = CURLSHE_NOT_BUILT_IN;
|
---|
136 | #endif
|
---|
137 | break;
|
---|
138 |
|
---|
139 | default:
|
---|
140 | res = CURLSHE_BAD_OPTION;
|
---|
141 | }
|
---|
142 | if(!res)
|
---|
143 | share->specifier |= (unsigned int)(1 << type);
|
---|
144 | break;
|
---|
145 |
|
---|
146 | case CURLSHOPT_UNSHARE:
|
---|
147 | /* this is a type this share will no longer share */
|
---|
148 | type = va_arg(param, int);
|
---|
149 | share->specifier &= ~(unsigned int)(1 << type);
|
---|
150 | switch(type) {
|
---|
151 | case CURL_LOCK_DATA_DNS:
|
---|
152 | break;
|
---|
153 |
|
---|
154 | case CURL_LOCK_DATA_COOKIE:
|
---|
155 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
---|
156 | if(share->cookies) {
|
---|
157 | Curl_cookie_cleanup(share->cookies);
|
---|
158 | share->cookies = NULL;
|
---|
159 | }
|
---|
160 | #else /* CURL_DISABLE_HTTP */
|
---|
161 | res = CURLSHE_NOT_BUILT_IN;
|
---|
162 | #endif
|
---|
163 | break;
|
---|
164 |
|
---|
165 | case CURL_LOCK_DATA_HSTS:
|
---|
166 | #ifndef CURL_DISABLE_HSTS
|
---|
167 | if(share->hsts) {
|
---|
168 | Curl_hsts_cleanup(&share->hsts);
|
---|
169 | }
|
---|
170 | #else /* CURL_DISABLE_HSTS */
|
---|
171 | res = CURLSHE_NOT_BUILT_IN;
|
---|
172 | #endif
|
---|
173 | break;
|
---|
174 |
|
---|
175 | case CURL_LOCK_DATA_SSL_SESSION:
|
---|
176 | #ifdef USE_SSL
|
---|
177 | Curl_safefree(share->sslsession);
|
---|
178 | #else
|
---|
179 | res = CURLSHE_NOT_BUILT_IN;
|
---|
180 | #endif
|
---|
181 | break;
|
---|
182 |
|
---|
183 | case CURL_LOCK_DATA_CONNECT:
|
---|
184 | break;
|
---|
185 |
|
---|
186 | default:
|
---|
187 | res = CURLSHE_BAD_OPTION;
|
---|
188 | break;
|
---|
189 | }
|
---|
190 | break;
|
---|
191 |
|
---|
192 | case CURLSHOPT_LOCKFUNC:
|
---|
193 | lockfunc = va_arg(param, curl_lock_function);
|
---|
194 | share->lockfunc = lockfunc;
|
---|
195 | break;
|
---|
196 |
|
---|
197 | case CURLSHOPT_UNLOCKFUNC:
|
---|
198 | unlockfunc = va_arg(param, curl_unlock_function);
|
---|
199 | share->unlockfunc = unlockfunc;
|
---|
200 | break;
|
---|
201 |
|
---|
202 | case CURLSHOPT_USERDATA:
|
---|
203 | ptr = va_arg(param, void *);
|
---|
204 | share->clientdata = ptr;
|
---|
205 | break;
|
---|
206 |
|
---|
207 | default:
|
---|
208 | res = CURLSHE_BAD_OPTION;
|
---|
209 | break;
|
---|
210 | }
|
---|
211 |
|
---|
212 | va_end(param);
|
---|
213 |
|
---|
214 | return res;
|
---|
215 | }
|
---|
216 |
|
---|
217 | CURLSHcode
|
---|
218 | curl_share_cleanup(CURLSH *sh)
|
---|
219 | {
|
---|
220 | struct Curl_share *share = sh;
|
---|
221 | if(!GOOD_SHARE_HANDLE(share))
|
---|
222 | return CURLSHE_INVALID;
|
---|
223 |
|
---|
224 | if(share->lockfunc)
|
---|
225 | share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
|
---|
226 | share->clientdata);
|
---|
227 |
|
---|
228 | if(share->dirty) {
|
---|
229 | if(share->unlockfunc)
|
---|
230 | share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
|
---|
231 | return CURLSHE_IN_USE;
|
---|
232 | }
|
---|
233 |
|
---|
234 | if(share->specifier & (1 << CURL_LOCK_DATA_CONNECT)) {
|
---|
235 | Curl_cpool_destroy(&share->cpool);
|
---|
236 | }
|
---|
237 | Curl_hash_destroy(&share->hostcache);
|
---|
238 |
|
---|
239 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
|
---|
240 | Curl_cookie_cleanup(share->cookies);
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | #ifndef CURL_DISABLE_HSTS
|
---|
244 | Curl_hsts_cleanup(&share->hsts);
|
---|
245 | #endif
|
---|
246 |
|
---|
247 | #ifdef USE_SSL
|
---|
248 | if(share->sslsession) {
|
---|
249 | size_t i;
|
---|
250 | for(i = 0; i < share->max_ssl_sessions; i++)
|
---|
251 | Curl_ssl_kill_session(&(share->sslsession[i]));
|
---|
252 | free(share->sslsession);
|
---|
253 | }
|
---|
254 | #endif
|
---|
255 |
|
---|
256 | Curl_psl_destroy(&share->psl);
|
---|
257 |
|
---|
258 | if(share->unlockfunc)
|
---|
259 | share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
|
---|
260 | share->magic = 0;
|
---|
261 | free(share);
|
---|
262 |
|
---|
263 | return CURLSHE_OK;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | CURLSHcode
|
---|
268 | Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
|
---|
269 | curl_lock_access accesstype)
|
---|
270 | {
|
---|
271 | struct Curl_share *share = data->share;
|
---|
272 |
|
---|
273 | if(!share)
|
---|
274 | return CURLSHE_INVALID;
|
---|
275 |
|
---|
276 | if(share->specifier & (unsigned int)(1 << type)) {
|
---|
277 | if(share->lockfunc) /* only call this if set! */
|
---|
278 | share->lockfunc(data, type, accesstype, share->clientdata);
|
---|
279 | }
|
---|
280 | /* else if we do not share this, pretend successful lock */
|
---|
281 |
|
---|
282 | return CURLSHE_OK;
|
---|
283 | }
|
---|
284 |
|
---|
285 | CURLSHcode
|
---|
286 | Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
|
---|
287 | {
|
---|
288 | struct Curl_share *share = data->share;
|
---|
289 |
|
---|
290 | if(!share)
|
---|
291 | return CURLSHE_INVALID;
|
---|
292 |
|
---|
293 | if(share->specifier & (unsigned int)(1 << type)) {
|
---|
294 | if(share->unlockfunc) /* only call this if set! */
|
---|
295 | share->unlockfunc (data, type, share->clientdata);
|
---|
296 | }
|
---|
297 |
|
---|
298 | return CURLSHE_OK;
|
---|
299 | }
|
---|