2 * @file mediation_manager.c
4 * @brief Implementation of mediation_manager_t.
9 * Copyright (C) 2007 Tobias Brunner
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 "mediation_manager.h"
27 #include <utils/linked_list.h>
28 #include <processing/jobs/mediation_job.h>
31 typedef struct peer_t peer_t
;
34 * An entry in the linked list.
40 /** sa id of the peer, NULL if offline */
41 ike_sa_id_t
*ike_sa_id
;
43 /** list of peer ids that reuested this peer */
44 linked_list_t
*requested_by
;
48 * Implementation of peer_t.destroy.
50 static void peer_destroy(peer_t
*this)
53 DESTROY_IF(this->ike_sa_id
);
54 this->requested_by
->destroy_offset(this->requested_by
, offsetof(identification_t
, destroy
));
59 * Creates a new entry for the list.
61 static peer_t
*peer_create(identification_t
*id
, ike_sa_id_t
* ike_sa_id
)
63 peer_t
*this = malloc_thing(peer_t
);
65 /* clone everything */
66 this->id
= id
->clone(id
);
67 this->ike_sa_id
= ike_sa_id ? ike_sa_id
->clone(ike_sa_id
) : NULL
;
68 this->requested_by
= linked_list_create();
74 typedef struct private_mediation_manager_t private_mediation_manager_t
;
77 * Additional private members of mediation_manager_t.
79 struct private_mediation_manager_t
{
81 * Public interface of mediation_manager_t.
83 mediation_manager_t
public;
86 * Lock for exclusivly accessing the manager.
88 pthread_mutex_t mutex
;
91 * Linked list with state entries.
97 * Registers a peer's ID at another peer, if it is not yet registered
99 static void register_peer(peer_t
*peer
, identification_t
*peer_id
)
101 iterator_t
*iterator
;
102 identification_t
*current
;
104 iterator
= peer
->requested_by
->create_iterator(peer
->requested_by
, TRUE
);
105 while (iterator
->iterate(iterator
, (void**)¤t
))
107 if (peer_id
->equals(peer_id
, current
))
109 iterator
->destroy(iterator
);
113 iterator
->destroy(iterator
);
115 peer
->requested_by
->insert_last(peer
->requested_by
, peer_id
->clone(peer_id
));
119 * Get a peer_t object by a peer's id
121 static status_t
get_peer_by_id(private_mediation_manager_t
*this,
122 identification_t
*id
, peer_t
**peer
)
124 iterator_t
*iterator
;
126 status_t status
= NOT_FOUND
;
128 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
129 while (iterator
->iterate(iterator
, (void**)¤t
))
131 if (id
->equals(id
, current
->id
))
141 iterator
->destroy(iterator
);
147 * Check if a given peer is registered at other peers. If so, remove it there
148 * and then remove peers completely that are not online and have no registered
151 static void unregister_peer(private_mediation_manager_t
*this, identification_t
*peer_id
)
153 iterator_t
*iterator
, *iterator_r
;
155 identification_t
*registered
;
157 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
158 while (iterator
->iterate(iterator
, (void**)&peer
))
160 iterator_r
= peer
->requested_by
->create_iterator(peer
->requested_by
, TRUE
);
161 while (iterator_r
->iterate(iterator_r
, (void**)®istered
))
163 if (peer_id
->equals(peer_id
, registered
))
165 iterator_r
->remove(iterator_r
);
166 registered
->destroy(registered
);
170 iterator_r
->destroy(iterator_r
);
172 if (!peer
->ike_sa_id
&& !peer
->requested_by
->get_count(peer
->requested_by
))
174 iterator
->remove(iterator
);
179 iterator
->destroy(iterator
);
183 * Implementation of mediation_manager_t.remove
185 static void remove_sa(private_mediation_manager_t
*this, ike_sa_id_t
*ike_sa_id
)
187 iterator_t
*iterator
;
190 pthread_mutex_lock(&(this->mutex
));
192 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
193 while (iterator
->iterate(iterator
, (void**)&peer
))
195 if (ike_sa_id
->equals(ike_sa_id
, peer
->ike_sa_id
))
197 iterator
->remove(iterator
);
199 unregister_peer(this, peer
->id
);
205 iterator
->destroy(iterator
);
207 pthread_mutex_unlock(&(this->mutex
));
211 * Implementation of mediation_manager_t.update_sa_id
213 static void update_sa_id(private_mediation_manager_t
*this, identification_t
*peer_id
, ike_sa_id_t
*ike_sa_id
)
215 iterator_t
*iterator
;
219 pthread_mutex_lock(&(this->mutex
));
221 iterator
= this->peers
->create_iterator(this->peers
, TRUE
);
222 while (iterator
->iterate(iterator
, (void**)&peer
))
224 if (peer_id
->equals(peer_id
, peer
->id
))
226 DESTROY_IF(peer
->ike_sa_id
);
231 iterator
->destroy(iterator
);
235 DBG2(DBG_IKE
, "adding peer '%D'", peer_id
);
236 peer
= peer_create(peer_id
, NULL
);
237 this->peers
->insert_last(this->peers
, peer
);
240 DBG2(DBG_IKE
, "changing registered IKE_SA ID of peer '%D'", peer_id
);
241 peer
->ike_sa_id
= ike_sa_id ? ike_sa_id
->clone(ike_sa_id
) : NULL
;
243 /* send callbacks to registered peers */
244 identification_t
*requester
;
245 while(peer
->requested_by
->remove_last(peer
->requested_by
, (void**)&requester
) == SUCCESS
)
247 job_t
*job
= (job_t
*)mediation_callback_job_create(requester
, peer_id
);
248 charon
->processor
->queue_job(charon
->processor
, job
);
251 pthread_mutex_unlock(&(this->mutex
));
255 * Implementation of mediation_manager_t.check.
257 static ike_sa_id_t
*check(private_mediation_manager_t
*this,
258 identification_t
*peer_id
)
261 ike_sa_id_t
*ike_sa_id
;
263 pthread_mutex_lock(&(this->mutex
));
265 if (get_peer_by_id(this, peer_id
, &peer
) != SUCCESS
)
267 pthread_mutex_unlock(&(this->mutex
));
271 ike_sa_id
= peer
->ike_sa_id
;
273 pthread_mutex_unlock(&(this->mutex
));
279 * Implementation of mediation_manager_t.check_and_register.
281 static ike_sa_id_t
*check_and_register(private_mediation_manager_t
*this,
282 identification_t
*peer_id
, identification_t
*requester
)
285 ike_sa_id_t
*ike_sa_id
;
287 pthread_mutex_lock(&(this->mutex
));
289 if (get_peer_by_id(this, peer_id
, &peer
) != SUCCESS
)
291 DBG2(DBG_IKE
, "adding peer %D", peer_id
);
292 peer
= peer_create(peer_id
, NULL
);
293 this->peers
->insert_last(this->peers
, peer
);
296 if (!peer
->ike_sa_id
)
298 /* the peer is not online */
299 DBG2(DBG_IKE
, "requested peer '%D' is offline, registering peer '%D'", peer_id
, requester
);
300 register_peer(peer
, requester
);
301 pthread_mutex_unlock(&(this->mutex
));
305 ike_sa_id
= peer
->ike_sa_id
;
307 pthread_mutex_unlock(&(this->mutex
));
313 * Implementation of mediation_manager_t.destroy.
315 static void destroy(private_mediation_manager_t
*this)
317 pthread_mutex_lock(&(this->mutex
));
319 this->peers
->destroy_function(this->peers
, (void*)peer_destroy
);
321 pthread_mutex_unlock(&(this->mutex
));
322 pthread_mutex_destroy(&(this->mutex
));
327 * Described in header.
329 mediation_manager_t
*mediation_manager_create()
331 private_mediation_manager_t
*this = malloc_thing(private_mediation_manager_t
);
333 this->public.destroy
= (void(*)(mediation_manager_t
*))destroy
;
334 this->public.remove
= (void(*)(mediation_manager_t
*,ike_sa_id_t
*))remove_sa
;
335 this->public.update_sa_id
= (void(*)(mediation_manager_t
*,identification_t
*,ike_sa_id_t
*))update_sa_id
;
336 this->public.check
= (ike_sa_id_t
*(*)(mediation_manager_t
*,identification_t
*))check
;
337 this->public.check_and_register
= (ike_sa_id_t
*(*)(mediation_manager_t
*,identification_t
*,identification_t
*))check_and_register
;
339 this->peers
= linked_list_create();
340 pthread_mutex_init(&(this->mutex
), NULL
);
342 return (mediation_manager_t
*)this;