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