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