1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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.haxx.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 ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #include <curl/curl.h>
26 #include "urldata.h"
27 #include "share.h"
28 #include "psl.h"
29 #include "vtls/vtls.h"
30 #include "curl_memory.h"
31
32 /* The last #include file should be: */
33 #include "memdebug.h"
34
35 struct Curl_share *
curl_share_init(void)36 curl_share_init(void)
37 {
38 struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
39 if(share) {
40 share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
41
42 if(Curl_mk_dnscache(&share->hostcache)) {
43 free(share);
44 return NULL;
45 }
46 }
47
48 return share;
49 }
50
51 #undef curl_share_setopt
52 CURLSHcode
curl_share_setopt(struct Curl_share * share,CURLSHoption option,...)53 curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
54 {
55 va_list param;
56 int type;
57 curl_lock_function lockfunc;
58 curl_unlock_function unlockfunc;
59 void *ptr;
60 CURLSHcode res = CURLSHE_OK;
61
62 if(share->dirty)
63 /* don't allow setting options while one or more handles are already
64 using this share */
65 return CURLSHE_IN_USE;
66
67 va_start(param, option);
68
69 switch(option) {
70 case CURLSHOPT_SHARE:
71 /* this is a type this share will share */
72 type = va_arg(param, int);
73
74 switch(type) {
75 case CURL_LOCK_DATA_DNS:
76 break;
77
78 case CURL_LOCK_DATA_COOKIE:
79 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
80 if(!share->cookies) {
81 share->cookies = Curl_cookie_init(NULL, NULL, NULL, TRUE);
82 if(!share->cookies)
83 res = CURLSHE_NOMEM;
84 }
85 #else /* CURL_DISABLE_HTTP */
86 res = CURLSHE_NOT_BUILT_IN;
87 #endif
88 break;
89
90 case CURL_LOCK_DATA_SSL_SESSION:
91 #ifdef USE_SSL
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)
98 res = CURLSHE_NOMEM;
99 }
100 #else
101 res = CURLSHE_NOT_BUILT_IN;
102 #endif
103 break;
104
105 case CURL_LOCK_DATA_CONNECT:
106 if(Curl_conncache_init(&share->conn_cache, 103))
107 res = CURLSHE_NOMEM;
108 break;
109
110 case CURL_LOCK_DATA_PSL:
111 #ifndef USE_LIBPSL
112 res = CURLSHE_NOT_BUILT_IN;
113 #endif
114 break;
115
116 default:
117 res = CURLSHE_BAD_OPTION;
118 }
119 if(!res)
120 share->specifier |= (1<<type);
121 break;
122
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);
127 switch(type) {
128 case CURL_LOCK_DATA_DNS:
129 break;
130
131 case CURL_LOCK_DATA_COOKIE:
132 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
133 if(share->cookies) {
134 Curl_cookie_cleanup(share->cookies);
135 share->cookies = NULL;
136 }
137 #else /* CURL_DISABLE_HTTP */
138 res = CURLSHE_NOT_BUILT_IN;
139 #endif
140 break;
141
142 case CURL_LOCK_DATA_SSL_SESSION:
143 #ifdef USE_SSL
144 Curl_safefree(share->sslsession);
145 #else
146 res = CURLSHE_NOT_BUILT_IN;
147 #endif
148 break;
149
150 case CURL_LOCK_DATA_CONNECT:
151 break;
152
153 default:
154 res = CURLSHE_BAD_OPTION;
155 break;
156 }
157 break;
158
159 case CURLSHOPT_LOCKFUNC:
160 lockfunc = va_arg(param, curl_lock_function);
161 share->lockfunc = lockfunc;
162 break;
163
164 case CURLSHOPT_UNLOCKFUNC:
165 unlockfunc = va_arg(param, curl_unlock_function);
166 share->unlockfunc = unlockfunc;
167 break;
168
169 case CURLSHOPT_USERDATA:
170 ptr = va_arg(param, void *);
171 share->clientdata = ptr;
172 break;
173
174 default:
175 res = CURLSHE_BAD_OPTION;
176 break;
177 }
178
179 va_end(param);
180
181 return res;
182 }
183
184 CURLSHcode
curl_share_cleanup(struct Curl_share * share)185 curl_share_cleanup(struct Curl_share *share)
186 {
187 if(share == NULL)
188 return CURLSHE_INVALID;
189
190 if(share->lockfunc)
191 share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
192 share->clientdata);
193
194 if(share->dirty) {
195 if(share->unlockfunc)
196 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
197 return CURLSHE_IN_USE;
198 }
199
200 Curl_conncache_close_all_connections(&share->conn_cache);
201 Curl_conncache_destroy(&share->conn_cache);
202 Curl_hash_destroy(&share->hostcache);
203
204 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
205 Curl_cookie_cleanup(share->cookies);
206 #endif
207
208 #ifdef USE_SSL
209 if(share->sslsession) {
210 size_t i;
211 for(i = 0; i < share->max_ssl_sessions; i++)
212 Curl_ssl_kill_session(&(share->sslsession[i]));
213 free(share->sslsession);
214 }
215 #endif
216
217 Curl_psl_destroy(&share->psl);
218
219 if(share->unlockfunc)
220 share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
221 free(share);
222
223 return CURLSHE_OK;
224 }
225
226
227 CURLSHcode
Curl_share_lock(struct Curl_easy * data,curl_lock_data type,curl_lock_access accesstype)228 Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
229 curl_lock_access accesstype)
230 {
231 struct Curl_share *share = data->share;
232
233 if(share == NULL)
234 return CURLSHE_INVALID;
235
236 if(share->specifier & (1<<type)) {
237 if(share->lockfunc) /* only call this if set! */
238 share->lockfunc(data, type, accesstype, share->clientdata);
239 }
240 /* else if we don't share this, pretend successful lock */
241
242 return CURLSHE_OK;
243 }
244
245 CURLSHcode
Curl_share_unlock(struct Curl_easy * data,curl_lock_data type)246 Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
247 {
248 struct Curl_share *share = data->share;
249
250 if(share == NULL)
251 return CURLSHE_INVALID;
252
253 if(share->specifier & (1<<type)) {
254 if(share->unlockfunc) /* only call this if set! */
255 share->unlockfunc (data, type, share->clientdata);
256 }
257
258 return CURLSHE_OK;
259 }
260