define pb_tnc_state_machine_t object

This commit is contained in:
Andreas Steffen
2010-12-10 14:56:40 +01:00
parent 755f2419a5
commit 5988fc0dfd
6 changed files with 401 additions and 229 deletions
@@ -0,0 +1,292 @@
/*
* Copyright (C) 2010 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "pb_tnc_state_machine.h"
#include "messages/pb_error_message.h"
#include <debug.h>
#include <utils/linked_list.h>
#include <tls_writer.h>
#include <tls_reader.h>
#include <tnc/tnccs/tnccs.h>
ENUM(pb_tnc_state_names, PB_STATE_INIT, PB_STATE_END,
"Init",
"Server Working",
"Client Working",
"Decided",
"End"
);
/**
* PB-TNC State Machine (see section 3.2 of RFC 5793)
*
* Receive CRETRY SRETRY
* or SRETRY +----------------+
* +--+ | |
* v | v |
* +---------+ CRETRY +---------+
* CDATA | Server |<---------| Decided | CLOSE
* +----------->| Working |--------->| |-------+
* | +---------+ RESULT +---------+ |
* | ^ | | v
* | | | +---------------------->=======
* ======== | | CLOSE " End "
* " Init " CDATA| |SDATA =======
* ======== | | ^ ^
* | | | v | |
* | | SDATA +---------+ CLOSE | |
* | +-------->| Client |----------------------+ |
* | | Working | |
* | +---------+ |
* | | ^ |
* | +--+ |
* | Receive CRETRY |
* | CLOSE |
* +--------------------------------------------------+
*/
typedef struct private_pb_tnc_state_machine_t private_pb_tnc_state_machine_t;
/**
* Private data of a pb_tnc_state_machine_t object.
*
*/
struct private_pb_tnc_state_machine_t {
/**
* Public pb_pa_message_t interface.
*/
pb_tnc_state_machine_t public;
/**
* PB-TNC Server if TRUE, PB-TNC Client if FALSE
*/
bool is_server;
/**
* Current PB-TNC state
*/
pb_tnc_state_t state;
};
METHOD(pb_tnc_state_machine_t, get_state, pb_tnc_state_t,
private_pb_tnc_state_machine_t *this)
{
return this->state;
}
METHOD(pb_tnc_state_machine_t, receive_batch, bool,
private_pb_tnc_state_machine_t *this, pb_tnc_batch_type_t type)
{
pb_tnc_state_t old_state = this->state;
switch (this->state)
{
case PB_STATE_INIT:
if (this->is_server && type == PB_BATCH_CDATA)
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (!this->is_server && type == PB_BATCH_SDATA)
{
this->state = PB_STATE_CLIENT_WORKING;
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_SERVER_WORKING:
if (!this->is_server && type == PB_BATCH_SDATA)
{
this->state = PB_STATE_CLIENT_WORKING;
break;
}
if (!this->is_server && type == PB_BATCH_RESULT)
{
this->state = PB_STATE_DECIDED;
break;
}
if ((this->is_server && type == PB_BATCH_CRETRY) ||
(!this->is_server && type == PB_BATCH_SRETRY))
{
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_CLIENT_WORKING:
if (this->is_server && type == PB_BATCH_CDATA)
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (this->is_server && type == PB_BATCH_CRETRY)
{
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_DECIDED:
if ((this->is_server && type == PB_BATCH_CRETRY) ||
(!this->is_server && type == PB_BATCH_SRETRY))
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_END:
if (type == PB_BATCH_CLOSE)
{
break;
}
return FALSE;
}
if (this->state != old_state)
{
DBG2(DBG_TNC, "PB-TNC state transition from '%N' to '%N'",
pb_tnc_state_names, old_state, pb_tnc_state_names, this->state);
}
return TRUE;
}
METHOD(pb_tnc_state_machine_t, send_batch, bool,
private_pb_tnc_state_machine_t *this, pb_tnc_batch_type_t type)
{
pb_tnc_state_t old_state = this->state;
switch (this->state)
{
case PB_STATE_INIT:
if (!this->is_server && type == PB_BATCH_CDATA)
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (this->is_server && type == PB_BATCH_SDATA)
{
this->state = PB_STATE_CLIENT_WORKING;
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_SERVER_WORKING:
if (this->is_server && type == PB_BATCH_SDATA)
{
this->state = PB_STATE_CLIENT_WORKING;
break;
}
if (this->is_server && type == PB_BATCH_RESULT)
{
this->state = PB_STATE_DECIDED;
break;
}
if (this->is_server && type == PB_BATCH_SRETRY)
{
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_CLIENT_WORKING:
if (!this->is_server && type == PB_BATCH_CDATA)
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_DECIDED:
if ((this->is_server && type == PB_BATCH_SRETRY) ||
(!this->is_server && type == PB_BATCH_CRETRY))
{
this->state = PB_STATE_SERVER_WORKING;
break;
}
if (type == PB_BATCH_CLOSE)
{
this->state = PB_STATE_END;
break;
}
return FALSE;
case PB_STATE_END:
if (type == PB_BATCH_CLOSE)
{
break;
}
return FALSE;
}
if (this->state != old_state)
{
DBG2(DBG_TNC, "PB-TNC state transition from '%N' to '%N'",
pb_tnc_state_names, old_state, pb_tnc_state_names, this->state);
}
return TRUE;
}
METHOD(pb_tnc_state_machine_t, destroy, void,
private_pb_tnc_state_machine_t *this)
{
free(this);
}
/**
* See header
*/
pb_tnc_state_machine_t* pb_tnc_state_machine_create(bool is_server)
{
private_pb_tnc_state_machine_t *this;
INIT(this,
.public = {
.get_state = _get_state,
.receive_batch = _receive_batch,
.send_batch = _send_batch,
.destroy = _destroy,
},
.is_server = is_server,
.state = PB_STATE_INIT,
);
return &this->public;
}
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2010 Andreas Steffen
* HSR Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
/**
* @defgroup pb_tnc_state_machine pb_tnc_state_machine
* @{ @ingroup tnccs_20
*/
#ifndef PB_TNC_STATE_MACHINE_H_
#define PB_TNC_STATE_MACHINE_H_
typedef struct pb_tnc_state_machine_t pb_tnc_state_machine_t;
typedef enum pb_tnc_state_t pb_tnc_state_t;
#include "batch/pb_tnc_batch.h"
#include <library.h>
/**
* PB-TNC States (state machine) as defined in section 3.2 of RFC 5793
*/
enum pb_tnc_state_t {
PB_STATE_INIT,
PB_STATE_SERVER_WORKING,
PB_STATE_CLIENT_WORKING,
PB_STATE_DECIDED,
PB_STATE_END,
};
/**
* enum name for pb_tnc_state_t.
*/
extern enum_name_t *pb_tnc_state_names;
/**
* Interface for the PB-TNC state machine.
*/
struct pb_tnc_state_machine_t {
/**
* Get the current PB-TNC STATE
*
* @return current state
*/
pb_tnc_state_t (*get_state)(pb_tnc_state_machine_t *this);
/**
* Compute state transition due to received PB-TNC Batch
*
* @param type type of received batch
* @result TRUE if a valid transition was found, FALSE otherwise
*/
bool (*receive_batch)(pb_tnc_state_machine_t *this, pb_tnc_batch_type_t type);
/**
* Compute state transition due to sent PB-TNC Batch
*
* @param type type of sent batch
* @result TRUE if a valid transition was found, FALSE otherwise
*/
bool (*send_batch)(pb_tnc_state_machine_t *this, pb_tnc_batch_type_t type);
/**
* Destroys a pb_tnc_state_machine_t object.
*/
void (*destroy)(pb_tnc_state_machine_t *this);
};
/**
* Create and initialize a PB-TNC state machine
*
* @param is_server TRUE if PB-TNC server, FALSE if PB-TNC client
*/
pb_tnc_state_machine_t* pb_tnc_state_machine_create(bool is_server);
#endif /** PB_TNC_STATE_MACHINE_H_ @}*/