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>
28 typedef struct private_init_config_t private_init_config_t
;
31 * Private data of an init_config_t object
33 struct private_init_config_t
{
41 * Host information of my host.
46 * Host information of other host.
53 linked_list_t
*proposals
;
57 * Implementation of init_config_t.get_my_host.
59 static host_t
* get_my_host (private_init_config_t
*this)
61 return this->my_host
->clone(this->my_host
);
65 * Implementation of init_config_t.get_other_host.
67 static host_t
* get_other_host (private_init_config_t
*this)
69 return this->other_host
->clone(this->other_host
);
73 * Implementation of init_config_t.get_dh_group_number.
75 static diffie_hellman_group_t
get_dh_group_number (private_init_config_t
*this,size_t priority
)
77 ike_proposal_t
*ike_proposal
;
79 if ((this->proposals
->get_count(this->proposals
) == 0) || (this->proposals
->get_count(this->proposals
) < priority
))
81 return MODP_UNDEFINED
;
84 this->proposals
->get_at_position(this->proposals
,(priority
-1),(void **) &ike_proposal
);
86 return (ike_proposal
->diffie_hellman_group
);
90 * Implementation of init_config_t.get_proposals.
92 static size_t get_proposals (private_init_config_t
*this,ike_proposal_t
**proposals
)
95 ike_proposal_t
*current_proposal
;
97 ike_proposal_t
*proposal_array
;
99 proposal_array
= allocator_alloc(this->proposals
->get_count(this->proposals
) * sizeof(ike_proposal_t
));
101 iterator
= this->proposals
->create_iterator(this->proposals
,TRUE
);
103 while (iterator
->has_next(iterator
))
105 iterator
->current(iterator
,(void **) ¤t_proposal
);
106 proposal_array
[i
] = (*current_proposal
);
109 iterator
->destroy(iterator
);
111 *proposals
= proposal_array
;
112 return this->proposals
->get_count(this->proposals
);
116 * Implementation of init_config_t.select_proposal.
118 static status_t
select_proposal (private_init_config_t
*this, ike_proposal_t
*proposals
, size_t proposal_count
, ike_proposal_t
*selected_proposal
)
120 iterator_t
* my_iterator
;
122 ike_proposal_t
*my_current_proposal
;
124 my_iterator
= this->proposals
->create_iterator(this->proposals
,TRUE
);
127 for (i
= 0; i
< proposal_count
; i
++)
129 my_iterator
->reset(my_iterator
);
130 while (my_iterator
->has_next(my_iterator
))
132 my_iterator
->current(my_iterator
,(void **) &my_current_proposal
);
134 if (memcmp(my_current_proposal
,&proposals
[i
],sizeof(ike_proposal_t
)) == 0)
136 /* found a matching proposal */
137 *selected_proposal
= *my_current_proposal
;
138 my_iterator
->destroy(my_iterator
);
144 my_iterator
->destroy(my_iterator
);
149 * Implementation of init_config_t.destroy.
151 static void add_proposal (private_init_config_t
*this,size_t priority
, ike_proposal_t proposal
)
153 ike_proposal_t
* new_proposal
= allocator_alloc(sizeof(ike_proposal_t
));
155 *new_proposal
= proposal
;
158 if (priority
> this->proposals
->get_count(this->proposals
))
160 this->proposals
->insert_last(this->proposals
,new_proposal
);
164 this->proposals
->insert_at_position(this->proposals
,(priority
- 1),new_proposal
);
168 * Implementation of init_config_t.destroy.
170 static void destroy (private_init_config_t
*this)
172 ike_proposal_t
*proposal
;
174 while (this->proposals
->get_count(this->proposals
) > 0)
176 this->proposals
->remove_first(this->proposals
,(void **) &proposal
);
177 allocator_free(proposal
);
179 this->proposals
->destroy(this->proposals
);
181 this->my_host
->destroy(this->my_host
);
182 this->other_host
->destroy(this->other_host
);
184 allocator_free(this);
188 * Described in header.
190 init_config_t
* init_config_create(char * my_ip
, char *other_ip
, u_int16_t my_port
, u_int16_t other_port
)
192 private_init_config_t
*this = allocator_alloc_thing(private_init_config_t
);
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
;
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
);
207 this->proposals
= linked_list_create();
209 return (&this->public);