42d2ab06419c898f630d0b43b1a0cd04b1ed8a4a
[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 /**
26 * Private data of an imv_t object.
27 */
28 struct private_tnc_imv_t {
29
30 /**
31 * Public members of imv_t.
32 */
33 imv_t public;
34
35 /**
36 * Path of loaded IMV
37 */
38 char *path;
39
40 /**
41 * Name of loaded IMV
42 */
43 char *name;
44
45 /**
46 * Handle of loaded IMV
47 */
48 void *handle;
49
50 /**
51 * ID of loaded IMV
52 */
53 TNC_IMVID id;
54
55 /**
56 * List of message types supported by IMC
57 */
58 TNC_MessageTypeList supported_types;
59
60 /**
61 * Number of supported message types
62 */
63 TNC_UInt32 type_count;
64 };
65
66 METHOD(imv_t, set_id, void,
67 private_tnc_imv_t *this, TNC_IMVID id)
68 {
69 this->id = id;
70 }
71
72 METHOD(imv_t, get_id, TNC_IMVID,
73 private_tnc_imv_t *this)
74 {
75 return this->id;
76 }
77
78 METHOD(imv_t, get_name, char*,
79 private_tnc_imv_t *this)
80 {
81 return this->name;
82 }
83
84 METHOD(imv_t, set_message_types, void,
85 private_tnc_imv_t *this, TNC_MessageTypeList supported_types,
86 TNC_UInt32 type_count)
87 {
88 /* Free an existing MessageType list */
89 free(this->supported_types);
90 this->supported_types = NULL;
91
92 /* Store the new MessageType list */
93 this->type_count = type_count;
94 if (type_count && supported_types)
95 {
96 size_t size = type_count * sizeof(TNC_MessageType);
97
98 this->supported_types = malloc(size);
99 memcpy(this->supported_types, supported_types, size);
100 }
101 DBG2(DBG_TNC, "IMV %u supports %u message types", this->id, type_count);
102 }
103
104 METHOD(imv_t, type_supported, bool,
105 private_tnc_imv_t *this, TNC_MessageType message_type)
106 {
107 TNC_VendorID msg_vid, vid;
108 TNC_MessageSubtype msg_subtype, subtype;
109 int i;
110
111 msg_vid = (message_type >> 8) & TNC_VENDORID_ANY;
112 msg_subtype = message_type & TNC_SUBTYPE_ANY;
113
114 for (i = 0; i < this->type_count; i++)
115 {
116 vid = (this->supported_types[i] >> 8) & TNC_VENDORID_ANY;
117 subtype = this->supported_types[i] & TNC_SUBTYPE_ANY;
118
119 if (this->supported_types[i] == message_type
120 || (subtype == TNC_SUBTYPE_ANY
121 && (msg_vid == vid || vid == TNC_VENDORID_ANY))
122 || (vid == TNC_VENDORID_ANY
123 && (msg_subtype == subtype || subtype == TNC_SUBTYPE_ANY)))
124 {
125 return TRUE;
126 }
127 }
128 return FALSE;
129 }
130
131 METHOD(imv_t, destroy, void,
132 private_tnc_imv_t *this)
133 {
134 dlclose(this->handle);
135 free(this->supported_types);
136 free(this->name);
137 free(this->path);
138 free(this);
139 }
140
141 /**
142 * Described in header.
143 */
144 imv_t* tnc_imv_create(char *name, char *path)
145 {
146 private_tnc_imv_t *this;
147
148 INIT(this,
149 .public = {
150 .set_id = _set_id,
151 .get_id = _get_id,
152 .get_name = _get_name,
153 .set_message_types = _set_message_types,
154 .type_supported = _type_supported,
155 .destroy = _destroy,
156 },
157 .name = name,
158 .path = path,
159 );
160
161 this->handle = dlopen(path, RTLD_LAZY);
162 if (!this->handle)
163 {
164 DBG1(DBG_TNC, "IMV \"%s\" failed to load: %s", name, dlerror());
165 free(this);
166 return NULL;
167 }
168
169 this->public.initialize = dlsym(this->handle, "TNC_IMV_Initialize");
170 if (!this->public.initialize)
171 {
172 DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n",
173 path, dlerror());
174 dlclose(this->handle);
175 free(this);
176 return NULL;
177 }
178 this->public.notify_connection_change =
179 dlsym(this->handle, "TNC_IMV_NotifyConnectionChange");
180 this->public.solicit_recommendation =
181 dlsym(this->handle, "TNC_IMV_SolicitRecommendation");
182 if (!this->public.solicit_recommendation)
183 {
184 DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
185 path, dlerror());
186 dlclose(this->handle);
187 free(this);
188 return NULL;
189 }
190 this->public.receive_message =
191 dlsym(this->handle, "TNC_IMV_ReceiveMessage");
192 this->public.batch_ending =
193 dlsym(this->handle, "TNC_IMV_BatchEnding");
194 this->public.terminate =
195 dlsym(this->handle, "TNC_IMV_Terminate");
196 this->public.provide_bind_function =
197 dlsym(this->handle, "TNC_IMV_ProvideBindFunction");
198 if (!this->public.provide_bind_function)
199 {
200 DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
201 path, dlerror());
202 dlclose(this->handle);
203 free(this);
204 return NULL;
205 }
206
207 return &this->public;
208 }