7365d100a434691290f9c25bd0342574e530fe0f
[strongswan.git] / src / libimcv / plugins / imc_test / imc_test_state.c
1 /*
2 * Copyright (C) 2011 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
3 *
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>.
8 *
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
12 * for more details.
13 */
14
15 #include "imc_test_state.h"
16
17 #include <debug.h>
18 #include <utils/linked_list.h>
19
20 typedef struct private_imc_test_state_t private_imc_test_state_t;
21
22 /**
23 * Private data of an imc_test_state_t object.
24 */
25 struct private_imc_test_state_t {
26
27 /**
28 * Public members of imc_test_state_t
29 */
30 imc_test_state_t public;
31
32 /**
33 * TNCCS connection ID
34 */
35 TNC_ConnectionID connection_id;
36
37 /**
38 * TNCCS connection state
39 */
40 TNC_ConnectionState state;
41
42 /**
43 * Does the TNCCS connection support long message types?
44 */
45 bool has_long;
46
47 /**
48 * Does the TNCCS connection support exclusive delivery?
49 */
50 bool has_excl;
51
52 /**
53 * Command to transmit to IMV
54 */
55 char *command;
56
57 /**
58 * Is it the first handshake?
59 */
60 bool first_handshake;
61
62 /**
63 * Do a handshake retry
64 */
65 bool handshake_retry;
66
67 /**
68 * List of additional IMC IDs
69 */
70 linked_list_t *additional_ids;
71
72 };
73
74 METHOD(imc_state_t, get_connection_id, TNC_ConnectionID,
75 private_imc_test_state_t *this)
76 {
77 return this->connection_id;
78 }
79
80 METHOD(imc_state_t, has_long, bool,
81 private_imc_test_state_t *this)
82 {
83 return this->has_long;
84 }
85
86 METHOD(imc_state_t, has_excl, bool,
87 private_imc_test_state_t *this)
88 {
89 return this->has_excl;
90 }
91
92 METHOD(imc_state_t, set_flags, void,
93 private_imc_test_state_t *this, bool has_long, bool has_excl)
94 {
95 this->has_long = has_long;
96 this->has_excl = has_excl;
97 }
98
99 METHOD(imc_state_t, change_state, void,
100 private_imc_test_state_t *this, TNC_ConnectionState new_state)
101 {
102 this->state = new_state;
103 }
104
105 METHOD(imc_state_t, destroy, void,
106 private_imc_test_state_t *this)
107 {
108 this->additional_ids->destroy(this->additional_ids);
109 free(this->command);
110 free(this);
111 }
112
113 METHOD(imc_test_state_t, get_command, char*,
114 private_imc_test_state_t *this)
115 {
116 return this->command;
117 }
118
119 METHOD(imc_test_state_t, set_command, void,
120 private_imc_test_state_t *this, char* command)
121 {
122 char *old_command;
123
124 old_command = this->command;
125 this->command = strdup(command);
126 free(old_command);
127 }
128
129 METHOD(imc_test_state_t, is_first_handshake, bool,
130 private_imc_test_state_t *this)
131 {
132 bool first;
133
134 /* test and reset first_handshake flag */
135 first= this->first_handshake;
136 this->first_handshake = FALSE;
137 return first;
138 }
139
140 METHOD(imc_test_state_t, do_handshake_retry, bool,
141 private_imc_test_state_t *this)
142 {
143 bool retry;
144
145 /* test and reset handshake_retry flag */
146 retry = this->handshake_retry;
147 this->handshake_retry = FALSE;
148 return retry;
149 }
150
151 METHOD(imc_test_state_t, add_id, void,
152 private_imc_test_state_t *this, TNC_IMCID id)
153 {
154 void *pointer;
155
156 /* store the scalar value in the pointer */
157 pointer = (void*)id;
158 this->additional_ids->insert_last(this->additional_ids, pointer);
159 }
160
161 METHOD(imc_test_state_t, create_id_enumerator, enumerator_t*,
162 private_imc_test_state_t *this)
163 {
164 return this->additional_ids->create_enumerator(this->additional_ids);
165 }
166
167 /**
168 * Described in header.
169 */
170 imc_state_t *imc_test_state_create(TNC_ConnectionID connection_id,
171 char *command, bool retry)
172 {
173 private_imc_test_state_t *this;
174
175 INIT(this,
176 .public = {
177 .interface = {
178 .get_connection_id = _get_connection_id,
179 .has_long = _has_long,
180 .has_excl = _has_excl,
181 .set_flags = _set_flags,
182 .change_state = _change_state,
183 .destroy = _destroy,
184 },
185 .get_command = _get_command,
186 .set_command = _set_command,
187 .is_first_handshake = _is_first_handshake,
188 .do_handshake_retry = _do_handshake_retry,
189 .add_id = _add_id,
190 .create_id_enumerator = _create_id_enumerator,
191 },
192 .state = TNC_CONNECTION_STATE_CREATE,
193 .connection_id = connection_id,
194 .command = strdup(command),
195 .first_handshake = TRUE,
196 .handshake_retry = retry,
197 .additional_ids = linked_list_create(),
198 );
199
200 return &this->public.interface;
201 }
202
203