2 * Copyright (C) 2007 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "mediation_manager.h"
21 #include <utils/mutex.h>
22 #include <utils/linked_list.h>
23 #include <processing/jobs/mediation_job.h>
26 typedef struct peer_t peer_t
;
29 * An entry in the linked list.
35 /** sa id of the peer, NULL if offline */
36 ike_sa_id_t
*ike_sa_id
;
38 /** list of peer ids that reuested this peer */
39 linked_list_t
*requested_by
;
43 * Implementation of peer_t.destroy.
45 static void peer_destroy(peer_t
*this)
48 DESTROY_IF(this->ike_sa_id
);
49 this->requested_by
->destroy_offset(this->requested_by
, offsetof(identification_t
, destroy
));
54 * Creates a new entry for the list.
56 static peer_t
*peer_create(identification_t
*id
, ike_sa_id_t
* ike_sa_id
)
58 peer_t
*this = malloc_thing(peer_t
);
60 /* clone everything */
61 this->id
= id
->clone(id
);
62 this->ike_sa_id
= ike_sa_id ? ike_sa_id
->clone(ike_sa_id
) : NULL
;
63 this->requested_by
= linked_list_create();
69 typedef struct private_mediation_manager_t private_mediation_manager_t
;
72 * Additional private members of mediation_manager_t.
74 struct private_mediation_manager_t
{
76 * Public interface of mediation_manager_t.
78 mediation_manager_t
public;
81 * Lock for exclusivly accessing the manager.
86 * Linked list with state entries.
92 * Registers a peer's ID at another peer, if it is not yet registered
94 static void register_peer(peer_t
*peer
, identification_t
*peer_id
)
97 identification_t
*current
;
99 iterator
= peer
->requested_by
->create_iterator(peer
->requested_by
, TRUE
);
100 while (iterator
->iterate(iterator
, (void**)¤t
))
102 if (peer_id
->equals(peer_id
, current
))
104 iterator
->destroy(iterator
);
108 iterator
->destroy(iterator
);
110 peer
->requested_by
->insert_last(peer
->requested_by
, peer_id
->clone(peer_id
));
114 * Get a peer_t object by a peer's id
116 static status_t
get_peer_by_id(private_mediation_manager_t
*this,
117 identification_t
*id
, peer_t
**peer
)
119 iterator_t
*iterator
;
121 status_t status
= NOT_FOUND
;
123 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
124 while (iterator
->iterate(iterator
, (void**)¤t
))
126 if (id
->equals(id
, current
->id
))
136 iterator
->destroy(iterator
);
142 * Check if a given peer is registered at other peers. If so, remove it there
143 * and then remove peers completely that are not online and have no registered
146 static void unregister_peer(private_mediation_manager_t
*this, identification_t
*peer_id
)
148 iterator_t
*iterator
, *iterator_r
;
150 identification_t
*registered
;
152 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
153 while (iterator
->iterate(iterator
, (void**)&peer
))
155 iterator_r
= peer
->requested_by
->create_iterator(peer
->requested_by
, TRUE
);
156 while (iterator_r
->iterate(iterator_r
, (void**)®istered
))
158 if (peer_id
->equals(peer_id
, registered
))
160 iterator_r
->remove(iterator_r
);
161 registered
->destroy(registered
);
165 iterator_r
->destroy(iterator_r
);
167 if (!peer
->ike_sa_id
&& !peer
->requested_by
->get_count(peer
->requested_by
))
169 iterator
->remove(iterator
);
174 iterator
->destroy(iterator
);
178 * Implementation of mediation_manager_t.remove
180 static void remove_sa(private_mediation_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
182 iterator_t
*iterator
;
185 this->mutex
->lock(this->mutex
);
187 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
188 while (iterator
->iterate(iterator
, (void**)&peer
))
190 if (ike_sa_id
->equals(ike_sa_id
, peer
->ike_sa_id
))
192 iterator
->remove(iterator
);
194 unregister_peer(this, peer
->id
);
200 iterator
->destroy(iterator
);
202 this->mutex
->unlock(this->mutex
);
206 * Implementation of mediation_manager_t.update_sa_id
208 static void update_sa_id(private_mediation_manager_t
*this, identification_t
*peer_id
, ike_sa_id_t
*ike_sa_id
)
210 iterator_t
*iterator
;
214 this->mutex
->lock(this->mutex
);
216 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
217 while (iterator
->iterate(iterator
, (void**)&peer
))
219 if (peer_id
->equals(peer_id
, peer
->id
))
221 DESTROY_IF(peer
->ike_sa_id
);
226 iterator
->destroy(iterator
);
230 DBG2(DBG_IKE
, "adding peer '%D'", peer_id
);
231 peer
= peer_create(peer_id
, NULL
);
232 this->peers
->insert_last(this->peers
, peer
);
235 DBG2(DBG_IKE
, "changing registered IKE_SA ID of peer '%D'", peer_id
);
236 peer
->ike_sa_id
= ike_sa_id ? ike_sa_id
->clone(ike_sa_id
) : NULL
;
238 /* send callbacks to registered peers */
239 identification_t
*requester
;
240 while(peer
->requested_by
->remove_last(peer
->requested_by
, (void**)&requester
) == SUCCESS
)
242 job_t
*job
= (job_t
*)mediation_callback_job_create(requester
, peer_id
);
243 charon
->processor
->queue_job(charon
->processor
, job
);
244 requester
->destroy(requester
);
247 this->mutex
->unlock(this->mutex
);
251 * Implementation of mediation_manager_t.check.
253 static ike_sa_id_t
*check(private_mediation_manager_t
*this,
254 identification_t
*peer_id
)
257 ike_sa_id_t
*ike_sa_id
;
259 this->mutex
->lock(this->mutex
);
261 if (get_peer_by_id(this, peer_id
, &peer
) != SUCCESS
)
263 this->mutex
->unlock(this->mutex
);
267 ike_sa_id
= peer
->ike_sa_id
;
269 this->mutex
->unlock(this->mutex
);
275 * Implementation of mediation_manager_t.check_and_register.
277 static ike_sa_id_t
*check_and_register(private_mediation_manager_t
*this,
278 identification_t
*peer_id
, identification_t
*requester
)
281 ike_sa_id_t
*ike_sa_id
;
283 this->mutex
->lock(this->mutex
);
285 if (get_peer_by_id(this, peer_id
, &peer
) != SUCCESS
)
287 DBG2(DBG_IKE
, "adding peer %D", peer_id
);
288 peer
= peer_create(peer_id
, NULL
);
289 this->peers
->insert_last(this->peers
, peer
);
292 if (!peer
->ike_sa_id
)
294 /* the peer is not online */
295 DBG2(DBG_IKE
, "requested peer '%D' is offline, registering peer '%D'", peer_id
, requester
);
296 register_peer(peer
, requester
);
297 this->mutex
->unlock(this->mutex
);
301 ike_sa_id
= peer
->ike_sa_id
;
303 this->mutex
->unlock(this->mutex
);
309 * Implementation of mediation_manager_t.destroy.
311 static void destroy(private_mediation_manager_t
*this)
313 this->mutex
->lock(this->mutex
);
315 this->peers
->destroy_function(this->peers
, (void*)peer_destroy
);
317 this->mutex
->unlock(this->mutex
);
318 this->mutex
->destroy(this->mutex
);
323 * Described in header.
325 mediation_manager_t
*mediation_manager_create()
327 private_mediation_manager_t
*this = malloc_thing(private_mediation_manager_t
);
329 this->public.destroy
= (void(*)(mediation_manager_t
*))destroy
;
330 this->public.remove
= (void(*)(mediation_manager_t
*,ike_sa_id_t
*))remove_sa
;
331 this->public.update_sa_id
= (void(*)(mediation_manager_t
*,identification_t
*,ike_sa_id_t
*))update_sa_id
;
332 this->public.check
= (ike_sa_id_t
*(*)(mediation_manager_t
*,identification_t
*))check
;
333 this->public.check_and_register
= (ike_sa_id_t
*(*)(mediation_manager_t
*,identification_t
*,identification_t
*))check_and_register
;
335 this->peers
= linked_list_create();
336 this->mutex
= mutex_create(MUTEX_DEFAULT
);
338 return (mediation_manager_t
*)this;