Output handler of a given workitem
[strongswan.git] / src / libimcv / imv / imv_workitem.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_workitem.h"
17
18 #include <utils/debug.h>
19 #include <tncif_names.h>
20
21 typedef struct private_imv_workitem_t private_imv_workitem_t;
22
23 ENUM(imv_workitem_type_names, IMV_WORKITEM_PACKAGES, IMV_WORKITEM_SWID_TAGS,
24 "PCKGS",
25 "UNSRC",
26 "FWDEN",
27 "PWDEN",
28 "FREFM",
29 "FMEAS",
30 "FMETA",
31 "DREFM",
32 "DMEAS",
33 "DMETA",
34 "TCPOP",
35 "TCPBL",
36 "UDPOP",
37 "UDPBL",
38 "SWIDT"
39 );
40
41 /**
42 * Private data of a imv_workitem_t object.
43 *
44 */
45 struct private_imv_workitem_t {
46
47 /**
48 * Public imv_workitem_t interface.
49 */
50 imv_workitem_t public;
51
52 /**
53 * Primary workitem key
54 */
55 int id;
56
57 /**
58 * IMV ID
59 */
60 TNC_IMVID imv_id;
61
62 /**
63 * Workitem type
64 */
65 imv_workitem_type_t type;
66
67 /**
68 * Argument string
69 */
70 char *arg_str;
71
72 /**
73 * Argument integer
74 */
75 int arg_int;
76
77 /**
78 * Result string
79 */
80 char *result;
81
82 /**
83 * IMV action recommendation
84 */
85 TNC_IMV_Action_Recommendation rec_fail;
86
87 /**
88 * IMV action recommendation
89 */
90 TNC_IMV_Action_Recommendation rec_noresult;
91
92 /**
93 * IMV action recommendation
94 */
95 TNC_IMV_Action_Recommendation rec_final;
96
97 };
98
99 METHOD(imv_workitem_t, get_id, int,
100 private_imv_workitem_t *this)
101 {
102 return this->id;
103 }
104
105 METHOD(imv_workitem_t, set_imv_id, void,
106 private_imv_workitem_t *this, TNC_IMVID imv_id)
107 {
108 this->imv_id = imv_id;
109
110 DBG2(DBG_IMV, "IMV %d handles %N workitem %d", imv_id,
111 imv_workitem_type_names, this->type, this->id);
112 }
113
114 METHOD(imv_workitem_t, get_imv_id, TNC_IMVID,
115 private_imv_workitem_t *this)
116 {
117 return this->imv_id;
118 }
119
120 METHOD(imv_workitem_t, get_type, imv_workitem_type_t,
121 private_imv_workitem_t *this)
122 {
123 return this->type;
124 }
125
126 METHOD(imv_workitem_t, get_arg_str, char*,
127 private_imv_workitem_t *this)
128 {
129 return this->arg_str;
130 }
131
132 METHOD(imv_workitem_t, get_arg_int, int,
133 private_imv_workitem_t *this)
134 {
135 return this->arg_int;
136 }
137
138 METHOD(imv_workitem_t, set_result, TNC_IMV_Action_Recommendation,
139 private_imv_workitem_t *this, char *result, TNC_IMV_Evaluation_Result eval)
140 {
141 this->result = strdup(result);
142 switch (eval)
143 {
144 case TNC_IMV_EVALUATION_RESULT_COMPLIANT:
145 this->rec_final = TNC_IMV_ACTION_RECOMMENDATION_ALLOW;
146 break;
147 case TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MINOR:
148 case TNC_IMV_EVALUATION_RESULT_NONCOMPLIANT_MAJOR:
149 this->rec_final = this->rec_fail;
150 break;
151 case TNC_IMV_EVALUATION_RESULT_ERROR:
152 case TNC_IMV_EVALUATION_RESULT_DONT_KNOW:
153 default:
154 this->rec_final = this->rec_noresult;
155 break;
156 }
157 DBG2(DBG_IMV, "IMV %d handled %N workitem %d: %N%s%s", this->imv_id,
158 imv_workitem_type_names, this->type, this->id,
159 TNC_IMV_Action_Recommendation_names, this->rec_final,
160 strlen(result) ? " - " : "", result);
161
162 return this->rec_final;
163 }
164
165 METHOD(imv_workitem_t, get_result, TNC_IMV_Action_Recommendation,
166 private_imv_workitem_t *this, char **result)
167 {
168 if (result)
169 {
170 *result = this->result;
171 }
172 return this->rec_final;
173 }
174
175 METHOD(imv_workitem_t, destroy, void,
176 private_imv_workitem_t *this)
177 {
178 free(this->arg_str);
179 free(this->result);
180 free(this);
181 }
182
183 /**
184 * See header
185 */
186 imv_workitem_t *imv_workitem_create(int id, imv_workitem_type_t type,
187 char *arg_str, int arg_int,
188 TNC_IMV_Action_Recommendation rec_fail,
189 TNC_IMV_Action_Recommendation rec_noresult)
190 {
191 private_imv_workitem_t *this;
192
193 INIT(this,
194 .public = {
195 .get_id = _get_id,
196 .set_imv_id = _set_imv_id,
197 .get_imv_id = _get_imv_id,
198 .get_type = _get_type,
199 .get_arg_str = _get_arg_str,
200 .get_arg_int = _get_arg_int,
201 .set_result = _set_result,
202 .get_result = _get_result,
203 .destroy = _destroy,
204 },
205 .id = id,
206 .imv_id = TNC_IMVID_ANY,
207 .type = type,
208 .arg_str = arg_str ? strdup(arg_str) : NULL,
209 .arg_int = arg_int,
210 .rec_fail = rec_fail,
211 .rec_noresult = rec_noresult,
212 .rec_final = TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION,
213 );
214
215 return &this->public;
216 }
217