removed unused variables
[strongswan.git] / src / libcharon / plugins / imc_test / imc_test.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 <imc/imc_agent.h>
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <ita/ita_attr_command.h>
20 #include <tnc/pen/pen.h>
21 #include <debug.h>
22
23 /* IMC definitions */
24
25 static const char imc_name[] = "Test";
26
27 #define IMC_VENDOR_ID PEN_ITA
28 #define IMC_SUBTYPE 0x01
29
30 static imc_agent_t *imc_test;
31
32 /**
33 * see section 3.7.1 of TCG TNC IF-IMC Specification 1.2
34 */
35 TNC_Result TNC_IMC_Initialize(TNC_IMCID imc_id,
36 TNC_Version min_version,
37 TNC_Version max_version,
38 TNC_Version *actual_version)
39 {
40 if (imc_test)
41 {
42 DBG1(DBG_IMC, "IMC \"%s\" has already been initialized", imc_name);
43 return TNC_RESULT_ALREADY_INITIALIZED;
44 }
45 if (min_version > TNC_IFIMC_VERSION_1 || max_version < TNC_IFIMC_VERSION_1)
46 {
47 DBG1(DBG_IMC, "no common IF-IMC version");
48 return TNC_RESULT_NO_COMMON_VERSION;
49 }
50 imc_test = imc_agent_create(imc_name, IMC_VENDOR_ID, IMC_SUBTYPE,
51 imc_id, actual_version);
52 return TNC_RESULT_SUCCESS;
53 }
54
55 /**
56 * see section 3.7.2 of TCG TNC IF-IMC Specification 1.2
57 */
58 TNC_Result TNC_IMC_NotifyConnectionChange(TNC_IMCID imc_id,
59 TNC_ConnectionID connection_id,
60 TNC_ConnectionState new_state)
61 {
62 imc_state_t *state;
63
64 if (!imc_test)
65 {
66 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
67 return TNC_RESULT_NOT_INITIALIZED;
68 }
69 switch (new_state)
70 {
71 case TNC_CONNECTION_STATE_CREATE:
72 state = imc_test_state_create(connection_id);
73 return imc_test->create_state(imc_test, state);
74 case TNC_CONNECTION_STATE_DELETE:
75 return imc_test->delete_state(imc_test, connection_id);
76 default:
77 return imc_test->change_state(imc_test, connection_id, new_state);
78 }
79 }
80
81 static TNC_Result send_message(TNC_ConnectionID connection_id)
82 {
83 pa_tnc_msg_t *msg;
84 pa_tnc_attr_t *attr;
85 char *command;
86 TNC_Result result;
87
88 command = lib->settings->get_str(lib->settings, "imc-test.command", "none");
89 attr = ita_attr_command_create(command);
90 attr->set_noskip_flag(attr, TRUE);
91 msg = pa_tnc_msg_create();
92 msg->add_attribute(msg, attr);
93 msg->build(msg);
94 result = imc_test->send_message(imc_test, connection_id,
95 msg->get_encoding(msg));
96 msg->destroy(msg);
97
98 return result;
99 }
100
101 /**
102 * see section 3.7.3 of TCG TNC IF-IMC Specification 1.2
103 */
104 TNC_Result TNC_IMC_BeginHandshake(TNC_IMCID imc_id,
105 TNC_ConnectionID connection_id)
106 {
107 if (!imc_test)
108 {
109 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
110 return TNC_RESULT_NOT_INITIALIZED;
111 }
112 return send_message(connection_id);
113 }
114
115 /**
116 * see section 3.7.4 of TCG TNC IF-IMC Specification 1.2
117 */
118 TNC_Result TNC_IMC_ReceiveMessage(TNC_IMCID imc_id,
119 TNC_ConnectionID connection_id,
120 TNC_BufferReference msg,
121 TNC_UInt32 msg_len,
122 TNC_MessageType msg_type)
123 {
124 pa_tnc_msg_t *pa_tnc_msg;
125 status_t status;
126
127 if (!imc_test)
128 {
129 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
130 return TNC_RESULT_NOT_INITIALIZED;
131 }
132
133 /* process received message */
134 DBG2(DBG_IMC, "IMC %u \"%s\" received message type 0x%08x for Connection ID %u",
135 imc_id, imc_name, msg_type, connection_id);
136 pa_tnc_msg = pa_tnc_msg_create_from_data(chunk_create(msg, msg_len));
137 status = pa_tnc_msg->process(pa_tnc_msg);
138 pa_tnc_msg->destroy(pa_tnc_msg);
139 if (status != SUCCESS)
140 {
141 return TNC_RESULT_FATAL;
142 }
143
144 /* always return the same response */
145 return send_message(connection_id);
146 }
147
148 /**
149 * see section 3.7.5 of TCG TNC IF-IMC Specification 1.2
150 */
151 TNC_Result TNC_IMC_BatchEnding(TNC_IMCID imc_id,
152 TNC_ConnectionID connection_id)
153 {
154 if (!imc_test)
155 {
156 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
157 return TNC_RESULT_NOT_INITIALIZED;
158 }
159 return TNC_RESULT_SUCCESS;
160 }
161
162 /**
163 * see section 3.7.6 of TCG TNC IF-IMC Specification 1.2
164 */
165 TNC_Result TNC_IMC_Terminate(TNC_IMCID imc_id)
166 {
167 if (!imc_test)
168 {
169 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
170 return TNC_RESULT_NOT_INITIALIZED;
171 }
172 imc_test->destroy(imc_test);
173 imc_test = NULL;
174
175 return TNC_RESULT_SUCCESS;
176 }
177
178 /**
179 * see section 4.2.8.1 of TCG TNC IF-IMC Specification 1.2
180 */
181 TNC_Result TNC_IMC_ProvideBindFunction(TNC_IMCID imc_id,
182 TNC_TNCC_BindFunctionPointer bind_function)
183 {
184 if (!imc_test)
185 {
186 DBG1(DBG_IMC, "IMC \"%s\" has not been initialized", imc_name);
187 return TNC_RESULT_NOT_INITIALIZED;
188 }
189 return imc_test->bind_functions(imc_test, bind_function);
190 }