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