Merge pull request #25 from trtom/master
[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         if(!share->sslsession)
95           return CURLSHE_NOMEM;
96       }
97       break;
98 #else
99       return CURLSHE_NOT_BUILT_IN;
100 #endif
101
102     case CURL_LOCK_DATA_CONNECT:     /* not supported (yet) */
103
104     default:
105       return CURLSHE_BAD_OPTION;
106     }
107     break;
108
109   case CURLSHOPT_UNSHARE:
110     /* this is a type this share will no longer share */
111     type = va_arg(param, int);
112     share->specifier &= ~(1<<type);
113     switch( type ) {
114     case CURL_LOCK_DATA_DNS:
115       if(share->hostcache) {
116         Curl_hash_destroy(share->hostcache);
117         share->hostcache = NULL;
118       }
119       break;
120
121     case CURL_LOCK_DATA_COOKIE:
122 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
123       if(share->cookies) {
124         Curl_cookie_cleanup(share->cookies);
125         share->cookies = NULL;
126       }
127       break;
128 #else   /* CURL_DISABLE_HTTP */
129       return CURLSHE_NOT_BUILT_IN;
130 #endif
131
132     case CURL_LOCK_DATA_SSL_SESSION:
133 #ifdef USE_SSL
134       if(share->sslsession) {
135         free(share->sslsession);
136         share->sslsession = NULL;
137         share->nsslsession = 0;
138       }
139       break;
140 #else
141       return CURLSHE_NOT_BUILT_IN;
142 #endif
143
144     case CURL_LOCK_DATA_CONNECT:
145       break;
146
147     default:
148       return CURLSHE_BAD_OPTION;
149     }
150     break;
151
152   case CURLSHOPT_LOCKFUNC:
153     lockfunc = va_arg(param, curl_lock_function);
154     share->lockfunc = lockfunc;
155     break;
156
157   case CURLSHOPT_UNLOCKFUNC:
158     unlockfunc = va_arg(param, curl_unlock_function);
159     share->unlockfunc = unlockfunc;
160     break;
161
162   case CURLSHOPT_USERDATA:
163     ptr = va_arg(param, void *);
164     share->clientdata = ptr;
165     break;
166
167   default:
168     return CURLSHE_BAD_OPTION;
169   }
170
171   return CURLSHE_OK;
172 }
173
174 CURLSHcode
175 curl_share_cleanup(CURLSH *sh)
176 {
177   struct Curl_share *share = (struct Curl_share *)sh;
178
179   if(share == NULL)
180     return CURLSHE_INVALID;
181
182   if(share->lockfunc)
183     share->lockfunc(NULL, CURL_LOCK_DATA_SHARE, CURL_LOCK_ACCESS_SINGLE,
184                     share->clientdata);
185
186   if(share->dirty) {
187     if(share->unlockfunc)
188       share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
189     return CURLSHE_IN_USE;
190   }
191
192   if(share->hostcache) {
193     Curl_hash_destroy(share->hostcache);
194     share->hostcache = NULL;
195   }
196
197 #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES)
198   if(share->cookies)
199     Curl_cookie_cleanup(share->cookies);
200 #endif
201
202 #ifdef USE_SSL
203   if(share->sslsession) {
204     unsigned int i;
205     for(i = 0; i < share->nsslsession; ++i)
206       Curl_ssl_kill_session(&(share->sslsession[i]));
207     free(share->sslsession);
208   }
209 #endif
210
211   if(share->unlockfunc)
212     share->unlockfunc(NULL, CURL_LOCK_DATA_SHARE, share->clientdata);
213   free(share);
214
215   return CURLSHE_OK;
216 }
217
218
219 CURLSHcode
220 Curl_share_lock(struct SessionHandle *data, curl_lock_data type,
221                 curl_lock_access accesstype)
222 {
223   struct Curl_share *share = data->share;
224
225   if(share == NULL)
226     return CURLSHE_INVALID;
227
228   if(share->specifier & (1<<type)) {
229     if(share->lockfunc) /* only call this if set! */
230       share->lockfunc(data, type, accesstype, share->clientdata);
231   }
232   /* else if we don't share this, pretend successful lock */
233
234   return CURLSHE_OK;
235 }
236
237 CURLSHcode
238 Curl_share_unlock(struct SessionHandle *data, curl_lock_data type)
239 {
240   struct Curl_share *share = data->share;
241
242   if(share == NULL)
243     return CURLSHE_INVALID;
244
245   if(share->specifier & (1<<type)) {
246     if(share->unlockfunc) /* only call this if set! */
247       share->unlockfunc (data, type, share->clientdata);
248   }
249
250   return CURLSHE_OK;
251 }