4 * @brief Implementation of init_config_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "init_config.h"
25 #include <utils/allocator.h>
26 #include <utils/linked_list.h>
27 #include <utils/logger.h>
29 typedef struct private_init_config_t private_init_config_t
;
32 * Private data of an init_config_t object
34 struct private_init_config_t
{
42 * Host information of my host.
47 * Host information of other host.
54 linked_list_t
*proposals
;
58 * Implementation of init_config_t.get_my_host.
60 static host_t
* get_my_host (private_init_config_t
*this)
66 * Implementation of init_config_t.get_other_host.
68 static host_t
* get_other_host (private_init_config_t
*this)
70 return this->other_host
;
74 * Implementation of init_config_t.get_my_host_clone.
76 static host_t
* get_my_host_clone (private_init_config_t
*this)
78 return this->my_host
->clone(this->my_host
);
82 * Implementation of init_config_t.get_other_host_clone.
84 static host_t
* get_other_host_clone (private_init_config_t
*this)
86 return this->other_host
->clone(this->other_host
);
90 * Implementation of init_config_t.get_dh_group_number.
92 static diffie_hellman_group_t
get_dh_group_number (private_init_config_t
*this,size_t priority
)
94 ike_proposal_t
*ike_proposal
;
96 if ((this->proposals
->get_count(this->proposals
) == 0) || (this->proposals
->get_count(this->proposals
) < priority
))
98 return MODP_UNDEFINED
;
101 this->proposals
->get_at_position(this->proposals
,(priority
-1),(void **) &ike_proposal
);
103 return (ike_proposal
->diffie_hellman_group
);
107 * Implementation of init_config_t.get_proposals.
109 static size_t get_proposals (private_init_config_t
*this,ike_proposal_t
**proposals
)
111 iterator_t
*iterator
;
112 ike_proposal_t
*current_proposal
;
114 ike_proposal_t
*proposal_array
;
116 proposal_array
= allocator_alloc(this->proposals
->get_count(this->proposals
) * sizeof(ike_proposal_t
));
118 iterator
= this->proposals
->create_iterator(this->proposals
,TRUE
);
120 while (iterator
->has_next(iterator
))
122 iterator
->current(iterator
,(void **) ¤t_proposal
);
123 proposal_array
[i
] = (*current_proposal
);
126 iterator
->destroy(iterator
);
128 *proposals
= proposal_array
;
129 return this->proposals
->get_count(this->proposals
);
133 * Implementation of init_config_t.select_proposal.
135 static status_t
select_proposal (private_init_config_t
*this, ike_proposal_t
*proposals
, size_t proposal_count
, ike_proposal_t
*selected_proposal
)
137 iterator_t
* my_iterator
;
139 ike_proposal_t
*my_current_proposal
;
141 my_iterator
= this->proposals
->create_iterator(this->proposals
,TRUE
);
144 for (i
= 0; i
< proposal_count
; i
++)
146 my_iterator
->reset(my_iterator
);
147 while (my_iterator
->has_next(my_iterator
))
149 my_iterator
->current(my_iterator
,(void **) &my_current_proposal
);
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
))
160 /* found a matching proposal */
161 *selected_proposal
= *my_current_proposal
;
162 my_iterator
->destroy(my_iterator
);
169 my_iterator
->destroy(my_iterator
);
174 * Implementation of init_config_t.destroy.
176 static void add_proposal (private_init_config_t
*this,size_t priority
, ike_proposal_t proposal
)
178 ike_proposal_t
* new_proposal
= allocator_alloc(sizeof(ike_proposal_t
));
181 *new_proposal
= proposal
;
184 if (priority
> this->proposals
->get_count(this->proposals
))
186 this->proposals
->insert_last(this->proposals
,new_proposal
);
190 status
= this->proposals
->insert_at_position(this->proposals
,(priority
- 1),new_proposal
);
195 * Implementation of init_config_t.destroy.
197 static void destroy (private_init_config_t
*this)
199 ike_proposal_t
*proposal
;
201 while (this->proposals
->get_count(this->proposals
) > 0)
203 this->proposals
->remove_first(this->proposals
,(void **) &proposal
);
204 allocator_free(proposal
);
206 this->proposals
->destroy(this->proposals
);
208 this->my_host
->destroy(this->my_host
);
209 this->other_host
->destroy(this->other_host
);
210 allocator_free(this);
214 * Described in header.
216 init_config_t
* init_config_create(char * my_ip
, char *other_ip
, u_int16_t my_port
, u_int16_t other_port
)
218 private_init_config_t
*this = allocator_alloc_thing(private_init_config_t
);
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
;
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
);
235 this->proposals
= linked_list_create();
237 return (&this->public);