2 * Copyright (C) 2006 Mike McCauley
3 * Copyright (C) 2010 Andreas Steffen, HSR Hochschule fuer Technik Rapperswil
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>.
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
22 #include <threading/mutex.h>
24 typedef struct private_tnc_imc_t private_tnc_imc_t
;
27 * Private data of an imc_t object.
29 struct private_tnc_imc_t
{
32 * Public members of imc_t.
47 * Handle of loaded IMC
57 * List of message types supported by IMC
59 TNC_MessageTypeList supported_types
;
62 * Number of supported message types
64 TNC_UInt32 type_count
;
67 * mutex to lock the imc_t object
72 METHOD(imc_t
, set_id
, void,
73 private_tnc_imc_t
*this, TNC_IMCID id
)
78 METHOD(imc_t
, get_id
, TNC_IMCID
,
79 private_tnc_imc_t
*this)
84 METHOD(imc_t
, get_name
, char*,
85 private_tnc_imc_t
*this)
90 METHOD(imc_t
, set_message_types
, void,
91 private_tnc_imc_t
*this, TNC_MessageTypeList supported_types
,
92 TNC_UInt32 type_count
)
96 int len
= sizeof(buf
);
99 /* lock the imc_t instance */
100 this->mutex
->lock(this->mutex
);
102 /* Free an existing MessageType list */
103 free(this->supported_types
);
104 this->supported_types
= NULL
;
106 /* Store the new MessageType list */
107 this->type_count
= type_count
;
108 if (type_count
&& supported_types
)
110 size_t size
= type_count
* sizeof(TNC_MessageType
);
113 for (i
= 0; i
< type_count
; i
++)
115 written
= snprintf(pos
, len
, " 0x%08x", supported_types
[i
]);
123 this->supported_types
= malloc(size
);
124 memcpy(this->supported_types
, supported_types
, size
);
127 DBG2(DBG_TNC
, "IMC %u supports %u message type%s:%s",
128 this->id
, type_count
, (type_count
== 1) ?
"":"s", buf
);
130 /* lock the imc_t instance */
131 this->mutex
->unlock(this->mutex
);
134 METHOD(imc_t
, type_supported
, bool,
135 private_tnc_imc_t
*this, TNC_MessageType message_type
)
137 TNC_VendorID msg_vid
, vid
;
138 TNC_MessageSubtype msg_subtype
, subtype
;
141 msg_vid
= (message_type
>> 8) & TNC_VENDORID_ANY
;
142 msg_subtype
= message_type
& TNC_SUBTYPE_ANY
;
144 for (i
= 0; i
< this->type_count
; i
++)
146 vid
= (this->supported_types
[i
] >> 8) & TNC_VENDORID_ANY
;
147 subtype
= this->supported_types
[i
] & TNC_SUBTYPE_ANY
;
149 if (this->supported_types
[i
] == message_type
150 || (subtype
== TNC_SUBTYPE_ANY
151 && (msg_vid
== vid
|| vid
== TNC_VENDORID_ANY
))
152 || (vid
== TNC_VENDORID_ANY
153 && (msg_subtype
== subtype
|| subtype
== TNC_SUBTYPE_ANY
)))
161 METHOD(imc_t
, destroy
, void,
162 private_tnc_imc_t
*this)
164 dlclose(this->handle
);
165 this->mutex
->destroy(this->mutex
);
166 free(this->supported_types
);
173 * Described in header.
175 imc_t
* tnc_imc_create(char *name
, char *path
)
177 private_tnc_imc_t
*this;
183 .get_name
= _get_name
,
184 .set_message_types
= _set_message_types
,
185 .type_supported
= _type_supported
,
190 .mutex
= mutex_create(MUTEX_TYPE_DEFAULT
),
193 this->handle
= dlopen(path
, RTLD_LAZY
);
196 DBG1(DBG_TNC
, "IMC \"%s\" failed to load: %s", name
, dlerror());
201 this->public.initialize
= dlsym(this->handle
, "TNC_IMC_Initialize");
202 if (!this->public.initialize
)
204 DBG1(DBG_TNC
, "could not resolve TNC_IMC_Initialize in %s: %s\n",
206 dlclose(this->handle
);
210 this->public.notify_connection_change
=
211 dlsym(this->handle
, "TNC_IMC_NotifyConnectionChange");
212 this->public.begin_handshake
= dlsym(this->handle
, "TNC_IMC_BeginHandshake");
213 if (!this->public.begin_handshake
)
215 DBG1(DBG_TNC
, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
217 dlclose(this->handle
);
221 this->public.receive_message
=
222 dlsym(this->handle
, "TNC_IMC_ReceiveMessage");
223 this->public.batch_ending
=
224 dlsym(this->handle
, "TNC_IMC_BatchEnding");
225 this->public.terminate
=
226 dlsym(this->handle
, "TNC_IMC_Terminate");
227 this->public.provide_bind_function
=
228 dlsym(this->handle
, "TNC_IMC_ProvideBindFunction");
229 if (!this->public.provide_bind_function
)
231 DBG1(DBG_TNC
, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
233 dlclose(this->handle
);
238 return &this->public;