Migrated credential manager to INIT/METHOD macros

This commit is contained in:
Martin Willi
2010-07-13 10:26:07 +02:00
parent 2ccc02a4fd
commit c3a9bef08e
+123 -157
View File
@@ -119,21 +119,9 @@ typedef struct {
enumerator_t *local; enumerator_t *local;
} sets_enumerator_t; } sets_enumerator_t;
/**
* destroy a sets_enumerator_t
*/
static void sets_enumerator_destroy(sets_enumerator_t *this)
{
DESTROY_IF(this->global);
DESTROY_IF(this->local);
free(this);
}
/** METHOD(enumerator_t, sets_enumerate, bool,
* sets_enumerator_t.enumerate sets_enumerator_t *this, credential_set_t **set)
*/
static bool sets_enumerator_enumerate(sets_enumerator_t *this,
credential_set_t **set)
{ {
if (this->global) if (this->global)
{ {
@@ -152,18 +140,27 @@ static bool sets_enumerator_enumerate(sets_enumerator_t *this,
return FALSE; return FALSE;
} }
METHOD(enumerator_t, sets_destroy, void,
sets_enumerator_t *this)
{
DESTROY_IF(this->global);
DESTROY_IF(this->local);
free(this);
}
/** /**
* create an enumerator over both, global and local sets * create an enumerator over both, global and local sets
*/ */
static enumerator_t *create_sets_enumerator(private_credential_manager_t *this) static enumerator_t *create_sets_enumerator(private_credential_manager_t *this)
{ {
sets_enumerator_t *enumerator;
linked_list_t *local; linked_list_t *local;
sets_enumerator_t *enumerator = malloc_thing(sets_enumerator_t);
enumerator->public.enumerate = (void*)sets_enumerator_enumerate; INIT(enumerator,
enumerator->public.destroy = (void*)sets_enumerator_destroy; .public.enumerate = (void*)_sets_enumerate,
enumerator->global = this->sets->create_enumerator(this->sets); .public.destroy = _sets_destroy,
enumerator->local = NULL; .global = this->sets->create_enumerator(this->sets),
);
local = this->local_sets->get(this->local_sets); local = this->local_sets->get(this->local_sets);
if (local) if (local)
{ {
@@ -190,12 +187,9 @@ static enumerator_t *create_cert(credential_set_t *set, cert_data_t *data)
data->id, data->trusted); data->id, data->trusted);
} }
/** METHOD(credential_manager_t, create_cert_enumerator, enumerator_t*,
* Implementation of credential_manager_t.create_cert_enumerator. private_credential_manager_t *this, certificate_type_t certificate,
*/ key_type_t key, identification_t *id, bool trusted)
static enumerator_t *create_cert_enumerator(private_credential_manager_t *this,
certificate_type_t certificate, key_type_t key,
identification_t *id, bool trusted)
{ {
cert_data_t *data = malloc_thing(cert_data_t); cert_data_t *data = malloc_thing(cert_data_t);
data->this = this; data->this = this;
@@ -210,12 +204,9 @@ static enumerator_t *create_cert_enumerator(private_credential_manager_t *this,
(void*)destroy_cert_data); (void*)destroy_cert_data);
} }
/** METHOD(credential_manager_t, get_cert, certificate_t*,
* Implementation of credential_manager_t.get_cert. private_credential_manager_t *this, certificate_type_t cert, key_type_t key,
*/ identification_t *id, bool trusted)
static certificate_t *get_cert(private_credential_manager_t *this,
certificate_type_t cert, key_type_t key,
identification_t *id, bool trusted)
{ {
certificate_t *current, *found = NULL; certificate_t *current, *found = NULL;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -247,17 +238,18 @@ static enumerator_t *create_cdp(credential_set_t *set, cdp_data_t *data)
{ {
return set->create_cdp_enumerator(set, data->type, data->id); return set->create_cdp_enumerator(set, data->type, data->id);
} }
/**
* Implementation of credential_manager_t.create_cdp_enumerator.
*/
static enumerator_t * create_cdp_enumerator(private_credential_manager_t *this,
certificate_type_t type, identification_t *id)
{
cdp_data_t *data = malloc_thing(cdp_data_t);
data->this = this;
data->type = type;
data->id = id;
METHOD(credential_manager_t, create_cdp_enumerator, enumerator_t*,
private_credential_manager_t *this, certificate_type_t type,
identification_t *id)
{
cdp_data_t *data;
INIT(data,
.this = this,
.type = type,
.id = id,
);
this->lock->read_lock(this->lock); this->lock->read_lock(this->lock);
return enumerator_create_nested(create_sets_enumerator(this), return enumerator_create_nested(create_sets_enumerator(this),
(void*)create_cdp, data, (void*)create_cdp, data,
@@ -282,18 +274,18 @@ static enumerator_t *create_private(credential_set_t *set, private_data_t *data)
} }
/** /**
* Implementation of credential_manager_t.create_private_enumerator. * Create an enumerator over private keys
*/ */
static enumerator_t* create_private_enumerator( static enumerator_t *create_private_enumerator(
private_credential_manager_t *this, private_credential_manager_t *this, key_type_t key, identification_t *keyid)
key_type_t key, identification_t *keyid)
{ {
private_data_t *data; private_data_t *data;
data = malloc_thing(private_data_t); INIT(data,
data->this = this; .this = this,
data->type = key; .type = key,
data->keyid = keyid; .keyid = keyid,
);
this->lock->read_lock(this->lock); this->lock->read_lock(this->lock);
return enumerator_create_nested(create_sets_enumerator(this), return enumerator_create_nested(create_sets_enumerator(this),
(void*)create_private, data, (void*)create_private, data,
@@ -301,10 +293,10 @@ static enumerator_t* create_private_enumerator(
} }
/** /**
* Implementation of credential_manager_t.get_private_by_keyid. * Look up a private key by its key identifier
*/ */
static private_key_t *get_private_by_keyid(private_credential_manager_t *this, static private_key_t* get_private_by_keyid(private_credential_manager_t *this,
key_type_t key, identification_t *keyid) key_type_t key, identification_t *keyid)
{ {
private_key_t *found = NULL; private_key_t *found = NULL;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -335,31 +327,27 @@ static enumerator_t *create_shared(credential_set_t *set, shared_data_t *data)
return set->create_shared_enumerator(set, data->type, data->me, data->other); return set->create_shared_enumerator(set, data->type, data->me, data->other);
} }
/** METHOD(credential_manager_t, create_shared_enumerator, enumerator_t*,
* Implementation of credential_manager_t.create_shared_enumerator. private_credential_manager_t *this, shared_key_type_t type,
*/ identification_t *me, identification_t *other)
static enumerator_t *create_shared_enumerator(private_credential_manager_t *this,
shared_key_type_t type,
identification_t *me, identification_t *other)
{ {
shared_data_t *data = malloc_thing(shared_data_t); shared_data_t *data;
data->this = this;
data->type = type;
data->me = me;
data->other = other;
INIT(data,
.this = this,
.type = type,
.me = me,
.other = other,
);
this->lock->read_lock(this->lock); this->lock->read_lock(this->lock);
return enumerator_create_nested(create_sets_enumerator(this), return enumerator_create_nested(create_sets_enumerator(this),
(void*)create_shared, data, (void*)create_shared, data,
(void*)destroy_shared_data); (void*)destroy_shared_data);
} }
/** METHOD(credential_manager_t, get_shared, shared_key_t*,
* Implementation of credential_manager_t.get_shared. private_credential_manager_t *this, shared_key_type_t type,
*/ identification_t *me, identification_t *other)
static shared_key_t *get_shared(private_credential_manager_t *this,
shared_key_type_t type, identification_t *me,
identification_t *other)
{ {
shared_key_t *current, *found = NULL; shared_key_t *current, *found = NULL;
id_match_t *best_me = ID_MATCH_NONE, *best_other = ID_MATCH_NONE; id_match_t *best_me = ID_MATCH_NONE, *best_other = ID_MATCH_NONE;
@@ -411,10 +399,8 @@ static void remove_local_set(private_credential_manager_t *this,
sets->remove(sets, set, NULL); sets->remove(sets, set, NULL);
} }
/** METHOD(credential_manager_t, cache_cert, void,
* Implementation of credential_manager_t.cache_cert. private_credential_manager_t *this, certificate_t *cert)
*/
static void cache_cert(private_credential_manager_t *this, certificate_t *cert)
{ {
credential_set_t *set; credential_set_t *set;
enumerator_t *enumerator; enumerator_t *enumerator;
@@ -1247,11 +1233,8 @@ typedef struct {
auth_cfg_t *auth; auth_cfg_t *auth;
} trusted_enumerator_t; } trusted_enumerator_t;
/** METHOD(enumerator_t, trusted_enumerate, bool,
* Implements trusted_enumerator_t.enumerate trusted_enumerator_t *this, certificate_t **cert, auth_cfg_t **auth)
*/
static bool trusted_enumerate(trusted_enumerator_t *this,
certificate_t **cert, auth_cfg_t **auth)
{ {
certificate_t *current; certificate_t *current;
@@ -1313,10 +1296,8 @@ static bool trusted_enumerate(trusted_enumerator_t *this,
return FALSE; return FALSE;
} }
/** METHOD(enumerator_t, trusted_destroy, void,
* Implements trusted_enumerator_t.destroy trusted_enumerator_t *this)
*/
static void trusted_destroy(trusted_enumerator_t *this)
{ {
DESTROY_IF(this->pretrusted); DESTROY_IF(this->pretrusted);
DESTROY_IF(this->auth); DESTROY_IF(this->auth);
@@ -1330,19 +1311,18 @@ static void trusted_destroy(trusted_enumerator_t *this)
static enumerator_t *create_trusted_enumerator(private_credential_manager_t *this, static enumerator_t *create_trusted_enumerator(private_credential_manager_t *this,
key_type_t type, identification_t *id, bool online) key_type_t type, identification_t *id, bool online)
{ {
trusted_enumerator_t *enumerator = malloc_thing(trusted_enumerator_t); trusted_enumerator_t *enumerator;
enumerator->public.enumerate = (void*)trusted_enumerate;
enumerator->public.destroy = (void*)trusted_destroy;
enumerator->candidates = NULL;
enumerator->this = this;
enumerator->type = type;
enumerator->id = id;
enumerator->online = online;
enumerator->pretrusted = NULL;
enumerator->auth = NULL;
INIT(enumerator,
.public = {
.enumerate = (void*)_trusted_enumerate,
.destroy = _trusted_destroy,
},
.this = this,
.type = type,
.id = id,
.online = online,
);
return &enumerator->public; return &enumerator->public;
} }
@@ -1362,11 +1342,8 @@ typedef struct {
auth_cfg_wrapper_t *wrapper; auth_cfg_wrapper_t *wrapper;
} public_enumerator_t; } public_enumerator_t;
/** METHOD(enumerator_t, public_enumerate, bool,
* Implements public_enumerator_t.enumerate public_enumerator_t *this, public_key_t **key, auth_cfg_t **auth)
*/
static bool public_enumerate(public_enumerator_t *this,
public_key_t **key, auth_cfg_t **auth)
{ {
certificate_t *cert; certificate_t *cert;
@@ -1383,10 +1360,8 @@ static bool public_enumerate(public_enumerator_t *this,
return FALSE; return FALSE;
} }
/** METHOD(enumerator_t, public_destroy, void,
* Implements public_enumerator_t.destroy public_enumerator_t *this)
*/
static void public_destroy(public_enumerator_t *this)
{ {
DESTROY_IF(this->current); DESTROY_IF(this->current);
this->inner->destroy(this->inner); this->inner->destroy(this->inner);
@@ -1402,20 +1377,20 @@ static void public_destroy(public_enumerator_t *this)
free(this); free(this);
} }
/** METHOD(credential_manager_t, create_public_enumerator, enumerator_t*,
* Implementation of credential_manager_t.create_public_enumerator. private_credential_manager_t *this, key_type_t type, identification_t *id,
*/ auth_cfg_t *auth)
static enumerator_t* create_public_enumerator(private_credential_manager_t *this,
key_type_t type, identification_t *id, auth_cfg_t *auth)
{ {
public_enumerator_t *enumerator = malloc_thing(public_enumerator_t); public_enumerator_t *enumerator;
enumerator->public.enumerate = (void*)public_enumerate; INIT(enumerator,
enumerator->public.destroy = (void*)public_destroy; .public = {
enumerator->inner = create_trusted_enumerator(this, type, id, TRUE); .enumerate = (void*)_public_enumerate,
enumerator->this = this; .destroy = _public_destroy,
enumerator->current = NULL; },
enumerator->wrapper = NULL; .inner = create_trusted_enumerator(this, type, id, TRUE),
.this = this,
);
if (auth) if (auth)
{ {
enumerator->wrapper = auth_cfg_wrapper_create(auth); enumerator->wrapper = auth_cfg_wrapper_create(auth);
@@ -1524,12 +1499,9 @@ static private_key_t *get_private_by_cert(private_credential_manager_t *this,
return private; return private;
} }
/** METHOD(credential_manager_t, get_private, private_key_t*,
* Implementation of credential_manager_t.get_private. private_credential_manager_t *this, key_type_t type, identification_t *id,
*/ auth_cfg_t *auth)
static private_key_t *get_private(private_credential_manager_t *this,
key_type_t type, identification_t *id,
auth_cfg_t *auth)
{ {
enumerator_t *enumerator; enumerator_t *enumerator;
certificate_t *cert; certificate_t *cert;
@@ -1601,40 +1573,30 @@ static private_key_t *get_private(private_credential_manager_t *this,
return private; return private;
} }
/** METHOD(credential_manager_t, flush_cache, void,
* Implementation of credential_manager_t.flush_cache. private_credential_manager_t *this, certificate_type_t type)
*/
static void flush_cache(private_credential_manager_t *this,
certificate_type_t type)
{ {
this->cache->flush(this->cache, type); this->cache->flush(this->cache, type);
} }
/** METHOD(credential_manager_t, add_set, void,
* Implementation of credential_manager_t.add_set. private_credential_manager_t *this, credential_set_t *set)
*/
static void add_set(private_credential_manager_t *this,
credential_set_t *set)
{ {
this->lock->write_lock(this->lock); this->lock->write_lock(this->lock);
this->sets->insert_last(this->sets, set); this->sets->insert_last(this->sets, set);
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(credential_manager_t, remove_set, void,
* Implementation of credential_manager_t.remove_set. private_credential_manager_t *this, credential_set_t *set)
*/
static void remove_set(private_credential_manager_t *this, credential_set_t *set)
{ {
this->lock->write_lock(this->lock); this->lock->write_lock(this->lock);
this->sets->remove(this->sets, set, NULL); this->sets->remove(this->sets, set, NULL);
this->lock->unlock(this->lock); this->lock->unlock(this->lock);
} }
/** METHOD(credential_manager_t, destroy, void,
* Implementation of credential_manager_t.destroy private_credential_manager_t *this)
*/
static void destroy(private_credential_manager_t *this)
{ {
cache_queue(this); cache_queue(this);
this->cache_queue->destroy(this->cache_queue); this->cache_queue->destroy(this->cache_queue);
@@ -1652,28 +1614,32 @@ static void destroy(private_credential_manager_t *this)
*/ */
credential_manager_t *credential_manager_create() credential_manager_t *credential_manager_create()
{ {
private_credential_manager_t *this = malloc_thing(private_credential_manager_t); private_credential_manager_t *this;
this->public.create_cert_enumerator = (enumerator_t *(*)(credential_manager_t *this,certificate_type_t cert, key_type_t key,identification_t *id,bool))create_cert_enumerator; INIT(this,
this->public.create_shared_enumerator = (enumerator_t *(*)(credential_manager_t *this, shared_key_type_t type,identification_t *me, identification_t *other))create_shared_enumerator; .public = {
this->public.create_cdp_enumerator = (enumerator_t *(*)(credential_manager_t*, certificate_type_t type, identification_t *id))create_cdp_enumerator; .create_cert_enumerator = _create_cert_enumerator,
this->public.get_cert = (certificate_t *(*)(credential_manager_t *this,certificate_type_t cert, key_type_t key,identification_t *, bool))get_cert; .create_shared_enumerator = _create_shared_enumerator,
this->public.get_shared = (shared_key_t *(*)(credential_manager_t *this,shared_key_type_t type,identification_t *me, identification_t *other))get_shared; .create_cdp_enumerator = _create_cdp_enumerator,
this->public.get_private = (private_key_t*(*)(credential_manager_t*, key_type_t type, identification_t *, auth_cfg_t*))get_private; .get_cert = _get_cert,
this->public.create_public_enumerator = (enumerator_t*(*)(credential_manager_t*, key_type_t type, identification_t *id, auth_cfg_t *aut))create_public_enumerator; .get_shared = _get_shared,
this->public.flush_cache = (void(*)(credential_manager_t*, certificate_type_t type))flush_cache; .get_private = _get_private,
this->public.cache_cert = (void(*)(credential_manager_t*, certificate_t *cert))cache_cert; .create_public_enumerator = _create_public_enumerator,
this->public.add_set = (void(*)(credential_manager_t*, credential_set_t *set))add_set; .flush_cache = _flush_cache,
this->public.remove_set = (void(*)(credential_manager_t*, credential_set_t *set))remove_set; .cache_cert = _cache_cert,
this->public.destroy = (void(*)(credential_manager_t*))destroy; .add_set = _add_set,
.remove_set = _remove_set,
.destroy = _destroy,
},
.sets = linked_list_create(),
.cache = cert_cache_create(),
.cache_queue = linked_list_create(),
.lock = rwlock_create(RWLOCK_TYPE_DEFAULT),
.queue_mutex = mutex_create(MUTEX_TYPE_DEFAULT),
);
this->sets = linked_list_create();
this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy); this->local_sets = thread_value_create((thread_cleanup_t)this->sets->destroy);
this->cache = cert_cache_create();
this->cache_queue = linked_list_create();
this->sets->insert_first(this->sets, this->cache); this->sets->insert_first(this->sets, this->cache);
this->lock = rwlock_create(RWLOCK_TYPE_DEFAULT);
this->queue_mutex = mutex_create(MUTEX_TYPE_DEFAULT);
return &this->public; return &this->public;
} }