aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/config/backends
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2007-04-10 06:01:03 +0000
committerMartin Willi <martin@strongswan.org>2007-04-10 06:01:03 +0000
commite0fe7651524d96abb5a7109d17bd8bd9da2e25fa (patch)
treedececc3d13c511dc9f560035b13789bb0d008c93 /src/charon/config/backends
parent1628cd6bda76f6b1fb33bb853497ce92bd829252 (diff)
downloadstrongswan-e0fe7651524d96abb5a7109d17bd8bd9da2e25fa.tar.bz2
strongswan-e0fe7651524d96abb5a7109d17bd8bd9da2e25fa.tar.xz
restructured file layout
new configuration structure: peer_cfg: configuration related to a peer (authenitcation, ...= ike_cfg: config to use for IKE setup (proposals) child_Cfg: config for CHILD_SA (proposals, traffic selectors) a peer_cfg has one ike_cfg and multiple child_cfg's stroke now uses fixed count of threads
Diffstat (limited to 'src/charon/config/backends')
-rw-r--r--src/charon/config/backends/backend.h77
-rw-r--r--src/charon/config/backends/local_backend.c231
-rw-r--r--src/charon/config/backends/local_backend.h82
3 files changed, 390 insertions, 0 deletions
diff --git a/src/charon/config/backends/backend.h b/src/charon/config/backends/backend.h
new file mode 100644
index 000000000..a054a7221
--- /dev/null
+++ b/src/charon/config/backends/backend.h
@@ -0,0 +1,77 @@
+/**
+ * @file backend.h
+ *
+ * @brief Interface backend_t.
+ *
+ */
+
+/*
+ * 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 BACKEND_H_
+#define BACKEND_H_
+
+typedef struct backend_t backend_t;
+
+#include <library.h>
+#include <config/ike_cfg.h>
+#include <config/peer_cfg.h>
+#include <utils/linked_list.h>
+
+
+/**
+ * @brief The interface for a configuration backend.
+ *
+ * @b Constructors:
+ * - implementations constructor, such as local_backend_create()
+ *
+ * @ingroup backends
+ */
+struct backend_t {
+
+ /**
+ * @brief Get an ike_cfg identified by two hosts.
+ *
+ * @param this calling object
+ * @param my_host address of own host
+ * @param other_host address of remote host
+ * @return matching ike_config, or NULL if none found
+ */
+ ike_cfg_t *(*get_ike_cfg)(backend_t *this,
+ host_t *my_host, host_t *other_host);
+
+ /**
+ * @brief Get a peer_cfg identified by two IDs.
+ *
+ * @param this calling object
+ * @param my_id own ID
+ * @param other_id peers ID
+ * @return matching peer_config, or NULL if none found
+ */
+ peer_cfg_t *(*get_peer_cfg)(backend_t *this,
+ identification_t *my_id,
+ identification_t *other_id);
+
+ /**
+ * @brief Get a peer_cfg identified by its name.
+ *
+ * @param this calling object
+ * @param name configs name
+ * @return matching peer_config, or NULL if none found
+ */
+ peer_cfg_t *(*get_peer_cfg_by_name)(backend_t *this, char *name);
+};
+
+#endif /* BACKEND_H_ */
diff --git a/src/charon/config/backends/local_backend.c b/src/charon/config/backends/local_backend.c
new file mode 100644
index 000000000..534c71c97
--- /dev/null
+++ b/src/charon/config/backends/local_backend.c
@@ -0,0 +1,231 @@
+/**
+ * @file local_backend.c
+ *
+ * @brief Implementation of local_backend_t.
+ *
+ */
+
+/*
+ * 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 <string.h>
+
+#include "local_backend.h"
+
+#include <daemon.h>
+#include <utils/linked_list.h>
+
+
+typedef struct private_local_backend_t private_local_backend_t;
+
+/**
+ * Private data of an local_backend_t object
+ */
+struct private_local_backend_t {
+
+ /**
+ * Public part
+ */
+ local_backend_t public;
+
+ /**
+ * list of configs
+ */
+ linked_list_t *cfgs;
+
+ /**
+ * Mutex to exclusivly access list
+ */
+ pthread_mutex_t mutex;
+};
+
+/**
+ * implements cfg_store_t.get_ike_cfg.
+ */
+static ike_cfg_t *get_ike_cfg(private_local_backend_t *this,
+ host_t *my_host, host_t *other_host)
+{
+ peer_cfg_t *peer;
+ ike_cfg_t *current, *found = NULL;
+ iterator_t *iterator;
+ host_t *my_candidate, *other_candidate;
+ enum {
+ MATCH_NONE = 0x00,
+ MATCH_ANY = 0x01,
+ MATCH_ME = 0x04,
+ MATCH_OTHER = 0x08,
+ } prio, best = MATCH_ANY;
+
+ DBG2(DBG_CFG, "looking for a config for %H...%H",
+ my_host, other_host);
+
+ iterator = this->cfgs->create_iterator_locked(this->cfgs, &this->mutex);
+ while (iterator->iterate(iterator, (void**)&peer))
+ {
+ prio = MATCH_NONE;
+ current = peer->get_ike_cfg(peer);
+ my_candidate = current->get_my_host(current);
+ other_candidate = current->get_other_host(current);
+
+ if (my_candidate->ip_equals(my_candidate, my_host))
+ {
+ prio += MATCH_ME;
+ }
+ else if (my_candidate->is_anyaddr(my_candidate))
+ {
+ prio += MATCH_ANY;
+ }
+
+ if (other_candidate->ip_equals(other_candidate, other_host))
+ {
+ prio += MATCH_OTHER;
+ }
+ else if (other_candidate->is_anyaddr(other_candidate))
+ {
+ prio += MATCH_ANY;
+ }
+
+ DBG2(DBG_CFG, " candidate '%s': %H...%H, prio %d",
+ peer->get_name(peer), my_candidate, other_candidate, prio);
+
+ /* we require at least two MATCH_ANY */
+ if (prio > best)
+ {
+ best = prio;
+ found = current;
+ }
+ }
+ if (found)
+ {
+ found->get_ref(found);
+ }
+ iterator->destroy(iterator);
+ return found;
+}
+
+/**
+ * implements cfg_store_t.get_peer.
+ */
+static peer_cfg_t *get_peer_cfg(private_local_backend_t *this,
+ identification_t *my_id,
+ identification_t *other_id)
+{
+ peer_cfg_t *current, *found = NULL;
+ iterator_t *iterator;
+ identification_t *my_candidate, *other_candidate;
+ int wc1, wc2, total, best = MAX_WILDCARDS;
+
+ DBG2(DBG_CFG, "looking for a config for %D...%D", my_id, other_id);
+
+ iterator = this->cfgs->create_iterator_locked(this->cfgs, &this->mutex);
+ while (iterator->iterate(iterator, (void**)&current))
+ {
+ my_candidate = current->get_my_id(current);
+ other_candidate = current->get_other_id(current);
+
+ if (my_candidate->matches(my_candidate, my_id, &wc1) &&
+ other_id->matches(other_id, other_candidate, &wc2))
+ {
+ total = wc1 + wc2;
+
+ DBG2(DBG_CFG, " candidate '%s': %D...%D, wildcards %d",
+ current->get_name(current), my_candidate, other_candidate,
+ total);
+
+ if (total < best)
+ {
+ found = current;
+ best = total;
+ }
+ }
+ }
+ if (found)
+ {
+ found->get_ref(found);
+ }
+ iterator->destroy(iterator);
+ return found;
+}
+
+/**
+ * implements cfg_store_t.get_peer_by_name.
+ */
+static peer_cfg_t *get_peer_cfg_by_name(private_local_backend_t *this,
+ char *name)
+{
+ iterator_t *iterator;
+ peer_cfg_t *current, *found = NULL;
+
+ iterator = this->cfgs->create_iterator(this->cfgs, TRUE);
+ while (iterator->iterate(iterator, (void**)&current))
+ {
+ if (streq(current->get_name(current), name))
+ {
+ found = current;
+ found->get_ref(found);
+ break;
+ }
+ }
+ iterator->destroy(iterator);
+ return found;
+}
+
+/**
+ * Implementation of local_backend_t.create_peer_cfg_iterator.
+ */
+static iterator_t* create_peer_cfg_iterator(private_local_backend_t *this)
+{
+ return this->cfgs->create_iterator_locked(this->cfgs, &this->mutex);
+}
+
+/**
+ * Implementation of local_backend_t.add_peer_cfg.
+ */
+static void add_peer_cfg(private_local_backend_t *this, peer_cfg_t *config)
+{
+ pthread_mutex_lock(&this->mutex);
+ this->cfgs->insert_last(this->cfgs, config);
+ pthread_mutex_unlock(&this->mutex);
+}
+
+/**
+ * Implementation of local_backend_t.destroy.
+ */
+static void destroy(private_local_backend_t *this)
+{
+ this->cfgs->destroy_offset(this->cfgs, offsetof(peer_cfg_t, destroy));
+ free(this);
+}
+
+/**
+ * Described in header.
+ */
+local_backend_t *local_backend_create(void)
+{
+ private_local_backend_t *this = malloc_thing(private_local_backend_t);
+
+ this->public.backend.get_ike_cfg = (ike_cfg_t*(*)(backend_t*, host_t *, host_t *))get_ike_cfg;
+ this->public.backend.get_peer_cfg = (peer_cfg_t*(*)(backend_t*, identification_t *, identification_t *))get_peer_cfg;
+ this->public.backend.get_peer_cfg_by_name = (peer_cfg_t*(*)(backend_t*, char *))get_peer_cfg_by_name;
+ this->public.create_peer_cfg_iterator = (iterator_t*(*)(local_backend_t*))create_peer_cfg_iterator;
+ this->public.add_peer_cfg = (void(*)(local_backend_t*, peer_cfg_t *))add_peer_cfg;
+ this->public.destroy = (void(*)(local_backend_t*))destroy;
+
+ /* private variables */
+ this->cfgs = linked_list_create();
+ pthread_mutex_init(&this->mutex, NULL);
+
+ return (&this->public);
+}
diff --git a/src/charon/config/backends/local_backend.h b/src/charon/config/backends/local_backend.h
new file mode 100644
index 000000000..041725ae4
--- /dev/null
+++ b/src/charon/config/backends/local_backend.h
@@ -0,0 +1,82 @@
+/**
+ * @file local_backend.h
+ *
+ * @brief Interface of local_backend_t.
+ *
+ */
+
+/*
+ * Copyright (C) 2007 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 LOCAL_BACKEND_H_
+#define LOCAL_BACKEND_H_
+
+typedef struct local_backend_t local_backend_t;
+
+#include <library.h>
+#include <config/backends/backend.h>
+
+/**
+ * @brief An in-memory backend to store configuration information.
+ *
+ * The local_backend_t stores the configuration in a simple list. Additional
+ * to the backend_t functionality, it adds the modification (add/remove).
+ *
+ * @b Constructors:
+ * - local_backend_create()
+ *
+ * @ingroup backends
+ */
+struct local_backend_t {
+
+ /**
+ * Implements backend_t interface
+ */
+ backend_t backend;
+
+ /**
+ * @brief Add a peer_config to the backend.
+ *
+ * @param this calling object
+ * @param config peer_config to add to the backend
+ */
+ void (*add_peer_cfg)(local_backend_t *this, peer_cfg_t *config);
+
+ /**
+ * @brief Create an iterator over all peer configs.
+ *
+ * @param this calling object
+ * @return iterator over peer configs
+ */
+ iterator_t* (*create_peer_cfg_iterator)(local_backend_t *this);
+
+ /**
+ * @brief Destroy a local backend.
+ *
+ * @param this calling object
+ */
+ void (*destroy)(local_backend_t *this);
+};
+
+/**
+ * @brief Creates a local_backend_t instance.
+ *
+ * @return local_backend instance.
+ *
+ * @ingroup config
+ */
+local_backend_t *local_backend_create(void);
+
+#endif /* LOCAL_BACKEND_H_ */