refactored message handling

This commit is contained in:
Andreas Steffen
2010-12-10 17:09:21 +01:00
parent af1e3ff567
commit 512d2e045f
+71 -24
View File
@@ -116,14 +116,8 @@ METHOD(tnccs_t, send_message, void,
this->mutex->unlock(this->mutex);
}
static status_t handle_messages(private_tnccs_20_t *this, pb_tnc_batch_t *batch)
static void handle_message(private_tnccs_20_t *this, pb_tnc_message_t *msg)
{
enumerator_t *enumerator;
pb_tnc_message_t *msg;
enumerator = batch->create_msg_enumerator(batch);
while (enumerator->enumerate(enumerator, &msg))
{
switch (msg->get_type(msg))
{
case PB_MSG_EXPERIMENTAL:
@@ -239,8 +233,7 @@ static status_t handle_messages(private_tnccs_20_t *this, pb_tnc_batch_t *batch)
lang_msg = (pb_language_preference_message_t*)msg;
lang = lang_msg->get_language_preference(lang_msg);
DBG2(DBG_TNC, "setting language preference '%.*s'",
lang.len, lang.ptr);
DBG2(DBG_TNC, "setting language preference '%.*s'", lang.len, lang.ptr);
this->recs->set_preferred_language(this->recs, lang);
break;
}
@@ -261,10 +254,6 @@ static status_t handle_messages(private_tnccs_20_t *this, pb_tnc_batch_t *batch)
default:
break;
}
}
enumerator->destroy(enumerator);
return SUCCESS;
}
METHOD(tls_t, process, status_t,
@@ -296,10 +285,50 @@ METHOD(tls_t, process, status_t,
batch = pb_tnc_batch_create_from_data(this->is_server, data);
status = batch->process(batch, this->state_machine);
switch (status)
if (status != FAILED)
{
case SUCCESS:
handle_messages(this, batch);
enumerator_t *enumerator;
pb_tnc_message_t *msg;
pb_tnc_batch_type_t batch_type;
bool empty = TRUE;
batch_type = batch->get_type(batch);
if (batch_type == PB_BATCH_CRETRY)
{
this->mutex->lock(this->mutex);
if (this->batch)
{
DBG1(DBG_TNC, "cancelling PB-TNC %N Batch",
pb_tnc_batch_type_names, this->batch->get_type(this->batch));
this->batch->destroy(this->batch);
}
this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_SRETRY);
this->mutex->unlock(this->mutex);
}
else if (batch_type == PB_BATCH_SRETRY)
{
/* Restart the measurements */
charon->imcs->notify_connection_change(charon->imcs,
this->connection_id, TNC_CONNECTION_STATE_HANDSHAKE);
charon->imcs->begin_handshake(charon->imcs, this->connection_id);
}
enumerator = batch->create_msg_enumerator(batch);
while (enumerator->enumerate(enumerator, &msg))
{
handle_message(this, msg);
empty = FALSE;
}
enumerator->destroy(enumerator);
/* received an empty CLOSE Batch from PB-TNC Client */
if (this->is_server && batch_type == PB_BATCH_CLOSE && empty)
{
batch->destroy(batch);
return SUCCESS;
}
if (this->is_server)
{
charon->imvs->batch_ending(charon->imvs, this->connection_id);
@@ -308,7 +337,10 @@ METHOD(tls_t, process, status_t,
{
charon->imcs->batch_ending(charon->imcs, this->connection_id);
}
break;
}
switch (status)
{
case FAILED:
this->mutex->lock(this->mutex);
if (this->batch)
@@ -319,7 +351,7 @@ METHOD(tls_t, process, status_t,
}
this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
this->mutex->unlock(this->mutex);
/* fall through */
/* fall through to add error messages to outbound batch */
case VERIFY_ERROR:
enumerator = batch->create_error_enumerator(batch);
while (enumerator->enumerate(enumerator, &msg))
@@ -329,6 +361,8 @@ METHOD(tls_t, process, status_t,
this->mutex->unlock(this->mutex);
}
enumerator->destroy(enumerator);
break;
case SUCCESS:
default:
break;
}
@@ -340,6 +374,8 @@ METHOD(tls_t, process, status_t,
METHOD(tls_t, build, status_t,
private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen)
{
status_t status;
if (!this->is_server && !this->connection_id)
{
pb_tnc_message_t *msg;
@@ -369,13 +405,24 @@ METHOD(tls_t, build, status_t,
charon->imcs->begin_handshake(charon->imcs, this->connection_id);
}
/* Do not allow any asynchronous IMCs or IMVs to add additional messages */
this->mutex->lock(this->mutex);
/**
* if the DECIDED state has been reached, close down the TNCCS connection
* by sending an empty CLOSE batch unless a CRETRY batch is under way
*/
if (this->state_machine->get_state(this->state_machine) == PB_STATE_DECIDED &&
!this->batch)
{
this->batch = pb_tnc_batch_create(this->is_server, PB_BATCH_CLOSE);
}
if (this->batch)
{
pb_tnc_batch_type_t batch_type;
status_t status;
chunk_t data;
this->mutex->lock(this->mutex);
batch_type = this->batch->get_type(this->batch);
if (this->state_machine->send_batch(this->state_machine, batch_type))
@@ -401,15 +448,15 @@ METHOD(tls_t, build, status_t,
this->batch->destroy(this->batch);
this->batch = NULL;
this->mutex->unlock(this->mutex);
return status;
}
else
{
DBG1(DBG_TNC, "no TNCCS Batch to send");
return INVALID_STATE;
status = INVALID_STATE;
}
this->mutex->unlock(this->mutex);
return status;
}
METHOD(tls_t, is_server, bool,