5f4a7e8106541453cdd5947b258acf6f4f88eae7
[strongswan.git] / src / charon / control / interface_manager.c
1 /**
2 * @file interface_manager.c
3 *
4 * @brief Implementation of interface_manager_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "interface_manager.h"
24
25 #include <sys/types.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <dlfcn.h>
29
30 #include <daemon.h>
31 #include <library.h>
32 #include <control/interfaces/interface.h>
33 #include <processing/job_queue.h>
34 #include <processing/jobs/initiate_job.h>
35
36
37 typedef struct private_interface_manager_t private_interface_manager_t;
38
39 /**
40 * Private data of an stroke_t object.
41 */
42 struct private_interface_manager_t {
43
44 /**
45 * Public part of stroke_t object.
46 */
47 interface_manager_t public;
48
49 /**
50 * a list of all loaded interfaces
51 */
52 linked_list_t *interfaces;
53
54 /**
55 * dlopen() handles of interfaces
56 */
57 linked_list_t *handles;
58 };
59
60 /**
61 * Implementation of interface_manager_t.initiate.
62 */
63 static status_t initiate(private_interface_manager_t *this,
64 peer_cfg_t *peer_cfg, child_cfg_t *child_cfg,
65 bool(*cb)(void*,signal_t,level_t,ike_sa_t*,char*,va_list),
66 void *param)
67 {
68 ike_sa_t *ours = NULL;
69 job_t *job;
70 status_t retval;
71
72 charon->bus->set_listen_state(charon->bus, TRUE);
73
74 job = (job_t*)initiate_job_create(peer_cfg, child_cfg);
75 charon->job_queue->add(charon->job_queue, job);
76
77 while (TRUE)
78 {
79 level_t level;
80 signal_t signal;
81 int thread;
82 ike_sa_t *ike_sa;
83 char* format;
84 va_list args;
85
86 signal = charon->bus->listen(charon->bus, &level, &thread,
87 &ike_sa, &format, &args);
88
89 if (cb && (ike_sa == ours || ours == NULL))
90 {
91 if (!cb(param, signal, level, ike_sa, format, args))
92 {
93 charon->bus->set_listen_state(charon->bus, FALSE);
94 return NEED_MORE;
95 }
96 }
97
98 switch (signal)
99 {
100 case CHILD_UP_SUCCESS:
101 if (ike_sa == ours)
102 {
103 retval = SUCCESS;
104 break;
105 }
106 continue;
107 case CHILD_UP_FAILED:
108 case IKE_UP_FAILED:
109 if (ike_sa == ours)
110 {
111 retval = FAILED;
112 break;
113 }
114 continue;
115 case CHILD_UP_START:
116 case IKE_UP_START:
117 if (ours == NULL)
118 {
119 ours = ike_sa;
120 }
121 continue;
122 default:
123 continue;
124 }
125 break;
126 }
127 charon->bus->set_listen_state(charon->bus, FALSE);
128 return retval;
129 }
130
131 /**
132 * load the control interface modules
133 */
134 static void load_interfaces(private_interface_manager_t *this)
135 {
136 struct dirent* entry;
137 struct stat stb;
138 DIR* dir;
139
140 if (stat(IPSEC_INTERFACEDIR, &stb) == -1 || !(stb.st_mode & S_IFDIR))
141 {
142 DBG1(DBG_CFG, "error opening interface modules directory "IPSEC_INTERFACEDIR);
143 return;
144 }
145
146 dir = opendir(IPSEC_INTERFACEDIR);
147 if (dir == NULL)
148 {
149 DBG1(DBG_CFG, "error opening interface modules directory "IPSEC_INTERFACEDIR);
150 return;
151 }
152
153 DBG1(DBG_CFG, "loading control interface modules from '"IPSEC_INTERFACEDIR"'");
154
155 while ((entry = readdir(dir)) != NULL)
156 {
157 char file[256];
158 interface_t *interface;
159 interface_constructor_t constructor;
160 void *handle;
161 char *ending;
162
163 snprintf(file, sizeof(file), IPSEC_INTERFACEDIR"/%s", entry->d_name);
164
165 if (stat(file, &stb) == -1 || !(stb.st_mode & S_IFREG))
166 {
167 DBG2(DBG_CFG, " skipping %s, doesn't look like a file",
168 entry->d_name);
169 continue;
170 }
171 ending = entry->d_name + strlen(entry->d_name) - 3;
172 if (ending <= entry->d_name || !streq(ending, ".so"))
173 {
174 /* skip anything which does not look like a library */
175 DBG2(DBG_CFG, " skipping %s, doesn't look like a library",
176 entry->d_name);
177 continue;
178 }
179 /* try to load the library */
180 handle = dlopen(file, RTLD_LAZY);
181 if (handle == NULL)
182 {
183 DBG1(DBG_CFG, " opening control interface module %s failed: %s",
184 entry->d_name, dlerror());
185 continue;
186 }
187 constructor = dlsym(handle, "interface_create");
188 if (constructor == NULL)
189 {
190 DBG1(DBG_CFG, " interface module %s has no interface_create() "
191 "function, skipped", entry->d_name);
192 dlclose(handle);
193 continue;
194 }
195
196 interface = constructor();
197 if (interface == NULL)
198 {
199 DBG1(DBG_CFG, " unable to create instance of interface "
200 "module %s, skipped", entry->d_name);
201 dlclose(handle);
202 continue;
203 }
204 DBG1(DBG_CFG, " loaded control interface module successfully from %s", entry->d_name);
205 this->interfaces->insert_last(this->interfaces, interface);
206 this->handles->insert_last(this->handles, handle);
207 }
208 closedir(dir);
209 }
210
211
212 /**
213 * Implementation of stroke_t.destroy.
214 */
215 static void destroy(private_interface_manager_t *this)
216 {
217 this->interfaces->destroy_offset(this->interfaces, offsetof(interface_t, destroy));
218 this->handles->destroy_function(this->handles, (void*)dlclose);
219 free(this);
220 }
221
222 /*
223 * Described in header-file
224 */
225 interface_manager_t *interface_manager_create(void)
226 {
227 private_interface_manager_t *this = malloc_thing(private_interface_manager_t);
228
229 this->public.initiate = (status_t(*)(interface_manager_t*,peer_cfg_t*,child_cfg_t*,bool(*)(void*,signal_t,level_t,ike_sa_t*,char*,va_list),void*))initiate;
230 this->public.destroy = (void (*)(interface_manager_t*))destroy;
231
232 this->interfaces = linked_list_create();
233 this->handles = linked_list_create();
234
235 load_interfaces(this);
236
237 return &this->public;
238 }
239