2005-11-08 16:19:06 +00:00
|
|
|
/**
|
|
|
|
* @file generator.c
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-10 09:31:07 +00:00
|
|
|
* @brief Generic generator class used to generate IKEv2-header and payloads.
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-08 16:19:06 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2005 Jan Hutter, Martin Willi
|
|
|
|
* 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 <stdlib.h>
|
2005-11-09 13:34:36 +00:00
|
|
|
#include <string.h>
|
2005-11-10 09:31:07 +00:00
|
|
|
#include <arpa/inet.h>
|
2005-11-10 14:56:35 +00:00
|
|
|
#include <stdio.h>
|
2005-11-08 16:19:06 +00:00
|
|
|
|
2005-11-11 13:52:11 +00:00
|
|
|
|
2005-11-08 16:19:06 +00:00
|
|
|
#include "generator.h"
|
|
|
|
|
2005-11-11 13:52:11 +00:00
|
|
|
#include "types.h"
|
|
|
|
#include "utils/allocator.h"
|
2005-11-14 06:55:51 +00:00
|
|
|
#include "payloads/payload.h"
|
2005-11-11 13:52:11 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
typedef struct private_generator_context_s private_generator_context_t;
|
2005-11-09 13:34:36 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
struct private_generator_context_s{
|
|
|
|
/**
|
|
|
|
* Public part of the context
|
|
|
|
*/
|
|
|
|
generator_context_t public;
|
2005-11-09 13:34:36 +00:00
|
|
|
|
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Buffer used to generate the data into.
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
|
|
|
u_int8_t *buffer;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Current write position in buffer (one byte aligned).
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
|
|
|
u_int8_t *out_position;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Position of last byte in buffer.
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
|
|
|
u_int8_t *roof_position;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Current bit writing to in current byte (between 0 and 7).
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
|
|
|
size_t current_bit;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Associated data struct to read informations from.
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
|
|
|
void * data_struct;
|
2005-11-10 09:54:09 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 17:17:46 +00:00
|
|
|
* Writes the current buffer content into a chunk_t
|
|
|
|
*
|
|
|
|
* Memory of specific chunk_t gets allocated.
|
2005-11-10 09:35:28 +00:00
|
|
|
*
|
2005-11-10 17:17:46 +00:00
|
|
|
* @param generator_infos_t calling generator_infos_t object
|
|
|
|
* @param data pointer of chunk_t to write to
|
|
|
|
* @return
|
|
|
|
* - SUCCESSFUL if succeeded
|
|
|
|
* - OUT_OF_RES otherwise
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*write_chunk) (private_generator_context_t *this,chunk_t *data);
|
|
|
|
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Makes sure enough space is available in buffer to store amount of bits.
|
|
|
|
*
|
|
|
|
* If buffer is to small to hold the specific amount of bits it
|
|
|
|
* is increased using reallocation function of allocator.
|
2005-11-10 09:35:28 +00:00
|
|
|
*
|
2005-11-10 09:54:09 +00:00
|
|
|
* @param generator_infos_t calling generator_infos_t object
|
|
|
|
* @param bits number of bits to make available in buffer
|
|
|
|
* @return
|
|
|
|
* - SUCCESSFUL if succeeded
|
|
|
|
* - OUT_OF_RES otherwise
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*make_space_available) (private_generator_context_t *this,size_t bits);
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-10 09:54:09 +00:00
|
|
|
/**
|
|
|
|
* Writes a specific amount of byte into the buffer.
|
|
|
|
*
|
|
|
|
* If buffer is to small to hold the specific amount of bytes it
|
|
|
|
* is increased.
|
|
|
|
*
|
|
|
|
* @param generator_infos_t calling generator_infos_t object
|
|
|
|
* @param bytes pointer to bytes to write
|
|
|
|
* @param number_of_bytes number of bytes to write into buffer
|
|
|
|
* @return
|
|
|
|
* - SUCCESSFUL if succeeded
|
|
|
|
* - OUT_OF_RES otherwise
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*write_bytes_to_buffer) (private_generator_context_t *this,void * bytes,size_t number_of_bytes);
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Implements generator_infos_t's increase_buffer function.
|
|
|
|
* See #generator_infos_s.increase_buffer.
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generator_context_make_space_available (private_generator_context_t *this, size_t bits)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
2005-11-10 09:31:07 +00:00
|
|
|
while ((((this->roof_position - this->out_position) * 8) - this->current_bit) < bits)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
|
|
|
size_t old_buffer_size = ((this->roof_position) - ( this->buffer));
|
|
|
|
size_t new_buffer_size = old_buffer_size + GENERATOR_DATA_BUFFER_INCREASE_VALUE;
|
|
|
|
size_t out_position_offset = ((this->out_position) - (this->buffer));
|
|
|
|
u_int8_t *new_buffer;
|
2005-11-10 09:31:07 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
new_buffer = allocator_realloc(this->buffer,new_buffer_size);
|
|
|
|
if (new_buffer == NULL)
|
|
|
|
{
|
|
|
|
return OUT_OF_RES;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
this->buffer = new_buffer;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
this->out_position = (this->buffer + out_position_offset);
|
|
|
|
this->roof_position = (this->buffer + new_buffer_size);
|
2005-11-10 09:31:07 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
}
|
2005-11-10 09:31:07 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-10 09:54:09 +00:00
|
|
|
/**
|
|
|
|
* Implements generator_infos_t's write_bytes_to_buffer function.
|
|
|
|
* See #generator_infos_s.write_bytes_to_buffer.
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generator_context_write_bytes_to_buffer (private_generator_context_t *this,void * bytes,size_t number_of_bytes)
|
2005-11-09 15:12:48 +00:00
|
|
|
{
|
|
|
|
u_int8_t *read_position = (u_int8_t *) bytes;
|
|
|
|
int i;
|
|
|
|
status_t status;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
status = this->make_space_available(this,number_of_bytes * 8);
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
for (i = 0; i < number_of_bytes; i++)
|
|
|
|
{
|
|
|
|
*(this->out_position) = *(read_position);
|
|
|
|
read_position++;
|
|
|
|
this->out_position++;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-11-10 09:54:09 +00:00
|
|
|
/**
|
|
|
|
* Implements generator_infos_t's write_chunk function.
|
|
|
|
* See #generator_infos_s.write_chunk.
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generator_context_write_chunk (private_generator_context_t *this,chunk_t *data)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
|
|
|
size_t data_length = this->out_position - this->buffer;
|
2005-11-10 17:17:46 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
if (this->current_bit > 0)
|
|
|
|
data_length++;
|
|
|
|
data->ptr = allocator_alloc(data_length);
|
|
|
|
if (data->ptr == NULL)
|
|
|
|
{
|
|
|
|
data->len = 0;
|
|
|
|
return OUT_OF_RES;
|
|
|
|
}
|
|
|
|
memcpy(data->ptr,this->buffer,data_length);
|
|
|
|
data->len = data_length;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-10 09:54:09 +00:00
|
|
|
/**
|
|
|
|
* Implements generator_infos_t's destroy function.
|
|
|
|
* See #generator_infos_s.destroy.
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generator_context_destroy (private_generator_context_t *this)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
|
|
|
allocator_free(this->buffer);
|
|
|
|
allocator_free(this);
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
/*
|
|
|
|
* Described in header
|
2005-11-09 13:34:36 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static generator_context_t * generator_context_create(generator_t *generator)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
private_generator_context_t *this = allocator_alloc_thing(private_generator_context_t);
|
2005-11-09 13:34:36 +00:00
|
|
|
|
|
|
|
if (this == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* object methods */
|
2005-11-10 17:17:46 +00:00
|
|
|
this->public.destroy = (status_t (*) (generator_context_t *this))generator_context_destroy;
|
|
|
|
this->make_space_available = generator_context_make_space_available;
|
|
|
|
this->write_chunk = generator_context_write_chunk;
|
|
|
|
this->write_bytes_to_buffer = generator_context_write_bytes_to_buffer;
|
2005-11-09 13:34:36 +00:00
|
|
|
|
|
|
|
/* allocate memory for buffer */
|
|
|
|
this->buffer = allocator_alloc(GENERATOR_DATA_BUFFER_SIZE);
|
|
|
|
if (this->buffer == NULL)
|
|
|
|
{
|
|
|
|
allocator_free(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/* set private data */
|
|
|
|
this->out_position = this->buffer;
|
|
|
|
this->roof_position = this->buffer + GENERATOR_DATA_BUFFER_SIZE;
|
2005-11-10 17:17:46 +00:00
|
|
|
this->data_struct = NULL;
|
2005-11-09 13:34:36 +00:00
|
|
|
this->current_bit = 0;
|
2005-11-10 17:17:46 +00:00
|
|
|
return &(this->public);
|
2005-11-09 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-08 16:19:06 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Private part of a generator_t object
|
2005-11-08 16:19:06 +00:00
|
|
|
*/
|
|
|
|
typedef struct private_generator_s private_generator_t;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
|
|
|
struct private_generator_s {
|
2005-11-08 16:19:06 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Public part of a generator_t object
|
2005-11-08 16:19:06 +00:00
|
|
|
*/
|
|
|
|
generator_t public;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/* private functions and fields */
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Generates a chunk_t with specific encoding rules.
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-10 09:54:09 +00:00
|
|
|
* Iems are bytewhise written.
|
2005-11-09 08:40:10 +00:00
|
|
|
*
|
2005-11-10 09:54:09 +00:00
|
|
|
* @param this private_generator_t object
|
|
|
|
* @param data_struct data_struct to read data from
|
|
|
|
* @param encoding_rules pointer to first encoding_rule
|
|
|
|
* of encoding rules array
|
|
|
|
* @param encoding_rules_count number of encoding rules
|
|
|
|
* in encoding rules array
|
|
|
|
* @param data pointer to chunk_t where to write the data in
|
2005-11-09 09:11:06 +00:00
|
|
|
*
|
2005-11-10 09:54:09 +00:00
|
|
|
* @return - SUCCESS if succeeded
|
|
|
|
* - OUT_OF_RES if out of ressources
|
2005-11-09 08:40:10 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*generate) (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, private_generator_context_t *generator_context);
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Generates a U_INT-Field type
|
|
|
|
*
|
|
|
|
* @param this private_generator_t object
|
|
|
|
* @param int_type type of U_INT field (U_INT_4, U_INT_8, etc.)
|
|
|
|
* @param offset offset of value in data struct
|
2005-11-10 17:17:46 +00:00
|
|
|
* @param generator_contexts generator_contexts_t object where the context is written or read from
|
2005-11-10 09:54:09 +00:00
|
|
|
* @return - SUCCESS if succeeded
|
|
|
|
* - OUT_OF_RES if out of ressources
|
2005-11-09 08:40:10 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*generate_u_int_type) (private_generator_t *this,encoding_type_t int_type,u_int32_t offset, private_generator_context_t *generator_context);
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-10 14:56:35 +00:00
|
|
|
/**
|
|
|
|
* Generates a RESERVED BIT field or a RESERVED BYTE field
|
|
|
|
*
|
|
|
|
* @param this private_generator_t object
|
2005-11-10 17:17:46 +00:00
|
|
|
* @param generator_contexts generator_contexts_t object where the context is written or read from
|
2005-11-10 14:56:35 +00:00
|
|
|
* @param bits number of bits to generate
|
|
|
|
* @return - SUCCESS if succeeded
|
|
|
|
* - OUT_OF_RES if out of ressources
|
|
|
|
* - FAILED if bit count not supported
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*generate_reserved_field) (private_generator_t *this,private_generator_context_t *generator_context,int bits);
|
2005-11-10 14:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a FLAG field
|
|
|
|
*
|
|
|
|
* @param this private_generator_t object
|
2005-11-10 17:17:46 +00:00
|
|
|
* @param generator_contexts generator_contexts_t object where the context is written or read from
|
2005-11-10 14:56:35 +00:00
|
|
|
* @param offset offset of flag value in data struct
|
|
|
|
* @return - SUCCESS if succeeded
|
|
|
|
* - OUT_OF_RES if out of ressources
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t (*generate_flag) (private_generator_t *this,private_generator_context_t *generator_context,u_int32_t offset);
|
2005-11-10 14:56:35 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/**
|
|
|
|
* Pointer to the payload informations needed to automatic
|
|
|
|
* generate a specific payload type
|
|
|
|
*/
|
|
|
|
payload_info_t **payload_infos;
|
2005-11-08 16:19:06 +00:00
|
|
|
};
|
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Implements private_generator_t's generate_u_int_type function.
|
|
|
|
* See #private_generator_s.generate_u_int_type.
|
2005-11-09 08:40:10 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generate_u_int_type (private_generator_t *this,encoding_type_t int_type,u_int32_t offset,private_generator_context_t *generator_context)
|
2005-11-09 08:40:10 +00:00
|
|
|
{
|
2005-11-09 09:11:06 +00:00
|
|
|
size_t number_of_bits = 0;
|
2005-11-09 13:34:36 +00:00
|
|
|
status_t status;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
switch (int_type)
|
|
|
|
{
|
|
|
|
case U_INT_4:
|
|
|
|
number_of_bits = 4;
|
|
|
|
break;
|
|
|
|
case U_INT_8:
|
|
|
|
number_of_bits = 8;
|
|
|
|
break;
|
|
|
|
case U_INT_16:
|
|
|
|
number_of_bits = 16;
|
|
|
|
break;
|
|
|
|
case U_INT_32:
|
|
|
|
number_of_bits = 32;
|
|
|
|
break;
|
|
|
|
case U_INT_64:
|
|
|
|
number_of_bits = 64;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-10 17:17:46 +00:00
|
|
|
if (((number_of_bits % 8) == 0) && (generator_context->current_bit != 0))
|
2005-11-09 15:12:48 +00:00
|
|
|
{
|
|
|
|
/* current bit has to be zero for values greater then 4 bits */
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
status = generator_context->make_space_available(generator_context,number_of_bits);
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
2005-11-09 15:12:48 +00:00
|
|
|
|
|
|
|
switch (int_type)
|
2005-11-09 13:34:36 +00:00
|
|
|
{
|
2005-11-09 15:12:48 +00:00
|
|
|
case U_INT_4:
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
if (generator_context->current_bit == 0)
|
2005-11-10 09:35:28 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int8_t high_val = *((u_int8_t *)(generator_context->data_struct + offset)) << 4;
|
|
|
|
u_int8_t low_val = *(generator_context->out_position) & 0x0F;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
*(generator_context->out_position) = high_val | low_val;
|
2005-11-09 15:12:48 +00:00
|
|
|
/* write position is not changed, just bit position is moved */
|
2005-11-10 17:17:46 +00:00
|
|
|
generator_context->current_bit = 4;
|
2005-11-09 15:12:48 +00:00
|
|
|
}
|
2005-11-10 17:17:46 +00:00
|
|
|
else if (generator_context->current_bit == 4)
|
2005-11-09 15:12:48 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int high_val = *(generator_context->out_position) & 0xF0;
|
|
|
|
u_int low_val = *((u_int8_t *)(generator_context->data_struct + offset)) & 0x0F;
|
|
|
|
*(generator_context->out_position) = high_val | low_val;
|
|
|
|
generator_context->out_position++;
|
|
|
|
generator_context->current_bit = 0;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* 4 Bit integers must have a 4 bit alignment */
|
|
|
|
return FAILED;
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
case U_INT_8:
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
*generator_context->out_position = *((u_int8_t *)(generator_context->data_struct + offset));
|
|
|
|
generator_context->out_position++;
|
2005-11-09 15:12:48 +00:00
|
|
|
break;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
}
|
|
|
|
case U_INT_16:
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int16_t int16_val = htons(*((u_int16_t*)(generator_context->data_struct + offset)));
|
|
|
|
generator_context->write_bytes_to_buffer(generator_context,&int16_val,sizeof(u_int16_t));
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_32:
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int32_t int32_val = htonl(*((u_int32_t*)(generator_context->data_struct + offset)));
|
|
|
|
generator_context->write_bytes_to_buffer(generator_context,&int32_val,sizeof(u_int32_t));
|
2005-11-09 15:12:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_64:
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int32_t int32_val_low = htonl(*((u_int32_t*)(generator_context->data_struct + offset)));
|
|
|
|
u_int32_t int32_val_high = htonl(*((u_int32_t*)(generator_context->data_struct + offset) + 1));
|
|
|
|
generator_context->write_bytes_to_buffer(generator_context,&int32_val_high,sizeof(u_int32_t));
|
|
|
|
generator_context->write_bytes_to_buffer(generator_context,&int32_val_low,sizeof(u_int32_t));
|
2005-11-09 15:12:48 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 15:12:48 +00:00
|
|
|
default:
|
2005-11-09 13:34:36 +00:00
|
|
|
return FAILED;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
}
|
2005-11-09 15:12:48 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generate_reserved_field (private_generator_t *this,private_generator_context_t *generator_context,int bits)
|
2005-11-10 14:56:35 +00:00
|
|
|
{
|
|
|
|
status_t status;
|
|
|
|
|
|
|
|
if ((bits != 1) && (bits != 8))
|
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-10 17:17:46 +00:00
|
|
|
status = generator_context->make_space_available(generator_context,bits);
|
2005-11-10 14:56:35 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bits == 1)
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int8_t reserved_bit = ~(1 << (7 - generator_context->current_bit));
|
2005-11-10 14:56:35 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
*(generator_context->out_position) = *(generator_context->out_position) & reserved_bit;
|
|
|
|
generator_context->current_bit++;
|
|
|
|
if (generator_context->current_bit >= 8)
|
2005-11-10 14:56:35 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
generator_context->current_bit = generator_context->current_bit % 8;
|
|
|
|
generator_context->out_position++;
|
2005-11-10 14:56:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* one byte */
|
2005-11-10 17:17:46 +00:00
|
|
|
if (generator_context->current_bit > 0)
|
2005-11-10 14:56:35 +00:00
|
|
|
{
|
|
|
|
return FAILED;
|
|
|
|
}
|
2005-11-10 17:17:46 +00:00
|
|
|
*(generator_context->out_position) = 0x00;
|
|
|
|
generator_context->out_position++;
|
2005-11-10 14:56:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generate_flag (private_generator_t *this,private_generator_context_t *generator_context,u_int32_t offset)
|
2005-11-10 14:56:35 +00:00
|
|
|
{
|
|
|
|
status_t status;
|
2005-11-10 17:17:46 +00:00
|
|
|
u_int8_t flag_value = (*((bool *) (generator_context->data_struct + offset))) ? 1 : 0;
|
|
|
|
u_int8_t flag = (flag_value << (7 - generator_context->current_bit));
|
2005-11-10 14:56:35 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
status = generator_context->make_space_available(generator_context,1);
|
2005-11-10 14:56:35 +00:00
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
*(generator_context->out_position) = *(generator_context->out_position) | flag;
|
2005-11-10 14:56:35 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
generator_context->current_bit++;
|
|
|
|
if (generator_context->current_bit >= 8)
|
2005-11-10 14:56:35 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
generator_context->current_bit = generator_context->current_bit % 8;
|
|
|
|
generator_context->out_position++;
|
2005-11-10 14:56:35 +00:00
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Implements private_generator_t's generate function.
|
|
|
|
* See #private_generator_s.generate.
|
2005-11-09 08:40:10 +00:00
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generate (private_generator_t *this,void * data_struct,encoding_rule_t *encoding_rules, size_t encoding_rules_count, private_generator_context_t *generator_context)
|
2005-11-09 08:40:10 +00:00
|
|
|
{
|
|
|
|
int i;
|
2005-11-09 13:34:36 +00:00
|
|
|
status_t status;
|
2005-11-10 09:35:28 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
if (generator_context == NULL)
|
2005-11-09 08:40:10 +00:00
|
|
|
{
|
|
|
|
return OUT_OF_RES;
|
|
|
|
}
|
2005-11-09 13:34:36 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
for (i = 0; i < encoding_rules_count;i++)
|
|
|
|
{
|
2005-11-09 13:34:36 +00:00
|
|
|
status = SUCCESS;
|
2005-11-09 08:40:10 +00:00
|
|
|
switch (encoding_rules[i].type)
|
|
|
|
{
|
2005-11-09 16:07:33 +00:00
|
|
|
/* all u int values are generated in generate_u_int_type */
|
2005-11-09 08:40:10 +00:00
|
|
|
case U_INT_4:
|
|
|
|
case U_INT_8:
|
|
|
|
case U_INT_16:
|
|
|
|
case U_INT_32:
|
|
|
|
case U_INT_64:
|
2005-11-10 17:17:46 +00:00
|
|
|
status = this->generate_u_int_type(this,encoding_rules[i].type,encoding_rules[i].offset,generator_context);
|
2005-11-09 08:40:10 +00:00
|
|
|
break;
|
|
|
|
case RESERVED_BIT:
|
2005-11-09 16:07:33 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
status = this->generate_reserved_field(this,generator_context,1);
|
2005-11-10 14:56:35 +00:00
|
|
|
|
2005-11-09 16:07:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-11-09 08:40:10 +00:00
|
|
|
case RESERVED_BYTE:
|
2005-11-09 16:07:33 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
status = this->generate_reserved_field(this,generator_context,8);
|
2005-11-09 16:07:33 +00:00
|
|
|
break;
|
2005-11-10 14:56:35 +00:00
|
|
|
}
|
2005-11-09 08:40:10 +00:00
|
|
|
case FLAG:
|
2005-11-09 16:07:33 +00:00
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
status = this->generate_flag(this,generator_context,encoding_rules[i].offset);
|
2005-11-09 16:07:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-11-09 08:40:10 +00:00
|
|
|
case LENGTH:
|
2005-11-09 16:07:33 +00:00
|
|
|
/* length is generated like an U_INT_32 */
|
2005-11-10 17:17:46 +00:00
|
|
|
status = this->generate_u_int_type(this,U_INT_32,encoding_rules[i].offset,generator_context);
|
2005-11-10 14:56:35 +00:00
|
|
|
break;
|
2005-11-09 08:40:10 +00:00
|
|
|
case SPI_SIZE:
|
2005-11-10 14:56:35 +00:00
|
|
|
/* currently not implemented */
|
2005-11-09 08:40:10 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2005-11-10 17:17:46 +00:00
|
|
|
generator_context->public.destroy(&(generator_context->public));
|
2005-11-09 08:40:10 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
// infos->destroy(infos);
|
2005-11-09 13:34:36 +00:00
|
|
|
return status;
|
2005-11-09 08:40:10 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 09:54:09 +00:00
|
|
|
/**
|
|
|
|
* Implements generator_t's generate_payload function.
|
|
|
|
* See #generator_s.generate_payload.
|
|
|
|
*/
|
2005-11-10 17:17:46 +00:00
|
|
|
static status_t generate_payload (private_generator_t *this,payload_type_t payload_type,void * data_struct, generator_context_t *generator_context)
|
2005-11-08 16:19:06 +00:00
|
|
|
{
|
2005-11-09 08:40:10 +00:00
|
|
|
int i;
|
2005-11-10 17:17:46 +00:00
|
|
|
|
|
|
|
private_generator_context_t *private_generator_context = (private_generator_context_t *) generator_context;
|
|
|
|
|
|
|
|
private_generator_context->data_struct = data_struct;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/* check every payload info for specific type */
|
|
|
|
for (i = 0; this->payload_infos[i] != NULL; i++)
|
|
|
|
{
|
|
|
|
if (this->payload_infos[i]->payload_type == payload_type)
|
|
|
|
{
|
|
|
|
/* found payload informations, generating is done in private function generate() */
|
2005-11-10 17:17:46 +00:00
|
|
|
return (this->generate(this, data_struct,this->payload_infos[i]->ecoding_rules,this->payload_infos[i]->encoding_rules_count,private_generator_context));
|
2005-11-09 08:40:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NOT_SUPPORTED;
|
2005-11-08 16:19:06 +00:00
|
|
|
}
|
|
|
|
|
2005-11-10 17:17:46 +00:00
|
|
|
status_t write_to_chunk (private_generator_t *this,private_generator_context_t *generator_context, chunk_t *data)
|
|
|
|
{
|
|
|
|
return generator_context->write_chunk(generator_context,data);
|
|
|
|
}
|
|
|
|
|
2005-11-08 16:19:06 +00:00
|
|
|
/**
|
2005-11-10 09:54:09 +00:00
|
|
|
* Implements generator_t's destroy function.
|
|
|
|
* See #generator_s.destroy.
|
2005-11-08 16:19:06 +00:00
|
|
|
*/
|
|
|
|
static status_t destroy(private_generator_t *this)
|
|
|
|
{
|
2005-11-09 09:11:06 +00:00
|
|
|
allocator_free(this);
|
2005-11-08 16:19:06 +00:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
/*
|
|
|
|
* Described in header
|
|
|
|
*/
|
|
|
|
generator_t * generator_create(payload_info_t ** payload_infos)
|
2005-11-08 16:19:06 +00:00
|
|
|
{
|
2005-11-09 08:40:10 +00:00
|
|
|
private_generator_t *this;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 08:40:10 +00:00
|
|
|
if (payload_infos == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 09:35:06 +00:00
|
|
|
this = allocator_alloc_thing(private_generator_t);
|
2005-11-09 08:40:10 +00:00
|
|
|
if (this == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/* initiate public functions */
|
2005-11-10 17:17:46 +00:00
|
|
|
this->public.create_context = (generator_context_t * (*) (generator_t *)) generator_context_create;
|
|
|
|
this->public.generate_payload = (status_t(*)(generator_t*, payload_type_t, void *, generator_context_t *)) generate_payload;
|
2005-11-08 16:19:06 +00:00
|
|
|
this->public.destroy = (status_t(*)(generator_t*)) destroy;
|
2005-11-10 17:17:46 +00:00
|
|
|
this->public.write_to_chunk = (status_t (*) (generator_t *,generator_context_t *, chunk_t *)) write_to_chunk;
|
2005-11-09 13:34:36 +00:00
|
|
|
/* initiate private functions */
|
2005-11-09 08:40:10 +00:00
|
|
|
this->generate = generate;
|
|
|
|
this->generate_u_int_type = generate_u_int_type;
|
2005-11-10 14:56:35 +00:00
|
|
|
this->generate_reserved_field = generate_reserved_field;
|
|
|
|
this->generate_flag = generate_flag;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-09 13:34:36 +00:00
|
|
|
/* initiate private variables */
|
2005-11-09 08:40:10 +00:00
|
|
|
this->payload_infos = payload_infos;
|
2005-11-09 09:11:06 +00:00
|
|
|
|
2005-11-08 16:19:06 +00:00
|
|
|
return &(this->public);
|
|
|
|
}
|