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