/src/PROJ/curl/lib/share.c
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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 "vtls/vtls_scache.h" |
34 | | #include "hsts.h" |
35 | | #include "url.h" |
36 | | |
37 | | /* The last 2 #include files should be in this order */ |
38 | | #include "curl_memory.h" |
39 | | #include "memdebug.h" |
40 | | |
41 | | CURLSH * |
42 | | curl_share_init(void) |
43 | 0 | { |
44 | 0 | struct Curl_share *share = calloc(1, sizeof(struct Curl_share)); |
45 | 0 | if(share) { |
46 | 0 | share->magic = CURL_GOOD_SHARE; |
47 | 0 | share->specifier |= (1 << CURL_LOCK_DATA_SHARE); |
48 | 0 | Curl_dnscache_init(&share->dnscache, 23); |
49 | 0 | share->admin = curl_easy_init(); |
50 | 0 | if(!share->admin) { |
51 | 0 | free(share); |
52 | 0 | return NULL; |
53 | 0 | } |
54 | | /* admin handles have mid 0 */ |
55 | 0 | share->admin->mid = 0; |
56 | 0 | share->admin->state.internal = TRUE; |
57 | | #ifdef DEBUGBUILD |
58 | | if(getenv("CURL_DEBUG")) |
59 | | share->admin->set.verbose = TRUE; |
60 | | #endif |
61 | 0 | } |
62 | | |
63 | 0 | return share; |
64 | 0 | } |
65 | | |
66 | | #undef curl_share_setopt |
67 | | CURLSHcode |
68 | | curl_share_setopt(CURLSH *sh, CURLSHoption option, ...) |
69 | 0 | { |
70 | 0 | va_list param; |
71 | 0 | int type; |
72 | 0 | curl_lock_function lockfunc; |
73 | 0 | curl_unlock_function unlockfunc; |
74 | 0 | void *ptr; |
75 | 0 | CURLSHcode res = CURLSHE_OK; |
76 | 0 | struct Curl_share *share = sh; |
77 | |
|
78 | 0 | if(!GOOD_SHARE_HANDLE(share)) |
79 | 0 | return CURLSHE_INVALID; |
80 | | |
81 | 0 | if(share->dirty) |
82 | | /* do not allow setting options while one or more handles are already |
83 | | using this share */ |
84 | 0 | return CURLSHE_IN_USE; |
85 | | |
86 | 0 | va_start(param, option); |
87 | |
|
88 | 0 | switch(option) { |
89 | 0 | case CURLSHOPT_SHARE: |
90 | | /* this is a type this share will share */ |
91 | 0 | type = va_arg(param, int); |
92 | |
|
93 | 0 | switch(type) { |
94 | 0 | case CURL_LOCK_DATA_DNS: |
95 | 0 | break; |
96 | | |
97 | 0 | case CURL_LOCK_DATA_COOKIE: |
98 | 0 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) |
99 | 0 | if(!share->cookies) { |
100 | 0 | share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE); |
101 | 0 | if(!share->cookies) |
102 | 0 | res = CURLSHE_NOMEM; |
103 | 0 | } |
104 | | #else /* CURL_DISABLE_HTTP */ |
105 | | res = CURLSHE_NOT_BUILT_IN; |
106 | | #endif |
107 | 0 | break; |
108 | | |
109 | 0 | case CURL_LOCK_DATA_HSTS: |
110 | 0 | #ifndef CURL_DISABLE_HSTS |
111 | 0 | if(!share->hsts) { |
112 | 0 | share->hsts = Curl_hsts_init(); |
113 | 0 | if(!share->hsts) |
114 | 0 | res = CURLSHE_NOMEM; |
115 | 0 | } |
116 | | #else /* CURL_DISABLE_HSTS */ |
117 | | res = CURLSHE_NOT_BUILT_IN; |
118 | | #endif |
119 | 0 | break; |
120 | | |
121 | 0 | case CURL_LOCK_DATA_SSL_SESSION: |
122 | 0 | #ifdef USE_SSL |
123 | 0 | if(!share->ssl_scache) { |
124 | | /* There is no way (yet) for the application to configure the |
125 | | * session cache size, shared between many transfers. As for curl |
126 | | * itself, a high session count will impact startup time. Also, the |
127 | | * scache is not optimized for several hundreds of peers. So, |
128 | | * keep it at a reasonable level. */ |
129 | 0 | if(Curl_ssl_scache_create(25, 2, &share->ssl_scache)) |
130 | 0 | res = CURLSHE_NOMEM; |
131 | 0 | } |
132 | | #else |
133 | | res = CURLSHE_NOT_BUILT_IN; |
134 | | #endif |
135 | 0 | break; |
136 | | |
137 | 0 | case CURL_LOCK_DATA_CONNECT: |
138 | | /* It is safe to set this option several times on a share. */ |
139 | 0 | if(!share->cpool.initialised) { |
140 | 0 | Curl_cpool_init(&share->cpool, share->admin, share, 103); |
141 | 0 | } |
142 | 0 | break; |
143 | | |
144 | 0 | case CURL_LOCK_DATA_PSL: |
145 | 0 | #ifndef USE_LIBPSL |
146 | 0 | res = CURLSHE_NOT_BUILT_IN; |
147 | 0 | #endif |
148 | 0 | break; |
149 | | |
150 | 0 | default: |
151 | 0 | res = CURLSHE_BAD_OPTION; |
152 | 0 | } |
153 | 0 | if(!res) |
154 | 0 | share->specifier |= (unsigned int)(1 << type); |
155 | 0 | break; |
156 | | |
157 | 0 | case CURLSHOPT_UNSHARE: |
158 | | /* this is a type this share will no longer share */ |
159 | 0 | type = va_arg(param, int); |
160 | 0 | share->specifier &= ~(unsigned int)(1 << type); |
161 | 0 | switch(type) { |
162 | 0 | case CURL_LOCK_DATA_DNS: |
163 | 0 | break; |
164 | | |
165 | 0 | case CURL_LOCK_DATA_COOKIE: |
166 | 0 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) |
167 | 0 | if(share->cookies) { |
168 | 0 | Curl_cookie_cleanup(share->cookies); |
169 | 0 | share->cookies = NULL; |
170 | 0 | } |
171 | | #else /* CURL_DISABLE_HTTP */ |
172 | | res = CURLSHE_NOT_BUILT_IN; |
173 | | #endif |
174 | 0 | break; |
175 | | |
176 | 0 | case CURL_LOCK_DATA_HSTS: |
177 | 0 | #ifndef CURL_DISABLE_HSTS |
178 | 0 | if(share->hsts) { |
179 | 0 | Curl_hsts_cleanup(&share->hsts); |
180 | 0 | } |
181 | | #else /* CURL_DISABLE_HSTS */ |
182 | | res = CURLSHE_NOT_BUILT_IN; |
183 | | #endif |
184 | 0 | break; |
185 | | |
186 | 0 | case CURL_LOCK_DATA_SSL_SESSION: |
187 | 0 | #ifdef USE_SSL |
188 | 0 | if(share->ssl_scache) { |
189 | 0 | Curl_ssl_scache_destroy(share->ssl_scache); |
190 | 0 | share->ssl_scache = NULL; |
191 | 0 | } |
192 | | #else |
193 | | res = CURLSHE_NOT_BUILT_IN; |
194 | | #endif |
195 | 0 | break; |
196 | | |
197 | 0 | case CURL_LOCK_DATA_CONNECT: |
198 | 0 | break; |
199 | | |
200 | 0 | default: |
201 | 0 | res = CURLSHE_BAD_OPTION; |
202 | 0 | break; |
203 | 0 | } |
204 | 0 | break; |
205 | | |
206 | 0 | case CURLSHOPT_LOCKFUNC: |
207 | 0 | lockfunc = va_arg(param, curl_lock_function); |
208 | 0 | share->lockfunc = lockfunc; |
209 | 0 | break; |
210 | | |
211 | 0 | case CURLSHOPT_UNLOCKFUNC: |
212 | 0 | unlockfunc = va_arg(param, curl_unlock_function); |
213 | 0 | share->unlockfunc = unlockfunc; |
214 | 0 | break; |
215 | | |
216 | 0 | case CURLSHOPT_USERDATA: |
217 | 0 | ptr = va_arg(param, void *); |
218 | 0 | share->clientdata = ptr; |
219 | 0 | break; |
220 | | |
221 | 0 | default: |
222 | 0 | res = CURLSHE_BAD_OPTION; |
223 | 0 | break; |
224 | 0 | } |
225 | | |
226 | 0 | va_end(param); |
227 | |
|
228 | 0 | return res; |
229 | 0 | } |
230 | | |
231 | | CURLSHcode |
232 | | curl_share_cleanup(CURLSH *sh) |
233 | 0 | { |
234 | 0 | struct Curl_share *share = sh; |
235 | 0 | if(!GOOD_SHARE_HANDLE(share)) |
236 | 0 | return CURLSHE_INVALID; |
237 | | |
238 | 0 | if(share->lockfunc) |
239 | 0 | share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE, |
240 | 0 | share->clientdata); |
241 | |
|
242 | 0 | if(share->dirty) { |
243 | 0 | if(share->unlockfunc) |
244 | 0 | share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata); |
245 | 0 | return CURLSHE_IN_USE; |
246 | 0 | } |
247 | | |
248 | 0 | if(share->specifier & (1 << CURL_LOCK_DATA_CONNECT)) { |
249 | 0 | Curl_cpool_destroy(&share->cpool); |
250 | 0 | } |
251 | |
|
252 | 0 | Curl_dnscache_destroy(&share->dnscache); |
253 | |
|
254 | 0 | #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) |
255 | 0 | Curl_cookie_cleanup(share->cookies); |
256 | 0 | #endif |
257 | |
|
258 | 0 | #ifndef CURL_DISABLE_HSTS |
259 | 0 | Curl_hsts_cleanup(&share->hsts); |
260 | 0 | #endif |
261 | |
|
262 | 0 | #ifdef USE_SSL |
263 | 0 | if(share->ssl_scache) { |
264 | 0 | Curl_ssl_scache_destroy(share->ssl_scache); |
265 | 0 | share->ssl_scache = NULL; |
266 | 0 | } |
267 | 0 | #endif |
268 | |
|
269 | 0 | Curl_psl_destroy(&share->psl); |
270 | 0 | Curl_close(&share->admin); |
271 | |
|
272 | 0 | if(share->unlockfunc) |
273 | 0 | share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata); |
274 | 0 | share->magic = 0; |
275 | 0 | free(share); |
276 | |
|
277 | 0 | return CURLSHE_OK; |
278 | 0 | } |
279 | | |
280 | | |
281 | | CURLSHcode |
282 | | Curl_share_lock(struct Curl_easy *data, curl_lock_data type, |
283 | | curl_lock_access accesstype) |
284 | 0 | { |
285 | 0 | struct Curl_share *share = data->share; |
286 | |
|
287 | 0 | if(!share) |
288 | 0 | return CURLSHE_INVALID; |
289 | | |
290 | 0 | if(share->specifier & (unsigned int)(1 << type)) { |
291 | 0 | if(share->lockfunc) /* only call this if set! */ |
292 | 0 | share->lockfunc(data, type, accesstype, share->clientdata); |
293 | 0 | } |
294 | | /* else if we do not share this, pretend successful lock */ |
295 | |
|
296 | 0 | return CURLSHE_OK; |
297 | 0 | } |
298 | | |
299 | | CURLSHcode |
300 | | Curl_share_unlock(struct Curl_easy *data, curl_lock_data type) |
301 | 0 | { |
302 | 0 | struct Curl_share *share = data->share; |
303 | |
|
304 | 0 | if(!share) |
305 | 0 | return CURLSHE_INVALID; |
306 | | |
307 | 0 | if(share->specifier & (unsigned int)(1 << type)) { |
308 | 0 | if(share->unlockfunc) /* only call this if set! */ |
309 | 0 | share->unlockfunc (data, type, share->clientdata); |
310 | 0 | } |
311 | |
|
312 | 0 | return CURLSHE_OK; |
313 | 0 | } |