- import of strongswan-2.7.0
- applied patch for charon
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
# 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.
|
||||
#
|
||||
|
||||
UTILS_DIR= $(LIB_DIR)utils/
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)leak_detective.o
|
||||
$(BUILD_DIR)leak_detective.o : $(UTILS_DIR)leak_detective.c $(UTILS_DIR)leak_detective.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)linked_list.o
|
||||
$(BUILD_DIR)linked_list.o : $(UTILS_DIR)linked_list.c $(UTILS_DIR)linked_list.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)logger.o
|
||||
$(BUILD_DIR)logger.o : $(UTILS_DIR)logger.c $(UTILS_DIR)logger.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)logger_manager.o
|
||||
$(BUILD_DIR)logger_manager.o : $(UTILS_DIR)logger_manager.c $(UTILS_DIR)logger_manager.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)randomizer.o
|
||||
$(BUILD_DIR)randomizer.o : $(UTILS_DIR)randomizer.c $(UTILS_DIR)randomizer.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)tester.o
|
||||
$(BUILD_DIR)tester.o : $(UTILS_DIR)tester.c $(UTILS_DIR)tester.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)identification.o
|
||||
$(BUILD_DIR)identification.o : $(UTILS_DIR)identification.c $(UTILS_DIR)identification.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
|
||||
LIB_OBJS+= $(BUILD_DIR)host.o
|
||||
$(BUILD_DIR)host.o : $(UTILS_DIR)host.c $(UTILS_DIR)host.h
|
||||
$(CC) $(CFLAGS) -c -o $@ $<
|
||||
@@ -0,0 +1,365 @@
|
||||
/**
|
||||
* @file host.c
|
||||
*
|
||||
* @brief Implementation of host_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "host.h"
|
||||
|
||||
|
||||
typedef struct private_host_t private_host_t;
|
||||
|
||||
/**
|
||||
* @brief Private Data of a host object.
|
||||
*/
|
||||
struct private_host_t {
|
||||
/**
|
||||
* Public data
|
||||
*/
|
||||
host_t public;
|
||||
|
||||
/**
|
||||
* Address family to use, such as AF_INET or AF_INET6
|
||||
*/
|
||||
int family;
|
||||
|
||||
/**
|
||||
* string representation of host
|
||||
*/
|
||||
char *string;
|
||||
|
||||
/**
|
||||
* low-lewel structure, wich stores the address
|
||||
*/
|
||||
union {
|
||||
struct sockaddr address;
|
||||
struct sockaddr_in address4;
|
||||
};
|
||||
/**
|
||||
* length of address structure
|
||||
*/
|
||||
socklen_t socklen;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* implements host_t.get_sockaddr
|
||||
*/
|
||||
static sockaddr_t *get_sockaddr(private_host_t *this)
|
||||
{
|
||||
return &(this->address);
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_sockaddr_len
|
||||
*/
|
||||
static socklen_t *get_sockaddr_len(private_host_t *this)
|
||||
{
|
||||
return &(this->socklen);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of host_t.is_default_route.
|
||||
*/
|
||||
static bool is_default_route (private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
static u_int8_t default_route[4] = {0x00,0x00,0x00,0x00};
|
||||
|
||||
if (memcmp(default_route,&(this->address4.sin_addr.s_addr),4) == 0)
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* empty chunk is returned */
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_address
|
||||
*/
|
||||
static char *get_address(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
char *string;
|
||||
/* we need to clone it, since inet_ntoa overwrites
|
||||
* internal buffer on subsequent calls
|
||||
*/
|
||||
free(this->string);
|
||||
string = inet_ntoa(this->address4.sin_addr);
|
||||
this->string = malloc(strlen(string)+1);
|
||||
strcpy(this->string, string);
|
||||
return this->string;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return "(family not supported)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of host_t.get_address_as_chunk.
|
||||
*/
|
||||
static chunk_t get_address_as_chunk(private_host_t *this)
|
||||
{
|
||||
chunk_t address = CHUNK_INITIALIZER;
|
||||
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
/* allocate 4 bytes for IPV4 address*/
|
||||
address.ptr = malloc(4);
|
||||
address.len = 4;
|
||||
memcpy(address.ptr,&(this->address4.sin_addr.s_addr),4);
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* empty chunk is returned */
|
||||
return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static xfrm_address_t get_xfrm_addr(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
return (xfrm_address_t)(this->address4.sin_addr.s_addr);
|
||||
}
|
||||
default:
|
||||
{
|
||||
/* todo */
|
||||
return (xfrm_address_t)(this->address4.sin_addr.s_addr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static int get_family(private_host_t *this)
|
||||
{
|
||||
return this->family;
|
||||
}
|
||||
|
||||
/**
|
||||
* implements host_t.get_port
|
||||
*/
|
||||
static u_int16_t get_port(private_host_t *this)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
case AF_INET:
|
||||
{
|
||||
return ntohs(this->address4.sin_port);
|
||||
}
|
||||
default:
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements host_t.clone.
|
||||
*/
|
||||
static private_host_t *clone(private_host_t *this)
|
||||
{
|
||||
private_host_t *new = malloc_thing(private_host_t);
|
||||
|
||||
|
||||
memcpy(new, this, sizeof(private_host_t));
|
||||
if (this->string)
|
||||
{
|
||||
new->string = malloc(strlen(this->string)+1);
|
||||
strcpy(new->string, this->string);
|
||||
}
|
||||
return new;
|
||||
}
|
||||
|
||||
/**
|
||||
* Impelements host_t.ip_equals
|
||||
*/
|
||||
static bool ip_equals(private_host_t *this, private_host_t *other)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
if ((this->address4.sin_family == other->address4.sin_family) &&
|
||||
(this->address4.sin_addr.s_addr == other->address4.sin_addr.s_addr))
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Impelements host_t.equals
|
||||
*/
|
||||
static bool equals(private_host_t *this, private_host_t *other)
|
||||
{
|
||||
switch (this->family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
if ((this->address4.sin_family == other->address4.sin_family) &&
|
||||
(this->address4.sin_addr.s_addr == other->address4.sin_addr.s_addr) &&
|
||||
(this->address4.sin_port == other->address4.sin_port))
|
||||
{
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements host_t.destroy
|
||||
*/
|
||||
static void destroy(private_host_t *this)
|
||||
{
|
||||
free(this->string);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty host_t object
|
||||
*/
|
||||
static private_host_t *host_create_empty()
|
||||
{
|
||||
private_host_t *this = malloc_thing(private_host_t);
|
||||
|
||||
this->public.get_sockaddr = (sockaddr_t* (*) (host_t*))get_sockaddr;
|
||||
this->public.get_sockaddr_len = (socklen_t*(*) (host_t*))get_sockaddr_len;
|
||||
this->public.clone = (host_t* (*) (host_t*))clone;
|
||||
this->public.get_family = (int (*) (host_t*))get_family;
|
||||
this->public.get_xfrm_addr = (xfrm_address_t (*) (host_t *))get_xfrm_addr;
|
||||
this->public.get_address = (char* (*) (host_t *))get_address;
|
||||
this->public.get_address_as_chunk = (chunk_t (*) (host_t *)) get_address_as_chunk;
|
||||
this->public.get_port = (u_int16_t (*) (host_t *))get_port;
|
||||
this->public.ip_equals = (bool (*) (host_t *,host_t *)) ip_equals;
|
||||
this->public.equals = (bool (*) (host_t *,host_t *)) equals;
|
||||
this->public.is_default_route = (bool (*) (host_t *)) is_default_route;
|
||||
this->public.destroy = (void (*) (host_t*))destroy;
|
||||
|
||||
this->string = NULL;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port)
|
||||
{
|
||||
private_host_t *this = host_create_empty();
|
||||
|
||||
this->family = family;
|
||||
|
||||
switch (family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
this->address4.sin_family = AF_INET;
|
||||
this->address4.sin_addr.s_addr = inet_addr(address);
|
||||
this->address4.sin_port = htons(port);
|
||||
this->socklen = sizeof(struct sockaddr_in);
|
||||
return &(this->public);
|
||||
}
|
||||
default:
|
||||
{
|
||||
free(this);
|
||||
return NULL;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port)
|
||||
{
|
||||
private_host_t *this = host_create_empty();
|
||||
|
||||
this->family = family;
|
||||
switch (family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
if (address.len != 4)
|
||||
{
|
||||
break;
|
||||
}
|
||||
this->address4.sin_family = AF_INET;
|
||||
memcpy(&(this->address4.sin_addr.s_addr),address.ptr,4);
|
||||
this->address4.sin_port = htons(port);
|
||||
this->socklen = sizeof(struct sockaddr_in);
|
||||
return &(this->public);
|
||||
}
|
||||
}
|
||||
free(this);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr)
|
||||
{
|
||||
chunk_t address;
|
||||
|
||||
switch (sockaddr->sa_family)
|
||||
{
|
||||
/* IPv4 */
|
||||
case AF_INET:
|
||||
{
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)sockaddr;
|
||||
address.ptr = (void*)&(sin->sin_addr.s_addr);
|
||||
address.len = 4;
|
||||
return host_create_from_chunk(AF_INET, address, ntohs(sin->sin_port));
|
||||
}
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,225 @@
|
||||
/**
|
||||
* @file host.h
|
||||
*
|
||||
* @brief Interface of host_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef HOST_H_
|
||||
#define HOST_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <linux/xfrm.h>
|
||||
|
||||
#include <types.h>
|
||||
|
||||
|
||||
typedef struct host_t host_t;
|
||||
|
||||
/**
|
||||
* @brief Representates a Host
|
||||
*
|
||||
* Host object, identifies a address:port pair and defines some
|
||||
* useful functions on it.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - host_create()
|
||||
* - host_create_from_chunk()
|
||||
* - host_create_from_sockaddr()
|
||||
*
|
||||
* @todo Add IPv6 support
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
struct host_t {
|
||||
|
||||
/**
|
||||
* @brief Build a clone of this host object.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return cloned host
|
||||
*/
|
||||
host_t *(*clone) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Get a pointer to the internal sockaddr struct.
|
||||
*
|
||||
* This is used for sending and receiving via sockets.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return pointer to the internal sockaddr structure
|
||||
*/
|
||||
sockaddr_t *(*get_sockaddr) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Get the length of the sockaddr struct.
|
||||
*
|
||||
* Sepending on the family, the length of the sockaddr struct
|
||||
* is different. Use this function to get the length of the sockaddr
|
||||
* struct returned by get_sock_addr.
|
||||
*
|
||||
* This is used for sending and receiving via sockets.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return length of the sockaddr struct
|
||||
*/
|
||||
socklen_t *(*get_sockaddr_len) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Gets the address as xfrm_address_t.
|
||||
*
|
||||
* This function allows the conversion to an
|
||||
* xfrm_address_t, used for netlink communication
|
||||
* with the kernel.
|
||||
*
|
||||
* @see kernel_interface_t.
|
||||
*
|
||||
* @param this calling object
|
||||
* @return address in xfrm_address_t format
|
||||
*/
|
||||
xfrm_address_t (*get_xfrm_addr) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Gets the family of the address
|
||||
*
|
||||
* @param this calling object
|
||||
* @return family
|
||||
*/
|
||||
int (*get_family) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the address of this host
|
||||
*
|
||||
* Mostly used for debugging purposes.
|
||||
* @warning string must NOT be freed
|
||||
*
|
||||
* @param this object
|
||||
* @return address string,
|
||||
*/
|
||||
char* (*get_address) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Checks if the ip address of host is set to default route.
|
||||
*
|
||||
* @param this calling object
|
||||
* @return
|
||||
* - TRUE if host has IP 0.0.0.0 for default route
|
||||
* - FALSE otherwise
|
||||
*/
|
||||
bool (*is_default_route) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the address of this host as chunk_t
|
||||
*
|
||||
* @warning returned chunk has to get destroyed by caller.
|
||||
*
|
||||
* @param this object
|
||||
* @return address string,
|
||||
*/
|
||||
chunk_t (*get_address_as_chunk) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief get the port of this host
|
||||
*
|
||||
* Mostly used for debugging purposes.
|
||||
*
|
||||
* @param this object to clone
|
||||
* @return port number
|
||||
*/
|
||||
u_int16_t (*get_port) (host_t *this);
|
||||
|
||||
/**
|
||||
* @brief Compare the ips of two hosts hosts.
|
||||
*
|
||||
* @param this object to compare
|
||||
* @param other the other to compare
|
||||
* @return TRUE if addresses are equal.
|
||||
*/
|
||||
bool (*ip_equals) (host_t *this, host_t *other);
|
||||
|
||||
/**
|
||||
* @brief Compare two hosts, with port.
|
||||
*
|
||||
* @param this object to compare
|
||||
* @param other the other to compare
|
||||
* @return TRUE if addresses and ports are equal.
|
||||
*/
|
||||
bool (*equals) (host_t *this, host_t *other);
|
||||
|
||||
/**
|
||||
* @brief Destroy this host object
|
||||
*
|
||||
* @param this calling
|
||||
* @return SUCCESS in any case
|
||||
*/
|
||||
void (*destroy) (host_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from an address string
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @param family Address family to use for this object, such as AF_INET or AF_INET6
|
||||
* @param address string of an address, such as "152.96.193.130"
|
||||
* @param port port number
|
||||
* @return
|
||||
* - host_t object
|
||||
* - NULL, if family not supported.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create(int family, char *address, u_int16_t port);
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from an address chunk
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @param family Address family to use for this object, such as AF_INET or AF_INET6
|
||||
* @param address address as 4 byte chunk_t in networ order
|
||||
* @param port port number
|
||||
* @return
|
||||
* - host_t object
|
||||
* - NULL, if family not supported or chunk_t length not 4 bytes.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create_from_chunk(int family, chunk_t address, u_int16_t port);
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a host_t object from a sockaddr struct
|
||||
*
|
||||
* Currently supports only IPv4!
|
||||
*
|
||||
* @param sockaddr sockaddr struct which contains family, address and port
|
||||
* @return
|
||||
* - host_t object
|
||||
* - NULL, if family not supported.
|
||||
*
|
||||
* @ingroup network
|
||||
*/
|
||||
host_t *host_create_from_sockaddr(sockaddr_t *sockaddr);
|
||||
|
||||
|
||||
#endif /*HOST_H_*/
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,245 @@
|
||||
/**
|
||||
* @file identification.h
|
||||
*
|
||||
* @brief Interface of identification_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef IDENTIFICATION_H_
|
||||
#define IDENTIFICATION_H_
|
||||
|
||||
|
||||
#include "types.h"
|
||||
|
||||
typedef enum id_type_t id_type_t;
|
||||
|
||||
/**
|
||||
* @brief ID Types in a ID payload.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
enum id_type_t {
|
||||
|
||||
/**
|
||||
* ID data is a single four (4) octet IPv4 address.
|
||||
*/
|
||||
ID_IPV4_ADDR = 1,
|
||||
|
||||
/**
|
||||
* ID data is a fully-qualified domain name string.
|
||||
* An example of a ID_FQDN is, "example.com".
|
||||
* The string MUST not contain any terminators (e.g., NULL, CR, etc.).
|
||||
*/
|
||||
ID_FQDN = 2,
|
||||
|
||||
/**
|
||||
* ID data is a fully-qualified RFC822 email address string, An example of
|
||||
* a ID_RFC822_ADDR is, "[email protected]". The string MUST
|
||||
* not contain any terminators.
|
||||
*/
|
||||
ID_RFC822_ADDR = 3,
|
||||
|
||||
/**
|
||||
* ID data is a single sixteen (16) octet IPv6 address.
|
||||
*/
|
||||
ID_IPV6_ADDR = 5,
|
||||
|
||||
/**
|
||||
* ID data is the binary DER encoding of an ASN.1 X.500 Distinguished Name
|
||||
* [X.501].
|
||||
*/
|
||||
ID_DER_ASN1_DN = 9,
|
||||
|
||||
/**
|
||||
* ID data is the binary DER encoding of an ASN.1 X.500 GeneralName
|
||||
* [X.509].
|
||||
*/
|
||||
ID_DER_ASN1_GN = 10,
|
||||
|
||||
/**
|
||||
* ID data is an opaque octet stream which may be used to pass vendor-
|
||||
* specific information necessary to do certain proprietary
|
||||
* types of identification.
|
||||
*/
|
||||
ID_KEY_ID = 11,
|
||||
|
||||
/**
|
||||
* Special type of PRIVATE USE which matches to any other id.
|
||||
*/
|
||||
ID_ANY = 201,
|
||||
};
|
||||
|
||||
/**
|
||||
* String mappings for id_type_t.
|
||||
*/
|
||||
extern mapping_t id_type_m[];
|
||||
|
||||
typedef struct identification_t identification_t;
|
||||
|
||||
/**
|
||||
* @brief Generic identification, such as used in ID payload.
|
||||
*
|
||||
* The following types are possible:
|
||||
* - ID_IPV4_ADDR
|
||||
* - ID_FQDN
|
||||
* - ID_RFC822_ADDR
|
||||
* - ID_IPV6_ADDR
|
||||
* - ID_DER_ASN1_DN
|
||||
* - ID_DER_ASN1_GN
|
||||
* - ID_KEY_ID
|
||||
*
|
||||
* @b Constructors:
|
||||
* - identification_create_from_string()
|
||||
* - identification_create_from_encoding()
|
||||
*
|
||||
* @todo Support for ID_DER_ASN1_GN is minimal right now. Comparison
|
||||
* between them and ID_IPV4_ADDR/RFC822_ADDR would be nice.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct identification_t {
|
||||
|
||||
/**
|
||||
* @brief Get the encoding of this id, to send over
|
||||
* the network.
|
||||
*
|
||||
* @warning Result points to internal data, do NOT free!
|
||||
*
|
||||
* @param this the identification_t object
|
||||
* @return a chunk containing the encoded bytes
|
||||
*/
|
||||
chunk_t (*get_encoding) (identification_t *this);
|
||||
|
||||
/**
|
||||
* @brief Get the type of this identification.
|
||||
*
|
||||
* @param this the identification_t object
|
||||
* @return id_type_t
|
||||
*/
|
||||
id_type_t (*get_type) (identification_t *this);
|
||||
|
||||
/**
|
||||
* @brief Get a string representation of this id.
|
||||
*
|
||||
* @warning Result points to internal data, do NOT free!
|
||||
*
|
||||
* @param this the identification_t object
|
||||
* @return string
|
||||
*/
|
||||
char *(*get_string) (identification_t *this);
|
||||
|
||||
/**
|
||||
* @brief Check if two identification_t objects are equal.
|
||||
*
|
||||
* @param this the identification_t object
|
||||
* @param other other identification_t object
|
||||
* @return TRUE if the IDs are equal
|
||||
*/
|
||||
bool (*equals) (identification_t *this,identification_t *other);
|
||||
|
||||
/**
|
||||
* @brief Check if an ID belongs to a wildcard ID.
|
||||
*
|
||||
* An identification_t may contain wildcards, such as
|
||||
* *@strongswan.org. This call checks if a given ID
|
||||
* (e.g. [email protected]) belongs to a such wildcard
|
||||
* ID. Returns TRUE if
|
||||
* - IDs are identical
|
||||
* - other is of type ID_ANY
|
||||
* - other contains a wildcard and matches this
|
||||
*
|
||||
* @param this the ID without wildcard
|
||||
* @param other the ID containing a wildcard
|
||||
* @return TRUE if other belongs to this
|
||||
*/
|
||||
bool (*belongs_to) (identification_t *this, identification_t *other);
|
||||
|
||||
/**
|
||||
* @brief Check if an ID is a wildcard ID.
|
||||
*
|
||||
* If the ID represents multiple IDs (with wildcards, or
|
||||
* as the type ID_ANY), TRUE is returned. If it is unique,
|
||||
* FALSE is returned.
|
||||
*
|
||||
* @param this identification_t object
|
||||
* @return TRUE if ID contains wildcards
|
||||
*/
|
||||
bool (*contains_wildcards) (identification_t *this);
|
||||
|
||||
/**
|
||||
* @brief Clone a identification_t instance.
|
||||
*
|
||||
* @param this the identification_t object to clone
|
||||
* @return clone of this
|
||||
*/
|
||||
identification_t *(*clone) (identification_t *this);
|
||||
|
||||
/**
|
||||
* @brief Destroys a identification_t object.
|
||||
*
|
||||
* @param this identification_t object
|
||||
*/
|
||||
void (*destroy) (identification_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Creates an identification_t object from a string.
|
||||
*
|
||||
* @param string input string, which will be converted
|
||||
* @return
|
||||
* - created identification_t object, or
|
||||
* - NULL if unsupported string supplied.
|
||||
*
|
||||
* The input string may be e.g. one of the following:
|
||||
* - ID_IPV4_ADDR: 192.168.0.1
|
||||
* - ID_IPV6_ADDR: 2001:0db8:85a3:08d3:1319:8a2e:0370:7345
|
||||
* - ID_FQDN: @www.strongswan.org (@indicates FQDN)
|
||||
* - ID_RFC822_ADDR: [email protected]
|
||||
* - ID_DER_ASN1_DN: C=CH, O=Linux strongSwan, CN=bob
|
||||
*
|
||||
* In favour of pluto, domainnames are prepended with an @, since
|
||||
* pluto resolves domainnames without an @ to IPv4 addresses. Since
|
||||
* we use a seperate host_t class for addresses, this doesn't
|
||||
* make sense for us.
|
||||
*
|
||||
* A distinguished name may contain one or more of the following RDNs:
|
||||
* ND, UID, DC, CN, S, SN, serialNumber, C, L, ST, O, OU, T, D,
|
||||
* N, G, I, ID, EN, EmployeeNumber, E, Email, emailAddress, UN,
|
||||
* unstructuredName, TCGID.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
identification_t * identification_create_from_string(char *string);
|
||||
|
||||
/**
|
||||
* @brief Creates an identification_t object from an encoded chunk.
|
||||
*
|
||||
* @param type type of this id, such as ID_IPV4_ADDR
|
||||
* @param encoded encoded bytes, such as from identification_t.get_encoding
|
||||
* @return identification_t object
|
||||
*
|
||||
* In contrast to identification_create_from_string(), this constructor never
|
||||
* returns NULL, even when the conversion to a sring representation fails.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
|
||||
|
||||
|
||||
#endif /* IDENTIFICATION_H_ */
|
||||
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* @file iterator.h
|
||||
*
|
||||
* @brief Interface iterator_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef ITERATOR_H_
|
||||
#define ITERATOR_H_
|
||||
|
||||
typedef struct iterator_t iterator_t;
|
||||
|
||||
/**
|
||||
* @brief Iterator interface, allows iteration over collections.
|
||||
*
|
||||
* iterator_t defines an interface for iterating over collections.
|
||||
* It allows searching, deleting, updating and inserting.
|
||||
*
|
||||
* Thanks to JMP for iterator lessons :-)
|
||||
*
|
||||
* @b Constructors:
|
||||
* - via linked_list_t.create_iterator, or
|
||||
* - any other class which supports the iterator_t interface
|
||||
*
|
||||
* @see linked_list_t
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct iterator_t {
|
||||
|
||||
/**
|
||||
* @brief Iterate over all items.
|
||||
*
|
||||
* The easy way to iterate over items.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param[out] value item
|
||||
* @return
|
||||
* - TRUE, if more elements are avaiable,
|
||||
* - FALSE otherwise
|
||||
*/
|
||||
bool (*iterate) (iterator_t *this, void** value);
|
||||
|
||||
/**
|
||||
* @brief Moves to the next element, if available.
|
||||
*
|
||||
* A newly created iterator_t object doesn't point to any item.
|
||||
* Call iterator_t.has_next first to point it to the first item.
|
||||
*
|
||||
* @param this calling object
|
||||
* @return
|
||||
* - TRUE, if more elements are avaiable,
|
||||
* - FALSE otherwise
|
||||
*/
|
||||
bool (*has_next) (iterator_t *this);
|
||||
|
||||
/**
|
||||
* @brief Returns the current value at the iterator position.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param[out] value value is set to the current value at iterator position
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - FAILED if iterator on an invalid position
|
||||
*/
|
||||
status_t (*current) (iterator_t *this, void **value);
|
||||
|
||||
/**
|
||||
* @brief Inserts a new item before the given iterator position.
|
||||
*
|
||||
* The iterator position is not changed after inserting
|
||||
*
|
||||
* @param this calling iterator
|
||||
* @param[in] item value to insert in list
|
||||
*/
|
||||
void (*insert_before) (iterator_t *this, void *item);
|
||||
|
||||
/**
|
||||
* @brief Inserts a new item after the given iterator position.
|
||||
*
|
||||
* The iterator position is not changed after inserting.
|
||||
*
|
||||
* @param this calling iterator
|
||||
* @param[in] item value to insert in list
|
||||
*/
|
||||
void (*insert_after) (iterator_t *this, void *item);
|
||||
|
||||
/**
|
||||
* @brief Replace the current item at current iterator position.
|
||||
*
|
||||
* The iterator position is not changed after replacing.
|
||||
*
|
||||
* @param this calling iterator
|
||||
* @param[out] old_item old value will be written here(can be NULL)
|
||||
* @param[in] new_item new value
|
||||
*
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - FAILED if iterator is on an invalid position
|
||||
*/
|
||||
status_t (*replace) (iterator_t *this, void **old_item, void *new_item);
|
||||
|
||||
/**
|
||||
* @brief Removes an element from list at the given iterator position.
|
||||
*
|
||||
* The position of the iterator is set in the following order:
|
||||
* - to the item before, if available
|
||||
* - otherwise to the item after, if available
|
||||
* - otherwise it gets reseted
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - FAILED if iterator is on an invalid position
|
||||
*/
|
||||
status_t (*remove) (iterator_t *iterator);
|
||||
|
||||
/**
|
||||
* @brief Resets the iterator position.
|
||||
*
|
||||
* After reset, the iterator_t objects doesn't point to an element.
|
||||
* A call to iterator_t.has_next is necessary to do any other operations
|
||||
* with the resetted iterator.
|
||||
*
|
||||
* @param this calling object
|
||||
*/
|
||||
void (*reset) (iterator_t *this);
|
||||
|
||||
/**
|
||||
* @brief Destroys an iterator.
|
||||
*
|
||||
* @param this iterator to destroy
|
||||
*
|
||||
*/
|
||||
void (*destroy) (iterator_t *this);
|
||||
};
|
||||
|
||||
#endif /*ITERATOR_H_*/
|
||||
@@ -0,0 +1,540 @@
|
||||
/**
|
||||
* @file leak_detective.c
|
||||
*
|
||||
* @brief Allocation hooks to find memory leaks.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 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 <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <malloc.h>
|
||||
#include <execinfo.h>
|
||||
#include <signal.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <dlfcn.h>
|
||||
#include <unistd.h>
|
||||
#include <syslog.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "leak_detective.h"
|
||||
|
||||
#include <types.h>
|
||||
#include <utils/logger_manager.h>
|
||||
|
||||
#ifdef LEAK_DETECTIVE
|
||||
|
||||
/**
|
||||
* Magic value which helps to detect memory corruption
|
||||
*/
|
||||
#define MEMORY_HEADER_MAGIC 0xF1367ADF
|
||||
|
||||
static void install_hooks(void);
|
||||
static void uninstall_hooks(void);
|
||||
static void *malloc_hook(size_t, const void *);
|
||||
static void *realloc_hook(void *, size_t, const void *);
|
||||
static void free_hook(void*, const void *);
|
||||
static void load_excluded_functions();
|
||||
|
||||
typedef struct memory_header_t memory_header_t;
|
||||
|
||||
/**
|
||||
* Header which is prepended to each allocated memory block
|
||||
*/
|
||||
struct memory_header_t {
|
||||
/**
|
||||
* Magci byte which must(!) hold MEMORY_HEADER_MAGIC
|
||||
*/
|
||||
u_int32_t magic;
|
||||
|
||||
/**
|
||||
* Number of bytes following after the header
|
||||
*/
|
||||
size_t bytes;
|
||||
|
||||
/**
|
||||
* Stack frames at the time of allocation
|
||||
*/
|
||||
void *stack_frames[STACK_FRAMES_COUNT];
|
||||
|
||||
/**
|
||||
* Number of stacks frames obtained in stack_frames
|
||||
*/
|
||||
int stack_frame_count;
|
||||
|
||||
/**
|
||||
* Pointer to previous entry in linked list
|
||||
*/
|
||||
memory_header_t *previous;
|
||||
|
||||
/**
|
||||
* Pointer to next entry in linked list
|
||||
*/
|
||||
memory_header_t *next;
|
||||
};
|
||||
|
||||
/**
|
||||
* first mem header is just a dummy to chain
|
||||
* the others on it...
|
||||
*/
|
||||
static memory_header_t first_header = {
|
||||
magic: MEMORY_HEADER_MAGIC,
|
||||
bytes: 0,
|
||||
stack_frame_count: 0,
|
||||
previous: NULL,
|
||||
next: NULL
|
||||
};
|
||||
|
||||
/**
|
||||
* logger for the leak detective
|
||||
*/
|
||||
static logger_t *logger;
|
||||
|
||||
/**
|
||||
* standard hooks, used to temparily remove hooking
|
||||
*/
|
||||
static void *old_malloc_hook, *old_realloc_hook, *old_free_hook;
|
||||
|
||||
/**
|
||||
* are the hooks currently installed?
|
||||
*/
|
||||
static bool installed = FALSE;
|
||||
|
||||
/**
|
||||
* Mutex to exclusivly uninstall hooks, access heap list
|
||||
*/
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
|
||||
/**
|
||||
* log stack frames queried by backtrace()
|
||||
* TODO: Dump symbols of static functions!!!
|
||||
*/
|
||||
static void log_stack_frames(void **stack_frames, int stack_frame_count)
|
||||
{
|
||||
char **strings;
|
||||
size_t i;
|
||||
|
||||
strings = backtrace_symbols (stack_frames, stack_frame_count);
|
||||
|
||||
logger->log(logger, ERROR, " dumping %d stack frame addresses.", stack_frame_count);
|
||||
|
||||
for (i = 0; i < stack_frame_count; i++)
|
||||
{
|
||||
logger->log(logger, ERROR, " %s", strings[i]);
|
||||
}
|
||||
free (strings);
|
||||
}
|
||||
|
||||
/**
|
||||
* Report leaks at library destruction
|
||||
*/
|
||||
void report_leaks()
|
||||
{
|
||||
memory_header_t *hdr;
|
||||
int leaks = 0;
|
||||
|
||||
/* reaquire a logger is necessary, this will force ((destructor))
|
||||
* order to work correctly */
|
||||
logger = logger_manager->get_logger(logger_manager, LEAK_DETECT);
|
||||
for (hdr = first_header.next; hdr != NULL; hdr = hdr->next)
|
||||
{
|
||||
logger->log(logger, ERROR, "Leak (%d bytes at %p)", hdr->bytes, hdr + 1);
|
||||
log_stack_frames(hdr->stack_frames, hdr->stack_frame_count);
|
||||
leaks++;
|
||||
}
|
||||
|
||||
switch (leaks)
|
||||
{
|
||||
case 0:
|
||||
logger->log(logger, CONTROL, "No leaks detected");
|
||||
break;
|
||||
case 1:
|
||||
logger->log(logger, ERROR, "One leak detected");
|
||||
break;
|
||||
default:
|
||||
logger->log(logger, ERROR, "%d leaks detected", leaks);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs the malloc hooks, enables leak detection
|
||||
*/
|
||||
static void install_hooks()
|
||||
{
|
||||
if (!installed)
|
||||
{
|
||||
old_malloc_hook = __malloc_hook;
|
||||
old_realloc_hook = __realloc_hook;
|
||||
old_free_hook = __free_hook;
|
||||
__malloc_hook = malloc_hook;
|
||||
__realloc_hook = realloc_hook;
|
||||
__free_hook = free_hook;
|
||||
installed = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstalls the malloc hooks, disables leak detection
|
||||
*/
|
||||
static void uninstall_hooks()
|
||||
{
|
||||
if (installed)
|
||||
{
|
||||
__malloc_hook = old_malloc_hook;
|
||||
__free_hook = old_free_hook;
|
||||
__realloc_hook = old_realloc_hook;
|
||||
installed = FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook function for malloc()
|
||||
*/
|
||||
void *malloc_hook(size_t bytes, const void *caller)
|
||||
{
|
||||
memory_header_t *hdr;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
hdr = malloc(bytes + sizeof(memory_header_t));
|
||||
|
||||
hdr->magic = MEMORY_HEADER_MAGIC;
|
||||
hdr->bytes = bytes;
|
||||
hdr->stack_frame_count = backtrace(hdr->stack_frames, STACK_FRAMES_COUNT);
|
||||
|
||||
/* insert at the beginning of the list */
|
||||
hdr->next = first_header.next;
|
||||
if (hdr->next)
|
||||
{
|
||||
hdr->next->previous = hdr;
|
||||
}
|
||||
hdr->previous = &first_header;
|
||||
first_header.next = hdr;
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return hdr + 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook function for free()
|
||||
*/
|
||||
void free_hook(void *ptr, const void *caller)
|
||||
{
|
||||
void *stack_frames[STACK_FRAMES_COUNT];
|
||||
int stack_frame_count;
|
||||
memory_header_t *hdr = ptr - sizeof(memory_header_t);
|
||||
|
||||
/* allow freeing of NULL */
|
||||
if (ptr == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
||||
{
|
||||
pthread_mutex_unlock(&mutex);
|
||||
/* TODO: since pthread_join cannot be excluded cleanly, we are not whining about bad frees */
|
||||
return;
|
||||
logger->log(logger, ERROR, "freeing of invalid memory (%p)", ptr);
|
||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||
log_stack_frames(stack_frames, stack_frame_count);
|
||||
return;
|
||||
}
|
||||
/* remove magic from hdr */
|
||||
hdr->magic = 0;
|
||||
|
||||
/* remove item from list */
|
||||
if (hdr->next)
|
||||
{
|
||||
hdr->next->previous = hdr->previous;
|
||||
}
|
||||
hdr->previous->next = hdr->next;
|
||||
|
||||
uninstall_hooks();
|
||||
free(hdr);
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook function for realloc()
|
||||
*/
|
||||
void *realloc_hook(void *old, size_t bytes, const void *caller)
|
||||
{
|
||||
void *new;
|
||||
memory_header_t *hdr = old - sizeof(memory_header_t);
|
||||
void *stack_frames[STACK_FRAMES_COUNT];
|
||||
int stack_frame_count;
|
||||
|
||||
/* allow reallocation of NULL */
|
||||
if (old == NULL)
|
||||
{
|
||||
return malloc_hook(bytes, caller);
|
||||
}
|
||||
if (hdr->magic != MEMORY_HEADER_MAGIC)
|
||||
{
|
||||
logger->log(logger, ERROR, "reallocation of invalid memory (%p)", old);
|
||||
stack_frame_count = backtrace(stack_frames, STACK_FRAMES_COUNT);
|
||||
log_stack_frames(stack_frames, stack_frame_count);
|
||||
kill(getpid(), SIGKILL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* malloc and free is done with hooks */
|
||||
new = malloc_hook(bytes, caller);
|
||||
memcpy(new, old, min(bytes, hdr->bytes));
|
||||
free_hook(old, caller);
|
||||
|
||||
return new;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Setup leak detective
|
||||
*/
|
||||
void leak_detective_init()
|
||||
{
|
||||
logger = logger_manager->get_logger(logger_manager, LEAK_DETECT);
|
||||
load_excluded_functions();
|
||||
install_hooks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean up leak detective
|
||||
*/
|
||||
void leak_detective_cleanup()
|
||||
{
|
||||
uninstall_hooks();
|
||||
report_leaks();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The following glibc functions are excluded from leak detection, since
|
||||
* they use static allocated buffers or other ugly allocation hacks.
|
||||
* For this to work, the linker must link libstrongswan preferred to
|
||||
* the other (overriden) libs.
|
||||
*/
|
||||
struct excluded_function {
|
||||
char *lib_name;
|
||||
char *function_name;
|
||||
void *handle;
|
||||
void *lib_function;
|
||||
} excluded_functions[] = {
|
||||
{"libc.so.6", "inet_ntoa", NULL, NULL},
|
||||
{"libpthread.so.0", "pthread_create", NULL, NULL},
|
||||
{"libpthread.so.0", "pthread_cancel", NULL, NULL},
|
||||
{"libpthread.so.0", "pthread_join", NULL, NULL},
|
||||
{"libpthread.so.0", "_pthread_cleanup_push",NULL, NULL},
|
||||
{"libpthread.so.0", "_pthread_cleanup_pop", NULL, NULL},
|
||||
{"libc.so.6", "mktime", NULL, NULL},
|
||||
{"libc.so.6", "vsyslog", NULL, NULL},
|
||||
{"libc.so.6", "strerror", NULL, NULL},
|
||||
};
|
||||
#define INET_NTOA 0
|
||||
#define PTHREAD_CREATE 1
|
||||
#define PTHREAD_CANCEL 2
|
||||
#define PTHREAD_JOIN 3
|
||||
#define PTHREAD_CLEANUP_PUSH 4
|
||||
#define PTHREAD_CLEANUP_POP 5
|
||||
#define MKTIME 6
|
||||
#define VSYSLOG 7
|
||||
#define STRERROR 8
|
||||
|
||||
|
||||
/**
|
||||
* Load libraries and function pointers for excluded functions
|
||||
*/
|
||||
static void load_excluded_functions()
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < sizeof(excluded_functions)/sizeof(struct excluded_function); i++)
|
||||
{
|
||||
void *handle, *function;
|
||||
handle = dlopen(excluded_functions[i].lib_name, RTLD_LAZY);
|
||||
if (handle == NULL)
|
||||
{
|
||||
kill(getpid(), SIGSEGV);
|
||||
}
|
||||
|
||||
function = dlsym(handle, excluded_functions[i].function_name);
|
||||
|
||||
if (function == NULL)
|
||||
{
|
||||
dlclose(handle);
|
||||
kill(getpid(), SIGSEGV);
|
||||
}
|
||||
excluded_functions[i].handle = handle;
|
||||
excluded_functions[i].lib_function = function;
|
||||
}
|
||||
}
|
||||
|
||||
char *inet_ntoa(struct in_addr in)
|
||||
{
|
||||
char *(*_inet_ntoa)(struct in_addr) = excluded_functions[INET_NTOA].lib_function;
|
||||
char *result;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
result = _inet_ntoa(in);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
int pthread_create(pthread_t *__restrict __threadp, __const pthread_attr_t *__restrict __attr,
|
||||
void *(*__start_routine) (void *), void *__restrict __arg)
|
||||
{
|
||||
int (*_pthread_create) (pthread_t *__restrict __threadp,
|
||||
__const pthread_attr_t *__restrict __attr,
|
||||
void *(*__start_routine) (void *),
|
||||
void *__restrict __arg) = excluded_functions[PTHREAD_CREATE].lib_function;
|
||||
int result;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
result = _pthread_create(__threadp, __attr, __start_routine, __arg);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
int pthread_cancel(pthread_t __th)
|
||||
{
|
||||
int (*_pthread_cancel) (pthread_t) = excluded_functions[PTHREAD_CANCEL].lib_function;
|
||||
int result;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
result = _pthread_cancel(__th);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
// /* TODO: join has probs, since it dellocates memory
|
||||
// * allocated (somewhere) with leak_detective :-(.
|
||||
// * We should exclude all pthread_ functions to fix it !? */
|
||||
// int pthread_join(pthread_t __th, void **__thread_return)
|
||||
// {
|
||||
// int (*_pthread_join) (pthread_t, void **) = excluded_functions[PTHREAD_JOIN].lib_function;
|
||||
// int result;
|
||||
//
|
||||
// pthread_mutex_lock(&mutex);
|
||||
// uninstall_hooks();
|
||||
//
|
||||
// result = _pthread_join(__th, __thread_return);
|
||||
//
|
||||
// install_hooks();
|
||||
// pthread_mutex_unlock(&mutex);
|
||||
// return result;
|
||||
// }
|
||||
//
|
||||
// void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer,
|
||||
// void (*__routine) (void *),
|
||||
// void *__arg)
|
||||
// {
|
||||
// int (*__pthread_cleanup_push) (struct _pthread_cleanup_buffer *__buffer,
|
||||
// void (*__routine) (void *),
|
||||
// void *__arg) =
|
||||
// excluded_functions[PTHREAD_CLEANUP_PUSH].lib_function;
|
||||
//
|
||||
// pthread_mutex_lock(&mutex);
|
||||
// uninstall_hooks();
|
||||
//
|
||||
// __pthread_cleanup_push(__buffer, __routine, __arg);
|
||||
//
|
||||
// install_hooks();
|
||||
// pthread_mutex_unlock(&mutex);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, int __execute)
|
||||
// {
|
||||
// int (*__pthread_cleanup_pop) (struct _pthread_cleanup_buffer *__buffer, int __execute) =
|
||||
// excluded_functions[PTHREAD_CLEANUP_POP].lib_function;
|
||||
//
|
||||
// pthread_mutex_lock(&mutex);
|
||||
// uninstall_hooks();
|
||||
//
|
||||
// __pthread_cleanup_pop(__buffer, __execute);
|
||||
//
|
||||
// install_hooks();
|
||||
// pthread_mutex_unlock(&mutex);
|
||||
// return;
|
||||
// }
|
||||
|
||||
time_t mktime(struct tm *tm)
|
||||
{
|
||||
time_t (*_mktime)(struct tm *tm) = excluded_functions[MKTIME].lib_function;
|
||||
time_t result;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
result = _mktime(tm);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
void vsyslog (int __pri, __const char *__fmt, __gnuc_va_list __ap)
|
||||
{
|
||||
void (*_vsyslog) (int __pri, __const char *__fmt, __gnuc_va_list __ap) = excluded_functions[VSYSLOG].lib_function;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
_vsyslog(__pri, __fmt, __ap);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *strerror(int errnum)
|
||||
{
|
||||
char* (*_strerror) (int) = excluded_functions[STRERROR].lib_function;
|
||||
char *result;
|
||||
|
||||
pthread_mutex_lock(&mutex);
|
||||
uninstall_hooks();
|
||||
|
||||
result = _strerror(errnum);
|
||||
|
||||
install_hooks();
|
||||
pthread_mutex_unlock(&mutex);
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif /* LEAK_DETECTION */
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* @file leak_detective.h
|
||||
*
|
||||
* @brief malloc/free hooks to detect leaks.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2006 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.
|
||||
*/
|
||||
|
||||
#ifndef LEAK_DETECTIVE_H_
|
||||
#define LEAK_DETECTIVE_H_
|
||||
|
||||
|
||||
#ifdef LEAK_DETECTIVE
|
||||
|
||||
/**
|
||||
* Max number of stack frames to include in a backtrace.
|
||||
*/
|
||||
#define STACK_FRAMES_COUNT 30
|
||||
|
||||
/**
|
||||
* Initialize leak detective, activates it
|
||||
*/
|
||||
void leak_detective_init();
|
||||
|
||||
/**
|
||||
* Cleanup leak detective, deactivates it
|
||||
*/
|
||||
void leak_detective_cleanup();
|
||||
|
||||
#else /* !LEAK_DETECTIVE */
|
||||
|
||||
#define leak_detective_init() {}
|
||||
#define leak_detective_cleanup() {}
|
||||
|
||||
#endif /* LEAK_DETECTIVE */
|
||||
|
||||
#endif /* LEAK_DETECTIVE_H_ */
|
||||
@@ -0,0 +1,727 @@
|
||||
/**
|
||||
* @file linked_list.c
|
||||
*
|
||||
* @brief Implementation of linked_list_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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>
|
||||
|
||||
#include "linked_list.h"
|
||||
|
||||
|
||||
|
||||
typedef struct linked_list_element_t linked_list_element_t;
|
||||
|
||||
/**
|
||||
* @brief Element in a linked list.
|
||||
*
|
||||
* This element holds a pointer to the value it represents.
|
||||
*/
|
||||
struct linked_list_element_t {
|
||||
|
||||
/**
|
||||
* Value of a list item.
|
||||
*/
|
||||
void *value;
|
||||
|
||||
/**
|
||||
* Previous list element.
|
||||
*
|
||||
* NULL if first element in list.
|
||||
*/
|
||||
linked_list_element_t *previous;
|
||||
|
||||
/**
|
||||
* Next list element.
|
||||
*
|
||||
* NULL if last element in list.
|
||||
*/
|
||||
linked_list_element_t *next;
|
||||
|
||||
/**
|
||||
* Destroys a linked_list_element object.
|
||||
*
|
||||
* @param linked_list_element_t calling object
|
||||
*/
|
||||
void (*destroy) (linked_list_element_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_element_t.destroy.
|
||||
*/
|
||||
static void linked_list_element_destroy(linked_list_element_t *this)
|
||||
{
|
||||
free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates an empty linked list object.
|
||||
*
|
||||
* @warning Only the pointer to the value is stored.
|
||||
*
|
||||
* @param[in] value value of item to be set
|
||||
* @return linked_list_element_t object
|
||||
*/
|
||||
|
||||
linked_list_element_t *linked_list_element_create(void *value)
|
||||
{
|
||||
linked_list_element_t *this = malloc_thing(linked_list_element_t);
|
||||
|
||||
this->destroy = linked_list_element_destroy;
|
||||
|
||||
this->previous=NULL;
|
||||
this->next=NULL;
|
||||
this->value = value;
|
||||
|
||||
return (this);
|
||||
}
|
||||
|
||||
|
||||
typedef struct private_linked_list_t private_linked_list_t;
|
||||
|
||||
/**
|
||||
* Private data of a linked_list_t object.
|
||||
*
|
||||
*/
|
||||
struct private_linked_list_t {
|
||||
/**
|
||||
* Public part of linked list.
|
||||
*/
|
||||
linked_list_t public;
|
||||
|
||||
/**
|
||||
* Number of items in the list.
|
||||
*/
|
||||
int count;
|
||||
|
||||
/**
|
||||
* First element in list.
|
||||
* NULL if no elements in list.
|
||||
*/
|
||||
linked_list_element_t *first;
|
||||
|
||||
/**
|
||||
* Last element in list.
|
||||
* NULL if no elements in list.
|
||||
*/
|
||||
linked_list_element_t *last;
|
||||
};
|
||||
|
||||
|
||||
typedef struct private_iterator_t private_iterator_t;
|
||||
|
||||
/**
|
||||
* Private variables and functions of linked list iterator.
|
||||
*/
|
||||
struct private_iterator_t {
|
||||
/**
|
||||
* Public part of linked list iterator.
|
||||
*/
|
||||
iterator_t public;
|
||||
|
||||
/**
|
||||
* Associated linked list.
|
||||
*/
|
||||
private_linked_list_t * list;
|
||||
|
||||
/**
|
||||
* Current element of the iterator.
|
||||
*/
|
||||
linked_list_element_t *current;
|
||||
|
||||
/**
|
||||
* Direction of iterator.
|
||||
*/
|
||||
bool forward;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.has_next.
|
||||
*/
|
||||
static bool iterate(private_iterator_t *this, void** value)
|
||||
{
|
||||
if (this->list->count == 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (this->current == NULL)
|
||||
{
|
||||
this->current = (this->forward) ? this->list->first : this->list->last;
|
||||
*value = this->current->value;
|
||||
return TRUE;
|
||||
}
|
||||
if (this->forward)
|
||||
{
|
||||
if (this->current->next == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
this->current = this->current->next;
|
||||
*value = this->current->value;
|
||||
return TRUE;
|
||||
}
|
||||
/* backward */
|
||||
if (this->current->previous == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
this->current = this->current->previous;
|
||||
*value = this->current->value;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.has_next.
|
||||
*/
|
||||
static bool iterator_has_next(private_iterator_t *this)
|
||||
{
|
||||
if (this->list->count == 0)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
if (this->current == NULL)
|
||||
{
|
||||
this->current = (this->forward) ? this->list->first : this->list->last;
|
||||
return TRUE;
|
||||
}
|
||||
if (this->forward)
|
||||
{
|
||||
if (this->current->next == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
this->current = this->current->next;
|
||||
return TRUE;
|
||||
}
|
||||
/* backward */
|
||||
if (this->current->previous == NULL)
|
||||
{
|
||||
return FALSE;
|
||||
}
|
||||
this->current = this->current->previous;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.current.
|
||||
*/
|
||||
static status_t iterator_current(private_iterator_t *this, void **value)
|
||||
{
|
||||
if (this->current == NULL)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
*value = this->current->value;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.reset.
|
||||
*/
|
||||
static void iterator_reset(private_iterator_t *this)
|
||||
{
|
||||
this->current = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.remove.
|
||||
*/
|
||||
static status_t remove(private_iterator_t *this)
|
||||
{
|
||||
linked_list_element_t *new_current;
|
||||
|
||||
if (this->current == NULL)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
if (this->list->count == 0)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
/* find out the new iterator position */
|
||||
if (this->current->previous != NULL)
|
||||
{
|
||||
new_current = this->current->previous;
|
||||
}
|
||||
else if (this->current->next != NULL)
|
||||
{
|
||||
new_current = this->current->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
new_current = NULL;
|
||||
}
|
||||
|
||||
/* now delete the entry :-) */
|
||||
if (this->current->previous == NULL)
|
||||
{
|
||||
if (this->current->next == NULL)
|
||||
{
|
||||
this->list->first = NULL;
|
||||
this->list->last = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->current->next->previous = NULL;
|
||||
this->list->first = this->current->next;
|
||||
}
|
||||
}
|
||||
else if (this->current->next == NULL)
|
||||
{
|
||||
this->current->previous->next = NULL;
|
||||
this->list->last = this->current->previous;
|
||||
}
|
||||
else
|
||||
{
|
||||
this->current->previous->next = this->current->next;
|
||||
this->current->next->previous = this->current->previous;
|
||||
}
|
||||
|
||||
this->list->count--;
|
||||
this->current->destroy(this->current);
|
||||
/* set the new iterator position */
|
||||
this->current = new_current;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.insert_before.
|
||||
*/
|
||||
static void insert_before(private_iterator_t * iterator, void *item)
|
||||
{
|
||||
if (iterator->current == NULL)
|
||||
{
|
||||
iterator->list->public.insert_first(&(iterator->list->public), item);
|
||||
}
|
||||
|
||||
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
|
||||
|
||||
if (iterator->current->previous == NULL)
|
||||
{
|
||||
iterator->current->previous = element;
|
||||
element->next = iterator->current;
|
||||
iterator->list->first = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
iterator->current->previous->next = element;
|
||||
element->previous = iterator->current->previous;
|
||||
iterator->current->previous = element;
|
||||
element->next = iterator->current;
|
||||
}
|
||||
|
||||
iterator->list->count++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.replace.
|
||||
*/
|
||||
static status_t replace (private_iterator_t *this, void **old_item, void *new_item)
|
||||
{
|
||||
if (this->current == NULL)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
if (old_item != NULL)
|
||||
{
|
||||
*old_item = this->current->value;
|
||||
}
|
||||
this->current->value = new_item;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.insert_after.
|
||||
*/
|
||||
static void insert_after(private_iterator_t * iterator, void *item)
|
||||
{
|
||||
if (iterator->current == NULL)
|
||||
{
|
||||
iterator->list->public.insert_first(&(iterator->list->public),item);
|
||||
return;
|
||||
}
|
||||
|
||||
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
|
||||
|
||||
if (iterator->current->next == NULL)
|
||||
{
|
||||
iterator->current->next = element;
|
||||
element->previous = iterator->current;
|
||||
iterator->list->last = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
iterator->current->next->previous = element;
|
||||
element->next = iterator->current->next;
|
||||
iterator->current->next = element;
|
||||
element->previous = iterator->current;
|
||||
}
|
||||
iterator->list->count++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of iterator_t.destroy.
|
||||
*/
|
||||
static void iterator_destroy(private_iterator_t *this)
|
||||
{
|
||||
free(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.get_count.
|
||||
*/
|
||||
static int get_count(private_linked_list_t *this)
|
||||
{
|
||||
return this->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.call_on_items.
|
||||
*/
|
||||
static void call_on_items(private_linked_list_t *this, void(*func)(void*))
|
||||
{
|
||||
iterator_t *iterator;
|
||||
void *item;
|
||||
|
||||
iterator = this->public.create_iterator(&(this->public),TRUE);
|
||||
|
||||
while (iterator->has_next(iterator))
|
||||
{
|
||||
iterator->current(iterator, &item);
|
||||
(*func)(item);
|
||||
}
|
||||
iterator->destroy(iterator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.insert_first.
|
||||
*/
|
||||
static void insert_first(private_linked_list_t *this, void *item)
|
||||
{
|
||||
linked_list_element_t *element;
|
||||
|
||||
element =(linked_list_element_t *) linked_list_element_create(item);
|
||||
|
||||
if (this->count == 0)
|
||||
{
|
||||
/* first entry in list */
|
||||
this->first = element;
|
||||
this->last = element;
|
||||
element->previous = NULL;
|
||||
element->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
linked_list_element_t *old_first_element = this->first;
|
||||
element->next = old_first_element;
|
||||
element->previous = NULL;
|
||||
old_first_element->previous = element;
|
||||
this->first = element;
|
||||
}
|
||||
|
||||
this->count++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.remove_first.
|
||||
*/
|
||||
static status_t remove_first(private_linked_list_t *this, void **item)
|
||||
{
|
||||
if (this->count == 0)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
linked_list_element_t *element = this->first;
|
||||
|
||||
if (element->next != NULL)
|
||||
{
|
||||
element->next->previous = NULL;
|
||||
}
|
||||
this->first = element->next;
|
||||
|
||||
if (item != NULL)
|
||||
{
|
||||
*item = element->value;
|
||||
}
|
||||
|
||||
this->count--;
|
||||
|
||||
element->destroy(element);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.get_first.
|
||||
*/
|
||||
static status_t get_first(private_linked_list_t *this, void **item)
|
||||
{
|
||||
if (this->count == 0)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
*item = this->first->value;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.insert_last.
|
||||
*/
|
||||
static void insert_last(private_linked_list_t *this, void *item)
|
||||
{
|
||||
linked_list_element_t *element = (linked_list_element_t *) linked_list_element_create(item);
|
||||
|
||||
if (this->count == 0)
|
||||
{
|
||||
/* first entry in list */
|
||||
this->first = element;
|
||||
this->last = element;
|
||||
element->previous = NULL;
|
||||
element->next = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
linked_list_element_t *old_last_element = this->last;
|
||||
element->previous = old_last_element;
|
||||
element->next = NULL;
|
||||
old_last_element->next = element;
|
||||
this->last = element;
|
||||
}
|
||||
|
||||
this->count++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.remove_last.
|
||||
*/
|
||||
static status_t remove_last(private_linked_list_t *this, void **item)
|
||||
{
|
||||
if (this->count == 0)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
linked_list_element_t *element = this->last;
|
||||
|
||||
if (element->previous != NULL)
|
||||
{
|
||||
element->previous->next = NULL;
|
||||
}
|
||||
this->last = element->previous;
|
||||
|
||||
if (item != NULL)
|
||||
{
|
||||
*item = element->value;
|
||||
}
|
||||
|
||||
this->count--;
|
||||
|
||||
element->destroy(element);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.insert_at_position.
|
||||
*/
|
||||
static status_t insert_at_position (private_linked_list_t *this,size_t position, void *item)
|
||||
{
|
||||
linked_list_element_t *current_element;
|
||||
int i;
|
||||
|
||||
if (this->count <= position)
|
||||
{
|
||||
return INVALID_ARG;
|
||||
}
|
||||
|
||||
current_element = this->first;
|
||||
|
||||
for (i = 0; i < position;i++)
|
||||
{
|
||||
current_element = current_element->next;
|
||||
}
|
||||
|
||||
if (current_element == NULL)
|
||||
{
|
||||
this->public.insert_last(&(this->public),item);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
linked_list_element_t *element =(linked_list_element_t *) linked_list_element_create(item);
|
||||
|
||||
|
||||
if (current_element->previous == NULL)
|
||||
{
|
||||
current_element->previous = element;
|
||||
element->next = current_element;
|
||||
this->first = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
current_element->previous->next = element;
|
||||
element->previous = current_element->previous;
|
||||
current_element->previous = element;
|
||||
element->next = current_element;
|
||||
}
|
||||
|
||||
|
||||
this->count++;
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.remove_at_position.
|
||||
*/
|
||||
static status_t remove_at_position (private_linked_list_t *this,size_t position, void **item)
|
||||
{
|
||||
iterator_t *iterator;
|
||||
int i;
|
||||
|
||||
if (this->count <= position)
|
||||
{
|
||||
return INVALID_ARG;
|
||||
}
|
||||
|
||||
iterator = this->public.create_iterator(&(this->public),TRUE);
|
||||
|
||||
iterator->has_next(iterator);
|
||||
for (i = 0; i < position;i++)
|
||||
{
|
||||
iterator->has_next(iterator);
|
||||
}
|
||||
iterator->current(iterator,item);
|
||||
iterator->remove(iterator);
|
||||
iterator->destroy(iterator);
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.get_at_position.
|
||||
*/
|
||||
static status_t get_at_position (private_linked_list_t *this,size_t position, void **item)
|
||||
{
|
||||
int i;
|
||||
iterator_t *iterator;
|
||||
status_t status;
|
||||
if (this->count <= position)
|
||||
{
|
||||
return INVALID_ARG;
|
||||
}
|
||||
|
||||
iterator = this->public.create_iterator(&(this->public),TRUE);
|
||||
|
||||
iterator->has_next(iterator);
|
||||
for (i = 0; i < position;i++)
|
||||
{
|
||||
iterator->has_next(iterator);
|
||||
}
|
||||
status = iterator->current(iterator,item);
|
||||
iterator->destroy(iterator);
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.get_last.
|
||||
*/
|
||||
static status_t get_last(private_linked_list_t *this, void **item)
|
||||
{
|
||||
if (this->count == 0)
|
||||
{
|
||||
return NOT_FOUND;
|
||||
}
|
||||
|
||||
*item = this->last->value;
|
||||
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.create_iterator.
|
||||
*/
|
||||
static iterator_t *create_iterator (private_linked_list_t *linked_list,bool forward)
|
||||
{
|
||||
private_iterator_t *this = malloc_thing(private_iterator_t);
|
||||
|
||||
this->public.iterate = (bool (*) (iterator_t *this, void **value)) iterate;
|
||||
this->public.has_next = (bool (*) (iterator_t *this)) iterator_has_next;
|
||||
this->public.current = (status_t (*) (iterator_t *this, void **value)) iterator_current;
|
||||
this->public.insert_before = (void (*) (iterator_t *this, void *item)) insert_before;
|
||||
this->public.insert_after = (void (*) (iterator_t *this, void *item)) insert_after;
|
||||
this->public.replace = (status_t (*) (iterator_t *, void **, void *)) replace;
|
||||
this->public.remove = (status_t (*) (iterator_t *this)) remove;
|
||||
this->public.reset = (void (*) (iterator_t *this)) iterator_reset;
|
||||
this->public.destroy = (void (*) (iterator_t *this)) iterator_destroy;
|
||||
|
||||
this->forward = forward;
|
||||
this->current = NULL;
|
||||
this->list = linked_list;
|
||||
|
||||
return &(this->public);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of linked_list_t.destroy.
|
||||
*/
|
||||
static void linked_list_destroy(private_linked_list_t *this)
|
||||
{
|
||||
void * value;
|
||||
/* Remove all list items before destroying list */
|
||||
while (this->public.remove_first(&(this->public),&value) != NOT_FOUND)
|
||||
{
|
||||
/* values are not destroyed so memory leaks are possible
|
||||
* if list is not empty when deleting */
|
||||
}
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
linked_list_t *linked_list_create()
|
||||
{
|
||||
private_linked_list_t *this = malloc_thing(private_linked_list_t);
|
||||
|
||||
this->public.get_count = (int (*) (linked_list_t *)) get_count;
|
||||
this->public.create_iterator = (iterator_t * (*) (linked_list_t *,bool )) create_iterator;
|
||||
this->public.call_on_items = (void (*) (linked_list_t *, void(*func)(void*)))call_on_items;
|
||||
this->public.get_first = (status_t (*) (linked_list_t *, void **item)) get_first;
|
||||
this->public.get_last = (status_t (*) (linked_list_t *, void **item)) get_last;
|
||||
this->public.insert_first = (void (*) (linked_list_t *, void *item)) insert_first;
|
||||
this->public.insert_last = (void (*) (linked_list_t *, void *item)) insert_last;
|
||||
this->public.remove_first = (status_t (*) (linked_list_t *, void **item)) remove_first;
|
||||
this->public.remove_last = (status_t (*) (linked_list_t *, void **item)) remove_last;
|
||||
this->public.insert_at_position =(status_t (*) (linked_list_t *,size_t, void *)) insert_at_position;
|
||||
this->public.remove_at_position =(status_t (*) (linked_list_t *,size_t, void **)) remove_at_position;
|
||||
this->public.get_at_position =(status_t (*) (linked_list_t *,size_t, void **)) get_at_position;
|
||||
|
||||
this->public.destroy = (void (*) (linked_list_t *)) linked_list_destroy;
|
||||
|
||||
this->count = 0;
|
||||
this->first = NULL;
|
||||
this->last = NULL;
|
||||
|
||||
return (&(this->public));
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
/**
|
||||
* @file linked_list.h
|
||||
*
|
||||
* @brief Interface of linked_list_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef LINKED_LIST_H_
|
||||
#define LINKED_LIST_H_
|
||||
|
||||
#include <types.h>
|
||||
#include <utils/iterator.h>
|
||||
|
||||
|
||||
typedef struct linked_list_t linked_list_t;
|
||||
|
||||
/**
|
||||
* @brief Class implementing a double linked list (named only as linked list).
|
||||
*
|
||||
* @warning Access to an object of this type is not thread-save.
|
||||
*
|
||||
* @b Costructors:
|
||||
* - linked_list_create()
|
||||
*
|
||||
* @see
|
||||
* - job_queue_t
|
||||
* - event_queue_t
|
||||
* - send_queue_t
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct linked_list_t {
|
||||
|
||||
/**
|
||||
* @brief Gets the count of items in the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @return number of items in list
|
||||
*/
|
||||
int (*get_count) (linked_list_t *linked_list);
|
||||
|
||||
/**
|
||||
* @brief Creates a iterator for the given list.
|
||||
*
|
||||
* @warning Created iterator_t object has to get destroyed by the caller.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param forward iterator direction (TRUE: front to end)
|
||||
* @return new iterator_t object
|
||||
*/
|
||||
iterator_t * (*create_iterator) (linked_list_t *linked_list, bool forward);
|
||||
|
||||
/**
|
||||
* @brief Call a function with list element as argument.
|
||||
*
|
||||
* This method accepts a function, which will be called for
|
||||
* each list element once. The function must accept the list
|
||||
* element as the first argument. Handy for destruction of
|
||||
* list elements.
|
||||
*
|
||||
* @todo Additional vararg which are passed to the
|
||||
* function would be nice...
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param func function to call
|
||||
*/
|
||||
void (*call_on_items) (linked_list_t *linked_list, void(*func)(void*));
|
||||
|
||||
/**
|
||||
* @brief Inserts a new item at the beginning of the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[in] item item value to insert in list
|
||||
*/
|
||||
void (*insert_first) (linked_list_t *linked_list, void *item);
|
||||
|
||||
/**
|
||||
* @brief Removes the first item in the list and returns its value.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[out] item returned value of first item, or NULL
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - NOT_FOUND, if list is empty
|
||||
*/
|
||||
status_t (*remove_first) (linked_list_t *linked_list, void **item);
|
||||
|
||||
/**
|
||||
* @brief Returns the value of the first list item without removing it.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[out] item item returned value of first item
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - NOT_FOUND, if list is empty
|
||||
*/
|
||||
status_t (*get_first) (linked_list_t *linked_list, void **item);
|
||||
|
||||
/**
|
||||
* @brief Inserts a new item at the end of the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[in] item item value to insert into list
|
||||
*/
|
||||
void (*insert_last) (linked_list_t *linked_list, void *item);
|
||||
|
||||
/**
|
||||
* @brief Inserts a new item at a given position in the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param position position starting at 0 to insert new entry
|
||||
* @param[in] item item value to insert into list
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - INVALID_ARG if position not existing
|
||||
*/
|
||||
status_t (*insert_at_position) (linked_list_t *linked_list,size_t position, void *item);
|
||||
|
||||
/**
|
||||
* @brief Removes an item from a given position in the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param position position starting at 0 to remove entry from
|
||||
* @param[out] item removed item will be stored at this location
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - INVALID_ARG if position not existing
|
||||
*/
|
||||
status_t (*remove_at_position) (linked_list_t *linked_list,size_t position, void **item);
|
||||
|
||||
/**
|
||||
* @brief Get an item from a given position in the list.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param position position starting at 0 to get entry from
|
||||
* @param[out] item item will be stored at this location
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - INVALID_ARG if position not existing
|
||||
*/
|
||||
status_t (*get_at_position) (linked_list_t *linked_list,size_t position, void **item);
|
||||
|
||||
/**
|
||||
* @brief Removes the last item in the list and returns its value.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[out] item returned value of last item, or NULL
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - NOT_FOUND if list is empty
|
||||
*/
|
||||
status_t (*remove_last) (linked_list_t *linked_list, void **item);
|
||||
|
||||
/**
|
||||
* @brief Returns the value of the last list item without removing it.
|
||||
*
|
||||
* @param linked_list calling object
|
||||
* @param[out] item returned value of last item
|
||||
* @return
|
||||
* - SUCCESS
|
||||
* - NOT_FOUND if list is empty
|
||||
*/
|
||||
status_t (*get_last) (linked_list_t *linked_list, void **item);
|
||||
|
||||
/**
|
||||
* @brief Destroys a linked_list object.
|
||||
*
|
||||
* @warning All items are removed before deleting the list. The
|
||||
* associated values are NOT destroyed.
|
||||
* Destroying an list which is not empty may cause
|
||||
* memory leaks!
|
||||
*
|
||||
* @param linked_list calling object
|
||||
*/
|
||||
void (*destroy) (linked_list_t *linked_list);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Creates an empty linked list object.
|
||||
*
|
||||
* @return linked_list_t object.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
linked_list_t *linked_list_create();
|
||||
|
||||
|
||||
#endif /*LINKED_LIST_H_*/
|
||||
@@ -0,0 +1,342 @@
|
||||
/**
|
||||
* @file logger.c
|
||||
*
|
||||
* @brief Implementation of logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 <syslog.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <pthread.h>
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
|
||||
/**
|
||||
* Maximum length of a log entry (only used for logger_s.log).
|
||||
*/
|
||||
#define MAX_LOG 8192
|
||||
|
||||
/**
|
||||
* Maximum number of logged bytes per line
|
||||
*/
|
||||
#define MAX_BYTES 16
|
||||
|
||||
typedef struct private_logger_t private_logger_t;
|
||||
|
||||
/**
|
||||
* @brief Private data of a logger_t object.
|
||||
*/
|
||||
struct private_logger_t {
|
||||
/**
|
||||
* Public data.
|
||||
*/
|
||||
logger_t public;
|
||||
/**
|
||||
* Detail-level of logger.
|
||||
*/
|
||||
log_level_t level;
|
||||
/**
|
||||
* Name of logger.
|
||||
*/
|
||||
char *name;
|
||||
/**
|
||||
* File to write log output to.
|
||||
* NULL for syslog.
|
||||
*/
|
||||
FILE *output;
|
||||
|
||||
/**
|
||||
* Should a thread_id be included in the log?
|
||||
*/
|
||||
bool log_thread_id;
|
||||
};
|
||||
|
||||
/**
|
||||
* prepend the logging prefix to string and store it in buffer
|
||||
*/
|
||||
static void prepend_prefix(private_logger_t *this, log_level_t loglevel, const char *string, char *buffer)
|
||||
{
|
||||
char log_type, log_details;
|
||||
char thread_id[10] = "";
|
||||
|
||||
if (loglevel & CONTROL)
|
||||
{
|
||||
log_type = 'C';
|
||||
}
|
||||
else if (loglevel & ERROR)
|
||||
{
|
||||
log_type = 'E';
|
||||
}
|
||||
else if (loglevel & RAW)
|
||||
{
|
||||
log_type = 'R';
|
||||
}
|
||||
else if (loglevel & PRIVATE)
|
||||
{
|
||||
log_type = 'P';
|
||||
}
|
||||
else if (loglevel & AUDIT)
|
||||
{
|
||||
log_type = 'A';
|
||||
}
|
||||
else
|
||||
{
|
||||
log_type = '-';
|
||||
}
|
||||
|
||||
if (loglevel & (LEVEL3 - LEVEL2))
|
||||
{
|
||||
log_details = '3';
|
||||
}
|
||||
else if (loglevel & (LEVEL2 - LEVEL1))
|
||||
{
|
||||
log_details = '2';
|
||||
}
|
||||
else if (loglevel & LEVEL1)
|
||||
{
|
||||
log_details = '1';
|
||||
}
|
||||
else
|
||||
{
|
||||
log_details = '0';
|
||||
}
|
||||
|
||||
if (this->log_thread_id)
|
||||
{
|
||||
snprintf(thread_id, sizeof(thread_id), " @%d", (int)pthread_self());
|
||||
}
|
||||
snprintf(buffer, MAX_LOG, "[%c%c:%s]%s %s", log_type, log_details, this->name, thread_id, string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a charon-loglevel to a syslog priority
|
||||
*/
|
||||
static int get_priority(log_level_t loglevel)
|
||||
{
|
||||
if (loglevel & AUDIT)
|
||||
{
|
||||
return LOG_AUTHPRIV|LOG_INFO;
|
||||
}
|
||||
return LOG_DAEMON|LOG_DEBUG;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.log.
|
||||
*
|
||||
* Yes, logg is written wrong :-).
|
||||
*/
|
||||
static void logg(private_logger_t *this, log_level_t loglevel, const char *format, ...)
|
||||
{
|
||||
if ((this->level & loglevel) == loglevel)
|
||||
{
|
||||
char buffer[MAX_LOG];
|
||||
va_list args;
|
||||
|
||||
|
||||
if (this->output == NULL)
|
||||
{
|
||||
/* syslog */
|
||||
prepend_prefix(this, loglevel, format, buffer);
|
||||
va_start(args, format);
|
||||
vsyslog(get_priority(loglevel), buffer, args);
|
||||
va_end(args);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* File output */
|
||||
prepend_prefix(this, loglevel, format, buffer);
|
||||
va_start(args, format);
|
||||
vfprintf(this->output, buffer, args);
|
||||
va_end(args);
|
||||
fprintf(this->output, "\n");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.log_bytes.
|
||||
*/
|
||||
static void log_bytes(private_logger_t *this, log_level_t loglevel, const char *label, const char *bytes, size_t len)
|
||||
{
|
||||
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
if ((this->level & loglevel) == loglevel)
|
||||
{
|
||||
char thread_id[10] = "";
|
||||
char buffer[MAX_LOG];
|
||||
char ascii_buffer[MAX_BYTES+1];
|
||||
|
||||
char *buffer_pos = buffer;
|
||||
const char format[] = "%s %d bytes @ %p";
|
||||
const char *bytes_pos = bytes;
|
||||
const char *bytes_roof = bytes + len;
|
||||
|
||||
int line_start = 0;
|
||||
int i = 0;
|
||||
|
||||
if (this->log_thread_id)
|
||||
{
|
||||
snprintf(thread_id, sizeof(thread_id), " @%d", (int)pthread_self());
|
||||
}
|
||||
|
||||
/* since me can't do multi-line output to syslog,
|
||||
* we must do multiple syslogs. To avoid
|
||||
* problems in output order, lock this by a mutex.
|
||||
*/
|
||||
pthread_mutex_lock(&mutex);
|
||||
|
||||
prepend_prefix(this, loglevel, format, buffer);
|
||||
|
||||
if (this->output == NULL)
|
||||
{
|
||||
syslog(get_priority(loglevel), buffer, label, len, bytes);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(this->output, buffer, label, len, bytes);
|
||||
fprintf(this->output, "\n");
|
||||
}
|
||||
|
||||
while (bytes_pos < bytes_roof)
|
||||
{
|
||||
static char hexdig[] = "0123456789ABCDEF";
|
||||
|
||||
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
|
||||
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
|
||||
|
||||
ascii_buffer[i++] = (*bytes_pos > 31 && *bytes_pos < 127)
|
||||
? *bytes_pos : '.';
|
||||
|
||||
if (++bytes_pos == bytes_roof || i == MAX_BYTES)
|
||||
{
|
||||
int padding = 3 * (MAX_BYTES - i);
|
||||
|
||||
while (padding--)
|
||||
{
|
||||
*buffer_pos++ = ' ';
|
||||
}
|
||||
*buffer_pos++ = '\0';
|
||||
ascii_buffer[i] = '\0';
|
||||
|
||||
if (this->output == NULL)
|
||||
{
|
||||
syslog(get_priority(loglevel), "[ :%5d]%s %s %s", line_start, thread_id, buffer, ascii_buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(this->output, "[ :%5d]%s %s %s\n", line_start, thread_id, buffer, ascii_buffer);
|
||||
}
|
||||
buffer_pos = buffer;
|
||||
line_start += MAX_BYTES;
|
||||
i = 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
*buffer_pos++ = ' ';
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&mutex);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.log_chunk.
|
||||
*/
|
||||
static void log_chunk(logger_t *this, log_level_t loglevel, const char *label, chunk_t chunk)
|
||||
{
|
||||
this->log_bytes(this, loglevel, label, chunk.ptr, chunk.len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.enable_level.
|
||||
*/
|
||||
static void enable_level(private_logger_t *this, log_level_t log_level)
|
||||
{
|
||||
this->level |= log_level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.disable_level.
|
||||
*/
|
||||
static void disable_level(private_logger_t *this, log_level_t log_level)
|
||||
{
|
||||
this->level &= ~log_level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.set_output.
|
||||
*/
|
||||
static void set_output(private_logger_t *this, FILE * output)
|
||||
{
|
||||
this->output = output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.get_level.
|
||||
*/
|
||||
static log_level_t get_level(private_logger_t *this)
|
||||
{
|
||||
return this->level;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_t.destroy.
|
||||
*/
|
||||
static void destroy(private_logger_t *this)
|
||||
{
|
||||
free(this->name);
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output)
|
||||
{
|
||||
private_logger_t *this = malloc_thing(private_logger_t);
|
||||
|
||||
/* public functions */
|
||||
this->public.log = (void(*)(logger_t*,log_level_t,const char*,...))logg;
|
||||
this->public.log_bytes = (void(*)(logger_t*, log_level_t, const char*, const char*,size_t))log_bytes;
|
||||
this->public.log_chunk = log_chunk;
|
||||
this->public.enable_level = (void(*)(logger_t*,log_level_t))enable_level;
|
||||
this->public.disable_level = (void(*)(logger_t*,log_level_t))disable_level;
|
||||
this->public.get_level = (log_level_t(*)(logger_t*))get_level;
|
||||
this->public.set_output = (void(*)(logger_t*,FILE*))set_output;
|
||||
this->public.destroy = (void(*)(logger_t*))destroy;
|
||||
|
||||
if (logger_name == NULL)
|
||||
{
|
||||
logger_name = "";
|
||||
}
|
||||
|
||||
/* private variables */
|
||||
this->level = log_level;
|
||||
this->log_thread_id = log_thread_id;
|
||||
this->name = malloc(strlen(logger_name) + 1);
|
||||
|
||||
strcpy(this->name,logger_name);
|
||||
this->output = output;
|
||||
|
||||
return (logger_t*)this;
|
||||
}
|
||||
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* @file logger.h
|
||||
*
|
||||
* @brief Interface of logger_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef LOGGER_H_
|
||||
#define LOGGER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <types.h>
|
||||
|
||||
typedef enum log_level_t log_level_t;
|
||||
|
||||
/**
|
||||
* @brief Log Levels supported by the logger object.
|
||||
*
|
||||
* Logleves are devided in two different kinds:
|
||||
* - levels to specify the type of the log
|
||||
* - levels to specify the detail-level of the log
|
||||
*
|
||||
* Use combinations of these to build detailed loglevels, such
|
||||
* as CONTROL|LEVEL2 fore a detailed cotrol level, or
|
||||
* use RAW to see all raw data dumps (except private).
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
enum log_level_t {
|
||||
/**
|
||||
* Control flow.
|
||||
*/
|
||||
CONTROL = 1,
|
||||
/**
|
||||
* Error reporting.
|
||||
*/
|
||||
ERROR = 2,
|
||||
/**
|
||||
* Logs important for the sysadmin.
|
||||
*/
|
||||
AUDIT = 4,
|
||||
/**
|
||||
* Raw data dumps.
|
||||
*/
|
||||
RAW = 8,
|
||||
/**
|
||||
* Private data dumps.
|
||||
*/
|
||||
PRIVATE = 16,
|
||||
|
||||
/**
|
||||
* Log most important output, can be omitted.
|
||||
*/
|
||||
LEVEL0 = 0,
|
||||
/**
|
||||
* Log more detailed output.
|
||||
*/
|
||||
LEVEL1 = 32,
|
||||
/**
|
||||
* Log even more detailed output.
|
||||
*/
|
||||
LEVEL2 = LEVEL1 + 64,
|
||||
/**
|
||||
* Use maximum detailed output.
|
||||
*/
|
||||
LEVEL3 = LEVEL2 + 128,
|
||||
|
||||
/**
|
||||
* Summary for all types with all detail-levels.
|
||||
*/
|
||||
FULL = LEVEL3 + CONTROL + ERROR + RAW + PRIVATE + AUDIT
|
||||
};
|
||||
|
||||
typedef struct logger_t logger_t;
|
||||
|
||||
/**
|
||||
* @brief Class to simplify logging.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - logger_create()
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct logger_t {
|
||||
|
||||
/**
|
||||
* @brief Log an entry, using printf()-like params.
|
||||
*
|
||||
* All specified loglevels must be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param loglevel or'ed set of log_level_t's
|
||||
* @param format printf like format string
|
||||
* @param ... printf like parameters
|
||||
*/
|
||||
void (*log) (logger_t *this, log_level_t log_level, const char *format, ...);
|
||||
|
||||
/**
|
||||
* @brief Log some bytes, useful for debugging.
|
||||
*
|
||||
* All specified loglevels must be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param loglevel or'ed set of log_level_t's
|
||||
* @param label a labeling name, logged with the bytes
|
||||
* @param bytes pointer to the bytes to dump
|
||||
* @param len number of bytes to dump
|
||||
*/
|
||||
void (*log_bytes) (logger_t *this, log_level_t loglevel, const char *label, const char *bytes, size_t len);
|
||||
|
||||
/**
|
||||
* @brief Log a chunk, useful for debugging.
|
||||
*
|
||||
* All specified loglevels must be activated that
|
||||
* the log is done.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param loglevel or'ed set of log_level_t's
|
||||
* @param label a labeling name, logged with the bytes
|
||||
* @param chunk chunk to log
|
||||
*/
|
||||
void (*log_chunk) (logger_t *this, log_level_t loglevel, const char *label, chunk_t chunk);
|
||||
|
||||
/**
|
||||
* @brief Enables a loglevel for the current logger_t object.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param log_level loglevel to enable
|
||||
*/
|
||||
void (*enable_level) (logger_t *this, log_level_t log_level);
|
||||
|
||||
/**
|
||||
* @brief Disables a loglevel for the current logger_t object.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param log_level loglevel to enable
|
||||
*/
|
||||
void (*disable_level) (logger_t *this, log_level_t log_level);
|
||||
|
||||
/**
|
||||
* @brief Set the output of the logger.
|
||||
*
|
||||
* Use NULL for syslog.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @param output file, where log output should be written
|
||||
*/
|
||||
void (*set_output) (logger_t *this, FILE *output);
|
||||
|
||||
/**
|
||||
* @brief Get the currently used loglevel.
|
||||
*
|
||||
* @param this logger_t object
|
||||
* @return currently used loglevel
|
||||
*/
|
||||
log_level_t (*get_level) (logger_t *this);
|
||||
|
||||
/**
|
||||
* @brief Destroys a logger_t object.
|
||||
*
|
||||
* @param this logger_t object
|
||||
*/
|
||||
void (*destroy) (logger_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Constructor to create a logger_t object.
|
||||
*
|
||||
* @param logger_name name for the logger_t object
|
||||
* @param log_level or'ed set of log_levels to assign to the new logger_t object
|
||||
* @param log_thread_id TRUE if thread id should also be logged
|
||||
* @param output FILE * if log has to go on a file output, NULL for syslog
|
||||
* @return logger_t object
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
logger_t *logger_create(char *logger_name, log_level_t log_level, bool log_thread_id, FILE * output);
|
||||
|
||||
|
||||
#endif /*LOGGER_H_*/
|
||||
@@ -0,0 +1,220 @@
|
||||
/**
|
||||
* @file logger_manager.c
|
||||
*
|
||||
* @brief Implementation of logger_manager_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 "logger_manager.h"
|
||||
|
||||
#include <daemon.h>
|
||||
#include <definitions.h>
|
||||
#include <utils/linked_list.h>
|
||||
|
||||
/**
|
||||
* String mappings for logger_context_t
|
||||
*/
|
||||
mapping_t logger_context_t_mappings[] = {
|
||||
{PARSER, "PARSER"},
|
||||
{GENERATOR, "GENERATOR"},
|
||||
{IKE_SA, "IKE_SA"},
|
||||
{IKE_SA_MANAGER, "IKE_SA_MANAGER"},
|
||||
{CHILD_SA, "CHILD_SA"},
|
||||
{MESSAGE, "MESSAGE"},
|
||||
{THREAD_POOL, "THREAD_POOL"},
|
||||
{WORKER, "WORKER"},
|
||||
{SCHEDULER, "SCHEDULER"},
|
||||
{SENDER, "SENDER"},
|
||||
{RECEIVER, "RECEIVER"},
|
||||
{SOCKET, "SOCKET"},
|
||||
{TESTER, "TESTER"},
|
||||
{DAEMON, "DAEMON"},
|
||||
{CONFIG, "CONFIG"},
|
||||
{ENCRYPTION_PAYLOAD, "ENCRYPTION_PAYLOAD"},
|
||||
{PAYLOAD, "PAYLOAD"},
|
||||
{DER_DECODER, "DER_DECODER"},
|
||||
{DER_ENCODER, "DER_ENCODER"},
|
||||
{ASN1, "ASN1"},
|
||||
{XFRM, "XFRM"},
|
||||
{LEAK_DETECT, "LEAK_DETECT"},
|
||||
{MAPPING_END, NULL},
|
||||
};
|
||||
|
||||
struct {
|
||||
char *name;
|
||||
log_level_t level;
|
||||
bool log_thread_ids;
|
||||
} logger_defaults[] = {
|
||||
{ "PARSR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* PARSER */
|
||||
{ "GNRAT", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* GENERATOR */
|
||||
{ "IKESA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* IKE_SA */
|
||||
{ "SAMGR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* IKE_SA_MANAGER */
|
||||
{ "CHDSA", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* CHILD_SA */
|
||||
{ "MESSG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* MESSAGE */
|
||||
{ "TPOOL", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* THREAD_POOL */
|
||||
{ "WORKR", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* WORKER */
|
||||
{ "SCHED", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* SCHEDULER */
|
||||
{ "SENDR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* SENDER */
|
||||
{ "RECVR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* RECEIVER */
|
||||
{ "SOCKT", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* SOCKET */
|
||||
{ "TESTR", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* TESTER */
|
||||
{ "DAEMN", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* DAEMON */
|
||||
{ "CONFG", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* CONFIG */
|
||||
{ "ENCPL", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* ENCRYPTION_PAYLOAD */
|
||||
{ "PAYLD", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* PAYLOAD */
|
||||
{ "DERDC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* DER_DECODER */
|
||||
{ "DEREC", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* DER_ENCODER */
|
||||
{ "ASN_1", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* ASN1 */
|
||||
{ "XFRM ", ERROR|CONTROL|AUDIT|LEVEL0, TRUE }, /* XFRM */
|
||||
{ "LEAKD", ERROR|CONTROL|AUDIT|LEVEL0, FALSE}, /* LEAK_DETECT */
|
||||
};
|
||||
|
||||
|
||||
typedef struct private_logger_manager_t private_logger_manager_t;
|
||||
|
||||
/**
|
||||
* Private data of logger_manager_t object.
|
||||
*/
|
||||
struct private_logger_manager_t {
|
||||
/**
|
||||
* Public data.
|
||||
*/
|
||||
logger_manager_t public;
|
||||
|
||||
/**
|
||||
* Array of loggers, one for each context
|
||||
*/
|
||||
logger_t *loggers[LOGGER_CONTEXT_ROOF];
|
||||
};
|
||||
|
||||
/**
|
||||
* The one and only instance of the logger manager
|
||||
*/
|
||||
static private_logger_manager_t private_logger_manager;
|
||||
|
||||
/**
|
||||
* Exported pointer for the logger manager
|
||||
*/
|
||||
logger_manager_t *logger_manager = (logger_manager_t *)&private_logger_manager;
|
||||
|
||||
/**
|
||||
* Implementation of logger_manager_t.get_logger.
|
||||
*/
|
||||
static logger_t *get_logger(private_logger_manager_t *this, logger_context_t context)
|
||||
{
|
||||
return this->loggers[context];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of logger_manager_t.get_log_level.
|
||||
*/
|
||||
static log_level_t get_log_level (private_logger_manager_t *this, logger_context_t context)
|
||||
{
|
||||
return this->loggers[context]->get_level(this->loggers[context]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of private_logger_manager_t.enable_log_level.
|
||||
*/
|
||||
static void enable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
|
||||
{
|
||||
if (context == ALL_LOGGERS)
|
||||
{
|
||||
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
|
||||
{
|
||||
this->loggers[context]->enable_level(this->loggers[context], level);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this->loggers[context]->enable_level(this->loggers[context], level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of private_logger_manager_t.disable_log_level.
|
||||
*/
|
||||
static void disable_log_level(private_logger_manager_t *this, logger_context_t context, log_level_t level)
|
||||
{
|
||||
if (context == ALL_LOGGERS)
|
||||
{
|
||||
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
|
||||
{
|
||||
this->loggers[context]->disable_level(this->loggers[context], level);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this->loggers[context]->disable_level(this->loggers[context], level);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of private_logger_manager_t.set_output.
|
||||
*/
|
||||
static void set_output(private_logger_manager_t *this, logger_context_t context, FILE *output)
|
||||
{
|
||||
if (context == ALL_LOGGERS)
|
||||
{
|
||||
for (context = 0; context < LOGGER_CONTEXT_ROOF; context++)
|
||||
{
|
||||
this->loggers[context]->set_output(this->loggers[context], output);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
this->loggers[context]->set_output(this->loggers[context], output);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates the instance of the logger manager at library startup
|
||||
*/
|
||||
void logger_manager_init()
|
||||
{
|
||||
int i;
|
||||
|
||||
logger_manager->get_logger = (logger_t *(*)(logger_manager_t*,logger_context_t context))get_logger;
|
||||
logger_manager->get_log_level = (log_level_t (*)(logger_manager_t *, logger_context_t)) get_log_level;
|
||||
logger_manager->enable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) enable_log_level;
|
||||
logger_manager->disable_log_level = (void (*)(logger_manager_t *, logger_context_t, log_level_t)) disable_log_level;
|
||||
logger_manager->set_output = (void (*)(logger_manager_t *, logger_context_t, FILE*)) set_output;
|
||||
|
||||
for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
|
||||
{
|
||||
private_logger_manager.loggers[i] = logger_create(logger_defaults[i].name,
|
||||
logger_defaults[i].level,
|
||||
logger_defaults[i].log_thread_ids,
|
||||
INITIAL_LOG_OUTPUT);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the logger manager at library exit
|
||||
*/
|
||||
void logger_manager_cleanup()
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < LOGGER_CONTEXT_ROOF; i++)
|
||||
{
|
||||
private_logger_manager.loggers[i]->destroy(private_logger_manager.loggers[i]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* @file logger_manager.h
|
||||
*
|
||||
* @brief Interface of logger_manager_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef LOGGER_MANAGER_H_
|
||||
#define LOGGER_MANAGER_H_
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <utils/logger.h>
|
||||
|
||||
#define INITIAL_LOG_OUTPUT stdout
|
||||
|
||||
typedef enum logger_context_t logger_context_t;
|
||||
|
||||
/**
|
||||
* @brief Context of a specific logger.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
enum logger_context_t {
|
||||
ALL_LOGGERS = -1,
|
||||
PARSER = 0,
|
||||
GENERATOR,
|
||||
IKE_SA,
|
||||
IKE_SA_MANAGER,
|
||||
CHILD_SA,
|
||||
MESSAGE,
|
||||
THREAD_POOL,
|
||||
WORKER,
|
||||
SCHEDULER,
|
||||
SENDER,
|
||||
RECEIVER,
|
||||
SOCKET,
|
||||
TESTER,
|
||||
DAEMON,
|
||||
CONFIG,
|
||||
ENCRYPTION_PAYLOAD,
|
||||
PAYLOAD,
|
||||
DER_DECODER,
|
||||
DER_ENCODER,
|
||||
ASN1,
|
||||
XFRM,
|
||||
LEAK_DETECT,
|
||||
LOGGER_CONTEXT_ROOF,
|
||||
};
|
||||
|
||||
|
||||
typedef struct logger_manager_t logger_manager_t;
|
||||
|
||||
/**
|
||||
* @brief Class to manage logger_t objects.
|
||||
*
|
||||
* The logger manager manages all logger_t object in a list and
|
||||
* allows their manipulation. Via a logger_context_t, the loglevel
|
||||
* of a specific logging type can be adjusted at runtime.
|
||||
* This class differs from others, as it has no constructor or destroy
|
||||
* function. The one and only instance "logger_manager" is created at
|
||||
* library start and destroyed at exit.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - none, logger_manager is the single instance
|
||||
* use logger_manager_init/logger_manager_cleanup
|
||||
*
|
||||
* @see logger_t
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct logger_manager_t {
|
||||
|
||||
/**
|
||||
* @brief Gets a logger_t object for a specific logger context.
|
||||
*
|
||||
* @param this logger_manager_t object
|
||||
* @param context logger_context to use the logger for
|
||||
* @param name name for the new logger. Context name is already included
|
||||
* and has not to be specified (so NULL is allowed)
|
||||
* @return logger_t object
|
||||
*/
|
||||
logger_t *(*get_logger) (logger_manager_t *this, logger_context_t context);
|
||||
|
||||
/**
|
||||
* @brief Returns the set log_level of a specific context.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param context context to check level
|
||||
* @return log_level for the given logger_context
|
||||
*/
|
||||
log_level_t (*get_log_level) (logger_manager_t *this, logger_context_t context);
|
||||
|
||||
/**
|
||||
* @brief Enables a logger level of a specific context.
|
||||
*
|
||||
* Use context ALL_LOGGERS to manipulate all loggers.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param context context to set level
|
||||
* @param log_level logger level to eanble
|
||||
*/
|
||||
void (*enable_log_level) (logger_manager_t *this, logger_context_t context,log_level_t log_level);
|
||||
|
||||
/**
|
||||
* @brief Disables a logger level of a specific context.
|
||||
*
|
||||
* Use context ALL_LOGGERS to manipulate all loggers.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param context context to set level
|
||||
* @param log_level logger level to disable
|
||||
*/
|
||||
void (*disable_log_level) (logger_manager_t *this, logger_context_t context,log_level_t log_level);
|
||||
|
||||
/**
|
||||
* @brief Sets the output of a logger.
|
||||
*
|
||||
* Use context ALL_LOGGERS to redirect all loggers.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param context context to set output
|
||||
* @param log_level logger level to disable
|
||||
*/
|
||||
void (*set_output) (logger_manager_t *this, logger_context_t context, FILE *output);
|
||||
};
|
||||
|
||||
/**
|
||||
* The single and global instance of the logger_manager
|
||||
*/
|
||||
extern logger_manager_t *logger_manager;
|
||||
|
||||
/**
|
||||
* Initialize the logger manager with all its logger.
|
||||
* Has to be called before logger_manager is accessed.
|
||||
*/
|
||||
void logger_manager_init();
|
||||
|
||||
/**
|
||||
* Free any resources hold by the logger manager. Do
|
||||
* not access logger_manager after this call.
|
||||
*/
|
||||
void logger_manager_cleanup();
|
||||
|
||||
#endif /*LOGGER_MANAGER_H_*/
|
||||
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* @file randomizer.c
|
||||
*
|
||||
* @brief Implementation of randomizer_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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 <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "randomizer.h"
|
||||
|
||||
|
||||
typedef struct private_randomizer_t private_randomizer_t;
|
||||
|
||||
/**
|
||||
* Private data of an randomizer_t object.
|
||||
*/
|
||||
struct private_randomizer_t {
|
||||
|
||||
/**
|
||||
* Public randomizer_t interface.
|
||||
*/
|
||||
randomizer_t public;
|
||||
|
||||
/**
|
||||
* @brief Reads a specific number of bytes from random or pseudo random device.
|
||||
*
|
||||
* @param this calling object
|
||||
* @param pseudo_random TRUE, if from pseudo random bytes should be read,
|
||||
* FALSE for true random bytes
|
||||
* @param bytes number of bytes to read
|
||||
* @param[out] buffer pointer to buffer where to write the data in.
|
||||
* Size of buffer has to be at least bytes.
|
||||
*/
|
||||
status_t (*get_bytes_from_device) (private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of private_randomizer_t.get_bytes_from_device.
|
||||
*/
|
||||
static status_t get_bytes_from_device(private_randomizer_t *this,bool pseudo_random, size_t bytes, u_int8_t *buffer)
|
||||
{
|
||||
size_t ndone;
|
||||
int device;
|
||||
size_t got;
|
||||
char * device_name;
|
||||
|
||||
device_name = pseudo_random ? PSEUDO_RANDOM_DEVICE : RANDOM_DEVICE;
|
||||
|
||||
device = open(device_name, 0);
|
||||
if (device < 0) {
|
||||
return FAILED;
|
||||
}
|
||||
ndone = 0;
|
||||
|
||||
/* read until nbytes are read */
|
||||
while (ndone < bytes)
|
||||
{
|
||||
got = read(device, buffer + ndone, bytes - ndone);
|
||||
if (got <= 0) {
|
||||
close(device);
|
||||
return FAILED;
|
||||
}
|
||||
ndone += got;
|
||||
}
|
||||
close(device);
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of randomizer_t.get_random_bytes.
|
||||
*/
|
||||
static status_t get_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
|
||||
{
|
||||
return this->get_bytes_from_device(this, FALSE, bytes, buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of randomizer_t.allocate_random_bytes.
|
||||
*/
|
||||
static status_t allocate_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
|
||||
{
|
||||
status_t status;
|
||||
chunk->len = bytes;
|
||||
chunk->ptr = malloc(bytes);
|
||||
status = this->get_bytes_from_device(this, FALSE, bytes, chunk->ptr);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
free(chunk->ptr);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of randomizer_t.get_pseudo_random_bytes.
|
||||
*/
|
||||
static status_t get_pseudo_random_bytes(private_randomizer_t *this,size_t bytes, u_int8_t *buffer)
|
||||
{
|
||||
return (this->get_bytes_from_device(this, TRUE, bytes, buffer));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of randomizer_t.allocate_pseudo_random_bytes.
|
||||
*/
|
||||
static status_t allocate_pseudo_random_bytes(private_randomizer_t *this, size_t bytes, chunk_t *chunk)
|
||||
{
|
||||
status_t status;
|
||||
chunk->len = bytes;
|
||||
chunk->ptr = malloc(bytes);
|
||||
status = this->get_bytes_from_device(this, TRUE, bytes, chunk->ptr);
|
||||
if (status != SUCCESS)
|
||||
{
|
||||
free(chunk->ptr);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of randomizer_t.destroy.
|
||||
*/
|
||||
static void destroy(private_randomizer_t *this)
|
||||
{
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
randomizer_t *randomizer_create(void)
|
||||
{
|
||||
private_randomizer_t *this = malloc_thing(private_randomizer_t);
|
||||
|
||||
/* public functions */
|
||||
this->public.get_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_random_bytes;
|
||||
this->public.allocate_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_random_bytes;
|
||||
this->public.get_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, u_int8_t *)) get_pseudo_random_bytes;
|
||||
this->public.allocate_pseudo_random_bytes = (status_t (*) (randomizer_t *,size_t, chunk_t *)) allocate_pseudo_random_bytes;
|
||||
this->public.destroy = (void (*) (randomizer_t *))destroy;
|
||||
|
||||
/* private functions */
|
||||
this->get_bytes_from_device = get_bytes_from_device;
|
||||
|
||||
return &(this->public);
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* @file randomizer.h
|
||||
*
|
||||
* @brief Interface of randomizer_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef RANDOMIZER_H_
|
||||
#define RANDOMIZER_H_
|
||||
|
||||
#include <types.h>
|
||||
|
||||
|
||||
/**
|
||||
* Device to read real random bytes
|
||||
*/
|
||||
#define RANDOM_DEVICE "/dev/random"
|
||||
|
||||
/**
|
||||
* Device to read pseudo random bytes
|
||||
*/
|
||||
#define PSEUDO_RANDOM_DEVICE "/dev/urandom"
|
||||
|
||||
typedef struct randomizer_t randomizer_t;
|
||||
|
||||
/**
|
||||
* @brief Class used to get random and pseudo random values.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - randomizer_create()
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct randomizer_t {
|
||||
|
||||
/**
|
||||
* @brief Reads a specific number of bytes from random device.
|
||||
*
|
||||
* @param this calling randomizer_t object
|
||||
* @param bytes number of bytes to read
|
||||
* @param[out] buffer pointer to buffer where to write the data in.
|
||||
* Size of buffer has to be at least bytes.
|
||||
* @return SUCCESS, or FAILED
|
||||
*/
|
||||
status_t (*get_random_bytes) (randomizer_t *this, size_t bytes, u_int8_t *buffer);
|
||||
|
||||
/**
|
||||
* @brief Allocates space and writes in random bytes.
|
||||
*
|
||||
* @param this calling randomizer_t object
|
||||
* @param bytes number of bytes to allocate
|
||||
* @param[out] chunk chunk which will hold the allocated random bytes
|
||||
* @return SUCCESS, or FAILED
|
||||
*/
|
||||
status_t (*allocate_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
|
||||
|
||||
/**
|
||||
* @brief Reads a specific number of bytes from pseudo random device.
|
||||
*
|
||||
* @param this calling randomizer_t object
|
||||
* @param bytes number of bytes to read
|
||||
* @param[out] buffer pointer to buffer where to write the data in.
|
||||
* size of buffer has to be at least bytes.
|
||||
* @return SUCCESS, or FAILED
|
||||
*/
|
||||
status_t (*get_pseudo_random_bytes) (randomizer_t *this,size_t bytes, u_int8_t *buffer);
|
||||
|
||||
/**
|
||||
* @brief Allocates space and writes in pseudo random bytes.
|
||||
*
|
||||
* @param this calling randomizer_t object
|
||||
* @param bytes number of bytes to allocate
|
||||
* @param[out] chunk chunk which will hold the allocated random bytes
|
||||
* @return SUCCESS, or FAILED
|
||||
*/
|
||||
status_t (*allocate_pseudo_random_bytes) (randomizer_t *this, size_t bytes, chunk_t *chunk);
|
||||
|
||||
/**
|
||||
* @brief Destroys a randomizer_t object.
|
||||
*
|
||||
* @param this randomizer_t object to destroy
|
||||
*/
|
||||
void (*destroy) (randomizer_t *this);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Creates a randomizer_t object.
|
||||
*
|
||||
* @return created randomizer_t, or
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
randomizer_t *randomizer_create();
|
||||
|
||||
#endif /*RANDOMIZER_H_*/
|
||||
@@ -0,0 +1,256 @@
|
||||
/**
|
||||
* @file tester.c
|
||||
*
|
||||
* @brief Implementation of tester_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "tester.h"
|
||||
|
||||
#include <utils/linked_list.h>
|
||||
#include <queues/job_queue.h>
|
||||
|
||||
|
||||
typedef struct private_tester_t private_tester_t;
|
||||
|
||||
/**
|
||||
* @brief Private Data of tester_t class.
|
||||
*
|
||||
*/
|
||||
struct private_tester_t {
|
||||
|
||||
/**
|
||||
* Protected interface of tester_t.
|
||||
*/
|
||||
protected_tester_t protected;
|
||||
|
||||
/**
|
||||
* Runs a specific test.
|
||||
*
|
||||
* @param tester associated tester object
|
||||
* @param test_function test function to perform
|
||||
* @param test_name name for the given test
|
||||
*/
|
||||
void (*run_test) (private_tester_t *tester, void (*test_function) (protected_tester_t * tester), char * test_name);
|
||||
|
||||
/**
|
||||
* Returns the difference of to timeval structs in microseconds.
|
||||
*
|
||||
* @warning this function is also defined in the event queue
|
||||
* in later improvements, this function can be added to a general
|
||||
* class type!
|
||||
*
|
||||
* @param end_time end time
|
||||
* @param start_time start time
|
||||
*
|
||||
* @TODO make object function or move to utils!
|
||||
*
|
||||
* @return difference in microseconds
|
||||
*/
|
||||
long (*time_difference) (private_tester_t *tester,struct timeval *end_time, struct timeval *start_time);
|
||||
|
||||
/**
|
||||
* Output is written into this file.
|
||||
*/
|
||||
FILE* output;
|
||||
|
||||
/**
|
||||
* Number of already performed tests.
|
||||
*/
|
||||
int tests_count;
|
||||
|
||||
/**
|
||||
* Number of failed tests.
|
||||
*/
|
||||
int failed_tests_count;
|
||||
|
||||
/**
|
||||
* Number of failed asserts in current test.
|
||||
*/
|
||||
int failed_asserts_count;
|
||||
|
||||
/**
|
||||
* TRUE if also succeeded asserts should be written to output.
|
||||
*/
|
||||
bool display_succeeded_asserts;
|
||||
|
||||
/**
|
||||
* Mutex to make this class thread-save.
|
||||
*/
|
||||
pthread_mutex_t mutex;
|
||||
};
|
||||
|
||||
/**
|
||||
* Implementation of tester_t.perform_tests.
|
||||
*/
|
||||
static void perform_tests(private_tester_t *this,test_t **tests)
|
||||
{
|
||||
int current_test = 0;
|
||||
fprintf(this->output,"\nStart testing...\n\n");
|
||||
fprintf(this->output,"_____________________________________________________________________\n");
|
||||
fprintf(this->output,"Testname | running time\n");
|
||||
fprintf(this->output,"_______________________________________________________|_____________\n");
|
||||
|
||||
while (tests[current_test] != NULL)
|
||||
{
|
||||
this->run_test(this,tests[current_test]->test_function,tests[current_test]->test_name);
|
||||
current_test++;
|
||||
}
|
||||
fprintf(this->output,"=====================================================================\n");
|
||||
fprintf(this->output,"End testing. %d of %d tests succeeded\n",this->tests_count - this->failed_tests_count,this->tests_count);
|
||||
fprintf(this->output,"=====================================================================\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of tester_t.perform_test.
|
||||
*/
|
||||
static void perform_test(private_tester_t *this, test_t *test)
|
||||
{
|
||||
test_t *tests[] = {test, NULL};
|
||||
return (perform_tests(this,tests));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the difference of to timeval structs in microseconds.
|
||||
*
|
||||
* @warning this function is also defined in the event queue
|
||||
* in later improvements, this function can be added to a general
|
||||
* class type!
|
||||
*
|
||||
* @param end_time end time
|
||||
* @param start_time start time
|
||||
*
|
||||
* @TODO make object function or move to utils!
|
||||
*
|
||||
* @return difference in microseconds
|
||||
*/
|
||||
static long time_difference(private_tester_t *this,struct timeval *end_time, struct timeval *start_time)
|
||||
{
|
||||
long seconds, microseconds;
|
||||
|
||||
seconds = (end_time->tv_sec - start_time->tv_sec);
|
||||
microseconds = (end_time->tv_usec - start_time->tv_usec);
|
||||
return ((seconds * 1000000) + microseconds);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of private_tester_t.run_test.
|
||||
*/
|
||||
static void run_test(private_tester_t *this, void (*test_function) (protected_tester_t * tester), char * test_name)
|
||||
{
|
||||
struct timeval start_time, end_time;
|
||||
long timediff;
|
||||
this->tests_count++;
|
||||
this->failed_asserts_count = 0;
|
||||
fprintf(this->output,"%-55s\n", test_name);
|
||||
gettimeofday(&start_time,NULL);
|
||||
test_function(&(this->protected));
|
||||
gettimeofday(&end_time,NULL);
|
||||
timediff = this->time_difference(this,&end_time, &start_time);
|
||||
|
||||
if (this->failed_asserts_count > 0)
|
||||
{
|
||||
fprintf(this->output," => Test failed: %-37s|%10ld us\n",test_name,timediff);
|
||||
}else
|
||||
{
|
||||
fprintf(this->output,"\033[1A\033[55C|%10ld us\033[1B\033[80D",timediff);
|
||||
}
|
||||
if (this->failed_asserts_count > 0)
|
||||
{
|
||||
this->failed_tests_count++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implementation of tester_t.assert_true.
|
||||
*/
|
||||
static void assert_true(private_tester_t *this, bool to_be_true,char * assert_name)
|
||||
{
|
||||
if (assert_name == NULL)
|
||||
{
|
||||
assert_name = "unknown";
|
||||
}
|
||||
|
||||
pthread_mutex_lock(&(this->mutex));
|
||||
if (!to_be_true)
|
||||
{
|
||||
this->failed_asserts_count++;
|
||||
fprintf(this->output," check '%s' failed!\n", assert_name);
|
||||
}else
|
||||
{
|
||||
if (this->display_succeeded_asserts)
|
||||
{
|
||||
fprintf(this->output," check '%s' succeeded\n", assert_name);
|
||||
}
|
||||
}
|
||||
pthread_mutex_unlock(&(this->mutex));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of tester_t.assert_false.
|
||||
*/
|
||||
static void assert_false(private_tester_t *this, bool to_be_false,char * assert_name)
|
||||
{
|
||||
this->protected.assert_true(&(this->protected),(!to_be_false),assert_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of tester_t.destroy.
|
||||
*/
|
||||
static void destroy(private_tester_t *tester)
|
||||
{
|
||||
private_tester_t *this = (private_tester_t*) tester;
|
||||
pthread_mutex_destroy(&(this->mutex));
|
||||
free(this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Described in header.
|
||||
*/
|
||||
tester_t *tester_create(FILE *output, bool display_succeeded_asserts)
|
||||
{
|
||||
private_tester_t *this = malloc_thing(private_tester_t);
|
||||
|
||||
/* public functions */
|
||||
this->protected.public.destroy = (void (*) (tester_t *))destroy;
|
||||
this->protected.public.perform_tests = (void (*) (tester_t *, test_t**)) perform_tests;
|
||||
this->protected.public.perform_test = (void (*) (tester_t *, test_t*))perform_test;
|
||||
this->protected.assert_true = (void (*) (protected_tester_t *, bool, char*)) assert_true;
|
||||
this->protected.assert_false = (void (*) (protected_tester_t *, bool, char*)) assert_false;
|
||||
|
||||
/* private functions */
|
||||
this->run_test = run_test;
|
||||
this->time_difference = time_difference;
|
||||
|
||||
/* private data */
|
||||
this->display_succeeded_asserts = display_succeeded_asserts;
|
||||
this->failed_tests_count = 0;
|
||||
this->tests_count = 0;
|
||||
this->output = output;
|
||||
pthread_mutex_init(&(this->mutex),NULL);
|
||||
|
||||
return &(this->protected.public);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
/**
|
||||
* @file tester.h
|
||||
*
|
||||
* @brief Interface of tester_t.
|
||||
*
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef TESTER_H_
|
||||
#define TESTER_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <types.h>
|
||||
|
||||
|
||||
/* must be defined here cause it is used in test_t */
|
||||
typedef struct protected_tester_t protected_tester_t;
|
||||
|
||||
typedef struct test_t test_t;
|
||||
|
||||
/**
|
||||
* @brief Representing a specified test.
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct test_t {
|
||||
/**
|
||||
* Testfunction called for this test.
|
||||
*
|
||||
* @param tester associated tester_t object
|
||||
*/
|
||||
void (*test_function) (protected_tester_t * tester);
|
||||
|
||||
/**
|
||||
* Name of the test.
|
||||
*/
|
||||
char * test_name;
|
||||
};
|
||||
|
||||
|
||||
typedef struct tester_t tester_t;
|
||||
|
||||
/**
|
||||
* @brief A class to perform tests.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - tester_create()
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct tester_t {
|
||||
/**
|
||||
* @brief Test all testcases in array tests with specific tester_t object.
|
||||
*
|
||||
* @param tester tester_t object
|
||||
* @param tests pointer to an array of test_t-pointers.
|
||||
* The last item has to be NULL to mark end of array.
|
||||
*/
|
||||
void (*perform_tests) (tester_t *tester,test_t **tests);
|
||||
|
||||
/**
|
||||
* @brief Run a specific test case.
|
||||
*
|
||||
* @param this tester_t object
|
||||
* @param test pointer to a test_t object which will be performed
|
||||
*/
|
||||
void (*perform_test) (tester_t *tester, test_t *test);
|
||||
|
||||
/**
|
||||
* @brief Destroys a tester_t object.
|
||||
*
|
||||
* @param tester tester_t object
|
||||
*/
|
||||
void (*destroy) (tester_t *tester);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief A class used in a specific testcase.
|
||||
*
|
||||
* For each testcase an object of this type is passed to the testfunction. The testfunction uses this
|
||||
* object to check specific asserts with protected_tester_t.assert_true and protected_tester_t.assert_false.
|
||||
*
|
||||
* @b Constructors:
|
||||
* - tester_create()
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
struct protected_tester_t {
|
||||
|
||||
/**
|
||||
* Public functions of a tester_t object
|
||||
*/
|
||||
tester_t public;
|
||||
|
||||
/**
|
||||
* @brief Is called in a testcase to check a specific situation for TRUE.
|
||||
*
|
||||
* Log-Values to the tester output are protected from multiple access.
|
||||
*
|
||||
* @param this tester_t object
|
||||
* @param to_be_true assert which has to be TRUE
|
||||
* @param assert_name name of the assertion
|
||||
*/
|
||||
void (*assert_true) (protected_tester_t *tester, bool to_be_true, char *assert_name);
|
||||
|
||||
/**
|
||||
* @brief Is called in a testcase to check a specific situation for FALSE.
|
||||
*
|
||||
* Log-Values to the tester output are protected from multiple access.
|
||||
*
|
||||
* @param this tester_t object
|
||||
* @param to_be_false assert which has to be FALSE
|
||||
* @param assert_name name of the assertion
|
||||
*/
|
||||
void (*assert_false) (protected_tester_t *tester, bool to_be_false, char *assert_name);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Creates a tester_t object used to perform tests with.
|
||||
*
|
||||
* @param output test output is written to this output.
|
||||
* @param display_succeeded_asserts has to be TRUE, if all asserts should be displayed,
|
||||
* FALSE otherwise
|
||||
*
|
||||
* @return tester_t object
|
||||
*
|
||||
* @ingroup utils
|
||||
*/
|
||||
tester_t *tester_create(FILE *output, bool display_succeeded_asserts);
|
||||
|
||||
#endif /*TESTER_H_*/
|
||||
Reference in New Issue
Block a user