Replaced builder_t objects by simple builder_function_t functions
This commit is contained in:
@@ -54,11 +54,3 @@ ENUM(builder_part_names, BUILD_FROM_FILE, BUILD_END,
|
||||
"BUILD_END",
|
||||
);
|
||||
|
||||
/**
|
||||
* See header.
|
||||
*/
|
||||
void* builder_free(builder_t *this)
|
||||
{
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -21,16 +21,21 @@
|
||||
#ifndef BUILDER_H_
|
||||
#define BUILDER_H_
|
||||
|
||||
typedef struct builder_t builder_t;
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef enum builder_part_t builder_part_t;
|
||||
|
||||
/**
|
||||
* Constructor function which creates a new builder instance.
|
||||
* Constructor function to build credentials.
|
||||
*
|
||||
* @param subtype constructor specific subtype, e.g. certificate_type_t
|
||||
* @return builder to construct a instance of type
|
||||
* Any added parts are cloned/refcounted by the builder implementation, a
|
||||
* caller may need to free the passed ressources themself.
|
||||
*
|
||||
* @param subtype constructor specific subtype, e.g. a certificate_type_t
|
||||
* @param args list of builder part types, followed by parts, BUILD_END
|
||||
* @return builder specific credential, NULL on error
|
||||
*/
|
||||
typedef builder_t* (*builder_constructor_t)(int subtype);
|
||||
typedef void* (*builder_function_t)(int subtype, va_list args);
|
||||
|
||||
#include <library.h>
|
||||
|
||||
@@ -119,44 +124,4 @@ enum builder_part_t {
|
||||
*/
|
||||
extern enum_name_t *builder_part_names;
|
||||
|
||||
/**
|
||||
* Credential construction API.
|
||||
*
|
||||
* The builder allows the construction of credentials in a generic and
|
||||
* flexible way.
|
||||
*/
|
||||
struct builder_t {
|
||||
|
||||
/**
|
||||
* Add a part to the construct.
|
||||
*
|
||||
* Any added parts are cloned/refcounted by the builder implementation, a
|
||||
* caller may need to free the passed ressources themself.
|
||||
*
|
||||
* @param part kind of part
|
||||
* @param ... part specific variable argument
|
||||
*/
|
||||
void (*add)(builder_t *this, builder_part_t part, ...);
|
||||
|
||||
/**
|
||||
* Build the construct with all supplied parts.
|
||||
*
|
||||
* Once build() is called, the builder gets destroyed.
|
||||
*
|
||||
* @return specific interface, as requested with constructor.
|
||||
*/
|
||||
void* (*build)(builder_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper macro to cancel a build in a builder
|
||||
*/
|
||||
#define builder_cancel(builder) { (builder)->add = (void*)nop; \
|
||||
(builder)->build = (void*)builder_free; }
|
||||
|
||||
/**
|
||||
* Helper function for a cancelled build.
|
||||
*/
|
||||
void* builder_free(builder_t *this);
|
||||
|
||||
#endif /** BUILDER_H_ @}*/
|
||||
|
||||
@@ -64,55 +64,16 @@ struct entry_t {
|
||||
credential_type_t type;
|
||||
/** subtype of credential, e.g. certificate_type_t */
|
||||
int subtype;
|
||||
/** builder construction function */
|
||||
builder_constructor_t constructor;
|
||||
/** builder function */
|
||||
builder_function_t constructor;
|
||||
};
|
||||
|
||||
/**
|
||||
* type/subtype filter function for builder_enumerator
|
||||
*/
|
||||
static bool builder_filter(entry_t *data, entry_t **in, builder_t **out)
|
||||
{
|
||||
builder_t *builder;
|
||||
|
||||
if (data->type == (*in)->type &&
|
||||
data->subtype == (*in)->subtype)
|
||||
{
|
||||
builder = (*in)->constructor(data->subtype);
|
||||
if (builder)
|
||||
{
|
||||
*out = builder;
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
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->lock->read_lock(this->lock);
|
||||
return enumerator_create_cleaner(
|
||||
enumerator_create_filter(
|
||||
this->constructors->create_enumerator(this->constructors),
|
||||
(void*)builder_filter, data, free),
|
||||
(void*)this->lock->unlock, this->lock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of credential_factory_t.add_builder_constructor.
|
||||
*/
|
||||
static void add_builder(private_credential_factory_t *this,
|
||||
credential_type_t type, int subtype,
|
||||
builder_constructor_t constructor)
|
||||
builder_function_t constructor)
|
||||
{
|
||||
entry_t *entry = malloc_thing(entry_t);
|
||||
|
||||
@@ -128,7 +89,7 @@ static void add_builder(private_credential_factory_t *this,
|
||||
* Implementation of credential_factory_t.remove_builder.
|
||||
*/
|
||||
static void remove_builder(private_credential_factory_t *this,
|
||||
builder_constructor_t constructor)
|
||||
builder_function_t constructor)
|
||||
{
|
||||
enumerator_t *enumerator;
|
||||
entry_t *entry;
|
||||
@@ -154,92 +115,34 @@ static void* create(private_credential_factory_t *this, credential_type_t type,
|
||||
int subtype, ...)
|
||||
{
|
||||
enumerator_t *enumerator;
|
||||
builder_t *builder;
|
||||
builder_part_t part;
|
||||
entry_t *entry;
|
||||
va_list args;
|
||||
void* construct = NULL, *fn, *data;
|
||||
void *construct = NULL;
|
||||
int failures = 0;
|
||||
uintptr_t level;
|
||||
|
||||
level = (uintptr_t)pthread_getspecific(this->recursive);
|
||||
pthread_setspecific(this->recursive, (void*)level + 1);
|
||||
|
||||
enumerator = create_builder_enumerator(this, type, subtype);
|
||||
while (enumerator->enumerate(enumerator, &builder))
|
||||
this->lock->read_lock(this->lock);
|
||||
enumerator = this->constructors->create_enumerator(this->constructors);
|
||||
while (enumerator->enumerate(enumerator, &entry))
|
||||
{
|
||||
va_start(args, subtype);
|
||||
while (TRUE)
|
||||
if (entry->type == type && entry->subtype == subtype)
|
||||
{
|
||||
part = va_arg(args, builder_part_t);
|
||||
switch (part)
|
||||
va_start(args, subtype);
|
||||
construct = entry->constructor(subtype, args);
|
||||
va_end(args);
|
||||
if (construct)
|
||||
{
|
||||
case BUILD_END:
|
||||
break;
|
||||
case BUILD_BLOB_PEM:
|
||||
case BUILD_BLOB_ASN1_DER:
|
||||
case BUILD_BLOB_PGP:
|
||||
case BUILD_BLOB_DNSKEY:
|
||||
case BUILD_PASSPHRASE:
|
||||
case BUILD_SERIAL:
|
||||
case BUILD_RSA_MODULUS:
|
||||
case BUILD_RSA_PUB_EXP:
|
||||
case BUILD_RSA_PRIV_EXP:
|
||||
case BUILD_RSA_PRIME1:
|
||||
case BUILD_RSA_PRIME2:
|
||||
case BUILD_RSA_EXP1:
|
||||
case BUILD_RSA_EXP2:
|
||||
case BUILD_RSA_COEFF:
|
||||
builder->add(builder, part, va_arg(args, chunk_t));
|
||||
continue;
|
||||
case BUILD_X509_FLAG:
|
||||
builder->add(builder, part, va_arg(args, x509_flag_t));
|
||||
continue;
|
||||
case BUILD_KEY_SIZE:
|
||||
case BUILD_FROM_FD:
|
||||
builder->add(builder, part, va_arg(args, u_int));
|
||||
continue;
|
||||
case BUILD_DIGEST_ALG:
|
||||
builder->add(builder, part, va_arg(args, int));
|
||||
continue;
|
||||
case BUILD_NOT_BEFORE_TIME:
|
||||
case BUILD_NOT_AFTER_TIME:
|
||||
builder->add(builder, part, va_arg(args, time_t));
|
||||
continue;
|
||||
case BUILD_FROM_FILE:
|
||||
case BUILD_AGENT_SOCKET:
|
||||
case BUILD_SIGNING_KEY:
|
||||
case BUILD_PUBLIC_KEY:
|
||||
case BUILD_SUBJECT:
|
||||
case BUILD_SUBJECT_ALTNAMES:
|
||||
case BUILD_ISSUER:
|
||||
case BUILD_ISSUER_ALTNAMES:
|
||||
case BUILD_SIGNING_CERT:
|
||||
case BUILD_CA_CERT:
|
||||
case BUILD_CERT:
|
||||
case BUILD_IETF_GROUP_ATTR:
|
||||
case BUILD_SMARTCARD_KEYID:
|
||||
case BUILD_SMARTCARD_PIN:
|
||||
builder->add(builder, part, va_arg(args, void*));
|
||||
continue;
|
||||
case BUILD_PASSPHRASE_CALLBACK:
|
||||
fn = va_arg(args, void*);
|
||||
data = va_arg(args, void*);
|
||||
builder->add(builder, part, fn, data);
|
||||
continue;
|
||||
/* no default to get a compiler warning */
|
||||
break;
|
||||
}
|
||||
break;
|
||||
failures++;
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
construct = builder->build(builder);
|
||||
if (construct)
|
||||
{
|
||||
break;
|
||||
}
|
||||
failures++;
|
||||
}
|
||||
enumerator->destroy(enumerator);
|
||||
this->lock->unlock(this->lock);
|
||||
|
||||
if (!construct && !level)
|
||||
{
|
||||
enum_name_t *names = key_type_names;
|
||||
@@ -274,9 +177,8 @@ 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_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.add_builder = (void(*)(credential_factory_t*,credential_type_t type, int subtype, builder_function_t constructor))add_builder;
|
||||
this->public.remove_builder = (void(*)(credential_factory_t*,builder_function_t constructor))remove_builder;
|
||||
this->public.destroy = (void(*)(credential_factory_t*))destroy;
|
||||
|
||||
this->constructors = linked_list_create();
|
||||
|
||||
@@ -53,7 +53,7 @@ struct credential_factory_t {
|
||||
*
|
||||
* The variable argument list takes builder_part_t types followed
|
||||
* by the type specific value. The list must be terminated using BUILD_END.
|
||||
* All passed parts get cloned/refcounted by the builder implementations,
|
||||
* All passed parts get cloned/refcounted by the builder functions,
|
||||
* so free up allocated ressources after successful and unsuccessful
|
||||
* invocations.
|
||||
*
|
||||
@@ -66,34 +66,21 @@ struct credential_factory_t {
|
||||
int subtype, ...);
|
||||
|
||||
/**
|
||||
* Create an enumerator for a builder type.
|
||||
*
|
||||
* The build() method has to be called on each enumerated builder to
|
||||
* cleanup associated ressources.
|
||||
*
|
||||
* @param type type of credentials the builder creates
|
||||
* @param subtype type specific subtype, such as certificate_type_t
|
||||
* @return enumerator over builder_t
|
||||
*/
|
||||
enumerator_t* (*create_builder_enumerator)(credential_factory_t *this,
|
||||
credential_type_t type, int subtype);
|
||||
|
||||
/**
|
||||
* Register a builder_t constructor function.
|
||||
* Register a credential builder function.
|
||||
*
|
||||
* @param type type of credential the builder creates
|
||||
* @param constructor builder constructor function to register
|
||||
*/
|
||||
void (*add_builder)(credential_factory_t *this,
|
||||
credential_type_t type, int subtype,
|
||||
builder_constructor_t constructor);
|
||||
builder_function_t constructor);
|
||||
/**
|
||||
* Unregister a builder_t constructor function.
|
||||
* Unregister a credential builder function.
|
||||
*
|
||||
* @param constructor constructor function to unregister.
|
||||
*/
|
||||
void (*remove_builder)(credential_factory_t *this,
|
||||
builder_constructor_t constructor);
|
||||
builder_function_t constructor);
|
||||
|
||||
/**
|
||||
* Destroy a credential_factory instance.
|
||||
|
||||
Reference in New Issue
Block a user