From e6b6fc881f70211a1510cf2a23230b6fc546e59c Mon Sep 17 00:00:00 2001 From: Andreas Steffen Date: Sun, 7 Nov 2010 01:17:21 +0100 Subject: [PATCH] implemented receive_message() function --- src/libcharon/plugins/tnc_imc/tnc_imc.c | 28 +++++++++++++++++++ .../plugins/tnc_imc/tnc_imc_manager.c | 21 ++++++++++++++ src/libcharon/plugins/tnc_imv/tnc_imv.c | 28 +++++++++++++++++++ .../plugins/tnc_imv/tnc_imv_manager.c | 22 +++++++++++++++ src/libcharon/plugins/tnccs_20/tnccs_20.c | 27 ++++++++++++++++-- src/libcharon/tnc/imc/imc.h | 10 +++++++ src/libcharon/tnc/imc/imc_manager.h | 14 ++++++++++ src/libcharon/tnc/imv/imv.h | 10 +++++++ src/libcharon/tnc/imv/imv_manager.h | 14 ++++++++++ 9 files changed, 172 insertions(+), 2 deletions(-) diff --git a/src/libcharon/plugins/tnc_imc/tnc_imc.c b/src/libcharon/plugins/tnc_imc/tnc_imc.c index f87853bf91..29f34d9392 100644 --- a/src/libcharon/plugins/tnc_imc/tnc_imc.c +++ b/src/libcharon/plugins/tnc_imc/tnc_imc.c @@ -84,6 +84,33 @@ METHOD(imc_t, set_message_types, void, } } +METHOD(imc_t, type_supported, bool, + private_tnc_imc_t *this, TNC_MessageType message_type) +{ + TNC_VendorID msg_vid, vid; + TNC_MessageSubtype msg_subtype, subtype; + int i; + + msg_vid = (message_type >> 8) & TNC_VENDORID_ANY; + msg_subtype = message_type & TNC_SUBTYPE_ANY; + + for (i = 0; i < this->type_count; i++) + { + vid = (this->supported_types[i] >> 8) & TNC_VENDORID_ANY; + subtype = this->supported_types[i] & TNC_SUBTYPE_ANY; + + if (this->supported_types[i] == message_type + || (subtype == TNC_SUBTYPE_ANY + && (msg_vid == vid || vid == TNC_VENDORID_ANY)) + || (vid == TNC_VENDORID_ANY + && (msg_subtype == subtype || subtype == TNC_SUBTYPE_ANY))) + { + return TRUE; + } + } + return FALSE; +} + METHOD(imc_t, destroy, void, private_tnc_imc_t *this) { @@ -106,6 +133,7 @@ imc_t* tnc_imc_create(char* name, char *filename) .get_id = _get_id, .get_name = _get_name, .set_message_types = _set_message_types, + .type_supported = _type_supported, .destroy = _destroy, }, ); diff --git a/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c b/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c index e6555cb514..8e0564446e 100644 --- a/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c +++ b/src/libcharon/plugins/tnc_imc/tnc_imc_manager.c @@ -124,6 +124,26 @@ METHOD(imc_manager_t, set_message_types, TNC_Result, return result; } +METHOD(imc_manager_t, receive_message, void, + private_tnc_imc_manager_t *this, TNC_ConnectionID connection_id, + TNC_BufferReference message, + TNC_UInt32 message_len, + TNC_MessageType message_type) +{ + enumerator_t *enumerator; + imc_t *imc; + + enumerator = this->imcs->create_enumerator(this->imcs); + while (enumerator->enumerate(enumerator, &imc)) + { + if (imc->receive_message && imc->type_supported(imc, message_type)) + { + imc->receive_message(imc->get_id(imc), connection_id, + message, message_len, message_type); + } + } + enumerator->destroy(enumerator); +} METHOD(imc_manager_t, destroy, void, private_tnc_imc_manager_t *this) @@ -157,6 +177,7 @@ imc_manager_t* tnc_imc_manager_create(void) .notify_connection_change = _notify_connection_change, .begin_handshake = _begin_handshake, .set_message_types = _set_message_types, + .receive_message = _receive_message, .destroy = _destroy, }, .imcs = linked_list_create(), diff --git a/src/libcharon/plugins/tnc_imv/tnc_imv.c b/src/libcharon/plugins/tnc_imv/tnc_imv.c index 722f6d946d..3dfc50b4fe 100644 --- a/src/libcharon/plugins/tnc_imv/tnc_imv.c +++ b/src/libcharon/plugins/tnc_imv/tnc_imv.c @@ -84,6 +84,33 @@ METHOD(imv_t, set_message_types, void, } } +METHOD(imv_t, type_supported, bool, + private_tnc_imv_t *this, TNC_MessageType message_type) +{ + TNC_VendorID msg_vid, vid; + TNC_MessageSubtype msg_subtype, subtype; + int i; + + msg_vid = (message_type >> 8) & TNC_VENDORID_ANY; + msg_subtype = message_type & TNC_SUBTYPE_ANY; + + for (i = 0; i < this->type_count; i++) + { + vid = (this->supported_types[i] >> 8) & TNC_VENDORID_ANY; + subtype = this->supported_types[i] & TNC_SUBTYPE_ANY; + + if (this->supported_types[i] == message_type + || (subtype == TNC_SUBTYPE_ANY + && (msg_vid == vid || vid == TNC_VENDORID_ANY)) + || (vid == TNC_VENDORID_ANY + && (msg_subtype == subtype || subtype == TNC_SUBTYPE_ANY))) + { + return TRUE; + } + } + return FALSE; +} + METHOD(imv_t, destroy, void, private_tnc_imv_t *this) { @@ -106,6 +133,7 @@ imv_t* tnc_imv_create(char *name, char *filename) .get_id = _get_id, .get_name = _get_name, .set_message_types = _set_message_types, + .type_supported = _type_supported, .destroy = _destroy, }, ); diff --git a/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c b/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c index a5309e4622..fa6d35d412 100644 --- a/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c +++ b/src/libcharon/plugins/tnc_imv/tnc_imv_manager.c @@ -109,6 +109,27 @@ METHOD(imv_manager_t, set_message_types, TNC_Result, return result; } +METHOD(imv_manager_t, receive_message, void, + private_tnc_imv_manager_t *this, TNC_ConnectionID connection_id, + TNC_BufferReference message, + TNC_UInt32 message_len, + TNC_MessageType message_type) +{ + enumerator_t *enumerator; + imv_t *imv; + + enumerator = this->imvs->create_enumerator(this->imvs); + while (enumerator->enumerate(enumerator, &imv)) + { + if (imv->receive_message && imv->type_supported(imv, message_type)) + { + imv->receive_message(imv->get_id(imv), connection_id, + message, message_len, message_type); + } + } + enumerator->destroy(enumerator); +} + METHOD(imv_manager_t, destroy, void, private_tnc_imv_manager_t *this) { @@ -140,6 +161,7 @@ imv_manager_t* tnc_imv_manager_create(void) .add = _add, .notify_connection_change = _notify_connection_change, .set_message_types = _set_message_types, + .receive_message = _receive_message, .destroy = _destroy, }, .imvs = linked_list_create(), diff --git a/src/libcharon/plugins/tnccs_20/tnccs_20.c b/src/libcharon/plugins/tnccs_20/tnccs_20.c index 5afc6e196a..8f6f11e500 100644 --- a/src/libcharon/plugins/tnccs_20/tnccs_20.c +++ b/src/libcharon/plugins/tnccs_20/tnccs_20.c @@ -63,6 +63,9 @@ METHOD(tnccs_t, send_message, void, METHOD(tls_t, process, status_t, private_tnccs_20_t *this, void *buf, size_t buflen) { + char *pos; + size_t len; + if (this->is_server && !this->connection_id) { this->connection_id = charon->tnccs->create_connection(charon->tnccs, @@ -73,14 +76,34 @@ METHOD(tls_t, process, status_t, DBG1(DBG_TNC, "received TNCCS Batch (%u bytes) for Connection ID %u", buflen, this->connection_id); DBG3(DBG_TNC, "%.*s", buflen, buf); - + pos = strchr(buf, '|'); + if (pos) + { + pos++; + len = buflen - ((char*)buf - pos); + } + else + { + pos = buf; + len = buflen; + } + if (this->is_server) + { + charon->imvs->receive_message(charon->imvs, this->connection_id, + pos, len, 0x0080ab31); + } + else + { + charon->imcs->receive_message(charon->imcs, this->connection_id, + pos, len, 0x0080ab31); + } return NEED_MORE; } METHOD(tls_t, build, status_t, private_tnccs_20_t *this, void *buf, size_t *buflen, size_t *msglen) { - char *msg = this->is_server ? "|tncs->tncc 2.0|" : "|tncc->tncs 2.0|"; + char *msg = this->is_server ? "tncs->tncc 2.0|" : "tncc->tncs 2.0|"; size_t len; this->batch = chunk_clone(chunk_create(msg, strlen(msg))); diff --git a/src/libcharon/tnc/imc/imc.h b/src/libcharon/tnc/imc/imc.h index 2f859e87b6..0a50767ad9 100644 --- a/src/libcharon/tnc/imc/imc.h +++ b/src/libcharon/tnc/imc/imc.h @@ -23,6 +23,8 @@ #include +#include + typedef struct imc_t imc_t; struct imc_t { @@ -151,6 +153,14 @@ struct imc_t { void (*set_message_types)(imc_t *this, TNC_MessageTypeList supported_types, TNC_UInt32 type_count); + /** + * Check if the IMC supports a given message type. + * + * @param message_type message type + * @return TRUE if supported + */ + bool (*type_supported)(imc_t *this, TNC_MessageType message_type); + /** * Destroys an imc_t object. */ diff --git a/src/libcharon/tnc/imc/imc_manager.h b/src/libcharon/tnc/imc/imc_manager.h index 9621f05cc6..da78d8022a 100644 --- a/src/libcharon/tnc/imc/imc_manager.h +++ b/src/libcharon/tnc/imc/imc_manager.h @@ -69,6 +69,20 @@ struct imc_manager_t { TNC_MessageTypeList supported_types, TNC_UInt32 type_count); + /** + * Delivers a message to interested IMCs. + * + * @param connection_id ID of connection over which message was received + * @param message message + * @param message_len message length + * @param message_type message type + */ + void (*receive_message)(imc_manager_t *this, + TNC_ConnectionID connection_id, + TNC_BufferReference message, + TNC_UInt32 message_len, + TNC_MessageType message_type); + /** * Destroy an IMC manager and all its controlled instances. */ diff --git a/src/libcharon/tnc/imv/imv.h b/src/libcharon/tnc/imv/imv.h index fa8e048fd8..e5194a9ec4 100644 --- a/src/libcharon/tnc/imv/imv.h +++ b/src/libcharon/tnc/imv/imv.h @@ -23,6 +23,8 @@ #include +#include + typedef struct imv_t imv_t; struct imv_t { @@ -151,6 +153,14 @@ struct imv_t { void (*set_message_types)(imv_t *this, TNC_MessageTypeList supported_types, TNC_UInt32 type_count); + /** + * Check if the IMV supports a given message type. + * + * @param message_type message type + * @return TRUE if supported + */ + bool (*type_supported)(imv_t *this, TNC_MessageType message_type); + /** * Destroys an imv_t object. */ diff --git a/src/libcharon/tnc/imv/imv_manager.h b/src/libcharon/tnc/imv/imv_manager.h index 4d985552f7..b78c94875c 100644 --- a/src/libcharon/tnc/imv/imv_manager.h +++ b/src/libcharon/tnc/imv/imv_manager.h @@ -62,6 +62,20 @@ struct imv_manager_t { TNC_MessageTypeList supported_types, TNC_UInt32 type_count); + /** + * Delivers a message to interested IMVs. + * + * @param connection_id ID of connection over which message was received + * @param message message + * @param message_len message length + * @param message_type message type + */ + void (*receive_message)(imv_manager_t *this, + TNC_ConnectionID connection_id, + TNC_BufferReference message, + TNC_UInt32 message_len, + TNC_MessageType message_type); + /** * Destroy an IMV manager and all its controlled instances. */ -- 2.47.2