f87853bf916f1e2c9112fcd56ad7e38166bbf7ee
[strongswan.git] / src / libcharon / plugins / tnc_imc / tnc_imc.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_imc.h"
17
18 #include <dlfcn.h>
19
20 #include <debug.h>
21 #include <library.h>
22
23 typedef struct private_tnc_imc_t private_tnc_imc_t;
24
25 struct private_tnc_imc_t {
26
27 /**
28 * Public members of imc_t.
29 */
30 imc_t public;
31
32 /**
33 * Name of loaded IMC
34 */
35 char *name;
36
37 /**
38 * ID of loaded IMC
39 */
40 TNC_IMCID 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(imc_t, set_id, void,
54 private_tnc_imc_t *this, TNC_IMCID id)
55 {
56 this->id = id;
57 }
58
59 METHOD(imc_t, get_id, TNC_IMCID,
60 private_tnc_imc_t *this)
61 {
62 return this->id;
63 }
64
65 METHOD(imc_t, get_name, char*,
66 private_tnc_imc_t *this)
67 {
68 return this->name;
69 }
70
71 METHOD(imc_t, set_message_types, void,
72 private_tnc_imc_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(imc_t, destroy, void,
88 private_tnc_imc_t *this)
89 {
90 free(this->name);
91 free(this->supported_types);
92 free(this);
93 }
94
95 /**
96 * Described in header.
97 */
98 imc_t* tnc_imc_create(char* name, char *filename)
99 {
100 private_tnc_imc_t *this;
101 void *handle;
102
103 INIT(this,
104 .public = {
105 .set_id = _set_id,
106 .get_id = _get_id,
107 .get_name = _get_name,
108 .set_message_types = _set_message_types,
109 .destroy = _destroy,
110 },
111 );
112
113 handle = dlopen(filename, RTLD_NOW);
114 if (handle == NULL)
115 {
116 DBG1(DBG_TNC, "IMC '%s' failed to load from '%s': %s",
117 name, filename, dlerror());
118 free(this);
119 return NULL;
120 }
121
122 /* we do not store or free dlopen() handles, leak_detective requires
123 * the modules to keep loaded until leak report */
124
125 this->public.initialize = dlsym(handle, "TNC_IMC_Initialize");
126 if (!this->public.initialize)
127 {
128 DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n",
129 filename, dlerror());
130 free(this);
131 return NULL;
132 }
133 this->public.notify_connection_change =
134 dlsym(handle, "TNC_IMC_NotifyConnectionChange");
135 this->public.begin_handshake = dlsym(handle, "TNC_IMC_BeginHandshake");
136 if (!this->public.begin_handshake)
137 {
138 DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
139 filename, dlerror());
140 free(this);
141 return NULL;
142 }
143 this->public.receive_message =
144 dlsym(handle, "TNC_IMC_ReceiveMessage");
145 this->public.batch_ending =
146 dlsym(handle, "TNC_IMC_BatchEnding");
147 this->public.terminate =
148 dlsym(handle, "TNC_IMC_Terminate");
149 this->public.provide_bind_function =
150 dlsym(handle, "TNC_IMC_ProvideBindFunction");
151 if (!this->public.provide_bind_function)
152 {
153 DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
154 filename, dlerror());
155 free(this);
156 return NULL;
157 }
158 this->name = strdup(name);
159
160 return &this->public;
161 }