implemented server-initiated handshake retry in IMC/IMV Test pair
[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
19 typedef struct private_imc_test_state_t private_imc_test_state_t;
20
21 /**
22 * Private data of an imc_test_state_t object.
23 */
24 struct private_imc_test_state_t {
25
26 /**
27 * Public members of imc_test_state_t
28 */
29 imc_test_state_t public;
30
31 /**
32 * TNCCS connection ID
33 */
34 TNC_ConnectionID connection_id;
35
36 /**
37 * TNCCS connection state
38 */
39 TNC_ConnectionState state;
40
41 /**
42 * Command to transmit to IMV
43 */
44 char *command;
45
46 /**
47 * Is it the first handshake?
48 */
49 bool first_handshake;
50
51 /**
52 * Do a handshake retry
53 */
54 bool handshake_retry;
55 };
56
57 METHOD(imc_state_t, get_connection_id, TNC_ConnectionID,
58 private_imc_test_state_t *this)
59 {
60 return this->connection_id;
61 }
62
63 METHOD(imc_state_t, change_state, void,
64 private_imc_test_state_t *this, TNC_ConnectionState new_state)
65 {
66 this->state = new_state;
67 }
68
69 METHOD(imc_state_t, destroy, void,
70 private_imc_test_state_t *this)
71 {
72 free(this->command);
73 free(this);
74 }
75
76 METHOD(imc_test_state_t, get_command, char*,
77 private_imc_test_state_t *this)
78 {
79 return this->command;
80 }
81
82 METHOD(imc_test_state_t, set_command, void,
83 private_imc_test_state_t *this, char* command)
84 {
85 char *old_command;
86
87 old_command = this->command;
88 this->command = strdup(command);
89 free(old_command);
90 }
91
92 METHOD(imc_test_state_t, is_first_handshake, bool,
93 private_imc_test_state_t *this)
94 {
95 bool first;
96
97 /* test and reset first_handshake flag */
98 first= this->first_handshake;
99 this->first_handshake = FALSE;
100 return first;
101 }
102
103 METHOD(imc_test_state_t, do_handshake_retry, bool,
104 private_imc_test_state_t *this)
105 {
106 bool retry;
107
108 /* test and reset handshake_retry flag */
109 retry = this->handshake_retry;
110 this->handshake_retry = FALSE;
111 return retry;
112 }
113
114 /**
115 * Described in header.
116 */
117 imc_state_t *imc_test_state_create(TNC_ConnectionID connection_id,
118 char *command, bool retry)
119 {
120 private_imc_test_state_t *this;
121
122 INIT(this,
123 .public = {
124 .interface = {
125 .get_connection_id = _get_connection_id,
126 .change_state = _change_state,
127 .destroy = _destroy,
128 },
129 .get_command = _get_command,
130 .set_command = _set_command,
131 .is_first_handshake = _is_first_handshake,
132 .do_handshake_retry = _do_handshake_retry,
133 },
134 .state = TNC_CONNECTION_STATE_CREATE,
135 .connection_id = connection_id,
136 .command = strdup(command),
137 .first_handshake = TRUE,
138 .handshake_retry = retry,
139 );
140
141 return &this->public.interface;
142 }
143
144