From 8b80ab5a6950ce6515f477624794defd7531642a Mon Sep 17 00:00:00 2001 From: Rene Mayrhofer Date: Wed, 29 Oct 2008 11:11:01 +0000 Subject: [svn-upgrade] Integrating new upstream version, strongswan (4.2.8) --- src/libstrongswan/credentials/credential_factory.c | 161 ++++++--------------- 1 file changed, 44 insertions(+), 117 deletions(-) (limited to 'src/libstrongswan/credentials/credential_factory.c') diff --git a/src/libstrongswan/credentials/credential_factory.c b/src/libstrongswan/credentials/credential_factory.c index 52ee2060d..203317fa4 100644 --- a/src/libstrongswan/credentials/credential_factory.c +++ b/src/libstrongswan/credentials/credential_factory.c @@ -12,7 +12,7 @@ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * - * $Id: credential_factory.c 4059 2008-06-11 14:10:02Z martin $ + * $Id: credential_factory.c 4317 2008-09-02 11:00:13Z martin $ */ #include "credential_factory.h" @@ -62,31 +62,36 @@ struct entry_t { }; /** - * Implementation of credential_factory_t.create_builder. + * type/subtype filter function for builder_enumerator */ -static builder_t* create_builder(private_credential_factory_t *this, - credential_type_t type, int subtype) +static bool builder_filter(entry_t *data, entry_t **in, builder_t **out) { - enumerator_t *enumerator; - entry_t *entry; - builder_t *builder = NULL; - - this->mutex->lock(this->mutex); - enumerator = this->constructors->create_enumerator(this->constructors); - while (enumerator->enumerate(enumerator, &entry)) + if (data->type == (*in)->type && + data->subtype == (*in)->subtype) { - if (entry->type == type && entry->subtype == subtype) - { - builder = entry->constructor(subtype); - if (builder) - { - break; - } - } + *out = (*in)->constructor(data->subtype); + return TRUE; } - enumerator->destroy(enumerator); - this->mutex->unlock(this->mutex); - return builder; + return FALSE; +} + +/** + * Implementation of credential_factory_t.create_builder_enumerator. + */ +static enumerator_t* create_builder_enumerator( + private_credential_factory_t *this, credential_type_t type, int subtype) +{ + entry_t *data = malloc_thing(entry_t); + + data->type = type; + data->subtype = subtype; + + this->mutex->lock(this->mutex); + return enumerator_create_cleaner( + enumerator_create_filter( + this->constructors->create_enumerator(this->constructors), + (void*)builder_filter, data, free), + (void*)this->mutex->unlock, this->mutex); } /** @@ -135,18 +140,19 @@ static void remove_builder(private_credential_factory_t *this, static void* create(private_credential_factory_t *this, credential_type_t type, int subtype, ...) { + enumerator_t *enumerator; builder_t *builder; builder_part_t part; va_list args; + void* construct = NULL; - builder = create_builder(this, type, subtype); - if (builder) + enumerator = create_builder_enumerator(this, type, subtype); + while (enumerator->enumerate(enumerator, &builder)) { va_start(args, subtype); while (TRUE) { part = va_arg(args, builder_part_t); - switch (part) { case BUILD_END: @@ -167,6 +173,7 @@ static void* create(private_credential_factory_t *this, credential_type_t type, continue; case BUILD_BLOB_ASN1_PEM: case BUILD_FROM_FILE: + case BUILD_AGENT_SOCKET: case BUILD_SIGNING_KEY: case BUILD_PUBLIC_KEY: case BUILD_SUBJECT: @@ -179,105 +186,25 @@ static void* create(private_credential_factory_t *this, credential_type_t type, case BUILD_IETF_GROUP_ATTR: builder->add(builder, part, va_arg(args, void*)); continue; - default: - DBG1("builder part %N not supported by factory", - builder_part_names, part); - break; + /* no default to get a compiler warning */ } break; } va_end(args); - - return builder->build(builder); - } - else - { - DBG1("failed to create a builder for credential type %N," - " subtype (%d)", credential_type_names, type, subtype); - } - - /** shredder all data on failure */ - va_start(args, subtype); - while (TRUE) - { - part = va_arg(args, builder_part_t); - switch (part) + construct = builder->build(builder); + if (construct) { - case BUILD_END: - break; - case BUILD_BLOB_ASN1_DER: - { - chunk_t chunk = va_arg(args, chunk_t); - free(chunk.ptr); - continue; - } - case BUILD_SERIAL: - { - va_arg(args, chunk_t); - continue; - } - case BUILD_X509_FLAG: - { - va_arg(args, x509_flag_t); - continue; - } - case BUILD_KEY_SIZE: - { - va_arg(args, u_int); - continue; - } - case BUILD_NOT_BEFORE_TIME: - case BUILD_NOT_AFTER_TIME: - { - va_arg(args, time_t); - continue; - } - case BUILD_SIGNING_KEY: - { - private_key_t *private = va_arg(args, private_key_t*); - private->destroy(private); - continue; - } - case BUILD_PUBLIC_KEY: - { - public_key_t *public = va_arg(args, public_key_t*); - public->destroy(public); - continue; - } - case BUILD_SUBJECT: - case BUILD_SUBJECT_ALTNAME: - case BUILD_ISSUER: - case BUILD_ISSUER_ALTNAME: - { - identification_t *id = va_arg(args, identification_t*); - id->destroy(id); - continue; - } - case BUILD_SIGNING_CERT: - case BUILD_CA_CERT: - case BUILD_CERT: - { - certificate_t *cert = va_arg(args, certificate_t*); - cert->destroy(cert); - continue; - } - case BUILD_BLOB_ASN1_PEM: - case BUILD_FROM_FILE: - case BUILD_IETF_GROUP_ATTR: - { - va_arg(args, void*); - continue; - } - default: - DBG1("builder part %N not supported by factory", - builder_part_names, part); - continue; + break; } - break; } - va_end(args); - return NULL; + enumerator->destroy(enumerator); + if (!construct) + { + DBG1("failed to create a builder for credential type %N," + " subtype (%d)", credential_type_names, type, subtype); + } + return construct; } /** @@ -298,7 +225,7 @@ credential_factory_t *credential_factory_create() private_credential_factory_t *this = malloc_thing(private_credential_factory_t); this->public.create = (void*(*)(credential_factory_t*, credential_type_t type, int subtype, ...))create; - this->public.create_builder = (builder_t*(*)(credential_factory_t*, credential_type_t type, int subtype))create_builder; + this->public.create_builder_enumerator = (enumerator_t*(*)(credential_factory_t*, credential_type_t type, int subtype))create_builder_enumerator; this->public.add_builder = (void(*)(credential_factory_t*,credential_type_t type, int subtype, builder_constructor_t constructor))add_builder; this->public.remove_builder = (void(*)(credential_factory_t*,builder_constructor_t constructor))remove_builder; this->public.destroy = (void(*)(credential_factory_t*))destroy; -- cgit v1.2.3