implemented IMC/IMV handler
[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 METHOD(imc_t, get_id, TNC_IMCID,
44 private_tnc_imc_t *this)
45 {
46 return this->id;
47 }
48
49 METHOD(imc_t, get_name, char*,
50 private_tnc_imc_t *this)
51 {
52 return this->name;
53 }
54
55 METHOD(imc_t, destroy, void,
56 private_tnc_imc_t *this)
57 {
58 free(this->name);
59 free(this);
60 }
61
62 /**
63 * Described in header.
64 */
65 imc_t* tnc_imc_create(char* name, char *filename, TNC_IMCID id)
66 {
67 private_tnc_imc_t *this;
68 void *handle;
69
70 INIT(this,
71 .public = {
72 .get_id = _get_id,
73 .destroy = _destroy,
74 },
75 );
76
77 handle = dlopen(filename, RTLD_NOW);
78 if (handle == NULL)
79 {
80 DBG1(DBG_TNC, "IMC '%s' failed to load from '%s': %s",
81 name, filename, dlerror());
82 free(this->name);
83 free(this);
84 return NULL;
85 }
86
87 /* we do not store or free dlopen() handles, leak_detective requires
88 * the modules to keep loaded until leak report */
89
90 this->public.initialize = dlsym(handle, "TNC_IMC_Initialize");
91 if (!this->public.initialize)
92 {
93 DBG1(DBG_TNC, "could not resolve TNC_IMC_Initialize in %s: %s\n",
94 filename, dlerror());
95 free(this);
96 return NULL;
97 }
98 this->public.notify_connection_change =
99 dlsym(handle, "TNC_IMC_NotifyConnectionChange");
100 this->public.begin_handshake = dlsym(handle, "TNC_IMC_BeginHandshake");
101 if (!this->public.begin_handshake)
102 {
103 DBG1(DBG_TNC, "could not resolve TNC_IMC_BeginHandshake in %s: %s\n",
104 filename, dlerror());
105 free(this);
106 return NULL;
107 }
108 this->public.receive_message =
109 dlsym(handle, "TNC_IMC_ReceiveMessage");
110 this->public.batch_ending =
111 dlsym(handle, "TNC_IMC_BatchEnding");
112 this->public.terminate =
113 dlsym(handle, "TNC_IMC_Terminate");
114 this->public.provide_bind_function =
115 dlsym(handle, "TNC_IMC_ProvideBindFunction");
116 if (!this->public.provide_bind_function)
117 {
118 DBG1(DBG_TNC, "could not resolve TNC_IMC_ProvideBindFunction in %s: %s\n",
119 filename, dlerror());
120 free(this);
121 return NULL;
122 }
123 DBG2(DBG_TNC, "IMC '%s' loaded successfully with ID %u", name, id);
124 this->name = strdup(name);
125 this->id = id;
126
127 return &this->public;
128 }
129