moved batch size calculation into pb_tnc_batch_t

This commit is contained in:
Andreas Steffen
2012-07-11 17:09:05 +02:00
parent d7dcbc95a9
commit a5c79d0175
4 changed files with 50 additions and 31 deletions
@@ -1,6 +1,8 @@
INCLUDES = \ INCLUDES = \
-I$(top_srcdir)/src/libstrongswan \ -I$(top_srcdir)/src/libstrongswan \
-I$(top_srcdir)/src/libcharon \
-I$(top_srcdir)/src/libhydra \
-I$(top_srcdir)/src/libtls \ -I$(top_srcdir)/src/libtls \
-I$(top_srcdir)/src/libtncif \ -I$(top_srcdir)/src/libtncif \
-I$(top_srcdir)/src/libtnccs -I$(top_srcdir)/src/libtnccs
@@ -51,6 +51,7 @@ typedef struct private_pb_tnc_batch_t private_pb_tnc_batch_t;
#define PB_TNC_BATCH_FLAG_NONE 0x00 #define PB_TNC_BATCH_FLAG_NONE 0x00
#define PB_TNC_BATCH_FLAG_D (1<<7) #define PB_TNC_BATCH_FLAG_D (1<<7)
#define PB_TNC_BATCH_HEADER_SIZE 8
/** /**
* PB-TNC Message (see section 4.2 of RFC 5793) * PB-TNC Message (see section 4.2 of RFC 5793)
@@ -70,6 +71,7 @@ typedef struct private_pb_tnc_batch_t private_pb_tnc_batch_t;
#define PB_TNC_FLAG_NONE 0x00 #define PB_TNC_FLAG_NONE 0x00
#define PB_TNC_FLAG_NOSKIP (1<<7) #define PB_TNC_FLAG_NOSKIP (1<<7)
#define PB_TNC_HEADER_SIZE 12
#define PB_TNC_RESERVED_MSG_TYPE 0xffffffff #define PB_TNC_RESERVED_MSG_TYPE 0xffffffff
@@ -93,6 +95,16 @@ struct private_pb_tnc_batch_t {
*/ */
pb_tnc_batch_type_t type; pb_tnc_batch_type_t type;
/**
* Current PB-TNC Batch size
*/
size_t batch_len;
/**
* Maximum PB-TNC Batch size
*/
size_t max_batch_len;
/** /**
* linked list of PB-TNC messages * linked list of PB-TNC messages
*/ */
@@ -126,41 +138,46 @@ METHOD(pb_tnc_batch_t, get_encoding, chunk_t,
return this->encoding; return this->encoding;
} }
METHOD(pb_tnc_batch_t, add_msg, void, METHOD(pb_tnc_batch_t, add_msg, bool,
private_pb_tnc_batch_t *this, pb_tnc_msg_t* msg) private_pb_tnc_batch_t *this, pb_tnc_msg_t* msg)
{ {
chunk_t msg_value;
size_t msg_len;
msg->build(msg);
msg_value = msg->get_encoding(msg);
msg_len = PB_TNC_HEADER_SIZE + msg_value.len;
if (this->batch_len + msg_len > this->max_batch_len)
{
/* message just does not fit into this batch */
return FALSE;
}
this->batch_len += msg_len;
DBG2(DBG_TNC, "adding %N message", pb_tnc_msg_type_names, DBG2(DBG_TNC, "adding %N message", pb_tnc_msg_type_names,
msg->get_type(msg)); msg->get_type(msg));
this->messages->insert_last(this->messages, msg); this->messages->insert_last(this->messages, msg);
return TRUE;
} }
METHOD(pb_tnc_batch_t, build, void, METHOD(pb_tnc_batch_t, build, void,
private_pb_tnc_batch_t *this) private_pb_tnc_batch_t *this)
{ {
u_int32_t batch_len, msg_len; u_int32_t msg_len;
chunk_t msg_value; chunk_t msg_value;
enumerator_t *enumerator; enumerator_t *enumerator;
pb_tnc_msg_type_t msg_type; pb_tnc_msg_type_t msg_type;
pb_tnc_msg_t *msg; pb_tnc_msg_t *msg;
bio_writer_t *writer; bio_writer_t *writer;
/* compute total PB-TNC batch size by summing over all messages */
batch_len = PB_TNC_BATCH_HEADER_SIZE;
enumerator = this->messages->create_enumerator(this->messages);
while (enumerator->enumerate(enumerator, &msg))
{
msg_value = msg->get_encoding(msg);
batch_len += PB_TNC_HEADER_SIZE + msg_value.len;
}
enumerator->destroy(enumerator);
/* build PB-TNC batch header */ /* build PB-TNC batch header */
writer = bio_writer_create(batch_len); writer = bio_writer_create(this->batch_len);
writer->write_uint8 (writer, PB_TNC_VERSION); writer->write_uint8 (writer, PB_TNC_VERSION);
writer->write_uint8 (writer, this->is_server ? writer->write_uint8 (writer, this->is_server ?
PB_TNC_BATCH_FLAG_D : PB_TNC_BATCH_FLAG_NONE); PB_TNC_BATCH_FLAG_D : PB_TNC_BATCH_FLAG_NONE);
writer->write_uint16(writer, this->type); writer->write_uint16(writer, this->type);
writer->write_uint32(writer, batch_len); writer->write_uint32(writer, this->batch_len);
/* build PB-TNC messages */ /* build PB-TNC messages */
enumerator = this->messages->create_enumerator(this->messages); enumerator = this->messages->create_enumerator(this->messages);
@@ -487,7 +504,8 @@ METHOD(pb_tnc_batch_t, destroy, void,
/** /**
* See header * See header
*/ */
pb_tnc_batch_t* pb_tnc_batch_create(bool is_server, pb_tnc_batch_type_t type) pb_tnc_batch_t* pb_tnc_batch_create(bool is_server, pb_tnc_batch_type_t type,
size_t max_batch_len)
{ {
private_pb_tnc_batch_t *this; private_pb_tnc_batch_t *this;
@@ -504,6 +522,8 @@ pb_tnc_batch_t* pb_tnc_batch_create(bool is_server, pb_tnc_batch_type_t type)
}, },
.is_server = is_server, .is_server = is_server,
.type = type, .type = type,
.max_batch_len = max_batch_len,
.batch_len = PB_TNC_BATCH_HEADER_SIZE,
.messages = linked_list_create(), .messages = linked_list_create(),
.errors = linked_list_create(), .errors = linked_list_create(),
); );
@@ -29,9 +29,6 @@ typedef struct pb_tnc_batch_t pb_tnc_batch_t;
#include <library.h> #include <library.h>
#define PB_TNC_BATCH_HEADER_SIZE 8
#define PB_TNC_HEADER_SIZE 12
/** /**
* PB-TNC Batch Types as defined in section 4.1 of RFC 5793 * PB-TNC Batch Types as defined in section 4.1 of RFC 5793
*/ */
@@ -74,8 +71,9 @@ struct pb_tnc_batch_t {
* Add a PB-TNC Message * Add a PB-TNC Message
* *
* @param msg PB-TNC message to be addedd * @param msg PB-TNC message to be addedd
* @return TRUE if message fit into batch and was added
*/ */
void (*add_msg)(pb_tnc_batch_t *this, pb_tnc_msg_t* msg); bool (*add_msg)(pb_tnc_batch_t *this, pb_tnc_msg_t* msg);
/** /**
* Build the PB-TNC Batch * Build the PB-TNC Batch
@@ -116,8 +114,10 @@ struct pb_tnc_batch_t {
* *
* @param is_server TRUE if server, FALSE if client * @param is_server TRUE if server, FALSE if client
* @param type PB-TNC batch type * @param type PB-TNC batch type
* @param max_batch_len maximum size the PB-TNC batch
*/ */
pb_tnc_batch_t* pb_tnc_batch_create(bool is_server, pb_tnc_batch_type_t type); pb_tnc_batch_t* pb_tnc_batch_create(bool is_server, pb_tnc_batch_type_t type,
size_t max_batch_len);
/** /**
* Create an unprocessed PB-TNC Batch from data * Create an unprocessed PB-TNC Batch from data
+8 -11
View File
@@ -639,29 +639,26 @@ METHOD(tls_t, build, status_t,
{ {
pb_tnc_batch_t *batch; pb_tnc_batch_t *batch;
pb_tnc_msg_t *msg; pb_tnc_msg_t *msg;
chunk_t msg_value, data; chunk_t data;
int msg_count; int msg_count;
size_t batch_len;
enumerator_t *enumerator; enumerator_t *enumerator;
if (this->state_machine->send_batch(this->state_machine, this->batch_type)) if (this->state_machine->send_batch(this->state_machine, this->batch_type))
{ {
batch = pb_tnc_batch_create(this->is_server, this->batch_type); batch = pb_tnc_batch_create(this->is_server, this->batch_type,
batch_len = PB_TNC_BATCH_HEADER_SIZE; min(this->max_batch_len, *buflen));
enumerator = this->messages->create_enumerator(this->messages); enumerator = this->messages->create_enumerator(this->messages);
while (enumerator->enumerate(enumerator, &msg)) while (enumerator->enumerate(enumerator, &msg))
{ {
msg->build(msg); if (batch->add_msg(batch, msg))
msg_value = msg->get_encoding(msg); {
batch_len += PB_TNC_HEADER_SIZE + msg_value.len; this->messages->remove_at(this->messages, enumerator);
if (batch_len > min(this->max_batch_len, *buflen)) }
else
{ {
/* message does not fit into batch of maximum size */
break; break;
} }
batch->add_msg(batch, msg);
this->messages->remove_at(this->messages, enumerator);
} }
enumerator->destroy(enumerator); enumerator->destroy(enumerator);