Cast first argument for %.*s to int
[strongswan.git] / src / libstrongswan / library.c
index f66818b..30a7774 100644 (file)
@@ -1,13 +1,6 @@
-/**
- * @file library.c
- *
- * @brief Helper functions and definitions.
- *
- */
-
 /*
- * Copyright (C) 2005-2006 Martin Willi
- * Copyright (C) 2005 Jan Hutter
+ * Copyright (C) 2009 Tobias Brunner
+ * Copyright (C) 2008 Martin Willi
  * Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
  * for more details.
  */
 
-#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",
-       "NEED_MORE",
-);
+#include <stdlib.h>
+
+#include <utils/debug.h>
+#include <threading/thread.h>
+#include <utils/identification.h>
+#include <networking/host.h>
+#include <collections/hashtable.h>
+#include <utils/backtrace.h>
+#include <selectors/traffic_selector.h>
+
+#define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
+
+typedef struct private_library_t private_library_t;
 
 /**
- * Described in header.
+ * private data of library
  */
-void *clalloc(void * pointer, size_t size)
-{
-       void *data;
-       data = malloc(size);
-       
-       memcpy(data, pointer,size);
-       
-       return (data);
-}
+struct private_library_t {
+
+       /**
+        * public functions
+        */
+       library_t public;
+
+       /**
+        * Hashtable with registered objects (name => object)
+        */
+       hashtable_t *objects;
+
+       /**
+        * Integrity check failed?
+        */
+       bool integrity_failed;
+
+       /**
+        * Number of times we have been initialized
+        */
+       refcount_t ref;
+};
 
 /**
- * Described in header.
+ * library instance
  */
-void memxor(u_int8_t dest[], u_int8_t src[], size_t n)
+library_t *lib = NULL;
+
+/**
+ * Deinitialize library
+ */
+void library_deinit()
 {
-       size_t i;
-       for (i = 0; i < n; i++)
+       private_library_t *this = (private_library_t*)lib;
+       bool detailed;
+
+       if (!this || !ref_put(&this->ref))
+       {       /* have more users */
+               return;
+       }
+
+       detailed = lib->settings->get_bool(lib->settings,
+                                                               "libstrongswan.leak_detective.detailed", TRUE);
+
+       /* make sure the cache is clear before unloading plugins */
+       lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
+
+       this->public.scheduler->destroy(this->public.scheduler);
+       this->public.processor->destroy(this->public.processor);
+       this->public.plugins->destroy(this->public.plugins);
+       this->public.hosts->destroy(this->public.hosts);
+       this->public.settings->destroy(this->public.settings);
+       this->public.credmgr->destroy(this->public.credmgr);
+       this->public.creds->destroy(this->public.creds);
+       this->public.encoding->destroy(this->public.encoding);
+       this->public.crypto->destroy(this->public.crypto);
+       this->public.proposal->destroy(this->public.proposal);
+       this->public.fetcher->destroy(this->public.fetcher);
+       this->public.db->destroy(this->public.db);
+       this->public.printf_hook->destroy(this->public.printf_hook);
+       this->objects->destroy(this->objects);
+       if (this->public.integrity)
+       {
+               this->public.integrity->destroy(this->public.integrity);
+       }
+
+       if (lib->leak_detective)
        {
-               dest[i] ^= src[i];
+               lib->leak_detective->report(lib->leak_detective, detailed);
+               lib->leak_detective->destroy(lib->leak_detective);
        }
+
+       threads_deinit();
+       backtrace_deinit();
+
+       free(this);
+       lib = NULL;
 }
 
-/**
- * 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;
+METHOD(library_t, get, void*,
+       private_library_t *this, char *name)
+{
+       return this->objects->get(this->objects, name);
+}
 
-/**
- * Described in header.
- * 
- * TODO: May be implemented with atomic CPU instructions
- * instead of a mutex.
- */
-void ref_get(refcount_t *ref)
+METHOD(library_t, set, bool,
+       private_library_t *this, char *name, void *object)
 {
-       pthread_mutex_lock(&ref_mutex);
-       (*ref)++;
-       pthread_mutex_unlock(&ref_mutex);
+       if (object)
+       {
+               if (this->objects->get(this->objects, name))
+               {
+                       return FALSE;
+               }
+               this->objects->put(this->objects, name, object);
+               return TRUE;
+       }
+       return this->objects->remove(this->objects, name) != NULL;
 }
 
 /**
- * Described in header.
- * 
- * TODO: May be implemented with atomic CPU instructions
- * instead of a mutex.
+ * Hashtable hash function
  */
