full support of ikev1 and ikev2 connection flags
[strongswan.git] / src / charon / config / connections / local_connection_store.c
1 /**
2 * @file local_connection_store.c
3 *
4 * @brief Implementation of local_connection_store_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 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 <string.h>
24
25 #include "local_connection_store.h"
26
27 #include <utils/linked_list.h>
28 #include <utils/logger_manager.h>
29
30
31 typedef struct private_local_connection_store_t private_local_connection_store_t;
32
33 /**
34 * Private data of an local_connection_store_t object
35 */
36 struct private_local_connection_store_t {
37
38 /**
39 * Public part
40 */
41 local_connection_store_t public;
42
43 /**
44 * stored connection
45 */
46 linked_list_t *connections;
47
48 /**
49 * Mutex to exclusivly access connection list
50 */
51 pthread_mutex_t mutex;
52
53 /**
54 * Assigned logger
55 */
56 logger_t *logger;
57 };
58
59
60 /**
61 * Implementation of connection_store_t.get_connection_by_hosts.
62 */
63 static connection_t *get_connection_by_hosts(private_local_connection_store_t *this, host_t *my_host, host_t *other_host)
64 {
65 typedef enum {
66 PRIO_UNDEFINED= 0x00,
67 PRIO_ADDR_ANY= 0x01,
68 PRIO_ADDR_MATCH= 0x02
69 } prio_t;
70
71 prio_t best_prio = PRIO_UNDEFINED;
72
73 iterator_t *iterator;
74 connection_t *candidate;
75 connection_t *found = NULL;
76
77 this->logger->log(this->logger, CONTROL|LEVEL1, "looking for connection for host pair %s...%s",
78 my_host->get_address(my_host), other_host->get_address(other_host));
79
80 pthread_mutex_lock(&(this->mutex));
81 iterator = this->connections->create_iterator(this->connections, TRUE);
82 /* determine closest matching connection */
83 while (iterator->has_next(iterator))
84 {
85 host_t *candidate_my_host;
86 host_t *candidate_other_host;
87
88 iterator->current(iterator, (void**)&candidate);
89
90 candidate_my_host = candidate->get_my_host(candidate);
91 candidate_other_host = candidate->get_other_host(candidate);
92
93 /* my_host addresses must match*/
94 if (my_host->ip_equals(my_host, candidate_my_host))
95 {
96 prio_t prio = PRIO_UNDEFINED;
97
98 /* exact match of peer host address or wildcard address? */
99 if (other_host->ip_equals(other_host, candidate_other_host))
100 {
101 prio |= PRIO_ADDR_MATCH;
102 }
103 else if (candidate_other_host->is_anyaddr(candidate_other_host))
104 {
105 prio |= PRIO_ADDR_ANY;
106 }
107
108 this->logger->log(this->logger, CONTROL|LEVEL2,
109 "candidate connection \"%s\": %s...%s (prio=%d)",
110 candidate->get_name(candidate),
111 candidate_my_host->get_address(candidate_my_host),
112 candidate_other_host->get_address(candidate_other_host),
113 prio);
114
115 if (prio > best_prio)
116 {
117 found = candidate;
118 best_prio = prio;
119 }
120 }
121 }
122 iterator->destroy(iterator);
123
124 if (found)
125 {
126 host_t *found_my_host = found->get_my_host(found);
127 host_t *found_other_host = found->get_other_host(found);
128
129 this->logger->log(this->logger, CONTROL,
130 "found matching connection \"%s\": %s...%s (prio=%d)",
131 found->get_name(found),
132 found_my_host->get_address(found_my_host),
133 found_other_host->get_address(found_other_host),
134 best_prio);
135
136 found = found->clone(found);
137 if (best_prio & PRIO_ADDR_ANY)
138 {
139 /* replace %any by the peer's address */
140 found->update_other_host(found, other_host->clone(other_host));
141 }
142 }
143 pthread_mutex_unlock(&(this->mutex));
144 return found;
145 }
146
147 /**
148 * Implementation of connection_store_t.get_connection_by_name.
149 */
150 static connection_t *get_connection_by_name(private_local_connection_store_t *this, char *name)
151 {
152 iterator_t *iterator;
153 connection_t *current, *found = NULL;
154
155 pthread_mutex_lock(&(this->mutex));
156 iterator = this->connections->create_iterator(this->connections, TRUE);
157 while (iterator->has_next(iterator))
158 {
159 iterator->current(iterator, (void**)&current);
160 if (strcmp(name, current->get_name(current)) == 0)
161 {
162 found = current->clone(current);
163 break;
164 }
165 }
166 iterator->destroy(iterator);
167 pthread_mutex_unlock(&(this->mutex));
168
169 return found;
170 }
171
172 /**
173 * Implementation of connection_store_t.delete_connection.
174 */
175 static status_t delete_connection(private_local_connection_store_t *this, char *name)
176 {
177 iterator_t *iterator;
178 connection_t *current;
179 bool found = FALSE;
180
181 pthread_mutex_lock(&(this->mutex));
182 iterator = this->connections->create_iterator(this->connections, TRUE);
183 while (iterator->has_next(iterator))
184 {
185 iterator->current(iterator, (void **)&current);
186 if (strcmp(current->get_name(current), name) == 0)
187 {
188 /* remove connection from list, and destroy it */
189 iterator->remove(iterator);
190 current->destroy(current);
191 found = TRUE;
192 break;
193 }
194 }
195 iterator->destroy(iterator);
196 pthread_mutex_unlock(&(this->mutex));
197 if (found)
198 {
199 return SUCCESS;
200 }
201 return NOT_FOUND;
202 }
203
204 /**
205 * Implementation of connection_store_t.add_connection.
206 */
207 static status_t add_connection(private_local_connection_store_t *this, connection_t *connection)
208 {
209 pthread_mutex_lock(&(this->mutex));
210 this->connections->insert_last(this->connections, connection);
211 pthread_mutex_unlock(&(this->mutex));
212 return SUCCESS;
213 }
214
215 /**
216 * Implementation of connection_store_t.log_connections.
217 */
218 void log_connections(private_local_connection_store_t *this, logger_t *logger, char *name)
219 {
220 iterator_t *iterator;
221 connection_t *current, *found = NULL;
222
223 if (logger == NULL)
224 {
225 logger = this->logger;
226 }
227
228 logger->log(logger, CONTROL, "Templates:");
229
230 pthread_mutex_lock(&(this->mutex));
231 iterator = this->connections->create_iterator(this->connections, TRUE);
232 while (iterator->has_next(iterator))
233 {
234 iterator->current(iterator, (void**)&current);
235 if (current->is_ikev2(current) && ( name == NULL || streq(name, current->get_name(current))))
236 {
237 host_t *my_host = current->get_my_host(current);
238 host_t *other_host = current->get_other_host(current);
239
240 logger->log(logger, CONTROL, " \"%s\": %s...%s",
241 current->get_name(current),
242 my_host->get_address(my_host),
243 other_host->get_address(other_host));
244 }
245 }
246 iterator->destroy(iterator);
247 pthread_mutex_unlock(&(this->mutex));
248 }
249
250 /**
251 * Implementation of connection_store_t.destroy.
252 */
253 static void destroy (private_local_connection_store_t *this)
254 {
255 connection_t *connection;
256
257 pthread_mutex_lock(&(this->mutex));
258 while (this->connections->remove_last(this->connections, (void**)&connection) == SUCCESS)
259 {
260 connection->destroy(connection);
261 }
262 this->connections->destroy(this->connections);
263 pthread_mutex_unlock(&(this->mutex));
264 free(this);
265 }
266
267 /**
268 * Described in header.
269 */
270 local_connection_store_t * local_connection_store_create(void)
271 {
272 private_local_connection_store_t *this = malloc_thing(private_local_connection_store_t);
273
274 this->public.connection_store.get_connection_by_hosts = (connection_t*(*)(connection_store_t*,host_t*,host_t*))get_connection_by_hosts;
275 this->public.connection_store.get_connection_by_name = (connection_t*(*)(connection_store_t*,char*))get_connection_by_name;
276 this->public.connection_store.delete_connection = (status_t(*)(connection_store_t*,char*))delete_connection;
277 this->public.connection_store.add_connection = (status_t(*)(connection_store_t*,connection_t*))add_connection;
278 this->public.connection_store.log_connections = (void(*)(connection_store_t*,logger_t*,char*))log_connections;
279 this->public.connection_store.destroy = (void(*)(connection_store_t*))destroy;
280
281 /* private variables */
282 this->connections = linked_list_create();
283 this->logger = logger_manager->get_logger(logger_manager, CONFIG);
284 pthread_mutex_init(&(this->mutex), NULL);
285
286 return (&this->public);
287 }