better split up of library files "types.h" & "definitions.h"

centralized all printf specifier character definitions
reuse of arginfo handlers
more cleanups
fixed more AMD64 issues
added DEBUG_LEVEL compile flag to exclude DBGn() statements
This commit is contained in:
Martin Willi
2006-10-31 12:27:59 +00:00
parent de8b8a8c44
commit db7ef62494
151 changed files with 1094 additions and 1061 deletions
+4 -2
View File
@@ -1,9 +1,11 @@
lib_LTLIBRARIES = libstrongswan.la
libstrongswan_la_SOURCES = \
definitions.c definitions.h \
types.c types.h \
library.c library.h \
chunk.c chunk.h \
debug.c debug.h \
enum.c enum.h \
printf_hook.c printf_hook.h \
asn1/oid.c asn1/oid.h \
asn1/asn1.c asn1/asn1.h \
asn1/pem.c asn1/pem.h \
+11 -3
View File
@@ -19,8 +19,8 @@
#include "asn1.h"
#include <types.h>
#include <library.h>
#include <debug.h>
/* some common prefabricated ASN.1 constants */
static u_char ASN1_INTEGER_0_str[] = { 0x02, 0x00 };
@@ -98,7 +98,7 @@ chunk_t asn1_algorithmIdentifier(int oid)
case OID_SHA1:
return ASN1_sha1_id;
default:
return CHUNK_INITIALIZER;
return chunk_empty;
}
}
@@ -320,9 +320,13 @@ static void debug_asn1_simple_object(chunk_t object, asn1_t type, bool private)
break;
}
if (private)
{
DBG4("%B", &object);
}
else
{
DBG3("%B", &object);
}
}
/**
@@ -335,7 +339,7 @@ bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *objec
chunk_t *blob1;
u_char *start_ptr;
*object = CHUNK_INITIALIZER;
*object = chunk_empty;
if (obj.flags & ASN1_END) /* end of loop or option found */
{
@@ -447,9 +451,13 @@ bool extract_object(asn1Object_t const *objects, u_int *objectID, chunk_t *objec
object->ptr = start_ptr;
object->len = (size_t)(blob->ptr - start_ptr);
if (ctx->private)
{
DBG4("%B", object);
}
else
{
DBG3("%B", object);
}
}
else if (obj.flags & ASN1_BODY)
{
+1 -1
View File
@@ -19,7 +19,7 @@
#include <stdarg.h>
#include <gmp.h>
#include <types.h>
#include <library.h>
#include <asn1/oid.h>
+6 -5
View File
@@ -23,6 +23,7 @@
#include "pem.h"
#include <library.h>
#include <debug.h>
#include <asn1/asn1.h>
#include <asn1/ttodata.h>
@@ -53,7 +54,7 @@ static bool present(const char* pattern, chunk_t* ch)
*/
static bool find_boundary(const char* tag, chunk_t *line)
{
chunk_t name = CHUNK_INITIALIZER;
chunk_t name = chunk_empty;
if (!present("-----", line))
return FALSE;
@@ -162,8 +163,8 @@ err_t pem_to_bin(chunk_t *blob, chunk_t *passphrase, bool *pgp)
chunk_t src = *blob;
chunk_t dst = *blob;
chunk_t line = CHUNK_INITIALIZER;
chunk_t iv = CHUNK_INITIALIZER;
chunk_t line = chunk_empty;
chunk_t iv = chunk_empty;
u_char iv_buf[16]; /* MD5 digest size */
@@ -198,8 +199,8 @@ err_t pem_to_bin(chunk_t *blob, chunk_t *passphrase, bool *pgp)
if (state == PEM_HEADER)
{
err_t ugh = NULL;
chunk_t name = CHUNK_INITIALIZER;
chunk_t value = CHUNK_INITIALIZER;
chunk_t name = chunk_empty;
chunk_t value = chunk_empty;
/* an empty line separates HEADER and BODY */
if (line.len == 0)
+1 -1
View File
@@ -17,7 +17,7 @@
#include <stdio.h>
#include <types.h>
#include <library.h>
err_t pem_to_bin(chunk_t *blob, chunk_t *passphrase, bool *pgp);
+1 -1
View File
@@ -16,7 +16,7 @@
#ifndef TTODATA_H_
#define TTODATA_H_
#include <types.h>
#include <library.h>
#define TTODATAV_BUF 40 /* ttodatav's largest non-literal message */
#define TTODATAV_IGNORESPACE (1<<1) /* ignore spaces in base64 encodings*/
@@ -1,8 +1,8 @@
/**
* @file types.c
*
* @brief Generic types.
*
* @file chunk.c
*
* @brief Pointer/lenght abstraction and its functions.
*
*/
/*
@@ -21,41 +21,23 @@
* for more details.
*/
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include <printf.h>
#include "types.h"
ENUM(status_names, SUCCESS, DESTROY_ME,
"SUCCESS",
"FAILED",
"OUT_OF_RES",
"ALREADY_DONE",
"NOT_SUPPORTED",
"INVALID_ARG",
"NOT_FOUND",
"PARSE_ERROR",
"VERIFY_ERROR",
"INVALID_STATE",
"DESTROY_ME",
);
#include "chunk.h"
#include <printf_hook.h>
/**
* Empty chunk.
*/
chunk_t CHUNK_INITIALIZER = { NULL, 0 };
chunk_t chunk_empty = { NULL, 0 };
/**
* Described in header.
*/
chunk_t chunk_clone(chunk_t chunk)
{
chunk_t clone = CHUNK_INITIALIZER;
chunk_t clone = chunk_empty;
if (chunk.ptr && chunk.len > 0)
{
@@ -142,7 +124,7 @@ chunk_t chunk_alloc(size_t bytes)
bool chunk_equals(chunk_t a, chunk_t b)
{
return a.ptr != NULL && b.ptr != NULL &&
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
}
/**
@@ -155,56 +137,6 @@ bool chunk_equals_or_null(chunk_t a, chunk_t b)
return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
}
/**
* Described in header.
*/
void *clalloc(void * pointer, size_t size)
{
void *data;
data = malloc(size);
memcpy(data, pointer,size);
return (data);
}
/**
* We use a single mutex for all refcount variables. This
* is not optimal for performance, but the critical section
* is not that long...
* TODO: Consider to include a mutex in each refcount_t variable.
*/
static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Described in header.
*
* TODO: May be implemented with atomic CPU instructions
* instead of a mutex.
*/
void ref_get(refcount_t *ref)
{
pthread_mutex_lock(&ref_mutex);
(*ref)++;
pthread_mutex_unlock(&ref_mutex);
}
/**
* Described in header.
*
* TODO: May be implemented with atomic CPU instructions
* instead of a mutex.
*/
bool ref_put(refcount_t *ref)
{
bool more_refs;
pthread_mutex_lock(&ref_mutex);
more_refs = --(*ref);
pthread_mutex_unlock(&ref_mutex);
return !more_refs;
}
/**
* Number of bytes per line to dump raw data
*/
@@ -253,7 +185,7 @@ static int print_bytes(FILE *stream, const struct printf_info *info,
ascii_buffer[i] = '\0';
written += fprintf(stream, "\n%4d: %s %s",
line_start, buffer, ascii_buffer);
line_start, buffer, ascii_buffer);
buffer_pos = buffer;
@@ -302,137 +234,10 @@ static int print_chunk(FILE *stream, const struct printf_info *info,
}
/**
* output handler in printf() for time_t
*/
static int print_time(FILE *stream, const struct printf_info *info,
const void *const *args)
{
static const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
time_t time = *((time_t*)(args[0]));
bool utc = TRUE;
struct tm t;
if (info->alt)
{
utc = *((bool*)(args[1]));
}
if (time == UNDEFINED_TIME)
{
return fprintf(stream, "--- -- --:--:--%s----",
info->alt ? " UTC " : " ");
}
if (utc)
{
gmtime_r(&time, &t);
}
else
{
localtime_r(&time, &t);
}
return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
}
/**
* output handler in printf() for time deltas
*/
static int print_time_delta(FILE *stream, const struct printf_info *info,
const void *const *args)
{
time_t start = *((time_t*)(args[0]));
time_t end = *((time_t*)(args[1]));
u_int delta = abs(end - start);
char* unit = "second";
if (delta > 2 * 60 * 60 * 24)
{
delta /= 60 * 60 * 24;
unit = "days";
}
else if (delta > 2 * 60 * 60)
{
delta /= 60 * 60;
unit = "hours";
}
else if (delta > 2 * 60)
{
delta /= 60;
unit = "minutes";
}
return fprintf(stream, "%d %s", delta, unit);
}
/**
* arginfo handler in printf() for byte ranges
*/
static int print_bytes_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 1)
{
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
}
return 2;
}
/**
* arginfo handler in printf() for time deltas
*/
static int print_time_delta_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 1)
{
argtypes[0] = PA_INT;
argtypes[1] = PA_INT;
}
return 2;
}
/**
* arginfo handler in printf() for time_t
*/
static int print_time_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (info->alt)
{
if (n > 1)
{
argtypes[0] = PA_INT;
argtypes[1] = PA_INT;
}
return 2;
}
if (n > 0)
{
argtypes[0] = PA_INT;
}
return 1;
}
/**
* arginfo handler in printf() for chunks
*/
static int print_chunk_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* register printf() handlers for time_t
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(CHUNK_PRINTF_SPEC, print_chunk, print_chunk_arginfo);
register_printf_function(BYTES_PRINTF_SPEC, print_bytes, print_bytes_arginfo);
register_printf_function(TIME_PRINTF_SPEC, print_time, print_time_arginfo);
register_printf_function(TIME_DELTA_PRINTF_SPEC, print_time_delta, print_time_delta_arginfo);
register_printf_function(PRINTF_CHUNK, print_chunk, arginfo_ptr);
register_printf_function(PRINTF_BYTES, print_bytes, arginfo_ptr_int);
}
+88
View File
@@ -0,0 +1,88 @@
/**
* @file chunk.h
*
* @brief Pointer/lenght abstraction and its functions.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 CHUNK_H_
#define CHUNK_H_
#include <string.h>
#include <stdarg.h>
#include <library.h>
typedef struct chunk_t chunk_t;
/**
* General purpose pointer/length abstraction.
*/
struct chunk_t {
/** Pointer to start of data */
u_char *ptr;
/** Length of data in bytes */
size_t len;
};
/**
* A { NULL, 0 }-chunk handy for initialization.
*/
extern chunk_t chunk_empty;
/**
* Initialize a chunk to point to a static(!) buffer
*/
#define chunk_from_buf(str) { str, sizeof(str) }
/**
* Clone chunk contents in a newly allocated chunk
*/
chunk_t chunk_clone(chunk_t chunk);
/**
* Allocate a chunk from concatenation of other chunks.
* mode is a string 'm' and 'c, 'm' means move chunk,
* 'c' means copy chunk.
*/
chunk_t chunk_cat(const char* mode, ...);
/**
* Free contents of a chunk
*/
void chunk_free(chunk_t *chunk);
/**
* Allocate a chunk
*/
chunk_t chunk_alloc(size_t bytes);
/**
* Compare two chunks for equality,
* NULL chunks are never equal.
*/
bool chunk_equals(chunk_t a, chunk_t b);
/**
* Compare two chunks for equality,
* NULL chunks are always equal.
*/
bool chunk_equals_or_null(chunk_t a, chunk_t b);
#endif /* CHUNK_H_ */
+1 -2
View File
@@ -22,8 +22,7 @@
#include <time.h>
#include <types.h>
#include <definitions.h>
#include <library.h>
#include "certinfo.h"
+1 -2
View File
@@ -27,8 +27,7 @@ typedef enum cert_status_t cert_status_t;
typedef enum crl_reason_t crl_reason_t;
typedef struct certinfo_t certinfo_t;
#include <types.h>
#include <definitions.h>
#include <library.h>
/**
* RFC 2560 OCSP - certificate status
+7 -30
View File
@@ -25,9 +25,8 @@
#include <string.h>
#include <printf.h>
#include <types.h>
#include <library.h>
#include <definitions.h>
#include <debug.h>
#include <asn1/oid.h>
#include <asn1/asn1.h>
#include <asn1/pem.h>
@@ -219,7 +218,7 @@ bool parse_x509crl(chunk_t blob, u_int level0, private_crl_t *crl)
asn1_ctx_t ctx;
bool critical;
chunk_t extnID;
chunk_t userCertificate = CHUNK_INITIALIZER;
chunk_t userCertificate = chunk_empty;
revokedCert_t *revokedCert = NULL;
chunk_t object;
u_int level;
@@ -473,34 +472,12 @@ static int print(FILE *stream, const struct printf_info *info,
return written;
}
/**
* arginfo handler in printf()
*/
static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (info->alt)
{
if (n > 1)
{
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
}
return 2;
}
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(CRL_PRINTF_SPEC, print, print_arginfo);
register_printf_function(PRINTF_CRL, print, arginfo_ptr_alt_ptr_int);
}
/*
@@ -512,11 +489,11 @@ crl_t *crl_create_from_chunk(chunk_t chunk)
/* initialize */
this->crlDistributionPoints = linked_list_create();
this->tbsCertList = CHUNK_INITIALIZER;
this->tbsCertList = chunk_empty;
this->issuer = NULL;
this->revokedCertificates = linked_list_create();
this->authKeyID = CHUNK_INITIALIZER;
this->authKeySerialNumber = CHUNK_INITIALIZER;
this->authKeyID = chunk_empty;
this->authKeySerialNumber = chunk_empty;
/* public functions */
this->public.get_issuer = (identification_t* (*) (const crl_t*))get_issuer;
@@ -543,7 +520,7 @@ crl_t *crl_create_from_chunk(chunk_t chunk)
crl_t *crl_create_from_file(const char *filename)
{
bool pgp = FALSE;
chunk_t chunk = CHUNK_INITIALIZER;
chunk_t chunk = chunk_empty;
crl_t *crl = NULL;
if (!pem_asn1_load_file(filename, NULL, "crl", &chunk, &pgp))
+1 -9
View File
@@ -25,20 +25,12 @@
typedef struct crl_t crl_t;
#include <types.h>
#include <definitions.h>
#include <library.h>
#include <crypto/rsa/rsa_public_key.h>
#include <crypto/certinfo.h>
#include <utils/identification.h>
#include <utils/iterator.h>
/**
* printf specifier for printing crls. When using the
* #-modifier, an additional bool argument defines if dates
* are printed in UTC.
*/
#define CRL_PRINTF_SPEC 'U'
/**
* @brief X.509 certificate revocation list
*
+1 -1
View File
@@ -27,7 +27,7 @@
typedef enum encryption_algorithm_t encryption_algorithm_t;
typedef struct crypter_t crypter_t;
#include <types.h>
#include <library.h>
/**
* @brief Encryption algorithm, as in IKEv2 RFC 3.3.2.
+1 -1
View File
@@ -27,7 +27,7 @@
typedef enum diffie_hellman_group_t diffie_hellman_group_t;
typedef struct diffie_hellman_t diffie_hellman_t;
#include <types.h>
#include <library.h>
/**
* @brief Diffie-Hellman group.
+1 -1
View File
@@ -27,7 +27,7 @@
typedef enum hash_algorithm_t hash_algorithm_t;
typedef struct hasher_t hasher_t;
#include <types.h>
#include <library.h>
/**
* @brief Algorithms to use for hashing.
@@ -30,8 +30,6 @@
#include "md5_hasher.h"
#include <definitions.h>
/* Constants for MD5Transform routine. */
#define S11 7
@@ -28,8 +28,6 @@
#include "sha1_hasher.h"
#include <definitions.h>
/*
* ugly macro stuff
*/
@@ -27,8 +27,6 @@
#include "sha2_hasher.h"
#include <definitions.h>
typedef struct private_sha512_hasher_t private_sha512_hasher_t;
-1
View File
@@ -26,7 +26,6 @@
typedef struct hmac_t hmac_t;
#include <crypto/hashers/hasher.h>
#include <definitions.h>
/**
* @brief Message authentication using hash functions.
-2
View File
@@ -25,8 +25,6 @@
#include "prf_plus.h"
#include <definitions.h>
typedef struct private_prf_plus_t private_prf_plus_t;
/**
+1 -1
View File
@@ -26,7 +26,7 @@
typedef struct hmac_prf_t hmac_prf_t;
#include <types.h>
#include <library.h>
#include <crypto/prfs/prf.h>
#include <crypto/hashers/hasher.h>
+1 -1
View File
@@ -27,7 +27,7 @@
typedef enum pseudo_random_function_t pseudo_random_function_t;
typedef struct prf_t prf_t;
#include <types.h>
#include <library.h>
/**
* @brief Pseudo random function, as in IKEv2 RFC 3.3.2.
@@ -762,7 +762,7 @@ rsa_private_key_t *rsa_private_key_create_from_chunk(chunk_t blob)
rsa_private_key_t *rsa_private_key_create_from_file(char *filename, chunk_t *passphrase)
{
bool pgp = FALSE;
chunk_t chunk = CHUNK_INITIALIZER;
chunk_t chunk = chunk_empty;
rsa_private_key_t *key = NULL;
if (!pem_asn1_load_file(filename, passphrase, "private key", &chunk, &pgp))
@@ -26,8 +26,7 @@
typedef struct rsa_private_key_t rsa_private_key_t;
#include <types.h>
#include <definitions.h>
#include <library.h>
#include <crypto/rsa/rsa_public_key.h>
#include <crypto/hashers/hasher.h>
@@ -485,7 +485,7 @@ rsa_public_key_t *rsa_public_key_create_from_chunk(chunk_t blob)
rsa_public_key_t *rsa_public_key_create_from_file(char *filename)
{
bool pgp = FALSE;
chunk_t chunk = CHUNK_INITIALIZER;
chunk_t chunk = chunk_empty;
rsa_public_key_t *pubkey = NULL;
if (!pem_asn1_load_file(filename, NULL, "public key", &chunk, &pgp))
@@ -28,8 +28,7 @@ typedef struct rsa_public_key_t rsa_public_key_t;
#include <gmp.h>
#include <types.h>
#include <definitions.h>
#include <library.h>
/**
* @brief RSA public key with associated functions.
+1 -2
View File
@@ -27,8 +27,7 @@
typedef enum integrity_algorithm_t integrity_algorithm_t;
typedef struct signer_t signer_t;
#include <types.h>
#include <definitions.h>
#include <library.h>
/**
* @brief Integrity algorithm, as in IKEv2 RFC 3.3.2.
+7 -30
View File
@@ -28,9 +28,8 @@
#include "x509.h"
#include <types.h>
#include <library.h>
#include <definitions.h>
#include <debug.h>
#include <asn1/oid.h>
#include <asn1/asn1.h>
#include <asn1/pem.h>
@@ -1161,34 +1160,12 @@ static int print(FILE *stream, const struct printf_info *info,
return written;
}
/**
* arginfo handler in printf()
*/
static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (info->alt)
{
if (n > 1)
{
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
}
return 2;
}
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(X509_PRINTF_SPEC, print, print_arginfo);
register_printf_function(PRINTF_X509, print, arginfo_ptr_alt_ptr_int);
}
/**
@@ -1215,15 +1192,15 @@ x509_t *x509_create_from_chunk(chunk_t chunk)
private_x509_t *this = malloc_thing(private_x509_t);
/* initialize */
this->subjectPublicKey = CHUNK_INITIALIZER;
this->subjectPublicKey = chunk_empty;
this->public_key = NULL;
this->subject = NULL;
this->issuer = NULL;
this->subjectAltNames = linked_list_create();
this->crlDistributionPoints = linked_list_create();
this->subjectKeyID = CHUNK_INITIALIZER;
this->authKeyID = CHUNK_INITIALIZER;
this->authKeySerialNumber = CHUNK_INITIALIZER;
this->subjectKeyID = chunk_empty;
this->authKeyID = chunk_empty;
this->authKeySerialNumber = chunk_empty;
/* public functions */
this->public.equals = (bool (*) (const x509_t*,const x509_t*))equals;
@@ -1270,7 +1247,7 @@ x509_t *x509_create_from_chunk(chunk_t chunk)
x509_t *x509_create_from_file(const char *filename, const char *label)
{
bool pgp = FALSE;
chunk_t chunk = CHUNK_INITIALIZER;
chunk_t chunk = chunk_empty;
x509_t *cert = NULL;
if (!pem_asn1_load_file(filename, NULL, label, &chunk, &pgp))
+1 -9
View File
@@ -25,20 +25,12 @@
typedef struct x509_t x509_t;
#include <types.h>
#include <definitions.h>
#include <library.h>
#include <crypto/rsa/rsa_public_key.h>
#include <crypto/certinfo.h>
#include <utils/identification.h>
#include <utils/iterator.h>
/**
* printf specifier for printing certificates. When using the
* #-modifier, an additional bool argument defines if dates
* are printed in UTC.
*/
#define X509_PRINTF_SPEC 'Q'
/**
* @brief X.509 certificate.
*
+41
View File
@@ -0,0 +1,41 @@
/**
* @file library.c
*
* @brief Logging functions for the library.
*
*/
/*
* 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 <stdarg.h>
#include <stdio.h>
#include "debug.h"
/**
* default dbg function which printf all to stderr
*/
static void dbg_stderr(int level, char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
void (*dbg) (int level, char *fmt, ...) = dbg_stderr;
+60
View File
@@ -0,0 +1,60 @@
/**
* @file log.h
*
* @brief Logging functions for the library.
*
*/
/*
* 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 DEBUG_H_
#define DEBUG_H_
#ifndef DEBUG_LEVEL
# define DEBUG_LEVEL 4
#endif /* DEBUG_LEVEL */
/** debug macros, they call the dbg function hook */
#if DEBUG_LEVEL >= 1
# define DBG1(fmt, ...) dbg(1, fmt, ##__VA_ARGS__)
#endif /* DEBUG_LEVEL */
#if DEBUG_LEVEL >= 2
# define DBG2(fmt, ...) dbg(2, fmt, ##__VA_ARGS__)
#endif /* DEBUG_LEVEL */
#if DEBUG_LEVEL >= 3
# define DBG3(fmt, ...) dbg(3, fmt, ##__VA_ARGS__)
#endif /* DEBUG_LEVEL */
#if DEBUG_LEVEL >= 4
# define DBG4(fmt, ...) dbg(4, fmt, ##__VA_ARGS__)
#endif /* DEBUG_LEVEL */
#ifndef DBG1
# define DBG1(...) {}
#endif
#ifndef DBG2
# define DBG2(...) {}
#endif
#ifndef DBG3
# define DBG3(...) {}
#endif
#ifndef DBG4
# define DBG4(...) {}
#endif
/** dbg function hook, uses stderr logger by default */
extern void (*dbg) (int level, char *fmt, ...);
#endif /* DEBUG_H_ */
-108
View File
@@ -1,108 +0,0 @@
/**
* @file definitions.h
*
* @brief General purpose definitions and macros.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Hochschule fuer Technik Rapperswil
* Copyright (C) 1998, 1999 D. Hugh Redelmeier. (Endian stuff)
*
* 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 DEFINITIONS_H_
#define DEFINITIONS_H_
#include <stddef.h>
#define BITS_PER_BYTE 8
/**
* Default length for various auxiliary text buffers
*/
#define BUF_LEN 512
/**
* Macro compares two strings for equality
*/
#define streq(x,y) (strcmp(x, y) == 0)
/**
* Macro compares two binary blobs for equality
*/
#define memeq(x,y,len) (memcmp(x, y, len) == 0)
/**
* Macro gives back larger of two values.
*/
#define max(x,y) ((x) > (y) ? (x):(y))
/**
* Macro gives back smaller of two values.
*/
#define min(x,y) ((x) < (y) ? (x):(y))
/**
* Call destructor of a object if object != NULL
*/
#define DESTROY_IF(obj) if (obj) obj->destroy(obj)
/**
* Debug macro to follow control flow
*/
#define POS printf("%s, line %d\n", __FILE__, __LINE__)
/**
* Macro to allocate a sized type.
*/
#define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
/**
* Assign a function as a class method
*/
#define ASSIGN(method, function) (method = (typeof(method))function)
/**
* printf() specifier to resolf enum names, see enum_names
*/
#define ENUM_PRINTF_SPEC 'N'
typedef struct enum_name_t enum_name_t;
/**
* Struct to store names for enums. Use the convenience macros
* to define these.
* For a single range, use:
* ENUM(name, first, last, string1, string2, ...)
*
* For multiple ranges, use:
* ENUM_BEGIN(name, first, last, string1, string2, ...)
* ENUM_NEXT(name, first, last, last_from_previous, string3, ...)
* ENUM_NEXT(name, first, last, last_from_previous, string4, ...)
* ENUM_END(name, last_from_previous)
*/
struct enum_name_t {
long first;
long last;
enum_name_t *next;
char *names[];
};
#define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
#define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
#define ENUM_END(name, prev) enum_name_t *name = &name##prev;
#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
#endif /*DEFINITIONS_H_*/
@@ -1,13 +1,12 @@
/**
* @file definitions.c
*
* @brief General purpose definitions and macros.
*
* @file library.c
*
* @brief enum value to string conversion functions.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* Copyright (C) 2006 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -21,13 +20,15 @@
* for more details.
*/
#include <printf.h>
#include <stddef.h>
#include <stdio.h>
#include "definitions.h"
#include "enum.h"
/*
* Described in header
#include <printf_hook.h>
/**
* get the name of an enum value in a enum_name_t list
*/
static char *enum_name(enum_name_t *e, long val)
{
@@ -42,15 +43,14 @@ static char *enum_name(enum_name_t *e, long val)
return NULL;
}
/**
* output handler in printf() for enum names
*/
static int print_enum(FILE *stream, const struct printf_info *info,
const void *const *args)
const void *const *args)
{
enum_name_t *ed = *((void**)(args[0]));
long val = *((size_t*)(args[1]));
long val = *((long*)(args[1]));
char *name;
name = enum_name(ed, val);
@@ -62,24 +62,9 @@ static int print_enum(FILE *stream, const struct printf_info *info,
}
/**
* arginfo handler in printf() for enum names
*/
static int print_enum_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 1)
{
/* enum_names ptr */
argtypes[0] = PA_POINTER;
/* value */
argtypes[1] = PA_INT;
}
return 2;
}
/**
* register printf() handlers for enum names
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(ENUM_PRINTF_SPEC, print_enum, print_enum_arginfo);
register_printf_function(PRINTF_ENUM, print_enum, arginfo_ptr_int);
}
+106
View File
@@ -0,0 +1,106 @@
/**
* @file enum.h
*
* @brief enum value to string conversion functions.
*
*/
/*
* 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 ENUM_H_
#define ENUM_H_
typedef struct enum_name_t enum_name_t;
/**
* @brief Struct to store names for enums.
*
* To print the string representation of enumeration values, the strings
* are stored in these structures. Every enum_name contains a range
* of strings, multiple ranges are linked together.
* Use the convenience macros to define these linked ranges.
*
* For a single range, use:
* ENUM(name, first, last, string1, string2, ...)
*
* For multiple linked ranges, use:
* ENUM_BEGIN(name, first, last, string1, string2, ...)
* ENUM_NEXT(name, first, last, last_from_previous, string3, ...)
* ENUM_NEXT(name, first, last, last_from_previous, string4, ...)
* ENUM_END(name, last_from_previous)
*
* The ENUM and the ENUM_END define a enum_name_t pointer with the name supplied
* in "name".
*
* Resolving of enum names is done using a printf hook. A printf fromat
* character %N is replaced by the enum string. Printf needs two arguments to
* resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed
* by the numerical enum value.
*/
struct enum_name_t {
/** value of the first enum string */
long first;
/** value of the last enum string */
long last;
/** next enum_name_t in list */
enum_name_t *next;
/** array of strings containing names from first to last */
char *names[];
};
/**
* @brief Begin a new enum_name list.
*
* @param name name of the enum_name list
* @param first enum value of the first enum string
* @param last enum value of the last enum string
* @param ... a list of strings
*/
#define ENUM_BEGIN(name, first, last, ...) static enum_name_t name##last = {first, last, NULL, { __VA_ARGS__ }}
/**
* @brief Continue a enum name list startetd with ENUM_BEGIN.
*
* @param name name of the enum_name list
* @param first enum value of the first enum string
* @param last enum value of the last enum string
* @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
* @param ... a list of strings
*/
#define ENUM_NEXT(name, first, last, prev, ...) static enum_name_t name##last = {first, last, &name##prev, { __VA_ARGS__ }}
/**
* @brief Complete enum name list started with ENUM_BEGIN.
*
* @param name name of the enum_name list
* @param prev enum value of the "last" defined in ENUM_BEGIN/previous ENUM_NEXT
*/
#define ENUM_END(name, prev) enum_name_t *name = &name##prev;
/**
* @brief Define a enum name with only one range.
*
* This is a convenience macro to use when a enum_name list contains only
* one range, and is equal as defining ENUM_BEGIN followed by ENUM_END.
*
* @param name name of the enum_name list
* @param first enum value of the first enum string
* @param last enum value of the last enum string
* @param ... a list of strings
*/
#define ENUM(name, first, last, ...) ENUM_BEGIN(name, first, last, __VA_ARGS__); ENUM_END(name, last)
#endif /* ENUM_H_ */
+138 -10
View File
@@ -1,7 +1,7 @@
/**
* @file library.c
*
* @brief Library (de-)initialization.
* @brief Helper functions and definitions.
*
*/
@@ -21,22 +21,150 @@
* for more details.
*/
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <stdarg.h>
#include <pthread.h>
#include "library.h"
#include <printf_hook.h>
ENUM(status_names, SUCCESS, DESTROY_ME,
"SUCCESS",
"FAILED",
"OUT_OF_RES",
"ALREADY_DONE",
"NOT_SUPPORTED",
"INVALID_ARG",
"NOT_FOUND",
"PARSE_ERROR",
"VERIFY_ERROR",
"INVALID_STATE",
"DESTROY_ME",
);
/**
* default dbg function which printf all to stderr
* Described in header.
*/
static void dbg_stderr(int level, char *fmt, ...)
void *clalloc(void * pointer, size_t size)
{
va_list args;
void *data;
data = malloc(size);
va_start(args, fmt);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
memcpy(data, pointer,size);
return (data);
}
void (*dbg) (int level, char *fmt, ...) = dbg_stderr;
/**
* We use a single mutex for all refcount variables. This
* is not optimal for performance, but the critical section
* is not that long...
* TODO: Consider to include a mutex in each refcount_t variable.
*/
static pthread_mutex_t ref_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Described in header.
*
* TODO: May be implemented with atomic CPU instructions
* instead of a mutex.
*/
void ref_get(refcount_t *ref)
{
pthread_mutex_lock(&ref_mutex);
(*ref)++;
pthread_mutex_unlock(&ref_mutex);
}
/**
* Described in header.
*
* TODO: May be implemented with atomic CPU instructions
* instead of a mutex.
*/
bool ref_put(refcount_t *ref)
{
bool more_refs;
pthread_mutex_lock(&ref_mutex);
more_refs = --(*ref);
pthread_mutex_unlock(&ref_mutex);
return !more_refs;
}
/**
* output handler in printf() for time_t
*/
static int print_time(FILE *stream, const struct printf_info *info,
const void *const *args)
{
static const char* months[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
time_t time = *((time_t*)(args[0]));
bool utc = TRUE;
struct tm t;
if (info->alt)
{
utc = *((bool*)(args[1]));
}
if (time == UNDEFINED_TIME)
{
return fprintf(stream, "--- -- --:--:--%s----",
info->alt ? " UTC " : " ");
}
if (utc)
{
gmtime_r(&time, &t);
}
else
{
localtime_r(&time, &t);
}
return fprintf(stream, "%s %02d %02d:%02d:%02d%s%04d",
months[t.tm_mon], t.tm_mday, t.tm_hour, t.tm_min,
t.tm_sec, utc ? " UTC " : " ", t.tm_year + 1900);
}
/**
* output handler in printf() for time deltas
*/
static int print_time_delta(FILE *stream, const struct printf_info *info,
const void *const *args)
{
time_t start = *((time_t*)(args[0]));
time_t end = *((time_t*)(args[1]));
u_int delta = abs(end - start);
char* unit = "second";
if (delta > 2 * 60 * 60 * 24)
{
delta /= 60 * 60 * 24;
unit = "days";
}
else if (delta > 2 * 60 * 60)
{
delta /= 60 * 60;
unit = "hours";
}
else if (delta > 2 * 60)
{
delta /= 60;
unit = "minutes";
}
return fprintf(stream, "%d %s", delta, unit);
}
/**
* register printf() handlers for time_t
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(PRINTF_TIME, print_time, arginfo_int_alt_int_int);
register_printf_function(PRINTF_TIME_DELTA, print_time_delta, arginfo_int_int);
}
+194 -11
View File
@@ -1,14 +1,14 @@
/**
* @file library.h
*
* @brief Global library header.
*
*
* @brief Helper functions and definitions.
*
*/
/*
* 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
@@ -96,13 +96,196 @@
* @ingroup libstrongswan
*/
/** debug macros, they call the dbg function hook */
#define DBG1(fmt, ...) dbg(1, fmt, ##__VA_ARGS__)
#define DBG2(fmt, ...) dbg(2, fmt, ##__VA_ARGS__)
#define DBG3(fmt, ...) dbg(3, fmt, ##__VA_ARGS__)
#define DBG4(fmt, ...) dbg(4, fmt, ##__VA_ARGS__)
#include <gmp.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stddef.h>
#include <printf.h>
/** dbg function hook, uses stderr logger by default */
extern void (*dbg) (int level, char *fmt, ...);
#include <enum.h>
/**
* Number of bits in a byte
*/
#define BITS_PER_BYTE 8
/**
* Default length for various auxiliary text buffers
*/
#define BUF_LEN 512
/**
* Macro compares two strings for equality
*/
#define streq(x,y) (strcmp(x, y) == 0)
/**
* Macro compares two binary blobs for equality
*/
#define memeq(x,y,len) (memcmp(x, y, len) == 0)
/**
* Macro gives back larger of two values.
*/
#define max(x,y) ((x) > (y) ? (x):(y))
/**
* Macro gives back smaller of two values.
*/
#define min(x,y) ((x) < (y) ? (x):(y))
/**
* Call destructor of a object if object != NULL
*/
#define DESTROY_IF(obj) if (obj) obj->destroy(obj)
/**
* Debug macro to follow control flow
*/
#define POS printf("%s, line %d\n", __FILE__, __LINE__)
/**
* Macro to allocate a sized type.
*/
#define malloc_thing(thing) ((thing*)malloc(sizeof(thing)))
/**
* Assign a function as a class method
*/
#define ASSIGN(method, function) (method = (typeof(method))function)
/**
* time_t not defined
*/
#define UNDEFINED_TIME 0
/**
* General purpose boolean type.
*/
typedef int bool;
#define FALSE 0
#define TRUE 1
typedef enum status_t status_t;
/**
* Return values of function calls.
*/
enum status_t {
/**
* Call succeeded.
*/
SUCCESS,
/**
* Call failed.
*/
FAILED,
/**
* Out of resources.
*/
OUT_OF_RES,
/**
* The suggested operation is already done
*/
ALREADY_DONE,
/**
* Not supported.
*/
NOT_SUPPORTED,
/**
* One of the arguments is invalid.
*/
INVALID_ARG,
/**
* Something could not be found.
*/
NOT_FOUND,
/**
* Error while parsing.
*/
PARSE_ERROR,
/**
* Error while verifying.
*/
VERIFY_ERROR,
/**
* Object in invalid state.
*/
INVALID_STATE,
/**
* Destroy object which called method belongs to.
*/
DESTROY_ME,
};
/**
* enum_names for type status_t.
*/
extern enum_name_t *status_names;
/**
* deprecated pluto style return value:
* error message, NULL for success
*/
typedef const char *err_t;
/**
* Handle struct timeval like an own type.
*/
typedef struct timeval timeval_t;
/**
* Handle struct timespec like an own type.
*/
typedef struct timespec timespec_t;
/**
* Handle struct chunk_t like an own type.
*/
typedef struct sockaddr sockaddr_t;
/**
* Clone a data to a newly allocated buffer
*/
void *clalloc(void *pointer, size_t size);
/**
* Special type to count references
*/
typedef volatile u_int refcount_t;
/**
* @brief Get a new reference.
*
* Increments the reference counter atomic.
*
* @param ref pointer to ref counter
*/
void ref_get(refcount_t *ref);
/**
* @brief Put back a unused reference.
*
* Decrements the reference counter atomic and
* says if more references available.
*
* @param ref pointer to ref counter
* @return TRUE if no more references counted
*/
bool ref_put(refcount_t *ref);
#include <chunk.h>
#include <printf_hook.h>
#endif /* LIBRARY_H_ */
+105
View File
@@ -0,0 +1,105 @@
/**
* @file printf_hook.c
*
* @brief Printf hook definitions and arginfo functions.
*
*/
/*
* 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 "printf_hook.h"
/**
* arginfo handler in printf() pointer
*/
int arginfo_ptr(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* arginfo handler for one ptr, one int
*/
int arginfo_ptr_int(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 1)
{
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
}
return 2;
}
/**
* arginfo handler for two int arguments
*/
int arginfo_int_int(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 1)
{
argtypes[0] = PA_INT;
argtypes[1] = PA_INT;
}
return 2;
}
/**
* special arginfo handler respecting alt flag
*/
int arginfo_int_alt_int_int(const struct printf_info *info, size_t n, int *argtypes)
{
if (info->alt)
{
if (n > 1)
{
argtypes[0] = PA_INT;
argtypes[1] = PA_INT;
}
return 2;
}
if (n > 0)
{
argtypes[0] = PA_INT;
}
return 1;
}
/**
* special arginfo handler respecting alt flag
*/
int arginfo_ptr_alt_ptr_int(const struct printf_info *info, size_t n, int *argtypes)
{
if (info->alt)
{
if (n > 1)
{
argtypes[0] = PA_POINTER;
argtypes[1] = PA_INT;
}
return 2;
}
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
+71
View File
@@ -0,0 +1,71 @@
/**
* @file printf_hook.h
*
* @brief Printf hook definitions and arginfo functions.
*
*/
/*
* 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 PRINTF_HOOK_H_
#define PRINTF_HOOK_H_
#include <printf.h>
/**
* Printf() hook characters.
* We define all characters here to have them on a central place.
*/
/** 2 arguments: enum_name_t *name, long value */
#define PRINTF_ENUM 'N'
/** 1 argument: chunk_t *chunk; use #-modifier to print inline */
#define PRINTF_CHUNK 'B'
/** 2 arguments: u_char *buffer, int size */
#define PRINTF_BYTES 'b'
/** 1 argument: int time; with #-modifier 2 arguments: int time, bool utc */
#define PRINTF_TIME 'T'
/** 2 arguments: integer begin, int end */
#define PRINTF_TIME_DELTA 'V'
/** 1 argument: x509_t *cert; with #-modifier 2 arguments: x509_t *cert, bool utc */
#define PRINTF_X509 'Q'
/** 1 argument: crl_t *crl; with #-modifier 2 arguments: crl_t *crl, bool utc */
#define PRINTF_CRL 'U'
/** 1 argumnet: host_t *host; use #-modifier to include port number */
#define PRINTF_HOST 'H'
/** 1 argument: identification_t *id */
#define PRINTF_IDENTIFICATION 'D'
/** 1 argument: traffic_selector_t *ts */
#define PRINTF_TRAFFIC_SELECTOR 'R'
/** 1 argument: ike_sa_t *ike_sa */
#define PRINTF_IKE_SA 'K'
/** 1 argument: ike_sa_id_t *id */
#define PRINTF_IKE_SA_ID 'J'
/** 1 argument: child_sa_t *child_sa */
#define PRINTF_CHILD_SA 'P'
/** 1 argument: message_t *message */
#define PRINTF_MESSAGE 'M'
/**
* Generic arginfo handlers for printf() hooks
*/
int arginfo_ptr(const struct printf_info *info, size_t n, int *argtypes);
int arginfo_ptr_int(const struct printf_info *info, size_t n, int *argtypes);
int arginfo_int_int(const struct printf_info *info, size_t n, int *argtypes);
int arginfo_ptr_alt_ptr_int(const struct printf_info *info, size_t n, int *argtypes);
int arginfo_int_alt_int_int(const struct printf_info *info, size_t n, int *argtypes);
#endif /* PRINTF_HOOK_H_ */
-256
View File
@@ -1,256 +0,0 @@
/**
* @file types.h
*
* @brief Generic types.
*
*/
/*
* Copyright (C) 2005-2006 Martin Willi
* Copyright (C) 2005 Jan Hutter
* 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 TYPES_H_
#define TYPES_H_
#include <gmp.h>
#include <sys/types.h>
#include <stdlib.h>
#include <definitions.h>
/**
* General purpose boolean type.
*/
typedef int bool;
#define FALSE 0
#define TRUE 1
/**
* error message, or NULL for success
*/
typedef const char *err_t;
typedef enum status_t status_t;
/**
* Return values of function calls.
*/
enum status_t {
/**
* Call succeeded.
*/
SUCCESS,
/**
* Call failed.
*/
FAILED,
/**
* Out of resources.
*/
OUT_OF_RES,
/**
* The suggested operation is already done
*/
ALREADY_DONE,
/**
* Not supported.
*/
NOT_SUPPORTED,
/**
* One of the arguments is invalid.
*/
INVALID_ARG,
/**
* Something could not be found.
*/
NOT_FOUND,
/**
* Error while parsing.
*/
PARSE_ERROR,
/**
* Error while verifying.
*/
VERIFY_ERROR,
/**
* Object in invalid state.
*/
INVALID_STATE,
/**
* Destroy object which called method belongs to.
*/
DESTROY_ME,
};
/**
* enum_names for type status_t.
*/
extern enum_name_t *status_names;
/**
* Handle struct timeval like an own type.
*/
typedef struct timeval timeval_t;
/**
* Handle struct timespec like an own type.
*/
typedef struct timespec timespec_t;
/**
* Handle struct chunk_t like an own type.
*/
typedef struct sockaddr sockaddr_t;
/**
* Use struct chunk_t as chunk_t.
*/
typedef struct chunk_t chunk_t;
/**
* General purpose pointer/length abstraction.
*/
struct chunk_t {
/**
* Pointer to start of data
*/
u_char *ptr;
/**
* Length of data in bytes
*/
size_t len;
};
/**
* used to initialize a chunk to { NULL, 0 }.
*/
extern chunk_t CHUNK_INITIALIZER;
/**
* Printf() hook character to dump a chunk using printf.
* The argument supplied to printf() is a pointer to a chunk.
* E.g. printf("chunk xy is: %B", &xy);
*/
#define CHUNK_PRINTF_SPEC 'B'
/**
* Printf() hook character to dump a chunk using printf.
* Two arguments are supplied for one format string charactar,
* first a pointer to the buffer, and as second the length of the buffer.
* E.g. printf("buffer xy is: %b", buffer, sizeof(buffer));
*/
#define BYTES_PRINTF_SPEC 'b'
/**
* printf specifier for time_t, use #-modifier to print time as UTC
*/
#define TIME_PRINTF_SPEC 'T'
/**
* printf specifier for time_t deltas, uses two arguments
* E.g. printf("%V", begin, end);
*/
#define TIME_DELTA_PRINTF_SPEC 'V'
/**
* time_t for a not defined time
*/
#define UNDEFINED_TIME 0
/**
* Initialize a chunk to a static buffer
*/
#define chunk_from_buf(str) { str, sizeof(str) }
/**
* Clone chunk contents in a newly allocated chunk
*/
chunk_t chunk_clone(chunk_t chunk);
/**
* Allocate a chunk from concatenation of other chunks.
* mode is a string 'm' and 'c, 'm' means move chunk,
* 'c' means copy chunk.
*/
chunk_t chunk_cat(const char* mode, ...);
/**
* Free contents of a chunk
*/
void chunk_free(chunk_t *chunk);
/**
* Allocate a chunk
*/
chunk_t chunk_alloc(size_t bytes);
/**
* Compare two chunks for equality,
* NULL chunks are never equal.
*/
bool chunk_equals(chunk_t a, chunk_t b);
/**
* Compare two chunks for equality,
* NULL chunks are always equal.
*/
bool chunk_equals_or_null(chunk_t a, chunk_t b);
/**
* Clone a data to a newly allocated buffer
*/
void *clalloc(void *pointer, size_t size);
/**
* Special type to count references
*/
typedef volatile u_int refcount_t;
/**
* @brief Get a new reference.
*
* Increments the reference counter atomic.
*
* @param ref pointer to ref counter
*/
void ref_get(refcount_t *ref);
/**
* @brief Put back a unused reference.
*
* Decrements the reference counter atomic and
* says if more references available.
*
* @param ref pointer to ref counter
* @return TRUE if no more references counted
*/
bool ref_put(refcount_t *ref);
#endif /*TYPES_H_*/
+2 -14
View File
@@ -154,24 +154,12 @@ static int print(FILE *stream, const struct printf_info *info,
}
}
/**
* arginfo handler in printf()
*/
static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(HOST_PRINTF_SPEC, print, print_arginfo);
register_printf_function(PRINTF_HOST, print, arginfo_ptr);
}
/**
@@ -179,7 +167,7 @@ static void __attribute__ ((constructor))print_register()
*/
static chunk_t get_address(private_host_t *this)
{
chunk_t address = CHUNK_INITIALIZER;
chunk_t address = chunk_empty;
switch (this->address.sa_family)
{
+1 -8
View File
@@ -36,14 +36,7 @@ typedef struct host_t host_t;
#include <arpa/inet.h>
#include <linux/xfrm.h>
#include <types.h>
/**
* printf() specifier to print a host.
* The specifier option '#' does include the port number, e.g.:
* printf("my host is %#H\n", my_host);
*/
#define HOST_PRINTF_SPEC 'H'
#include <library.h>
/**
* Differences between two hosts. They differ in
+11 -24
View File
@@ -30,7 +30,6 @@
#include <ctype.h>
#include <printf.h>
#include "definitions.h"
#include "identification.h"
#include <asn1/asn1.h>
@@ -251,8 +250,8 @@ static chunk_t sanitize_chunk(chunk_t chunk)
*/
static status_t init_rdn(chunk_t dn, chunk_t *rdn, chunk_t *attribute, bool *next)
{
*rdn = CHUNK_INITIALIZER;
*attribute = CHUNK_INITIALIZER;
*rdn = chunk_empty;
*attribute = chunk_empty;
/* a DN is a SEQUENCE OF RDNs */
if (*dn.ptr != ASN1_SEQUENCE)
@@ -285,8 +284,8 @@ static status_t get_next_rdn(chunk_t *rdn, chunk_t * attribute, chunk_t *oid, ch
chunk_t body;
/* initialize return values */
*oid = CHUNK_INITIALIZER;
*value = CHUNK_INITIALIZER;
*oid = chunk_empty;
*value = chunk_empty;
/* if all attributes have been parsed, get next rdn */
if (attribute->len <= 0)
@@ -569,8 +568,8 @@ static status_t atodn(char *src, chunk_t *dn)
UNKNOWN_OID = 4
} state_t;
chunk_t oid = CHUNK_INITIALIZER;
chunk_t name = CHUNK_INITIALIZER;
chunk_t oid = chunk_empty;
chunk_t name = chunk_empty;
chunk_t rdns[RDN_MAX];
int rdn_count = 0;
int dn_len = 0;
@@ -614,7 +613,7 @@ static status_t atodn(char *src, chunk_t *dn)
break;
}
/* reset oid and change state */
oid = CHUNK_INITIALIZER;
oid = chunk_empty;
state = SEARCH_NAME;
}
break;
@@ -658,7 +657,7 @@ static status_t atodn(char *src, chunk_t *dn)
status = OUT_OF_RES;
}
/* reset name and change state */
name = CHUNK_INITIALIZER;
name = chunk_empty;
state = SEARCH_OID;
}
break;
@@ -683,7 +682,7 @@ static status_t atodn(char *src, chunk_t *dn)
if (status != SUCCESS)
{
free(dn->ptr);
*dn = CHUNK_INITIALIZER;
*dn = chunk_empty;
}
return status;
}
@@ -911,24 +910,12 @@ static int print(FILE *stream, const struct printf_info *info,
}
}
/**
* arginfo handler in printf()
*/
static int print_arginfo(const struct printf_info *info, size_t n, int *argtypes)
{
if (n > 0)
{
argtypes[0] = PA_POINTER;
}
return 1;
}
/**
* register printf() handlers
*/
static void __attribute__ ((constructor))print_register()
{
register_printf_function(IDENTIFICATION_PRINTF_SPEC, print, print_arginfo);
register_printf_function(PRINTF_IDENTIFICATION, print, arginfo_ptr);
}
/**
@@ -971,7 +958,7 @@ static private_identification_t *identification_create(void)
this->public.equals = (bool (*) (identification_t*,identification_t*))equals_binary;
this->public.matches = (bool (*) (identification_t*,identification_t*,int*))matches_binary;
this->encoded = CHUNK_INITIALIZER;
this->encoded = chunk_empty;
return this;
}
+1 -5
View File
@@ -28,13 +28,9 @@
typedef enum id_type_t id_type_t;
typedef struct identification_t identification_t;
#include <types.h>
#include <library.h>
#define MAX_WILDCARDS 14
/**
* printf() specifier to print a identification.
*/
#define IDENTIFICATION_PRINTF_SPEC 'D'
/**
* @brief ID Types in a ID payload.
+1 -1
View File
@@ -37,8 +37,8 @@
#include "leak_detective.h"
#include <types.h>
#include <library.h>
#include <debug.h>
#ifdef LEAK_DETECTIVE
+2 -2
View File
@@ -52,7 +52,7 @@ bool extract_token(chunk_t *token, const char termination, chunk_t *src)
u_char *eot = memchr(src->ptr, termination, src->len);
/* initialize empty token */
*token = CHUNK_INITIALIZER;
*token = chunk_empty;
if (eot == NULL) /* termination symbol not found */
{
@@ -98,7 +98,7 @@ err_t extract_value(chunk_t *value, chunk_t *line)
if (!eat_whitespace(line))
{
*value = CHUNK_INITIALIZER;
*value = chunk_empty;
return NULL;
}
if (*line->ptr == '\'' || *line->ptr == '"')
+1 -1
View File
@@ -19,7 +19,7 @@
* for more details.
*/
#include <types.h>
#include <library.h>
/**
* @brief Eats whitespace
+9 -9
View File
@@ -669,15 +669,15 @@ static iterator_t *create_iterator(private_linked_list_t *linked_list, bool forw
{
private_iterator_t *this = malloc_thing(private_iterator_t);
this->public.get_count = (bool (*) (iterator_t *this)) get_list_count;
this->public.iterate = (bool (*) (iterator_t *this, void **value)) iterate;
this->public.set_iterator_hook = (void(*)(iterator_t *this, void*(*)(void*)))set_iterator_hook;
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->public.get_count = (int (*) (iterator_t*)) get_list_count;
this->public.iterate = (bool (*) (iterator_t*, void **value)) iterate;
this->public.set_iterator_hook = (void(*)(iterator_t*, void*(*)(void*)))set_iterator_hook;
this->public.insert_before = (void (*) (iterator_t*, void *item)) insert_before;
this->public.insert_after = (void (*) (iterator_t*, void *item)) insert_after;
this->public.replace = (status_t (*) (iterator_t*, void **, void *)) replace;
this->public.remove = (status_t (*) (iterator_t*)) remove_;
this->public.reset = (void (*) (iterator_t*)) iterator_reset;
this->public.destroy = (void (*) (iterator_t*)) iterator_destroy;
this->forward = forward;
this->current = NULL;
+1 -1
View File
@@ -28,7 +28,7 @@ typedef struct linked_list_t linked_list_t;
#include <pthread.h>
#include <types.h>
#include <library.h>
#include <utils/iterator.h>
/**
+1 -1
View File
@@ -26,7 +26,7 @@
typedef struct randomizer_t randomizer_t;
#include <types.h>
#include <library.h>
#ifndef DEV_RANDOM
/**