4 * @brief Implementation of ike_cfg_t.
9 * Copyright (C) 2005-2007 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 typedef struct private_ike_cfg_t private_ike_cfg_t
;
32 * Private data of an ike_cfg_t object
34 struct private_ike_cfg_t
{
42 * Number of references hold by others to this ike_cfg
47 * Address of local host
52 * Address of remote host
57 * should we send a certificate request?
62 * List of proposals to use
64 linked_list_t
*proposals
;
68 * Implementation of ike_cfg_t.certreq.
70 static bool send_certreq(private_ike_cfg_t
*this)
76 * Implementation of ike_cfg_t.get_my_host.
78 static host_t
*get_my_host (private_ike_cfg_t
*this)
84 * Implementation of ike_cfg_t.get_other_host.
86 static host_t
*get_other_host (private_ike_cfg_t
*this)
88 return this->other_host
;
92 * Implementation of ike_cfg_t.add_proposal.
94 static void add_proposal(private_ike_cfg_t
*this, proposal_t
*proposal
)
96 this->proposals
->insert_last(this->proposals
, proposal
);
100 * Implementation of ike_cfg_t.get_proposals.
102 static linked_list_t
* get_proposals(private_ike_cfg_t
*this)
104 iterator_t
*iterator
;
106 linked_list_t
*proposals
= linked_list_create();
108 iterator
= this->proposals
->create_iterator(this->proposals
, TRUE
);
109 while (iterator
->iterate(iterator
, (void**)¤t
))
111 current
= current
->clone(current
);
112 proposals
->insert_last(proposals
, (void*)current
);
114 iterator
->destroy(iterator
);
120 * Implementation of ike_cfg_t.select_proposal.
122 static proposal_t
*select_proposal(private_ike_cfg_t
*this,
123 linked_list_t
*proposals
)
125 iterator_t
*stored_iter
, *supplied_iter
;
126 proposal_t
*stored
, *supplied
, *selected
;
128 stored_iter
= this->proposals
->create_iterator(this->proposals
, TRUE
);
129 supplied_iter
= proposals
->create_iterator(proposals
, TRUE
);
131 /* compare all stored proposals with all supplied. Stored ones are preferred.*/
132 while (stored_iter
->iterate(stored_iter
, (void**)&stored
))
134 supplied_iter
->reset(supplied_iter
);
136 while (supplied_iter
->iterate(supplied_iter
, (void**)&supplied
))
138 selected
= stored
->select(stored
, supplied
);
141 /* they match, return */
142 stored_iter
->destroy(stored_iter
);
143 supplied_iter
->destroy(supplied_iter
);
148 /* no proposal match :-(, will result in a NO_PROPOSAL_CHOSEN... */
149 stored_iter
->destroy(stored_iter
);
150 supplied_iter
->destroy(supplied_iter
);
156 * Implementation of ike_cfg_t.get_dh_group.
158 static diffie_hellman_group_t
get_dh_group(private_ike_cfg_t
*this)
160 iterator_t
*iterator
;
161 proposal_t
*proposal
;
163 diffie_hellman_group_t dh_group
= MODP_NONE
;
165 iterator
= this->proposals
->create_iterator(this->proposals
, TRUE
);
166 while (iterator
->iterate(iterator
, (void**)&proposal
))
168 if (proposal
->get_algorithm(proposal
, DIFFIE_HELLMAN_GROUP
, &algo
))
170 dh_group
= algo
->algorithm
;
174 iterator
->destroy(iterator
);
179 * Implementation of ike_cfg_t.get_ref.
181 static void get_ref(private_ike_cfg_t
*this)
183 ref_get(&this->refcount
);
187 * Implementation of ike_cfg_t.destroy.
189 static void destroy(private_ike_cfg_t
*this)
191 if (ref_put(&this->refcount
))
193 this->proposals
->destroy_offset(this->proposals
,
194 offsetof(proposal_t
, destroy
));
195 this->my_host
->destroy(this->my_host
);
196 this->other_host
->destroy(this->other_host
);
202 * Described in header.
204 ike_cfg_t
*ike_cfg_create(bool certreq
, host_t
*my_host
, host_t
*other_host
)
206 private_ike_cfg_t
*this = malloc_thing(private_ike_cfg_t
);
208 /* public functions */
209 this->public.send_certreq
= (bool(*)(ike_cfg_t
*))send_certreq
;
210 this->public.get_my_host
= (host_t
*(*)(ike_cfg_t
*))get_my_host
;
211 this->public.get_other_host
= (host_t
*(*)(ike_cfg_t
*))get_other_host
;
212 this->public.add_proposal
= (void(*)(ike_cfg_t
*, proposal_t
*)) add_proposal
;
213 this->public.get_proposals
= (linked_list_t
*(*)(ike_cfg_t
*))get_proposals
;
214 this->public.select_proposal
= (proposal_t
*(*)(ike_cfg_t
*,linked_list_t
*))select_proposal
;
215 this->public.get_dh_group
= (diffie_hellman_group_t(*)(ike_cfg_t
*)) get_dh_group
;
216 this->public.get_ref
= (void(*)(ike_cfg_t
*))get_ref
;
217 this->public.destroy
= (void(*)(ike_cfg_t
*))destroy
;
219 /* private variables */
221 this->certreq
= certreq
;
222 this->my_host
= my_host
;
223 this->other_host
= other_host
;
225 this->proposals
= linked_list_create();
227 return &this->public;