- first attempt for connection loading and starting via "stroke"
[strongswan.git] / Source / charon / config / init_config.c
1 /**
2 * @file init_config.c
3 *
4 * @brief Implementation of init_config_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, 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 "init_config.h"
24
25 #include <utils/allocator.h>
26 #include <utils/linked_list.h>
27 #include <utils/logger.h>
28
29 typedef struct private_init_config_t private_init_config_t;
30
31 /**
32 * Private data of an init_config_t object
33 */
34 struct private_init_config_t {
35
36 /**
37 * Public part
38 */
39 init_config_t public;
40
41 /**
42 * Host information of my host.
43 */
44 host_t *my_host;
45
46 /**
47 * Host information of other host.
48 */
49 host_t *other_host;
50
51 /**
52 * Supported proposals
53 */
54 linked_list_t *proposals;
55 };
56
57 /**
58 * Implementation of init_config_t.get_my_host.
59 */
60 static host_t * get_my_host (private_init_config_t *this)
61 {
62 return this->my_host;
63 }
64
65 /**
66 * Implementation of init_config_t.get_other_host.
67 */
68 static host_t * get_other_host (private_init_config_t *this)
69 {
70 return this->other_host;
71 }
72
73 /**
74 * Implementation of init_config_t.get_my_host_clone.
75 */
76 static host_t * get_my_host_clone (private_init_config_t *this)
77 {
78 return this->my_host->clone(this->my_host);
79 }
80
81 /**
82 * Implementation of init_config_t.get_other_host_clone.
83 */
84 static host_t * get_other_host_clone (private_init_config_t *this)
85 {
86 return this->other_host->clone(this->other_host);
87 }
88
89 /**
90 * Implementation of init_config_t.get_proposals.
91 */
92 static linked_list_t* get_proposals (private_init_config_t *this)
93 {
94 return this->proposals;
95 }
96
97 /**
98 * Implementation of init_config_t.select_proposal.
99 */
100 static proposal_t *select_proposal(private_init_config_t *this, linked_list_t *proposals)
101 {
102 iterator_t *stored_iter, *supplied_iter;
103 proposal_t *stored, *supplied, *selected;
104
105 stored_iter = this->proposals->create_iterator(this->proposals, TRUE);
106 supplied_iter = proposals->create_iterator(proposals, TRUE);
107
108 /* compare all stored proposals with all supplied. Stored ones are preferred. */
109 while (stored_iter->has_next(stored_iter))
110 {
111 supplied_iter->reset(supplied_iter);
112 stored_iter->current(stored_iter, (void**)&stored);
113
114 while (supplied_iter->has_next(supplied_iter))
115 {
116 supplied_iter->current(supplied_iter, (void**)&supplied);
117 selected = stored->select(stored, supplied);
118 if (selected)
119 {
120 /* they match, return */
121 stored_iter->destroy(stored_iter);
122 supplied_iter->destroy(supplied_iter);
123 return selected;
124 }
125 }
126 }
127
128 /* no proposal match :-(, will result in a NO_PROPOSAL_CHOSEN... */
129 stored_iter->destroy(stored_iter);
130 supplied_iter->destroy(supplied_iter);
131
132 return NULL;
133 }
134
135 /**
136 * Implementation of init_config_t.add_proposal.
137 */
138 static void add_proposal (private_init_config_t *this, proposal_t *proposal)
139 {
140 this->proposals->insert_last(this->proposals, proposal);
141 }
142
143 /**
144 * Implementation of init_config_t.get_dh_group.
145 */
146 static diffie_hellman_group_t get_dh_group(private_init_config_t *this)
147 {
148 iterator_t *iterator;
149 proposal_t *proposal;
150 algorithm_t *algo;
151
152 iterator = this->proposals->create_iterator(this->proposals, TRUE);
153 while (iterator->has_next(iterator))
154 {
155 iterator->current(iterator, (void**)&proposal);
156 proposal->get_algorithm(proposal, IKE, DIFFIE_HELLMAN_GROUP, &algo);
157 if (algo)
158 {
159 iterator->destroy(iterator);
160 return algo->algorithm;
161 }
162 }
163 iterator->destroy(iterator);
164 return MODP_UNDEFINED;
165 }
166
167 /**
168 * Implementation of init_config_t.check_dh_group.
169 */
170 static bool check_dh_group(private_init_config_t *this, diffie_hellman_group_t dh_group)
171 {
172 iterator_t *prop_iter, *alg_iter;
173 proposal_t *proposal;
174 algorithm_t *algo;
175
176 prop_iter = this->proposals->create_iterator(this->proposals, TRUE);
177 while (prop_iter->has_next(prop_iter))
178 {
179 prop_iter->current(prop_iter, (void**)&proposal);
180 alg_iter = proposal->create_algorithm_iterator(proposal, IKE, DIFFIE_HELLMAN_GROUP);
181 while (alg_iter->has_next(alg_iter))
182 {
183 alg_iter->current(alg_iter, (void**)&algo);
184 if (algo->algorithm == dh_group)
185 {
186 prop_iter->destroy(prop_iter);
187 alg_iter->destroy(alg_iter);
188 return TRUE;
189 }
190 }
191 }
192 prop_iter->destroy(prop_iter);
193 alg_iter->destroy(alg_iter);
194 return FALSE;
195 }
196
197 /**
198 * Implementation of init_config_t.destroy.
199 */
200 static void destroy (private_init_config_t *this)
201 {
202 proposal_t *proposal;
203
204 while (this->proposals->remove_last(this->proposals, (void**)&proposal) == SUCCESS)
205 {
206 proposal->destroy(proposal);
207 }
208 this->proposals->destroy(this->proposals);
209
210 this->my_host->destroy(this->my_host);
211 this->other_host->destroy(this->other_host);
212 allocator_free(this);
213 }
214
215 /**
216 * Described in header.
217 */
218 init_config_t * init_config_create(host_t *me, host_t *other)
219 {
220 private_init_config_t *this = allocator_alloc_thing(private_init_config_t);
221
222 /* public functions */
223 this->public.get_my_host = (host_t*(*)(init_config_t*))get_my_host;
224 this->public.get_other_host = (host_t*(*)(init_config_t*))get_other_host;
225 this->public.get_my_host_clone = (host_t*(*)(init_config_t*))get_my_host_clone;
226 this->public.get_other_host_clone = (host_t*(*)(init_config_t*))get_other_host_clone;
227 this->public.get_proposals = (linked_list_t*(*)(init_config_t*))get_proposals;
228 this->public.select_proposal = (proposal_t*(*)(init_config_t*,linked_list_t*))select_proposal;
229 this->public.add_proposal = (void(*)(init_config_t*, proposal_t*)) add_proposal;
230 this->public.get_dh_group = (diffie_hellman_group_t(*)(init_config_t*)) get_dh_group;
231 this->public.check_dh_group = (bool(*)(init_config_t*,diffie_hellman_group_t)) check_dh_group;
232 this->public.destroy = (void(*)(init_config_t*))destroy;
233
234 /* private variables */
235 this->my_host = me;
236 this->other_host = other;
237
238 this->proposals = linked_list_create();
239
240 return (&this->public);
241 }