created class init_config_t encapsulating configuration issues of
[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
28 typedef struct private_init_config_t private_init_config_t;
29
30 /**
31 * Private data of an init_config_t object
32 */
33 struct private_init_config_t {
34
35 /**
36 * Public part
37 */
38 init_config_t public;
39
40 /**
41 * Host information of my host.
42 */
43 host_t *my_host;
44
45 /**
46 * Host information of other host.
47 */
48 host_t *other_host;
49
50 /**
51 * Supported proposals
52 */
53 linked_list_t *proposals;
54 };
55
56 /**
57 * Implementation of init_config_t.get_my_host.
58 */
59 static host_t * get_my_host (private_init_config_t *this)
60 {
61 return this->my_host->clone(this->my_host);
62 }
63
64 /**
65 * Implementation of init_config_t.get_other_host.
66 */
67 static host_t * get_other_host (private_init_config_t *this)
68 {
69 return this->other_host->clone(this->other_host);
70 }
71
72 /**
73 * Implementation of init_config_t.get_dh_group_number.
74 */
75 static diffie_hellman_group_t get_dh_group_number (private_init_config_t *this,size_t priority)
76 {
77 ike_proposal_t *ike_proposal;
78
79 if ((this->proposals->get_count(this->proposals) == 0) || (this->proposals->get_count(this->proposals) < priority))
80 {
81 return MODP_UNDEFINED;
82 }
83
84 this->proposals->get_at_position(this->proposals,(priority -1),(void **) &ike_proposal);
85
86 return (ike_proposal->diffie_hellman_group);
87 }
88
89 /**
90 * Implementation of init_config_t.get_proposals.
91 */
92 static size_t get_proposals (private_init_config_t *this,ike_proposal_t **proposals)
93 {
94 iterator_t *iterator;
95 ike_proposal_t *current_proposal;
96 int i = 0;
97 ike_proposal_t *proposal_array;
98
99 proposal_array = allocator_alloc(this->proposals->get_count(this->proposals) * sizeof(ike_proposal_t));
100
101 iterator = this->proposals->create_iterator(this->proposals,TRUE);
102
103 while (iterator->has_next(iterator))
104 {
105 iterator->current(iterator,(void **) &current_proposal);
106 proposal_array[i] = (*current_proposal);
107 i++;
108 }
109 iterator->destroy(iterator);
110
111 *proposals = proposal_array;
112 return this->proposals->get_count(this->proposals);
113 }
114
115 /**
116 * Implementation of init_config_t.select_proposal.
117 */
118 static status_t select_proposal (private_init_config_t *this, ike_proposal_t *proposals, size_t proposal_count, ike_proposal_t *selected_proposal)
119 {
120 iterator_t * my_iterator;
121 int i;
122 ike_proposal_t *my_current_proposal;
123
124 my_iterator = this->proposals->create_iterator(this->proposals,TRUE);
125
126
127 for (i = 0; i < proposal_count; i++)
128 {
129 my_iterator->reset(my_iterator);
130 while (my_iterator->has_next(my_iterator))
131 {
132 my_iterator->current(my_iterator,(void **) &my_current_proposal);
133
134 if (memcmp(my_current_proposal,&proposals[i],sizeof(ike_proposal_t)) == 0)
135 {
136 /* found a matching proposal */
137 *selected_proposal = *my_current_proposal;
138 my_iterator->destroy(my_iterator);
139 return SUCCESS;
140 }
141 }
142 }
143
144 my_iterator->destroy(my_iterator);
145 return NOT_FOUND;
146 }
147
148 /**
149 * Implementation of init_config_t.destroy.
150 */
151 static void add_proposal (private_init_config_t *this,size_t priority, ike_proposal_t proposal)
152 {
153 ike_proposal_t * new_proposal = allocator_alloc(sizeof(ike_proposal_t));
154
155 *new_proposal = proposal;
156
157
158 if (priority > this->proposals->get_count(this->proposals))
159 {
160 this->proposals->insert_last(this->proposals,new_proposal);
161 return;
162 }
163
164 this->proposals->insert_at_position(this->proposals,(priority - 1),new_proposal);
165 }
166
167 /**
168 * Implementation of init_config_t.destroy.
169 */
170 static void destroy (private_init_config_t *this)
171 {
172 ike_proposal_t *proposal;
173
174 while (this->proposals->get_count(this->proposals) > 0)
175 {
176 this->proposals->remove_first(this->proposals,(void **) &proposal);
177 allocator_free(proposal);
178 }
179 this->proposals->destroy(this->proposals);
180
181 this->my_host->destroy(this->my_host);
182 this->other_host->destroy(this->other_host);
183
184 allocator_free(this);
185 }
186
187 /**
188 * Described in header.
189 */
190 init_config_t * init_config_create(char * my_ip, char *other_ip, u_int16_t my_port, u_int16_t other_port)
191 {
192 private_init_config_t *this = allocator_alloc_thing(private_init_config_t);
193
194 /* public functions */
195 this->public.get_my_host = (host_t*(*)(init_config_t*))get_my_host;
196 this->public.get_other_host = (host_t*(*)(init_config_t*))get_other_host;
197 this->public.get_dh_group_number = (diffie_hellman_group_t (*)(init_config_t*,size_t))get_dh_group_number;
198 this->public.get_proposals = (size_t(*)(init_config_t*,ike_proposal_t**))get_proposals;
199 this->public.select_proposal = (status_t(*)(init_config_t*,ike_proposal_t*,size_t,ike_proposal_t*))select_proposal;
200 this->public.add_proposal = (void(*)(init_config_t*, size_t, ike_proposal_t)) add_proposal;
201 this->public.destroy = (void(*)(init_config_t*))destroy;
202
203 /* private variables */
204 this->my_host = host_create(AF_INET,my_ip, my_port);
205 this->other_host = host_create(AF_INET,other_ip, other_port);
206
207 this->proposals = linked_list_create();
208
209 return (&this->public);
210 }