2005-11-09 13:37:55 +00:00
|
|
|
/**
|
|
|
|
* @file parser.c
|
|
|
|
*
|
|
|
|
* @brief Generic parser class used to parse IKEv2-Header and Payload
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-10 19:19:56 +00:00
|
|
|
#include <arpa/inet.h>
|
2005-11-09 13:37:55 +00:00
|
|
|
|
2005-11-11 13:52:11 +00:00
|
|
|
#include "parser.h"
|
|
|
|
|
|
|
|
#include "types.h"
|
2005-11-11 12:27:06 +00:00
|
|
|
#include "definitions.h"
|
2005-11-11 11:20:22 +00:00
|
|
|
#include "globals.h"
|
2005-11-11 13:52:11 +00:00
|
|
|
#include "utils/allocator.h"
|
|
|
|
#include "utils/logger.h"
|
2005-11-14 17:29:22 +00:00
|
|
|
#include "utils/linked_list.h"
|
2005-11-15 10:22:41 +00:00
|
|
|
#include "payloads/encodings.h"
|
2005-11-21 12:32:00 +00:00
|
|
|
#include "payloads/payload.h"
|
|
|
|
#include "payloads/sa_payload.h"
|
|
|
|
#include "payloads/proposal_substructure.h"
|
|
|
|
#include "payloads/transform_substructure.h"
|
|
|
|
#include "payloads/transform_attribute.h"
|
|
|
|
#include "payloads/ke_payload.h"
|
|
|
|
#include "payloads/nonce_payload.h"
|
|
|
|
#include "payloads/notify_payload.h"
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 17:29:22 +00:00
|
|
|
|
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* @private data stored in a context
|
|
|
|
*
|
|
|
|
* contains pointers and counters to store current state
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
typedef struct private_parser_s private_parser_t;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
struct private_parser_s {
|
2005-11-10 19:19:56 +00:00
|
|
|
/**
|
2005-11-15 10:22:41 +00:00
|
|
|
* Public members, see parser_t
|
2005-11-10 19:19:56 +00:00
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
parser_t public;
|
2005-11-14 17:29:22 +00:00
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* @brief parse a 4-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint4) (private_parser_t *this, int rule_number, u_int8_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a 8-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint8) (private_parser_t *this, int rule_number, u_int8_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a 15-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* This is a special case used for ATTRIBUTE_TYPE.
|
|
|
|
* Big-/Little-endian conversion is done here.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint15) (private_parser_t *this, int rule_number, u_int16_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a 16-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* Big-/Little-endian conversion is done here.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint16) (private_parser_t *this, int rule_number, u_int16_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a 32-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* Big-/Little-endian conversion is done here.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint32) (private_parser_t *this, int rule_number, u_int32_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a 64-Bit unsigned integer from the current parsing position.
|
|
|
|
*
|
|
|
|
* @todo add support for big-endian machines.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_uint64) (private_parser_t *this, int rule_number, u_int64_t *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse a single Bit from the current parsing position
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer where to write the parsed result
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_bit) (private_parser_t *this, int rule_number, bool *output_pos);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse substructures in a list
|
|
|
|
*
|
|
|
|
* This function calls the parser recursivly to parse contained substructures
|
|
|
|
* in a linked_list_t. The list must already be created. Payload defines
|
|
|
|
* the type of the substructures. parsing is continued until the specified length
|
|
|
|
* is completely parsed.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer of a linked_list where substructures are added
|
|
|
|
* @param payload_type type of the contained substructures to parse
|
|
|
|
* @param length number of bytes to parse in this list
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_list) (private_parser_t *this, int rule_number, linked_list_t **output_pos, payload_type_t payload_ype, size_t length);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief parse data from current parsing position in a chunk.
|
|
|
|
*
|
|
|
|
* This function clones length number of bytes to output_pos, without
|
|
|
|
* modifiyng them. Space will be allocated and must be freed by caller.
|
|
|
|
*
|
|
|
|
* @param this parser object
|
|
|
|
* @param rule_number number of current rule
|
|
|
|
* @param[out] output_pos pointer of a chunk which will point to the allocated data
|
|
|
|
* @param length number of bytes to clone
|
|
|
|
* @return
|
|
|
|
* - SUCCESS or
|
|
|
|
* - PARSE_ERROR when not successful
|
|
|
|
*/
|
|
|
|
status_t (*parse_chunk) (private_parser_t *this, int rule_number, chunk_t *output_pos, size_t length);
|
2005-11-10 19:19:56 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Current bit for reading in input data
|
|
|
|
*/
|
|
|
|
u_int8_t bit_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current byte for reading in input data
|
|
|
|
*/
|
|
|
|
u_int8_t *byte_pos;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* input data to parse
|
|
|
|
*/
|
|
|
|
u_int8_t *input;
|
|
|
|
|
|
|
|
/**
|
2005-11-15 10:22:41 +00:00
|
|
|
* roof of input, used for length-checking
|
2005-11-10 19:19:56 +00:00
|
|
|
*/
|
|
|
|
u_int8_t *input_roof;
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* set of encoding rules for this parsing session
|
|
|
|
*/
|
|
|
|
encoding_rule_t *rules;
|
|
|
|
|
2005-11-10 19:19:56 +00:00
|
|
|
/**
|
|
|
|
* logger object
|
|
|
|
*/
|
|
|
|
logger_t *logger;
|
2005-11-14 17:29:22 +00:00
|
|
|
};
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint4
|
|
|
|
*/
|
|
|
|
static status_t parse_uint4(private_parser_t *this, int rule_number, u_int8_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m,
|
|
|
|
this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
switch (this->bit_pos)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
*output_pos = *(this->byte_pos) >> 4;
|
|
|
|
}
|
|
|
|
this->bit_pos = 4;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
*output_pos = *(this->byte_pos) & 0x0F;
|
|
|
|
}
|
|
|
|
this->bit_pos = 0;
|
|
|
|
this->byte_pos++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m,
|
|
|
|
this->rules[rule_number].type), this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-11 08:04:51 +00:00
|
|
|
|
2005-11-14 17:29:22 +00:00
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-14 17:29:22 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint8
|
|
|
|
*/
|
|
|
|
static status_t parse_uint8(private_parser_t *this, int rule_number, u_int8_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m,
|
|
|
|
this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m,
|
|
|
|
this->rules[rule_number].type), this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
*output_pos = *(this->byte_pos);
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->byte_pos++;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint15
|
|
|
|
*/
|
|
|
|
static status_t parse_uint15(private_parser_t *this, int rule_number, u_int16_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m,
|
|
|
|
this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos != 1)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type),
|
|
|
|
this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
2005-11-15 15:26:27 +00:00
|
|
|
*output_pos = ntohs(*((u_int16_t*)this->byte_pos)) & ~0x8000;
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->byte_pos += 2;
|
|
|
|
this->bit_pos = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint16
|
|
|
|
*/
|
|
|
|
static status_t parse_uint16(private_parser_t *this, int rule_number, u_int16_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int16_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type),
|
|
|
|
this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
*output_pos = ntohs(*((u_int16_t*)this->byte_pos));
|
|
|
|
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->byte_pos += 2;
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint32
|
|
|
|
*/
|
|
|
|
static status_t parse_uint32(private_parser_t *this, int rule_number, u_int32_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int32_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type),
|
|
|
|
this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
*output_pos = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->byte_pos += 4;
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_uint64
|
|
|
|
*/
|
|
|
|
static status_t parse_uint64(private_parser_t *this, int rule_number, u_int64_t *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->byte_pos + sizeof(u_int64_t) > this->input_roof)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-21 12:32:00 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type),
|
|
|
|
this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
/* assuming little endian host order */
|
|
|
|
*(output_pos + 1) = ntohl(*((u_int32_t*)this->byte_pos));
|
|
|
|
*output_pos = ntohl(*(((u_int32_t*)this->byte_pos) + 1));
|
|
|
|
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log_bytes(this->logger, RAW|MORE, " =>", (void*)output_pos, 8);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->byte_pos += 8;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_bit
|
|
|
|
*/
|
|
|
|
static status_t parse_bit(private_parser_t *this, int rule_number, bool *output_pos)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + sizeof(u_int8_t) > this->input_roof)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " not enough input to parse rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
/* caller interested in result ? */
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
u_int8_t mask;
|
|
|
|
mask = 0x01 << (7 - this->bit_pos);
|
|
|
|
*output_pos = *this->byte_pos & mask;
|
|
|
|
|
|
|
|
if (*output_pos)
|
|
|
|
{
|
|
|
|
/* set to a "clean", comparable true */
|
|
|
|
*output_pos = TRUE;
|
|
|
|
}
|
|
|
|
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, RAW|MORE, " => %d", *output_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
}
|
|
|
|
this->bit_pos = (this->bit_pos + 1) % 8;
|
|
|
|
if (this->bit_pos == 0)
|
|
|
|
{
|
|
|
|
this->byte_pos++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_list
|
|
|
|
*/
|
|
|
|
static status_t parse_list(private_parser_t *this, int rule_number, linked_list_t **output_pos, payload_type_t payload_type, size_t length)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
linked_list_t * list = *output_pos;
|
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " invalid length for rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type), this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (length > 0)
|
|
|
|
{
|
|
|
|
u_int8_t *pos_before = this->byte_pos;
|
|
|
|
payload_t *payload;
|
|
|
|
status_t status;
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, CONTROL|MORE, " %d bytes left, parsing recursivly %s",
|
|
|
|
length, mapping_find(payload_type_m, payload_type));
|
2005-11-14 17:29:22 +00:00
|
|
|
status = this->public.parse_payload((parser_t*)this, payload_type, &payload);
|
|
|
|
if (status != SUCCESS)
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " parsing of a %s substructure failed",
|
|
|
|
mapping_find(payload_type_m, payload_type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
list->insert_last(list, payload);
|
|
|
|
length -= this->byte_pos - pos_before;
|
|
|
|
}
|
|
|
|
*output_pos = list;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
/**
|
|
|
|
* implementation of private_parser_t.parse_chunk
|
|
|
|
*/
|
|
|
|
static status_t parse_chunk(private_parser_t *this, int rule_number, chunk_t *output_pos, size_t length)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
if (this->byte_pos + length > this->input_roof)
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " not enough input (%d bytes) to parse rule %d %s",
|
|
|
|
length, rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type));
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (this->bit_pos)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, " found rule %d %s on bitpos %d",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, this->rules[rule_number].type), this->bit_pos);
|
2005-11-14 17:29:22 +00:00
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
if (output_pos != NULL)
|
|
|
|
{
|
|
|
|
output_pos->len = length;
|
|
|
|
output_pos->ptr = allocator_alloc(length);
|
|
|
|
if (output_pos->ptr == NULL)
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " allocation of chunk (%d bytes) failed", length);
|
2005-11-14 17:29:22 +00:00
|
|
|
return OUT_OF_RES;
|
|
|
|
}
|
|
|
|
memcpy(output_pos->ptr, this->byte_pos, length);
|
|
|
|
}
|
|
|
|
this->byte_pos += length;
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log_bytes(this->logger, RAW|MORE, " =>", (void*)output_pos->ptr, length);
|
2005-11-14 17:29:22 +00:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2005-11-10 19:19:56 +00:00
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* implementation of parser_context_t.parse_payload
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
static status_t parse_payload(private_parser_t *this, payload_type_t payload_type, payload_t **payload)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 09:43:18 +00:00
|
|
|
payload_t *pld;
|
|
|
|
void *output;
|
2005-11-14 17:29:22 +00:00
|
|
|
size_t rule_count, payload_length, spi_size, attribute_length;
|
|
|
|
bool attribute_format;
|
2005-11-15 10:22:41 +00:00
|
|
|
int rule_number;
|
2005-11-14 09:43:18 +00:00
|
|
|
encoding_rule_t *rule;
|
2005-11-14 17:29:22 +00:00
|
|
|
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "parsing %s payload, %d bytes left",
|
|
|
|
mapping_find(payload_type_m, payload_type),
|
|
|
|
this->input_roof-this->byte_pos);
|
|
|
|
|
|
|
|
this->logger->log_bytes(this->logger, RAW, "parsing payload from", this->byte_pos,
|
|
|
|
this->input_roof-this->byte_pos);
|
2005-11-14 09:43:18 +00:00
|
|
|
|
|
|
|
/* ok, do the parsing */
|
|
|
|
pld = payload_create(payload_type);
|
|
|
|
if (pld == NULL)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-16 16:08:18 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " payload %s not supported", mapping_find(payload_type_m, payload_type));
|
2005-11-14 09:43:18 +00:00
|
|
|
return NOT_SUPPORTED;
|
|
|
|
}
|
|
|
|
/* base pointer for output, avoids casting in every rule */
|
|
|
|
output = pld;
|
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
pld->get_encoding_rules(pld, &(this->rules), &rule_count);
|
2005-11-14 09:43:18 +00:00
|
|
|
|
2005-11-15 10:22:41 +00:00
|
|
|
for (rule_number = 0; rule_number < rule_count; rule_number++)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
rule = &(this->rules[rule_number]);
|
2005-11-18 07:07:32 +00:00
|
|
|
this->logger->log(this->logger, CONTROL|MORE, " parsing rule %d %s",
|
2005-11-16 16:08:18 +00:00
|
|
|
rule_number, mapping_find(encoding_type_m, rule->type));
|
2005-11-14 09:43:18 +00:00
|
|
|
switch (rule->type)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 09:43:18 +00:00
|
|
|
case U_INT_4:
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint4(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_8:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint8(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_16:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint16(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_32:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint32(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case U_INT_64:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint64(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case RESERVED_BIT:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_bit(this, rule_number, NULL) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case RESERVED_BYTE:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint8(this, rule_number, NULL) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case FLAG:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_bit(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case PAYLOAD_LENGTH:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint16(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
payload_length = *(u_int16_t*)(output + rule->offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case HEADER_LENGTH:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint32(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPI_SIZE:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint8(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
spi_size = *(u_int8_t*)(output + rule->offset);
|
|
|
|
break;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case SPI:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_chunk(this, rule_number, output + rule->offset, spi_size) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case PROPOSALS:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t proposals_length = payload_length - SA_PAYLOAD_HEADER_LENGTH;
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_list(this, rule_number, output + rule->offset, PROPOSAL_SUBSTRUCTURE, proposals_length) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TRANSFORMS:
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t transforms_length = payload_length - spi_size - PROPOSAL_SUBSTRUCTURE_HEADER_LENGTH;
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_list(this, rule_number, output + rule->offset, TRANSFORM_SUBSTRUCTURE, transforms_length) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case TRANSFORM_ATTRIBUTES:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t transform_a_length = payload_length - TRANSFORM_SUBSTRUCTURE_HEADER_LENGTH;
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_list(this, rule_number, output + rule->offset, TRANSFORM_ATTRIBUTE, transform_a_length) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ATTRIBUTE_FORMAT:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_bit(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-14 17:29:22 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
attribute_format = *(bool*)(output + rule->offset);
|
2005-11-14 09:43:18 +00:00
|
|
|
break;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
case ATTRIBUTE_TYPE:
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint15(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
attribute_format = *(bool*)(output + rule->offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ATTRIBUTE_LENGTH_OR_VALUE:
|
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_uint16(this, rule_number, output + rule->offset) != SUCCESS)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
attribute_length = *(u_int16_t*)(output + rule->offset);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ATTRIBUTE_VALUE:
|
|
|
|
{
|
|
|
|
if (attribute_format == FALSE)
|
2005-11-14 09:43:18 +00:00
|
|
|
{
|
2005-11-15 10:22:41 +00:00
|
|
|
if (this->parse_chunk(this, rule_number, output + rule->offset, attribute_length) != SUCCESS)
|
2005-11-14 17:29:22 +00:00
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-14 17:29:22 +00:00
|
|
|
break;
|
2005-11-14 09:43:18 +00:00
|
|
|
}
|
2005-11-15 15:26:27 +00:00
|
|
|
case NONCE_DATA:
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t nonce_length = payload_length - NONCE_PAYLOAD_HEADER_LENGTH;
|
2005-11-15 15:26:27 +00:00
|
|
|
if (this->parse_chunk(this, rule_number, output + rule->offset, nonce_length) != SUCCESS)
|
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-11-15 15:44:18 +00:00
|
|
|
case KEY_EXCHANGE_DATA:
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t keydata_length = payload_length - KE_PAYLOAD_HEADER_LENGTH;
|
2005-11-15 15:44:18 +00:00
|
|
|
if (this->parse_chunk(this, rule_number, output + rule->offset, keydata_length) != SUCCESS)
|
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-11-15 16:18:02 +00:00
|
|
|
case NOTIFICATION_DATA:
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
size_t notify_length = payload_length - NOTIFY_PAYLOAD_HEADER_LENGTH - spi_size;
|
2005-11-15 16:18:02 +00:00
|
|
|
if (this->parse_chunk(this, rule_number, output + rule->offset, notify_length) != SUCCESS)
|
|
|
|
{
|
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
default:
|
|
|
|
{
|
2005-11-16 16:08:18 +00:00
|
|
|
this->logger->log(this->logger, ERROR, " no rule to parse rule %d %s (%d)", rule_number, mapping_find(encoding_type_m, rule->type), rule->type);
|
2005-11-14 09:43:18 +00:00
|
|
|
pld->destroy(pld);
|
|
|
|
return PARSE_ERROR;
|
|
|
|
}
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
/* process next rulue */
|
|
|
|
rule++;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
*payload = pld;
|
2005-11-14 17:29:22 +00:00
|
|
|
|
2005-11-16 16:08:18 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "parsing %s successful", mapping_find(payload_type_m, payload_type));
|
2005-11-14 09:43:18 +00:00
|
|
|
return SUCCESS;
|
2005-11-10 19:19:56 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 16:50:13 +00:00
|
|
|
/**
|
|
|
|
* implementation of parser_t.reset_context
|
|
|
|
*/
|
|
|
|
static status_t reset_context (private_parser_t *this)
|
|
|
|
{
|
|
|
|
this->byte_pos = this->input;
|
|
|
|
this->bit_pos = 0;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/**
|
|
|
|
* implementation of parser_t.destroy
|
|
|
|
*/
|
2005-11-10 19:19:56 +00:00
|
|
|
static status_t destroy(private_parser_t *this)
|
|
|
|
{
|
2005-11-11 11:20:22 +00:00
|
|
|
global_logger_manager->destroy_logger(global_logger_manager,this->logger);
|
2005-11-10 19:19:56 +00:00
|
|
|
allocator_free(this);
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2005-11-11 08:04:51 +00:00
|
|
|
/*
|
|
|
|
* see header file
|
|
|
|
*/
|
2005-11-14 09:43:18 +00:00
|
|
|
parser_t *parser_create(chunk_t data)
|
2005-11-10 19:19:56 +00:00
|
|
|
{
|
|
|
|
private_parser_t *this = allocator_alloc_thing(private_parser_t);
|
|
|
|
|
|
|
|
if (this == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
this->logger = global_logger_manager->create_logger(global_logger_manager, PARSER, NULL);
|
2005-11-21 12:32:00 +00:00
|
|
|
this->logger->disable_level(this->logger, FULL);
|
|
|
|
this->logger->enable_level(this->logger, RAW|CONTROL|ALL);
|
2005-11-14 12:10:05 +00:00
|
|
|
|
2005-11-11 11:20:22 +00:00
|
|
|
|
2005-11-10 19:19:56 +00:00
|
|
|
if (this->logger == NULL)
|
|
|
|
{
|
2005-11-21 12:32:00 +00:00
|
|
|
global_logger_manager->destroy_logger(global_logger_manager, this->logger);
|
2005-11-10 19:19:56 +00:00
|
|
|
allocator_free(this);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
|
|
|
|
this->public.parse_payload = (status_t(*)(parser_t*,payload_type_t,payload_t**)) parse_payload;
|
2005-11-16 16:50:13 +00:00
|
|
|
this->public.reset_context = (status_t(*)(parser_t*)) reset_context;
|
2005-11-10 19:19:56 +00:00
|
|
|
this->public.destroy = (status_t(*)(parser_t*)) destroy;
|
|
|
|
|
2005-11-14 17:29:22 +00:00
|
|
|
|
|
|
|
this->parse_uint4 = parse_uint4;
|
|
|
|
this->parse_uint8 = parse_uint8;
|
|
|
|
this->parse_uint15 = parse_uint15;
|
|
|
|
this->parse_uint16 = parse_uint16;
|
|
|
|
this->parse_uint32 = parse_uint32;
|
|
|
|
this->parse_uint64 = parse_uint64;
|
|
|
|
this->parse_bit = parse_bit;
|
|
|
|
this->parse_list = parse_list;
|
|
|
|
this->parse_chunk = parse_chunk;
|
2005-11-21 12:32:00 +00:00
|
|
|
|
2005-11-14 09:43:18 +00:00
|
|
|
this->input = data.ptr;
|
|
|
|
this->byte_pos = data.ptr;
|
|
|
|
this->bit_pos = 0;
|
|
|
|
this->input_roof = data.ptr + data.len;
|
2005-11-10 19:19:56 +00:00
|
|
|
|
|
|
|
return (parser_t*)this;
|
|
|
|
}
|
2005-11-14 09:43:18 +00:00
|
|
|
|