~ubuntu-branches/ubuntu/maverick/strongswan/maverick

« back to all changes in this revision

Viewing changes to src/charon/credentials/sets/cert_cache.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2008-12-05 17:21:42 UTC
  • mfrom: (1.1.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20081205172142-9g77wgyzcj0blq7p
* New upstream release, fixes a MOBIKE issue.
  Closes: #507542: strongswan: endless loop
* Explicitly enable compilation with libcurl for CRL fetching
  Closes: #497756: strongswan: not compiled with curl support; crl 
                   fetching not available
* Enable compilation with SSH agent support.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
#include "cert_cache.h"
19
19
 
 
20
#include <time.h>
 
21
 
20
22
#include <daemon.h>
 
23
#include <utils/mutex.h>
21
24
#include <utils/linked_list.h>
22
 
#include <utils/mutex.h>
23
25
 
24
26
#define CACHE_SIZE 30
25
27
 
44
46
        /**
45
47
         * do we have an active enumerator
46
48
         */
47
 
        bool enumerating;
 
49
        refcount_t enumerating;
48
50
        
49
51
        /**
50
52
         * have we increased the cache without a check_cache?
52
54
        bool check_required;
53
55
        
54
56
        /**
55
 
         * mutex to lock relations list
 
57
         * read-write lock to sets list
56
58
         */
57
 
        mutex_t *mutex;
 
59
        rwlock_t *lock;
58
60
};
59
61
 
60
62
/**
88
90
        {
89
91
                this->check_required = TRUE;
90
92
        }
91
 
        else
92
 
        {
 
93
        else if (this->lock->try_write_lock(this->lock))
 
94
        {       /* never blocks, only done if lock is available */
93
95
                while (this->relations->get_count(this->relations) > CACHE_SIZE)
94
96
                {
95
97
                        relation_t *oldest = NULL, *current;
108
110
                        relation_destroy(oldest);
109
111
                }
110
112
                this->check_required = FALSE;
 
113
                this->lock->unlock(this->lock);
111
114
        }
112
115
}
113
116
 
121
124
        enumerator_t *enumerator;
122
125
        
123
126
        /* lookup cache */
124
 
        this->mutex->lock(this->mutex);
 
127
        this->lock->read_lock(this->lock);
125
128
        enumerator = this->relations->create_enumerator(this->relations);
126
129
        while (enumerator->enumerate(enumerator, &current))
127
130
        {
146
149
                }
147
150
        }
148
151
        enumerator->destroy(enumerator);
149
 
        this->mutex->unlock(this->mutex);
 
152
        this->lock->unlock(this->lock);
150
153
        if (found)
151
154
        {
152
155
                return TRUE;
161
164
        found->subject = subject->get_ref(subject);
162
165
        found->issuer = issuer->get_ref(issuer);
163
166
        found->last_use = time(NULL);
164
 
        this->mutex->lock(this->mutex);
 
167
        /* insert should be ok without lock */
165
168
        this->relations->insert_last(this->relations, found);
166
169
        check_cache(this);
167
 
        this->mutex->unlock(this->mutex);
168
170
        return TRUE;
169
171
}
170
172
 
230
232
 */
231
233
static void certs_destroy(cert_data_t *data)
232
234
{
233
 
        data->this->enumerating--;
 
235
        ref_put(&data->this->enumerating);
 
236
        data->this->lock->unlock(data->this->lock);
234
237
        if (data->this->check_required)
235
238
        {
236
239
                check_cache(data->this);
237
240
        }
238
 
        data->this->mutex->unlock(data->this->mutex);
239
241
        free(data);
240
242
}
241
243
 
258
260
        data->id = id;
259
261
        data->this = this;
260
262
        
261
 
        this->mutex->lock(this->mutex);
262
 
        this->enumerating++;
 
263
        this->lock->read_lock(this->lock);
 
264
        ref_get(&this->enumerating);
263
265
        return enumerator_create_filter(
264
266
                                                        this->relations->create_enumerator(this->relations),
265
267
                                                        (void*)certs_filter, data, (void*)certs_destroy);
266
268
}
267
269
 
268
270
/**
269
 
 * Implementation of credential_set_t.cache_cert.
270
 
 */
271
 
static void cache_cert(private_cert_cache_t *this, certificate_t *cert)
272
 
{
273
 
        /* TODO: implement caching */
274
 
}
275
 
 
276
 
/**
277
271
 * Implementation of cert_cache_t.flush.
278
272
 */
279
273
static void flush(private_cert_cache_t *this, certificate_type_t type)
281
275
        enumerator_t *enumerator;
282
276
        relation_t *relation;
283
277
        
284
 
        this->mutex->lock(this->mutex);
 
278
        this->lock->write_lock(this->lock);
285
279
        enumerator = this->relations->create_enumerator(this->relations);
286
280
        while (enumerator->enumerate(enumerator, &relation))
287
281
        {
293
287
                }
294
288
        }
295
289
        enumerator->destroy(enumerator);
296
 
        this->mutex->unlock(this->mutex);
 
290
        this->lock->unlock(this->lock);
297
291
}
298
292
 
299
293
/**
302
296
static void destroy(private_cert_cache_t *this)
303
297
{
304
298
        this->relations->destroy_function(this->relations, (void*)relation_destroy);
305
 
        this->mutex->destroy(this->mutex);
 
299
        this->lock->destroy(this->lock);
306
300
        free(this);
307
301
}
308
302
 
317
311
        this->public.set.create_cert_enumerator = (void*)create_enumerator;
318
312
        this->public.set.create_shared_enumerator = (void*)return_null;
319
313
        this->public.set.create_cdp_enumerator = (void*)return_null;
320
 
        this->public.set.cache_cert = (void*)cache_cert;
 
314
        this->public.set.cache_cert = (void*)nop;
321
315
        this->public.issued_by = (bool(*)(cert_cache_t*, certificate_t *subject, certificate_t *issuer))issued_by;
322
316
        this->public.flush = (void(*)(cert_cache_t*, certificate_type_t type))flush;
323
317
        this->public.destroy = (void(*)(cert_cache_t*))destroy;
324
318
        
325
319
        this->relations = linked_list_create();
326
 
        this->enumerating = FALSE;
 
320
        this->enumerating = 0;
327
321
        this->check_required = FALSE;
328
 
        this->mutex = mutex_create(MUTEX_RECURSIVE);
 
322
        this->lock = rwlock_create(RWLOCK_DEFAULT);
329
323
        
330
324
        return &this->public;
331
325
}