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