implemented receive_message() function
[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 * List of message types supported by IMC
44 */
45 TNC_MessageTypeList supported_types;
46
47 /**
48 * Number of supported message types
49 */
50 TNC_UInt32 type_count;
51 };
52
53 METHOD(imv_t, set_id, void,
54 private_tnc_imv_t *this, TNC_IMVID id)
55 {
56 this->id = id;
57 }
58
59 METHOD(imv_t, get_id, TNC_IMVID,
60 private_tnc_imv_t *this)
61 {
62 return this->id;
63 }
64
65 METHOD(imv_t, get_name, char*,
66 private_tnc_imv_t *this)
67 {
68 return this->name;
69 }
70
71 METHOD(imv_t, set_message_types, void,
72 private_tnc_imv_t *this, TNC_MessageTypeList supported_types,
73 TNC_UInt32 type_count)
74 {
75 free(this->supported_types);
76 this->supported_types = NULL;
77 this->type_count = type_count;
78 if (type_count && supported_types)
79 {
80 size_t size = type_count * sizeof(TNC_MessageType);
81
82 this->supported_types = malloc(size);
83 memcpy(this->supported_types, supported_types, size);
84 }
85 }
86
87 METHOD(imv_t, type_supported, bool,
88 private_tnc_imv_t *this, TNC_MessageType message_type)
89 {
90 TNC_VendorID msg_vid, vid;
91 TNC_MessageSubtype msg_subtype, subtype;
92 int i;
93
94 msg_vid = (message_type >> 8) & TNC_VENDORID_ANY;
95 msg_subtype = message_type & TNC_SUBTYPE_ANY;
96
97 for (i = 0; i < this->type_count; i++)
98 {
99 vid = (this->supported_types[i] >> 8) & TNC_VENDORID_ANY;
100 subtype = this->supported_types[i] & TNC_SUBTYPE_ANY;
101
102 if (this->supported_types[i] == message_type
103 || (subtype == TNC_SUBTYPE_ANY
104 && (msg_vid == vid || vid == TNC_VENDORID_ANY))
105 || (vid == TNC_VENDORID_ANY
106 && (msg_subtype == subtype || subtype == TNC_SUBTYPE_ANY)))
107 {
108 return TRUE;
109 }
110 }
111 return FALSE;
112 }
113
114 METHOD(imv_t, destroy, void,
115 private_tnc_imv_t *this)
116 {
117 free(this->name);
118 free(this->supported_types);
119 free(this);
120 }
121
122 /**
123 * Described in header.
124 */
125 imv_t* tnc_imv_create(char *name, char *filename)
126 {
127 private_tnc_imv_t *this;
128 void *handle;
129
130 INIT(this,
131 .public = {
132 .set_id = _set_id,
133 .get_id = _get_id,
134 .get_name = _get_name,
135 .set_message_types = _set_message_types,
136 .type_supported = _type_supported,
137 .destroy = _destroy,
138 },
139 );
140
141 handle = dlopen(filename, RTLD_NOW);
142 if (handle == NULL)
143 {
144 DBG1(DBG_TNC, "IMV '%s' failed to load from '%s': %s",
145 name, filename, dlerror());
146 free(this);
147 return NULL;
148 }
149
150 /* we do not store or free dlopen() handles, leak_detective requires
151 * the modules to keep loaded until leak report */
152
153 this->public.initialize = dlsym(handle, "TNC_IMV_Initialize");
154 if (!this->public.initialize)
155 {
156 DBG1(DBG_TNC, "could not resolve TNC_IMV_Initialize in %s: %s\n",
157 filename, dlerror());
158 free(this);
159 return NULL;
160 }
161 this->public.notify_connection_change =
162 dlsym(handle, "TNC_IMV_NotifyConnectionChange");
163 this->public.solicit_recommendation =
164 dlsym(handle, "TNC_IMV_SolicitRecommendation");
165 if (!this->public.solicit_recommendation)
166 {
167 DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
168 filename, dlerror());
169 free(this);
170 return NULL;
171 }
172 this->public.receive_message =
173 dlsym(handle, "TNC_IMV_ReceiveMessage");
174 this->public.batch_ending =
175 dlsym(handle, "TNC_IMV_BatchEnding");
176 this->public.terminate =
177 dlsym(handle, "TNC_IMV_Terminate");
178 this->public.provide_bind_function =
179 dlsym(handle, "TNC_IMV_ProvideBindFunction");
180 if (!this->public.provide_bind_function)
181 {
182 DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
183 filename, dlerror());
184 free(this);
185 return NULL;
186 }
187 this->name = strdup(name);
188
189 return &this->public;
190 }