-bool ref_put(refcount_t *ref)
+static u_int hash(char *key)
 {
-       bool more_refs;
-       
-       pthread_mutex_lock(&ref_mutex);
-       more_refs = --(*ref);
-       pthread_mutex_unlock(&ref_mutex);
-       return !more_refs;
+       return chunk_hash(chunk_create(key, strlen(key)));
 }
 
 /**
- * output handler in printf() for time_t
+ * Hashtable equals function
  */
-static int print_time(FILE *stream, const struct printf_info *info,
-                                         const void *const *args)
+static bool equals(char *a, char *b)
 {
-       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);
+       return streq(a, b);
 }
 
-/**
- * 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)
+bool library_init(char *settings)
 {
-       char* unit = "second";
-       time_t *arg1, *arg2;
-       time_t delta;
-       
-       arg1 = *((time_t**)(args[0]));
-       if (info->alt)
-       {
-               arg2 = *((time_t**)(args[1]));
-               delta = abs(*arg1 - *arg2);
-       }
-       else
-       {
-               delta = *arg1;
-       }
+       private_library_t *this;
+       printf_hook_t *pfh;
 
-       if (delta > 2 * 60 * 60 * 24)
-       {
-               delta /= 60 * 60 * 24;
-               unit = "day";
-       }
-       else if (delta > 2 * 60 * 60)
-       {
-               delta /= 60 * 60;
-               unit = "hour";
+       if (lib)
+       {       /* already initialized, increase refcount */
+               this = (private_library_t*)lib;
+               ref_get(&this->ref);
+               return !this->integrity_failed;
        }
-       else if (delta > 2 * 60)
+
+       INIT(this,
+               .public = {
+                       .get = _get,
+                       .set = _set,
+               },
+               .ref = 1,
+       );
+       lib = &this->public;
+
+       backtrace_init();
+       threads_init();
+
+#ifdef LEAK_DETECTIVE
+       lib->leak_detective = leak_detective_create();
+#endif /* LEAK_DETECTIVE */
+
+       pfh = printf_hook_create();
+       this->public.printf_hook = pfh;
+
+       pfh->add_handler(pfh, 'b', mem_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
+                                        PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'B', chunk_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'H', host_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'N', enum_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
+                                        PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'T', time_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
+                                        PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'V', time_delta_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
+                                        PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'Y', identification_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
+       pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
+                                        PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
+
+       this->objects = hashtable_create((hashtable_hash_t)hash,
+                                                                        (hashtable_equals_t)equals, 4);
+       this->public.settings = settings_create(settings);
+       this->public.hosts = host_resolver_create();
+       this->public.proposal = proposal_keywords_create();
+       this->public.crypto = crypto_factory_create();
+       this->public.creds = credential_factory_create();
+       this->public.credmgr = credential_manager_create();
+       this->public.encoding = cred_encoding_create();
+       this->public.fetcher = fetcher_manager_create();
+       this->public.db = database_factory_create();
+       this->public.processor = processor_create();
+       this->public.scheduler = scheduler_create();
+       this->public.plugins = plugin_loader_create();
+
+       if (lib->settings->get_bool(lib->settings,
+                                                               "libstrongswan.integrity_test", FALSE))
        {
-               delta /= 60;
-               unit = "minute";
+#ifdef INTEGRITY_TEST
+               this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
+               if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
+               {
+                       DBG1(DBG_LIB, "integrity check of libstrongswan failed");
+                       this->integrity_failed = TRUE;
+               }
+#else /* !INTEGRITY_TEST */
+               DBG1(DBG_LIB, "integrity test enabled, but not supported");
+               this->integrity_failed = TRUE;
+#endif /* INTEGRITY_TEST */
        }
-       return fprintf(stream, "%d %s%s", delta, unit, (delta == 1)? "":"s");
-}
 
-/**
- * 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);
+       return !this->integrity_failed;
 }
+