Introduce priority classes for jobs
[strongswan.git] / src / libcharon / processing / jobs / roam_job.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 */
15
16 #include <stdlib.h>
17
18 #include "roam_job.h"
19
20 #include <sa/ike_sa.h>
21 #include <daemon.h>
22
23
24 typedef struct private_roam_job_t private_roam_job_t;
25
26 /**
27 * Private data of an roam_job_t Object
28 */
29 struct private_roam_job_t {
30 /**
31 * public roam_job_t interface
32 */
33 roam_job_t public;
34
35 /**
36 * has the address list changed, or the routing only?
37 */
38 bool address;
39 };
40
41 METHOD(job_t, destroy, void,
42 private_roam_job_t *this)
43 {
44 free(this);
45 }
46
47 METHOD(job_t, execute, void,
48 private_roam_job_t *this)
49 {
50 ike_sa_t *ike_sa;
51 linked_list_t *list;
52 ike_sa_id_t *id;
53 enumerator_t *enumerator;
54
55 /* enumerator over all IKE_SAs gives us no way to checkin_and_destroy
56 * after a DESTROY_ME, so we check out each available IKE_SA by hand. */
57 list = linked_list_create();
58 enumerator = charon->ike_sa_manager->create_enumerator(charon->ike_sa_manager);
59 while (enumerator->enumerate(enumerator, &ike_sa))
60 {
61 id = ike_sa->get_id(ike_sa);
62 list->insert_last(list, id->clone(id));
63 }
64 enumerator->destroy(enumerator);
65
66 while (list->remove_last(list, (void**)&id) == SUCCESS)
67 {
68 ike_sa = charon->ike_sa_manager->checkout(charon->ike_sa_manager, id);
69 if (ike_sa)
70 {
71 if (ike_sa->roam(ike_sa, this->address) == DESTROY_ME)
72 {
73 charon->ike_sa_manager->checkin_and_destroy(
74 charon->ike_sa_manager, ike_sa);
75 }
76 else
77 {
78 charon->ike_sa_manager->checkin(charon->ike_sa_manager, ike_sa);
79 }
80 }
81 id->destroy(id);
82 }
83 list->destroy(list);
84
85 destroy(this);
86 }
87
88 METHOD(job_t, get_priority, job_priority_t,
89 private_roam_job_t *this)
90 {
91 return JOB_PRIO_MEDIUM;
92 }
93
94 /*
95 * Described in header
96 */
97 roam_job_t *roam_job_create(bool address)
98 {
99 private_roam_job_t *this;
100
101 INIT(this,
102 .public = {
103 .job_interface = {
104 .execute = _execute,
105 .get_priority = _get_priority,
106 .destroy = _destroy,
107 },
108 },
109 .address = address,
110 );
111
112 return &this->public;
113 }
114