summaryrefslogtreecommitdiff
path: root/src/libstrongswan/library.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstrongswan/library.c')
-rw-r--r--src/libstrongswan/library.c222
1 files changed, 70 insertions, 152 deletions
diff --git a/src/libstrongswan/library.c b/src/libstrongswan/library.c
index f66818bc2..cc3ee6bd6 100644
--- a/src/libstrongswan/library.c
+++ b/src/libstrongswan/library.c
@@ -1,13 +1,5 @@
-/**
- * @file library.c
- *
- * @brief Helper functions and definitions.
- *
- */
-
/*
- * Copyright (C) 2005-2006 Martin Willi
- * Copyright (C) 2005 Jan Hutter
+ * Copyright (C) 2008 Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
@@ -19,175 +11,101 @@
* 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.
+ *
+ * $Id: library.c 3750 2008-04-04 11:38:16Z martin $
*/
-#include <string.h>
-#include <time.h>
-#include <stdio.h>
-#include <stdarg.h>
-#include <pthread.h>
-
#include "library.h"
-#include <printf_hook.h>
+#include <stdlib.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",
- "NEED_MORE",
-);
+#include <utils.h>
+#include <chunk.h>
+#include <utils/identification.h>
+#include <utils/host.h>
+#ifdef LEAK_DETECTIVE
+#include <utils/leak_detective.h>
+#endif
-/**
- * Described in header.
- */
-void *clalloc(void * pointer, size_t size)
-{
- void *data;
- data = malloc(size);
-
- memcpy(data, pointer,size);
-
- return (data);
-}
+typedef struct private_library_t private_library_t;
/**
- * Described in header.
+ * private data of library
*/
-void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
-{
- size_t i;
- for (i = 0; i < n; i++)
- {
- dest[i] ^= src[i];
- }
-}
+struct private_library_t {
-/**
- * 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;
+ /**
+ * public functions
+ */
+ library_t public;
-/**
- * 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);
-}
+#ifdef LEAK_DETECTIVE
+ /**
+ * Memory leak detective, if enabled
+ */
+ leak_detective_t *detective;
+#endif /* LEAK_DETECTIVE */
+};
/**
- * Described in header.
- *
- * TODO: May be implemented with atomic CPU instructions
- * instead of a mutex.
+ * library instance
*/
-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;
-}
+library_t *lib;
/**
- * output handler in printf() for time_t
+ * Implementation of library_t.destroy
*/
-static int print_time(FILE *stream, const struct printf_info *info,
- const void *const *args)
+void library_deinit()
{
- 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;
+ private_library_t *this = (private_library_t*)lib;
+
+ this->public.plugins->destroy(this->public.plugins);
+ this->public.settings->destroy(this->public.settings);
+ this->public.creds->destroy(this->public.creds);
+ this->public.crypto->destroy(this->public.crypto);
+ this->public.fetcher->destroy(this->public.fetcher);
+ this->public.db->destroy(this->public.db);
+ this->public.printf_hook->destroy(this->public.printf_hook);
- 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
+#ifdef LEAK_DETECTIVE
+ if (this->detective)
{
- localtime_r(time, &t);
+ this->detective->destroy(this->detective);
}
- 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);
+#endif /* LEAK_DETECTIVE */
+ free(this);
+ lib = NULL;
}
-/**
- * output handler in printf() for time deltas
+/*
+ * see header file
*/
-static int print_time_delta(FILE *stream, const struct printf_info *info,
- const void *const *args)
+void library_init(char *settings)
{
- char* unit = "second";
- time_t *arg1, *arg2;
- time_t delta;
+ printf_hook_t *pfh;
+ private_library_t *this = malloc_thing(private_library_t);
+ lib = &this->public;
- arg1 = *((time_t**)(args[0]));
- if (info->alt)
- {
- arg2 = *((time_t**)(args[1]));
- delta = abs(*arg1 - *arg2);
- }
- else
- {
- delta = *arg1;
- }
+#ifdef LEAK_DETECTIVE
+ this->detective = leak_detective_create();
+#endif /* LEAK_DETECTIVE */
- if (delta > 2 * 60 * 60 * 24)
- {
- delta /= 60 * 60 * 24;
- unit = "day";
- }
- else if (delta > 2 * 60 * 60)
- {
- delta /= 60 * 60;
- unit = "hour";
- }
- else if (delta > 2 * 60)
- {
- delta /= 60;
- unit = "minute";
- }
- return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
+ pfh = printf_hook_create();
+ this->public.printf_hook = pfh;
+
+ pfh->add_handler(pfh, 'b', mem_get_printf_hooks());
+ pfh->add_handler(pfh, 'B', chunk_get_printf_hooks());
+ pfh->add_handler(pfh, 'D', identification_get_printf_hooks());
+ pfh->add_handler(pfh, 'H', host_get_printf_hooks());
+ pfh->add_handler(pfh, 'N', enum_get_printf_hooks());
+ pfh->add_handler(pfh, 'T', time_get_printf_hooks());
+ pfh->add_handler(pfh, 'V', time_delta_get_printf_hooks());
+
+ this->public.crypto = crypto_factory_create();
+ this->public.creds = credential_factory_create();
+ this->public.fetcher = fetcher_manager_create();
+ this->public.db = database_factory_create();
+ this->public.settings = settings_create(settings);
+ this->public.plugins = plugin_loader_create();
}
-/**
- * register printf() handlers for time_t
- */
-static void __attribute__ ((constructor))print_register()
-{
- register_printf_function(PRINTF_TIME, print_time, arginfo_ptr_alt_ptr_int);
- register_printf_function(PRINTF_TIME_DELTA, print_time_delta, arginfo_ptr_alt_ptr_ptr);
-}