Migrated controller_t to INIT/METHOD macros

This commit is contained in:
Martin Willi
2011-05-16 15:24:15 +02:00
parent 83245de0ac
commit 4baf1f3bfe
2 changed files with 98 additions and 99 deletions
+97 -98
View File
@@ -1,5 +1,6 @@
/* /*
* Copyright (C) 2007 Martin Willi * Copyright (C) 2007-2011 Martin Willi
* Copyright (C) 2011 revosec AG
* Hochschule fuer Technik Rapperswil * Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
@@ -96,6 +97,7 @@ typedef struct interface_job_t interface_job_t;
* job for asynchronous listen operations * job for asynchronous listen operations
*/ */
struct interface_job_t { struct interface_job_t {
/** /**
* job interface * job interface
*/ */
@@ -107,12 +109,9 @@ struct interface_job_t {
interface_listener_t listener; interface_listener_t listener;
}; };
/** METHOD(listener_t, listener_log, bool,
* listener log function interface_listener_t *this, debug_t group, level_t level, int thread,
*/ ike_sa_t *ike_sa, char* format, va_list args)
static bool listener_log(interface_listener_t *this, debug_t group,
level_t level, int thread, ike_sa_t *ike_sa,
char* format, va_list args)
{ {
if (this->ike_sa == ike_sa) if (this->ike_sa == ike_sa)
{ {
@@ -130,11 +129,8 @@ METHOD(job_t, get_priority_medium, job_priority_t,
return JOB_PRIO_MEDIUM; return JOB_PRIO_MEDIUM;
} }
/** METHOD(listener_t, ike_state_change, bool,
* Implementation of listener_t.ike_state_change interface_listener_t *this, ike_sa_t *ike_sa, ike_sa_state_t state)
*/
static bool listener_ike_state(interface_listener_t *this, ike_sa_t *ike_sa,
ike_sa_state_t state)
{ {
if (this->ike_sa == ike_sa) if (this->ike_sa == ike_sa)
{ {
@@ -166,11 +162,9 @@ static bool listener_ike_state(interface_listener_t *this, ike_sa_t *ike_sa,
return TRUE; return TRUE;
} }
/** METHOD(listener_t, child_state_change, bool,
* Implementation of listener_t.child_state_change interface_listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
*/ child_sa_state_t state)
static bool listener_child_state(interface_listener_t *this, ike_sa_t *ike_sa,
child_sa_t *child_sa, child_sa_state_t state)
{ {
if (this->ike_sa == ike_sa) if (this->ike_sa == ike_sa)
{ {
@@ -197,10 +191,8 @@ static bool listener_child_state(interface_listener_t *this, ike_sa_t *ike_sa,
return TRUE; return TRUE;
} }
/** METHOD(job_t, recheckin, void,
* cleanup job if job is never executed interface_job_t *job)
*/
static void recheckin(interface_job_t *job)
{ {
if (job->listener.ike_sa) if (job->listener.ike_sa)
{ {
@@ -209,19 +201,15 @@ static void recheckin(interface_job_t *job)
} }
} }
/** METHOD(controller_t, create_ike_sa_enumerator, enumerator_t*,
* Implementation of controller_t.create_ike_sa_iterator. private_controller_t *this, bool wait)
*/
static enumerator_t* create_ike_sa_enumerator(controller_t *this, bool wait)
{ {
return charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager, return charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager,
wait); wait);
} }
/** METHOD(job_t, initiate_execute, void,
* execute function for initiate interface_job_t *job)
*/
static status_t initiate_execute(interface_job_t *job)
{ {
ike_sa_t *ike_sa; ike_sa_t *ike_sa;
interface_listener_t *listener = &job->listener; interface_listener_t *listener = &job->listener;
@@ -240,25 +228,26 @@ static status_t initiate_execute(interface_job_t *job)
if (ike_sa->initiate(ike_sa, listener->child_cfg, 0, NULL, NULL) == SUCCESS) if (ike_sa->initiate(ike_sa, listener->child_cfg, 0, NULL, NULL) == SUCCESS)
{ {
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa); charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
return SUCCESS; listener->status = SUCCESS;
}
else
{
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
ike_sa);
listener->status = FAILED;
} }
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
return FAILED;
} }
/** METHOD(controller_t, initiate, status_t,
* Implementation of controller_t.initiate. private_controller_t *this, peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
*/ controller_cb_t callback, void *param)
static status_t initiate(private_controller_t *this,
peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
controller_cb_t callback, void *param)
{ {
interface_job_t job = { interface_job_t job = {
.listener = { .listener = {
.public = { .public = {
.log = (void*)listener_log, .log = _listener_log,
.ike_state_change = (void*)listener_ike_state, .ike_state_change = _ike_state_change,
.child_state_change = (void*)listener_child_state, .child_state_change = _child_state_change,
}, },
.callback = callback, .callback = callback,
.param = param, .param = param,
@@ -267,23 +256,24 @@ static status_t initiate(private_controller_t *this,
.peer_cfg = peer_cfg, .peer_cfg = peer_cfg,
}, },
.public = { .public = {
.execute = (void*)initiate_execute, .execute = _initiate_execute,
.get_priority = _get_priority_medium, .get_priority = _get_priority_medium,
.destroy = (void*)recheckin, .destroy = _recheckin,
}, },
}; };
if (callback == NULL) if (callback == NULL)
{ {
return initiate_execute(&job); initiate_execute(&job);
}
else
{
charon->bus->listen(charon->bus, &job.listener.public, &job.public);
} }
charon->bus->listen(charon->bus, &job.listener.public, (job_t*)&job);
return job.listener.status; return job.listener.status;
} }
/** METHOD(job_t, terminate_ike_execute, void,
* execute function for terminate_ike interface_job_t *job)
*/
static status_t terminate_ike_execute(interface_job_t *job)
{ {
interface_listener_t *listener = &job->listener; interface_listener_t *listener = &job->listener;
ike_sa_t *ike_sa = listener->ike_sa; ike_sa_t *ike_sa = listener->ike_sa;
@@ -294,25 +284,27 @@ static status_t terminate_ike_execute(interface_job_t *job)
{ {
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa); charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
/* delete failed */ /* delete failed */
return FAILED; listener->status = FAILED;
}
else
{
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
ike_sa);
listener->status = SUCCESS;
} }
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
return SUCCESS;
} }
/** METHOD(controller_t, terminate_ike, status_t,
* Implementation of controller_t.terminate_ike. controller_t *this, u_int32_t unique_id,
*/ controller_cb_t callback, void *param)
static status_t terminate_ike(controller_t *this, u_int32_t unique_id,
controller_cb_t callback, void *param)
{ {
ike_sa_t *ike_sa; ike_sa_t *ike_sa;
interface_job_t job = { interface_job_t job = {
.listener = { .listener = {
.public = { .public = {
.log = (void*)listener_log, .log = _listener_log,
.ike_state_change = (void*)listener_ike_state, .ike_state_change = _ike_state_change,
.child_state_change = (void*)listener_child_state, .child_state_change = _child_state_change,
}, },
.callback = callback, .callback = callback,
.param = param, .param = param,
@@ -320,9 +312,9 @@ static status_t terminate_ike(controller_t *this, u_int32_t unique_id,
.id = unique_id, .id = unique_id,
}, },
.public = { .public = {
.execute = (void*)terminate_ike_execute, .execute = _terminate_ike_execute,
.get_priority = _get_priority_medium, .get_priority = _get_priority_medium,
.destroy = (void*)recheckin, .destroy = _recheckin,
}, },
}; };
@@ -337,18 +329,19 @@ static status_t terminate_ike(controller_t *this, u_int32_t unique_id,
if (callback == NULL) if (callback == NULL)
{ {
return terminate_ike_execute(&job); terminate_ike_execute(&job);
}
else
{
charon->bus->listen(charon->bus, &job.listener.public, &job.public);
/* checkin of the ike_sa happend in the thread that executed the job */
charon->bus->set_sa(charon->bus, NULL);
} }
charon->bus->listen(charon->bus, &job.listener.public, (job_t*)&job);
/* checkin of the ike_sa happend in the thread that executed the job */
charon->bus->set_sa(charon->bus, NULL);
return job.listener.status; return job.listener.status;
} }
/** METHOD(job_t, terminate_child_execute, void,
* execute function for terminate_child interface_job_t *job)
*/
static status_t terminate_child_execute(interface_job_t *job)
{ {
interface_listener_t *listener = &job->listener; interface_listener_t *listener = &job->listener;
ike_sa_t *ike_sa = listener->ike_sa; ike_sa_t *ike_sa = listener->ike_sa;
@@ -359,17 +352,18 @@ static status_t terminate_child_execute(interface_job_t *job)
child_sa->get_spi(child_sa, TRUE)) != DESTROY_ME) child_sa->get_spi(child_sa, TRUE)) != DESTROY_ME)
{ {
charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa); charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
return SUCCESS; listener->status = SUCCESS;
}
else
{
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager,
ike_sa);
listener->status = FAILED;
} }
charon->ike_sa_manager->checkin_and_destroy(charon->ike_sa_manager, ike_sa);
return FAILED;
} }
/** METHOD(controller_t, terminate_child, status_t,
* Implementation of controller_t.terminate_child. controller_t *this, u_int32_t reqid, controller_cb_t callback, void *param)
*/
static status_t terminate_child(controller_t *this, u_int32_t reqid,
controller_cb_t callback, void *param)
{ {
ike_sa_t *ike_sa; ike_sa_t *ike_sa;
child_sa_t *child_sa; child_sa_t *child_sa;
@@ -377,9 +371,9 @@ static status_t terminate_child(controller_t *this, u_int32_t reqid,
interface_job_t job = { interface_job_t job = {
.listener = { .listener = {
.public = { .public = {
.log = (void*)listener_log, .log = _listener_log,
.ike_state_change = (void*)listener_ike_state, .ike_state_change = _ike_state_change,
.child_state_change = (void*)listener_child_state, .child_state_change = _child_state_change,
}, },
.callback = callback, .callback = callback,
.param = param, .param = param,
@@ -387,9 +381,9 @@ static status_t terminate_child(controller_t *this, u_int32_t reqid,
.id = reqid, .id = reqid,
}, },
.public = { .public = {
.execute = (void*)terminate_child_execute, .execute = _terminate_child_execute,
.get_priority = _get_priority_medium, .get_priority = _get_priority_medium,
.destroy = (void*)recheckin, .destroy = _recheckin,
}, },
}; };
@@ -426,11 +420,14 @@ static status_t terminate_child(controller_t *this, u_int32_t reqid,
if (callback == NULL) if (callback == NULL)
{ {
return terminate_child_execute(&job); terminate_child_execute(&job);
}
else
{
charon->bus->listen(charon->bus, &job.listener.public, &job.public);
/* checkin of the ike_sa happend in the thread that executed the job */
charon->bus->set_sa(charon->bus, NULL);
} }
charon->bus->listen(charon->bus, &job.listener.public, (job_t*)&job);
/* checkin of the ike_sa happend in the thread that executed the job */
charon->bus->set_sa(charon->bus, NULL);
return job.listener.status; return job.listener.status;
} }
@@ -438,15 +435,13 @@ static status_t terminate_child(controller_t *this, u_int32_t reqid,
* See header * See header
*/ */
bool controller_cb_empty(void *param, debug_t group, level_t level, bool controller_cb_empty(void *param, debug_t group, level_t level,
ike_sa_t *ike_sa, char *format, va_list args) ike_sa_t *ike_sa, char *format, va_list args)
{ {
return TRUE; return TRUE;
} }
/** METHOD(controller_t, destroy, void,
* Implementation of stroke_t.destroy. private_controller_t *this)
*/
static void destroy(private_controller_t *this)
{ {
free(this); free(this);
} }
@@ -456,13 +451,17 @@ static void destroy(private_controller_t *this)
*/ */
controller_t *controller_create(void) controller_t *controller_create(void)
{ {
private_controller_t *this = malloc_thing(private_controller_t); private_controller_t *this;
this->public.create_ike_sa_enumerator = (enumerator_t*(*)(controller_t*, bool))create_ike_sa_enumerator; INIT(this,
this->public.initiate = (status_t(*)(controller_t*,peer_cfg_t*,child_cfg_t*,controller_cb_t,void*))initiate; .public = {
this->public.terminate_ike = (status_t(*)(controller_t*,u_int32_t,controller_cb_t, void*))terminate_ike; .create_ike_sa_enumerator = _create_ike_sa_enumerator,
this->public.terminate_child = (status_t(*)(controller_t*,u_int32_t,controller_cb_t, void *param))terminate_child; .initiate = _initiate,
this->public.destroy = (void (*)(controller_t*))destroy; .terminate_ike = _terminate_ike,
.terminate_child = _terminate_child,
.destroy = _destroy,
},
);
return &this->public; return &this->public;
} }
+1 -1
View File
@@ -56,7 +56,7 @@ typedef struct controller_t controller_t;
* *
* Passing NULL as callback to the managers function calls them asynchronously. * Passing NULL as callback to the managers function calls them asynchronously.
* If a callback is specified, they are called synchronously. There is a default * If a callback is specified, they are called synchronously. There is a default
* callback "controller_cb_empty" if you wan't to call a function * callback "controller_cb_empty" if you want to call a function
* synchronously, but don't need a callback. * synchronously, but don't need a callback.
*/ */
struct controller_t { struct controller_t {