implement IMC and IMV manager classes
[strongswan.git] / src / libcharon / plugins / tnc_imv / tnc_imv.c
1 /*
2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "tnc_imv.h"
17
18 #include <dlfcn.h>
19
20 #include <debug.h>
21 #include <library.h>
22
23 typedef struct private_tnc_imv_t private_tnc_imv_t;
24
25 struct private_tnc_imv_t {
26
27 /**
28 * Public members of imv_t.
29 */
30 imv_t public;
31
32 /**
33 * Name of loaded IMV
34 */
35 char *name;
36
37 /**
38 * ID of loaded IMV
39 */
40 TNC_IMVID id;
41 };
42
43 METHOD(imv_t, set_id, void,
44 private_tnc_imv_t *this, TNC_IMVID id)
45 {
46 this->id = id;
47 }
48
49 METHOD(imv_t, get_id, TNC_IMVID,
50 private_tnc_imv_t *this)
51 {
52 return this->id;
53 }
54
55 METHOD(imv_t, get_name, char*,
56 private_tnc_imv_t *this)
57 {
58 return this->name;
59 }
60
61 METHOD(imv_t, destroy, void,
62 private_tnc_imv_t *this)
63 {
64 free(this->name);
65 free(this);
66 }
67
68 /**
69 * Described in header.
70 */
71 imv_t* tnc_imv_create(char *name, char *filename)
72 {
73 private_tnc_imv_t *this;
74 void *handle;
75
76 INIT(this,
77 .public = {
78 .set_id = _set_id,
79 .get_id = _get_id,
80 .get_name = _get_name,
81 .destroy = _destroy,
82 },
83 );
84
85 handle = dlopen(filename, RTLD_NOW);
86 if (handle == NULL)
87 {
88 DBG1(DBG_TNC, "IMV '%s' failed to load from '%s': %s",
89 name, filename, dlerror());
90 free(this);
91 return NULL;
92 }
93
94 /* we do not store or free dlopen() handles, leak_detective requires
95 * the modules to keep loaded until leak report */
96
97 this->public.initialize = dlsym(handle, "TNC_IMV_Initialize");
98 if (!this->public.initialize)
99 {
100 DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n",
101 filename, dlerror());
102 free(this);
103 return NULL;
104 }
105 this->public.notify_connection_change =
106 dlsym(handle, "TNC_IMV_NotifyConnectionChange");
107 this->public.solicit_recommendation =
108 dlsym(handle, "TNC_IMV_SolicitRecommendation");
109 if (!this->public.solicit_recommendation)
110 {
111 DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
112 filename, dlerror());
113 free(this);
114 return NULL;
115 }
116 this->public.receive_message =
117 dlsym(handle, "TNC_IMV_ReceiveMessage");
118 this->public.batch_ending =
119 dlsym(handle, "TNC_IMV_BatchEnding");
120 this->public.terminate =
121 dlsym(handle, "TNC_IMV_Terminate");
122 this->public.provide_bind_function =
123 dlsym(handle, "TNC_IMV_ProvideBindFunction");
124 if (!this->public.provide_bind_function)
125 {
126 DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
127 filename, dlerror());
128 free(this);
129 return NULL;
130 }
131 this->name = strdup(name);
132
133 return &this->public;
134 }
135
136 /**
137 * Called by the IMV to inform a TNCS about the set of message types the IMV
138 * is able to receive
139 */
140 TNC_Result TNC_TNCS_ReportMessageTypes(TNC_IMVID imv_id,
141 TNC_MessageTypeList supported_types,
142 TNC_UInt32 type_count)
143 {
144 DBG2(DBG_TNC,"TNCS_ReportMessageTypes %u %u", imv_id, type_count);
145 return TNC_RESULT_SUCCESS;
146 }
147
148 /**
149 * Called by the IMV to ask a TNCS to retry an Integrity Check Handshake
150 */
151 TNC_Result TNC_TNCS_RequestHandshakeRetry(TNC_IMVID imv_id,
152 TNC_ConnectionID connection_id,
153 TNC_RetryReason reason)
154 {
155 DBG2(DBG_TNC,"TNCS_RequestHandshakeRetry %u %u", imv_id, connection_id);
156 return TNC_RESULT_SUCCESS;
157 }
158
159 /**
160 * Called by the IMV when an IMV-IMC message is to be sent
161 */
162 TNC_Result TNC_TNCS_SendMessage(TNC_IMVID imv_id,
163 TNC_ConnectionID connection_id,
164 TNC_BufferReference message,
165 TNC_UInt32 message_len,
166 TNC_MessageType message_type)
167 {
168 DBG2(DBG_TNC,"TNCS_SendMessage %u %u '%s' %u %0x", imv_id, connection_id,
169 message, message_len, message_type);
170 return TNC_RESULT_SUCCESS;
171 }
172
173 /**
174 * Called by the IMV to deliver its IMV Action Recommendation and IMV Evaluation
175 * Result to the TNCS
176 */
177 TNC_Result TNC_TNCS_ProvideRecommendation(TNC_IMVID imv_id,
178 TNC_ConnectionID connection_id,
179 TNC_IMV_Action_Recommendation recommendation,
180 TNC_IMV_Evaluation_Result evaluation)
181 {
182 DBG2(DBG_TNC,"TNCS_ProvideRecommendation %u %u", imv_id, connection_id);
183 return TNC_RESULT_SUCCESS;
184 }
185
186 /**
187 * Called by the IMV when it needs a function pointer
188 */
189 TNC_Result TNC_TNCS_BindFunction(TNC_IMVID id,
190 char *function_name,
191 void **function_pointer)
192 {
193 if (streq(function_name, "TNC_TNCS_ReportMessageTypes"))
194 {
195 *function_pointer = (void*)TNC_TNCS_ReportMessageTypes;
196 }
197 else if (streq(function_name, "TNC_TNCS_RequestHandshakeRetry"))
198 {
199 *function_pointer = (void*)TNC_TNCS_RequestHandshakeRetry;
200 }
201 else if (streq(function_name, "TNC_TNCS_SendMessage"))
202 {
203 *function_pointer = (void*)TNC_TNCS_SendMessage;
204 }
205 else if (streq(function_name, "TNC_TNCS_ProvideRecommendation"))
206 {
207 *function_pointer = (void*)TNC_TNCS_ProvideRecommendation;
208 }
209 else
210 {
211 return TNC_RESULT_INVALID_PARAMETER;
212 }
213 return TNC_RESULT_SUCCESS;
214 }