diff options
author | Martin Willi <martin@strongswan.org> | 2006-04-28 07:14:48 +0000 |
---|---|---|
committer | Martin Willi <martin@strongswan.org> | 2006-04-28 07:14:48 +0000 |
commit | 997358a6c475c8886cce388ab325184a1ff733c9 (patch) | |
tree | 27a15790e030fc186d00cd710d2a3540f4defe69 /Source/lib/utils | |
parent | 52923c9acb349adec3d1cc039e7a74c2e822da6e (diff) | |
download | strongswan-997358a6c475c8886cce388ab325184a1ff733c9.tar.bz2 strongswan-997358a6c475c8886cce388ab325184a1ff733c9.tar.xz |
- import of strongswan-2.7.0
- applied patch for charon
Diffstat (limited to 'Source/lib/utils')
-rw-r--r-- | Source/lib/utils/Makefile.utils | 47 | ||||
-rw-r--r-- | Source/lib/utils/host.c | 365 | ||||
-rw-r--r-- | Source/lib/utils/host.h | 225 | ||||
-rw-r--r-- | Source/lib/utils/identification.c | 1167 | ||||
-rw-r--r-- | Source/lib/utils/identification.h | 245 | ||||
-rw-r--r-- | Source/lib/utils/iterator.h | 153 | ||||
-rw-r--r-- | Source/lib/utils/leak_detective.c | 540 | ||||
-rw-r--r-- | Source/lib/utils/leak_detective.h | 50 | ||||
-rw-r--r-- | Source/lib/utils/linked_list.c | 727 | ||||
-rw-r--r-- | Source/lib/utils/linked_list.h | 203 | ||||
-rw-r--r-- | Source/lib/utils/logger.c | 342 | ||||
-rw-r--r-- | Source/lib/utils/logger.h | 198 | ||||
-rw-r--r-- | Source/lib/utils/logger_manager.c | 220 | ||||
-rw-r--r-- | Source/lib/utils/logger_manager.h | 160 | ||||
-rw-r--r-- | Source/lib/utils/randomizer.c | 164 | ||||
-rw-r--r-- | Source/lib/utils/randomizer.h | 110 | ||||
-rw-r--r-- | Source/lib/utils/tester.c | 256 | ||||
-rw-r--r-- | Source/lib/utils/tester.h | 148 |
18 files changed, 0 insertions, 5320 deletions
diff --git a/Source/lib/utils/Makefile.utils b/Source/lib/utils/Makefile.utils deleted file mode 100644 index 1c82283d7..000000000 --- a/Source/lib/utils/Makefile.utils +++ /dev/null @@ -1,47 +0,0 @@ -# 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 $@ $<
\ No newline at end of file diff --git a/Source/lib/utils/host.c b/Source/lib/utils/host.c deleted file mode 100644 index 020ed27f3..000000000 --- a/Source/lib/utils/host.c +++ /dev/null @@ -1,365 +0,0 @@ -/** - * @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; - } -} - diff --git a/Source/lib/utils/host.h b/Source/lib/utils/host.h deleted file mode 100644 index d81efffa6..000000000 --- a/Source/lib/utils/host.h +++ /dev/null @@ -1,225 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/identification.c b/Source/lib/utils/identification.c deleted file mode 100644 index 33f3d92cd..000000000 --- a/Source/lib/utils/identification.c +++ /dev/null @@ -1,1167 +0,0 @@ -/** - * @file identification.c - * - * @brief Implementation 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. - */ - -#define _GNU_SOURCE -#include <sys/socket.h> -#include <netinet/in.h> -#include <arpa/inet.h> -#include <string.h> -#include <stdio.h> -#include <ctype.h> - -#include "identification.h" - -#include <asn1/asn1.h> - -/** - * String mappings for id_type_t. - */ -mapping_t id_type_m[] = { - {ID_IPV4_ADDR, "ID_IPV4_ADDR"}, - {ID_FQDN, "ID_FQDN"}, - {ID_RFC822_ADDR, "ID_RFC822_ADDR"}, - {ID_IPV6_ADDR, "ID_IPV6_ADDR"}, - {ID_DER_ASN1_DN, "ID_DER_ASN1_DN"}, - {ID_DER_ASN1_GN, "ID_DER_ASN1_GN"}, - {ID_KEY_ID, "ID_KEY_ID"}, - {ID_ANY, "ID_ANY"}, - {MAPPING_END, NULL} -}; - - -/** - * X.501 acronyms for well known object identifiers (OIDs) - */ -static u_char oid_ND[] = { - 0x02, 0x82, 0x06, 0x01, - 0x0A, 0x07, 0x14 -}; -static u_char oid_UID[] = { - 0x09, 0x92, 0x26, 0x89, 0x93, - 0xF2, 0x2C, 0x64, 0x01, 0x01 -}; -static u_char oid_DC[] = { - 0x09, 0x92, 0x26, 0x89, 0x93, - 0xF2, 0x2C, 0x64, 0x01, 0x19 -}; -static u_char oid_CN[] = { - 0x55, 0x04, 0x03 -}; -static u_char oid_S[] = { - 0x55, 0x04, 0x04 -}; -static u_char oid_SN[] = { - 0x55, 0x04, 0x05 -}; -static u_char oid_C[] = { - 0x55, 0x04, 0x06 -}; -static u_char oid_L[] = { - 0x55, 0x04, 0x07 -}; -static u_char oid_ST[] = { - 0x55, 0x04, 0x08 -}; -static u_char oid_O[] = { - 0x55, 0x04, 0x0A -}; -static u_char oid_OU[] = { - 0x55, 0x04, 0x0B -}; -static u_char oid_T[] = { - 0x55, 0x04, 0x0C -}; -static u_char oid_D[] = { - 0x55, 0x04, 0x0D -}; -static u_char oid_N[] = { - 0x55, 0x04, 0x29 -}; -static u_char oid_G[] = { - 0x55, 0x04, 0x2A -}; -static u_char oid_I[] = { - 0x55, 0x04, 0x2B -}; -static u_char oid_ID[] = { - 0x55, 0x04, 0x2D -}; -static u_char oid_EN[] = { - 0x60, 0x86, 0x48, 0x01, 0x86, - 0xF8, 0x42, 0x03, 0x01, 0x03 -}; -static u_char oid_E[] = { - 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x09, 0x01 -}; -static u_char oid_UN[] = { - 0x2A, 0x86, 0x48, 0x86, 0xF7, - 0x0D, 0x01, 0x09, 0x02 -}; -static u_char oid_TCGID[] = { - 0x2B, 0x06, 0x01, 0x04, 0x01, 0x89, - 0x31, 0x01, 0x01, 0x02, 0x02, 0x4B -}; - -/** - * coding of X.501 distinguished name - */ -typedef struct { - const u_char *name; - chunk_t oid; - u_char type; -} x501rdn_t; - -static const x501rdn_t x501rdns[] = { - {"ND", {oid_ND, 7}, ASN1_PRINTABLESTRING}, - {"UID", {oid_UID, 10}, ASN1_PRINTABLESTRING}, - {"DC", {oid_DC, 10}, ASN1_PRINTABLESTRING}, - {"CN", {oid_CN, 3}, ASN1_PRINTABLESTRING}, - {"S", {oid_S, 3}, ASN1_PRINTABLESTRING}, - {"SN", {oid_SN, 3}, ASN1_PRINTABLESTRING}, - {"serialNumber", {oid_SN, 3}, ASN1_PRINTABLESTRING}, - {"C", {oid_C, 3}, ASN1_PRINTABLESTRING}, - {"L", {oid_L, 3}, ASN1_PRINTABLESTRING}, - {"ST", {oid_ST, 3}, ASN1_PRINTABLESTRING}, - {"O", {oid_O, 3}, ASN1_PRINTABLESTRING}, - {"OU", {oid_OU, 3}, ASN1_PRINTABLESTRING}, - {"T", {oid_T, 3}, ASN1_PRINTABLESTRING}, - {"D", {oid_D, 3}, ASN1_PRINTABLESTRING}, - {"N", {oid_N, 3}, ASN1_PRINTABLESTRING}, - {"G", {oid_G, 3}, ASN1_PRINTABLESTRING}, - {"I", {oid_I, 3}, ASN1_PRINTABLESTRING}, - {"ID", {oid_ID, 3}, ASN1_PRINTABLESTRING}, - {"EN", {oid_EN, 10}, ASN1_PRINTABLESTRING}, - {"employeeNumber", {oid_EN, 10}, ASN1_PRINTABLESTRING}, - {"E", {oid_E, 9}, ASN1_IA5STRING}, - {"Email", {oid_E, 9}, ASN1_IA5STRING}, - {"emailAddress", {oid_E, 9}, ASN1_IA5STRING}, - {"UN", {oid_UN, 9}, ASN1_IA5STRING}, - {"unstructuredName",{oid_UN, 9}, ASN1_IA5STRING}, - {"TCGID", {oid_TCGID, 12}, ASN1_PRINTABLESTRING} -}; -#define X501_RDN_ROOF 26 - -/** - * Different kinds of generalNames - */ -enum generalNames_t { - GN_OTHER_NAME = 0, - GN_RFC822_NAME = 1, - GN_DNS_NAME = 2, - GN_X400_ADDRESS = 3, - GN_DIRECTORY_NAME = 4, - GN_EDI_PARTY_NAME = 5, - GN_URI = 6, - GN_IP_ADDRESS = 7, - GN_REGISTERED_ID = 8, -}; - -/** - * ASN.1 definition of generalName - */ -static const asn1Object_t generalNameObjects[] = { - { 0, "otherName", ASN1_CONTEXT_C_0, ASN1_OPT|ASN1_BODY }, /* 0 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 1 */ - { 0, "rfc822Name", ASN1_CONTEXT_S_1, ASN1_OPT|ASN1_BODY }, /* 2 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 3 */ - { 0, "dnsName", ASN1_CONTEXT_S_2, ASN1_OPT|ASN1_BODY }, /* 4 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 5 */ - { 0, "x400Address", ASN1_CONTEXT_S_3, ASN1_OPT|ASN1_BODY }, /* 6 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 7 */ - { 0, "directoryName", ASN1_CONTEXT_C_4, ASN1_OPT|ASN1_BODY }, /* 8 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 9 */ - { 0, "ediPartyName", ASN1_CONTEXT_C_5, ASN1_OPT|ASN1_BODY }, /* 10 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 11 */ - { 0, "URI", ASN1_CONTEXT_S_6, ASN1_OPT|ASN1_BODY }, /* 12 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 13 */ - { 0, "ipAddress", ASN1_CONTEXT_S_7, ASN1_OPT|ASN1_BODY }, /* 14 */ - { 0, "end choice", ASN1_EOC, ASN1_END }, /* 15 */ - { 0, "registeredID", ASN1_CONTEXT_S_8, ASN1_OPT|ASN1_BODY }, /* 16 */ - { 0, "end choice", ASN1_EOC, ASN1_END } /* 17 */ -}; -#define GN_OBJ_OTHER_NAME 0 -#define GN_OBJ_RFC822_NAME 2 -#define GN_OBJ_DNS_NAME 4 -#define GN_OBJ_X400_ADDRESS 6 -#define GN_OBJ_DIRECTORY_NAME 8 -#define GN_OBJ_EDI_PARTY_NAME 10 -#define GN_OBJ_URI 12 -#define GN_OBJ_IP_ADDRESS 14 -#define GN_OBJ_REGISTERED_ID 16 -#define GN_OBJ_ROOF 18 - -/** - * ASN.1 definition of otherName - */ -static const asn1Object_t otherNameObjects[] = { - {0, "type-id", ASN1_OID, ASN1_BODY }, /* 0 */ - {0, "value", ASN1_CONTEXT_C_0, ASN1_BODY } /* 1 */ -}; -#define ON_OBJ_ID_TYPE 0 -#define ON_OBJ_VALUE 1 -#define ON_OBJ_ROOF 2 - -typedef struct private_identification_t private_identification_t; - -/** - * Private data of an identification_t object. - */ -struct private_identification_t { - /** - * Public interface. - */ - identification_t public; - - /** - * String representation of this ID. - */ - char *string; - - /** - * Encoded representation of this ID. - */ - chunk_t encoded; - - /** - * Type of this ID. - */ - id_type_t type; -}; - -static private_identification_t *identification_create(); - - -/** - * updates a chunk (!????) - * TODO: We should reconsider this stuff, its not really clear - */ -static void update_chunk(chunk_t *ch, int n) -{ - n = (n > -1 && n < (int)ch->len)? n : (int)ch->len-1; - ch->ptr += n; ch->len -= n; -} - -/** - * Prints a binary string in hexadecimal form - */ -void hex_str(chunk_t bin, chunk_t *str) -{ - u_int i; - update_chunk(str, snprintf(str->ptr,str->len,"0x")); - for (i=0; i < bin.len; i++) - { - update_chunk(str, snprintf(str->ptr,str->len,"%02X",*bin.ptr++)); - } -} - -/** - * Pointer is set to the first RDN in a DN - */ -static status_t init_rdn(chunk_t dn, chunk_t *rdn, chunk_t *attribute, bool *next) -{ - *rdn = CHUNK_INITIALIZER; - *attribute = CHUNK_INITIALIZER; - - /* a DN is a SEQUENCE OF RDNs */ - if (*dn.ptr != ASN1_SEQUENCE) - { - /* DN is not a SEQUENCE */ - return FAILED; - } - - rdn->len = asn1_length(&dn); - - if (rdn->len == ASN1_INVALID_LENGTH) - { - /* Invalid RDN length */ - return FAILED; - } - - rdn->ptr = dn.ptr; - - /* are there any RDNs ? */ - *next = rdn->len > 0; - - return SUCCESS; -} - -/** - * Fetches the next RDN in a DN - */ -static status_t get_next_rdn(chunk_t *rdn, chunk_t * attribute, chunk_t *oid, chunk_t *value, asn1_t *type, bool *next) -{ - chunk_t body; - - /* initialize return values */ - *oid = CHUNK_INITIALIZER; - *value = CHUNK_INITIALIZER; - - /* if all attributes have been parsed, get next rdn */ - if (attribute->len <= 0) - { - /* an RDN is a SET OF attributeTypeAndValue */ - if (*rdn->ptr != ASN1_SET) - { - /* RDN is not a SET */ - return FAILED; - } - attribute->len = asn1_length(rdn); - if (attribute->len == ASN1_INVALID_LENGTH) - { - /* Invalid attribute length */ - return FAILED; - } - attribute->ptr = rdn->ptr; - /* advance to start of next RDN */ - rdn->ptr += attribute->len; - rdn->len -= attribute->len; - } - - /* an attributeTypeAndValue is a SEQUENCE */ - if (*attribute->ptr != ASN1_SEQUENCE) - { - /* attributeTypeAndValue is not a SEQUENCE */ - return FAILED; - } - - /* extract the attribute body */ - body.len = asn1_length(attribute); - - if (body.len == ASN1_INVALID_LENGTH) - { - /* Invalid attribute body length */ - return FAILED; - } - - body.ptr = attribute->ptr; - - /* advance to start of next attribute */ - attribute->ptr += body.len; - attribute->len -= body.len; - - /* attribute type is an OID */ - if (*body.ptr != ASN1_OID) - { - /* attributeType is not an OID */ - return FAILED; - } - /* extract OID */ - oid->len = asn1_length(&body); - - if (oid->len == ASN1_INVALID_LENGTH) - { - /* Invalid attribute OID length */ - return FAILED; - } - oid->ptr = body.ptr; - - /* advance to the attribute value */ - body.ptr += oid->len; - body.len -= oid->len; - - /* extract string type */ - *type = *body.ptr; - - /* extract string value */ - value->len = asn1_length(&body); - - if (value->len == ASN1_INVALID_LENGTH) - { - /* Invalid attribute string length */ - return FAILED; - } - value->ptr = body.ptr; - - /* are there any RDNs left? */ - *next = rdn->len > 0 || attribute->len > 0; - return SUCCESS; -} - -/** - * Parses an ASN.1 distinguished name int its OID/value pairs - */ -static status_t dntoa(chunk_t dn, chunk_t *str) -{ - chunk_t rdn, oid, attribute, value; - asn1_t type; - int oid_code; - bool next; - bool first = TRUE; - - status_t status = init_rdn(dn, &rdn, &attribute, &next); - - if (status != SUCCESS) - {/* a parsing error has occured */ - return status; - } - - while (next) - { - status = get_next_rdn(&rdn, &attribute, &oid, &value, &type, &next); - - if (status != SUCCESS) - {/* a parsing error has occured */ - return status; - } - - if (first) - { /* first OID/value pair */ - first = FALSE; - } - else - { /* separate OID/value pair by a comma */ - update_chunk(str, snprintf(str->ptr,str->len,", ")); - } - - /* print OID */ - oid_code = known_oid(oid); - if (oid_code == OID_UNKNOWN) - { /* OID not found in list */ - hex_str(oid, str); - } - else - { - update_chunk(str, snprintf(str->ptr,str->len,"%s", oid_names[oid_code].name)); - } - /* print value */ - update_chunk(str, snprintf(str->ptr,str->len,"=%.*s", (int)value.len,value.ptr)); - } - return SUCCESS; -} - -/** - * compare two distinguished names by - * comparing the individual RDNs - */ -static bool same_dn(chunk_t a, chunk_t b) -{ - chunk_t rdn_a, rdn_b, attribute_a, attribute_b; - chunk_t oid_a, oid_b, value_a, value_b; - asn1_t type_a, type_b; - bool next_a, next_b; - - /* same lengths for the DNs */ - if (a.len != b.len) - { - return FALSE; - } - /* try a binary comparison first */ - if (memcmp(a.ptr, b.ptr, b.len) == 0) - { - return TRUE; - } - - /* initialize DN parsing */ - if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != SUCCESS || - init_rdn(b, &rdn_b, &attribute_b, &next_b) != SUCCESS) - { - return FALSE; - } - - /* fetch next RDN pair */ - while (next_a && next_b) - { - /* parse next RDNs and check for errors */ - if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != SUCCESS || - get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != SUCCESS) - { - return FALSE; - } - /* OIDs must agree */ - if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0) - { - return FALSE; - } - /* same lengths for values */ - if (value_a.len != value_b.len) - { - return FALSE; - } - /* printableStrings and email RDNs require uppercase comparison */ - if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING || - (type_a == ASN1_IA5STRING && known_oid(oid_a) == OID_PKCS9_EMAIL))) - { - if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0) - { - return FALSE; - } - } - else - { - if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0) - { - return FALSE; - } - } - } - /* both DNs must have same number of RDNs */ - if (next_a || next_b) - return FALSE; - - /* the two DNs are equal! */ - return TRUE; -} - - -/** - * compare two distinguished names by comparing the individual RDNs. - * A single'*' character designates a wildcard RDN in DN b. - * TODO: Add support for different RDN order in DN !! - */ -bool match_dn(chunk_t a, chunk_t b, int *wildcards) -{ - chunk_t rdn_a, rdn_b, attribute_a, attribute_b; - chunk_t oid_a, oid_b, value_a, value_b; - asn1_t type_a, type_b; - bool next_a, next_b; - - /* initialize wildcard counter */ - *wildcards = 0; - - /* initialize DN parsing */ - if (init_rdn(a, &rdn_a, &attribute_a, &next_a) != SUCCESS || - init_rdn(b, &rdn_b, &attribute_b, &next_b) != SUCCESS) - { - return FALSE; - } - /* fetch next RDN pair */ - while (next_a && next_b) - { - /* parse next RDNs and check for errors */ - if (get_next_rdn(&rdn_a, &attribute_a, &oid_a, &value_a, &type_a, &next_a) != SUCCESS || - get_next_rdn(&rdn_b, &attribute_b, &oid_b, &value_b, &type_b, &next_b) != SUCCESS) - { - return FALSE; - } - /* OIDs must agree */ - if (oid_a.len != oid_b.len || memcmp(oid_a.ptr, oid_b.ptr, oid_b.len) != 0) - { - return FALSE; - } - /* does rdn_b contain a wildcard? */ - if (value_b.len == 1 && *value_b.ptr == '*') - { - (*wildcards)++; - continue; - } - /* same lengths for values */ - if (value_a.len != value_b.len) - { - return FALSE; - } - /* printableStrings and email RDNs require uppercase comparison */ - if (type_a == type_b && (type_a == ASN1_PRINTABLESTRING || - (type_a == ASN1_IA5STRING && known_oid(oid_a) == OID_PKCS9_EMAIL))) - { - if (strncasecmp(value_a.ptr, value_b.ptr, value_b.len) != 0) - { - return FALSE; - } - } - else - { - if (strncmp(value_a.ptr, value_b.ptr, value_b.len) != 0) - { - return FALSE; - } - } - } - /* both DNs must have same number of RDNs */ - if (next_a || next_b) - { - return FALSE; - } - /* the two DNs match! */ - return TRUE; -} - -/** - * get string representation of a general name - * TODO: Add support for gn types - */ -static char *gntoa(chunk_t blob) -{ - asn1_ctx_t ctx; - chunk_t object; - int objectID = 0; - u_int level; - char buf[128]; - - asn1_init(&ctx, blob, 0, FALSE); - - while (objectID < GN_OBJ_ROOF) - { - if (!extract_object(generalNameObjects, &objectID, &object, &level, &ctx)) - { - return NULL; - } - switch (objectID) - { - case GN_OBJ_RFC822_NAME: - case GN_OBJ_DNS_NAME: - case GN_OBJ_URI: - snprintf(buf, sizeof(buf), "%.*s", object.len, object.ptr); - return strdup(buf); - case GN_OBJ_IP_ADDRESS: - if (object.len == 4 && - inet_ntop(AF_INET, object.ptr, buf, sizeof(buf))) - { - return strdup(buf); - } - return NULL; - break; - case GN_OBJ_OTHER_NAME: - return strdup("(other name)"); - case GN_OBJ_X400_ADDRESS: - return strdup("(X400 Address)"); - case GN_OBJ_EDI_PARTY_NAME: - return strdup("(EDI party name)"); - case GN_OBJ_REGISTERED_ID: - return strdup("(registered ID)"); - case GN_OBJ_DIRECTORY_NAME: - return strdup("(directory name)"); - default: - break; - } - objectID++; - } - return NULL; -} - -/** - * Converts an LDAP-style human-readable ASCII-encoded - * ASN.1 distinguished name into binary DER-encoded format - */ -static status_t atodn(char *src, chunk_t *dn) -{ - /* finite state machine for atodn */ - typedef enum { - SEARCH_OID = 0, - READ_OID = 1, - SEARCH_NAME = 2, - READ_NAME = 3, - UNKNOWN_OID = 4 - } state_t; - - char *wrap_mode; - chunk_t oid = CHUNK_INITIALIZER; - chunk_t name = CHUNK_INITIALIZER; - chunk_t names[25]; /* max to 25 rdns */ - int name_count = 0; - int whitespace = 0; - int pos = 0; - asn1_t rdn_type; - state_t state = SEARCH_OID; - status_t status = SUCCESS; - - do - { - switch (state) - { - case SEARCH_OID: - if (*src != ' ' && *src != '/' && *src != ',') - { - oid.ptr = src; - oid.len = 1; - state = READ_OID; - } - break; - case READ_OID: - if (*src != ' ' && *src != '=') - { - oid.len++; - } - else - { - for (pos = 0; pos < X501_RDN_ROOF; pos++) - { - if (strlen(x501rdns[pos].name) == oid.len && - strncasecmp(x501rdns[pos].name, oid.ptr, oid.len) == 0) - { - break; /* found a valid OID */ - } - } - if (pos == X501_RDN_ROOF) - { - status = NOT_SUPPORTED; - state = UNKNOWN_OID; - break; - } - /* reset oid and change state */ - oid = CHUNK_INITIALIZER; - state = SEARCH_NAME; - } - break; - case SEARCH_NAME: - if (*src != ' ' && *src != '=') - { - name.ptr = src; - name.len = 1; - whitespace = 0; - state = READ_NAME; - } - break; - case READ_NAME: - if (*src != ',' && *src != '/' && *src != '\0') - { - name.len++; - if (*src == ' ') - { - whitespace++; - } - else - { - whitespace = 0; - } - } - else - { - name.len -= whitespace; - rdn_type = (x501rdns[pos].type == ASN1_PRINTABLESTRING - && !is_printablestring(name))? ASN1_T61STRING : x501rdns[pos].type; - - if (name_count < 25) - { - names[name_count++] = - asn1_wrap(ASN1_SET, "m", - asn1_wrap(ASN1_SEQUENCE, "mm", - asn1_wrap(ASN1_OID, "c", x501rdns[pos].oid), - asn1_wrap(rdn_type, "c", name) - ) - ); - } - else - { - status = OUT_OF_RES; - } - /* reset name and change state */ - name = CHUNK_INITIALIZER; - state = SEARCH_OID; - } - break; - case UNKNOWN_OID: - break; - } - } while (*src++ != '\0'); - - - /* build the distinguished name sequence */ - wrap_mode = alloca(26); - memset(wrap_mode, 0, 26); - memset(wrap_mode, 'm', name_count); - *dn = asn1_wrap(ASN1_SEQUENCE, wrap_mode, - names[0], names[1], names[2], names[3], names[4], - names[5], names[6], names[7], names[8], names[9], - names[10], names[11], names[12], names[13], names[14], - names[15], names[16], names[17], names[18], names[19], - names[20], names[21], names[22], names[23], names[24]); - if (status != SUCCESS) - { - free(dn->ptr); - *dn = CHUNK_INITIALIZER; - } - return status; -} - -/** - * Implementation of identification_t.get_encoding. - */ -static chunk_t get_encoding(private_identification_t *this) -{ - return this->encoded; -} - -/** - * Implementation of identification_t.get_type. - */ -static id_type_t get_type(private_identification_t *this) -{ - return this->type; -} - -/** - * Implementation of identification_t.get_string. - */ -static char *get_string(private_identification_t *this) -{ - return this->string; -} - -/** - * Implementation of identification_t.contains_wildcards. - */ -static bool contains_wildcards(private_identification_t *this) -{ - if (this->type == ID_ANY || - memchr(this->encoded.ptr, '*', this->encoded.len) != NULL) - { - return TRUE; - } - return FALSE; -} - -/** - * Default implementation of identification_t.equals and identification_t.belongs_to. - * compares encoded chunk for equality. - */ -static bool equals_binary(private_identification_t *this,private_identification_t *other) -{ - if (this->type == other->type) - { - if (this->encoded.len == other->encoded.len && - memcmp(this->encoded.ptr, other->encoded.ptr, this->encoded.len) == 0) - { - return TRUE; - } - } - return FALSE; -} - -/** - * Special implementation of identification_t.equals for ID_DER_ASN1_DN - */ -static bool equals_dn(private_identification_t *this, private_identification_t *other) -{ - return same_dn(this->encoded, other->encoded); -} - -/** - * Special implementation of identification_t.belongs_to for ID_RFC822_ADDR/ID_FQDN. - * checks for a wildcard in other-string, and compares it against this-string. - */ -static bool belongs_to_wc_string(private_identification_t *this, private_identification_t *other) -{ - char *this_str, *other_str, *pos; - - if (other->type == ID_ANY) - { - return TRUE; - } - - if (this->type == other->type) - { - /* try a binary comparison first */ - if (equals_binary(this, other)) - { - return TRUE; - } - } - if (other->encoded.len > 0 && - *(other->encoded.ptr) == '*') - { - if (other->encoded.len == 1) - { - /* other contains just a wildcard, and therefore matches anything */ - return TRUE; - } - /* We strdup chunks, since they are NOT null-terminated */ - this_str = strndupa(this->encoded.ptr, this->encoded.len); - other_str = strndupa(other->encoded.ptr + 1, other->encoded.len - 1); - pos = strstr(this_str, other_str); - if (pos != NULL) - { - /* ok, other is contained in this, but there may be more characters, so check it */ - if (strlen(pos) == strlen(other_str)) - { - return TRUE; - } - } - } - - return FALSE; -} - -/** - * Special implementation of identification_t.belongs_to for ID_ANY. - * ANY matches only another ANY, but nothing other - */ -static bool belongs_to_any(private_identification_t *this, private_identification_t *other) -{ - if (other->type == ID_ANY) - { - return TRUE; - } - return FALSE; -} - -/** - * Special implementation of identification_t.belongs_to for ID_DER_ASN1_DN. - * ANY matches any, even ANY, thats why its there... - */ -static bool belongs_to_dn(private_identification_t *this, private_identification_t *other) -{ - int wildcards; - - if (other->type == ID_ANY) - { - return TRUE; - } - - if (this->type == other->type) - { - return match_dn(this->encoded, other->encoded, &wildcards); - } - return FALSE; -} - -/** - * Implementation of identification_t.clone. - */ -static identification_t *clone(private_identification_t *this) -{ - private_identification_t *clone = identification_create(); - - clone->type = this->type; - clone->encoded = chunk_clone(this->encoded); - clone->string = malloc(strlen(this->string) + 1); - strcpy(clone->string, this->string); - - return &clone->public; -} - -/** - * Implementation of identification_t.destroy. - */ -static void destroy(private_identification_t *this) -{ - free(this->string); - free(this->encoded.ptr); - free(this); -} - -/** - * Generic constructor used for the other constructors. - */ -static private_identification_t *identification_create() -{ - private_identification_t *this = malloc_thing(private_identification_t); - - this->public.get_encoding = (chunk_t (*) (identification_t*))get_encoding; - this->public.get_type = (id_type_t (*) (identification_t*))get_type; - this->public.get_string = (char* (*) (identification_t*))get_string; - this->public.contains_wildcards = (bool (*) (identification_t *this))contains_wildcards; - this->public.clone = (identification_t* (*) (identification_t*))clone; - this->public.destroy = (void (*) (identification_t*))destroy; - /* we use these as defaults, the may be overloaded for special ID types */ - this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary; - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))equals_binary; - - this->string = NULL; - this->encoded = CHUNK_INITIALIZER; - - return this; -} - -/* - * Described in header. - */ -identification_t *identification_create_from_string(char *string) -{ - private_identification_t *this = identification_create(); - - if (strchr(string, '=') != NULL) - { - /* we interpret this as an ASCII X.501 ID_DER_ASN1_DN. - * convert from LDAP style or openssl x509 -subject style to ASN.1 DN - * discard optional @ character in front of DN - */ - if (atodn((*string == '@') ? string + 1 : string, &this->encoded) != SUCCESS) - { - free(this); - return NULL; - } - this->string = strdup(string); - this->type = ID_DER_ASN1_DN; - this->public.equals = (bool (*) (identification_t*,identification_t*))equals_dn; - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_dn; - return &this->public; - } - else if (strchr(string, '@') == NULL) - { - if (strcmp(string, "%any") == 0 || - strcmp(string, "0.0.0.0") == 0 || - strcmp(string, "*") == 0 || - strcmp(string, "::") == 0|| - strcmp(string, "0::0") == 0) - { - /* any ID will be accepted */ - this->type = ID_ANY; - this->string = strdup("%any"); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_any; - return &this->public; - } - else - { - /* TODO: Pluto resolve domainnames without '@' to IPv4/6 address. Is this really needed? */ - - if (strchr(string, ':') == NULL) - { - /* try IPv4 */ - struct in_addr address; - chunk_t chunk = {(void*)&address, sizeof(address)}; - - if (inet_pton(AF_INET, string, &address) <= 0) - { - free(this); - return NULL; - } - this->encoded = chunk_clone(chunk); - this->string = strdup(string); - this->type = ID_IPV4_ADDR; - return &(this->public); - } - else - { - /* try IPv6 */ - struct in6_addr address; - chunk_t chunk = {(void*)&address, sizeof(address)}; - - if (inet_pton(AF_INET6, string, &address) <= 0) - { - free(this); - return NULL; - } - this->encoded = chunk_clone(chunk); - this->string = strdup(string); - this->type = ID_IPV6_ADDR; - return &(this->public); - } - } - } - else - { - if (*string == '@') - { - if (*(string + 1) == '#') - { - /* TODO: Pluto handles '#' as hex encoded ASN1/KEY ID. Do we need this, too? */ - free(this); - return NULL; - } - else - { - this->type = ID_FQDN; - this->string = strdup(string + 1); /* discard @ */ - this->encoded.ptr = strdup(string + 1); - this->encoded.len = strlen(string + 1); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_wc_string; - return &(this->public); - } - } - else - { - this->type = ID_RFC822_ADDR; - this->string = strdup(string); - this->encoded.ptr = strdup(string); - this->encoded.len = strlen(string); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_wc_string; - return &(this->public); - } - } -} - -/* - * Described in header. - */ -identification_t *identification_create_from_encoding(id_type_t type, chunk_t encoded) -{ - private_identification_t *this = identification_create(); - char buf[256]; - chunk_t buf_chunk = chunk_from_buf(buf); - char *pos; - - this->type = type; - switch (type) - { - case ID_ANY: - this->string = strdup("%any"); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_any; - break; - case ID_IPV4_ADDR: - if (encoded.len < sizeof(struct in_addr) || - inet_ntop(AF_INET, encoded.ptr, buf, sizeof(buf)) == NULL) - { - this->string = strdup("(invalid ID_IPV4_ADDR)"); - } - else - { - this->string = strdup(buf); - } - break; - case ID_IPV6_ADDR: - if (encoded.len < sizeof(struct in6_addr) || - inet_ntop(AF_INET6, encoded.ptr, buf, INET6_ADDRSTRLEN) == NULL) - { - this->string = strdup("(invalid ID_IPV6_ADDR)"); - } - else - { - this->string = strdup(buf); - } - break; - case ID_FQDN: - snprintf(buf, sizeof(buf), "@%.*s", encoded.len, encoded.ptr); - this->string = strdup(buf); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_wc_string; - break; - case ID_RFC822_ADDR: - snprintf(buf, sizeof(buf), "%.*s", encoded.len, encoded.ptr); - this->string = strdup(buf); - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_wc_string; - break; - case ID_DER_ASN1_DN: - snprintf(buf, sizeof(buf), "%.*s", encoded.len, encoded.ptr); - /* TODO: whats returned on failure */ - dntoa(encoded, &buf_chunk); - this->string = strdup(buf); - this->public.equals = (bool (*) (identification_t*,identification_t*))equals_dn; - this->public.belongs_to = (bool (*) (identification_t*,identification_t*))belongs_to_dn; - break; - case ID_DER_ASN1_GN: - this->string = gntoa(encoded); - break; - case ID_KEY_ID: - this->string = strdup("(unparsed KEY_ID)"); - break; - default: - snprintf(buf, sizeof(buf), "(invalid ID type: %d)", type); - this->string = strdup(buf); - break; - } - - /* apply encoded chunk */ - if (type != ID_ANY) - { - this->encoded = chunk_clone(encoded); - } - - /* remove unprintable chars in string */ - for (pos = this->string; *pos != '\0'; pos++) - { - if (!isprint(*pos)) - { - *pos = '?'; - } - } - return &(this->public); -} diff --git a/Source/lib/utils/identification.h b/Source/lib/utils/identification.h deleted file mode 100644 index 309b6858c..000000000 --- a/Source/lib/utils/identification.h +++ /dev/null @@ -1,245 +0,0 @@ -/** - * @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, "jsmith@example.com". 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. tester@strongswan.org) 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: alice@wonderland.org - * - 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_ */ diff --git a/Source/lib/utils/iterator.h b/Source/lib/utils/iterator.h deleted file mode 100644 index de81db8e9..000000000 --- a/Source/lib/utils/iterator.h +++ /dev/null @@ -1,153 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/leak_detective.c b/Source/lib/utils/leak_detective.c deleted file mode 100644 index 780ba4c05..000000000 --- a/Source/lib/utils/leak_detective.c +++ /dev/null @@ -1,540 +0,0 @@ -/** - * @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 */ diff --git a/Source/lib/utils/leak_detective.h b/Source/lib/utils/leak_detective.h deleted file mode 100644 index 13c0d01ab..000000000 --- a/Source/lib/utils/leak_detective.h +++ /dev/null @@ -1,50 +0,0 @@ -/** - * @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_ */ diff --git a/Source/lib/utils/linked_list.c b/Source/lib/utils/linked_list.c deleted file mode 100644 index 64443434b..000000000 --- a/Source/lib/utils/linked_list.c +++ /dev/null @@ -1,727 +0,0 @@ -/** - * @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)); -} diff --git a/Source/lib/utils/linked_list.h b/Source/lib/utils/linked_list.h deleted file mode 100644 index 8647f064d..000000000 --- a/Source/lib/utils/linked_list.h +++ /dev/null @@ -1,203 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/logger.c b/Source/lib/utils/logger.c deleted file mode 100644 index fdaeddff0..000000000 --- a/Source/lib/utils/logger.c +++ /dev/null @@ -1,342 +0,0 @@ -/** - * @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; -} diff --git a/Source/lib/utils/logger.h b/Source/lib/utils/logger.h deleted file mode 100644 index dec73078e..000000000 --- a/Source/lib/utils/logger.h +++ /dev/null @@ -1,198 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/logger_manager.c b/Source/lib/utils/logger_manager.c deleted file mode 100644 index ecbe1a6c1..000000000 --- a/Source/lib/utils/logger_manager.c +++ /dev/null @@ -1,220 +0,0 @@ -/** - * @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]); - } -} diff --git a/Source/lib/utils/logger_manager.h b/Source/lib/utils/logger_manager.h deleted file mode 100644 index a3ff5a37e..000000000 --- a/Source/lib/utils/logger_manager.h +++ /dev/null @@ -1,160 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/randomizer.c b/Source/lib/utils/randomizer.c deleted file mode 100644 index 09e81894e..000000000 --- a/Source/lib/utils/randomizer.c +++ /dev/null @@ -1,164 +0,0 @@ -/** - * @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); -} diff --git a/Source/lib/utils/randomizer.h b/Source/lib/utils/randomizer.h deleted file mode 100644 index 55519550e..000000000 --- a/Source/lib/utils/randomizer.h +++ /dev/null @@ -1,110 +0,0 @@ -/** - * @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_*/ diff --git a/Source/lib/utils/tester.c b/Source/lib/utils/tester.c deleted file mode 100644 index a7599dd82..000000000 --- a/Source/lib/utils/tester.c +++ /dev/null @@ -1,256 +0,0 @@ -/** - * @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); -} diff --git a/Source/lib/utils/tester.h b/Source/lib/utils/tester.h deleted file mode 100644 index 3decb2039..000000000 --- a/Source/lib/utils/tester.h +++ /dev/null @@ -1,148 +0,0 @@ -/** - * @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_*/ |