- dead end implementation
[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_dh_group_number.
91 */
92 static diffie_hellman_group_t get_dh_group_number (private_init_config_t *this,size_t priority)
93 {
94 ike_proposal_t *ike_proposal;
95
96 if ((this->proposals->get_count(this->proposals) == 0) || (this->proposals->get_count(this->proposals) < priority))
97 {
98 return MODP_UNDEFINED;
99 }
100
101 this->proposals->get_at_position(this->proposals,(priority -1),(void **) &ike_proposal);
102
103 return (ike_proposal->diffie_hellman_group);
104 }
105
106 /**
107 * Implementation of init_config_t.get_proposals.
108 */
109 static size_t get_proposals (private_init_config_t *this,ike_proposal_t **proposals)
110 {
111 iterator_t *iterator;
112 ike_proposal_t *current_proposal;
113 int i = 0;
114 ike_proposal_t *proposal_array;
115
116 proposal_array = allocator_alloc(this->proposals->get_count(this->proposals) * sizeof(ike_proposal_t));
117
118 iterator = this->proposals->create_iterator(this->proposals,TRUE);
119
120 while (iterator->has_next(iterator))
121 {
122 iterator->current(iterator,(void **) &current_proposal);
123 proposal_array[i] = (*current_proposal);
124 i++;
125 }
126 iterator->destroy(iterator);
127
128 *proposals = proposal_array;
129 return this->proposals->get_count(this->proposals);
130 }
131
132 /**
133 * Implementation of init_config_t.select_proposal.
134 */
135 static status_t select_proposal (private_init_config_t *this, ike_proposal_t *proposals, size_t proposal_count, ike_proposal_t *selected_proposal)
136 {
137 iterator_t * my_iterator;
138 int i;
139 ike_proposal_t *my_current_proposal;
140
141 my_iterator = this->proposals->create_iterator(this->proposals,TRUE);
142
143
144 for (i = 0; i < proposal_count; i++)
145 {
146 my_iterator->reset(my_iterator);
147 while (my_iterator->has_next(my_iterator))
148 {
149 my_iterator->current(my_iterator,(void **) &my_current_proposal);
150
151 /* memcmp doesn't work here */
152 if ((proposals[i].encryption_algorithm == my_current_proposal->encryption_algorithm) &&
153 (proposals[i].encryption_algorithm_key_length == my_current_proposal->encryption_algorithm_key_length) &&
154 (proposals[i].integrity_algorithm == my_current_proposal->integrity_algorithm) &&
155 (proposals[i].integrity_algorithm_key_length == my_current_proposal->integrity_algorithm_key_length) &&
156 (proposals[i].pseudo_random_function == my_current_proposal->pseudo_random_function) &&
157 (proposals[i].pseudo_random_function_key_length == my_current_proposal->pseudo_random_function_key_length) &&
158 (proposals[i].diffie_hellman_group == my_current_proposal->diffie_hellman_group))
159 {
160 /* found a matching proposal */
161 *selected_proposal = *my_current_proposal;
162 my_iterator->destroy(my_iterator);
163 return SUCCESS;
164 }
165
166 }
167 }
168
169 my_iterator->destroy(my_iterator);
170 return NOT_FOUND;
171 }
172
173 /**
174 * Implementation of init_config_t.destroy.
175 */
176 static void add_proposal (private_init_config_t *this,size_t priority, ike_proposal_t proposal)
177 {
178 ike_proposal_t * new_proposal = allocator_alloc(sizeof(ike_proposal_t));
179 status_t status;
180
181 *new_proposal = proposal;
182
183
184 if (priority > this->proposals->get_count(this->proposals))
185 {
186 this->proposals->insert_last(this->proposals,new_proposal);
187 return;
188 }
189
190 status = this->proposals->insert_at_position(this->proposals,(priority - 1),new_proposal);
191
192 }
193
194 /**
195 * Implementation of init_config_t.destroy.
196 */
197 static void destroy (private_init_config_t *this)
198 {
199 ike_proposal_t *proposal;
200
201 while (this->proposals->get_count(this->proposals) > 0)
202 {
203 this->proposals->remove_first(this->proposals,(void **) &proposal);
204 allocator_free(proposal);
205 }
206 this->proposals->destroy(this->proposals);
207
208 this->my_host->destroy(this->my_host);
209 this->other_host->destroy(this->other_host);
210 allocator_free(this);
211 }
212
213 /**
214 * Described in header.
215 */
216 init_config_t * init_config_create(char * my_ip, char *other_ip, u_int16_t my_port, u_int16_t other_port)
217 {
218 private_init_config_t *this = allocator_alloc_thing(private_init_config_t);
219
220 /* public functions */
221 this->public.get_my_host = (host_t*(*)(init_config_t*))get_my_host;
222 this->public.get_other_host = (host_t*(*)(init_config_t*))get_other_host;
223 this->public.get_my_host_clone = (host_t*(*)(init_config_t*))get_my_host_clone;
224 this->public.get_other_host_clone = (host_t*(*)(init_config_t*))get_other_host_clone;
225 this->public.get_dh_group_number = (diffie_hellman_group_t (*)(init_config_t*,size_t))get_dh_group_number;
226 this->public.get_proposals = (size_t(*)(init_config_t*,ike_proposal_t**))get_proposals;
227 this->public.select_proposal = (status_t(*)(init_config_t*,ike_proposal_t*,size_t,ike_proposal_t*))select_proposal;
228 this->public.add_proposal = (void(*)(init_config_t*, size_t, ike_proposal_t)) add_proposal;
229 this->public.destroy = (void(*)(init_config_t*))destroy;
230
231 /* private variables */
232 this->my_host = host_create(AF_INET,my_ip, my_port);
233 this->other_host = host_create(AF_INET,other_ip, other_port);
234
235 this->proposals = linked_list_create();
236
237 return (&this->public);
238 }