support of reportMessageTypes() 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, destroy, void,
88 private_tnc_imv_t *this)
89 {
90 free(this->name);
91 free(this->supported_types);
92 free(this);
93 }
94
95 /**
96 * Described in header.
97 */
98 imv_t* tnc_imv_create(char *name, char *filename)
99 {
100 private_tnc_imv_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, "IMV '%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_IMV_Initialize");
126 if (!this->public.initialize)
127 {
128 DBG1(DBG_TNC, "could not resolve TNC_IMV_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_IMV_NotifyConnectionChange");
135 this->public.solicit_recommendation =
136 dlsym(handle, "TNC_IMV_SolicitRecommendation");
137 if (!this->public.solicit_recommendation)
138 {
139 DBG1(DBG_TNC, "could not resolve TNC_IMV_SolicitRecommendation in %s: %s\n",
140 filename, dlerror());
141 free(this);
142 return NULL;
143 }
144 this->public.receive_message =
145 dlsym(handle, "TNC_IMV_ReceiveMessage");
146 this->public.batch_ending =
147 dlsym(handle, "TNC_IMV_BatchEnding");
148 this->public.terminate =
149 dlsym(handle, "TNC_IMV_Terminate");
150 this->public.provide_bind_function =
151 dlsym(handle, "TNC_IMV_ProvideBindFunction");
152 if (!this->public.provide_bind_function)
153 {
154 DBG1(DBG_TNC, "could not resolve TNC_IMV_ProvideBindFunction in %s: %s\n",
155 filename, dlerror());
156 free(this);
157 return NULL;
158 }
159 this->name = strdup(name);
160
161 return &this->public;
162 }