refactored IMV policy management
[strongswan.git] / src / libimcv / imv / imv_session.c
1 /*
2 * Copyright (C) 2013 Andreas Steffen
3 * HSR 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 "imv_session.h"
17
18 #include <utils/debug.h>
19
20 typedef struct private_imv_session_t private_imv_session_t;
21
22 /**
23 * Private data of a imv_session_t object.
24 */
25 struct private_imv_session_t {
26
27 /**
28 * Public imv_session_t interface.
29 */
30 imv_session_t public;
31
32 /**
33 * Unique Session ID
34 */
35 int session_id;
36
37 /**
38 * TNCCS connection ID
39 */
40 TNC_ConnectionID conn_id;
41
42 /**
43 * Have the workitems been generated?
44 */
45 bool policy_started;
46
47 /**
48 * List of worklist items
49 */
50 linked_list_t *workitems;
51
52 /**
53 * Reference count
54 */
55 refcount_t ref;
56
57 };
58
59 METHOD(imv_session_t, get_session_id, int,
60 private_imv_session_t *this)
61 {
62 return this->session_id;
63 }
64
65 METHOD(imv_session_t, get_connection_id, TNC_ConnectionID,
66 private_imv_session_t *this)
67 {
68 return this->conn_id;
69 }
70
71 METHOD(imv_session_t, set_policy_started, void,
72 private_imv_session_t *this, bool start)
73 {
74 this->policy_started = start;
75 }
76
77 METHOD(imv_session_t, get_policy_started, bool,
78 private_imv_session_t *this)
79 {
80 return this->policy_started;
81 }
82
83 METHOD(imv_session_t, insert_workitem, void,
84 private_imv_session_t *this, imv_workitem_t *workitem)
85 {
86 this->workitems->insert_last(this->workitems, workitem);
87 }
88
89 METHOD(imv_session_t, remove_workitem, void,
90 private_imv_session_t *this, enumerator_t *enumerator)
91 {
92 this->workitems->remove_at(this->workitems, enumerator);
93 }
94
95 METHOD(imv_session_t, create_workitem_enumerator, enumerator_t*,
96 private_imv_session_t *this)
97 {
98 if (!this->policy_started)
99 {
100 return NULL;
101 }
102 return this->workitems->create_enumerator(this->workitems);
103 }
104
105 METHOD(imv_session_t, get_workitem_count, int,
106 private_imv_session_t *this, TNC_IMVID imv_id)
107 {
108 enumerator_t *enumerator;
109 imv_workitem_t *workitem;
110 int count = 0;
111
112 enumerator = this->workitems->create_enumerator(this->workitems);
113 while (enumerator->enumerate(enumerator, &workitem))
114 {
115 if (workitem->get_imv_id(workitem) == imv_id)
116 {
117 count++;
118 }
119 }
120 enumerator->destroy(enumerator);
121
122 return count;
123 }
124
125 METHOD(imv_session_t, get_ref, imv_session_t*,
126 private_imv_session_t *this)
127 {
128 ref_get(&this->ref);
129
130 return &this->public;
131 }
132
133 METHOD(imv_session_t, destroy, void,
134 private_imv_session_t *this)
135 {
136 if (ref_put(&this->ref))
137 {
138 this->workitems->destroy_offset(this->workitems,
139 offsetof(imv_workitem_t, destroy));
140 free(this);
141 }
142 }
143
144 /**
145 * See header
146 */
147 imv_session_t *imv_session_create(int session_id, TNC_ConnectionID conn_id)
148 {
149 private_imv_session_t *this;
150
151 INIT(this,
152 .public = {
153 .get_session_id = _get_session_id,
154 .get_connection_id = _get_connection_id,
155 .set_policy_started = _set_policy_started,
156 .get_policy_started = _get_policy_started,
157 .insert_workitem = _insert_workitem,
158 .remove_workitem = _remove_workitem,
159 .create_workitem_enumerator = _create_workitem_enumerator,
160 .get_workitem_count = _get_workitem_count,
161 .get_ref = _get_ref,
162 .destroy = _destroy,
163 },
164 .session_id = session_id,
165 .conn_id = conn_id,
166 .workitems = linked_list_create(),
167 .ref = 1,
168 );
169
170 return &this->public;
171 }