403563fdd6e6291d464349648296e14f22e119d8
[platform/upstream/cmake.git] / Utilities / cmcurl / lib / share.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2021, 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  ***************************************************************************/
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 *
36 curl_share_init(void)
37 {
38   struct Curl_share *share = calloc(1, sizeof(struct Curl_share));
39   if(share) {
40     share->magic = CURL_GOOD_SHARE;
41     share->specifier |= (1<<CURL_LOCK_DATA_SHARE);
42     Curl_init_dnscache(&share->hostcache);
43   }
44
45   return share;
46 }
47
48 #undef curl_share_setopt
49 CURLSHcode
50 curl_share_setopt(struct Curl_share *share, CURLSHoption option, ...)
51 {
52   va_list param;
53   int type;
54   curl_lock_function lockfunc;
55   curl_unlock_function unlockfunc;
56   void *ptr;
57   CURLSHcode res = CURLSHE_OK;
58
59   if(!GOOD_SHARE_HANDLE(share))
60     return CURLSHE_INVALID;
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
185 curl_share_cleanup(struct Curl_share *share)
186 {
187   if(!GOOD_SHARE_HANDLE(share))
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   share->magic = 0;
222   free(share);
223
224   return CURLSHE_OK;
225 }
226
227
228 CURLSHcode
229 Curl_share_lock(struct Curl_easy *data, curl_lock_data type,
230                 curl_lock_access accesstype)
231 {
232   struct Curl_share *share = data->share;
233
234   if(!share)
235     return CURLSHE_INVALID;
236
237   if(share->specifier & (1<<type)) {
238     if(share->lockfunc) /* only call this if set! */
239       share->lockfunc(data, type, accesstype, share->clientdata);
240   }
241   /* else if we don't share this, pretend successful lock */
242
243   return CURLSHE_OK;
244 }
245
246 CURLSHcode
247 Curl_share_unlock(struct Curl_easy *data, curl_lock_data type)
248 {
249   struct Curl_share *share = data->share;
250
251   if(!share)
252     return CURLSHE_INVALID;
253
254   if(share->specifier & (1<<type)) {
255     if(share->unlockfunc) /* only call this if set! */
256       share->unlockfunc (data, type, share->clientdata);
257   }
258
259   return CURLSHE_OK;
260 }