- return value cleanup

This commit is contained in:
Martin Willi
2005-11-28 20:29:47 +00:00
parent 3fe058703f
commit d048df5cab
85 changed files with 1086 additions and 2680 deletions
+3 -8
View File
@@ -254,7 +254,7 @@ status_t get_supported_payloads (private_message_t *this, supported_payload_entr
*/ */
static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id) static void set_ike_sa_id (private_message_t *this,ike_sa_id_t *ike_sa_id)
{ {
ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)); this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
} }
/** /**
@@ -266,7 +266,7 @@ static status_t get_ike_sa_id (private_message_t *this,ike_sa_id_t **ike_sa_id)
{ {
return FAILED; return FAILED;
} }
this->ike_sa_id->clone(this->ike_sa_id,ike_sa_id); *ike_sa_id = this->ike_sa_id->clone(this->ike_sa_id);
return SUCCESS; return SUCCESS;
} }
@@ -641,12 +641,7 @@ static status_t parse_body(private_message_t *this, crypter_t *crypter, signer_t
/* get next payload type */ /* get next payload type */
current_payload_type = current_payload->get_next_type(current_payload); current_payload_type = current_payload->get_next_type(current_payload);
status = this->payloads->insert_last(this->payloads,current_payload); this->payloads->insert_last(this->payloads,current_payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "%s on adding payload", mapping_find(status_m, status));
return status;;
}
} }
return this->public.verify(&(this->public)); return this->public.verify(&(this->public));
@@ -287,9 +287,9 @@ static size_t get_length(private_encryption_payload_t *this)
/** /**
* Implementation of payload_t.create_payload_iterator. * Implementation of payload_t.create_payload_iterator.
*/ */
static status_t create_payload_iterator (private_encryption_payload_t *this, iterator_t **iterator, bool forward) static void create_payload_iterator (private_encryption_payload_t *this, iterator_t **iterator, bool forward)
{ {
return (this->payloads->create_iterator(this->payloads, iterator, forward)); this->payloads->create_iterator(this->payloads, iterator, forward);
} }
/** /**
+1 -5
View File
@@ -126,11 +126,7 @@ static status_t verify(private_sa_payload_t *this)
} }
/* check proposal numbering */ /* check proposal numbering */
status = this->proposals->create_iterator(this->proposals,&iterator,TRUE); this->proposals->create_iterator(this->proposals,&iterator,TRUE);
if (status != SUCCESS)
{
return status;
}
while(iterator->has_next(iterator)) while(iterator->has_next(iterator))
{ {
+6 -18
View File
@@ -1,7 +1,7 @@
/** /**
* @file host.c * @file host.c
* *
* @brief host object, identifies a host and defines some useful functions on it. * @brief Implementation of host_t.
* *
*/ */
@@ -110,28 +110,20 @@ static u_int16_t get_port(private_host_t *this)
/** /**
* Implements host_t.destroy * Implements host_t.destroy
*/ */
static status_t destroy(private_host_t *this) static void destroy(private_host_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/** /**
* Implements host_t.clone. * Implements host_t.clone.
*/ */
static status_t clone(private_host_t *this, host_t **other) static private_host_t *clone(private_host_t *this)
{ {
private_host_t *new = allocator_alloc_thing(private_host_t); private_host_t *new = allocator_alloc_thing(private_host_t);
if (new == NULL)
{
return OUT_OF_RES;
}
memcpy(new, this, sizeof(private_host_t)); memcpy(new, this, sizeof(private_host_t));
*other = (host_t*)new; return new;
return SUCCESS;
} }
@@ -141,17 +133,13 @@ static status_t clone(private_host_t *this, host_t **other)
host_t *host_create(int family, char *address, u_int16_t port) host_t *host_create(int family, char *address, u_int16_t port)
{ {
private_host_t *this = allocator_alloc_thing(private_host_t); private_host_t *this = allocator_alloc_thing(private_host_t);
if (this == NULL)
{
return NULL;
}
this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr; this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr;
this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len; this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
this->public.clone = (status_t (*) (host_t*, host_t**))clone; this->public.clone = (host_t* (*) (host_t*))clone;
this->public.get_address = (char* (*) (host_t *))get_address; this->public.get_address = (char* (*) (host_t *))get_address;
this->public.get_port = (u_int16_t (*) (host_t *))get_port; this->public.get_port = (u_int16_t (*) (host_t *))get_port;
this->public.destroy = (status_t (*) (host_t*))destroy; this->public.destroy = (void (*) (host_t*))destroy;
this->family = family; this->family = family;
+8 -10
View File
@@ -1,7 +1,7 @@
/** /**
* @file host.h * @file host.h
* *
* @brief host object, identifies a host and defines some useful functions on it. * @brief Interface of host_t.
* *
*/ */
@@ -36,19 +36,18 @@
typedef struct host_t host_t; typedef struct host_t host_t;
/** /**
* @brief Representates a Host * @brief Representates a Host
*
* Host object, identifies a host and defines some useful functions on it.
*/ */
struct host_t { struct host_t {
/** /**
* @brief Build a clone of this host object. * @brief Build a clone of this host object.
* *
* @param this object to clone * @param this object to clone
* @param [out]other address where to allocate the clone * @return cloned host
* @return
* - SUCCESS, or
* - OUT_OF_RES
*/ */
host_t *(*clone) (host_t *this);
status_t (*clone) (host_t *this, host_t **other);
/** /**
* @brief Get a pointer to the internal sockaddr struct. * @brief Get a pointer to the internal sockaddr struct.
* *
@@ -100,7 +99,7 @@ struct host_t {
* @param this calling * @param this calling
* @return SUCCESS in any case * @return SUCCESS in any case
*/ */
status_t (*destroy) (host_t *this); void (*destroy) (host_t *this);
}; };
/** /**
@@ -112,10 +111,9 @@ struct host_t {
* @param address string of an address, such as "152.96.193.130" * @param address string of an address, such as "152.96.193.130"
* @param port port number * @param port port number
* @return the host_t object or NULL, when * @return the host_t object or NULL, when
* not enough ressources, or
* family not supported. * family not supported.
*/ */
host_t *host_create(int family, char *address, u_int16_t port); host_t *host_create(int family, char *address, u_int16_t port);
#endif /*HOST_H_*/ #endif /*HOST_H_*/
+2 -2
View File
@@ -76,7 +76,7 @@ static status_t clone (private_packet_t *this, packet_t **clone)
if (this->public.destination != NULL) if (this->public.destination != NULL)
{ {
this->public.destination->clone(this->public.destination, &(other->destination)); other->destination = this->public.destination->clone(this->public.destination);
} }
else { else {
other->destination = NULL; other->destination = NULL;
@@ -84,7 +84,7 @@ static status_t clone (private_packet_t *this, packet_t **clone)
if (this->public.source != NULL) if (this->public.source != NULL)
{ {
this->public.source->clone(this->public.source, &(other->source)); other->source = this->public.source->clone(this->public.source);
} }
else { else {
other->source = NULL; other->source = NULL;
+27 -65
View File
@@ -1,7 +1,7 @@
/** /**
* @file event_queue.c * @file event_queue.c
* *
* @brief Event-Queue based on class linked_list_t * @brief Implementation of event_queue_t
* *
*/ */
@@ -54,19 +54,17 @@ struct event_t{
* @brief Destroys a event_t object. * @brief Destroys a event_t object.
* *
* @param event_t calling object * @param event_t calling object
* @returns always SUCCESS
*/ */
status_t (*destroy) (event_t *event); void (*destroy) (event_t *event);
}; };
/** /**
* @brief implements function destroy of event_t * implements event_t.destroy
*/ */
static status_t event_destroy(event_t *event) static void event_destroy(event_t *event)
{ {
allocator_free(event); allocator_free(event);
return SUCCESS;
} }
/** /**
@@ -75,17 +73,11 @@ static status_t event_destroy(event_t *event)
* @param time absolute time to fire the event * @param time absolute time to fire the event
* @param job job to add to job-queue at specific time * @param job job to add to job-queue at specific time
* *
* @returns * @returns created event_t object
* - created event_t object
* - NULL if memory allocation failed
*/ */
static event_t *event_create(timeval_t time, job_t *job) static event_t *event_create(timeval_t time, job_t *job)
{ {
event_t *this = allocator_alloc_thing(event_t); event_t *this = allocator_alloc_thing(event_t);
if (this == NULL)
{
return this;
}
this->destroy = event_destroy; this->destroy = event_destroy;
this->time = time; this->time = time;
@@ -149,8 +141,7 @@ static long time_difference(struct timeval *end_time, struct timeval *start_time
/** /**
* Implements function get_count of event_queue_t. * Implements event_queue_t.get_count
* See #event_queue_s.get_count for description.
*/ */
static int get_count (private_event_queue_t *this) static int get_count (private_event_queue_t *this)
{ {
@@ -162,14 +153,14 @@ static int get_count (private_event_queue_t *this)
} }
/** /**
* Implements function get of event_queue_t. * Implements event_queue_t.get
* See #event_queue_s.get for description.
*/ */
static status_t get(private_event_queue_t *this, job_t **job) static job_t *get(private_event_queue_t *this)
{ {
timespec_t timeout; timespec_t timeout;
timeval_t current_time; timeval_t current_time;
event_t * next_event; event_t * next_event;
job_t *job;
int oldstate; int oldstate;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
@@ -205,7 +196,7 @@ static status_t get(private_event_queue_t *this, job_t **job)
/* event available */ /* event available */
this->list->remove_first(this->list,(void **) &next_event); this->list->remove_first(this->list,(void **) &next_event);
*job = next_event->job; job = next_event->job;
next_event->destroy(next_event); next_event->destroy(next_event);
break; break;
@@ -216,23 +207,19 @@ static status_t get(private_event_queue_t *this, job_t **job)
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS; return job;
} }
/** /**
* Implements function add_absolute of event_queue_t. * Implements function add_absolute of event_queue_t.
* See #event_queue_s.add_absolute for description. * See #event_queue_s.add_absolute for description.
*/ */
static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t time) static void add_absolute(private_event_queue_t *this, job_t *job, timeval_t time)
{ {
event_t *event = event_create(time,job); event_t *event = event_create(time,job);
event_t *current_event; event_t *current_event;
status_t status; status_t status;
if (event == NULL)
{
return FAILED;
}
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
/* while just used to break out */ /* while just used to break out */
@@ -240,7 +227,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
{ {
if (this->list->get_count(this->list) == 0) if (this->list->get_count(this->list) == 0)
{ {
status = this->list->insert_first(this->list,event); this->list->insert_first(this->list,event);
break; break;
} }
@@ -250,7 +237,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
if (time_difference(&(event->time), &(current_event->time)) >= 0) if (time_difference(&(event->time), &(current_event->time)) >= 0)
{ {
/* my event has to be fired after the last event in list */ /* my event has to be fired after the last event in list */
status = this->list->insert_last(this->list,event); this->list->insert_last(this->list,event);
break; break;
} }
@@ -260,18 +247,13 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
if (time_difference(&(event->time), &(current_event->time)) < 0) if (time_difference(&(event->time), &(current_event->time)) < 0)
{ {
/* my event has to be fired before the first event in list */ /* my event has to be fired before the first event in list */
status = this->list->insert_first(this->list,event); this->list->insert_first(this->list,event);
break; break;
} }
iterator_t * iterator; iterator_t * iterator;
status = this->list->create_iterator(this->list,&iterator,TRUE); this->list->create_iterator(this->list,&iterator,TRUE);
if (status != SUCCESS)
{
break;
}
iterator->has_next(iterator); iterator->has_next(iterator);
/* first element has not to be checked (already done) */ /* first element has not to be checked (already done) */
@@ -283,7 +265,7 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
if (time_difference(&(event->time), &(current_event->time)) <= 0) if (time_difference(&(event->time), &(current_event->time)) <= 0)
{ {
/* my event has to be fired before the current event in list */ /* my event has to be fired before the current event in list */
status = iterator->insert_before(iterator,event); iterator->insert_before(iterator,event);
break; break;
} }
} }
@@ -293,19 +275,12 @@ static status_t add_absolute(private_event_queue_t *this, job_t *job, timeval_t
pthread_cond_signal( &(this->condvar)); pthread_cond_signal( &(this->condvar));
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
if (status != SUCCESS)
{
event->destroy(event);
}
return status;
} }
/** /**
* Implements function add_relative of event_queue_t. * Implements event_queue_t.add_relative.
* See #event_queue_s.add_relative for description.
*/ */
static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms) static void add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
{ {
timeval_t current_time; timeval_t current_time;
timeval_t time; timeval_t time;
@@ -316,15 +291,14 @@ static status_t add_relative(event_queue_t *this, job_t *job, u_int32_t ms)
time.tv_usec = ((current_time.tv_usec + micros) % 1000000); time.tv_usec = ((current_time.tv_usec + micros) % 1000000);
time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000); time.tv_sec = current_time.tv_sec + ((current_time.tv_usec + micros)/ 1000000);
return this->add_absolute(this, job, time); this->add_absolute(this, job, time);
} }
/** /**
* Implements function destroy of event_queue_t. * Implements event_queue_t.destroy.
* See #event_queue_s.destroy for description.
*/ */
static status_t event_queue_destroy(private_event_queue_t *this) static void event_queue_destroy(private_event_queue_t *this)
{ {
while (this->list->get_count(this->list) > 0) while (this->list->get_count(this->list) > 0)
{ {
@@ -345,7 +319,6 @@ static status_t event_queue_destroy(private_event_queue_t *this)
pthread_cond_destroy(&(this->condvar)); pthread_cond_destroy(&(this->condvar));
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -353,26 +326,15 @@ static status_t event_queue_destroy(private_event_queue_t *this)
*/ */
event_queue_t *event_queue_create() event_queue_t *event_queue_create()
{ {
linked_list_t *linked_list = linked_list_create();
if (linked_list == NULL)
{
return NULL;
}
private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t); private_event_queue_t *this = allocator_alloc_thing(private_event_queue_t);
if (this == NULL)
{
linked_list->destroy(linked_list);
return NULL;
}
this->public.get_count = (int (*) (event_queue_t *event_queue)) get_count; this->public.get_count = (int (*) (event_queue_t *event_queue)) get_count;
this->public.get = (status_t (*) (event_queue_t *event_queue, job_t **job)) get; this->public.get = (job_t *(*) (event_queue_t *event_queue)) get;
this->public.add_absolute = (status_t (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add_absolute; this->public.add_absolute = (void (*) (event_queue_t *event_queue, job_t *job, timeval_t time)) add_absolute;
this->public.add_relative = (status_t (*) (event_queue_t *event_queue, job_t *job, u_int32_t ms)) add_relative; this->public.add_relative = (void (*) (event_queue_t *event_queue, job_t *job, u_int32_t ms)) add_relative;
this->public.destroy = (status_t (*) (event_queue_t *event_queue)) event_queue_destroy; this->public.destroy = (void (*) (event_queue_t *event_queue)) event_queue_destroy;
this->list = linked_list; this->list = linked_list_create();;
pthread_mutex_init(&(this->mutex), NULL); pthread_mutex_init(&(this->mutex), NULL);
pthread_cond_init(&(this->condvar), NULL); pthread_cond_init(&(this->condvar), NULL);
+7 -16
View File
@@ -1,7 +1,7 @@
/** /**
* @file event_queue.h * @file event_queue.h
* *
* @brief Event-Queue based on class linked_list_t * @brief Interface of job_queue_t.
* *
*/ */
@@ -53,10 +53,9 @@ struct event_queue_t {
* *
* @param event_queue calling object * @param event_queue calling object
* @param[out] job pointer to a job pointer where to job is returned to * @param[out] job pointer to a job pointer where to job is returned to
* @return - SUCCESS if succeeded * @return next job
* - FAILED otherwisesa
*/ */
status_t (*get) (event_queue_t *event_queue, job_t **job); job_t *(*get) (event_queue_t *event_queue);
/** /**
* @brief Adds a event to the queue, using a relative time. * @brief Adds a event to the queue, using a relative time.
@@ -68,11 +67,8 @@ struct event_queue_t {
* @param event_queue calling object * @param event_queue calling object
* @param[in] job job to add to the queue (job is not copied) * @param[in] job job to add to the queue (job is not copied)
* @param[in] time relative time, when the event has to get fired * @param[in] time relative time, when the event has to get fired
* @returns
* - SUCCESS if succeeded
* - FAILED otherwise
*/ */
status_t (*add_relative) (event_queue_t *event_queue, job_t *job, u_int32_t ms); void (*add_relative) (event_queue_t *event_queue, job_t *job, u_int32_t ms);
/** /**
* @brief Adds a event to the queue, using an absolute time. * @brief Adds a event to the queue, using an absolute time.
@@ -84,11 +80,8 @@ struct event_queue_t {
* @param event_queue calling object * @param event_queue calling object
* @param[in] job job to add to the queue (job is not copied) * @param[in] job job to add to the queue (job is not copied)
* @param[in] absolute time time, when the event has to get fired * @param[in] absolute time time, when the event has to get fired
* @returns
* - SUCCESS if succeeded
* - FAILED otherwise
*/ */
status_t (*add_absolute) (event_queue_t *event_queue, job_t *job, timeval_t time); void (*add_absolute) (event_queue_t *event_queue, job_t *job, timeval_t time);
/** /**
* @brief Destroys a event_queue object. * @brief Destroys a event_queue object.
@@ -100,15 +93,13 @@ struct event_queue_t {
* @param event_queue calling object * @param event_queue calling object
* @returns always SUCCESS * @returns always SUCCESS
*/ */
status_t (*destroy) (event_queue_t *event_queue); void (*destroy) (event_queue_t *event_queue);
}; };
/** /**
* @brief Creates an empty event_queue * @brief Creates an empty event_queue
* *
* @returns * @returns event_queue
* - Empty event_queue_t object
* - NULL if memory allocation failed
*/ */
event_queue_t *event_queue_create(); event_queue_t *event_queue_create();
#endif /*EVENT_QUEUE_H_*/ #endif /*EVENT_QUEUE_H_*/
+15 -28
View File
@@ -1,7 +1,7 @@
/** /**
* @file job_queue.c * @file job_queue.c
* *
* @brief Job-Queue based on linked_list_t * @brief Implementation of job_queue_t
* *
*/ */
@@ -56,7 +56,7 @@ struct private_job_queue_t {
/** /**
* @brief implements function get_count of job_queue_t * implements job_queue_t.get_count
*/ */
static int get_count(private_job_queue_t *this) static int get_count(private_job_queue_t *this)
{ {
@@ -68,11 +68,12 @@ static int get_count(private_job_queue_t *this)
} }
/** /**
* @brief implements function get of job_queue_t * implements job_queue_t.get
*/ */
static status_t get(private_job_queue_t *this, job_t **job) static job_t *get(private_job_queue_t *this)
{ {
int oldstate; int oldstate;
job_t *job;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
/* go to wait while no jobs available */ /* go to wait while no jobs available */
while(this->list->get_count(this->list) == 0) while(this->list->get_count(this->list) == 0)
@@ -87,28 +88,26 @@ static status_t get(private_job_queue_t *this, job_t **job)
pthread_setcancelstate(oldstate, NULL); pthread_setcancelstate(oldstate, NULL);
pthread_cleanup_pop(0); pthread_cleanup_pop(0);
} }
this->list->remove_first(this->list,(void **) job); this->list->remove_first(this->list,(void **) &job);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS; return job;
} }
/** /**
* @brief implements function add of job_queue_t * implements function job_queue_t.add
*/ */
static status_t add(private_job_queue_t *this, job_t *job) static void add(private_job_queue_t *this, job_t *job)
{ {
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
this->list->insert_last(this->list,job); this->list->insert_last(this->list,job);
pthread_cond_signal( &(this->condvar)); pthread_cond_signal( &(this->condvar));
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS;
} }
/** /**
* @brief implements function destroy of job_queue_t * implements job_queue_t.destroy
*
*/ */
static status_t job_queue_destroy (private_job_queue_t *this) static void job_queue_destroy (private_job_queue_t *this)
{ {
while (this->list->get_count(this->list) > 0) while (this->list->get_count(this->list) > 0)
{ {
@@ -127,7 +126,6 @@ static status_t job_queue_destroy (private_job_queue_t *this)
pthread_cond_destroy(&(this->condvar)); pthread_cond_destroy(&(this->condvar));
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -136,25 +134,14 @@ static status_t job_queue_destroy (private_job_queue_t *this)
*/ */
job_queue_t *job_queue_create() job_queue_t *job_queue_create()
{ {
linked_list_t *linked_list = linked_list_create();
if (linked_list == NULL)
{
return NULL;
}
private_job_queue_t *this = allocator_alloc_thing(private_job_queue_t); private_job_queue_t *this = allocator_alloc_thing(private_job_queue_t);
if (this == NULL)
{
linked_list->destroy(linked_list);
return NULL;
}
this->public.get_count = (int(*)(job_queue_t*))get_count; this->public.get_count = (int(*)(job_queue_t*))get_count;
this->public.get = (status_t(*)(job_queue_t*, job_t**))get; this->public.get = (job_t*(*)(job_queue_t*))get;
this->public.add = (status_t(*)(job_queue_t*, job_t*))add; this->public.add = (void(*)(job_queue_t*, job_t*))add;
this->public.destroy = (status_t(*)(job_queue_t*))job_queue_destroy; this->public.destroy = (void(*)(job_queue_t*))job_queue_destroy;
this->list = linked_list; this->list = linked_list_create();
pthread_mutex_init(&(this->mutex), NULL); pthread_mutex_init(&(this->mutex), NULL);
pthread_cond_init(&(this->condvar), NULL); pthread_cond_init(&(this->condvar), NULL);
+10 -12
View File
@@ -1,7 +1,7 @@
/** /**
* @file job_queue.h * @file job_queue.h
* *
* @brief Job-Queue based on linked_list_t * @brief Interface of job_queue_t-
* *
*/ */
@@ -39,8 +39,8 @@ struct job_queue_t {
/** /**
* @brief returns number of jobs in queue * @brief returns number of jobs in queue
* *
* @param job_queue_t calling object * @param job_queue_t calling object
* @returns number of items in queue * @returns number of items in queue
*/ */
int (*get_count) (job_queue_t *job_queue); int (*get_count) (job_queue_t *job_queue);
@@ -48,14 +48,13 @@ struct job_queue_t {
* @brief get the next job from the queue * @brief get the next job from the queue
* *
* If the queue is empty, this function blocks until a job can be returned. * If the queue is empty, this function blocks until a job can be returned.
*
* After using, the returned job has to get destroyed by the caller. * After using, the returned job has to get destroyed by the caller.
* *
* @param job_queue_t calling object * @param job_queue_t calling object
* @param[out] job pointer to a job pointer where to job is returned to * @param[out] job pointer to a job pointer where to job is returned to
* @returns SUCCESS if succeeded, FAILED otherwise * @return job
*/ */
status_t (*get) (job_queue_t *job_queue, job_t **job); job_t *(*get) (job_queue_t *job_queue);
/** /**
* @brief adds a job to the queue * @brief adds a job to the queue
@@ -66,9 +65,8 @@ struct job_queue_t {
* *
* @param job_queue_t calling object * @param job_queue_t calling object
* @param[in] job job to add to the queue (job is not copied) * @param[in] job job to add to the queue (job is not copied)
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*add) (job_queue_t *job_queue, job_t *job); void (*add) (job_queue_t *job_queue, job_t *job);
/** /**
* @brief destroys a job_queue object * @brief destroys a job_queue object
@@ -78,9 +76,8 @@ struct job_queue_t {
* after calling this function. * after calling this function.
* *
* @param job_queue_t calling object * @param job_queue_t calling object
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (job_queue_t *job_queue); void (*destroy) (job_queue_t *job_queue);
}; };
/** /**
@@ -89,4 +86,5 @@ struct job_queue_t {
* @return job_queue_t empty job_queue * @return job_queue_t empty job_queue
*/ */
job_queue_t *job_queue_create(); job_queue_t *job_queue_create();
#endif /*JOB_QUEUE_H_*/ #endif /*JOB_QUEUE_H_*/
@@ -96,11 +96,7 @@ delete_ike_sa_job_t *delete_ike_sa_job_create(ike_sa_id_t *ike_sa_id)
this->public.destroy = (status_t (*)(delete_ike_sa_job_t *)) destroy; this->public.destroy = (status_t (*)(delete_ike_sa_job_t *)) destroy;
/* private variables */ /* private variables */
if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS) this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
{
allocator_free(this);
return NULL;
}
return &(this->public); return &(this->public);
} }
+16 -29
View File
@@ -1,7 +1,7 @@
/** /**
* @file send_queue.c * @file send_queue.c
* *
* @brief Send-Queue based on linked_list_t * @brief Implementation of send_queue_t.
* *
*/ */
@@ -59,7 +59,7 @@ struct private_send_queue_t {
/** /**
* @brief implements function get_count of send_queue_t * implements send_queue_t.get_count
*/ */
static int get_count(private_send_queue_t *this) static int get_count(private_send_queue_t *this)
{ {
@@ -71,11 +71,12 @@ static int get_count(private_send_queue_t *this)
} }
/** /**
* @brief implements function get of send_queue_t * implements send_queue_t.get
*/ */
static status_t get(private_send_queue_t *this, packet_t **packet) static packet_t *get(private_send_queue_t *this)
{ {
int oldstate; int oldstate;
packet_t *packet;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
/* go to wait while no packets available */ /* go to wait while no packets available */
@@ -90,28 +91,26 @@ static status_t get(private_send_queue_t *this, packet_t **packet)
pthread_setcancelstate(oldstate, NULL); pthread_setcancelstate(oldstate, NULL);
pthread_cleanup_pop(0); pthread_cleanup_pop(0);
} }
this->list->remove_first(this->list,(void **) packet); this->list->remove_first(this->list,(void **)&packet);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS; return packet;
} }
/** /**
* @brief implements function add of send_queue_t * implements send_queue_t.add
*/ */
static status_t add(private_send_queue_t *this, packet_t *packet) static void add(private_send_queue_t *this, packet_t *packet)
{ {
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
this->list->insert_last(this->list,packet); this->list->insert_last(this->list,packet);
pthread_cond_signal( &(this->condvar)); pthread_cond_signal( &(this->condvar));
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS;
} }
/** /**
* @brief implements function destroy of send_queue_t * implements send_queue_t.destroy
*
*/ */
static status_t destroy (private_send_queue_t *this) static void destroy (private_send_queue_t *this)
{ {
/* destroy all packets in list before destroying list */ /* destroy all packets in list before destroying list */
@@ -132,7 +131,6 @@ static status_t destroy (private_send_queue_t *this)
pthread_cond_destroy(&(this->condvar)); pthread_cond_destroy(&(this->condvar));
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -141,25 +139,14 @@ static status_t destroy (private_send_queue_t *this)
*/ */
send_queue_t *send_queue_create() send_queue_t *send_queue_create()
{ {
linked_list_t *linked_list = linked_list_create();
if (linked_list == NULL)
{
return NULL;
}
private_send_queue_t *this = allocator_alloc_thing(private_send_queue_t); private_send_queue_t *this = allocator_alloc_thing(private_send_queue_t);
if (this == NULL)
{
linked_list->destroy(linked_list);
return NULL;
}
this->public.get_count = (int(*)(send_queue_t*)) get_count; this->public.get_count = (int(*)(send_queue_t*)) get_count;
this->public.get = (status_t(*)(send_queue_t*, packet_t**)) get; this->public.get = (packet_t*(*)(send_queue_t*)) get;
this->public.add = (status_t(*)(send_queue_t*, packet_t*)) add; this->public.add = (void(*)(send_queue_t*, packet_t*)) add;
this->public.destroy = (status_t(*)(send_queue_t*)) destroy; this->public.destroy = (void(*)(send_queue_t*)) destroy;
this->list = linked_list; this->list = linked_list_create();
pthread_mutex_init(&(this->mutex), NULL); pthread_mutex_init(&(this->mutex), NULL);
pthread_cond_init(&(this->condvar), NULL); pthread_cond_init(&(this->condvar), NULL);
+15 -17
View File
@@ -1,7 +1,7 @@
/** /**
* @file send_queue.h * @file send_queue.h
* *
* @brief Send-Queue based on linked_list_t * @brief Interface of send_queue_t.
* *
*/ */
@@ -40,40 +40,38 @@ struct send_queue_t {
/** /**
* @brief returns number of packets in queue * @brief returns number of packets in queue
* *
* @param send_queue_t calling object * @param send_queue_t calling object
* @param[out] count integer pointer to store the count in * @param[out] count integer pointer to store the count in
* @returns number of items in queue * @returns number of items in queue
*/ */
int (*get_count) (send_queue_t *send_queue); int (*get_count) (send_queue_t *send_queue);
/** /**
* @brief get the next packet from the queue * @brief get the next packet from the queue.
* *
* If the queue is empty, this function blocks until a packet can be returned. * If the queue is empty, this function blocks until a packet can be returned.
* *
* After using, the returned packet has to get destroyed by the caller. * After using, the returned packet has to get destroyed by the caller.
* *
* @param send_queue_t calling object * @param send_queue_t calling object
* @param[out] packet pointer to a packet_t pointer where to packet is returned to * @param[out] packet pointer to a packet_t pointer where to packet is returned to
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*get) (send_queue_t *send_queue, packet_t **packet); packet_t *(*get) (send_queue_t *send_queue);
/** /**
* @brief adds a packet to the queue * @brief adds a packet to the queue.
* *
* This function is non blocking and adds a packet_t to the list. * This function is non blocking and adds a packet_t to the list.
* The specific packet object has to get destroyed by the thread which * The specific packet object has to get destroyed by the thread which
* removes the packet. * removes the packet.
* *
* @param send_queue_t calling object * @param send_queue_t calling object
* @param[in] packet packet_t to add to the queue (packet is not copied) * @param packet packet_t to add to the queue (packet is not copied)
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*add) (send_queue_t *send_queue, packet_t *packet); void (*add) (send_queue_t *send_queue, packet_t *packet);
/** /**
* @brief destroys a send_queue object * @brief destroys a send_queue object.
* *
* @warning The caller of this function has to make sure * @warning The caller of this function has to make sure
* that no thread is going to add or get a packet from the send_queue * that no thread is going to add or get a packet from the send_queue
@@ -82,11 +80,11 @@ struct send_queue_t {
* @param send_queue_t calling object * @param send_queue_t calling object
* @returns SUCCESS if succeeded, FAILED otherwise * @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (send_queue_t *send_queue); void (*destroy) (send_queue_t *send_queue);
}; };
/** /**
* @brief Creates an empty send_queue_t * @brief Creates an empty send_queue_t.
* *
* @return send_queue_t empty send_queue_t * @return send_queue_t empty send_queue_t
*/ */
+89 -316
View File
@@ -206,14 +206,13 @@ struct private_ike_sa_t {
/** /**
* @brief implements function process_message of protected_ike_sa_t * Implements protected_ike_sa_t.process_message.
*/ */
static status_t process_message (private_ike_sa_t *this, message_t *message) static status_t process_message (private_ike_sa_t *this, message_t *message)
{ {
u_int32_t message_id; u_int32_t message_id;
exchange_type_t exchange_type; exchange_type_t exchange_type;
bool is_request; bool is_request;
status_t status;
/* we must process each request or response from remote host */ /* we must process each request or response from remote host */
@@ -228,7 +227,7 @@ static status_t process_message (private_ike_sa_t *this, message_t *message)
/* /*
* It has to be checked, if the message has to be resent cause of lost packets! * It has to be checked, if the message has to be resent cause of lost packets!
*/ */
if (is_request && ( message_id == (this->message_id_in - 1))) if (is_request && (message_id == (this->message_id_in - 1)))
{ {
/* message can be resent ! */ /* message can be resent ! */
this->logger->log(this->logger, CONTROL|MORE, "Resent message detected. Send stored reply"); this->logger->log(this->logger, CONTROL|MORE, "Resent message detected. Send stored reply");
@@ -257,61 +256,34 @@ static status_t process_message (private_ike_sa_t *this, message_t *message)
/* now the message is processed by the current state object */ /* now the message is processed by the current state object */
/* the current state does change the current change to the next one*/ /* the current state does change the current change to the next one*/
status = this->current_state->process_message(this->current_state,message); return this->current_state->process_message(this->current_state,message);
return status;
} }
/** /**
* @brief Implements function build_message of protected_ike_sa_t. * Implements protected_ike_sa_t.build_message.
*/ */
static status_t build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message) static void build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message)
{ {
status_t status;
message_t *new_message; message_t *new_message;
host_t *source, *destination; host_t *source, *destination;
this->logger->log(this->logger, CONTROL|MORE, "build empty message"); this->logger->log(this->logger, CONTROL|MORE, "build empty message");
new_message = message_create(); new_message = message_create();
if (new_message == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not create empty message object");
return OUT_OF_RES;
}
status = this->me.host->clone(this->me.host, &source);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not clone my host information");
new_message->destroy(new_message);
return status;
}
status = this->other.host->clone(this->other.host, &destination);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not clone other host information");
source->destroy(source);
new_message->destroy(new_message);
return status;
}
source = this->me.host->clone(this->me.host);
destination = this->other.host->clone(this->other.host);
new_message->set_source(new_message, source); new_message->set_source(new_message, source);
new_message->set_destination(new_message, destination); new_message->set_destination(new_message, destination);
new_message->set_exchange_type(new_message, type); new_message->set_exchange_type(new_message, type);
new_message->set_request(new_message, request); new_message->set_request(new_message, request);
new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in); new_message->set_message_id(new_message, (request) ? this->message_id_out : this->message_id_in);
new_message->set_ike_sa_id(new_message, this->ike_sa_id); new_message->set_ike_sa_id(new_message, this->ike_sa_id);
*message = new_message; *message = new_message;
return SUCCESS;
} }
/** /**
* @brief implements function process_configuration of protected_ike_sa_t * Implements protected_ike_sa_t.process_configuration.
*/ */
static status_t initialize_connection(private_ike_sa_t *this, char *name) static status_t initialize_connection(private_ike_sa_t *this, char *name)
{ {
@@ -336,7 +308,7 @@ static status_t initialize_connection(private_ike_sa_t *this, char *name)
} }
/** /**
* @brief implements function protected_ike_sa_t.get_id * Implements protected_ike_sa_t.get_id.
*/ */
static ike_sa_id_t* get_id(private_ike_sa_t *this) static ike_sa_id_t* get_id(private_ike_sa_t *this)
{ {
@@ -344,14 +316,13 @@ static ike_sa_id_t* get_id(private_ike_sa_t *this)
} }
/** /**
* @brief implements function protected_ike_sa_t.compute_secrets * Implements protected_ike_sa_t.compute_secrets.
*/ */
static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce) static void compute_secrets(private_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce)
{ {
chunk_t concatenated_nonces; chunk_t concatenated_nonces;
chunk_t skeyseed; chunk_t skeyseed;
chunk_t prf_plus_seed; chunk_t prf_plus_seed;
status_t status;
u_int64_t initiator_spi; u_int64_t initiator_spi;
u_int64_t responder_spi; u_int64_t responder_spi;
prf_plus_t *prf_plus; prf_plus_t *prf_plus;
@@ -362,11 +333,7 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret
*/ */
concatenated_nonces.len = (initiator_nonce.len + responder_nonce.len); concatenated_nonces.len = (initiator_nonce.len + responder_nonce.len);
concatenated_nonces.ptr = allocator_alloc(concatenated_nonces.len); concatenated_nonces.ptr = allocator_alloc(concatenated_nonces.len);
if (concatenated_nonces.ptr == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for concatenated nonces");
return FAILED;
}
/* first is initiator */ /* first is initiator */
memcpy(concatenated_nonces.ptr,initiator_nonce.ptr,initiator_nonce.len); memcpy(concatenated_nonces.ptr,initiator_nonce.ptr,initiator_nonce.len);
/* second is responder */ /* second is responder */
@@ -374,28 +341,15 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret
this->logger->log_chunk(this->logger, RAW, "Nonce data", &concatenated_nonces); this->logger->log_chunk(this->logger, RAW, "Nonce data", &concatenated_nonces);
/* status of set_key is not checked */ /* status of set_key is not checked */
status = this->prf->set_key(this->prf,concatenated_nonces); this->prf->set_key(this->prf,concatenated_nonces);
this->prf->allocate_bytes(this->prf,dh_shared_secret,&skeyseed);
status = this->prf->allocate_bytes(this->prf,dh_shared_secret,&skeyseed);
if (status != SUCCESS)
{
allocator_free_chunk(&concatenated_nonces);
this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate bytes for skeyseed");
return status;
}
allocator_free_chunk(&concatenated_nonces); allocator_free_chunk(&concatenated_nonces);
prf_plus_seed.len = (initiator_nonce.len + responder_nonce.len + 16); prf_plus_seed.len = (initiator_nonce.len + responder_nonce.len + 16);
prf_plus_seed.ptr = allocator_alloc(prf_plus_seed.len); prf_plus_seed.ptr = allocator_alloc(prf_plus_seed.len);
if (prf_plus_seed.ptr == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for prf+ seed");
allocator_free_chunk(&skeyseed);
return FAILED;
}
/* first is initiator */ /* first is initiator */
memcpy(prf_plus_seed.ptr,initiator_nonce.ptr,initiator_nonce.len); memcpy(prf_plus_seed.ptr,initiator_nonce.ptr,initiator_nonce.len);
@@ -412,113 +366,45 @@ static status_t compute_secrets (private_ike_sa_t *this,chunk_t dh_shared_secret
this->logger->log_chunk(this->logger, PRIVATE | MORE, "PRF+ Seed", &prf_plus_seed); this->logger->log_chunk(this->logger, PRIVATE | MORE, "PRF+ Seed", &prf_plus_seed);
this->logger->log(this->logger, CONTROL | MOST, "Set new key of prf object"); this->logger->log(this->logger, CONTROL | MOST, "Set new key of prf object");
status = this->prf->set_key(this->prf,skeyseed); this->prf->set_key(this->prf,skeyseed);
allocator_free_chunk(&skeyseed); allocator_free_chunk(&skeyseed);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal errror: Could not allocate memory for prf+ seed");
allocator_free_chunk(&prf_plus_seed);
return FAILED;
}
this->logger->log(this->logger, CONTROL | MOST, "Create new prf+ object"); this->logger->log(this->logger, CONTROL | MOST, "Create new prf+ object");
prf_plus = prf_plus_create(this->prf, prf_plus_seed); prf_plus = prf_plus_create(this->prf, prf_plus_seed);
allocator_free_chunk(&prf_plus_seed); allocator_free_chunk(&prf_plus_seed);
if (prf_plus == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal errror: prf+ object could not be created");
return FAILED;
}
status = prf_plus->allocate_bytes(prf_plus,this->prf->get_block_size(this->prf),&(this->secrets.d_key)); prf_plus->allocate_bytes(prf_plus,this->prf->get_block_size(this->prf),&(this->secrets.d_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_d");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", &(this->secrets.d_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_d secret", &(this->secrets.d_key));
status = prf_plus->allocate_bytes(prf_plus,this->crypter_initiator->get_block_size(this->crypter_initiator),&(this->secrets.ei_key)); prf_plus->allocate_bytes(prf_plus,this->crypter_initiator->get_block_size(this->crypter_initiator),&(this->secrets.ei_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ei");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", &(this->secrets.ei_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ei secret", &(this->secrets.ei_key));
status = this->crypter_initiator->set_key(this->crypter_initiator,this->secrets.ei_key); this->crypter_initiator->set_key(this->crypter_initiator,this->secrets.ei_key);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not set encryption key initiator crypter");
return status;
}
status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.er_key)); prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.er_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_er");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", &(this->secrets.er_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_er secret", &(this->secrets.er_key));
status = this->crypter_responder->set_key(this->crypter_responder,this->secrets.er_key); this->crypter_responder->set_key(this->crypter_responder,this->secrets.er_key);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not set encryption key responder crypter");
return status;
}
status = prf_plus->allocate_bytes(prf_plus,this->signer_initiator->get_block_size(this->signer_initiator),&(this->secrets.ai_key)); prf_plus->allocate_bytes(prf_plus,this->signer_initiator->get_block_size(this->signer_initiator),&(this->secrets.ai_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ai");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", &(this->secrets.ai_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ai secret", &(this->secrets.ai_key));
status = this->signer_initiator->set_key(this->signer_initiator,this->secrets.ai_key); this->signer_initiator->set_key(this->signer_initiator,this->secrets.ai_key);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not set key for initiator signer");
return status;
}
status = prf_plus->allocate_bytes(prf_plus,this->signer_responder->get_block_size(this->signer_responder),&(this->secrets.ar_key)); prf_plus->allocate_bytes(prf_plus,this->signer_responder->get_block_size(this->signer_responder),&(this->secrets.ar_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_ar");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", &(this->secrets.ar_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_ar secret", &(this->secrets.ar_key));
status = this->signer_responder->set_key(this->signer_responder,this->secrets.ar_key); this->signer_responder->set_key(this->signer_responder,this->secrets.ar_key);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not set key for responder signer");
return status;
}
status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pi_key)); prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pi_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_pi");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", &(this->secrets.pi_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_pi secret", &(this->secrets.pi_key));
status = prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pr_key)); prf_plus->allocate_bytes(prf_plus,this->crypter_responder->get_block_size(this->crypter_responder),&(this->secrets.pr_key));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not allocate bytes from prf+ for Sk_pr");
return status;
}
this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", &(this->secrets.pr_key)); this->logger->log_chunk(this->logger, PRIVATE, "Sk_pr secret", &(this->secrets.pr_key));
prf_plus->destroy(prf_plus); prf_plus->destroy(prf_plus);
return SUCCESS;
} }
/** /**
* @brief implements function resend_last_reply of protected_ike_sa_t * Implements protected_ike_sa_t.resend_last_reply.
*/ */
status_t resend_last_reply (private_ike_sa_t *this) static status_t resend_last_reply(private_ike_sa_t *this)
{ {
packet_t *packet; packet_t *packet;
status_t status; status_t status;
@@ -530,37 +416,22 @@ status_t resend_last_reply (private_ike_sa_t *this)
return status; return status;
} }
status = global_send_queue->add(global_send_queue, packet); global_send_queue->add(global_send_queue, packet);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
packet->destroy(packet);
return status;
}
return SUCCESS; return SUCCESS;
} }
status_t create_delete_job (private_ike_sa_t *this) /**
* Implements protected_ike_sa_t.resend_last_reply.
*/
static status_t create_delete_job(private_ike_sa_t *this)
{ {
job_t *delete_job; job_t *delete_job;
status_t status;
this->logger->log(this->logger, CONTROL | MORE, "Going to create job to delete this IKE_SA"); this->logger->log(this->logger, CONTROL | MORE, "Going to create job to delete this IKE_SA");
delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id); delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
if (delete_job == NULL) global_job_queue->add(global_job_queue,delete_job);
{
this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created");
return FAILED;
}
status = global_job_queue->add(global_job_queue,delete_job);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status));
delete_job->destroy_all(delete_job);
return status;
}
return SUCCESS; return SUCCESS;
} }
@@ -636,96 +507,87 @@ static status_t create_transforms_from_proposal (private_ike_sa_t *this,proposal
u_int16_t pseudo_random_function; u_int16_t pseudo_random_function;
u_int16_t pseudo_random_function_key_length; u_int16_t pseudo_random_function_key_length;
this ->logger->log(this->logger, CONTROL|MORE, "Going to create transform objects for proposal"); this->logger->log(this->logger, CONTROL|MORE, "Going to create transform objects for proposal");
this ->logger->log(this->logger, CONTROL|MOST, "Get encryption transform type"); this->logger->log(this->logger, CONTROL|MOST, "Get encryption transform type");
status = proposal->get_info_for_transform_type(proposal,ENCRYPTION_ALGORITHM,&(encryption_algorithm),&(encryption_algorithm_key_length)); status = proposal->get_info_for_transform_type(proposal,ENCRYPTION_ALGORITHM,&(encryption_algorithm),&(encryption_algorithm_key_length));
if (status != SUCCESS) if (status != SUCCESS)
{ {
this ->logger->log(this->logger, ERROR|MORE, "Could not get encryption transform type"); this->logger->log(this->logger, ERROR|MORE, "Could not get encryption transform type");
return status; return status;
} }
this ->logger->log(this->logger, CONTROL|MORE, "Encryption algorithm: %s with keylength %d",mapping_find(encryption_algorithm_m,encryption_algorithm),encryption_algorithm_key_length); this->logger->log(this->logger, CONTROL|MORE, "Encryption algorithm: %s with keylength %d",mapping_find(encryption_algorithm_m,encryption_algorithm),encryption_algorithm_key_length);
this ->logger->log(this->logger, CONTROL|MOST, "Get integrity transform type"); this->logger->log(this->logger, CONTROL|MOST, "Get integrity transform type");
status = proposal->get_info_for_transform_type(proposal,INTEGRITY_ALGORITHM,&(integrity_algorithm),&(integrity_algorithm_key_length)); status = proposal->get_info_for_transform_type(proposal,INTEGRITY_ALGORITHM,&(integrity_algorithm),&(integrity_algorithm_key_length));
if (status != SUCCESS) if (status != SUCCESS)
{ {
this ->logger->log(this->logger, ERROR|MORE, "Could not get integrity transform type"); this->logger->log(this->logger, ERROR|MORE, "Could not get integrity transform type");
return status; return status;
} }
this ->logger->log(this->logger, CONTROL|MORE, "integrity algorithm: %s with keylength %d",mapping_find(integrity_algorithm_m,integrity_algorithm),integrity_algorithm_key_length); this->logger->log(this->logger, CONTROL|MORE, "integrity algorithm: %s with keylength %d",mapping_find(integrity_algorithm_m,integrity_algorithm),integrity_algorithm_key_length);
this ->logger->log(this->logger, CONTROL|MOST, "Get prf transform type"); this->logger->log(this->logger, CONTROL|MOST, "Get prf transform type");
status = proposal->get_info_for_transform_type(proposal,PSEUDO_RANDOM_FUNCTION,&(pseudo_random_function),&(pseudo_random_function_key_length)); status = proposal->get_info_for_transform_type(proposal,PSEUDO_RANDOM_FUNCTION,&(pseudo_random_function),&(pseudo_random_function_key_length));
if (status != SUCCESS) if (status != SUCCESS)
{ {
this ->logger->log(this->logger, ERROR|MORE, "Could not prf transform type"); this->logger->log(this->logger, ERROR|MORE, "Could not prf transform type");
return status; return status;
} }
this ->logger->log(this->logger, CONTROL|MORE, "prf: %s with keylength %d",mapping_find(pseudo_random_function_m,pseudo_random_function),pseudo_random_function_key_length); this->logger->log(this->logger, CONTROL|MORE, "prf: %s with keylength %d",mapping_find(pseudo_random_function_m,pseudo_random_function),pseudo_random_function_key_length);
if (this->prf != NULL) if (this->prf != NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing prf_t object");
this->prf->destroy(this->prf); this->prf->destroy(this->prf);
} }
this->prf = prf_create(pseudo_random_function); this->prf = prf_create(pseudo_random_function);
if (this->prf == NULL) if (this->prf == NULL)
{ {
this ->logger->log(this->logger, ERROR|MORE, "prf does not seem to be supported!"); this->logger->log(this->logger, ERROR|MORE, "prf not supported!");
return FAILED; return FAILED;
} }
if (this->crypter_initiator != NULL) if (this->crypter_initiator != NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing initiator crypter_t object");
this->crypter_initiator->destroy(this->crypter_initiator); this->crypter_initiator->destroy(this->crypter_initiator);
} }
this->crypter_initiator = crypter_create(encryption_algorithm,encryption_algorithm_key_length); this->crypter_initiator = crypter_create(encryption_algorithm,encryption_algorithm_key_length);
if (this->crypter_initiator == NULL) if (this->crypter_initiator == NULL)
{ {
this ->logger->log(this->logger, ERROR|MORE, "encryption algorithm does not seem to be supported!"); this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!");
return FAILED; return FAILED;
} }
if (this->crypter_responder != NULL) if (this->crypter_responder != NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing responder crypter_t object");
this->crypter_responder->destroy(this->crypter_responder); this->crypter_responder->destroy(this->crypter_responder);
} }
this->crypter_responder = crypter_create(encryption_algorithm,encryption_algorithm_key_length); this->crypter_responder = crypter_create(encryption_algorithm,encryption_algorithm_key_length);
if (this->crypter_responder == NULL) if (this->crypter_responder == NULL)
{ {
this ->logger->log(this->logger, ERROR|MORE, "encryption algorithm does not seem to be supported!"); this->logger->log(this->logger, ERROR|MORE, "encryption algorithm not supported!");
return FAILED; return FAILED;
} }
if (this->signer_initiator != NULL) if (this->signer_initiator != NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing initiator signer_t object");
this->signer_initiator->destroy(this->signer_initiator); this->signer_initiator->destroy(this->signer_initiator);
} }
this->signer_initiator = signer_create(integrity_algorithm); this->signer_initiator = signer_create(integrity_algorithm);
if (this->signer_initiator == NULL) if (this->signer_initiator == NULL)
{ {
this ->logger->log(this->logger, ERROR|MORE, "integrity algorithm does not seem to be supported!"); this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!");
return FAILED; return FAILED;
} }
if (this->signer_responder != NULL) if (this->signer_responder != NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "Destroy existing responder signer_t object");
this->signer_responder->destroy(this->signer_responder); this->signer_responder->destroy(this->signer_responder);
} }
this->signer_responder = signer_create(integrity_algorithm); this->signer_responder = signer_create(integrity_algorithm);
if (this->signer_responder == NULL) if (this->signer_responder == NULL)
{ {
this ->logger->log(this->logger, ERROR|MORE, "integrity algorithm does not seem to be supported!"); this->logger->log(this->logger, ERROR|MORE, "integrity algorithm not supported!");
return FAILED; return FAILED;
} }
@@ -745,50 +607,47 @@ static randomizer_t *get_randomizer (private_ike_sa_t *this)
*/ */
static status_t set_last_requested_message (private_ike_sa_t *this,message_t * message) static status_t set_last_requested_message (private_ike_sa_t *this,message_t * message)
{ {
if ( this->last_requested_message != NULL) if (this->last_requested_message != NULL)
{ {
/* destroy message */ /* destroy message */
this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last requested message");
this->last_requested_message->destroy(this->last_requested_message); this->last_requested_message->destroy(this->last_requested_message);
} }
if (message->get_message_id(message) != this->message_id_out) if (message->get_message_id(message) != this->message_id_out)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "last requested message could not be set cause id was not as expected"); this->logger->log(this->logger, CONTROL|MOST, "last requested message could not be set cause id was not as expected");
return FAILED; return FAILED;
} }
this ->logger->log(this->logger, CONTROL|MOST, "replace last requested message with new one"); this->logger->log(this->logger, CONTROL|MOST, "replace last requested message with new one");
this->last_requested_message = message; this->last_requested_message = message;
/* message counter can now be increased */ /* message counter can now be increased */
this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages"); this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages");
this->message_id_out++; this->message_id_out++;
return SUCCESS; return SUCCESS;
} }
/** /**
* Implementation of protected_ike_sa_t.set_last_responded_message. * Implementation of protected_ike_sa_t.set_last_responded_message.
*/ */
static status_t set_last_responded_message (private_ike_sa_t *this,message_t * message) static status_t set_last_responded_message (private_ike_sa_t *this,message_t * message)
{ {
if ( this->last_responded_message != NULL) if (this->last_responded_message != NULL)
{ {
/* destroy message */ /* destroy message */
this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last responded message");
this->last_responded_message->destroy(this->last_responded_message); this->last_responded_message->destroy(this->last_responded_message);
} }
if (message->get_message_id(message) != this->message_id_in) if (message->get_message_id(message) != this->message_id_in)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "last responded message could not be set cause id was not as expected"); this->logger->log(this->logger, CONTROL|MOST, "last responded message could not be set cause id was not as expected");
return FAILED; return FAILED;
} }
this ->logger->log(this->logger, CONTROL|MOST, "replace last responded message with new one"); this->logger->log(this->logger, CONTROL|MOST, "replace last responded message with new one");
this->last_responded_message = message; this->last_responded_message = message;
/* message counter can now be increased */ /* message counter can now be increased */
this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages"); this->logger->log(this->logger, CONTROL|MOST, "Increate message counter for incoming messages");
this->message_id_in++; this->message_id_in++;
return SUCCESS; return SUCCESS;
@@ -796,11 +655,10 @@ static status_t set_last_responded_message (private_ike_sa_t *this,message_t * m
/** /**
* @brief implements function destroy of protected_ike_sa_t * Implements protected_ike_sa_t.destroy.
*/ */
static status_t destroy (private_ike_sa_t *this) static void destroy (private_ike_sa_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy IKE_SA"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy IKE_SA");
/* destroy child sa's */ /* destroy child sa's */
@@ -808,7 +666,7 @@ static status_t destroy (private_ike_sa_t *this)
while (this->child_sas->get_count(this->child_sas) > 0) while (this->child_sas->get_count(this->child_sas) > 0)
{ {
void *child_sa; void *child_sa;
if (this->child_sas->remove_first(this->child_sas,&child_sa) != SUCCESS) if (this->child_sas->remove_first(this->child_sas, &child_sa) != SUCCESS)
{ {
break; break;
} }
@@ -817,95 +675,64 @@ static status_t destroy (private_ike_sa_t *this)
this->child_sas->destroy(this->child_sas); this->child_sas->destroy(this->child_sas);
this->logger->log(this->logger, CONTROL | MOST, "Destroy secrets"); this->logger->log(this->logger, CONTROL | MOST, "Destroy secrets");
if (this->secrets.d_key.ptr != NULL)
{
allocator_free(this->secrets.d_key.ptr);
}
if (this->secrets.ai_key.ptr != NULL)
{
allocator_free(this->secrets.ai_key.ptr);
}
if (this->secrets.ar_key.ptr != NULL)
{
allocator_free(this->secrets.ar_key.ptr);
}
if (this->secrets.ei_key.ptr != NULL)
{
allocator_free(this->secrets.ei_key.ptr);
}
if (this->secrets.er_key.ptr != NULL)
{
allocator_free(this->secrets.er_key.ptr);
}
if (this->secrets.pi_key.ptr != NULL)
{
allocator_free(this->secrets.pi_key.ptr);
}
if (this->secrets.pr_key.ptr != NULL)
{
allocator_free(this->secrets.pr_key.ptr);
}
if ( this->crypter_initiator != NULL) allocator_free(this->secrets.d_key.ptr);
allocator_free(this->secrets.ai_key.ptr);
allocator_free(this->secrets.ar_key.ptr);
allocator_free(this->secrets.ei_key.ptr);
allocator_free(this->secrets.er_key.ptr);
allocator_free(this->secrets.pi_key.ptr);
allocator_free(this->secrets.pr_key.ptr);
if (this->crypter_initiator != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy initiator crypter");
this->crypter_initiator->destroy(this->crypter_initiator); this->crypter_initiator->destroy(this->crypter_initiator);
} }
if ( this->crypter_responder != NULL) if (this->crypter_responder != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy responder crypter");
this->crypter_responder->destroy(this->crypter_responder); this->crypter_responder->destroy(this->crypter_responder);
} }
if ( this->signer_initiator != NULL) if (this->signer_initiator != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy initiator signer");
this->signer_initiator->destroy(this->signer_initiator); this->signer_initiator->destroy(this->signer_initiator);
} }
if (this->signer_responder != NULL) if (this->signer_responder != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy responder signer");
this->signer_responder->destroy(this->signer_responder); this->signer_responder->destroy(this->signer_responder);
} }
if (this->prf != NULL) if (this->prf != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy prf");
this->prf->destroy(this->prf); this->prf->destroy(this->prf);
} }
/* destroy ike_sa_id */ /* destroy ike_sa_id */
this->logger->log(this->logger, CONTROL | MOST, "Destroy assigned ike_sa_id");
this->ike_sa_id->destroy(this->ike_sa_id); this->ike_sa_id->destroy(this->ike_sa_id);
/* destroy stored requested message */ /* destroy stored requested message */
if (this->last_requested_message != NULL) if (this->last_requested_message != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy last requested message");
this->last_requested_message->destroy(this->last_requested_message); this->last_requested_message->destroy(this->last_requested_message);
} }
/* destroy stored responded messages */ /* destroy stored responded messages */
if (this->last_responded_message != NULL) if (this->last_responded_message != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy last responded message");
this->last_responded_message->destroy(this->last_responded_message); this->last_responded_message->destroy(this->last_responded_message);
} }
this->logger->log(this->logger, CONTROL | MOST, "Destroy randomizer");
this->randomizer->destroy(this->randomizer); this->randomizer->destroy(this->randomizer);
if (this->me.host != NULL) if (this->me.host != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy host informations of me");
this->me.host->destroy(this->me.host); this->me.host->destroy(this->me.host);
} }
if (this->other.host != NULL) if (this->other.host != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy host informations of other");
this->other.host->destroy(this->other.host); this->other.host->destroy(this->other.host);
} }
@@ -913,11 +740,9 @@ static status_t destroy (private_ike_sa_t *this)
this->current_state->destroy(this->current_state); this->current_state->destroy(this->current_state);
this->logger->log(this->logger, CONTROL | MOST, "Destroy logger of IKE_SA"); this->logger->log(this->logger, CONTROL | MOST, "Destroy logger of IKE_SA");
global_logger_manager->destroy_logger(global_logger_manager, this->logger); global_logger_manager->destroy_logger(global_logger_manager, this->logger);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -926,20 +751,16 @@ static status_t destroy (private_ike_sa_t *this)
ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id) ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
{ {
private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t); private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
if (this == NULL)
{
return NULL;
}
/* Public functions */ /* Public functions */
this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message; this->protected.public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
this->protected.public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection; this->protected.public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id; this->protected.public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
this->protected.public.destroy = (status_t(*)(ike_sa_t*))destroy; this->protected.public.destroy = (void(*)(ike_sa_t*))destroy;
/* protected functions */ /* protected functions */
this->protected.build_message = (status_t (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message; this->protected.build_message = (void (*) (protected_ike_sa_t *, exchange_type_t , bool , message_t **)) build_message;
this->protected.compute_secrets = (status_t (*) (protected_ike_sa_t *,chunk_t ,chunk_t , chunk_t )) compute_secrets; this->protected.compute_secrets = (void (*) (protected_ike_sa_t *,chunk_t ,chunk_t , chunk_t )) compute_secrets;
this->protected.get_logger = (logger_t *(*) (protected_ike_sa_t *)) get_logger; this->protected.get_logger = (logger_t *(*) (protected_ike_sa_t *)) get_logger;
this->protected.get_my_host = (host_t *(*) (protected_ike_sa_t *)) get_my_host; this->protected.get_my_host = (host_t *(*) (protected_ike_sa_t *)) get_my_host;
this->protected.get_other_host = (host_t *(*) (protected_ike_sa_t *)) get_other_host; this->protected.get_other_host = (host_t *(*) (protected_ike_sa_t *)) get_other_host;
@@ -958,36 +779,10 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
/* initialize private fields */ /* initialize private fields */
this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL); this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
if (this->logger == NULL)
{
allocator_free(this);
}
if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS) this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not clone ike_sa_id");
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
allocator_free(this);
return NULL;
}
this->child_sas = linked_list_create(); this->child_sas = linked_list_create();
if (this->child_sas == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's");
this->ike_sa_id->destroy(this->ike_sa_id);
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
allocator_free(this);
return NULL;
}
this->randomizer = randomizer_create(); this->randomizer = randomizer_create();
if (this->randomizer == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's");
this->child_sas->destroy(this->child_sas);
this->ike_sa_id->destroy(this->ike_sa_id);
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
allocator_free(this);
}
this->me.host = NULL; this->me.host = NULL;
this->other.host = NULL; this->other.host = NULL;
@@ -995,29 +790,19 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
this->last_responded_message = NULL; this->last_responded_message = NULL;
this->message_id_out = 0; this->message_id_out = 0;
this->message_id_in = 0; this->message_id_in = 0;
this->secrets.d_key.ptr = NULL; this->secrets.d_key = CHUNK_INITIALIZER;
this->secrets.d_key.len = 0; this->secrets.ai_key = CHUNK_INITIALIZER;
this->secrets.ai_key.ptr = NULL; this->secrets.ar_key = CHUNK_INITIALIZER;
this->secrets.ai_key.len = 0; this->secrets.ei_key = CHUNK_INITIALIZER;
this->secrets.ar_key.ptr = NULL; this->secrets.er_key = CHUNK_INITIALIZER;
this->secrets.ar_key.len = 0; this->secrets.pi_key = CHUNK_INITIALIZER;
this->secrets.ei_key.ptr = NULL; this->secrets.pr_key = CHUNK_INITIALIZER;
this->secrets.ei_key.len = 0;
this->secrets.er_key.ptr = NULL;
this->secrets.er_key.len = 0;
this->secrets.pi_key.ptr = NULL;
this->secrets.pi_key.len = 0;
this->secrets.pr_key.ptr = NULL;
this->secrets.pr_key.len = 0;
this->crypter_initiator = NULL; this->crypter_initiator = NULL;
this->crypter_responder = NULL; this->crypter_responder = NULL;
this->signer_initiator = NULL; this->signer_initiator = NULL;
this->signer_responder = NULL; this->signer_responder = NULL;
this->prf = NULL; this->prf = NULL;
/* at creation time, IKE_SA is in a initiator state */ /* at creation time, IKE_SA is in a initiator state */
if (ike_sa_id->is_initiator(ike_sa_id)) if (ike_sa_id->is_initiator(ike_sa_id))
{ {
@@ -1027,17 +812,5 @@ ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
{ {
this->current_state = (state_t *) responder_init_create(&(this->protected)); this->current_state = (state_t *) responder_init_create(&(this->protected));
} }
if (this->current_state == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create state object");
this->child_sas->destroy(this->child_sas);
this->ike_sa_id->destroy(this->ike_sa_id);
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
this->randomizer->destroy(this->randomizer);
allocator_free(this);
}
return &(this->protected.public); return &(this->protected.public);
} }
+25 -28
View File
@@ -1,8 +1,7 @@
/** /**
* @file ike_sa.h * @file ike_sa.h
* *
* @brief Class ike_sa_t. An object of this type is managed by an * @brief Interface of ike_sa_id_t.
* ike_sa_manager_t object and represents an IKE_SA
* *
*/ */
@@ -45,8 +44,8 @@
typedef struct ike_sa_t ike_sa_t; typedef struct ike_sa_t ike_sa_t;
/** /**
* @brief This class is used to represent an IKE_SA * @brief Class ike_sa_t. An object of this type is managed by an
* * ike_sa_manager_t object and represents an IKE_SA.
*/ */
struct ike_sa_t { struct ike_sa_t {
@@ -60,7 +59,7 @@ struct ike_sa_t {
status_t (*process_message) (ike_sa_t *this,message_t *message); status_t (*process_message) (ike_sa_t *this,message_t *message);
/** /**
* Initiate a new connection with given configuration name * @brief Initiate a new connection with given configuration name.
* *
* @param this calling object * @param this calling object
* @param name name of the configuration * @param name name of the configuration
@@ -69,26 +68,28 @@ struct ike_sa_t {
status_t (*initialize_connection) (ike_sa_t *this, char *name); status_t (*initialize_connection) (ike_sa_t *this, char *name);
/** /**
* @brief Get the id of the SA * @brief Get the id of the SA.
* *
* @param this ike_sa_t-message_t object object * @param this ike_sa_t object object
* @return ike_sa's ike_sa_id_t * @return ike_sa's ike_sa_id_t
*/ */
ike_sa_id_t* (*get_id) (ike_sa_t *this); ike_sa_id_t* (*get_id) (ike_sa_t *this);
/** /**
* @brief Destroys a ike_sa_t object * @brief Destroys a ike_sa_t object.
* *
* @param this ike_sa_t object * @param this ike_sa_t object
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (ike_sa_t *this); void (*destroy) (ike_sa_t *this);
}; };
typedef struct protected_ike_sa_t protected_ike_sa_t; typedef struct protected_ike_sa_t protected_ike_sa_t;
/** /**
* Protected data of an ike_sa_t object * @brief Protected data of an ike_sa_t object.
*
* This members should only be accessed from
* the varius state classes.
*/ */
struct protected_ike_sa_t { struct protected_ike_sa_t {
@@ -105,15 +106,12 @@ struct protected_ike_sa_t {
* *
* Used in every state. * Used in every state.
* *
* @param this calling object * @param this calling object
* @param type exchange type of new message * @param type exchange type of new message
* @param request TRUE, if message has to be a request * @param request TRUE, if message has to be a request
* @param message new message is stored at this location * @param message new message is stored at this location
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_message) (protected_ike_sa_t *this, exchange_type_t type, bool request, message_t **message); void (*build_message) (protected_ike_sa_t *this, exchange_type_t type, bool request, message_t **message);
/** /**
* Initiate a new connection with given configuration name * Initiate a new connection with given configuration name
@@ -122,9 +120,8 @@ struct protected_ike_sa_t {
* @param dh_shared_secret shared secret of diffie hellman exchange * @param dh_shared_secret shared secret of diffie hellman exchange
* @param initiator_nonce nonce of initiator * @param initiator_nonce nonce of initiator
* @param responder_nonce nonce of responder * @param responder_nonce nonce of responder
* @return TODO
*/ */
status_t (*compute_secrets) (protected_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce); void (*compute_secrets) (protected_ike_sa_t *this,chunk_t dh_shared_secret,chunk_t initiator_nonce, chunk_t responder_nonce);
/** /**
* Gets the internal stored logger_t object for given ike_sa_t object. * Gets the internal stored logger_t object for given ike_sa_t object.
@@ -234,14 +231,14 @@ struct protected_ike_sa_t {
/** /**
* Creates an ike_sa_t object with a specific ike_sa_id_t object * Creates an ike_sa_t object with a specific ike_sa_id_t object
* *
* @param[in] ike_sa_id ike_sa_id_t object to associate with new IKE_SA. * @param[in] ike_sa_id ike_sa_id_t object to associate with new IKE_SA.
* The object is internal getting cloned * The object is internal getting cloned
* and so has to be destroyed by the caller. * and so has to be destroyed by the caller.
* *
* @warning the Content of internal ike_sa_id_t object can change over time * @warning the Content of internal ike_sa_id_t object can change over time
* e.g. when a IKE_SA_INIT has been finished * e.g. when a IKE_SA_INIT has been finished.
* *
* @return created ike_sa_t object * @return created ike_sa_t object
*/ */
ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id); ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id);
+33 -54
View File
@@ -1,7 +1,7 @@
/** /**
* @file ike_sa_id.c * @file ike_sa_id.c
* *
* @brief Class for identification of an IKE_SA * @brief Implementation of ike_sa_id_t.
* *
*/ */
@@ -39,9 +39,6 @@ struct private_ike_sa_id_t {
*/ */
ike_sa_id_t public; ike_sa_id_t public;
/* Private values */
/** /**
* SPI of Initiator * SPI of Initiator
*/ */
@@ -61,22 +58,23 @@ struct private_ike_sa_id_t {
/** /**
* @brief implements function set_responder_spi of ike_sa_id_t * implements ike_sa_id_t.set_responder_spi.
*/ */
static status_t set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi) static void set_responder_spi (private_ike_sa_id_t *this, u_int64_t responder_spi)
{ {
this->responder_spi = responder_spi; this->responder_spi = responder_spi;
return SUCCESS;
}
static status_t set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
{
this->initiator_spi = initiator_spi;
return SUCCESS;
} }
/** /**
* @brief implements ike_sa_id_t.get_initiator_spi * implements ike_sa_id_t.set_initiator_spi.
*/
static void set_initiator_spi(private_ike_sa_id_t *this, u_int64_t initiator_spi)
{
this->initiator_spi = initiator_spi;
}
/**
* implements ike_sa_id_t.get_initiator_spi.
*/ */
static u_int64_t get_initiator_spi (private_ike_sa_id_t *this) static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
{ {
@@ -84,7 +82,7 @@ static u_int64_t get_initiator_spi (private_ike_sa_id_t *this)
} }
/** /**
* @brief implements ike_sa_id_t.get_responder_spi * implements ike_sa_id_t.get_responder_spi.
*/ */
static u_int64_t get_responder_spi (private_ike_sa_id_t *this) static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
{ {
@@ -92,51 +90,40 @@ static u_int64_t get_responder_spi (private_ike_sa_id_t *this)
} }
/** /**
* @brief implements function equals of ike_sa_id_t * implements ike_sa_id_t.get_responder_spi.
*/ */
static status_t equals (private_ike_sa_id_t *this,private_ike_sa_id_t *other, bool *are_equal) static bool equals (private_ike_sa_id_t *this, private_ike_sa_id_t *other)
{ {
if ((this == NULL)||(other == NULL)) if ((this == NULL)||(other == NULL))
{ {
return FAILED; return FALSE;
} }
if ((this->is_initiator_flag == other->is_initiator_flag) && if ((this->is_initiator_flag == other->is_initiator_flag) &&
(this->initiator_spi == other->initiator_spi) && (this->initiator_spi == other->initiator_spi) &&
(this->responder_spi == other->responder_spi)) (this->responder_spi == other->responder_spi))
{ {
/* private_ike_sa_id's are equal */ /* private_ike_sa_id's are equal */
*are_equal = TRUE; return TRUE;
} }
else else
{ {
/* private_ike_sa_id's are not equal */ /* private_ike_sa_id's are not equal */
*are_equal = FALSE; return FALSE;
} }
return SUCCESS;
} }
/** /**
* @brief implements function replace_values of ike_sa_id_t * implements ike_sa_id_t.replace_values.
*/ */
status_t replace_values (private_ike_sa_id_t *this, private_ike_sa_id_t *other) static void replace_values(private_ike_sa_id_t *this, private_ike_sa_id_t *other)
{ {
if ((this == NULL) || (other == NULL))
{
return FAILED;
}
this->initiator_spi = other->initiator_spi; this->initiator_spi = other->initiator_spi;
this->responder_spi = other->responder_spi; this->responder_spi = other->responder_spi;
this->is_initiator_flag = other->is_initiator_flag; this->is_initiator_flag = other->is_initiator_flag;
return SUCCESS;
} }
/** /**
* @brief implements ike_sa_id_t.is_initiator * implements ike_sa_id_t.is_initiator.
*/ */
static bool is_initiator(private_ike_sa_id_t *this) static bool is_initiator(private_ike_sa_id_t *this)
{ {
@@ -144,7 +131,7 @@ static bool is_initiator(private_ike_sa_id_t *this)
} }
/** /**
* @brief implements ike_sa_id_t.switch_initiator * implements ike_sa_id_t.switch_initiator.
*/ */
static bool switch_initiator(private_ike_sa_id_t *this) static bool switch_initiator(private_ike_sa_id_t *this)
{ {
@@ -159,24 +146,20 @@ static bool switch_initiator(private_ike_sa_id_t *this)
return this->is_initiator_flag; return this->is_initiator_flag;
} }
/** /**
* @brief implements function clone of ike_sa_id_t * implements ike_sa_id_t.clone.
*/ */
static status_t clone (private_ike_sa_id_t *this, ike_sa_id_t **clone_of_this) static ike_sa_id_t* clone(private_ike_sa_id_t *this)
{ {
*clone_of_this = ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag); return ike_sa_id_create(this->initiator_spi, this->responder_spi, this->is_initiator_flag);
return (*clone_of_this == NULL) ? OUT_OF_RES : SUCCESS;
} }
/** /**
* @brief implements function destroy of ike_sa_id_t * implements ike_sa_id_t.clone.
*/ */
static status_t destroy (private_ike_sa_id_t *this) static void destroy(private_ike_sa_id_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -185,24 +168,20 @@ static status_t destroy (private_ike_sa_id_t *this)
ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag) ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiator_flag)
{ {
private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t); private_ike_sa_id_t *this = allocator_alloc_thing(private_ike_sa_id_t);
if (this == NULL)
{
return NULL;
}
/* Public functions */ /* Public functions */
this->public.set_responder_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi; this->public.set_responder_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_responder_spi;
this->public.set_initiator_spi = (status_t(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi; this->public.set_initiator_spi = (void(*)(ike_sa_id_t*,u_int64_t)) set_initiator_spi;
this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi; this->public.get_responder_spi = (u_int64_t(*)(ike_sa_id_t*)) get_responder_spi;
this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi; this->public.get_initiator_spi = (u_int64_t(*)(ike_sa_id_t*)) get_initiator_spi;
this->public.equals = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*,bool*)) equals; this->public.equals = (bool(*)(ike_sa_id_t*,ike_sa_id_t*)) equals;
this->public.replace_values = (status_t(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values; this->public.replace_values = (void(*)(ike_sa_id_t*,ike_sa_id_t*)) replace_values;
this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator; this->public.is_initiator = (bool(*)(ike_sa_id_t*)) is_initiator;
this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator; this->public.switch_initiator = (bool(*)(ike_sa_id_t*)) switch_initiator;
this->public.clone = (status_t(*)(ike_sa_id_t*,ike_sa_id_t**)) clone; this->public.clone = (ike_sa_id_t*(*)(ike_sa_id_t*)) clone;
this->public.destroy = (status_t(*)(ike_sa_id_t*))destroy; this->public.destroy = (void(*)(ike_sa_id_t*))destroy;
/* private data */ /* private data */
this->initiator_spi = initiator_spi; this->initiator_spi = initiator_spi;
+42 -44
View File
@@ -1,7 +1,7 @@
/** /**
* @file ike_sa_id.h * @file ike_sa_id.h
* *
* @brief Class for identification of an IKE_SA * @brief Interface of ike_sa_id_t.
* *
*/ */
@@ -42,98 +42,96 @@ struct ike_sa_id_t {
* *
* This function is called when a request or reply of a IKE_SA_INIT is received. * This function is called when a request or reply of a IKE_SA_INIT is received.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @param responder_spi SPI of responder to set * @param responder_spi SPI of responder to set
* @return SUCCESSFUL in any case
*/ */
status_t (*set_responder_spi) (ike_sa_id_t *this, u_int64_t responder_spi); void (*set_responder_spi) (ike_sa_id_t *this, u_int64_t responder_spi);
/** /**
* @brief Sets the SPI of the initiator. * @brief Sets the SPI of the initiator.
* *
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @param initiator_spi SPI to set * @param initiator_spi SPI to set
* @return SUCCESSFUL in any case
*/ */
status_t (*set_initiator_spi) (ike_sa_id_t *this, u_int64_t initiator_spi); void (*set_initiator_spi) (ike_sa_id_t *this, u_int64_t initiator_spi);
/** /**
* @brief Returns the initiator spi * @brief Returns the initiator spi.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @return spi of the initiator * @return spi of the initiator
*/ */
u_int64_t (*get_initiator_spi) (ike_sa_id_t *this); u_int64_t (*get_initiator_spi) (ike_sa_id_t *this);
/** /**
* @brief Returns the responder spi * @brief Returns the responder spi.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @return spi of the responder * @return spi of the responder
*/ */
u_int64_t (*get_responder_spi) (ike_sa_id_t *this); u_int64_t (*get_responder_spi) (ike_sa_id_t *this);
/** /**
* @brief Check if two ike_sa_ids are equal * @brief Check if two ike_sa_ids are equal.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @param other ike_sa_id object to check if equal * @param other ike_sa_id object to check if equal
* @param are_equal is set to TRUE, if given ike_sa_ids are equal, FALSE otherwise * @return TRUE if given ike_sa_ids are equal, FALSE otherwise
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/ */
status_t (*equals) (ike_sa_id_t *this,ike_sa_id_t *other, bool *are_equal); bool (*equals) (ike_sa_id_t *this, ike_sa_id_t *other);
/** /**
* @brief Replace the values of a given ike_sa_id_t object with values * @brief Replace the values of a given ike_sa_id_t object with values.
* from another ike_sa_id_t object * from another ike_sa_id_t object.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @param other ike_sa_id_t object which values will be taken * @param other ike_sa_id_t object which values will be taken
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/ */
status_t (*replace_values) (ike_sa_id_t *this, ike_sa_id_t *other); void (*replace_values) (ike_sa_id_t *this, ike_sa_id_t *other);
/** /**
* @brief gets the initiator flag * @brief gets the initiator flag.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @return TRUE if we are the original initator * @return TRUE if we are the original initator
*/ */
bool (*is_initiator) (ike_sa_id_t *this); bool (*is_initiator) (ike_sa_id_t *this);
/** /**
* @brief switches the initiator flag * @brief switches the initiator flag.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @return TRUE if we are the original initator after switch * @return TRUE if we are the original initator after switch
*/ */
bool (*switch_initiator) (ike_sa_id_t *this); bool (*switch_initiator) (ike_sa_id_t *this);
/** /**
* @brief Clones a given ike_sa_id_t object * @brief Clones a given ike_sa_id_t object.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @param clone_of_this ike_sa_id_t object which will be created * @return cloned ike_sa_id
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/ */
status_t (*clone) (ike_sa_id_t *this,ike_sa_id_t **clone_of_this); ike_sa_id_t *(*clone) (ike_sa_id_t *this);
/** /**
* @brief Destroys a ike_sa_id_tobject * @brief Destroys a ike_sa_id_tobject.
* *
* @param this ike_sa_id_t object * @param this ike_sa_id_t object
* @return SUCCESSFUL if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (ike_sa_id_t *this); void (*destroy) (ike_sa_id_t *this);
}; };
/** /**
* Creates an ike_sa_id_t object with specific spi's and defined role * @brief Creates an ike_sa_id_t object with specific spi's and defined role
* *
* @warning The initiator SPI and role is not changeable after initiating a ike_sa_id object * @warning The initiator SPI and role is not changeable after initiating a ike_sa_id object
*
* @param initiator_spi initiators spi
* @param responder_spi responders spi
* @param is_initiator TRUE if we are the original initiator
* @return created ike_sa_id_t object
*/ */
ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiaor); ike_sa_id_t * ike_sa_id_create(u_int64_t initiator_spi, u_int64_t responder_spi, bool is_initiaor);
#endif /*IKE_SA_ID_H_*/ #endif /*IKE_SA_ID_H_*/
+106 -230
View File
@@ -96,11 +96,6 @@ static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
{ {
ike_sa_entry_t *this = allocator_alloc_thing(ike_sa_entry_t); ike_sa_entry_t *this = allocator_alloc_thing(ike_sa_entry_t);
if (this == NULL)
{
return NULL;
}
/* destroy function */ /* destroy function */
this->destroy = ike_sa_entry_destroy; this->destroy = ike_sa_entry_destroy;
@@ -113,22 +108,11 @@ static ike_sa_entry_t *ike_sa_entry_create(ike_sa_id_t *ike_sa_id)
this->driveout_waiting_threads = FALSE; this->driveout_waiting_threads = FALSE;
/* ike_sa_id is always cloned */ /* ike_sa_id is always cloned */
ike_sa_id->clone(ike_sa_id, &(this->ike_sa_id)); this->ike_sa_id = ike_sa_id->clone(ike_sa_id);
if (this->ike_sa_id == NULL)
{
allocator_free(this);
return NULL;
}
/* create new ike_sa */ /* create new ike_sa */
this->ike_sa = ike_sa_create(ike_sa_id); this->ike_sa = ike_sa_create(ike_sa_id);
if (this->ike_sa == NULL)
{
this->ike_sa_id->destroy(this->ike_sa_id);
allocator_free(this);
return NULL;
}
return this; return this;
} }
@@ -149,11 +133,9 @@ struct private_ike_sa_manager_t {
* we give out SPIs incremental * we give out SPIs incremental
* *
* @param this the ike_sa_manager * @param this the ike_sa_manager
* @param spi[out] spi will be written here * @return the next spi
* @return SUCCESS or,
* OUT_OF_RES when we already served 2^64 SPIs ;-)
*/ */
status_t (*get_next_spi) (private_ike_sa_manager_t *this, u_int64_t *spi); u_int64_t (*get_next_spi) (private_ike_sa_manager_t *this);
/** /**
* @brief find the ike_sa_entry in the list by SPIs * @brief find the ike_sa_entry in the list by SPIs
@@ -167,7 +149,6 @@ struct private_ike_sa_manager_t {
* @return * @return
* - SUCCESS when found, * - SUCCESS when found,
* - NOT_FOUND when no such ike_sa_id in list * - NOT_FOUND when no such ike_sa_id in list
* - OUT_OF_RES
*/ */
status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry); status_t (*get_entry_by_id) (private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry);
@@ -183,7 +164,6 @@ struct private_ike_sa_manager_t {
* @return * @return
* - SUCCESS when found, * - SUCCESS when found,
* - NOT_FOUND when no such ike_sa_id in list * - NOT_FOUND when no such ike_sa_id in list
* - OUT_OF_RES
*/ */
status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry); status_t (*get_entry_by_sa) (private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry);
@@ -221,7 +201,7 @@ struct private_ike_sa_manager_t {
/** /**
* @see private_ike_sa_manager_t.get_entry_by_id * Implements private_ike_sa_manager_t.get_entry_by_id.
*/ */
static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry) static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id, ike_sa_entry_t **entry)
{ {
@@ -230,22 +210,14 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike
status_t status; status_t status;
/* create iterator over list of ike_sa's */ /* create iterator over list of ike_sa's */
status = list->create_iterator(list, &iterator, TRUE); list->create_iterator(list, &iterator, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
/* out of res */
return status;
}
/* default status */ /* default status */
status = NOT_FOUND; status = NOT_FOUND;
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
{ {
ike_sa_entry_t *current; ike_sa_entry_t *current;
bool are_equal = FALSE;
iterator->current(iterator, (void**)&current); iterator->current(iterator, (void**)&current);
if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0) { if (current->ike_sa_id->get_responder_spi(current->ike_sa_id) == 0) {
@@ -259,10 +231,9 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike
break; break;
} }
} }
current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id, &are_equal); if (current->ike_sa_id->equals(current->ike_sa_id, ike_sa_id))
if (are_equal)
{ {
this->logger->log(this->logger,CONTROL | MOST,"Found entry by full ID"); this->logger->log(this->logger,CONTROL | MOST,"Found entry by full ID");
*entry = current; *entry = current;
status = SUCCESS; status = SUCCESS;
break; break;
@@ -274,7 +245,7 @@ static status_t get_entry_by_id(private_ike_sa_manager_t *this, ike_sa_id_t *ike
} }
/** /**
* @see private_ike_sa_manager_t.get_entry_by_sa * Implements private_ike_sa_manager_t.get_entry_by_sa.
*/ */
static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry) static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa, ike_sa_entry_t **entry)
{ {
@@ -282,12 +253,7 @@ static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa
iterator_t *iterator; iterator_t *iterator;
status_t status; status_t status;
status = list->create_iterator(list, &iterator, TRUE); list->create_iterator(list, &iterator, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
return status;
}
/* default status */ /* default status */
status = NOT_FOUND; status = NOT_FOUND;
@@ -306,11 +272,12 @@ static status_t get_entry_by_sa(private_ike_sa_manager_t *this, ike_sa_t *ike_sa
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
return status; return status;
} }
/** /**
* @see private_ike_sa_manager_s.delete_entry * Implements private_ike_sa_manager_s.delete_entry.
*/ */
static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry) static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *entry)
{ {
@@ -318,13 +285,7 @@ static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *ent
iterator_t *iterator; iterator_t *iterator;
status_t status; status_t status;
status = list->create_iterator(list, &iterator, TRUE); list->create_iterator(list, &iterator, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger,ERROR,"Fatal Error: could not create iterator: %s",mapping_find(status_m,status));
return status;
}
status = NOT_FOUND; status = NOT_FOUND;
@@ -347,85 +308,47 @@ static status_t delete_entry(private_ike_sa_manager_t *this, ike_sa_entry_t *ent
/** /**
* @see private_ike_sa_manager_t.get_next_spi * Implements private_ike_sa_manager_t.get_next_spi.
*/ */
static status_t get_next_spi(private_ike_sa_manager_t *this, u_int64_t *spi) static u_int64_t get_next_spi(private_ike_sa_manager_t *this)
{ {
this->next_spi++; this->next_spi++;
if (this->next_spi == 0) { if (this->next_spi == 0) {
/* our software ran so incredible stable, we have no more /* TODO handle overflow,
* SPIs to give away :-/. */ * delete all SAs or so
this->logger->log(this->logger,CONTROL | MOST,"No more SPI values available. WOW!"); */
return OUT_OF_RES;
} }
*spi = this->next_spi; return this->next_spi;
return SUCCESS;
} }
/** /**
* Implementation of ike_sa_manager.create_and_checkout. * Implementation of ike_sa_manager.create_and_checkout.
*/ */
static status_t create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa) static void create_and_checkout(private_ike_sa_manager_t *this,ike_sa_t **ike_sa)
{ {
status_t retval;
u_int64_t initiator_spi; u_int64_t initiator_spi;
ike_sa_entry_t *new_ike_sa_entry; ike_sa_entry_t *new_ike_sa_entry;
ike_sa_id_t *new_ike_sa_id; ike_sa_id_t *new_ike_sa_id;
retval = this->get_next_spi(this, &initiator_spi); initiator_spi = this->get_next_spi(this);
if (retval == SUCCESS) new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
{ new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
new_ike_sa_id = ike_sa_id_create(0, 0, TRUE);
if (new_ike_sa_id != NULL)
{
new_ike_sa_id->set_initiator_spi(new_ike_sa_id, initiator_spi);
/* create entry */
new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id);
new_ike_sa_id->destroy(new_ike_sa_id);
if (new_ike_sa_entry != NULL)
{
/* each access is locked */
pthread_mutex_lock(&(this->mutex));
retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
if (retval == SUCCESS)
{
/* check ike_sa out */
this->logger->log(this->logger,CONTROL | MORE ,"New IKE_SA created and added to list of known IKE_SA's");
new_ike_sa_entry->checked_out = TRUE;
*ike_sa = new_ike_sa_entry->ike_sa;
/* DON'T use return, we must unlock the mutex! */
}
else
{
/* ike_sa_entry could not be added to list*/
this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry_t could not be added to list");
}
pthread_mutex_unlock(&(this->mutex));
}
else
{
/* new ike_sa_entry could not be created */
this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry_t could not be created");
retval = OUT_OF_RES;
}
}
else
{
/* new ike_sa_id could not be created */
this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_id_t could not be created");
retval = OUT_OF_RES;
}
}
else
{
/* next SPI could not be created */
this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created");
}
return retval; /* create entry */
new_ike_sa_entry = ike_sa_entry_create(new_ike_sa_id);
new_ike_sa_id->destroy(new_ike_sa_id);
/* each access is locked */
pthread_mutex_lock(&(this->mutex));
this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
/* check ike_sa out */
this->logger->log(this->logger,CONTROL | MORE ,"New IKE_SA created and added to list of known IKE_SA's");
new_ike_sa_entry->checked_out = TRUE;
*ike_sa = new_ike_sa_entry->ike_sa;
pthread_mutex_unlock(&(this->mutex));
} }
/** /**
@@ -436,13 +359,13 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id,
bool responder_spi_set; bool responder_spi_set;
bool initiator_spi_set; bool initiator_spi_set;
status_t retval; status_t retval;
/* each access is locked */ /* each access is locked */
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id)); responder_spi_set = (FALSE != ike_sa_id->get_responder_spi(ike_sa_id));
initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id)); initiator_spi_set = (FALSE != ike_sa_id->get_initiator_spi(ike_sa_id));
if (initiator_spi_set && responder_spi_set) if (initiator_spi_set && responder_spi_set)
{ {
/* we SHOULD have an IKE_SA for these SPIs in the list, /* we SHOULD have an IKE_SA for these SPIs in the list,
@@ -461,44 +384,44 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id,
} }
else else
{ {
/* is this IKE_SA already checked out ?? /* is this IKE_SA already checked out ??
* are we welcome to get this SA ? */ * are we welcome to get this SA ? */
while (entry->checked_out && !entry->driveout_waiting_threads) while (entry->checked_out && !entry->driveout_waiting_threads)
{ {
/* so wait until we can get it for us. /* so wait until we can get it for us.
* we register us as waiting. * we register us as waiting.
*/ */
entry->waiting_threads++; entry->waiting_threads++;
pthread_cond_wait(&(entry->condvar), &(this->mutex)); pthread_cond_wait(&(entry->condvar), &(this->mutex));
entry->waiting_threads--; entry->waiting_threads--;
} }
/* hm, a deletion request forbids us to get this SA, go home */ /* hm, a deletion request forbids us to get this SA, go home */
if (entry->driveout_waiting_threads) if (entry->driveout_waiting_threads)
{ {
/* we must signal here, others are interested that we leave */ /* we must signal here, others are interested that we leave */
pthread_cond_signal(&(entry->condvar)); pthread_cond_signal(&(entry->condvar));
this->logger->log(this->logger,CONTROL|MORE,"Drive out waiting thread for existing IKE_SA"); this->logger->log(this->logger,CONTROL|MORE,"Drive out waiting thread for existing IKE_SA");
retval = NOT_FOUND; retval = NOT_FOUND;
} }
else else
{ {
this->logger->log(this->logger,CONTROL|MOST,"IKE SA successfully checked out"); this->logger->log(this->logger,CONTROL|MOST,"IKE SA successfully checked out");
/* ok, this IKE_SA is finally ours */ /* ok, this IKE_SA is finally ours */
entry->checked_out = TRUE; entry->checked_out = TRUE;
*ike_sa = entry->ike_sa; *ike_sa = entry->ike_sa;
/* DON'T use return, we must unlock the mutex! */ /* DON'T use return, we must unlock the mutex! */
retval = SUCCESS; retval = SUCCESS;
} }
} }
} }
else else
{ {
this->logger->log(this->logger,ERROR | MORE,"IKE SA not stored in known IKE_SA list"); this->logger->log(this->logger,ERROR | MORE,"IKE SA not stored in known IKE_SA list");
/* looks like there is no such IKE_SA, better luck next time... */ /* looks like there is no such IKE_SA, better luck next time... */
/* DON'T use return, we must unlock the mutex! */ /* DON'T use return, we must unlock the mutex! */
retval = NOT_FOUND; retval = NOT_FOUND;
} }
} }
else if (initiator_spi_set && !responder_spi_set) else if (initiator_spi_set && !responder_spi_set)
{ {
@@ -512,53 +435,29 @@ static status_t checkout(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id,
u_int64_t responder_spi; u_int64_t responder_spi;
ike_sa_entry_t *new_ike_sa_entry; ike_sa_entry_t *new_ike_sa_entry;
/* set SPIs, we are the responder */
retval = this->get_next_spi(this, &responder_spi);
if (retval == SUCCESS)
{ /* next SPI could be created */
/* we also set arguments spi, so its still valid */
ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
/* create entry */
new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
if (new_ike_sa_entry != NULL)
{
retval = this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
if (retval == SUCCESS)
{
/* check ike_sa out */
this->logger->log(this->logger,CONTROL | MORE ,"IKE_SA added to list of known IKE_SA's");
new_ike_sa_entry->checked_out = TRUE;
*ike_sa = new_ike_sa_entry->ike_sa;
/* DON'T use return, we must unlock the mutex! */
}
else
{
/* ike_sa_entry could not be added to list*/
this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be added to list");
}
}
else
{
/* new ike_sa_entry could not be created */
this->logger->log(this->logger,ERROR,"Fatal error: ike_sa_entry could not be created");
retval = OUT_OF_RES;
}
}
else
{ /* next SPI could not be created */
this->logger->log(this->logger,ERROR,"Fatal error: Next SPI could not be created");
}
/* set SPIs, we are the responder */
responder_spi = this->get_next_spi(this);
/* we also set arguments spi, so its still valid */
ike_sa_id->set_responder_spi(ike_sa_id, responder_spi);
/* create entry */
new_ike_sa_entry = ike_sa_entry_create(ike_sa_id);
this->ike_sa_list->insert_last(this->ike_sa_list, new_ike_sa_entry);
/* check ike_sa out */
this->logger->log(this->logger,CONTROL | MORE ,"IKE_SA added to list of known IKE_SA's");
new_ike_sa_entry->checked_out = TRUE;
*ike_sa = new_ike_sa_entry->ike_sa;
retval = SUCCESS;
} }
else else
{ {
/* responder set, initiator not: here is something seriously wrong! */ /* responder set, initiator not: here is something seriously wrong! */
this->logger->log(this->logger,ERROR | MORE,"Invalid IKE_SA SPI's"); this->logger->log(this->logger,ERROR | MORE, "Invalid IKE_SA SPI's");
/* DON'T use return, we must unlock the mutex! */ /* DON'T use return, we must unlock the mutex! */
retval = INVALID_ARG; retval = INVALID_ARG;
} }
@@ -647,14 +546,13 @@ static status_t checkin_and_delete(private_ike_sa_manager_t *this, ike_sa_t *ike
this->logger->log(this->logger,ERROR,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA"); this->logger->log(this->logger,ERROR,"Fatal Error: Tried to checkin and delete nonexisting IKE_SA");
retval = NOT_FOUND; retval = NOT_FOUND;
} }
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return retval; return retval;
} }
/** /**
* Implements ike_sa_manager_t-function delete. * Implements ike_sa_manager_t.delete.
* @see ike_sa_manager_t.delete.
*/ */
static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id) static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
{ {
@@ -697,15 +595,13 @@ static status_t delete(private_ike_sa_manager_t *this, ike_sa_id_t *ike_sa_id)
} }
/** /**
* Implements ike_sa_manager_t-function destroy. * Implements ike_sa_manager_t.destroy.
* @see ike_sa_manager_t.destroy.
*/ */
static status_t destroy(private_ike_sa_manager_t *this) static void destroy(private_ike_sa_manager_t *this)
{ {
/* destroy all list entries */ /* destroy all list entries */
linked_list_t *list = this->ike_sa_list; linked_list_t *list = this->ike_sa_list;
iterator_t *iterator; iterator_t *iterator;
status_t status;
ike_sa_entry_t *entry; ike_sa_entry_t *entry;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
@@ -713,13 +609,7 @@ static status_t destroy(private_ike_sa_manager_t *this)
this->logger->log(this->logger,CONTROL | MORE,"Going to destroy IKE_SA manager and all managed IKE_SA's"); this->logger->log(this->logger,CONTROL | MORE,"Going to destroy IKE_SA manager and all managed IKE_SA's");
/* Step 1: drive out all waiting threads */ /* Step 1: drive out all waiting threads */
status = list->create_iterator(list, &iterator, TRUE); list->create_iterator(list, &iterator, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger,ERROR,"Fatal Error: Create of iterator while destroying IKE_SA-Manager failed");
return FAILED;
}
this->logger->log(this->logger,CONTROL | MOST,"Set driveout flags for all stored IKE_SA's"); this->logger->log(this->logger,CONTROL | MOST,"Set driveout flags for all stored IKE_SA's");
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
@@ -761,8 +651,6 @@ static status_t destroy(private_ike_sa_manager_t *this)
global_logger_manager->destroy_logger(global_logger_manager,this->logger); global_logger_manager->destroy_logger(global_logger_manager,this->logger);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -773,8 +661,8 @@ ike_sa_manager_t *ike_sa_manager_create()
private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t); private_ike_sa_manager_t *this = allocator_alloc_thing(private_ike_sa_manager_t);
/* assign public functions */ /* assign public functions */
this->public.destroy = (status_t(*)(ike_sa_manager_t*))destroy; this->public.destroy = (void(*)(ike_sa_manager_t*))destroy;
this->public.create_and_checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_t **sa))create_and_checkout; this->public.create_and_checkout = (void(*)(ike_sa_manager_t*, ike_sa_t **sa))create_and_checkout;
this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id, ike_sa_t **sa))checkout; this->public.checkout = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id, ike_sa_t **sa))checkout;
this->public.checkin = (status_t(*)(ike_sa_manager_t*, ike_sa_t *sa))checkin; this->public.checkin = (status_t(*)(ike_sa_manager_t*, ike_sa_t *sa))checkin;
this->public.delete = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id))delete; this->public.delete = (status_t(*)(ike_sa_manager_t*, ike_sa_id_t *sa_id))delete;
@@ -788,20 +676,8 @@ ike_sa_manager_t *ike_sa_manager_create()
/* initialize private variables */ /* initialize private variables */
this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL); this->logger = global_logger_manager->create_logger(global_logger_manager,IKE_SA_MANAGER,NULL);
if (this->logger == NULL)
{
allocator_free(this);
return NULL;
}
this->ike_sa_list = linked_list_create(); this->ike_sa_list = linked_list_create();
if (this->ike_sa_list == NULL)
{
this->logger->log(this->logger,ERROR,"Fatal error: Failed to create list for managed IKE_SA");
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
allocator_free(this);
return NULL;
}
pthread_mutex_init(&(this->mutex), NULL); pthread_mutex_init(&(this->mutex), NULL);
+15 -16
View File
@@ -58,7 +58,6 @@ struct ike_sa_manager_t {
* @returns * @returns
* - SUCCESS if checkout successful * - SUCCESS if checkout successful
* - NOT_FOUND when no such SA is available * - NOT_FOUND when no such SA is available
* - OUT_OF_RES
*/ */
status_t (*checkout) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *sa_id, ike_sa_t **ike_sa); status_t (*checkout) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *sa_id, ike_sa_t **ike_sa);
@@ -73,12 +72,9 @@ struct ike_sa_manager_t {
* *
* @param ike_sa_manager the manager object * @param ike_sa_manager the manager object
* @param ike_sa[out] checked out SA * @param ike_sa[out] checked out SA
* @returns
* - SUCCESS if checkout successful
* - OUT_OF_RES
*/ */
status_t (*create_and_checkout) (ike_sa_manager_t* ike_sa_manager,ike_sa_t **ike_sa); void (*create_and_checkout) (ike_sa_manager_t* ike_sa_manager,ike_sa_t **ike_sa);
/** /**
* @brief Checkin the SA after usage * @brief Checkin the SA after usage
* *
@@ -88,8 +84,9 @@ struct ike_sa_manager_t {
* @param ike_sa_manager the manager object * @param ike_sa_manager the manager object
* @param ike_sa_id[in/out] the SA identifier, will be updated * @param ike_sa_id[in/out] the SA identifier, will be updated
* @param ike_sa[out] checked out SA * @param ike_sa[out] checked out SA
* @returns SUCCESS if checked in * @returns
* NOT_FOUND when not found (shouldn't happen!) * - SUCCESS if checked in
* - NOT_FOUND when not found (shouldn't happen!)
*/ */
status_t (*checkin) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa); status_t (*checkin) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa);
/** /**
@@ -100,17 +97,20 @@ struct ike_sa_manager_t {
* *
* @param ike_sa_manager the manager object * @param ike_sa_manager the manager object
* @param ike_sa_id[in/out] the SA identifier * @param ike_sa_id[in/out] the SA identifier
* @returns SUCCESS if found * @returns
* NOT_FOUND when no such SA is available * - SUCCESS if found
* - NOT_FOUND when no such SA is available
*/ */
status_t (*delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *ike_sa_id); status_t (*delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_id_t *ike_sa_id);
/** /**
* @brief delete a checked out SA * @brief delete a checked out SA
* *
* @param ike_sa_manager the manager object * @param ike_sa_manager the manager object
* @param ike_sa SA to delete * @param ike_sa SA to delete
* @returns SUCCESS if found * @returns
* NOT_FOUND when no such SA is available * - SUCCESS if found
* - NOT_FOUND when no such SA is available
*/ */
status_t (*checkin_and_delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa); status_t (*checkin_and_delete) (ike_sa_manager_t* ike_sa_manager, ike_sa_t *ike_sa);
@@ -120,15 +120,14 @@ struct ike_sa_manager_t {
* Threads will be driven out, so all SAs can be deleted cleanly * Threads will be driven out, so all SAs can be deleted cleanly
* *
* @param ike_sa_manager the manager object * @param ike_sa_manager the manager object
* @returns SUCCESS if succeeded, FAILED otherwise
*/ */
status_t (*destroy) (ike_sa_manager_t *ike_sa_manager); void (*destroy) (ike_sa_manager_t *ike_sa_manager);
}; };
/** /**
* @brief Create a manager * @brief Create a manager
* *
* @returns the manager * @returns the created manager
*/ */
ike_sa_manager_t *ike_sa_manager_create(); ike_sa_manager_t *ike_sa_manager_create();
+2 -8
View File
@@ -63,10 +63,9 @@ static ike_sa_state_t get_state(private_ike_auth_requested_t *this)
/** /**
* Implements state_t.get_state * Implements state_t.get_state
*/ */
static status_t destroy(private_ike_auth_requested_t *this) static void destroy(private_ike_auth_requested_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -75,16 +74,11 @@ static status_t destroy(private_ike_auth_requested_t *this)
ike_auth_requested_t *ike_auth_requested_create(protected_ike_sa_t *ike_sa) ike_auth_requested_t *ike_auth_requested_create(protected_ike_sa_t *ike_sa)
{ {
private_ike_auth_requested_t *this = allocator_alloc_thing(private_ike_auth_requested_t); private_ike_auth_requested_t *this = allocator_alloc_thing(private_ike_auth_requested_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private data */ /* private data */
this->ike_sa = ike_sa; this->ike_sa = ike_sa;
+2 -8
View File
@@ -63,10 +63,9 @@ static ike_sa_state_t get_state(private_ike_sa_established_t *this)
/** /**
* Implements state_t.get_state * Implements state_t.get_state
*/ */
static status_t destroy(private_ike_sa_established_t *this) static void destroy(private_ike_sa_established_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -75,16 +74,11 @@ static status_t destroy(private_ike_sa_established_t *this)
ike_sa_established_t *ike_sa_established_create(protected_ike_sa_t *ike_sa) ike_sa_established_t *ike_sa_established_create(protected_ike_sa_t *ike_sa)
{ {
private_ike_sa_established_t *this = allocator_alloc_thing(private_ike_sa_established_t); private_ike_sa_established_t *this = allocator_alloc_thing(private_ike_sa_established_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private data */ /* private data */
this->ike_sa = ike_sa; this->ike_sa = ike_sa;
+21 -81
View File
@@ -138,13 +138,8 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t
/* get the list of suggested proposals */ /* get the list of suggested proposals */
status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE); sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal errror: Could not create iterator on suggested proposals");
payloads->destroy(payloads);
return status;
}
/* now let the configuration-manager check the selected proposals*/ /* now let the configuration-manager check the selected proposals*/
this->logger->log(this->logger, CONTROL | MOST, "Check suggested proposals"); this->logger->log(this->logger, CONTROL | MOST, "Check suggested proposals");
@@ -194,14 +189,8 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t
case KEY_EXCHANGE: case KEY_EXCHANGE:
{ {
ke_payload_t *ke_payload = (ke_payload_t*)payload; ke_payload_t *ke_payload = (ke_payload_t*)payload;
status = this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload)); this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not set other public value for DH exchange. Status %s",mapping_find(status_m,status));
payloads->destroy(payloads);
return OUT_OF_RES;
}
/* shared secret is computed AFTER processing of all payloads... */ /* shared secret is computed AFTER processing of all payloads... */
break; break;
@@ -209,28 +198,16 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t
case NONCE: case NONCE:
{ {
nonce_payload_t *nonce_payload = (nonce_payload_t*)payload; nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
if (this->received_nonce.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy existing received nonce");
allocator_free(this->received_nonce.ptr);
this->received_nonce.ptr = NULL;
this->received_nonce.len = 0;
}
status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not get received nonce");
payloads->destroy(payloads);
return OUT_OF_RES;
}
allocator_free(this->received_nonce.ptr);
this->received_nonce = CHUNK_INITIALIZER;
nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
break; break;
} }
default: default:
{ {
this->logger->log(this->logger, ERROR, "Fatal errror: Payload type not supported!!!!"); this->logger->log(this->logger, ERROR, "Payload type not supported!!!!");
payloads->destroy(payloads); payloads->destroy(payloads);
return FAILED; return FAILED;
} }
@@ -239,31 +216,16 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t
} }
payloads->destroy(payloads); payloads->destroy(payloads);
if (this->shared_secret.ptr != NULL) allocator_free(this->shared_secret.ptr);
{ this->shared_secret = CHUNK_INITIALIZER;
this->logger->log(this->logger, CONTROL | MOST, "Destroy existing shared_secret");
allocator_free(this->shared_secret.ptr);
this->shared_secret.ptr = NULL;
this->shared_secret.len = 0;
}
/* store shared secret */ /* store shared secret */
this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it"); this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &(this->shared_secret)); status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &(this->shared_secret));
this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &this->shared_secret); this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &this->shared_secret);
status = this->ike_sa->compute_secrets(this->ike_sa,this->shared_secret,this->sent_nonce, this->received_nonce); this->ike_sa->compute_secrets(this->ike_sa,this->shared_secret,this->sent_nonce, this->received_nonce);
if (status != SUCCESS)
{
/* secrets could not be computed */
this->logger->log(this->logger, ERROR | MORE, "Secrets could not be computed!");
return status;
}
/**************************** /****************************
* *
@@ -285,7 +247,6 @@ static status_t process_message(private_ike_sa_init_requested_t *this, message_t
// response->destroy(response); // response->destroy(response);
return SUCCESS; return SUCCESS;
} }
@@ -301,31 +262,17 @@ static ike_sa_state_t get_state(private_ike_sa_init_requested_t *this)
/** /**
* Implements state_t.get_state * Implements state_t.get_state
*/ */
static status_t destroy(private_ike_sa_init_requested_t *this) static void destroy(private_ike_sa_init_requested_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy state of type ike_sa_init_requested_t"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy state of type ike_sa_init_requested_t");
this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object"); this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object");
this->diffie_hellman->destroy(this->diffie_hellman); this->diffie_hellman->destroy(this->diffie_hellman);
if (this->sent_nonce.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
allocator_free(this->sent_nonce.ptr);
}
if (this->received_nonce.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
allocator_free(this->received_nonce.ptr);
}
if (this->shared_secret.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy shared secret");
allocator_free(this->shared_secret.ptr);
}
allocator_free(this->sent_nonce.ptr);
allocator_free(this->received_nonce.ptr);
allocator_free(this->shared_secret.ptr);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -335,22 +282,15 @@ ike_sa_init_requested_t *ike_sa_init_requested_create(protected_ike_sa_t *ike_sa
{ {
private_ike_sa_init_requested_t *this = allocator_alloc_thing(private_ike_sa_init_requested_t); private_ike_sa_init_requested_t *this = allocator_alloc_thing(private_ike_sa_init_requested_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private data */ /* private data */
this->ike_sa = ike_sa; this->ike_sa = ike_sa;
this->received_nonce.ptr = NULL; this->received_nonce = CHUNK_INITIALIZER;
this->received_nonce.len = 0; this->shared_secret = CHUNK_INITIALIZER;
this->shared_secret.ptr = NULL;
this->shared_secret.len = 0;
this->logger = this->ike_sa->get_logger(this->ike_sa); this->logger = this->ike_sa->get_logger(this->ike_sa);
this->diffie_hellman = diffie_hellman; this->diffie_hellman = diffie_hellman;
this->sent_nonce = sent_nonce; this->sent_nonce = sent_nonce;
@@ -111,16 +111,11 @@ static status_t destroy(private_ike_sa_init_responded_t *this)
ike_sa_init_responded_t *ike_sa_init_responded_create(protected_ike_sa_t *ike_sa, chunk_t shared_secret, chunk_t received_nonce, chunk_t sent_nonce) ike_sa_init_responded_t *ike_sa_init_responded_create(protected_ike_sa_t *ike_sa, chunk_t shared_secret, chunk_t received_nonce, chunk_t sent_nonce)
{ {
private_ike_sa_init_responded_t *this = allocator_alloc_thing(private_ike_sa_init_responded_t); private_ike_sa_init_responded_t *this = allocator_alloc_thing(private_ike_sa_init_responded_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private data */ /* private data */
this->ike_sa = ike_sa; this->ike_sa = ike_sa;
@@ -1,7 +1,7 @@
/** /**
* @file ike_sa_init_responded.h * @file ike_sa_init_responded.h
* *
* @brief State of a IKE_SA after responding to an IKE_SA_INIT request * @brief Interface of ike_sa_init_responded_t.
* *
*/ */
+63 -218
View File
@@ -91,11 +91,8 @@ struct private_initiator_init_t {
* *
* @param this calling object * @param this calling object
* @param message the created message will be stored at this location * @param message the created message will be stored at this location
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message); void (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
/** /**
* Builds the SA payload for this state. * Builds the SA payload for this state.
@@ -103,11 +100,8 @@ struct private_initiator_init_t {
* @param this calling object * @param this calling object
* @param payload The generated SA payload object of type ke_payload_t is * @param payload The generated SA payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload); void (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
/** /**
* Builds the KE payload for this state. * Builds the KE payload for this state.
@@ -115,22 +109,17 @@ struct private_initiator_init_t {
* @param this calling object * @param this calling object
* @param payload The generated KE payload object of type ke_payload_t is * @param payload The generated KE payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload); void (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
/** /**
* Builds the NONCE payload for this state. * Builds the NONCE payload for this state.
* *
* @param this calling object * @param this calling object
* @param payload The generated NONCE payload object of type ke_payload_t is * @param payload The generated NONCE payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload); void (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
/** /**
* Destroy function called internally of this class after state change succeeded. * Destroy function called internally of this class after state change succeeded.
@@ -138,9 +127,8 @@ struct private_initiator_init_t {
* This destroy function does not destroy objects which were passed to the new state. * This destroy function does not destroy objects which were passed to the new state.
* *
* @param this calling object * @param this calling object
* @return SUCCESS in any case
*/ */
status_t (*destroy_after_state_change) (private_initiator_init_t *this); void (*destroy_after_state_change) (private_initiator_init_t *this);
}; };
/** /**
@@ -148,18 +136,18 @@ struct private_initiator_init_t {
*/ */
static status_t initiate_connection (private_initiator_init_t *this, char *name) static status_t initiate_connection (private_initiator_init_t *this, char *name)
{ {
iterator_t *proposal_iterator; iterator_t *proposal_iterator;
ike_sa_init_requested_t *next_state; ike_sa_init_requested_t *next_state;
message_t *message; message_t *message;
packet_t *packet; packet_t *packet;
status_t status; status_t status;
host_t *my_host; host_t *my_host;
host_t *other_host; host_t *other_host;
randomizer_t *randomizer; randomizer_t *randomizer;
this->logger->log(this->logger, CONTROL, "Initializing connection %s",name); this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
/* get local host */
status = global_configuration_manager->get_local_host(global_configuration_manager, name, &my_host); status = global_configuration_manager->get_local_host(global_configuration_manager, name, &my_host);
if (status != SUCCESS) if (status != SUCCESS)
{ {
@@ -168,6 +156,7 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name)
} }
this->ike_sa->set_my_host(this->ike_sa,my_host); this->ike_sa->set_my_host(this->ike_sa,my_host);
/* get remote host */
status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &other_host); status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &other_host);
if (status != SUCCESS) if (status != SUCCESS)
{ {
@@ -176,22 +165,17 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name)
} }
this->ike_sa->set_other_host(this->ike_sa,other_host); this->ike_sa->set_other_host(this->ike_sa,other_host);
/* get dh group */
status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority); status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
if (status != SUCCESS) if (status != SUCCESS)
{ {
this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name); this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name);
return INVALID_ARG; return INVALID_ARG;
} }
status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); /* get proposals */
if (status != SUCCESS) this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
return status;
}
status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->get_other_host(this->ike_sa), proposal_iterator); status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->get_other_host(this->ike_sa), proposal_iterator);
/* not needed anymore */
proposal_iterator->destroy(proposal_iterator); proposal_iterator->destroy(proposal_iterator);
if (status != SUCCESS) if (status != SUCCESS)
{ {
@@ -202,70 +186,45 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name)
/* a diffie hellman object could allready exist caused by an failed initiate_connection call */ /* a diffie hellman object could allready exist caused by an failed initiate_connection call */
if (this->diffie_hellman == NULL) if (this->diffie_hellman == NULL)
{ {
this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object");
this->diffie_hellman = diffie_hellman_create(this->dh_group_number); this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
} }
if (this->diffie_hellman == NULL) if (this->diffie_hellman == NULL)
{ {
this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!"); this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
return FAILED; return FAILED;
} }
if (this->sent_nonce.ptr != NULL) if (this->sent_nonce.ptr != NULL)
{ {
this->logger->log(this->logger, ERROR, "Free existing sent nonce!");
allocator_free(this->sent_nonce.ptr); allocator_free(this->sent_nonce.ptr);
this->sent_nonce = CHUNK_INITIALIZER; this->sent_nonce = CHUNK_INITIALIZER;
} }
this ->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce"); this->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce");
randomizer = this->ike_sa->get_randomizer(this->ike_sa); randomizer = this->ike_sa->get_randomizer(this->ike_sa);
if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS) randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
{
this->logger->log(this->logger, ERROR, "Could not create nonce!"); this->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
return OUT_OF_RES;
} this->build_ike_sa_init_request (this,&message);
this ->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
status = this->build_ike_sa_init_request (this,&message);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not build IKE_SA_INIT request message");
return status;
}
/* generate packet */ /* generate packet */
this->logger->log(this->logger, CONTROL|MOST, "generate packet from message"); this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
status = message->generate(message, NULL, NULL, &packet); status = message->generate(message, NULL, NULL, &packet);
if (status != SUCCESS) if (status != SUCCESS)
{ {
this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message"); this->logger->log(this->logger, ERROR, "could not generate packet from message");
message->destroy(message); message->destroy(message);
return status; return status;
} }
this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue"); this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
status = global_send_queue->add(global_send_queue, packet); global_send_queue->add(global_send_queue, packet);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
packet->destroy(packet);
message->destroy(message);
return status;
}
/* state can now be changed */ /* state can now be changed */
this->logger->log(this->logger, CONTROL|MOST, "Create next state object"); this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, this->sent_nonce); next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, this->sent_nonce);
if (next_state == NULL)
{
this ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_requested_t");
message->destroy(message);
return FAILED;
}
/* last message can now be set */ /* last message can now be set */
status = this->ike_sa->set_last_requested_message(this->ike_sa, message); status = this->ike_sa->set_last_requested_message(this->ike_sa, message);
@@ -282,7 +241,7 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name)
this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state); this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
/* state has NOW changed :-) */ /* state has NOW changed :-) */
this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) ); this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s", mapping_find(ike_sa_state_m,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) );
this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object"); this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
this->destroy_after_state_change(this); this->destroy_after_state_change(this);
@@ -293,200 +252,99 @@ static status_t initiate_connection (private_initiator_init_t *this, char *name)
/** /**
* implements private_initiator_init_t.build_ike_sa_init_request * implements private_initiator_init_t.build_ike_sa_init_request
*/ */
static status_t build_ike_sa_init_request (private_initiator_init_t *this, message_t **request) static void build_ike_sa_init_request (private_initiator_init_t *this, message_t **request)
{ {
status_t status;
payload_t *payload; payload_t *payload;
message_t *message; message_t *message;
/* going to build message */ /* going to build message */
this ->logger->log(this->logger, CONTROL|MOST, "Going to build message"); this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message); this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build empty message");
return status;
}
/* build SA payload */
status = this->build_sa_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build SA payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message"); /* build SA payload */
this->build_sa_payload(this, &payload);
this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
message->add_payload(message, payload); message->add_payload(message, payload);
/* build KE payload */ /* build KE payload */
status = this->build_ke_payload(this, &payload); this->build_ke_payload(this, &payload);
if (status != SUCCESS) this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
{
this->logger->log(this->logger, ERROR, "Could not build KE payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
message->add_payload(message, payload); message->add_payload(message, payload);
/* build Nonce payload */ /* build Nonce payload */
status = this->build_nonce_payload(this, &payload); this->build_nonce_payload(this, &payload);
if (status != SUCCESS) this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
{
this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
message->add_payload(message, payload); message->add_payload(message, payload);
*request = message; *request = message;
return SUCCESS;
} }
/** /**
* implements private_initiator_init_t.build_sa_payload * implements private_initiator_init_t.build_sa_payload
*/ */
static status_t build_sa_payload(private_initiator_init_t *this, payload_t **payload) static void build_sa_payload(private_initiator_init_t *this, payload_t **payload)
{ {
sa_payload_t* sa_payload; sa_payload_t* sa_payload;
iterator_t *proposal_iterator; iterator_t *proposal_iterator;
status_t status;
/* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */ /* SA payload takes proposals from this->ike_sa_init_data.proposals
* and writes them to the created sa_payload
*/
this->logger->log(this->logger, CONTROL|MORE, "building sa payload"); this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
return status;
}
sa_payload = sa_payload_create(); sa_payload = sa_payload_create();
if (sa_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
return OUT_OF_RES;
}
while (proposal_iterator->has_next(proposal_iterator)) while (proposal_iterator->has_next(proposal_iterator))
{ {
proposal_substructure_t *current_proposal; proposal_substructure_t *current_proposal;
proposal_substructure_t *current_proposal_clone; proposal_substructure_t *current_proposal_clone;
status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal); proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = current_proposal->clone(current_proposal,&current_proposal_clone);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not clone current proposal");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
current_proposal->clone(current_proposal,&current_proposal_clone);
sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
} }
proposal_iterator->destroy(proposal_iterator); proposal_iterator->destroy(proposal_iterator);
this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
*payload = (payload_t *) sa_payload; *payload = (payload_t *) sa_payload;
return SUCCESS;
} }
/** /**
* implements private_initiator_init_t.build_ke_payload * implements private_initiator_init_t.build_ke_payload
*/ */
static status_t build_ke_payload(private_initiator_init_t *this, payload_t **payload) static void build_ke_payload(private_initiator_init_t *this, payload_t **payload)
{ {
ke_payload_t *ke_payload; ke_payload_t *ke_payload;
chunk_t key_data; chunk_t key_data;
status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building ke payload"); this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get my DH public value");
return status;
}
ke_payload = ke_payload_create(); ke_payload = ke_payload_create();
if (ke_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Could not create KE payload");
allocator_free_chunk(&key_data);
return OUT_OF_RES;
}
ke_payload->set_dh_group_number(ke_payload, this->dh_group_number); ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS) ke_payload->set_key_exchange_data(ke_payload, key_data);
{
this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
ke_payload->destroy(ke_payload);
allocator_free_chunk(&key_data);
return OUT_OF_RES;
}
allocator_free_chunk(&key_data);
this->logger->log(this->logger, CONTROL|MORE, "ke payload builded"); allocator_free_chunk(&key_data);
*payload = (payload_t *) ke_payload; *payload = (payload_t *) ke_payload;
return SUCCESS;
} }
/** /**
* implements private_initiator_init_t.build_nonce_payload * implements private_initiator_init_t.build_nonce_payload
*/ */
static status_t build_nonce_payload(private_initiator_init_t *this, payload_t **payload) static void build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
{ {
nonce_payload_t *nonce_payload; nonce_payload_t *nonce_payload;
status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building nonce payload"); this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
nonce_payload = nonce_payload_create(); nonce_payload = nonce_payload_create();
if (nonce_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
return OUT_OF_RES;
}
status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
if (status != SUCCESS) nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
{
this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
nonce_payload->destroy(nonce_payload);
return status;
}
*payload = (payload_t *) nonce_payload; *payload = (payload_t *) nonce_payload;
this->logger->log(this->logger, CONTROL|MORE, "nonce payload builded");
return SUCCESS;
} }
/** /**
@@ -510,7 +368,7 @@ static ike_sa_state_t get_state(private_initiator_init_t *this)
/** /**
* Implements state_t.get_state * Implements state_t.get_state
*/ */
static status_t destroy(private_initiator_init_t *this) static void destroy(private_initiator_init_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
@@ -535,15 +393,13 @@ static status_t destroy(private_initiator_init_t *this)
this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce"); this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
allocator_free(this->sent_nonce.ptr); allocator_free(this->sent_nonce.ptr);
} }
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/** /**
* Implements private_initiator_init_t.destroy_after_state_change * Implements private_initiator_init_t.destroy_after_state_change
*/ */
static status_t destroy_after_state_change (private_initiator_init_t *this) static void destroy_after_state_change (private_initiator_init_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
@@ -557,7 +413,6 @@ static status_t destroy_after_state_change (private_initiator_init_t *this)
} }
this->proposals->destroy(this->proposals); this->proposals->destroy(this->proposals);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -566,16 +421,11 @@ static status_t destroy_after_state_change (private_initiator_init_t *this)
initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa) initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
{ {
private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t); private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* public functions */ /* public functions */
this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection; this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection;
@@ -593,11 +443,6 @@ initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
this->logger = this->ike_sa->get_logger(this->ike_sa); this->logger = this->ike_sa->get_logger(this->ike_sa);
this->proposals = linked_list_create(); this->proposals = linked_list_create();
this->sent_nonce = CHUNK_INITIALIZER; this->sent_nonce = CHUNK_INITIALIZER;
if (this->proposals == NULL)
{
allocator_free(this);
return NULL;
}
return &(this->public); return &(this->public);
} }
+53 -256
View File
@@ -1,7 +1,7 @@
/** /**
* @file responder_init.c * @file responder_init.c
* *
* @brief Start state of a IKE_SA as responder * @brief Implementation of responder_init_t.
* *
*/ */
@@ -99,11 +99,8 @@ struct private_responder_init_t {
* @param this calling object * @param this calling object
* @param payload The generated SA payload object of type ke_payload_t is * @param payload The generated SA payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_sa_payload) (private_responder_init_t *this, payload_t **payload); void (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
/** /**
* Builds the KE payload for this state. * Builds the KE payload for this state.
@@ -111,22 +108,17 @@ struct private_responder_init_t {
* @param this calling object * @param this calling object
* @param payload The generated KE payload object of type ke_payload_t is * @param payload The generated KE payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_ke_payload) (private_responder_init_t *this, payload_t **payload); void (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
/** /**
* Builds the NONCE payload for this state. * Builds the NONCE payload for this state.
* *
* @param this calling object * @param this calling object
* @param payload The generated NONCE payload object of type ke_payload_t is * @param payload The generated NONCE payload object of type ke_payload_t is
* stored at this location. * stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload); void (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);
/** /**
* Destroy function called internally of this class after state change succeeded. * Destroy function called internally of this class after state change succeeded.
@@ -134,9 +126,8 @@ struct private_responder_init_t {
* This destroy function does not destroy objects which were passed to the new state. * This destroy function does not destroy objects which were passed to the new state.
* *
* @param this calling object * @param this calling object
* @return SUCCESS in any case
*/ */
status_t (*destroy_after_state_change) (private_responder_init_t *this); void (*destroy_after_state_change) (private_responder_init_t *this);
}; };
/** /**
@@ -175,20 +166,8 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
message->get_destination(message, &destination); message->get_destination(message, &destination);
/* we need to clone them, since we destroy the message later */ /* we need to clone them, since we destroy the message later */
status = destination->clone(destination, &my_host); my_host = destination->clone(destination);
if (status != SUCCESS) other_host = source->clone(source);
{
this->logger->log(this->logger, ERROR, "Fatal error: could not clone my host informations");
return status;
}
status = source->clone(source, &other_host);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not clone other host informations");
my_host->destroy(my_host);
return status;
}
this->ike_sa->set_my_host(this->ike_sa, my_host); this->ike_sa->set_my_host(this->ike_sa, my_host);
this->ike_sa->set_other_host(this->ike_sa, other_host); this->ike_sa->set_other_host(this->ike_sa, other_host);
@@ -198,7 +177,7 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
if (status != SUCCESS) if (status != SUCCESS)
{ {
this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message"); this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
return status; return status;
} }
/* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */ /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
@@ -219,24 +198,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
sa_payload_t *sa_payload = (sa_payload_t*)payload; sa_payload_t *sa_payload = (sa_payload_t*)payload;
iterator_t *suggested_proposals, *accepted_proposals; iterator_t *suggested_proposals, *accepted_proposals;
proposal_substructure_t *accepted_proposal; proposal_substructure_t *accepted_proposal;
status = this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE); this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
payloads->destroy(payloads);
return status;
}
/* get the list of suggested proposals */ /* get the list of suggested proposals */
status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE); sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on suggested proposals");
accepted_proposals->destroy(accepted_proposals);
payloads->destroy(payloads);
return status;
}
/* now let the configuration-manager select a subset of the proposals */ /* now let the configuration-manager select a subset of the proposals */
status = global_configuration_manager->select_proposals_for_host(global_configuration_manager, status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
@@ -253,7 +219,6 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
/* iterators are not needed anymore */ /* iterators are not needed anymore */
suggested_proposals->destroy(suggested_proposals); suggested_proposals->destroy(suggested_proposals);
/* let the ike_sa create their own transforms from proposal informations */ /* let the ike_sa create their own transforms from proposal informations */
accepted_proposals->reset(accepted_proposals); accepted_proposals->reset(accepted_proposals);
/* TODO check for true*/ /* TODO check for true*/
@@ -309,19 +274,12 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
{ {
this->logger->log(this->logger, ERROR, "Could not generate DH object"); this->logger->log(this->logger, ERROR, "Could not generate DH object");
payloads->destroy(payloads); payloads->destroy(payloads);
return OUT_OF_RES; return NOT_SUPPORTED;
} }
this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value"); this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
status = dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload)); dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not set other DH public value");
dh->destroy(dh);
payloads->destroy(payloads);
return OUT_OF_RES;
}
this->diffie_hellman = dh; this->diffie_hellman = dh;
@@ -332,22 +290,11 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
{ {
nonce_payload_t *nonce_payload = (nonce_payload_t*)payload; nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
if (this->received_nonce.ptr != NULL) allocator_free(this->received_nonce.ptr);
{ this->received_nonce = CHUNK_INITIALIZER;
this->logger->log(this->logger, CONTROL | MOST, "Destroy stored received nonce");
allocator_free(this->received_nonce.ptr);
this->received_nonce.ptr = NULL;
this->received_nonce.len = 0;
}
this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it"); this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce)); nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not get nonce");
payloads->destroy(payloads);
return OUT_OF_RES;
}
this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed"); this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
break; break;
@@ -356,11 +303,9 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
{ {
this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!"); this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
payloads->destroy(payloads); payloads->destroy(payloads);
return OUT_OF_RES; return NOT_SUPPORTED;
} }
} }
} }
/* iterator can be destroyed */ /* iterator can be destroyed */
payloads->destroy(payloads); payloads->destroy(payloads);
@@ -371,63 +316,31 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
randomizer = this->ike_sa->get_randomizer(this->ike_sa); randomizer = this->ike_sa->get_randomizer(this->ike_sa);
if (randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS) randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
{
this->logger->log(this->logger, ERROR, "Could not create nonce!");
return OUT_OF_RES;
}
/* store shared secret */ /* store shared secret */
this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it"); this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret); status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret);
this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret); this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret);
status = this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce); this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
if (status != SUCCESS)
{
/* secrets could not be computed */
this->logger->log(this->logger, ERROR | MORE, "Secrets could not be computed!");
return status;
}
/* set up the reply */ /* set up the reply */
status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response); this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not create empty message");
return status;
}
/* build SA payload */ /* build SA payload */
status = this->build_sa_payload(this, &payload); this->build_sa_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build SA payload");
return status;
}
this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message"); this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
response->add_payload(response, payload); response->add_payload(response, payload);
/* build KE payload */ /* build KE payload */
status = this->build_ke_payload(this,&payload); this->build_ke_payload(this,&payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build KE payload");
return status;
}
this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message"); this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
response->add_payload(response, payload); response->add_payload(response, payload);
/* build Nonce payload */ /* build Nonce payload */
status = this->build_nonce_payload(this, &payload); this->build_nonce_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
return status;
}
this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message"); this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
response->add_payload(response, payload); response->add_payload(response, payload);
@@ -436,30 +349,17 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
status = response->generate(response, NULL, NULL, &packet); status = response->generate(response, NULL, NULL, &packet);
if (status != SUCCESS) if (status != SUCCESS)
{ {
this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message"); this->logger->log(this->logger, ERROR, "could not generate packet from message");
return status; return status;
} }
this ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue"); this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
status = global_send_queue->add(global_send_queue, packet); global_send_queue->add(global_send_queue, packet);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
packet->destroy(packet);
return status;
}
/* state can now be changed */ /* state can now be changed */
this ->logger->log(this->logger, CONTROL|MOST, "Create next state object"); this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce); next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce);
if (next_state == NULL)
{
this ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_responded_t");
allocator_free_chunk(&shared_secret);
return FAILED;
}
/* last message can now be set */ /* last message can now be set */
status = this->ike_sa->set_last_responded_message(this->ike_sa, response); status = this->ike_sa->set_last_responded_message(this->ike_sa, response);
@@ -475,9 +375,9 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
/* state can now be changed */ /* state can now be changed */
this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state); this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state);
/* state has NOW changed :-) */ /* state has NOW changed :-) */
this ->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) ); this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) );
this ->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object"); this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
this->destroy_after_state_change(this); this->destroy_after_state_change(this);
return SUCCESS; return SUCCESS;
@@ -486,142 +386,67 @@ static status_t process_message(private_responder_init_t *this, message_t *messa
/** /**
* implements private_initiator_init_t.build_sa_payload * implements private_initiator_init_t.build_sa_payload
*/ */
static status_t build_sa_payload(private_responder_init_t *this, payload_t **payload) static void build_sa_payload(private_responder_init_t *this, payload_t **payload)
{ {
sa_payload_t* sa_payload; sa_payload_t* sa_payload;
iterator_t *proposal_iterator; iterator_t *proposal_iterator;
status_t status;
/* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */ /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
this->logger->log(this->logger, CONTROL|MORE, "building sa payload"); this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE); this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
return status;
}
sa_payload = sa_payload_create(); sa_payload = sa_payload_create();
if (sa_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
return OUT_OF_RES;
}
while (proposal_iterator->has_next(proposal_iterator)) while (proposal_iterator->has_next(proposal_iterator))
{ {
proposal_substructure_t *current_proposal; proposal_substructure_t *current_proposal;
proposal_substructure_t *current_proposal_clone; proposal_substructure_t *current_proposal_clone;
status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = current_proposal->clone(current_proposal,&current_proposal_clone);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not clone current proposal");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone); proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
if (status != SUCCESS) current_proposal->clone(current_proposal,&current_proposal_clone);
{ sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
} }
proposal_iterator->destroy(proposal_iterator); proposal_iterator->destroy(proposal_iterator);
this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
*payload = (payload_t *) sa_payload; *payload = (payload_t *) sa_payload;
return SUCCESS;
} }
/** /**
* implements private_initiator_init_t.build_ke_payload * implements private_initiator_init_t.build_ke_payload
*/ */
static status_t build_ke_payload(private_responder_init_t *this, payload_t **payload) static void build_ke_payload(private_responder_init_t *this, payload_t **payload)
{ {
ke_payload_t *ke_payload; ke_payload_t *ke_payload;
chunk_t key_data; chunk_t key_data;
status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building ke payload"); this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get my DH public value");
return status;
}
ke_payload = ke_payload_create(); ke_payload = ke_payload_create();
if (ke_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Could not create KE payload");
allocator_free_chunk(&key_data);
return OUT_OF_RES;
}
ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT); ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
ke_payload->destroy(ke_payload);
allocator_free_chunk(&key_data);
return OUT_OF_RES;
}
allocator_free_chunk(&key_data);
allocator_free_chunk(&key_data);
*payload = (payload_t *) ke_payload; *payload = (payload_t *) ke_payload;
return SUCCESS;
} }
/** /**
* implements private_initiator_init_t.build_nonce_payload * implements private_initiator_init_t.build_nonce_payload
*/ */
static status_t build_nonce_payload(private_responder_init_t *this, payload_t **payload) static void build_nonce_payload(private_responder_init_t *this, payload_t **payload)
{ {
nonce_payload_t *nonce_payload; nonce_payload_t *nonce_payload;
status_t status; status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building nonce payload"); this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
nonce_payload = nonce_payload_create(); nonce_payload = nonce_payload_create();
if (nonce_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
return OUT_OF_RES;
}
status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce); status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
nonce_payload->destroy(nonce_payload);
return status;
}
*payload = (payload_t *) nonce_payload; *payload = (payload_t *) nonce_payload;
return SUCCESS;
} }
@@ -636,7 +461,7 @@ static ike_sa_state_t get_state(private_responder_init_t *this)
/** /**
* Implements state_t.get_state * Implements state_t.get_state
*/ */
static status_t destroy(private_responder_init_t *this) static void destroy(private_responder_init_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object");
@@ -650,35 +475,20 @@ static status_t destroy(private_responder_init_t *this)
} }
this->proposals->destroy(this->proposals); this->proposals->destroy(this->proposals);
if (this->sent_nonce.ptr != NULL) allocator_free(this->sent_nonce.ptr);
{ allocator_free(this->received_nonce.ptr);
this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
allocator_free(this->sent_nonce.ptr);
}
if (this->received_nonce.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
allocator_free(this->received_nonce.ptr);
}
/* destroy diffie hellman object */
if (this->diffie_hellman != NULL) if (this->diffie_hellman != NULL)
{ {
this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
this->diffie_hellman->destroy(this->diffie_hellman); this->diffie_hellman->destroy(this->diffie_hellman);
} }
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/** /**
* Implements private_responder_init_t.destroy_after_state_change * Implements private_responder_init_t.destroy_after_state_change
*/ */
static status_t destroy_after_state_change (private_responder_init_t *this) static void destroy_after_state_change (private_responder_init_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object"); this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object");
@@ -700,7 +510,6 @@ static status_t destroy_after_state_change (private_responder_init_t *this)
} }
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -709,16 +518,11 @@ static status_t destroy_after_state_change (private_responder_init_t *this)
responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa) responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
{ {
private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t); private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */ /* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message; this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state; this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy; this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
/* private functions */ /* private functions */
this->build_sa_payload = build_sa_payload; this->build_sa_payload = build_sa_payload;
@@ -729,16 +533,9 @@ responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
/* private data */ /* private data */
this->ike_sa = ike_sa; this->ike_sa = ike_sa;
this->logger = this->ike_sa->get_logger(this->ike_sa); this->logger = this->ike_sa->get_logger(this->ike_sa);
this->sent_nonce.ptr = NULL; this->sent_nonce = CHUNK_INITIALIZER;
this->sent_nonce.len = 0; this->received_nonce = CHUNK_INITIALIZER;
this->received_nonce.ptr = NULL;
this->received_nonce.len = 0;
this->proposals = linked_list_create(); this->proposals = linked_list_create();
if (this->proposals == NULL)
{
allocator_free(this);
return NULL;
}
return &(this->public); return &(this->public);
} }
+5 -3
View File
@@ -1,7 +1,7 @@
/** /**
* @file responder_init.h * @file responder_init.h
* *
* @brief Start state of a IKE_SA as responder * @brief Interface of responder_init_t.
* *
*/ */
@@ -31,7 +31,7 @@ typedef struct responder_init_t responder_init_t;
/** /**
* @brief This class represents an IKE_SA state when initializing. * @brief This class represents an IKE_SA state when initializing.
* a connection as responder * a connection as responder.
* *
*/ */
struct responder_init_t { struct responder_init_t {
@@ -45,7 +45,9 @@ struct responder_init_t {
/** /**
* Constructor of class responder_init_t * Constructor of class responder_init_t
* *
* @param ike_sa assigned IKE_SA * @param ike_sa assigned IKE_SA
*
* @return responder_init state
*/ */
responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa); responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa);
+6 -7
View File
@@ -1,7 +1,7 @@
/** /**
* @file state.h * @file state.h
* *
* @brief Interface for a specific IKE_SA state * @brief Interface for a specific IKE_SA state.
* *
*/ */
@@ -83,7 +83,7 @@ typedef struct state_t state_t;
struct state_t { struct state_t {
/** /**
* @brief Processes a incoming IKEv2-Message of type message_t * @brief Processes a incoming IKEv2-Message of type message_t.
* *
* @param this state_t object * @param this state_t object
* @param[in] message message_t object to process * @param[in] message message_t object to process
@@ -94,7 +94,7 @@ struct state_t {
status_t (*process_message) (state_t *this,message_t *message); status_t (*process_message) (state_t *this,message_t *message);
/** /**
* @brief Get the current state * @brief Get the current state.
* *
* @param this state_t object * @param this state_t object
* @return state * @return state
@@ -102,12 +102,11 @@ struct state_t {
ike_sa_state_t (*get_state) (state_t *this); ike_sa_state_t (*get_state) (state_t *this);
/** /**
* @brief Destroys a state_t object * @brief Destroys a state_t object.
* *
* @param this state_t object * @param this state_t object to destroy
* @return SUCCESS in any case
*/ */
status_t (*destroy) (state_t *this); void (*destroy) (state_t *this);
}; };
@@ -77,8 +77,7 @@ void test_aes_cbc_crypter(tester_t *tester)
allocator_free_chunk(&decrypted1); allocator_free_chunk(&decrypted1);
tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test"); crypter->destroy(crypter);
/* /*
@@ -133,7 +132,7 @@ void test_aes_cbc_crypter(tester_t *tester)
allocator_free_chunk(&decrypted2); allocator_free_chunk(&decrypted2);
tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test"); crypter->destroy(crypter);
/* /*
* Test 3 of RFC3603 * Test 3 of RFC3603
@@ -197,10 +196,8 @@ void test_aes_cbc_crypter(tester_t *tester)
logger->log_chunk(logger,RAW,"decrypted :", &decrypted3); logger->log_chunk(logger,RAW,"decrypted :", &decrypted3);
allocator_free_chunk(&decrypted3); allocator_free_chunk(&decrypted3);
tester->assert_true(tester, (crypter->destroy(crypter) == SUCCESS), "destroy call test");
crypter->destroy(crypter);
global_logger_manager->destroy_logger(global_logger_manager,logger); global_logger_manager->destroy_logger(global_logger_manager,logger);
} }
@@ -54,8 +54,8 @@ void test_diffie_hellman(tester_t *tester)
tester->assert_true(tester,( other_diffie_hellman->get_my_public_value(other_diffie_hellman,&other_public_value) == SUCCESS), "get_my_public_value call check"); tester->assert_true(tester,( other_diffie_hellman->get_my_public_value(other_diffie_hellman,&other_public_value) == SUCCESS), "get_my_public_value call check");
logger->log_chunk(logger,RAW,"Other public value",&other_public_value); logger->log_chunk(logger,RAW,"Other public value",&other_public_value);
tester->assert_true(tester,( my_diffie_hellman->set_other_public_value(my_diffie_hellman,other_public_value) == SUCCESS), "set_other_public_value call check"); my_diffie_hellman->set_other_public_value(my_diffie_hellman,other_public_value);
tester->assert_true(tester,( other_diffie_hellman->set_other_public_value(other_diffie_hellman,my_public_value) == SUCCESS), "set_other_public_value call check"); other_diffie_hellman->set_other_public_value(other_diffie_hellman,my_public_value);
allocator_free(my_public_value.ptr); allocator_free(my_public_value.ptr);
allocator_free(other_public_value.ptr); allocator_free(other_public_value.ptr);
@@ -71,7 +71,7 @@ void test_diffie_hellman(tester_t *tester)
allocator_free(my_secret.ptr); allocator_free(my_secret.ptr);
allocator_free(other_secret.ptr); allocator_free(other_secret.ptr);
tester->assert_true(tester,(my_diffie_hellman->destroy(my_diffie_hellman) == SUCCESS), "destroy call check"); my_diffie_hellman->destroy(my_diffie_hellman);
tester->assert_true(tester,(other_diffie_hellman->destroy(other_diffie_hellman) == SUCCESS), "destroy call check"); other_diffie_hellman->destroy(other_diffie_hellman);
global_logger_manager->destroy_logger(global_logger_manager,logger); global_logger_manager->destroy_logger(global_logger_manager,logger);
} }
+2 -2
View File
@@ -90,7 +90,7 @@ void test_md5_hasher(tester_t *tester)
tester->assert_true(tester, hash_chunk.len == 16, "hash len"); tester->assert_true(tester, hash_chunk.len == 16, "hash len");
tester->assert_false(tester, memcmp(hash_chunk.ptr, hash_abcd, hash_chunk.len), "hash for abcd..."); tester->assert_false(tester, memcmp(hash_chunk.ptr, hash_abcd, hash_chunk.len), "hash for abcd...");
allocator_free(hash_chunk.ptr); allocator_free(hash_chunk.ptr);
tester->assert_true(tester, (hasher->destroy(hasher) == SUCCESS), "destroy call test"); hasher->destroy(hasher);
} }
/* /*
@@ -168,5 +168,5 @@ void test_sha1_hasher(tester_t *tester)
tester->assert_false(tester, memcmp(hash_buffer, hash_aaa, 20), "hash for aaa..."); tester->assert_false(tester, memcmp(hash_buffer, hash_aaa, 20), "hash for aaa...");
tester->assert_true(tester, (hasher->destroy(hasher) == SUCCESS), "destroy call test"); hasher->destroy(hasher);
} }
+2 -6
View File
@@ -108,8 +108,7 @@ void test_hmac_md5_signer(tester_t *tester)
tester->assert_true(tester, (signer->destroy(signer) == SUCCESS), "signer destroy call check"); signer->destroy(signer);
global_logger_manager->destroy_logger(global_logger_manager,logger); global_logger_manager->destroy_logger(global_logger_manager,logger);
} }
@@ -205,10 +204,7 @@ void test_hmac_sha1_signer(tester_t *tester)
tester->assert_true(tester, (valid == FALSE), "Signature not valid check"); tester->assert_true(tester, (valid == FALSE), "Signature not valid check");
} }
signer->destroy(signer);
tester->assert_true(tester, (signer->destroy(signer) == SUCCESS), "signer destroy call check");
global_logger_manager->destroy_logger(global_logger_manager,logger); global_logger_manager->destroy_logger(global_logger_manager,logger);
} }
+7 -7
View File
@@ -83,11 +83,11 @@ void test_ike_sa_id(tester_t *tester)
/* check destroy functionality */ /* check destroy functionality */
tester->assert_true(tester,(ike_sa_id->destroy(ike_sa_id) == SUCCESS), "destroy call check"); ike_sa_id->destroy(ike_sa_id);
tester->assert_true(tester,(equal->destroy(equal) == SUCCESS), "destroy call check"); equal->destroy(equal);
tester->assert_true(tester,(clone->destroy(clone) == SUCCESS), "destroy call check"); clone->destroy(clone);
tester->assert_true(tester,(other1->destroy(other1) == SUCCESS), "destroy call check"); other1->destroy(other1);
tester->assert_true(tester,(other2->destroy(other2) == SUCCESS), "destroy call check"); other2->destroy(other2);
tester->assert_true(tester,(other3->destroy(other3) == SUCCESS), "destroy call check"); other3->destroy(other3);
tester->assert_true(tester,(other4->destroy(other4) == SUCCESS), "destroy call check"); other4->destroy(other4);
} }
@@ -95,8 +95,7 @@ void test_ike_sa_manager(tester_t *tester)
* *
*/ */
status = td.isam->create_and_checkout(td.isam, &ike_sa); td.isam->create_and_checkout(td.isam, &ike_sa);
tester->assert_true(tester, (status == SUCCESS), "checkout unexisting IKE_SA");
/* for testing purposes, we manipulate the responder spi. /* for testing purposes, we manipulate the responder spi.
* this is usually done be the response from the communication partner, * this is usually done be the response from the communication partner,
* but we don't have one... * but we don't have one...
@@ -212,8 +211,7 @@ void test_ike_sa_manager(tester_t *tester)
/* let them go acquiring */ /* let them go acquiring */
sleep(1); sleep(1);
status = td.isam->destroy(td.isam); td.isam->destroy(td.isam);
tester->assert_true(tester, (status == SUCCESS), "ike_sa_manager destruction");
for (i = 0; i < thread_count; i++) for (i = 0; i < thread_count; i++)
{ {
+7 -9
View File
@@ -84,7 +84,7 @@ void test_linked_list(tester_t *tester)
tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check"); tester->assert_true(tester,(strcmp((char *) test_value,"four") == 0), "get_first value check");
tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check"); tester->assert_true(tester,(linked_list->get_count(linked_list) == 3), "count check");
tester->assert_true(tester,(linked_list->destroy(linked_list) == SUCCESS), "destroy call check"); linked_list->destroy(linked_list);
} }
/* /*
@@ -105,7 +105,7 @@ void test_linked_list_iterator(tester_t *tester)
iterator_t * iterator2; iterator_t * iterator2;
tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator,TRUE) == SUCCESS), "create_iterator for it 1 call check"); linked_list->create_iterator(linked_list,&iterator,TRUE);
tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check"); tester->assert_true(tester,iterator->has_next(iterator), "it 1 has_next value check");
iterator->current(iterator,&value); iterator->current(iterator,&value);
@@ -115,7 +115,7 @@ void test_linked_list_iterator(tester_t *tester)
iterator->current(iterator,&value); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"four") == 0), "it 1 current value check"); tester->assert_true(tester,(strcmp((char *) value,"four") == 0), "it 1 current value check");
tester->assert_true(tester,(linked_list->create_iterator(linked_list,&iterator2,FALSE) == SUCCESS), "create_iterator for it 2 call check"); linked_list->create_iterator(linked_list,&iterator2,FALSE);
tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check"); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
iterator2->current(iterator2,&value); iterator2->current(iterator2,&value);
@@ -147,10 +147,8 @@ void test_linked_list_iterator(tester_t *tester)
tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check"); tester->assert_true(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_false(tester,iterator2->has_next(iterator2), "it 2 has_next value check"); tester->assert_false(tester,iterator2->has_next(iterator2), "it 2 has_next value check");
tester->assert_true(tester,(iterator->destroy(iterator) == SUCCESS), "it 1 destroy call check"); iterator->destroy(iterator);
iterator2->destroy(iterator2);
tester->assert_true(tester,(iterator2->destroy(iterator2) == SUCCESS), "it 2 destroy call check");
linked_list->destroy(linked_list); linked_list->destroy(linked_list);
} }
@@ -180,12 +178,12 @@ void test_linked_list_insert_and_remove(tester_t *tester)
iterator->current(iterator,&value); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
tester->assert_true(tester,(iterator->insert_before(iterator,"before_three") == SUCCESS), "insert_before call check"); iterator->insert_before(iterator,"before_three");
iterator->current(iterator,&value); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
tester->assert_true(tester,(iterator->insert_after(iterator,"after_three") == SUCCESS), "insert_after call check"); iterator->insert_after(iterator,"after_three");
iterator->current(iterator,&value); iterator->current(iterator,&value);
tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check"); tester->assert_true(tester,(strcmp((char *) value,"three") == 0), "current value check");
-2
View File
@@ -179,8 +179,6 @@ void test_parser_with_sa_payload(tester_t *tester)
} }
proposals->destroy(proposals); proposals->destroy(proposals);
sa_payload->destroy(sa_payload); sa_payload->destroy(sa_payload);
} }
+1 -1
View File
@@ -83,5 +83,5 @@ void test_receiver(tester_t *tester)
job->destroy(job); job->destroy(job);
} }
tester->assert_true(tester, (receiver->destroy(receiver) == SUCCESS), "destroy call check"); receiver->destroy(receiver);
} }
+1 -1
View File
@@ -88,5 +88,5 @@ void test_scheduler(tester_t *tester)
} }
/* destruction test */ /* destruction test */
tester->assert_true(tester, (scheduler->destroy(scheduler) == SUCCESS), "destroy call check"); scheduler->destroy(scheduler);
} }
+1 -1
View File
@@ -138,5 +138,5 @@ void test_send_queue(tester_t *tester)
/* the send-queue has to have diserd_value count entries*/ /* the send-queue has to have diserd_value count entries*/
tester->assert_true(tester,(send_queue->get_count(send_queue) == desired_value), "count value check"); tester->assert_true(tester,(send_queue->get_count(send_queue) == desired_value), "count value check");
tester->assert_true(tester,(send_queue->destroy(send_queue) == SUCCESS), "destroy call check"); send_queue->destroy(send_queue);
} }
+1 -1
View File
@@ -73,5 +73,5 @@ void test_sender(tester_t *tester)
received_packet->destroy(received_packet); received_packet->destroy(received_packet);
} }
tester->assert_true(tester, (sender->destroy(sender) == SUCCESS), "destroy call check"); sender->destroy(sender);
} }
+1 -1
View File
@@ -37,5 +37,5 @@ void test_thread_pool(tester_t *tester)
thread_pool_t *pool = thread_pool_create(desired_pool_size); thread_pool_t *pool = thread_pool_create(desired_pool_size);
pool_size = pool->get_pool_size(pool); pool_size = pool->get_pool_size(pool);
tester->assert_true(tester, (desired_pool_size == pool_size), "thread creation"); tester->assert_true(tester, (desired_pool_size == pool_size), "thread creation");
tester->assert_true(tester, (pool->destroy(pool) == SUCCESS), "threadpool destruction"); pool->destroy(pool);
} }
+3 -17
View File
@@ -61,8 +61,6 @@ struct private_receiver_t {
* logger for the receiver * logger for the receiver
*/ */
logger_t *logger; logger_t *logger;
}; };
/** /**
@@ -84,15 +82,8 @@ static void receive_packets(private_receiver_t * this)
{ {
this->logger->log(this->logger, CONTROL, "creating job from packet"); this->logger->log(this->logger, CONTROL, "creating job from packet");
current_job = (job_t *) incoming_packet_job_create(current_packet); current_job = (job_t *) incoming_packet_job_create(current_packet);
if (current_job == NULL)
{
this->logger->log(this->logger, ERROR, "job creation failed");
}
if (global_job_queue->add(global_job_queue,current_job) != SUCCESS) global_job_queue->add(global_job_queue,current_job);
{
this->logger->log(this->logger, ERROR, "job queueing failed");
}
} }
/* bad bad, rebuild the socket ? */ /* bad bad, rebuild the socket ? */
@@ -103,7 +94,7 @@ static void receive_packets(private_receiver_t * this)
/** /**
* Implementation of receiver_t's destroy function * Implementation of receiver_t's destroy function
*/ */
static status_t destroy(private_receiver_t *this) static void destroy(private_receiver_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to terminate receiver thread"); this->logger->log(this->logger, CONTROL | MORE, "Going to terminate receiver thread");
pthread_cancel(this->assigned_thread); pthread_cancel(this->assigned_thread);
@@ -114,7 +105,6 @@ static status_t destroy(private_receiver_t *this)
global_logger_manager->destroy_logger(global_logger_manager, this->logger); global_logger_manager->destroy_logger(global_logger_manager, this->logger);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -124,14 +114,10 @@ receiver_t * receiver_create()
{ {
private_receiver_t *this = allocator_alloc_thing(private_receiver_t); private_receiver_t *this = allocator_alloc_thing(private_receiver_t);
this->public.destroy = (status_t(*)(receiver_t*)) destroy; this->public.destroy = (void(*)(receiver_t*)) destroy;
this->receive_packets = receive_packets; this->receive_packets = receive_packets;
this->logger = global_logger_manager->create_logger(global_logger_manager, RECEIVER, NULL); this->logger = global_logger_manager->create_logger(global_logger_manager, RECEIVER, NULL);
if (this->logger == NULL)
{
allocator_free(this);
}
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0) if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->receive_packets, this) != 0)
{ {
+1 -3
View File
@@ -42,10 +42,8 @@ struct receiver_t {
* @brief Destroys a receiver_t * @brief Destroys a receiver_t
* *
* @param receiver receiver object * @param receiver receiver object
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (receiver_t *receiver); void (*destroy) (receiver_t *receiver);
}; };
/** /**
+13 -21
View File
@@ -43,7 +43,6 @@ struct private_scheduler_t {
*/ */
scheduler_t public; scheduler_t public;
/** /**
* @brief Get events from the event queue and add them to to job queue. * @brief Get events from the event queue and add them to to job queue.
* *
@@ -53,16 +52,15 @@ struct private_scheduler_t {
*/ */
void (*get_events) (private_scheduler_t *this); void (*get_events) (private_scheduler_t *this);
/** /**
* Assigned thread to the scheduler_t object * Assigned thread to the scheduler_t object
*/ */
pthread_t assigned_thread; pthread_t assigned_thread;
/** /**
* logger for this scheduler * logger for this scheduler
*/ */
logger_t *logger; logger_t *logger;
}; };
/** /**
@@ -81,7 +79,7 @@ static void get_events(private_scheduler_t * this)
{ {
this->logger->log(this->logger, CONTROL|MORE, "waiting for next event..."); this->logger->log(this->logger, CONTROL|MORE, "waiting for next event...");
/* get a job, this block until one is available */ /* get a job, this block until one is available */
global_event_queue->get(global_event_queue, &current_job); current_job = global_event_queue->get(global_event_queue);
/* queue the job in the job queue, workers will eat them */ /* queue the job in the job queue, workers will eat them */
global_job_queue->add(global_job_queue, current_job); global_job_queue->add(global_job_queue, current_job);
this->logger->log(this->logger, CONTROL, "got event, added job %s to job-queue.", this->logger->log(this->logger, CONTROL, "got event, added job %s to job-queue.",
@@ -92,7 +90,7 @@ static void get_events(private_scheduler_t * this)
/** /**
* Implementation of scheduler_t's destroy function * Implementation of scheduler_t's destroy function
*/ */
static status_t destroy(private_scheduler_t *this) static void destroy(private_scheduler_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to terminate scheduler thread"); this->logger->log(this->logger, CONTROL | MORE, "Going to terminate scheduler thread");
pthread_cancel(this->assigned_thread); pthread_cancel(this->assigned_thread);
@@ -103,7 +101,6 @@ static status_t destroy(private_scheduler_t *this)
global_logger_manager->destroy_logger(global_logger_manager, this->logger); global_logger_manager->destroy_logger(global_logger_manager, this->logger);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
@@ -111,20 +108,15 @@ scheduler_t * scheduler_create()
{ {
private_scheduler_t *this = allocator_alloc_thing(private_scheduler_t); private_scheduler_t *this = allocator_alloc_thing(private_scheduler_t);
this->public.destroy = (status_t(*)(scheduler_t*)) destroy; this->public.destroy = (void(*)(scheduler_t*)) destroy;
this->get_events = get_events; this->get_events = get_events;
this->logger = global_logger_manager->create_logger(global_logger_manager, SCHEDULER, NULL); this->logger = global_logger_manager->create_logger(global_logger_manager, SCHEDULER, NULL);
if (this->logger == NULL)
{
allocator_free(this);
return NULL;
}
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->get_events, this) != 0) if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->get_events, this) != 0)
{ {
/* thread could not be created */ /* thread could not be created */
this->logger->log(this->logger, ERROR, "Scheduler thread could not be created!"); this->logger->log(this->logger, ERROR, "Scheduler thread could not be created!");
global_logger_manager->destroy_logger(global_logger_manager, this->logger); global_logger_manager->destroy_logger(global_logger_manager, this->logger);
allocator_free(this); allocator_free(this);
return NULL; return NULL;
+3 -5
View File
@@ -41,10 +41,8 @@ struct scheduler_t {
* @brief Destroys a scheduler object. * @brief Destroys a scheduler object.
* *
* @param scheduler scheduler object * @param scheduler scheduler object
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (scheduler_t *scheduler); void (*destroy) (scheduler_t *scheduler);
}; };
/** /**
@@ -54,8 +52,8 @@ struct scheduler_t {
* and adds them to the job queue. * and adds them to the job queue.
* *
* @return * @return
* - the created scheduler_t instance, or * - the created scheduler_t instance, or
* - NULL if thread could not be started * - NULL if thread could not be started
* *
* @ingroup threads * @ingroup threads
*/ */
+10 -18
View File
@@ -78,24 +78,22 @@ static void send_packets(private_sender_t * this)
while (1) while (1)
{ {
while (global_send_queue->get(global_send_queue,&current_packet) == SUCCESS) current_packet = global_send_queue->get(global_send_queue);
this->logger->log(this->logger, CONTROL|MORE, "got a packet, sending it");
status = global_socket->send(global_socket,current_packet);
if (status != SUCCESS)
{ {
this->logger->log(this->logger, CONTROL|MORE, "got a packet, sending it"); this->logger->log(this->logger, ERROR, "sending failed, socket returned %s",
status = global_socket->send(global_socket,current_packet); mapping_find(status_m, status));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "sending failed, socket returned %s",
mapping_find(status_m, status));
}
current_packet->destroy(current_packet);
} }
current_packet->destroy(current_packet);
} }
} }
/** /**
* implements sender_t.destroy * implements sender_t.destroy
*/ */
static status_t destroy(private_sender_t *this) static void destroy(private_sender_t *this)
{ {
this->logger->log(this->logger, CONTROL | MORE, "Going to terminate sender thread"); this->logger->log(this->logger, CONTROL | MORE, "Going to terminate sender thread");
pthread_cancel(this->assigned_thread); pthread_cancel(this->assigned_thread);
@@ -106,7 +104,6 @@ static status_t destroy(private_sender_t *this)
global_logger_manager->destroy_logger(global_logger_manager, this->logger); global_logger_manager->destroy_logger(global_logger_manager, this->logger);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -117,15 +114,10 @@ sender_t * sender_create()
private_sender_t *this = allocator_alloc_thing(private_sender_t); private_sender_t *this = allocator_alloc_thing(private_sender_t);
this->send_packets = send_packets; this->send_packets = send_packets;
this->public.destroy = (status_t(*)(sender_t*)) destroy; this->public.destroy = (void(*)(sender_t*)) destroy;
this->logger = global_logger_manager->create_logger(global_logger_manager, SENDER, NULL); this->logger = global_logger_manager->create_logger(global_logger_manager, SENDER, NULL);
if (this->logger == NULL)
{
allocator_free(this);
return NULL;
}
if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0) if (pthread_create(&(this->assigned_thread), NULL, (void*(*)(void*))this->send_packets, this) != 0)
{ {
this->logger->log(this->logger, ERROR, "Sender thread could not be created"); this->logger->log(this->logger, ERROR, "Sender thread could not be created");
+1 -3
View File
@@ -38,10 +38,8 @@ struct sender_t {
* @brief Destroys a sender object * @brief Destroys a sender object
* *
* @param sender sender object * @param sender sender object
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (sender_t *sender); void (*destroy) (sender_t *sender);
}; };
+20 -48
View File
@@ -81,15 +81,18 @@ struct private_thread_pool_t {
/** /**
* number of running threads * number of running threads
*/ */
size_t pool_size; size_t pool_size;
/** /**
* array of thread ids * array of thread ids
*/ */
pthread_t *threads; pthread_t *threads;
/** /**
* logger of the threadpool * logger of the threadpool
*/ */
logger_t *pool_logger; logger_t *pool_logger;
/** /**
* logger of the worker threads * logger of the worker threads
*/ */
@@ -112,7 +115,7 @@ static void process_jobs(private_thread_pool_t *this)
job_t *job; job_t *job;
job_type_t job_type; job_type_t job_type;
global_job_queue->get(global_job_queue, &job); job = global_job_queue->get(global_job_queue);
job_type = job->get_type(job); job_type = job->get_type(job);
this->worker_logger->log(this->worker_logger, CONTROL|MORE, "got a job of type %s", this->worker_logger->log(this->worker_logger, CONTROL|MORE, "got a job of type %s",
mapping_find(job_type_m,job_type)); mapping_find(job_type_m,job_type));
@@ -148,15 +151,14 @@ static void process_jobs(private_thread_pool_t *this)
/** /**
* implementation of private_thread_pool_t.process_incoming_packet_job * implementation of private_thread_pool_t.process_incoming_packet_job
*/ */
void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_job_t *job) static void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_job_t *job)
{ {
packet_t *packet; packet_t *packet;
message_t *message; message_t *message;
ike_sa_t *ike_sa; ike_sa_t *ike_sa;
ike_sa_id_t *ike_sa_id; ike_sa_id_t *ike_sa_id;
status_t status; status_t status;
if (job->get_packet(job,&packet) != SUCCESS) if (job->get_packet(job,&packet) != SUCCESS)
{ {
this->worker_logger->log(this->worker_logger, ERROR, "packet in job could not be retrieved!"); this->worker_logger->log(this->worker_logger, ERROR, "packet in job could not be retrieved!");
@@ -239,7 +241,7 @@ void process_incoming_packet_job(private_thread_pool_t *this, incoming_packet_jo
/** /**
* implementation of private_thread_pool_t.process_initiate_ike_sa_job * implementation of private_thread_pool_t.process_initiate_ike_sa_job
*/ */
void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_job_t *job) static void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_job_t *job)
{ {
/* /*
* Initiatie an IKE_SA: * Initiatie an IKE_SA:
@@ -249,19 +251,12 @@ void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_jo
*/ */
ike_sa_t *ike_sa; ike_sa_t *ike_sa;
status_t status; status_t status;
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "create and checking out IKE SA"); this->worker_logger->log(this->worker_logger, CONTROL|MOST, "create and checking out IKE SA");
status = global_ike_sa_manager->create_and_checkout(global_ike_sa_manager, &ike_sa); global_ike_sa_manager->create_and_checkout(global_ike_sa_manager, &ike_sa);
if (status != SUCCESS)
{
this->worker_logger->log(this->worker_logger, ERROR, "%s by checking out new IKE_SA, job rejected.",
mapping_find(status_m, status));
return;
}
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "initializing connection \"%s\"", this->worker_logger->log(this->worker_logger, CONTROL|MOST, "initializing connection \"%s\"",
job->get_configuration_name(job)); job->get_configuration_name(job));
status = ike_sa->initialize_connection(ike_sa, job->get_configuration_name(job)); status = ike_sa->initialize_connection(ike_sa, job->get_configuration_name(job));
@@ -272,7 +267,7 @@ void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_jo
global_ike_sa_manager->checkin_and_delete(global_ike_sa_manager, ike_sa); global_ike_sa_manager->checkin_and_delete(global_ike_sa_manager, ike_sa);
return; return;
} }
this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking in IKE SA"); this->worker_logger->log(this->worker_logger, CONTROL|MOST, "checking in IKE SA");
status = global_ike_sa_manager->checkin(global_ike_sa_manager, ike_sa); status = global_ike_sa_manager->checkin(global_ike_sa_manager, ike_sa);
if (status != SUCCESS) if (status != SUCCESS)
@@ -285,7 +280,7 @@ void process_initiate_ike_sa_job(private_thread_pool_t *this, initiate_ike_sa_jo
/** /**
* implementation of private_thread_pool_t.process_delete_ike_sa_job * implementation of private_thread_pool_t.process_delete_ike_sa_job
*/ */
void process_delete_ike_sa_job(private_thread_pool_t *this, delete_ike_sa_job_t *job) static void process_delete_ike_sa_job(private_thread_pool_t *this, delete_ike_sa_job_t *job)
{ {
status_t status; status_t status;
ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job); ike_sa_id_t *ike_sa_id = job->get_ike_sa_id(job);
@@ -294,7 +289,7 @@ void process_delete_ike_sa_job(private_thread_pool_t *this, delete_ike_sa_job_t
ike_sa_id->get_initiator_spi(ike_sa_id), ike_sa_id->get_initiator_spi(ike_sa_id),
ike_sa_id->get_responder_spi(ike_sa_id), ike_sa_id->get_responder_spi(ike_sa_id),
ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder"); ike_sa_id->is_initiator(ike_sa_id) ? "initiator" : "responder");
status = global_ike_sa_manager->delete(global_ike_sa_manager, ike_sa_id); status = global_ike_sa_manager->delete(global_ike_sa_manager, ike_sa_id);
if (status != SUCCESS) if (status != SUCCESS)
{ {
@@ -315,7 +310,7 @@ static size_t get_pool_size(private_thread_pool_t *this)
/** /**
* Implementation of thread_pool_t.destroy * Implementation of thread_pool_t.destroy
*/ */
static status_t destroy(private_thread_pool_t *this) static void destroy(private_thread_pool_t *this)
{ {
int current; int current;
/* flag thread for termination */ /* flag thread for termination */
@@ -335,11 +330,8 @@ static status_t destroy(private_thread_pool_t *this)
global_logger_manager->destroy_logger(global_logger_manager, this->worker_logger); global_logger_manager->destroy_logger(global_logger_manager, this->worker_logger);
allocator_free(this->threads); allocator_free(this->threads);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
#include <stdio.h>
/* /*
* see header * see header
*/ */
@@ -348,13 +340,9 @@ thread_pool_t *thread_pool_create(size_t pool_size)
int current; int current;
private_thread_pool_t *this = allocator_alloc_thing(private_thread_pool_t); private_thread_pool_t *this = allocator_alloc_thing(private_thread_pool_t);
if (this == NULL)
{
return NULL;
}
/* fill in public fields */ /* fill in public fields */
this->public.destroy = (status_t(*)(thread_pool_t*))destroy; this->public.destroy = (void(*)(thread_pool_t*))destroy;
this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size; this->public.get_pool_size = (size_t(*)(thread_pool_t*))get_pool_size;
this->process_jobs = process_jobs; this->process_jobs = process_jobs;
@@ -364,26 +352,10 @@ thread_pool_t *thread_pool_create(size_t pool_size)
this->pool_size = pool_size; this->pool_size = pool_size;
this->threads = allocator_alloc(sizeof(pthread_t) * pool_size); this->threads = allocator_alloc(sizeof(pthread_t) * pool_size);
if (this->threads == NULL)
{
allocator_free(this);
return NULL;
}
this->pool_logger = global_logger_manager->create_logger(global_logger_manager,THREAD_POOL,NULL); this->pool_logger = global_logger_manager->create_logger(global_logger_manager,THREAD_POOL,NULL);
if (this->threads == NULL)
{
allocator_free(this);
allocator_free(this->threads);
return NULL;
}
this->worker_logger = global_logger_manager->create_logger(global_logger_manager,WORKER,NULL); this->worker_logger = global_logger_manager->create_logger(global_logger_manager,WORKER,NULL);
if (this->threads == NULL)
{
global_logger_manager->destroy_logger(global_logger_manager, this->pool_logger);
allocator_free(this);
allocator_free(this->threads);
return NULL;
}
/* try to create as many threads as possible, up tu pool_size */ /* try to create as many threads as possible, up tu pool_size */
for (current = 0; current < pool_size; current++) for (current = 0; current < pool_size; current++)
+1 -3
View File
@@ -52,10 +52,8 @@ struct thread_pool_t {
* sends cancellation request to all threads and AWAITS their termination. * sends cancellation request to all threads and AWAITS their termination.
* *
* @param thread_pool thread_pool_t object * @param thread_pool thread_pool_t object
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (thread_pool_t *thread_pool); void (*destroy) (thread_pool_t *thread_pool);
}; };
/** /**
@@ -1474,90 +1474,90 @@ static status_t set_key (private_aes_cbc_crypter_t *this, chunk_t key)
{ {
u_int32_t *kf, *kt, rci, f = 0; u_int32_t *kf, *kt, rci, f = 0;
u_int8_t *in_key = key.ptr; u_int8_t *in_key = key.ptr;
if (key.len != this->blocksize) if (key.len != this->blocksize)
{ {
return INVALID_ARG; return INVALID_ARG;
} }
this->aes_Nrnd = (this->aes_Nkey > (this->aes_Ncol) ? this->aes_Nkey : (this->aes_Ncol)) + 6;
this->aes_e_key[0] = const_word_in(in_key );
this->aes_e_key[1] = const_word_in(in_key + 4);
this->aes_e_key[2] = const_word_in(in_key + 8);
this->aes_e_key[3] = const_word_in(in_key + 12);
kf = this->aes_e_key;
kt = kf + nc * (this->aes_Nrnd + 1) - this->aes_Nkey;
rci = 0;
switch(this->aes_Nkey)
{
case 4: do
{ kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
kf[5] = kf[1] ^ kf[4];
kf[6] = kf[2] ^ kf[5];
kf[7] = kf[3] ^ kf[6];
kf += 4;
}
while(kf < kt);
break;
case 6: this->aes_e_key[4] = const_word_in(in_key + 16);
this->aes_e_key[5] = const_word_in(in_key + 20);
do
{ kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++];
kf[ 7] = kf[1] ^ kf[ 6];
kf[ 8] = kf[2] ^ kf[ 7];
kf[ 9] = kf[3] ^ kf[ 8];
kf[10] = kf[4] ^ kf[ 9];
kf[11] = kf[5] ^ kf[10];
kf += 6;
}
while(kf < kt);
break;
this->aes_Nrnd = (this->aes_Nkey > (this->aes_Ncol) ? this->aes_Nkey : (this->aes_Ncol)) + 6; case 8: this->aes_e_key[4] = const_word_in(in_key + 16);
this->aes_e_key[5] = const_word_in(in_key + 20);
this->aes_e_key[0] = const_word_in(in_key ); this->aes_e_key[6] = const_word_in(in_key + 24);
this->aes_e_key[1] = const_word_in(in_key + 4); this->aes_e_key[7] = const_word_in(in_key + 28);
this->aes_e_key[2] = const_word_in(in_key + 8); do
this->aes_e_key[3] = const_word_in(in_key + 12); { kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
kf[ 9] = kf[1] ^ kf[ 8];
kf = this->aes_e_key; kf[10] = kf[2] ^ kf[ 9];
kt = kf + nc * (this->aes_Nrnd + 1) - this->aes_Nkey; kf[11] = kf[3] ^ kf[10];
rci = 0; kf[12] = kf[4] ^ ls_box(kf[11],0);
kf[13] = kf[5] ^ kf[12];
switch(this->aes_Nkey) kf[14] = kf[6] ^ kf[13];
kf[15] = kf[7] ^ kf[14];
kf += 8;
}
while (kf < kt);
break;
}
if(!f)
{ {
case 4: do u_int32_t i;
{ kf[4] = kf[0] ^ ls_box(kf[3],3) ^ rcon_tab[rci++];
kf[5] = kf[1] ^ kf[4];
kf[6] = kf[2] ^ kf[5];
kf[7] = kf[3] ^ kf[6];
kf += 4;
}
while(kf < kt);
break;
case 6: this->aes_e_key[4] = const_word_in(in_key + 16); kt = this->aes_d_key + nc * this->aes_Nrnd;
this->aes_e_key[5] = const_word_in(in_key + 20); kf = this->aes_e_key;
do
{ kf[ 6] = kf[0] ^ ls_box(kf[5],3) ^ rcon_tab[rci++]; cpy(kt, kf); kt -= 2 * nc;
kf[ 7] = kf[1] ^ kf[ 6];
kf[ 8] = kf[2] ^ kf[ 7]; for(i = 1; i < this->aes_Nrnd; ++i)
kf[ 9] = kf[3] ^ kf[ 8]; {
kf[10] = kf[4] ^ kf[ 9];
kf[11] = kf[5] ^ kf[10];
kf += 6;
}
while(kf < kt);
break;
case 8: this->aes_e_key[4] = const_word_in(in_key + 16);
this->aes_e_key[5] = const_word_in(in_key + 20);
this->aes_e_key[6] = const_word_in(in_key + 24);
this->aes_e_key[7] = const_word_in(in_key + 28);
do
{ kf[ 8] = kf[0] ^ ls_box(kf[7],3) ^ rcon_tab[rci++];
kf[ 9] = kf[1] ^ kf[ 8];
kf[10] = kf[2] ^ kf[ 9];
kf[11] = kf[3] ^ kf[10];
kf[12] = kf[4] ^ ls_box(kf[11],0);
kf[13] = kf[5] ^ kf[12];
kf[14] = kf[6] ^ kf[13];
kf[15] = kf[7] ^ kf[14];
kf += 8;
}
while (kf < kt);
break;
}
if(!f)
{ u_int32_t i;
kt = this->aes_d_key + nc * this->aes_Nrnd;
kf = this->aes_e_key;
cpy(kt, kf); kt -= 2 * nc;
for(i = 1; i < this->aes_Nrnd; ++i)
{
#if defined(ONE_TABLE) || defined(FOUR_TABLES) #if defined(ONE_TABLE) || defined(FOUR_TABLES)
#if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES) #if !defined(ONE_IM_TABLE) && !defined(FOUR_IM_TABLES)
u_int32_t f2, f4, f8, f9; u_int32_t f2, f4, f8, f9;
#endif #endif
mix(kt, kf); mix(kt, kf);
#else #else
cpy(kt, kf); cpy(kt, kf);
#endif #endif
kt -= 2 * nc; kt -= 2 * nc;
} }
cpy(kt, kf);
cpy(kt, kf);
} }
return SUCCESS; return SUCCESS;
@@ -1566,10 +1566,9 @@ static status_t set_key (private_aes_cbc_crypter_t *this, chunk_t key)
/** /**
* Implementation of crypter_t.destroy and aes_cbc_crypter_t.destroy. * Implementation of crypter_t.destroy and aes_cbc_crypter_t.destroy.
*/ */
static status_t destroy (private_aes_cbc_crypter_t *this) static void destroy (private_aes_cbc_crypter_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -1578,10 +1577,7 @@ static status_t destroy (private_aes_cbc_crypter_t *this)
aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize) aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize)
{ {
private_aes_cbc_crypter_t *this = allocator_alloc_thing(private_aes_cbc_crypter_t); private_aes_cbc_crypter_t *this = allocator_alloc_thing(private_aes_cbc_crypter_t);
if (this == NULL)
{
return NULL;
}
#if !defined(FIXED_TABLES) #if !defined(FIXED_TABLES)
if(!tab_gen) { gen_tabs(); tab_gen = 1; } if(!tab_gen) { gen_tabs(); tab_gen = 1; }
#endif #endif
@@ -1610,10 +1606,7 @@ aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize)
this->public.crypter_interface.decrypt = (status_t (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt; this->public.crypter_interface.decrypt = (status_t (*) (crypter_t *, chunk_t , chunk_t, chunk_t *)) decrypt;
this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *)) get_block_size; this->public.crypter_interface.get_block_size = (size_t (*) (crypter_t *)) get_block_size;
this->public.crypter_interface.set_key = (status_t (*) (crypter_t *,chunk_t)) set_key; this->public.crypter_interface.set_key = (status_t (*) (crypter_t *,chunk_t)) set_key;
this->public.crypter_interface.destroy = (status_t (*) (crypter_t *)) destroy; this->public.crypter_interface.destroy = (void (*) (crypter_t *)) destroy;
/* public functions */
this->public.destroy = (status_t (*) (aes_cbc_crypter_t *)) destroy;
/* private functions */ /* private functions */
this->decrypt_block = decrypt_block; this->decrypt_block = decrypt_block;
@@ -40,26 +40,15 @@ struct aes_cbc_crypter_t {
* crypter_t interface. * crypter_t interface.
*/ */
crypter_t crypter_interface; crypter_t crypter_interface;
/**
* @brief Destroys a aes_cbc_crypter_t object.
*
* @param this crypter_t object to destroy
* @return
* - SUCCESS in any case
*/
status_t (*destroy) (aes_cbc_crypter_t *this);
}; };
/** /**
* @brief Constructor to create aes_cbc_crypter_t objects. * @brief Constructor to create aes_cbc_crypter_t objects.
* *
* @param blocksize block size of AES crypter * @param blocksize block size of AES crypter
* (16, 24 or 32 are supported) * (16, 24 or 32 are supported)
* Default size is set to 16. * Default size is set to 16.
* @return * @return aes_cbc_crypter_t if successfully
* - aes_cbc_crypter_t if successfully
* - NULL if out of ressources
*/ */
aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize); aes_cbc_crypter_t *aes_cbc_crypter_create(size_t blocksize);
@@ -56,7 +56,6 @@ crypter_t *crypter_create(encryption_algorithm_t encryption_algorithm,size_t blo
case ENCR_AES_CBC: case ENCR_AES_CBC:
{ {
return (crypter_t*)aes_cbc_crypter_create(blocksize); return (crypter_t*)aes_cbc_crypter_create(blocksize);
} }
default: default:
return NULL; return NULL;
+10 -9
View File
@@ -68,8 +68,9 @@ struct crypter_t {
* @param data data to encrypt * @param data data to encrypt
* @param iv iv * @param iv iv
* @param [out]encrypted pointer where the encrypted bytes will be written * @param [out]encrypted pointer where the encrypted bytes will be written
* @return * @return
* - SUCCESS in any case * - SUCCESS, or
* - INVALID_ARG if data size not a multiple of block size
*/ */
status_t (*encrypt) (crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted); status_t (*encrypt) (crypter_t *this, chunk_t data, chunk_t iv, chunk_t *encrypted);
@@ -81,8 +82,9 @@ struct crypter_t {
* @param data data to decrypt * @param data data to decrypt
* @param iv iv * @param iv iv
* @param [out]encrypted pointer where the decrypted bytes will be written * @param [out]encrypted pointer where the decrypted bytes will be written
* @return * @return
* - SUCCESS in any case * - SUCCESS, or
* - INVALID_ARG if data size not a multiple of block size
*/ */
status_t (*decrypt) (crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted); status_t (*decrypt) (crypter_t *this, chunk_t data, chunk_t iv, chunk_t *decrypted);
@@ -100,7 +102,8 @@ struct crypter_t {
* @param this calling crypter * @param this calling crypter
* @param key key to set * @param key key to set
* @return * @return
* - SUCCESS in any case * - SUCCESS, or
* - INVALID_ARG if key size != block size
*/ */
status_t (*set_key) (crypter_t *this, chunk_t key); status_t (*set_key) (crypter_t *this, chunk_t key);
@@ -108,10 +111,8 @@ struct crypter_t {
* @brief Destroys a crypter_t object. * @brief Destroys a crypter_t object.
* *
* @param this crypter_t object to destroy * @param this crypter_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (crypter_t *this); void (*destroy) (crypter_t *this);
}; };
/** /**
@@ -121,7 +122,7 @@ struct crypter_t {
* @param blocksize block size in bytes * @param blocksize block size in bytes
* @return * @return
* - crypter_t if successfully * - crypter_t if successfully
* - NULL if out of ressources or crypter not supported * - NULL if crypter not supported
*/ */
crypter_t *crypter_create(encryption_algorithm_t encryption_algorithm, size_t blocksize); crypter_t *crypter_create(encryption_algorithm_t encryption_algorithm, size_t blocksize);
+24 -46
View File
@@ -438,8 +438,7 @@ struct private_diffie_hellman_t {
}; };
/** /**
* Implements private_diffie_hellman_t's set_modulus function. * Implements private_diffie_hellman_tset_modulus.
* See #private_diffie_hellman_t.set_modulus for description.
*/ */
static status_t set_modulus(private_diffie_hellman_t *this) static status_t set_modulus(private_diffie_hellman_t *this)
{ {
@@ -464,19 +463,16 @@ static status_t set_modulus(private_diffie_hellman_t *this)
} }
/** /**
* Implements diffie_hellman_t's set_other_public_value function. * Implementation of diffie_hellman_t.set_other_public_value.
* See #diffie_hellman_t.set_other_public_value for description.
*/ */
static status_t set_other_public_value(private_diffie_hellman_t *this,chunk_t public_value) static void set_other_public_value(private_diffie_hellman_t *this,chunk_t public_value)
{ {
this->gmp_helper->chunk_to_mpz(this->gmp_helper,&(this->other_public_value),public_value); this->gmp_helper->chunk_to_mpz(this->gmp_helper,&(this->other_public_value),public_value);
this->compute_shared_secret(this); this->compute_shared_secret(this);
return SUCCESS;
} }
/** /**
* Implements diffie_hellman_t's get_other_public_value function. * Implements diffie_hellman_t.get_other_public_value.
* See #diffie_hellman_t.get_other_public_value for description.
*/ */
static status_t get_other_public_value(private_diffie_hellman_t *this,chunk_t *public_value) static status_t get_other_public_value(private_diffie_hellman_t *this,chunk_t *public_value)
{ {
@@ -484,12 +480,12 @@ static status_t get_other_public_value(private_diffie_hellman_t *this,chunk_t *p
{ {
return FAILED; return FAILED;
} }
return (this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->other_public_value), public_value,this->modulus_length)); this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->other_public_value), public_value,this->modulus_length);
return SUCCESS;
} }
/** /**
* Implements private_diffie_hellman_t's compute_shared_secret function. * Implements private_diffie_hellman_t.compute_shared_secret.
* See #private_diffie_hellman_t.compute_shared_secret for description.
*/ */
static void compute_shared_secret (private_diffie_hellman_t *this) static void compute_shared_secret (private_diffie_hellman_t *this)
{ {
@@ -497,14 +493,13 @@ static void compute_shared_secret (private_diffie_hellman_t *this)
mpz_init(this->shared_secret); mpz_init(this->shared_secret);
/* calculate my public value */ /* calculate my public value */
mpz_powm(this->shared_secret,this->other_public_value,this->my_prime,this->modulus); mpz_powm(this->shared_secret,this->other_public_value,this->my_prime,this->modulus);
this->shared_secret_is_computed = TRUE; this->shared_secret_is_computed = TRUE;
} }
/** /**
* Implements private_diffie_hellman_t's compute_public_value function. * Implements private_diffie_hellman_t.compute_public_value.
* See #private_diffie_hellman_t.compute_public_value for description.
*/ */
static void compute_public_value (private_diffie_hellman_t *this) static void compute_public_value (private_diffie_hellman_t *this)
{ {
@@ -521,8 +516,7 @@ static void compute_public_value (private_diffie_hellman_t *this)
} }
/** /**
* Implements diffie_hellman_t's get_my_public_value function. * Implements diffie_hellman_t.get_my_public_value.
* See #diffie_hellman_t.get_my_public_value for description.
*/ */
static status_t get_my_public_value(private_diffie_hellman_t *this,chunk_t *public_value) static status_t get_my_public_value(private_diffie_hellman_t *this,chunk_t *public_value)
{ {
@@ -530,12 +524,12 @@ static status_t get_my_public_value(private_diffie_hellman_t *this,chunk_t *publ
{ {
this->compute_public_value(this); this->compute_public_value(this);
} }
return (this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->my_public_value), public_value,this->modulus_length)); this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->my_public_value), public_value,this->modulus_length);
return SUCCESS;
} }
/** /**
* Implements diffie_hellman_t's get_shared_secret function. * Implements diffie_hellman_t.get_shared_secret.
* See #diffie_hellman_t.get_shared_secret for description.
*/ */
static status_t get_shared_secret(private_diffie_hellman_t *this,chunk_t *secret) static status_t get_shared_secret(private_diffie_hellman_t *this,chunk_t *secret)
{ {
@@ -543,14 +537,14 @@ static status_t get_shared_secret(private_diffie_hellman_t *this,chunk_t *secret
{ {
return FAILED; return FAILED;
} }
return (this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->shared_secret), secret,this->modulus_length)); this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->shared_secret), secret,this->modulus_length);
return SUCCESS;
} }
/** /**
* Implements diffie_hellman_t's destroy function. * Implements diffie_hellman_t.destroy.
* See #diffie_hellman_t.destroy for description.
*/ */
static status_t destroy(private_diffie_hellman_t *this) static void destroy(private_diffie_hellman_t *this)
{ {
this->gmp_helper->destroy(this->gmp_helper); this->gmp_helper->destroy(this->gmp_helper);
mpz_clear(this->modulus); mpz_clear(this->modulus);
@@ -565,9 +559,7 @@ static status_t destroy(private_diffie_hellman_t *this)
mpz_clear(this->other_public_value); mpz_clear(this->other_public_value);
mpz_clear(this->shared_secret); mpz_clear(this->shared_secret);
} }
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
@@ -577,17 +569,13 @@ static status_t destroy(private_diffie_hellman_t *this)
diffie_hellman_t *diffie_hellman_create(diffie_hellman_group_t dh_group_number) diffie_hellman_t *diffie_hellman_create(diffie_hellman_group_t dh_group_number)
{ {
private_diffie_hellman_t *this = allocator_alloc_thing(private_diffie_hellman_t); private_diffie_hellman_t *this = allocator_alloc_thing(private_diffie_hellman_t);
if ((this == NULL))
{
return NULL;
}
/* public functions */ /* public functions */
this->public.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret; this->public.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;
this->public.set_other_public_value = (status_t (*)(diffie_hellman_t *, chunk_t )) set_other_public_value; this->public.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;
this->public.get_other_public_value = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_other_public_value; this->public.get_other_public_value = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_other_public_value;
this->public.get_my_public_value = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value; this->public.get_my_public_value = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;
this->public.destroy = (status_t (*)(diffie_hellman_t *)) destroy; this->public.destroy = (void (*)(diffie_hellman_t *)) destroy;
/* private functions */ /* private functions */
this->set_modulus = set_modulus; this->set_modulus = set_modulus;
@@ -599,12 +587,6 @@ diffie_hellman_t *diffie_hellman_create(diffie_hellman_group_t dh_group_number)
this->gmp_helper = gmp_helper_create(); this->gmp_helper = gmp_helper_create();
if (this->gmp_helper == NULL)
{
allocator_free(this);
return NULL;
}
/* set this->modulus */ /* set this->modulus */
if (this->set_modulus(this) != SUCCESS) if (this->set_modulus(this) != SUCCESS)
{ {
@@ -612,13 +594,9 @@ diffie_hellman_t *diffie_hellman_create(diffie_hellman_group_t dh_group_number)
allocator_free(this); allocator_free(this);
return NULL; return NULL;
} }
if (this->gmp_helper->init_prime(this->gmp_helper,&(this->my_prime),this->modulus_length) != SUCCESS) this->gmp_helper->init_prime(this->gmp_helper,&(this->my_prime),this->modulus_length);
{
this->gmp_helper->destroy(this->gmp_helper);
allocator_free(this);
return NULL;
}
this->my_public_value_is_computed = FALSE; this->my_public_value_is_computed = FALSE;
this->shared_secret_is_computed = FALSE; this->shared_secret_is_computed = FALSE;
+6 -13
View File
@@ -73,7 +73,6 @@ struct diffie_hellman_t {
* @return * @return
* - SUCCESS, or * - SUCCESS, or
* - FAILED if not both DH values are set * - FAILED if not both DH values are set
* - OUT_OF_RES if out of ressources
*/ */
status_t (*get_shared_secret) (diffie_hellman_t *this, chunk_t *secret); status_t (*get_shared_secret) (diffie_hellman_t *this, chunk_t *secret);
@@ -84,11 +83,8 @@ struct diffie_hellman_t {
* *
* @param this calling diffie_hellman_t object * @param this calling diffie_hellman_t object
* @param public_value public value of partner * @param public_value public value of partner
* @return
* - SUCCESS, or
* - OUT_OF_RES if out of ressources
*/ */
status_t (*set_other_public_value) (diffie_hellman_t *this, chunk_t public_value); void (*set_other_public_value) (diffie_hellman_t *this, chunk_t public_value);
/** /**
* @brief Gets the public value of partner. * @brief Gets the public value of partner.
@@ -99,7 +95,6 @@ struct diffie_hellman_t {
* @param[out] public_value public value of partner is stored at this location * @param[out] public_value public value of partner is stored at this location
* @return * @return
* - SUCCESS, or * - SUCCESS, or
* - OUT_OF_RES if out of ressources
* - FAILED if other public value not set * - FAILED if other public value not set
*/ */
status_t (*get_other_public_value) (diffie_hellman_t *this, chunk_t *public_value); status_t (*get_other_public_value) (diffie_hellman_t *this, chunk_t *public_value);
@@ -110,10 +105,10 @@ struct diffie_hellman_t {
* @warning chunk gets copied * @warning chunk gets copied
* *
* @param this calling diffie_hellman_t object * @param this calling diffie_hellman_t object
* @param[out] public_value public value of caller is stored at this location * @param[out] public_value public value of caller is stored at this location
* @return * @return
* - SUCCESS, or * - SUCCESS, or
* - OUT_OF_RES if out of ressources * - FAILED if not computed
*/ */
status_t (*get_my_public_value) (diffie_hellman_t *this, chunk_t *public_value); status_t (*get_my_public_value) (diffie_hellman_t *this, chunk_t *public_value);
@@ -121,10 +116,8 @@ struct diffie_hellman_t {
* @brief Destroys an diffie_hellman_t object. * @brief Destroys an diffie_hellman_t object.
* *
* @param this diffie_hellman_t object to destroy * @param this diffie_hellman_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (diffie_hellman_t *this); void (*destroy) (diffie_hellman_t *this);
}; };
/** /**
@@ -134,8 +127,8 @@ struct diffie_hellman_t {
* *
* @param dh_group_number Diffie Hellman group number to use * @param dh_group_number Diffie Hellman group number to use
* @return * @return
* - diffie_hellman_t if successfully * - diffie_hellman_t object
* - NULL if out of ressources or dh_group not supported * - NULL if dh group not supported
* *
* @ingroup transforms * @ingroup transforms
*/ */
@@ -54,8 +54,3 @@ hasher_t *hasher_create(hash_algorithm_t hash_algorithm)
return NULL; return NULL;
} }
} }
+5 -13
View File
@@ -63,10 +63,8 @@ struct hasher_t {
* @param this calling hasher * @param this calling hasher
* @param data data to hash * @param data data to hash
* @param [out]buffer pointer where the hash will be written * @param [out]buffer pointer where the hash will be written
* @return
* - SUCCESS in any case
*/ */
status_t (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash); void (*get_hash) (hasher_t *this, chunk_t data, u_int8_t *hash);
/** /**
* @brief hash data and allocate space for the hash * @brief hash data and allocate space for the hash
@@ -78,11 +76,8 @@ struct hasher_t {
* @param this calling hasher * @param this calling hasher
* @param data chunk with data to hash * @param data chunk with data to hash
* @param [out]hash chunk which will hold allocated hash * @param [out]hash chunk which will hold allocated hash
* @return
* - SUCCESS in any case
* - OUT_OF_RES if space could not be allocated
*/ */
status_t (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash); void (*allocate_hash) (hasher_t *this, chunk_t data, chunk_t *hash);
/** /**
* @brief Get the block size of this hashing function. * @brief Get the block size of this hashing function.
@@ -97,18 +92,15 @@ struct hasher_t {
* computation of a completly new hash. * computation of a completly new hash.
* *
* @param this calling hasher * @param this calling hasher
* @return - SUCCESS in any case
*/ */
status_t (*reset) (hasher_t *this); void (*reset) (hasher_t *this);
/** /**
* @brief Destroys a hasher object. * @brief Destroys a hasher object.
* *
* @param this hasher_t object to destroy * @param this hasher_t object to destroy
* @return
* SUCCESS in any case
*/ */
status_t (*destroy) (hasher_t *this); void (*destroy) (hasher_t *this);
}; };
/** /**
@@ -117,7 +109,7 @@ struct hasher_t {
* @param hash_algorithm Algorithm to use for hashing * @param hash_algorithm Algorithm to use for hashing
* @return * @return
* - hasher_t if successfully * - hasher_t if successfully
* - NULL if out of ressources * - NULL if algorithm not supported
* *
* @ingroup hashers * @ingroup hashers
*/ */
+14 -26
View File
@@ -244,7 +244,7 @@ static void MD5Transform(u_int32_t state[4], u_int8_t block[64])
* operation, processing another message block, and updating the * operation, processing another message block, and updating the
* context. * context.
*/ */
void MD5Update(private_md5_hasher_t *this, u_int8_t *input, size_t inputLen) static void MD5Update(private_md5_hasher_t *this, u_int8_t *input, size_t inputLen)
{ {
u_int32_t i; u_int32_t i;
size_t index, partLen; size_t index, partLen;
@@ -285,7 +285,7 @@ void MD5Update(private_md5_hasher_t *this, u_int8_t *input, size_t inputLen)
/* MD5 finalization. Ends an MD5 message-digest operation, writing the /* MD5 finalization. Ends an MD5 message-digest operation, writing the
* the message digest and zeroizing the context. * the message digest and zeroizing the context.
*/ */
void MD5Final (private_md5_hasher_t *this, u_int8_t digest[16]) static void MD5Final (private_md5_hasher_t *this, u_int8_t digest[16])
{ {
u_int8_t bits[8]; u_int8_t bits[8];
size_t index, padLen; size_t index, padLen;
@@ -313,7 +313,7 @@ void MD5Final (private_md5_hasher_t *this, u_int8_t digest[16])
/** /**
* implementation of hasher_t.get_hash for md5 * implementation of hasher_t.get_hash for md5
*/ */
static status_t get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *buffer) static void get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
{ {
MD5Update(this, chunk.ptr, chunk.len); MD5Update(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -321,14 +321,13 @@ static status_t get_hash(private_md5_hasher_t *this, chunk_t chunk, u_int8_t *bu
MD5Final(this, buffer); MD5Final(this, buffer);
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
} }
return SUCCESS;
} }
/** /**
* implementation of hasher_t.allocate_hash for md5 * implementation of hasher_t.allocate_hash for md5
*/ */
static status_t allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t *hash) static void allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -337,17 +336,12 @@ static status_t allocate_hash(private_md5_hasher_t *this, chunk_t chunk, chunk_t
{ {
allocated_hash.ptr = allocator_alloc(BLOCK_SIZE_MD5); allocated_hash.ptr = allocator_alloc(BLOCK_SIZE_MD5);
allocated_hash.len = BLOCK_SIZE_MD5; allocated_hash.len = BLOCK_SIZE_MD5;
if (allocated_hash.ptr == NULL)
{
return OUT_OF_RES;
}
MD5Final(this, allocated_hash.ptr); MD5Final(this, allocated_hash.ptr);
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
*hash = allocated_hash; *hash = allocated_hash;
} }
return SUCCESS;
} }
/** /**
@@ -357,11 +351,11 @@ static size_t get_block_size(private_md5_hasher_t *this)
{ {
return BLOCK_SIZE_MD5; return BLOCK_SIZE_MD5;
} }
/** /**
* implementation of hasher_t.reset for md5 * implementation of hasher_t.reset for md5
*/ */
static status_t reset(private_md5_hasher_t *this) static void reset(private_md5_hasher_t *this)
{ {
this->state[0] = 0x67452301; this->state[0] = 0x67452301;
this->state[1] = 0xefcdab89; this->state[1] = 0xefcdab89;
@@ -369,34 +363,28 @@ static status_t reset(private_md5_hasher_t *this)
this->state[3] = 0x10325476; this->state[3] = 0x10325476;
this->count[0] = 0; this->count[0] = 0;
this->count[1] = 0; this->count[1] = 0;
return SUCCESS;
} }
/** /**
* implementation of hasher_t.destroy for md5 * implementation of hasher_t.destroy for md5
*/ */
static status_t destroy(private_md5_hasher_t *this) static void destroy(private_md5_hasher_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
* Described in header * Described in header
*/ */
md5_hasher_t *md5_hasher_create() md5_hasher_t *md5_hasher_create()
{ {
private_md5_hasher_t *this = allocator_alloc_thing(private_md5_hasher_t); private_md5_hasher_t *this = allocator_alloc_thing(private_md5_hasher_t);
if (this == NULL)
{ this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
return NULL; this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
}
this->public.hasher_interface.get_hash = (status_t (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
this->public.hasher_interface.allocate_hash = (status_t (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
this->public.hasher_interface.get_block_size = (size_t (*) (hasher_t*))get_block_size; this->public.hasher_interface.get_block_size = (size_t (*) (hasher_t*))get_block_size;
this->public.hasher_interface.reset = (size_t (*) (hasher_t*))reset; this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
this->public.hasher_interface.destroy = (size_t (*) (hasher_t*))destroy; this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
/* initialize */ /* initialize */
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
@@ -45,9 +45,7 @@ struct md5_hasher_t {
/** /**
* @brief Creates a new md5_hasher_t. * @brief Creates a new md5_hasher_t.
* *
* @return * @return md5_hasher_t object
* - md5_hasher_t if successfully
* - NULL if out of ressources
* *
* @ingroup hashers * @ingroup hashers
*/ */
+12 -20
View File
@@ -74,7 +74,7 @@ struct private_sha1_hasher_t {
/* /*
* Hash a single 512-bit block. This is the core of the algorithm. * * Hash a single 512-bit block. This is the core of the algorithm. *
*/ */
void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64]) static void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
{ {
u_int32_t a, b, c, d, e; u_int32_t a, b, c, d, e;
typedef union { typedef union {
@@ -125,7 +125,7 @@ void SHA1Transform(u_int32_t state[5], const unsigned char buffer[64])
/* /*
* Run your data through this. * Run your data through this.
*/ */
void SHA1Update(private_sha1_hasher_t* this, u_int8_t *data, u_int32_t len) static void SHA1Update(private_sha1_hasher_t* this, u_int8_t *data, u_int32_t len)
{ {
u_int32_t i; u_int32_t i;
u_int32_t j; u_int32_t j;
@@ -158,7 +158,7 @@ void SHA1Update(private_sha1_hasher_t* this, u_int8_t *data, u_int32_t len)
/* /*
* Add padding and return the message digest. * Add padding and return the message digest.
*/ */
void SHA1Final(private_sha1_hasher_t *this, u_int8_t *digest) static void SHA1Final(private_sha1_hasher_t *this, u_int8_t *digest)
{ {
u_int32_t i; u_int32_t i;
u_int8_t finalcount[8]; u_int8_t finalcount[8];
@@ -187,7 +187,7 @@ void SHA1Final(private_sha1_hasher_t *this, u_int8_t *digest)
/** /**
* implementation of hasher_t.get_hash for sha1 * implementation of hasher_t.get_hash for sha1
*/ */
static status_t get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffer) static void get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *buffer)
{ {
SHA1Update(this, chunk.ptr, chunk.len); SHA1Update(this, chunk.ptr, chunk.len);
if (buffer != NULL) if (buffer != NULL)
@@ -195,14 +195,13 @@ static status_t get_hash(private_sha1_hasher_t *this, chunk_t chunk, u_int8_t *b
SHA1Final(this, buffer); SHA1Final(this, buffer);
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
} }
return SUCCESS;
} }
/** /**
* implementation of hasher_t.allocate_hash for sha1 * implementation of hasher_t.allocate_hash for sha1
*/ */
static status_t allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash) static void allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_t *hash)
{ {
chunk_t allocated_hash; chunk_t allocated_hash;
@@ -211,17 +210,12 @@ static status_t allocate_hash(private_sha1_hasher_t *this, chunk_t chunk, chunk_
{ {
allocated_hash.ptr = allocator_alloc(BLOCK_SIZE_SHA1); allocated_hash.ptr = allocator_alloc(BLOCK_SIZE_SHA1);
allocated_hash.len = BLOCK_SIZE_SHA1; allocated_hash.len = BLOCK_SIZE_SHA1;
if (allocated_hash.ptr == NULL)
{
return OUT_OF_RES;
}
SHA1Final(this, allocated_hash.ptr); SHA1Final(this, allocated_hash.ptr);
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
*hash = allocated_hash; *hash = allocated_hash;
} }
return SUCCESS;
} }
/** /**
@@ -235,7 +229,7 @@ static size_t get_block_size(private_sha1_hasher_t *this)
/** /**
* implementation of hasher_t.reset for sha1 * implementation of hasher_t.reset for sha1
*/ */
static status_t reset(private_sha1_hasher_t *this) static void reset(private_sha1_hasher_t *this)
{ {
this->state[0] = 0x67452301; this->state[0] = 0x67452301;
this->state[1] = 0xEFCDAB89; this->state[1] = 0xEFCDAB89;
@@ -244,15 +238,13 @@ static status_t reset(private_sha1_hasher_t *this)
this->state[4] = 0xC3D2E1F0; this->state[4] = 0xC3D2E1F0;
this->count[0] = 0; this->count[0] = 0;
this->count[1] = 0; this->count[1] = 0;
return SUCCESS;
} }
/** /**
* implementation of hasher_t.destroy for sha1 * implementation of hasher_t.destroy for sha1
*/ */
static status_t destroy(private_sha1_hasher_t *this) static void destroy(private_sha1_hasher_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
@@ -267,11 +259,11 @@ sha1_hasher_t *sha1_hasher_create()
return NULL; return NULL;
} }
this->public.hasher_interface.get_hash = (status_t (*) (hasher_t*, chunk_t, u_int8_t*))get_hash; this->public.hasher_interface.get_hash = (void (*) (hasher_t*, chunk_t, u_int8_t*))get_hash;
this->public.hasher_interface.allocate_hash = (status_t (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash; this->public.hasher_interface.allocate_hash = (void (*) (hasher_t*, chunk_t, chunk_t*))allocate_hash;
this->public.hasher_interface.get_block_size = (size_t (*) (hasher_t*))get_block_size; this->public.hasher_interface.get_block_size = (size_t (*) (hasher_t*))get_block_size;
this->public.hasher_interface.reset = (size_t (*) (hasher_t*))reset; this->public.hasher_interface.reset = (void (*) (hasher_t*))reset;
this->public.hasher_interface.destroy = (size_t (*) (hasher_t*))destroy; this->public.hasher_interface.destroy = (void (*) (hasher_t*))destroy;
/* initialize */ /* initialize */
this->public.hasher_interface.reset(&(this->public.hasher_interface)); this->public.hasher_interface.reset(&(this->public.hasher_interface));
@@ -45,9 +45,7 @@ struct sha1_hasher_t {
/** /**
* @brief Creates a new sha1_hasher_t. * @brief Creates a new sha1_hasher_t.
* *
* @return * @return sha1_hasher_t object
* - sha1_hasher_t if successfully
* - NULL if out of ressources
* *
* @ingroup hashers * @ingroup hashers
*/ */
+10 -39
View File
@@ -59,7 +59,7 @@ struct private_hmac_t {
/** /**
* Implementation of hmac_t.get_mac. * Implementation of hmac_t.get_mac.
*/ */
static status_t get_mac(private_hmac_t *this, chunk_t data, u_int8_t *out) static void get_mac(private_hmac_t *this, chunk_t data, u_int8_t *out)
{ {
/* H(K XOR opad, H(K XOR ipad, text)) /* H(K XOR opad, H(K XOR ipad, text))
* *
@@ -92,13 +92,12 @@ static status_t get_mac(private_hmac_t *this, chunk_t data, u_int8_t *out)
/* reinit for next call */ /* reinit for next call */
this->h->get_hash(this->h, this->ipaded_key, NULL); this->h->get_hash(this->h, this->ipaded_key, NULL);
} }
return SUCCESS;
} }
/** /**
* Implementation of hmac_t.allocate_mac. * Implementation of hmac_t.allocate_mac.
*/ */
static status_t allocate_mac(private_hmac_t *this, chunk_t data, chunk_t *out) static void allocate_mac(private_hmac_t *this, chunk_t data, chunk_t *out)
{ {
/* allocate space and use get_mac */ /* allocate space and use get_mac */
if (out == NULL) if (out == NULL)
@@ -110,13 +109,8 @@ static status_t allocate_mac(private_hmac_t *this, chunk_t data, chunk_t *out)
{ {
out->len = this->h->get_block_size(this->h); out->len = this->h->get_block_size(this->h);
out->ptr = allocator_alloc(out->len); out->ptr = allocator_alloc(out->len);
if (out->ptr == NULL)
{
return OUT_OF_RES;
}
this->hmac.get_mac(&(this->hmac), data, out->ptr); this->hmac.get_mac(&(this->hmac), data, out->ptr);
} }
return SUCCESS;
} }
/** /**
@@ -130,7 +124,7 @@ static size_t get_block_size(private_hmac_t *this)
/** /**
* Implementation of hmac_t.set_key. * Implementation of hmac_t.set_key.
*/ */
static status_t set_key(private_hmac_t *this, chunk_t key) static void set_key(private_hmac_t *this, chunk_t key)
{ {
int i; int i;
u_int8_t buffer[this->b]; u_int8_t buffer[this->b];
@@ -158,20 +152,17 @@ static status_t set_key(private_hmac_t *this, chunk_t key)
/* begin hashing of inner pad */ /* begin hashing of inner pad */
this->h->reset(this->h); this->h->reset(this->h);
this->h->get_hash(this->h, this->ipaded_key, NULL); this->h->get_hash(this->h, this->ipaded_key, NULL);
return SUCCESS;;
} }
/** /**
* Implementation of hmac_t.destroy. * Implementation of hmac_t.destroy.
*/ */
static status_t destroy(private_hmac_t *this) static void destroy(private_hmac_t *this)
{ {
this->h->destroy(this->h); this->h->destroy(this->h);
allocator_free(this->opaded_key.ptr); allocator_free(this->opaded_key.ptr);
allocator_free(this->ipaded_key.ptr); allocator_free(this->ipaded_key.ptr);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -182,16 +173,13 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
private_hmac_t *this; private_hmac_t *this;
this = allocator_alloc_thing(private_hmac_t); this = allocator_alloc_thing(private_hmac_t);
if (this == NULL)
{
return NULL;
}
/* set hmac_t methods */ /* set hmac_t methods */
this->hmac.get_mac = (size_t (*)(hmac_t *,chunk_t,u_int8_t*))get_mac; this->hmac.get_mac = (void (*)(hmac_t *,chunk_t,u_int8_t*))get_mac;
this->hmac.allocate_mac = (size_t (*)(hmac_t *,chunk_t,chunk_t*))allocate_mac; this->hmac.allocate_mac = (void (*)(hmac_t *,chunk_t,chunk_t*))allocate_mac;
this->hmac.get_block_size = (size_t (*)(hmac_t *))get_block_size; this->hmac.get_block_size = (size_t (*)(hmac_t *))get_block_size;
this->hmac.set_key = (status_t (*)(hmac_t *,chunk_t))set_key; this->hmac.set_key = (void (*)(hmac_t *,chunk_t))set_key;
this->hmac.destroy = (status_t (*)(hmac_t *))destroy; this->hmac.destroy = (void (*)(hmac_t *))destroy;
/* set b, according to hasher */ /* set b, according to hasher */
switch (hash_algorithm) switch (hash_algorithm)
@@ -207,30 +195,13 @@ hmac_t *hmac_create(hash_algorithm_t hash_algorithm)
/* build the hasher */ /* build the hasher */
this->h = hasher_create(hash_algorithm); this->h = hasher_create(hash_algorithm);
if (this->h == NULL)
{
allocator_free(this);
return NULL;
}
/* build ipad and opad */ /* build ipad and opad */
this->opaded_key.ptr = allocator_alloc(this->b); this->opaded_key.ptr = allocator_alloc(this->b);
this->opaded_key.len = this->b; this->opaded_key.len = this->b;
if (this->opaded_key.ptr == NULL)
{
this->h->destroy(this->h);
allocator_free(this);
return NULL;
}
this->ipaded_key.ptr = allocator_alloc(this->b); this->ipaded_key.ptr = allocator_alloc(this->b);
this->ipaded_key.len = this->b; this->ipaded_key.len = this->b;
if (this->ipaded_key.ptr == NULL)
{
this->h->destroy(this->h);
allocator_free(this->opaded_key.ptr);
allocator_free(this);
return NULL;
}
return &(this->hmac); return &(this->hmac);
} }
+5 -14
View File
@@ -52,10 +52,8 @@ struct hmac_t {
* @param this calling hmac * @param this calling hmac
* @param data chunk of data to authenticate * @param data chunk of data to authenticate
* @param[out] buffer pointer where the generated bytes will be written * @param[out] buffer pointer where the generated bytes will be written
* @return
* - SUCCESS in any case
*/ */
status_t (*get_mac) (hmac_t *this, chunk_t data, u_int8_t *buffer); void (*get_mac) (hmac_t *this, chunk_t data, u_int8_t *buffer);
/** /**
* @brief Generates message authentication code and * @brief Generates message authentication code and
@@ -69,11 +67,8 @@ struct hmac_t {
* @param this calling hmac * @param this calling hmac
* @param data chunk of data to authenticate * @param data chunk of data to authenticate
* @param[out] chunk chunk which will hold generated bytes * @param[out] chunk chunk which will hold generated bytes
* @return
* - SUCCESS, or
* - OUT_OF_RES if space could not be allocated
*/ */
status_t (*allocate_mac) (hmac_t *this, chunk_t data, chunk_t *chunk); void (*allocate_mac) (hmac_t *this, chunk_t data, chunk_t *chunk);
/** /**
* @brief Get the block size of this hmac. * @brief Get the block size of this hmac.
@@ -90,19 +85,15 @@ struct hmac_t {
* *
* @param this calling hmac * @param this calling hmac
* @param key key to set * @param key key to set
* @return
* - SUCCESS in any case
*/ */
status_t (*set_key) (hmac_t *this, chunk_t key); void (*set_key) (hmac_t *this, chunk_t key);
/** /**
* @brief Destroys a hmac object. * @brief Destroys a hmac object.
* *
* @param this hmac_t object to destroy * @param this hmac_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (hmac_t *this); void (*destroy) (hmac_t *this);
}; };
/** /**
@@ -114,7 +105,7 @@ struct hmac_t {
* @param hash_algorithm hash algorithm to use * @param hash_algorithm hash algorithm to use
* @return * @return
* - hmac_t if successfully * - hmac_t if successfully
* - NULL if out of ressources or hash not supported * - NULL if hash not supported
* *
* @ingroup transforms * @ingroup transforms
*/ */
+10 -29
View File
@@ -68,7 +68,7 @@ struct private_prf_plus_t {
/** /**
* implementation of prf_plus_t.get_bytes * implementation of prf_plus_t.get_bytes
*/ */
static status_t get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer) static void get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buffer)
{ {
chunk_t appending_chunk; chunk_t appending_chunk;
size_t bytes_in_round; size_t bytes_in_round;
@@ -96,32 +96,26 @@ static status_t get_bytes(private_prf_plus_t *this, size_t length, u_int8_t *buf
this->given_out += bytes_in_round; this->given_out += bytes_in_round;
total_bytes_written += bytes_in_round; total_bytes_written += bytes_in_round;
} }
return SUCCESS;
} }
/** /**
* implementation of prf_plus_t.allocate_bytes * implementation of prf_plus_t.allocate_bytes
*/ */
static status_t allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk) static void allocate_bytes(private_prf_plus_t *this, size_t length, chunk_t *chunk)
{ {
chunk->ptr = allocator_alloc(length); chunk->ptr = allocator_alloc(length);
chunk->len = length; chunk->len = length;
if (chunk->ptr == NULL) this->public.get_bytes(&(this->public), length, chunk->ptr);
{
return OUT_OF_RES;
}
return this->public.get_bytes(&(this->public), length, chunk->ptr);
} }
/** /**
* implementation of prf_plus_t.destroy * implementation of prf_plus_t.destroy
*/ */
static status_t destroy(private_prf_plus_t *this) static void destroy(private_prf_plus_t *this)
{ {
allocator_free(this->buffer.ptr); allocator_free(this->buffer.ptr);
allocator_free(this->seed.ptr); allocator_free(this->seed.ptr);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -133,14 +127,11 @@ prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
chunk_t appending_chunk; chunk_t appending_chunk;
this = allocator_alloc_thing(private_prf_plus_t); this = allocator_alloc_thing(private_prf_plus_t);
if (this == NULL)
{
return NULL;
}
/* set public methods */ /* set public methods */
this->public.get_bytes = (size_t (*)(prf_plus_t *,size_t,u_int8_t*))get_bytes; this->public.get_bytes = (void (*)(prf_plus_t *,size_t,u_int8_t*))get_bytes;
this->public.allocate_bytes = (size_t (*)(prf_plus_t *,size_t,chunk_t*))allocate_bytes; this->public.allocate_bytes = (void (*)(prf_plus_t *,size_t,chunk_t*))allocate_bytes;
this->public.destroy = (status_t (*)(prf_plus_t *))destroy; this->public.destroy = (void (*)(prf_plus_t *))destroy;
/* take over prf */ /* take over prf */
this->prf = prf; this->prf = prf;
@@ -148,23 +139,13 @@ prf_plus_t *prf_plus_create(prf_t *prf, chunk_t seed)
/* allocate buffer for prf output */ /* allocate buffer for prf output */
this->buffer.len = prf->get_block_size(prf); this->buffer.len = prf->get_block_size(prf);
this->buffer.ptr = allocator_alloc(this->buffer.len); this->buffer.ptr = allocator_alloc(this->buffer.len);
if (this->buffer.ptr == NULL)
{
allocator_free(this);
return NULL;
}
this->appending_octet = 0x01; this->appending_octet = 0x01;
/* clone seed */ /* clone seed */
this->seed.ptr = allocator_clone_bytes(seed.ptr, seed.len); this->seed.ptr = allocator_clone_bytes(seed.ptr, seed.len);
this->seed.len = seed.len; this->seed.len = seed.len;
if (this->seed.ptr == NULL)
{
allocator_free(this->buffer.ptr);
allocator_free(this);
return NULL;
}
/* do the first run */ /* do the first run */
appending_chunk.ptr = &(this->appending_octet); appending_chunk.ptr = &(this->appending_octet);
appending_chunk.len = 1; appending_chunk.len = 1;
+4 -13
View File
@@ -49,10 +49,8 @@ struct prf_plus_t {
* @param this calling prf_plus * @param this calling prf_plus
* @param length number of bytes to get * @param length number of bytes to get
* @param[out] buffer pointer where the generated bytes will be written * @param[out] buffer pointer where the generated bytes will be written
* @return
* - SUCCESS in any case
*/ */
status_t (*get_bytes) (prf_plus_t *this, size_t length, u_int8_t *buffer); void (*get_bytes) (prf_plus_t *this, size_t length, u_int8_t *buffer);
/** /**
* @brief Allocate pseudo random bytes. * @brief Allocate pseudo random bytes.
@@ -63,20 +61,15 @@ struct prf_plus_t {
* @param this calling prf_plus * @param this calling prf_plus
* @param length number of bytes to get * @param length number of bytes to get
* @param[out] chunk chunk which will hold generated bytes * @param[out] chunk chunk which will hold generated bytes
* @return
* - SUCCESS in any case
* - OUT_OF_RES if space could not be allocated
*/ */
status_t (*allocate_bytes) (prf_plus_t *this, size_t length, chunk_t *chunk); void (*allocate_bytes) (prf_plus_t *this, size_t length, chunk_t *chunk);
/** /**
* @brief Destroys a prf_plus_t object. * @brief Destroys a prf_plus_t object.
* *
* @param this prf_plus_t object to destroy * @param this prf_plus_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (prf_plus_t *this); void (*destroy) (prf_plus_t *this);
}; };
/** /**
@@ -88,9 +81,7 @@ struct prf_plus_t {
* *
* @param prf prf object to use * @param prf prf object to use
* @param seed input seed for prf * @param seed input seed for prf
* @return * @return created prf_plus_t
* - prf_plus_t if successfully
* - NULL if out of ressources
* *
* @ingroup transforms * @ingroup transforms
*/ */
+10 -17
View File
@@ -42,17 +42,17 @@ struct private_hmac_prf_t {
/** /**
* implementation of prf_t.get_bytes * implementation of prf_t.get_bytes
*/ */
static status_t get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer) static void get_bytes(private_hmac_prf_t *this, chunk_t seed, u_int8_t *buffer)
{ {
return this->hmac->get_mac(this->hmac, seed, buffer); this->hmac->get_mac(this->hmac, seed, buffer);
} }
/** /**
* implementation of prf_t.allocate_bytes * implementation of prf_t.allocate_bytes
*/ */
static status_t allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk) static void allocate_bytes(private_hmac_prf_t *this, chunk_t seed, chunk_t *chunk)
{ {
return this->hmac->allocate_mac(this->hmac, seed, chunk); this->hmac->allocate_mac(this->hmac, seed, chunk);
} }
/** /**
@@ -66,20 +66,18 @@ static size_t get_block_size(private_hmac_prf_t *this)
/** /**
* implementation of prf_t.set_key * implementation of prf_t.set_key
*/ */
static status_t set_key(private_hmac_prf_t *this, chunk_t key) static void set_key(private_hmac_prf_t *this, chunk_t key)
{ {
this->hmac->set_key(this->hmac, key); this->hmac->set_key(this->hmac, key);
return SUCCESS;
} }
/** /**
* implementation of prf_t.destroy * implementation of prf_t.destroy
*/ */
static status_t destroy(private_hmac_prf_t *this) static void destroy(private_hmac_prf_t *this)
{ {
allocator_free(this); allocator_free(this);
this->hmac->destroy(this->hmac); this->hmac->destroy(this->hmac);
return SUCCESS;
} }
/* /*
@@ -89,16 +87,11 @@ hmac_prf_t *hmac_prf_create(hash_algorithm_t hash_algorithm)
{ {
private_hmac_prf_t *this = allocator_alloc_thing(private_hmac_prf_t); private_hmac_prf_t *this = allocator_alloc_thing(private_hmac_prf_t);
if (this == NULL) this->public.prf_interface.get_bytes = (void (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
{ this->public.prf_interface.allocate_bytes = (void (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
return NULL;
}
this->public.prf_interface.get_bytes = (status_t (*) (prf_t *,chunk_t,u_int8_t*))get_bytes;
this->public.prf_interface.allocate_bytes = (status_t (*) (prf_t*,chunk_t,chunk_t*))allocate_bytes;
this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size; this->public.prf_interface.get_block_size = (size_t (*) (prf_t*))get_block_size;
this->public.prf_interface.set_key = (status_t (*) (prf_t *,chunk_t))set_key; this->public.prf_interface.set_key = (void (*) (prf_t *,chunk_t))set_key;
this->public.prf_interface.destroy = (status_t (*) (prf_t *))destroy; this->public.prf_interface.destroy = (void (*) (prf_t *))destroy;
this->hmac = hmac_create(hash_algorithm); this->hmac = hmac_create(hash_algorithm);
if (this->hmac == NULL) if (this->hmac == NULL)
+3 -3
View File
@@ -49,10 +49,10 @@ struct hmac_prf_t {
/** /**
* @brief Creates a new hmac_prf_t object * @brief Creates a new hmac_prf_t object
* *
* @param hash_algorithm hmac's hash algorithm * @param hash_algorithm hmac's hash algorithm
* @return * @return
* - hmac_prf_t if successfully * - hmac_prf_t if successfully
* - NULL if out of ressources * - NULL if hash not supported
* *
* @ingroup prfs * @ingroup prfs
*/ */
+5 -14
View File
@@ -59,10 +59,8 @@ struct prf_t {
* @param this calling prf * @param this calling prf
* @param seed a chunk containing the seed for the next bytes * @param seed a chunk containing the seed for the next bytes
* @param[out] buffer pointer where the generated bytes will be written * @param[out] buffer pointer where the generated bytes will be written
* @return
* - SUCCESS in any case
*/ */
status_t (*get_bytes) (prf_t *this, chunk_t seed, u_int8_t *buffer); void (*get_bytes) (prf_t *this, chunk_t seed, u_int8_t *buffer);
/** /**
* @brief generates pseudo random bytes and allocate space for them. * @brief generates pseudo random bytes and allocate space for them.
@@ -70,11 +68,8 @@ struct prf_t {
* @param this calling prf * @param this calling prf
* @param seed a chunk containing the seed for the next bytes * @param seed a chunk containing the seed for the next bytes
* @param[out] chunk chunk which will hold generated bytes * @param[out] chunk chunk which will hold generated bytes
* @return
* - SUCCESS in any case
* - OUT_OF_RES if space could not be allocated
*/ */
status_t (*allocate_bytes) (prf_t *this, chunk_t seed, chunk_t *chunk); void (*allocate_bytes) (prf_t *this, chunk_t seed, chunk_t *chunk);
/** /**
* @brief get the block size of this prf. * @brief get the block size of this prf.
@@ -89,19 +84,15 @@ struct prf_t {
* *
* @param this calling prf * @param this calling prf
* @param key key to set * @param key key to set
* @return
* - SUCCESS in any case
*/ */
status_t (*set_key) (prf_t *this, chunk_t key); void (*set_key) (prf_t *this, chunk_t key);
/** /**
* @brief Destroys a prf object.. * @brief Destroys a prf object..
* *
* @param this prf_t object to destroy * @param this prf_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (prf_t *this); void (*destroy) (prf_t *this);
}; };
/** /**
@@ -110,7 +101,7 @@ struct prf_t {
* @param pseudo_random_function Algorithm to use * @param pseudo_random_function Algorithm to use
* @return * @return
* - prf_t if successfully * - prf_t if successfully
* - NULL if out of ressources or prf not supported * - NULL if prf not supported
* *
* @ingroup prfs * @ingroup prfs
*/ */
+18 -56
View File
@@ -48,66 +48,42 @@ struct private_hmac_signer_t {
}; };
static status_t get_signature (private_hmac_signer_t *this, chunk_t data, u_int8_t *buffer) static void get_signature (private_hmac_signer_t *this, chunk_t data, u_int8_t *buffer)
{ {
u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)]; u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)];
status_t status;
status = this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac); this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac);
if (status != SUCCESS)
{
return status;
}
/* copy mac aka signature :-) */ /* copy mac aka signature :-) */
memcpy(buffer,full_mac,BLOCK_SIZE); memcpy(buffer,full_mac,BLOCK_SIZE);
return SUCCESS;
} }
static status_t allocate_signature (private_hmac_signer_t *this, chunk_t data, chunk_t *chunk) static void allocate_signature (private_hmac_signer_t *this, chunk_t data, chunk_t *chunk)
{ {
chunk_t signature; chunk_t signature;
status_t status;
u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)]; u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)];
status = this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac); this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac);
if (status != SUCCESS)
{
return status;
}
signature.ptr = allocator_alloc(BLOCK_SIZE); signature.ptr = allocator_alloc(BLOCK_SIZE);
if (signature.ptr == NULL)
{
return OUT_OF_RES;
}
signature.len = BLOCK_SIZE; signature.len = BLOCK_SIZE;
/* copy mac aka signature :-) */ /* copy mac aka signature :-) */
memcpy(signature.ptr,full_mac,BLOCK_SIZE); memcpy(signature.ptr,full_mac,BLOCK_SIZE);
*chunk = signature; *chunk = signature;
return SUCCESS;
} }
static status_t verify_signature (private_hmac_signer_t *this, chunk_t data, chunk_t signature, bool *valid) static void verify_signature (private_hmac_signer_t *this, chunk_t data, chunk_t signature, bool *valid)
{ {
status_t status;
u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)]; u_int8_t full_mac[this->hmac_prf->get_block_size(this->hmac_prf)];
status = this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac); this->hmac_prf->get_bytes(this->hmac_prf,data,full_mac);
if (status != SUCCESS)
{
return status;
}
if (signature.len != BLOCK_SIZE) if (signature.len != BLOCK_SIZE)
{ {
/* signature must have BLOCK_SIZE length */ *valid = FALSE;
return INVALID_ARG; return;
} }
/* compare mac aka signature :-) */ /* compare mac aka signature :-) */
@@ -119,8 +95,6 @@ static status_t verify_signature (private_hmac_signer_t *this, chunk_t data, chu
{ {
*valid = FALSE; *valid = FALSE;
} }
return SUCCESS;
} }
static size_t get_block_size (private_hmac_signer_t *this) static size_t get_block_size (private_hmac_signer_t *this)
@@ -128,9 +102,9 @@ static size_t get_block_size (private_hmac_signer_t *this)
return BLOCK_SIZE; return BLOCK_SIZE;
} }
static status_t set_key (private_hmac_signer_t *this, chunk_t key) static void set_key (private_hmac_signer_t *this, chunk_t key)
{ {
return (this->hmac_prf->set_key(this->hmac_prf,key)); this->hmac_prf->set_key(this->hmac_prf,key);
} }
/** /**
@@ -150,35 +124,23 @@ static status_t destroy(private_hmac_signer_t *this)
hmac_signer_t *hmac_signer_create(hash_algorithm_t hash_algoritm) hmac_signer_t *hmac_signer_create(hash_algorithm_t hash_algoritm)
{ {
private_hmac_signer_t *this = allocator_alloc_thing(private_hmac_signer_t); private_hmac_signer_t *this = allocator_alloc_thing(private_hmac_signer_t);
if (this == NULL)
{
return NULL;
}
this->hmac_prf = (prf_t *) hmac_prf_create(hash_algoritm); this->hmac_prf = (prf_t *) hmac_prf_create(hash_algoritm);
if (this->hmac_prf == NULL) if (this->hmac_prf == NULL)
{ {
/* hmac prf could not be created !!! */ /* algorithm not supported */
allocator_free(this); allocator_free(this);
return NULL; return NULL;
} }
if (this->hmac_prf->get_block_size(this->hmac_prf) < BLOCK_SIZE)
{
/* hmac prf with given algorithm has to small block size */
allocator_free(this);
return NULL;
}
/* interface functions */ /* interface functions */
this->public.signer_interface.get_signature = (status_t (*) (signer_t*, chunk_t, u_int8_t*))get_signature; this->public.signer_interface.get_signature = (void (*) (signer_t*, chunk_t, u_int8_t*))get_signature;
this->public.signer_interface.allocate_signature = (status_t (*) (signer_t*, chunk_t, chunk_t*))allocate_signature; this->public.signer_interface.allocate_signature = (void (*) (signer_t*, chunk_t, chunk_t*))allocate_signature;
this->public.signer_interface.verify_signature = (status_t (*) (signer_t*, chunk_t, chunk_t,bool *))verify_signature; this->public.signer_interface.verify_signature = (void (*) (signer_t*, chunk_t, chunk_t,bool *))verify_signature;
this->public.signer_interface.get_block_size = (size_t (*) (signer_t*))get_block_size; this->public.signer_interface.get_block_size = (size_t (*) (signer_t*))get_block_size;
this->public.signer_interface.set_key = (size_t (*) (signer_t*,chunk_t))set_key; this->public.signer_interface.set_key = (void (*) (signer_t*,chunk_t))set_key;
this->public.signer_interface.destroy = (status_t (*) (signer_t*))destroy; this->public.signer_interface.destroy = (void (*) (signer_t*))destroy;
return &(this->public); return &(this->public);
} }
@@ -20,8 +20,8 @@
* for more details. * for more details.
*/ */
#ifndef _HMAC_SIGNER_H_ #ifndef HMAC_SIGNER_H_
#define _HMAC_SIGNER_H_ #define HMAC_SIGNER_H_
#include <transforms/signers/signer.h> #include <transforms/signers/signer.h>
#include <transforms/hashers/hasher.h> #include <transforms/hashers/hasher.h>
@@ -30,7 +30,7 @@ typedef struct hmac_signer_t hmac_signer_t;
/** /**
* @brief Implementation of hmac_signer_t interface using the * @brief Implementation of hmac_signer_t interface using the
* HMAC algorithm in combination with eather MD5 or SHA1. * HMAC algorithm in combination with either MD5 or SHA1.
* *
* @ingroup signers * @ingroup signers
*/ */
@@ -45,15 +45,14 @@ struct hmac_signer_t {
/** /**
* @brief Creates a new hmac_signer_t. * @brief Creates a new hmac_signer_t.
* *
* @param hash_algorithm Hash algorithm to use with signer * @param hash_algorithm Hash algorithm to use with signer
* * @return
* @return * - hmac_signer_t
* - hmac_signer_t if successfully * - NULL if hash not supported
* - NULL if out of ressources
* *
* @ingroup signers * @ingroup signers
*/ */
hmac_signer_t *hmac_signer_create(hash_algorithm_t hash_algoritm); hmac_signer_t *hmac_signer_create(hash_algorithm_t hash_algoritm);
#endif //_HMAC_SIGNER_H_ #endif /*HMAC_SIGNER_H_*/
+4 -1
View File
@@ -37,6 +37,10 @@ mapping_t integrity_algorithm_m[] = {
{MAPPING_END, NULL} {MAPPING_END, NULL}
}; };
/*
* see header
*/
signer_t *signer_create(integrity_algorithm_t integrity_algorithm) signer_t *signer_create(integrity_algorithm_t integrity_algorithm)
{ {
switch(integrity_algorithm) switch(integrity_algorithm)
@@ -49,7 +53,6 @@ signer_t *signer_create(integrity_algorithm_t integrity_algorithm)
{ {
return ((signer_t *) hmac_signer_create(HASH_MD5)); return ((signer_t *) hmac_signer_create(HASH_MD5));
} }
default: default:
return NULL; return NULL;
} }
+8 -19
View File
@@ -61,10 +61,8 @@ struct signer_t {
* @param this calling signer * @param this calling signer
* @param data a chunk containing the data to sign * @param data a chunk containing the data to sign
* @param[out] buffer pointer where the signature will be written * @param[out] buffer pointer where the signature will be written
* @return
* - SUCCESS in any case
*/ */
status_t (*get_signature) (signer_t *this, chunk_t data, u_int8_t *buffer); void (*get_signature) (signer_t *this, chunk_t data, u_int8_t *buffer);
/** /**
* @brief Generate a signature and allocate space for it. * @brief Generate a signature and allocate space for it.
@@ -72,11 +70,8 @@ struct signer_t {
* @param this calling signer * @param this calling signer
* @param data a chunk containing the data to sign * @param data a chunk containing the data to sign
* @param[out] chunk chunk which will hold the allocated signature * @param[out] chunk chunk which will hold the allocated signature
* @return
* - SUCCESS in any case
* - OUT_OF_RES if space could not be allocated
*/ */
status_t (*allocate_signature) (signer_t *this, chunk_t data, chunk_t *chunk); void (*allocate_signature) (signer_t *this, chunk_t data, chunk_t *chunk);
/** /**
* @brief Verify a signature. * @brief Verify a signature.
@@ -85,10 +80,8 @@ struct signer_t {
* @param data a chunk containing the data to verify * @param data a chunk containing the data to verify
* @param signature a chunk containing the signature * @param signature a chunk containing the signature
* @param[out] vaild set to TRUE, if signature is valid, to FALSE otherwise * @param[out] vaild set to TRUE, if signature is valid, to FALSE otherwise
* @return
* - SUCCESS in any case
*/ */
status_t (*verify_signature) (signer_t *this, chunk_t data, chunk_t signature, bool *valid); void (*verify_signature) (signer_t *this, chunk_t data, chunk_t signature, bool *valid);
/** /**
* @brief Get the block size of this signature algorithm. * @brief Get the block size of this signature algorithm.
@@ -103,19 +96,15 @@ struct signer_t {
* *
* @param this calling signer * @param this calling signer
* @param key key to set * @param key key to set
* @return
* - SUCCESS in any case
*/ */
status_t (*set_key) (signer_t *this, chunk_t key); void (*set_key) (signer_t *this, chunk_t key);
/** /**
* @brief Destroys a signer object. * @brief Destroys a signer object.
* *
* @param this signer_t object to destroy * @param this signer_t object to destroy
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (signer_t *this); void (*destroy) (signer_t *this);
}; };
/** /**
@@ -123,8 +112,8 @@ struct signer_t {
* *
* @param integrity_algorithm Algorithm to use for signing and verifying. * @param integrity_algorithm Algorithm to use for signing and verifying.
* @return * @return
* - signer_t if successfully * - signer_t if successfully,
* - NULL if out of ressources or signer not supported * - NULL if signer not supported
* *
* @ingroup signers * @ingroup signers
*/ */
+55 -164
View File
@@ -44,7 +44,6 @@ struct private_gmp_helper_t {
* Public gmp_helper_t interface. * Public gmp_helper_t interface.
*/ */
gmp_helper_t public; gmp_helper_t public;
}; };
@@ -53,186 +52,83 @@ struct private_gmp_helper_t {
*/ */
static void chunk_to_mpz(private_gmp_helper_t *this, mpz_t *mpz_value, chunk_t data) static void chunk_to_mpz(private_gmp_helper_t *this, mpz_t *mpz_value, chunk_t data)
{ {
size_t i; size_t i;
mpz_init_set_ui(*(mpz_value), 0); mpz_init_set_ui(*(mpz_value), 0);
for (i = 0; i < data.len; i++) for (i = 0; i < data.len; i++)
{ {
mpz_mul_ui(*(mpz_value),*(mpz_value), 1 << 8); mpz_mul_ui(*(mpz_value),*(mpz_value), 1 << 8);
mpz_add_ui(*(mpz_value),*(mpz_value), data.ptr[i]); mpz_add_ui(*(mpz_value),*(mpz_value), data.ptr[i]);
} }
} }
/** /**
* Implementation of gmp_helper_t.mpz_to_chunk. * Implementation of gmp_helper_t.mpz_to_chunk.
*/ */
static status_t mpz_to_chunk (private_gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes) static void mpz_to_chunk (private_gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes)
{ {
mpz_t temp1, temp2; mpz_t temp1, temp2;
status_t status = SUCCESS; int i;
int i; chunk_t tmp_chunk;
chunk_t tmp_chunk;
tmp_chunk.len = bytes;
tmp_chunk.len = bytes; tmp_chunk.ptr = allocator_alloc(tmp_chunk.len);
tmp_chunk.ptr = allocator_alloc(tmp_chunk.len);
memset(tmp_chunk.ptr,0,tmp_chunk.len);
if (tmp_chunk.ptr == NULL)
{ mpz_init(temp1);
allocator_free_chunk(&tmp_chunk); mpz_init(temp2);
return OUT_OF_RES;
} mpz_set(temp1, *mpz_value);
/* free memory */ for (i = tmp_chunk.len-1; i >= 0; i--)
memset(tmp_chunk.ptr,0,tmp_chunk.len); {
mpz_init(temp1);
mpz_init(temp2);
mpz_set(temp1, *mpz_value);
for (i = tmp_chunk.len-1; i >= 0; i--)
{
tmp_chunk.ptr[i] = mpz_mdivmod_ui(temp2, NULL, temp1, 1 << 8); tmp_chunk.ptr[i] = mpz_mdivmod_ui(temp2, NULL, temp1, 1 << 8);
mpz_set(temp1, temp2); mpz_set(temp1, temp2);
}
if (mpz_sgn(temp1) != 0)
{
fprintf (stderr,"value %d\n",mpz_sgn(temp1));
status = FAILED;
}
mpz_clear(temp1);
mpz_clear(temp2);
*data = tmp_chunk;
if (status != SUCCESS)
{
allocator_free_chunk(&tmp_chunk);
} }
return status;
mpz_clear(temp1);
mpz_clear(temp2);
*data = tmp_chunk;
} }
/** /**
* Implementation of gmp_helper_t.init_prime. * Implementation of gmp_helper_t.init_prime.
*/ */
static status_t init_prime (private_gmp_helper_t *this, mpz_t *prime, int bytes) static void init_prime (private_gmp_helper_t *this, mpz_t *prime, int bytes)
{ {
randomizer_t *randomizer; randomizer_t *randomizer;
chunk_t random_bytes; chunk_t random_bytes;
status_t status; randomizer = randomizer_create();
randomizer = randomizer_create();
/* TODO change to true random device ? */
if (randomizer == NULL) //randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
{ randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes);
return OUT_OF_RES;
} /* make sure most significant bit is set */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
/* TODO change to true random device ? */
//status = randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes); /* not needed anymore */
status = randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes); randomizer->destroy(randomizer);
/* make sure most significant bit is set */ /* convert chunk to mpz value */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80; this->public.chunk_to_mpz(&(this->public),prime, random_bytes);
/* chunk is not used anymore */
/* not needed anymore */ allocator_free(random_bytes.ptr);
randomizer->destroy(randomizer); random_bytes.ptr = NULL;
/* convert chunk to mpz value */
this->public.chunk_to_mpz(&(this->public),prime, random_bytes);
/* chunk is not used anymore */
allocator_free(random_bytes.ptr);
random_bytes.ptr = NULL;
/* composites are possible but should never occur */ /* composites are possible but should never occur */
mpz_nextprime (*(prime),*(prime)); mpz_nextprime (*(prime),*(prime));
return SUCCESS;
} }
/**
* Implementation of gmp_helper_t.init_prime_fast.
*/
static status_t init_prime_fast (private_gmp_helper_t *this, mpz_t *prime, int bytes){
randomizer_t *randomizer;
chunk_t random_bytes;
status_t status;
unsigned long tries;
size_t length;
randomizer = randomizer_create();
if (randomizer == NULL)
{
return OUT_OF_RES;
}
/* TODO change to true random device ? */
//status = randomizer->allocate_random_bytes(randomizer,bytes, &random_bytes);
status = randomizer->allocate_pseudo_random_bytes(randomizer,bytes, &random_bytes);
/* make sure most significant bit is set */
random_bytes.ptr[0] = random_bytes.ptr[0] | 0x80;
/* not needed anymore */
randomizer->destroy(randomizer);
/* convert chunk to mpz value */
this->public.chunk_to_mpz(&(this->public),prime, random_bytes);
/* chunk is not used anymore */
allocator_free(random_bytes.ptr);
random_bytes.ptr = NULL;
/* make value odd */
if (mpz_fdiv_ui(*prime, 2) != 1)
{
/* make value odd */
mpz_add_ui(*prime,*prime,1);
}
tries = 1;
/* starting find a prime */
while (!mpz_probab_prime_p(*prime, PRIMECHECK_ROUNDS))
{
/* not a prime, increase by 2 */
mpz_add_ui(*prime, *prime, 2);
tries++;
}
length = mpz_sizeinbase(*prime, 2);
/* check bit length of prime */
if ((length < (bytes * 8)) || (length > ((bytes * 8) + 1)))
{
return FAILED;
}
if (length == ((bytes * 8) + 1))
{
/* carry out occured! retry */
mpz_clear(*prime);
/* recursive call */
return this->public.init_prime_fast(&(this->public),prime, bytes);
}
return SUCCESS;
}
/** /**
* Implementation of gmp_helper_t.destroy. * Implementation of gmp_helper_t.destroy.
*/ */
static status_t destroy(private_gmp_helper_t *this) static void destroy(private_gmp_helper_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -241,19 +137,14 @@ static status_t destroy(private_gmp_helper_t *this)
gmp_helper_t *gmp_helper_create() gmp_helper_t *gmp_helper_create()
{ {
private_gmp_helper_t *this = allocator_alloc_thing(private_gmp_helper_t); private_gmp_helper_t *this = allocator_alloc_thing(private_gmp_helper_t);
if ((this == NULL))
{
return NULL;
}
/* public functions */ /* public functions */
this->public.destroy = (status_t (*)(gmp_helper_t *)) destroy; this->public.destroy = (void (*)(gmp_helper_t *)) destroy;
this->public.init_prime = (status_t (*) (gmp_helper_t *, mpz_t *, int)) init_prime; this->public.init_prime = (void (*) (gmp_helper_t *, mpz_t *, int)) init_prime;
this->public.init_prime_fast = (status_t (*) (gmp_helper_t *, mpz_t *, int)) init_prime_fast;
/* private functions */ /* private functions */
this->public.chunk_to_mpz = (void (*) (gmp_helper_t *,mpz_t *, chunk_t )) chunk_to_mpz; this->public.chunk_to_mpz = (void (*) (gmp_helper_t *,mpz_t *, chunk_t )) chunk_to_mpz;
this->public.mpz_to_chunk = (status_t (*) (gmp_helper_t *,mpz_t *, chunk_t *,size_t )) mpz_to_chunk; this->public.mpz_to_chunk = (void (*) (gmp_helper_t *,mpz_t *, chunk_t *,size_t )) mpz_to_chunk;
return &(this->public); return &(this->public);
} }
+6 -31
View File
@@ -47,26 +47,8 @@ struct gmp_helper_t {
* @param this calling object * @param this calling object
* @param[out] var pointer to mpz_t variable to initialize * @param[out] var pointer to mpz_t variable to initialize
* @param[in] bytes length of given prime in bytes * @param[in] bytes length of given prime in bytes
* @return
* - SUCCCESS
* - OUT_OF_RES
*/ */
status_t (*init_prime) (gmp_helper_t *this, mpz_t *var, int bytes); void (*init_prime) (gmp_helper_t *this, mpz_t *var, int bytes);
/**
* Initialize an mpz_t to a random prime of specified size without using gmp
* next prime function.
*
*
* @param this calling object
* @param[out] var mpz_t variable to initialize
* @param[in] bytes length of given prime in bytes
* @return
* - SUCCCESS
* - FAILED if length of prime not as asked. Try again.
* - OUT_OF_RES
*/
status_t (*init_prime_fast) (gmp_helper_t *this, mpz_t *prime, int bytes);
/** /**
* Convert network form (binary bytes, big-endian) to mpz_t of gmp library. * Convert network form (binary bytes, big-endian) to mpz_t of gmp library.
@@ -74,7 +56,7 @@ struct gmp_helper_t {
* The given mpz_t gets initialized in this function. * The given mpz_t gets initialized in this function.
* *
* @param this calling private_gmp_helper_t object * @param this calling private_gmp_helper_t object
* @param mpz_value pointer to a mpz_t value * @param mpz_value pointer to a mpz_t value
* @param data chunk_t containing the network form of data * @param data chunk_t containing the network form of data
*/ */
void (*chunk_to_mpz) (gmp_helper_t *this,mpz_t *mpz_value, chunk_t data); void (*chunk_to_mpz) (gmp_helper_t *this,mpz_t *mpz_value, chunk_t data);
@@ -83,16 +65,11 @@ struct gmp_helper_t {
* Convert mpz_t to network form (binary bytes, big-endian). * Convert mpz_t to network form (binary bytes, big-endian).
* *
* @param this calling private_gmp_helper_t object * @param this calling private_gmp_helper_t object
* @param mpz_value mpz_value to convert * @param mpz_value mpz_value to convert
* @param data chunk_t where the data are written to * @param data chunk_t where the data are written to
* @param bytes number of bytes to copy * @param bytes number of bytes to copy
*
* @return
* - SUCCESS
* - OUT_OF_RES
* - FAILED if mpz_t value was longer then given bytes count
*/ */
status_t (*mpz_to_chunk) (gmp_helper_t *this,mpz_t *mpz_value, chunk_t *data,size_t bytes); void (*mpz_to_chunk) (gmp_helper_t *this, mpz_t *mpz_value, chunk_t *data, size_t bytes);
/** /**
* @brief Destroys an gmp_helper_t object. * @brief Destroys an gmp_helper_t object.
@@ -100,15 +77,13 @@ struct gmp_helper_t {
* @param this gmp_helper_t object to destroy * @param this gmp_helper_t object to destroy
* @return SUCCESS in any case * @return SUCCESS in any case
*/ */
status_t (*destroy) (gmp_helper_t *this); void (*destroy) (gmp_helper_t *this);
}; };
/** /**
* Creates a new gmp_helper_t object * Creates a new gmp_helper_t object
* *
* @return * @return gmp_helper_t object
* - gmp_helper_t object
* - NULL if out of ressources
* *
* @ingroup utils * @ingroup utils
*/ */
+7 -13
View File
@@ -52,7 +52,7 @@ struct iterator_t {
* @param[out] value value is set to the current value at iterator position * @param[out] value value is set to the current value at iterator position
* @return * @return
* - SUCCESS * - SUCCESS
* - FAILED if list is empty * - FAILED if iterator on an invalid position
*/ */
status_t (*current) (iterator_t *this, void **value); status_t (*current) (iterator_t *this, void **value);
@@ -63,24 +63,18 @@ struct iterator_t {
* *
* @param this calling iterator * @param this calling iterator
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @return
* - SUCCESS
* - FAILED
*/ */
status_t (*insert_before) (iterator_t *this, void *item); void (*insert_before) (iterator_t *this, void *item);
/** /**
* Inserts a new item after the given iterator position. * @brief Inserts a new item after the given iterator position.
* *
* The iterator position is not changed after inserting. * The iterator position is not changed after inserting.
* *
* @param this calling iterator * @param this calling iterator
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @return
* - SUCCESS
* - FAILED
*/ */
status_t (*insert_after) (iterator_t *this, void *item); void (*insert_after) (iterator_t *this, void *item);
/** /**
* @brief removes an element from list at the given iterator position. * @brief removes an element from list at the given iterator position.
@@ -93,7 +87,7 @@ struct iterator_t {
* @param linked_list calling object * @param linked_list calling object
* @return * @return
* - SUCCESS * - SUCCESS
* - FAILED * - FAILED if iterator is on an invalid position
*/ */
status_t (*remove) (iterator_t *iterator); status_t (*remove) (iterator_t *iterator);
@@ -107,7 +101,7 @@ struct iterator_t {
* @param this calling object * @param this calling object
* @return SUCCESS in any case * @return SUCCESS in any case
*/ */
status_t (*reset) (iterator_t *this); void (*reset) (iterator_t *this);
/** /**
* @brief Destroys an iterator. * @brief Destroys an iterator.
@@ -116,7 +110,7 @@ struct iterator_t {
* @return SUCCESS in any case * @return SUCCESS in any case
* *
*/ */
status_t (*destroy) (iterator_t *this); void (*destroy) (iterator_t *this);
}; };
#endif /*ITERATOR_H_*/ #endif /*ITERATOR_H_*/
+40 -105
View File
@@ -40,33 +40,32 @@ struct linked_list_element_t {
*/ */
void *value; void *value;
/**
* Destroys a linked_list_element object.
*
* @param linked_list_element_t calling object
* @returns SUCCESS in any case
*/
status_t (*destroy) (linked_list_element_t *this);
/** /**
* previous list element * previous list element
* NULL if first element in list * NULL if first element in list
*/ */
linked_list_element_t *previous; linked_list_element_t *previous;
/** /**
* next list element * next list element
* NULL if last element in list * NULL if last element in list
*/ */
linked_list_element_t *next; linked_list_element_t *next;
/**
* Destroys a linked_list_element object.
*
* @param linked_list_element_t calling object
*/
void (*destroy) (linked_list_element_t *this);
}; };
/** /**
* Implementation of linked_list_element_t.destroy. * Implementation of linked_list_element_t.destroy.
*/ */
static status_t linked_list_element_destroy(linked_list_element_t *this) static void linked_list_element_destroy(linked_list_element_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/** /**
@@ -74,21 +73,14 @@ static status_t linked_list_element_destroy(linked_list_element_t *this)
* *
* @warning Only the pointer to the value is stored. * @warning Only the pointer to the value is stored.
* *
* @param[in] value value of item to be set * @param[in] value value of item to be set
* @return * @return linked_list_element_t object
* - linked_list_element_t object
* - NULL if out of ressources
*/ */
linked_list_element_t *linked_list_element_create(void *value) linked_list_element_t *linked_list_element_create(void *value)
{ {
linked_list_element_t *this = allocator_alloc_thing(linked_list_element_t); linked_list_element_t *this = allocator_alloc_thing(linked_list_element_t);
if (this == NULL)
{
return NULL;
}
this->destroy = linked_list_element_destroy; this->destroy = linked_list_element_destroy;
this->previous=NULL; this->previous=NULL;
@@ -120,6 +112,7 @@ struct private_linked_list_t {
* NULL if no elements in list. * NULL if no elements in list.
*/ */
linked_list_element_t *first; linked_list_element_t *first;
/** /**
* Last element in list. * Last element in list.
* NULL if no elements in list. * NULL if no elements in list.
@@ -132,7 +125,6 @@ typedef struct private_iterator_t private_iterator_t;
/** /**
* Private variables and functions of linked list iterator. * Private variables and functions of linked list iterator.
*
*/ */
struct private_iterator_t { struct private_iterator_t {
/** /**
@@ -204,10 +196,9 @@ static status_t iterator_current(private_iterator_t *this, void **value)
/** /**
* Implementation of iterator_t.reset. * Implementation of iterator_t.reset.
*/ */
static status_t iterator_reset(private_iterator_t *this) static void iterator_reset(private_iterator_t *this)
{ {
this->current = NULL; this->current = NULL;
return SUCCESS;
} }
/** /**
@@ -275,28 +266,17 @@ static status_t remove(private_iterator_t *this)
/** /**
* Implementation of iterator_t.insert_before. * Implementation of iterator_t.insert_before.
*/ */
static status_t insert_before(private_iterator_t * iterator, void *item) static void insert_before(private_iterator_t * iterator, void *item)
{ {
if (iterator->current == NULL) if (iterator->current == NULL)
{ {
return (iterator->list->public.insert_first(&(iterator->list->public), item)); iterator->list->public.insert_first(&(iterator->list->public), item);
} }
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item); linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL)
{
return OUT_OF_RES;
}
if (iterator->current->previous == NULL) if (iterator->current->previous == NULL)
{ {
if (iterator->list->first != iterator->current)
{
element->destroy(element);
return FAILED;
}
iterator->current->previous = element; iterator->current->previous = element;
element->next = iterator->current; element->next = iterator->current;
iterator->list->first = element; iterator->list->first = element;
@@ -310,35 +290,22 @@ static status_t insert_before(private_iterator_t * iterator, void *item)
} }
iterator->list->count++; iterator->list->count++;
return SUCCESS;
} }
/** /**
* Implementation of iterator_t.insert_after. * Implementation of iterator_t.insert_after.
*/ */
static status_t insert_after(private_iterator_t * iterator, void *item) static void insert_after(private_iterator_t * iterator, void *item)
{ {
if (iterator->current == NULL) if (iterator->current == NULL)
{ {
return (iterator->list->public.insert_first(&(iterator->list->public),item)); iterator->list->public.insert_first(&(iterator->list->public),item);
} }
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item); linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL)
{
return OUT_OF_RES;
}
if (iterator->current->next == NULL) if (iterator->current->next == NULL)
{ {
if (iterator->list->last != iterator->current)
{
element->destroy(element);
return FAILED;
}
iterator->current->next = element; iterator->current->next = element;
element->previous = iterator->current; element->previous = iterator->current;
iterator->list->last = element; iterator->list->last = element;
@@ -350,18 +317,15 @@ static status_t insert_after(private_iterator_t * iterator, void *item)
iterator->current->next = element; iterator->current->next = element;
element->previous = iterator->current; element->previous = iterator->current;
} }
iterator->list->count++; iterator->list->count++;
return SUCCESS;
} }
/** /**
* Implementation of iterator_t.destroy. * Implementation of iterator_t.destroy.
*/ */
static status_t iterator_destroy(private_iterator_t *this) static void iterator_destroy(private_iterator_t *this)
{ {
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/** /**
@@ -376,17 +340,12 @@ static int get_count(private_linked_list_t *this)
/** /**
* Implementation of linked_list_t.insert_first. * Implementation of linked_list_t.insert_first.
*/ */
static status_t insert_first(private_linked_list_t *this, void *item) static void insert_first(private_linked_list_t *this, void *item)
{ {
linked_list_element_t *element; linked_list_element_t *element;
element =(linked_list_element_t *) linked_list_element_create(item); element =(linked_list_element_t *) linked_list_element_create(item);
if (element == NULL)
{
return OUT_OF_RES;
}
if (this->count == 0) if (this->count == 0)
{ {
/* first entry in list */ /* first entry in list */
@@ -397,12 +356,6 @@ static status_t insert_first(private_linked_list_t *this, void *item)
} }
else else
{ {
if ((this->first == NULL) || (this->last == NULL))
{
/* should never happen */
element->destroy(element);
return FAILED;
}
linked_list_element_t *old_first_element = this->first; linked_list_element_t *old_first_element = this->first;
element->next = old_first_element; element->next = old_first_element;
element->previous = NULL; element->previous = NULL;
@@ -411,8 +364,6 @@ static status_t insert_first(private_linked_list_t *this, void *item)
} }
this->count++; this->count++;
return SUCCESS;
} }
/** /**
@@ -437,7 +388,9 @@ static status_t remove_first(private_linked_list_t *this, void **item)
this->count--; this->count--;
return (element->destroy(element)); element->destroy(element);
return SUCCESS;
} }
/** /**
@@ -458,15 +411,10 @@ static status_t get_first(private_linked_list_t *this, void **item)
/** /**
* Implementation of linked_list_t.insert_last. * Implementation of linked_list_t.insert_last.
*/ */
static status_t insert_last(private_linked_list_t *this, void *item) static void insert_last(private_linked_list_t *this, void *item)
{ {
linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item); linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item);
if (element == NULL)
{
return OUT_OF_RES;
}
if (this->count == 0) if (this->count == 0)
{ {
/* first entry in list */ /* first entry in list */
@@ -474,14 +422,10 @@ static status_t insert_last(private_linked_list_t *this, void *item)
this->last = element; this->last = element;
element->previous = NULL; element->previous = NULL;
element->next = NULL; element->next = NULL;
}else }
else
{ {
if ((this->first == NULL) || (this->last == NULL))
{
/* should never happen */
element->destroy(element);
return FAILED;
}
linked_list_element_t *old_last_element = this->last; linked_list_element_t *old_last_element = this->last;
element->previous = old_last_element; element->previous = old_last_element;
element->next = NULL; element->next = NULL;
@@ -490,8 +434,6 @@ static status_t insert_last(private_linked_list_t *this, void *item)
} }
this->count++; this->count++;
return SUCCESS;
} }
/** /**
@@ -516,7 +458,9 @@ static status_t remove_last(private_linked_list_t *this, void **item)
this->count--; this->count--;
return (element->destroy(element)); element->destroy(element);
return SUCCESS;
} }
/** /**
@@ -537,48 +481,39 @@ static status_t get_last(private_linked_list_t *this, void **item)
/** /**
* Implementation of linked_list_t.create_iterator. * Implementation of linked_list_t.create_iterator.
*/ */
static status_t create_iterator (private_linked_list_t *linked_list, iterator_t **iterator,bool forward) static void create_iterator (private_linked_list_t *linked_list, iterator_t **iterator,bool forward)
{ {
private_iterator_t *this = allocator_alloc_thing(private_iterator_t); private_iterator_t *this = allocator_alloc_thing(private_iterator_t);
if (this == NULL)
{
return OUT_OF_RES;
}
this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next; this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current; this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current;
this->public.insert_before = (status_t (*) (iterator_t *this, void *item)) insert_before; this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before;
this->public.insert_after = (status_t (*) (iterator_t *this, void *item)) insert_after; this->public.insert_after = (void (*) (iterator_t *this, void *item)) insert_after;
this->public.remove = (status_t (*) (iterator_t *this)) remove; this->public.remove = (status_t (*) (iterator_t *this)) remove;
this->public.reset = (status_t (*) (iterator_t *this)) iterator_reset; this->public.reset = (void (*) (iterator_t *this)) iterator_reset;
this->public.destroy = (status_t (*) (iterator_t *this)) iterator_destroy; this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy;
this->forward = forward; this->forward = forward;
this->current = NULL; this->current = NULL;
this->list = linked_list; this->list = linked_list;
*iterator = &(this->public); *iterator = &(this->public);
return (SUCCESS);
} }
/** /**
* Implementation of linked_list_t.destroy. * Implementation of linked_list_t.destroy.
*/ */
static status_t linked_list_destroy(private_linked_list_t *this) static void linked_list_destroy(private_linked_list_t *this)
{ {
void * value; void * value;
/* Remove all list items before destroying list */ /* Remove all list items before destroying list */
while (this->public.remove_first(&(this->public),&value) != NOT_FOUND) while (this->public.remove_first(&(this->public),&value) != NOT_FOUND)
{ {
/* values are not destroyed so memory leaks are possible /* values are not destroyed so memory leaks are possible
* if list is not empty when deleting */ * if list is not empty when deleting */
} }
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -589,14 +524,14 @@ linked_list_t *linked_list_create()
private_linked_list_t *this = allocator_alloc_thing(private_linked_list_t); private_linked_list_t *this = allocator_alloc_thing(private_linked_list_t);
this->public.get_count = (int (*) (linked_list_t *linked_list)) get_count; this->public.get_count = (int (*) (linked_list_t *linked_list)) get_count;
this->public.create_iterator = (status_t (*) (linked_list_t *linked_list, iterator_t **iterator,bool forward)) create_iterator; this->public.create_iterator = (void (*) (linked_list_t *linked_list, iterator_t **iterator,bool forward)) create_iterator;
this->public.get_first = (status_t (*) (linked_list_t *linked_list, void **item)) get_first; this->public.get_first = (status_t (*) (linked_list_t *linked_list, void **item)) get_first;
this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last; this->public.get_last = (status_t (*) (linked_list_t *linked_list, void **item)) get_last;
this->public.insert_first = (status_t (*) (linked_list_t *linked_list, void *item)) insert_first; this->public.insert_first = (void (*) (linked_list_t *linked_list, void *item)) insert_first;
this->public.insert_last = (status_t (*) (linked_list_t *linked_list, void *item)) insert_last; this->public.insert_last = (void (*) (linked_list_t *linked_list, void *item)) insert_last;
this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first; this->public.remove_first = (status_t (*) (linked_list_t *linked_list, void **item)) remove_first;
this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last; this->public.remove_last = (status_t (*) (linked_list_t *linked_list, void **item)) remove_last;
this->public.destroy = (status_t (*) (linked_list_t *linked_list)) linked_list_destroy; this->public.destroy = (void (*) (linked_list_t *linked_list)) linked_list_destroy;
this->count = 0; this->count = 0;
this->first = NULL; this->first = NULL;
+10 -19
View File
@@ -43,8 +43,8 @@ struct linked_list_t {
/** /**
* @brief Gets the count of items in the list. * @brief Gets the count of items in the list.
* *
* @param linked_list calling object * @param linked_list calling object
* @return number of items in list * @return number of items in list
*/ */
int (*get_count) (linked_list_t *linked_list); int (*get_count) (linked_list_t *linked_list);
@@ -53,26 +53,19 @@ struct linked_list_t {
* *
* @warning Created iterator has to get destroyed by the caller. * @warning Created iterator has to get destroyed by the caller.
* *
* @param linked_list calling object * @param linked_list calling object
* @param[out] iterator place where the iterator is written * @param[out] iterator place where the iterator is written
* @param[in] forward iterator direction (TRUE: front to end) * @param[in] forward iterator direction (TRUE: front to end)
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*create_iterator) (linked_list_t *linked_list, iterator_t **iterator,bool forward); void (*create_iterator) (linked_list_t *linked_list, iterator_t **iterator, bool forward);
/** /**
* @brief Inserts a new item at the beginning of the list. * @brief Inserts a new item at the beginning of the list.
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] item value to insert in list * @param[in] item value to insert in list
* @return
* - SUCCESS
* - FAILED if internal list is corrupted.
* - OUT_OF_RES
*/ */
status_t (*insert_first) (linked_list_t *linked_list, void *item); void (*insert_first) (linked_list_t *linked_list, void *item);
/** /**
* @brief Removes the first item in the list and returns its value. * @brief Removes the first item in the list and returns its value.
@@ -101,12 +94,8 @@ struct linked_list_t {
* *
* @param linked_list calling object * @param linked_list calling object
* @param[in] item value to insert into list * @param[in] item value to insert into list
* @return
* - SUCCESS
* - FAILED if internal list is corrupted.
* - OUT_OF_RES
*/ */
status_t (*insert_last) (linked_list_t *linked_list, void *item); void (*insert_last) (linked_list_t *linked_list, void *item);
/** /**
* @brief Removes the last item in the list and returns its value. * @brief Removes the last item in the list and returns its value.
@@ -142,12 +131,14 @@ struct linked_list_t {
* @return * @return
* - SUCCESS * - SUCCESS
*/ */
status_t (*destroy) (linked_list_t *linked_list); void (*destroy) (linked_list_t *linked_list);
}; };
/** /**
* @brief Creates an empty linked list object. * @brief Creates an empty linked list object.
* *
* @return the created linked list.
*
* @ingroup utils * @ingroup utils
*/ */
linked_list_t *linked_list_create(); linked_list_t *linked_list_create();
+13 -32
View File
@@ -41,7 +41,7 @@
typedef struct private_logger_t private_logger_t; typedef struct private_logger_t private_logger_t;
/** /**
* @brief The logger object. * @brief The logger object's private data.
*/ */
struct private_logger_t { struct private_logger_t {
/** /**
@@ -119,7 +119,6 @@ static void prepend_prefix(private_logger_t *this, logger_level_t loglevel, char
log_details = '0'; log_details = '0';
} }
if (this->log_pid) if (this->log_pid)
{ {
snprintf(buffer, MAX_LOG, "[%c%c] [%s] @%d %s", log_type, log_details, this->name, getpid(), string); snprintf(buffer, MAX_LOG, "[%c%c] [%s] @%d %s", log_type, log_details, this->name, getpid(), string);
@@ -128,7 +127,6 @@ static void prepend_prefix(private_logger_t *this, logger_level_t loglevel, char
{ {
snprintf(buffer, MAX_LOG, "[%c%c] [%s] %s", log_type, log_details, this->name, string); snprintf(buffer, MAX_LOG, "[%c%c] [%s] %s", log_type, log_details, this->name, string);
} }
} }
/** /**
@@ -136,7 +134,7 @@ static void prepend_prefix(private_logger_t *this, logger_level_t loglevel, char
* *
* Yes, logg is wrong written :-). * Yes, logg is wrong written :-).
*/ */
static status_t logg(private_logger_t *this, logger_level_t loglevel, char *format, ...) static void logg(private_logger_t *this, logger_level_t loglevel, char *format, ...)
{ {
if ((this->level & loglevel) == loglevel) if ((this->level & loglevel) == loglevel)
{ {
@@ -163,13 +161,12 @@ static status_t logg(private_logger_t *this, logger_level_t loglevel, char *form
} }
} }
return SUCCESS;
} }
/** /**
* Implementation of logger_t.log_bytes. * Implementation of logger_t.log_bytes.
*/ */
static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len) static void log_bytes(private_logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len)
{ {
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -256,48 +253,42 @@ static status_t log_bytes(private_logger_t *this, logger_level_t loglevel, char
} }
} }
} }
pthread_mutex_unlock(&mutex); pthread_mutex_unlock(&mutex);
return SUCCESS;
} }
/** /**
* Implementation of logger_t.log_chunk. * Implementation of logger_t.log_chunk.
*/ */
static status_t log_chunk(logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk) static void log_chunk(logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk)
{ {
this->log_bytes(this, loglevel, label, chunk->ptr, chunk->len); this->log_bytes(this, loglevel, label, chunk->ptr, chunk->len);
return SUCCESS;
} }
/** /**
* Implementation of logger_t.enable_level. * Implementation of logger_t.enable_level.
*/ */
static status_t enable_level(private_logger_t *this, logger_level_t log_level) static void enable_level(private_logger_t *this, logger_level_t log_level)
{ {
this->level |= log_level; this->level |= log_level;
return SUCCESS;
} }
/** /**
* Implementation of logger_t.disable_level. * Implementation of logger_t.disable_level.
*/ */
static status_t disable_level(private_logger_t *this, logger_level_t log_level) static void disable_level(private_logger_t *this, logger_level_t log_level)
{ {
this->level &= ~log_level; this->level &= ~log_level;
return SUCCESS;
} }
/** /**
* Implementation of logger_t.destroy. * Implementation of logger_t.destroy.
*/ */
static status_t destroy(private_logger_t *this) static void destroy(private_logger_t *this)
{ {
allocator_free(this->name); allocator_free(this->name);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -306,23 +297,18 @@ static status_t destroy(private_logger_t *this)
logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_pid, FILE * output) logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_pid, FILE * output)
{ {
private_logger_t *this = allocator_alloc_thing(private_logger_t); private_logger_t *this = allocator_alloc_thing(private_logger_t);
if (this == NULL)
{
return NULL;
}
if (logger_name == NULL) if (logger_name == NULL)
{ {
logger_name = ""; logger_name = "";
} }
this->public.log = (status_t(*)(logger_t*,logger_level_t,char*,...))logg; this->public.log = (void(*)(logger_t*,logger_level_t,char*,...))logg;
this->public.log_bytes = (status_t(*)(logger_t*, logger_level_t, char*,char*,size_t))log_bytes; this->public.log_bytes = (void(*)(logger_t*, logger_level_t, char*,char*,size_t))log_bytes;
this->public.log_chunk = log_chunk; this->public.log_chunk = log_chunk;
this->public.enable_level = (status_t(*)(logger_t*,logger_level_t))enable_level; this->public.enable_level = (void(*)(logger_t*,logger_level_t))enable_level;
this->public.disable_level = (status_t(*)(logger_t*,logger_level_t))disable_level; this->public.disable_level = (void(*)(logger_t*,logger_level_t))disable_level;
this->public.destroy = (status_t(*)(logger_t*))destroy; this->public.destroy = (void(*)(logger_t*))destroy;
this->prepend_prefix = prepend_prefix; this->prepend_prefix = prepend_prefix;
@@ -330,14 +316,9 @@ logger_t *logger_create(char *logger_name, logger_level_t log_level, bool log_pi
this->level = log_level; this->level = log_level;
this->log_pid = log_pid; this->log_pid = log_pid;
this->name = allocator_alloc(strlen(logger_name) + 1); this->name = allocator_alloc(strlen(logger_name) + 1);
if (this->name == NULL)
{
allocator_free(this);
return NULL;
}
strcpy(this->name,logger_name); strcpy(this->name,logger_name);
this->output = output; this->output = output;
if (output == NULL) if (output == NULL)
{ {
+7 -15
View File
@@ -99,9 +99,8 @@ struct logger_t {
* @param loglevel or'ed set of loglevels * @param loglevel or'ed set of loglevels
* @param format printf like format string * @param format printf like format string
* @param ... printf like parameters * @param ... printf like parameters
* @return SUCCESS in any case
*/ */
status_t (*log) (logger_t *this, logger_level_t log_level, char *format, ...); void (*log) (logger_t *this, logger_level_t log_level, char *format, ...);
/** /**
* @brief Log some bytes, useful for debugging. * @brief Log some bytes, useful for debugging.
@@ -114,9 +113,8 @@ struct logger_t {
* @param label a labeling name, logged with the bytes * @param label a labeling name, logged with the bytes
* @param bytes pointer to the bytes to dump * @param bytes pointer to the bytes to dump
* @param len number of bytes to dump * @param len number of bytes to dump
* @return SUCCESS in any case
*/ */
status_t (*log_bytes) (logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len); void (*log_bytes) (logger_t *this, logger_level_t loglevel, char *label, char *bytes, size_t len);
/** /**
* @brief Log a chunk, useful for debugging. * @brief Log a chunk, useful for debugging.
@@ -128,35 +126,31 @@ struct logger_t {
* @param loglevel or'ed set of loglevels * @param loglevel or'ed set of loglevels
* @param label a labeling name, logged with the bytes * @param label a labeling name, logged with the bytes
* @param chunk pointer to a chunk to log * @param chunk pointer to a chunk to log
* @return SUCCESS in any case
*/ */
status_t (*log_chunk) (logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk); void (*log_chunk) (logger_t *this, logger_level_t loglevel, char *label, chunk_t *chunk);
/** /**
* @brief Enables a loglevel for the current logger_t object. * @brief Enables a loglevel for the current logger_t object.
* *
* @param this logger_t object * @param this logger_t object
* @param log_level loglevel to enable * @param log_level loglevel to enable
* @return SUCCESS in any case
*/ */
status_t (*enable_level) (logger_t *this, logger_level_t log_level); void (*enable_level) (logger_t *this, logger_level_t log_level);
/** /**
* @brief Disables a loglevel for the current logger_t object. * @brief Disables a loglevel for the current logger_t object.
* *
* @param this logger_t object * @param this logger_t object
* @param log_level loglevel to enable * @param log_level loglevel to enable
* @return UCCESS in any case
*/ */
status_t (*disable_level) (logger_t *this, logger_level_t log_level); void (*disable_level) (logger_t *this, logger_level_t log_level);
/** /**
* @brief Destroys a logger_t object. * @brief Destroys a logger_t object.
* *
* @param this logger_t object * @param this logger_t object
* @return SUCCESS in any case
*/ */
status_t (*destroy) (logger_t *this); void (*destroy) (logger_t *this);
}; };
/** /**
@@ -166,9 +160,7 @@ struct logger_t {
* @param log_level or'ed set of log_levels to assign to the new logger_t object * @param log_level or'ed set of log_levels to assign to the new logger_t object
* @param log_pid TRUE if thread id should also be logged * @param log_pid TRUE if thread id should also be logged
* @param output FILE * if log has to go on a file output, NULL for syslog * @param output FILE * if log has to go on a file output, NULL for syslog
* @return * @return logger_t object
* - logger_t object
* - NULL if out of ressources
* *
* @ingroup utils * @ingroup utils
*/ */
+31 -130
View File
@@ -89,11 +89,8 @@ struct private_logger_manager_t {
* @param context context to set level * @param context context to set level
* @param logger_level logger_level to set * @param logger_level logger_level to set
* @param enable enable specific level or disable it * @param enable enable specific level or disable it
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*set_logger_level) (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable); void (*set_logger_level) (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable);
}; };
@@ -184,32 +181,13 @@ static logger_t *create_logger(private_logger_manager_t *this, logger_context_t
logger = logger_create(context_name,logger_level,log_thread_ids,output); logger = logger_create(context_name,logger_level,log_thread_ids,output);
} }
if (logger == NULL)
{
pthread_mutex_unlock(&(this->mutex));
return NULL;
}
entry = allocator_alloc_thing(loggers_entry_t); entry = allocator_alloc_thing(loggers_entry_t);
if (entry == NULL)
{
logger->destroy(logger);
pthread_mutex_unlock(&(this->mutex));
return NULL;
}
entry->context = context; entry->context = context;
entry->logger = logger; entry->logger = logger;
if (this->loggers->insert_last(this->loggers,entry) != SUCCESS) this->loggers->insert_last(this->loggers,entry);
{
allocator_free(entry);
logger->destroy(logger);
pthread_mutex_unlock(&(this->mutex));
return NULL;
}
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return logger; return logger;
@@ -227,28 +205,18 @@ static logger_level_t get_logger_level (private_logger_manager_t *this, logger_c
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS) this->logger_levels->create_iterator(this->logger_levels, &iterator,TRUE);
{
pthread_mutex_unlock(&(this->mutex));
return logger_level;
}
/* check for existing logger_level entry */ /* check for existing logger_level entry */
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
{ {
logger_levels_entry_t * entry; logger_levels_entry_t * entry;
if (iterator->current(iterator,(void **)&entry) != SUCCESS) iterator->current(iterator,(void **)&entry);
{
break;
}
if (entry->context == context) if (entry->context == context)
{ {
logger_level = entry->level; logger_level = entry->level;
break; break;
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
@@ -258,71 +226,45 @@ static logger_level_t get_logger_level (private_logger_manager_t *this, logger_c
/** /**
* Implementation of logger_manager_t.destroy_logger. * Implementation of logger_manager_t.destroy_logger.
*/ */
static status_t destroy_logger (private_logger_manager_t *this,logger_t *logger) static void destroy_logger(private_logger_manager_t *this,logger_t *logger)
{ {
iterator_t *iterator; iterator_t *iterator;
status_t status = NOT_FOUND;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS)
{ this->loggers->create_iterator(this->loggers,&iterator,TRUE);
pthread_mutex_unlock(&(this->mutex));
return OUT_OF_RES;
}
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
{ {
loggers_entry_t * entry; loggers_entry_t * entry;
status = iterator->current(iterator,(void **)&entry); iterator->current(iterator,(void **)&entry);
if (status != SUCCESS)
{
break;
}
status = NOT_FOUND;
if (entry->logger == logger) if (entry->logger == logger)
{ {
iterator->remove(iterator); iterator->remove(iterator);
allocator_free(entry); allocator_free(entry);
logger->destroy(logger); logger->destroy(logger);
status = SUCCESS;
break; break;
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return status;
} }
/** /**
* Implementation of private_logger_manager_t.set_logger_level. * Implementation of private_logger_manager_t.set_logger_level.
*/ */
static status_t set_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable) static void set_logger_level(private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level,bool enable)
{ {
iterator_t *iterator; iterator_t *iterator;
status_t status; bool found = FALSE;
pthread_mutex_lock(&(this->mutex)); pthread_mutex_lock(&(this->mutex));
if (this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE) != SUCCESS) this->logger_levels->create_iterator(this->logger_levels,&iterator,TRUE);
{
pthread_mutex_unlock(&(this->mutex));
return OUT_OF_RES;
}
status = NOT_FOUND;
/* find existing logger_level entry */ /* find existing logger_level entry */
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
{ {
logger_levels_entry_t * entry; logger_levels_entry_t * entry;
status = iterator->current(iterator,(void **)&entry); iterator->current(iterator,(void **)&entry);
if (status != SUCCESS)
{
iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex));
return status;
}
status = NOT_FOUND;
if (entry->context == context) if (entry->context == context)
{ {
if (enable) if (enable)
@@ -333,74 +275,51 @@ static status_t set_logger_level (private_logger_manager_t *this, logger_context
{ {
entry->level &= ~logger_level; entry->level &= ~logger_level;
} }
found = TRUE;
status = SUCCESS;
break; break;
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
if (status == NOT_FOUND) if (!found)
{ {
/* logger_levels entry not existing for current context */ /* logger_levels entry not existing for current context */
logger_levels_entry_t *entry = allocator_alloc_thing(logger_levels_entry_t); logger_levels_entry_t *entry = allocator_alloc_thing(logger_levels_entry_t);
if (entry == NULL)
{
pthread_mutex_unlock(&(this->mutex));
return OUT_OF_RES;
}
entry->context = context; entry->context = context;
entry->level = (enable) ? logger_level : (this->default_log_level & (~logger_level)); entry->level = (enable) ? logger_level : (this->default_log_level & (~logger_level));
status = this->logger_levels->insert_last(this->logger_levels,entry); this->logger_levels->insert_last(this->logger_levels,entry);
if (status != SUCCESS)
{
allocator_free(entry);
pthread_mutex_unlock(&(this->mutex));
return status;
}
} }
if (this->loggers->create_iterator(this->loggers,&iterator,TRUE) != SUCCESS) this->loggers->create_iterator(this->loggers,&iterator,TRUE);
{
pthread_mutex_unlock(&(this->mutex));
return OUT_OF_RES;
}
while (iterator->has_next(iterator)) while (iterator->has_next(iterator))
{ {
loggers_entry_t * entry; loggers_entry_t * entry;
status = iterator->current(iterator,(void **)&entry); iterator->current(iterator,(void **)&entry);
if (status != SUCCESS)
{
iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex));
return status;
}
if (entry->context == context) if (entry->context == context)
{ {
if (enable) if (enable)
{ {
status = entry->logger->enable_level(entry->logger,logger_level); entry->logger->enable_level(entry->logger,logger_level);
} }
else else
{ {
status = entry->logger->disable_level(entry->logger,logger_level); entry->logger->disable_level(entry->logger,logger_level);
} }
} }
} }
iterator->destroy(iterator); iterator->destroy(iterator);
pthread_mutex_unlock(&(this->mutex)); pthread_mutex_unlock(&(this->mutex));
return SUCCESS;
} }
/** /**
* Implementation of logger_manager_t.enable_logger_level. * Implementation of logger_manager_t.enable_logger_level.
*/ */
static status_t enable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level) static void enable_logger_level(private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
{ {
return set_logger_level(this,context,logger_level,TRUE); return set_logger_level(this,context,logger_level,TRUE);
} }
@@ -408,7 +327,7 @@ static status_t enable_logger_level (private_logger_manager_t *this, logger_cont
/** /**
* Implementation of logger_manager_t.disable_logger_level. * Implementation of logger_manager_t.disable_logger_level.
*/ */
static status_t disable_logger_level (private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level) static void disable_logger_level(private_logger_manager_t *this, logger_context_t context,logger_level_t logger_level)
{ {
return set_logger_level(this,context,logger_level,FALSE); return set_logger_level(this,context,logger_level,FALSE);
} }
@@ -416,7 +335,7 @@ static status_t disable_logger_level (private_logger_manager_t *this, logger_con
/** /**
* Implementation of logger_manager_t.destroy. * Implementation of logger_manager_t.destroy.
*/ */
static status_t destroy(private_logger_manager_t *this) static void destroy(private_logger_manager_t *this)
{ {
while (this->loggers->get_count(this->loggers) > 0) while (this->loggers->get_count(this->loggers) > 0)
@@ -447,7 +366,6 @@ static status_t destroy(private_logger_manager_t *this)
pthread_mutex_destroy(&(this->mutex)); pthread_mutex_destroy(&(this->mutex));
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -456,35 +374,18 @@ static status_t destroy(private_logger_manager_t *this)
logger_manager_t *logger_manager_create(logger_level_t default_log_level) logger_manager_t *logger_manager_create(logger_level_t default_log_level)
{ {
private_logger_manager_t *this = allocator_alloc_thing(private_logger_manager_t); private_logger_manager_t *this = allocator_alloc_thing(private_logger_manager_t);
if (this == NULL)
{
return NULL;
}
this->public.create_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context, char *))create_logger; this->public.create_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context, char *))create_logger;
this->public.destroy_logger = (status_t(*)(logger_manager_t*,logger_t *logger))destroy_logger; this->public.destroy_logger = (void(*)(logger_manager_t*,logger_t *logger))destroy_logger;
this->public.destroy = (status_t(*)(logger_manager_t*))destroy; this->public.destroy = (void(*)(logger_manager_t*))destroy;
this->public.get_logger_level = (logger_level_t (*)(logger_manager_t *, logger_context_t)) get_logger_level; this->public.get_logger_level = (logger_level_t (*)(logger_manager_t *, logger_context_t)) get_logger_level;
this->public.enable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) enable_logger_level; this->public.enable_logger_level = (void (*)(logger_manager_t *, logger_context_t,logger_level_t)) enable_logger_level;
this->public.disable_logger_level = (status_t (*)(logger_manager_t *, logger_context_t,logger_level_t)) disable_logger_level; this->public.disable_logger_level = (void (*)(logger_manager_t *, logger_context_t,logger_level_t)) disable_logger_level;
this->set_logger_level = (status_t (*)(private_logger_manager_t *, logger_context_t,logger_level_t,bool)) set_logger_level; this->set_logger_level = (void (*)(private_logger_manager_t *, logger_context_t,logger_level_t,bool)) set_logger_level;
/* private variables */ /* private variables */
this->loggers = linked_list_create(); this->loggers = linked_list_create();
if (this->loggers == NULL)
{
allocator_free(this);
return NULL;
}
this->logger_levels = linked_list_create(); this->logger_levels = linked_list_create();
if (this->logger_levels == NULL)
{
this->loggers->destroy(this->loggers);
allocator_free(this);
return NULL;
}
this->default_log_level = default_log_level; this->default_log_level = default_log_level;
pthread_mutex_init(&(this->mutex), NULL); pthread_mutex_init(&(this->mutex), NULL);
+7 -22
View File
@@ -73,9 +73,7 @@ struct logger_manager_t {
* @param[out] logger pointer to a a place where the new logger is stored * @param[out] logger pointer to a a place where the new logger is stored
* @param name name for the new logger. Context name is already included * @param name name for the new logger. Context name is already included
* and has not to be specified (so NULL is allowed) * and has not to be specified (so NULL is allowed)
* @return * @return logger_t object
* - logger_t object
* - NULL if out of ressources
*/ */
logger_t *(*create_logger) (logger_manager_t *this, logger_context_t context, char *name); logger_t *(*create_logger) (logger_manager_t *this, logger_context_t context, char *name);
@@ -88,12 +86,9 @@ struct logger_manager_t {
* destroy function. * destroy function.
* *
* @param this logger_manager_t object * @param this logger_manager_t object
* @param logger pointer to the logger which has to be destroyed * @param logger pointer to the logger which has to be destroyed
* @return - SUCCESS
* - OUT_OF_RES (when searching the specific logger_t object)
* - NOT_FOUND
*/ */
status_t (*destroy_logger) (logger_manager_t *this,logger_t *logger); void (*destroy_logger) (logger_manager_t *this,logger_t *logger);
/** /**
* Returns the set logger_level of a specific context or 0. * Returns the set logger_level of a specific context or 0.
@@ -110,11 +105,8 @@ struct logger_manager_t {
* @param this calling object * @param this calling object
* @param context context to set level * @param context context to set level
* @param logger_level logger_level to eanble * @param logger_level logger_level to eanble
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*enable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level); void (*enable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level);
/** /**
@@ -123,11 +115,8 @@ struct logger_manager_t {
* @param this calling object * @param this calling object
* @param context context to set level * @param context context to set level
* @param logger_level logger_level to disable * @param logger_level logger_level to disable
* @return
* - SUCCESS
* - OUT_OF_RES
*/ */
status_t (*disable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level); void (*disable_logger_level) (logger_manager_t *this, logger_context_t context,logger_level_t logger_level);
/** /**
@@ -136,19 +125,15 @@ struct logger_manager_t {
* All remaining managed logger_t objects are also destroyed. * All remaining managed logger_t objects are also destroyed.
* *
* @param this logger_manager_t object * @param this logger_manager_t object
* @return
* - SUCCESS in any case
*/ */
status_t (*destroy) (logger_manager_t *this); void (*destroy) (logger_manager_t *this);
}; };
/** /**
* @brief Constructor to create a logger_manager_t object. * @brief Constructor to create a logger_manager_t object.
* *
* @param default_log_level default log level for a context * @param default_log_level default log level for a context
* @return * @return logger_manager_t object
* - logger_manager_t object
* - NULL if out of ressources
* *
* @ingroup utils * @ingroup utils
*/ */
+3 -24
View File
@@ -155,13 +155,11 @@ static status_t allocate_pseudo_random_bytes(private_randomizer_t *this, size_t
/** /**
* Implementation of randomizer_t.destroy. * Implementation of randomizer_t.destroy.
*/ */
static status_t destroy(private_randomizer_t *this) static void destroy(private_randomizer_t *this)
{ {
allocator_free(this->random_dev_name); allocator_free(this->random_dev_name);
allocator_free(this->pseudo_random_dev_name); allocator_free(this->pseudo_random_dev_name);
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
@@ -178,41 +176,22 @@ randomizer_t *randomizer_create(void)
randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom_dev_name) randomizer_t *randomizer_create_on_devices(char * random_dev_name,char * prandom_dev_name)
{ {
private_randomizer_t *this = allocator_alloc_thing(private_randomizer_t); private_randomizer_t *this = allocator_alloc_thing(private_randomizer_t);
if (this == NULL)
{
return NULL;
}
if ((random_dev_name == NULL) || (prandom_dev_name == NULL))
{
return NULL;
}
/* public functions */ /* public functions */
this->public.get_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_random_bytes; this->public.get_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_random_bytes;
this->public.allocate_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_random_bytes; this->public.allocate_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_random_bytes;
this->public.get_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_pseudo_random_bytes; this->public.get_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_pseudo_random_bytes;
this->public.allocate_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_pseudo_random_bytes; this->public.allocate_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_pseudo_random_bytes;
this->public.destroy = (status_t (*) (randomizer_t *))destroy; this->public.destroy = (void (*) (randomizer_t *))destroy;
/* private functions */ /* private functions */
this->get_bytes_from_device = get_bytes_from_device; this->get_bytes_from_device = get_bytes_from_device;
/* private fields */ /* private fields */
this->random_dev_name = allocator_alloc(strlen(random_dev_name) + 1); this->random_dev_name = allocator_alloc(strlen(random_dev_name) + 1);
if (this->random_dev_name == NULL)
{
allocator_free(this);
return NULL;
}
strcpy(this->random_dev_name,random_dev_name); strcpy(this->random_dev_name,random_dev_name);
this->pseudo_random_dev_name = allocator_alloc(strlen(prandom_dev_name) + 1); this->pseudo_random_dev_name = allocator_alloc(strlen(prandom_dev_name) + 1);
if (this->pseudo_random_dev_name == NULL)
{
allocator_free(this->random_dev_name);
allocator_free(this);
return NULL;
}
strcpy(this->pseudo_random_dev_name,prandom_dev_name); strcpy(this->pseudo_random_dev_name,prandom_dev_name);
return &(this->public); return &(this->public);
+3 -6
View File
@@ -54,10 +54,9 @@ struct randomizer_t {
* *
* @param this calling randomizer_t object * @param this calling randomizer_t object
* @param bytes number of bytes to allocate * @param bytes number of bytes to allocate
* @param[out] chunk chunk which will hold the allocated random bytes * @param[out] chunk chunk which will hold the allocated random bytes
* @return * @return
* - SUCCESS * - SUCCESS
* - OUT_OF_RES
* - FAILED if random device could not be opened * - FAILED if random device could not be opened
*/ */
status_t (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk); status_t (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
@@ -83,7 +82,6 @@ struct randomizer_t {
* @param[out] chunk chunk which will hold the allocated random bytes * @param[out] chunk chunk which will hold the allocated random bytes
* @return * @return
* - SUCCESS * - SUCCESS
* - OUT_OF_RES
* - FAILED if random device could not be opened * - FAILED if random device could not be opened
*/ */
status_t (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk); status_t (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
@@ -92,9 +90,8 @@ struct randomizer_t {
* @brief Destroys a randomizer_t object. * @brief Destroys a randomizer_t object.
* *
* @param this randomizer_t object to destroy * @param this randomizer_t object to destroy
* @return SUCCESS in any case
*/ */
status_t (*destroy) (randomizer_t *this); void (*destroy) (randomizer_t *this);
}; };
/** /**
@@ -115,7 +112,7 @@ randomizer_t *randomizer_create();
* @param prandom_dev_name device name for pseudo random values, etc /dev/urandom * @param prandom_dev_name device name for pseudo random values, etc /dev/urandom
* @return * @return
* - created randomizer_t * - created randomizer_t
* - NULL if out of ressources * - NULL if failed
* *
* @ingroup utils * @ingroup utils
*/ */
+3 -5
View File
@@ -93,7 +93,7 @@ struct private_tester_t {
/** /**
* Implementation of tester_t.perform_tests. * Implementation of tester_t.perform_tests.
*/ */
static status_t perform_tests(tester_t *tester,test_t **tests) static void perform_tests(tester_t *tester,test_t **tests)
{ {
private_tester_t *this =(private_tester_t*) tester; private_tester_t *this =(private_tester_t*) tester;
int current_test = 0; int current_test = 0;
@@ -110,13 +110,12 @@ static status_t perform_tests(tester_t *tester,test_t **tests)
fprintf(this->output,"=====================================================================\n"); fprintf(this->output,"=====================================================================\n");
fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count); fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
fprintf(this->output,"=====================================================================\n"); fprintf(this->output,"=====================================================================\n");
return SUCCESS;
} }
/** /**
* Implementation of tester_t.perform_test. * Implementation of tester_t.perform_test.
*/ */
static status_t perform_test(tester_t *tester, test_t *test) static void perform_test(tester_t *tester, test_t *test)
{ {
test_t *tests[] = {test, NULL}; test_t *tests[] = {test, NULL};
return (perform_tests(tester,tests)); return (perform_tests(tester,tests));
@@ -214,12 +213,11 @@ static void assert_false(tester_t *tester, bool to_be_false,char * assert_name)
/** /**
* Implementation of tester_t.destroy. * Implementation of tester_t.destroy.
*/ */
static status_t destroy(tester_t *tester) static void destroy(tester_t *tester)
{ {
private_tester_t *this = (private_tester_t*) tester; private_tester_t *this = (private_tester_t*) tester;
pthread_mutex_destroy(&(this->mutex)); pthread_mutex_destroy(&(this->mutex));
allocator_free(this); allocator_free(this);
return SUCCESS;
} }
/* /*
+4 -9
View File
@@ -63,18 +63,16 @@ struct tester_t {
* @param tester tester_t object * @param tester tester_t object
* @param tests pointer to an array of test_t-pointers. * @param tests pointer to an array of test_t-pointers.
* The last item has to be NULL. * The last item has to be NULL.
* @return SUCCESS in any case
*/ */
status_t (*perform_tests) (tester_t *tester,test_t **tests); void (*perform_tests) (tester_t *tester,test_t **tests);
/** /**
* @brief Run a specific test case. * @brief Run a specific test case.
* *
* @param this tester_t object * @param this tester_t object
* @param test pointer to a test_t object which will be performed * @param test pointer to a test_t object which will be performed
* @return SUCCESS in any case
*/ */
status_t (*perform_test) (tester_t *tester, test_t *test); void (*perform_test) (tester_t *tester, test_t *test);
/** /**
* Is called in a testcase to check a specific situation for TRUE. * Is called in a testcase to check a specific situation for TRUE.
@@ -106,9 +104,8 @@ struct tester_t {
* @brief Destroys a tester_t object * @brief Destroys a tester_t object
* *
* @param tester tester_t object * @param tester tester_t object
* @return SUCCESS in any case
*/ */
status_t (*destroy) (tester_t *tester); void (*destroy) (tester_t *tester);
}; };
/** /**
@@ -118,9 +115,7 @@ struct tester_t {
* @param display_succeeded_asserts has to be TRUE, if all asserts should be displayed, * @param display_succeeded_asserts has to be TRUE, if all asserts should be displayed,
* FALSE otherwise * FALSE otherwise
* *
* @return * @return - tester_t object
* - tester_t object
* - NULL if out of ressources
* *
* @ingroup utils * @ingroup utils
*/ */