fixed CHILD_SA proposal selection when not using DH exchange
[strongswan.git] / src / charon / config / cfg_store.c
1 /**
2 * @file cfg_store.c
3 *
4 * @brief Implementation of cfg_store_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <pthread.h>
24
25 #include "cfg_store.h"
26
27 #include <library.h>
28 #include <utils/linked_list.h>
29
30
31 typedef struct private_cfg_store_t private_cfg_store_t;
32
33 /**
34 * Private data of an cfg_store_t object.
35 */
36 struct private_cfg_store_t {
37
38 /**
39 * Public part of cfg_store_t object.
40 */
41 cfg_store_t public;
42
43 /**
44 * list of registered backends
45 */
46 linked_list_t *backends;
47
48 /**
49 * mutex to lock backend list
50 */
51 pthread_mutex_t mutex;
52 };
53
54 /**
55 * implements cfg_store_t.get_ike.
56 */
57 static ike_cfg_t *get_ike_cfg(private_cfg_store_t *this,
58 host_t *my_host, host_t *other_host)
59 {
60 backend_t *backend;
61 ike_cfg_t *config = NULL;
62 iterator_t *iterator = this->backends->create_iterator_locked(
63 this->backends, &this->mutex);
64 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
65 {
66 config = backend->get_ike_cfg(backend, my_host, other_host);
67 }
68 iterator->destroy(iterator);
69 return config;
70 }
71
72 /**
73 * implements cfg_store_t.get_peer.
74 */
75 static peer_cfg_t *get_peer_cfg(private_cfg_store_t *this,
76 identification_t *my_id,
77 identification_t *other_id)
78 {
79 backend_t *backend;
80 peer_cfg_t *config = NULL;
81 iterator_t *iterator = this->backends->create_iterator_locked(
82 this->backends, &this->mutex);
83 while (config == NULL && iterator->iterate(iterator, (void**)&backend))
84 {
85 config = backend->get_peer_cfg(backend, my_id, other_id);
86 }
87 iterator->destroy(iterator);
88 return config;
89 }
90
91 /**
92 * implements cfg_store_t.register_backend.
93 */
94 static void register_backend(private_cfg_store_t *this, backend_t *backend)
95 {
96 pthread_mutex_lock(&this->mutex);
97 this->backends->insert_last(this->backends, backend);
98 pthread_mutex_unlock(&this->mutex);
99 }
100
101 /**
102 * implements cfg_store_t.unregister_backend.
103 */
104 static void unregister_backend(private_cfg_store_t *this, backend_t *backend)
105 {
106 backend_t *current;
107 iterator_t *iterator = this->backends->create_iterator_locked(
108 this->backends, &this->mutex);
109 while (iterator->iterate(iterator, (void**)&current))
110 {
111 if (backend == current)
112 {
113 iterator->remove(iterator);
114 break;
115 }
116 }
117 iterator->destroy(iterator);
118 }
119
120 /**
121 * Implementation of cfg_store_t.destroy.
122 */
123 static void destroy(private_cfg_store_t *this)
124 {
125 this->backends->destroy(this->backends);
126 free(this);
127 }
128
129 /*
130 * Described in header-file
131 */
132 cfg_store_t *cfg_store_create()
133 {
134 private_cfg_store_t *this = malloc_thing(private_cfg_store_t);
135
136 this->public.get_ike_cfg = (ike_cfg_t*(*)(cfg_store_t*, host_t *, host_t *))get_ike_cfg;
137 this->public.get_peer_cfg = (peer_cfg_t*(*)(cfg_store_t*, identification_t *, identification_t *))get_peer_cfg;
138 this->public.register_backend = (void(*)(cfg_store_t*, backend_t *))register_backend;
139 this->public.unregister_backend = (void(*)(cfg_store_t*, backend_t *))unregister_backend;
140 this->public.destroy = (void(*)(cfg_store_t*))destroy;
141
142 this->backends = linked_list_create();
143 pthread_mutex_init(&this->mutex, NULL);
144
145 return &this->public;
146 }