2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 #include "imv_scanner_state.h"
17 #include <utils/lexparser.h>
20 typedef struct private_imv_scanner_state_t private_imv_scanner_state_t
;
23 * Private data of an imv_scanner_state_t object.
25 struct private_imv_scanner_state_t
{
28 * Public members of imv_scanner_state_t
30 imv_scanner_state_t
public;
35 TNC_ConnectionID connection_id
;
38 * TNCCS connection state
40 TNC_ConnectionState state
;
43 * Does the TNCCS connection support long message types?
48 * Does the TNCCS connection support exclusive delivery?
53 * IMV action recommendation
55 TNC_IMV_Action_Recommendation rec
;
58 * IMV evaluation result
60 TNC_IMV_Evaluation_Result eval
;
63 * String with list of ports that should be closed
65 char *violating_ports
;
68 * Local copy of the reason string
70 chunk_t reason_string
;
73 typedef struct entry_t entry_t
;
76 * Define an internal reason string entry
84 * Table of multi-lingual reason string entries
86 static entry_t reasons
[] = {
87 { "en", "The following ports are open:" },
88 { "de", "Die folgenden Ports sind offen" },
89 { "fr", "Les ports suivants sont ouverts:" },
90 { "pl", "Następujące porty sa otwarte:" }
93 METHOD(imv_state_t
, get_connection_id
, TNC_ConnectionID
,
94 private_imv_scanner_state_t
*this)
96 return this->connection_id
;
99 METHOD(imv_state_t
, has_long
, bool,
100 private_imv_scanner_state_t
*this)
102 return this->has_long
;
105 METHOD(imv_state_t
, has_excl
, bool,
106 private_imv_scanner_state_t
*this)
108 return this->has_excl
;
111 METHOD(imv_state_t
, set_flags
, void,
112 private_imv_scanner_state_t
*this, bool has_long
, bool has_excl
)
114 this->has_long
= has_long
;
115 this->has_excl
= has_excl
;
118 METHOD(imv_state_t
, change_state
, void,
119 private_imv_scanner_state_t
*this, TNC_ConnectionState new_state
)
121 this->state
= new_state
;
124 METHOD(imv_state_t
, get_recommendation
, void,
125 private_imv_scanner_state_t
*this, TNC_IMV_Action_Recommendation
*rec
,
126 TNC_IMV_Evaluation_Result
*eval
)
132 METHOD(imv_state_t
, set_recommendation
, void,
133 private_imv_scanner_state_t
*this, TNC_IMV_Action_Recommendation rec
,
134 TNC_IMV_Evaluation_Result eval
)
140 METHOD(imv_state_t
, get_reason_string
, bool,
141 private_imv_scanner_state_t
*this, chunk_t preferred_language
,
142 chunk_t
*reason_string
, chunk_t
*reason_language
)
144 chunk_t pref_lang
, lang
;
148 if (!this->violating_ports
)
153 while (eat_whitespace(&preferred_language
))
155 if (!extract_token(&pref_lang
, ',', &preferred_language
))
157 /* last entry in a comma-separated list or single entry */
158 pref_lang
= preferred_language
;
161 /* eat trailing whitespace */
162 pos
= pref_lang
.ptr
+ pref_lang
.len
- 1;
163 while (pref_lang
.len
&& *pos
-- == ' ')
168 for (i
= 0 ; i
< countof(reasons
); i
++)
170 lang
= chunk_create(reasons
[i
].lang
, strlen(reasons
[i
].lang
));
171 if (chunk_equals(lang
, pref_lang
))
173 this->reason_string
= chunk_cat("cc",
174 chunk_create(reasons
[i
].string
,
175 strlen(reasons
[i
].string
)),
176 chunk_create(this->violating_ports
,
177 strlen(this->violating_ports
)));
178 *reason_string
= this->reason_string
;
179 *reason_language
= lang
;
185 /* no preferred language match found - use the default language */
187 this->reason_string
= chunk_cat("cc",
188 chunk_create(reasons
[0].string
,
189 strlen(reasons
[0].string
)),
190 chunk_create(this->violating_ports
,
191 strlen(this->violating_ports
)));
192 *reason_string
= this->reason_string
;
193 *reason_language
= chunk_create(reasons
[0].lang
,
194 strlen(reasons
[0].lang
));
198 METHOD(imv_state_t
, destroy
, void,
199 private_imv_scanner_state_t
*this)
201 free(this->violating_ports
);
202 free(this->reason_string
.ptr
);
206 METHOD(imv_scanner_state_t
, set_violating_ports
, void,
207 private_imv_scanner_state_t
*this, char *ports
)
209 this->violating_ports
= strdup(ports
);
213 * Described in header.
215 imv_state_t
*imv_scanner_state_create(TNC_ConnectionID connection_id
)
217 private_imv_scanner_state_t
*this;
222 .get_connection_id
= _get_connection_id
,
223 .has_long
= _has_long
,
224 .has_excl
= _has_excl
,
225 .set_flags
= _set_flags
,
226 .change_state
= _change_state
,
227 .get_recommendation
= _get_recommendation
,
228 .set_recommendation
= _set_recommendation
,
229 .get_reason_string
= _get_reason_string
,
232 .set_violating_ports
= _set_violating_ports
,
234 .state
= TNC_CONNECTION_STATE_CREATE
,
235 .rec
= TNC_IMV_ACTION_RECOMMENDATION_NO_RECOMMENDATION
,
236 .eval
= TNC_IMV_EVALUATION_RESULT_DONT_KNOW
,
237 .connection_id
= connection_id
,
240 return &this->public.interface
;