refactored PA-TNC message handling by IMVs
[strongswan.git] / src / libfast / dispatcher.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * 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 "dispatcher.h"
17
18 #include "request.h"
19 #include "session.h"
20
21 #include <fcgiapp.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include <debug.h>
26 #include <threading/thread.h>
27 #include <threading/condvar.h>
28 #include <threading/mutex.h>
29 #include <utils/linked_list.h>
30 #include <utils/hashtable.h>
31
32 /** Intervall to check for expired sessions, in seconds */
33 #define CLEANUP_INTERVAL 30
34
35 typedef struct private_dispatcher_t private_dispatcher_t;
36
37 /**
38 * private data of the task manager
39 */
40 struct private_dispatcher_t {
41
42 /**
43 * public functions
44 */
45 dispatcher_t public;
46
47 /**
48 * fcgi socket fd
49 */
50 int fd;
51
52 /**
53 * thread list
54 */
55 thread_t **threads;
56
57 /**
58 * number of threads in "threads"
59 */
60 int thread_count;
61
62 /**
63 * session locking mutex
64 */
65 mutex_t *mutex;
66
67 /**
68 * Hahstable with active sessions
69 */
70 hashtable_t *sessions;
71
72 /**
73 * session timeout
74 */
75 time_t timeout;
76
77 /**
78 * timestamp of last session cleanup round
79 */
80 time_t last_cleanup;
81
82 /**
83 * running in debug mode?
84 */
85 bool debug;
86
87 /**
88 * List of controllers controller_constructor_t
89 */
90 linked_list_t *controllers;
91
92 /**
93 * List of filters filter_constructor_t
94 */
95 linked_list_t *filters;
96
97 /**
98 * constructor function to create session context (in controller_entry_t)
99 */
100 context_constructor_t context_constructor;
101
102 /**
103 * user param to context constructor
104 */
105 void *param;
106 };
107
108 typedef struct {
109 /** constructor function */
110 controller_constructor_t constructor;
111 /** parameter to constructor */
112 void *param;
113 } controller_entry_t;
114
115 typedef struct {
116 /** constructor function */
117 filter_constructor_t constructor;
118 /** parameter to constructor */
119 void *param;
120 } filter_entry_t;
121
122 typedef struct {
123 /** session instance */
124 session_t *session;
125 /** condvar to wait for session */
126 condvar_t *cond;
127 /** client host address, to prevent session hijacking */
128 char *host;
129 /** TRUE if session is in use */
130 bool in_use;
131 /** last use of the session */
132 time_t used;
133 /** has the session been closed by the handler? */
134 bool closed;
135 } session_entry_t;
136
137 /**
138 * create a session and instanciate controllers
139 */
140 static session_t* load_session(private_dispatcher_t *this)
141 {
142 enumerator_t *enumerator;
143 controller_entry_t *centry;
144 filter_entry_t *fentry;
145 session_t *session;
146 context_t *context = NULL;
147 controller_t *controller;
148 filter_t *filter;
149
150 if (this->context_constructor)
151 {
152 context = this->context_constructor(this->param);
153 }
154 session = session_create(context);
155
156 enumerator = this->controllers->create_enumerator(this->controllers);
157 while (enumerator->enumerate(enumerator, &centry))
158 {
159 controller = centry->constructor(context, centry->param);
160 session->add_controller(session, controller);
161 }
162 enumerator->destroy(enumerator);
163
164 enumerator = this->filters->create_enumerator(this->filters);
165 while (enumerator->enumerate(enumerator, &fentry))
166 {
167 filter = fentry->constructor(context, fentry->param);
168 session->add_filter(session, filter);
169 }
170 enumerator->destroy(enumerator);
171
172 return session;
173 }
174
175 /**
176 * create a new session entry
177 */
178 static session_entry_t *session_entry_create(private_dispatcher_t *this,
179 char *host)
180 {
181 session_entry_t *entry;
182 session_t *session;
183
184 session = load_session(this);
185 if (!session)
186 {
187 return NULL;
188 }
189 INIT(entry,
190 .cond = condvar_create(CONDVAR_TYPE_DEFAULT),
191 .session = session,
192 .host = strdup(host),
193 .used = time_monotonic(NULL),
194 );
195 return entry;
196 }
197
198 /**
199 * destroy a session
200 */
201 static void session_entry_destroy(session_entry_t *entry)
202 {
203 entry->session->destroy(entry->session);
204 entry->cond->destroy(entry->cond);
205 free(entry->host);
206 free(entry);
207 }
208
209 METHOD(dispatcher_t, add_controller, void,
210 private_dispatcher_t *this, controller_constructor_t constructor,
211 void *param)
212 {
213 controller_entry_t *entry;
214
215 INIT(entry,
216 .constructor = constructor,
217 .param = param,
218 );
219 this->controllers->insert_last(this->controllers, entry);
220 }
221
222 METHOD(dispatcher_t, add_filter, void,
223 private_dispatcher_t *this, filter_constructor_t constructor, void *param)
224 {
225 filter_entry_t *entry;
226
227 INIT(entry,
228 .constructor = constructor,
229 .param = param,
230 );
231 this->filters->insert_last(this->filters, entry);
232 }
233
234 /**
235 * Hashtable hash function
236 */
237 static u_int session_hash(char *sid)
238 {
239 return chunk_hash(chunk_create(sid, strlen(sid)));
240 }
241
242 /**
243 * Hashtable equals function
244 */
245 static bool session_equals(char *sid1, char *sid2)
246 {
247 return streq(sid1, sid2);
248 }
249
250 /**
251 * Cleanup unused sessions
252 */
253 static void cleanup_sessions(private_dispatcher_t *this, time_t now)
254 {
255 if (this->last_cleanup < now - CLEANUP_INTERVAL)
256 {
257 char *sid;
258 session_entry_t *entry;
259 enumerator_t *enumerator;
260 linked_list_t *remove;
261
262 this->last_cleanup = now;
263 remove = linked_list_create();
264 enumerator = this->sessions->create_enumerator(this->sessions);
265 while (enumerator->enumerate(enumerator, &sid, &entry))
266 {
267 /* check all sessions for timeout or close flag */
268 if (!entry->in_use &&
269 (entry->used < now - this->timeout || entry->closed))
270 {
271 remove->insert_last(remove, sid);
272 }
273 }
274 enumerator->destroy(enumerator);
275
276 while (remove->remove_last(remove, (void**)&sid) == SUCCESS)
277 {
278 entry = this->sessions->remove(this->sessions, sid);
279 if (entry)
280 {
281 session_entry_destroy(entry);
282 }
283 }
284 remove->destroy(remove);
285 }
286 }
287
288 /**
289 * Actual dispatching code
290 */
291 static void dispatch(private_dispatcher_t *this)
292 {
293 thread_cancelability(FALSE);
294
295 while (TRUE)
296 {
297 request_t *request;
298 session_entry_t *found = NULL;
299 time_t now;
300 char *sid;
301
302 thread_cancelability(TRUE);
303 request = request_create(this->fd, this->debug);
304 thread_cancelability(FALSE);
305
306 if (request == NULL)
307 {
308 continue;
309 }
310 now = time_monotonic(NULL);
311 sid = request->get_cookie(request, "SID");
312
313 this->mutex->lock(this->mutex);
314 if (sid)
315 {
316 found = this->sessions->get(this->sessions, sid);
317 }
318 if (found && !streq(found->host, request->get_host(request)))
319 {
320 found = NULL;
321 }
322 if (found)
323 {
324 /* wait until session is unused */
325 while (found->in_use)
326 {
327 found->cond->wait(found->cond, this->mutex);
328 }
329 }
330 else
331 { /* create a new session if not found */
332 found = session_entry_create(this, request->get_host(request));
333 if (!found)
334 {
335 request->destroy(request);
336 this->mutex->unlock(this->mutex);
337 continue;
338 }
339 sid = found->session->get_sid(found->session);
340 this->sessions->put(this->sessions, sid, found);
341 }
342 found->in_use = TRUE;
343 this->mutex->unlock(this->mutex);
344
345 /* start processing */
346 found->session->process(found->session, request);
347 found->used = time_monotonic(NULL);
348
349 /* release session */
350 this->mutex->lock(this->mutex);
351 found->in_use = FALSE;
352 found->closed = request->session_closed(request);
353 found->cond->signal(found->cond);
354 cleanup_sessions(this, now);
355 this->mutex->unlock(this->mutex);
356
357 request->destroy(request);
358 }
359 }
360
361 METHOD(dispatcher_t, run, void,
362 private_dispatcher_t *this, int threads)
363 {
364 this->thread_count = threads;
365 this->threads = malloc(sizeof(thread_t*) * threads);
366 while (threads)
367 {
368 this->threads[threads - 1] = thread_create((thread_main_t)dispatch,
369 this);
370 if (this->threads[threads - 1])
371 {
372 threads--;
373 }
374 }
375 }
376
377 METHOD(dispatcher_t, waitsignal, void,
378 private_dispatcher_t *this)
379 {
380 sigset_t set;
381 int sig;
382
383 sigemptyset(&set);
384 sigaddset(&set, SIGINT);
385 sigaddset(&set, SIGTERM);
386 sigaddset(&set, SIGHUP);
387 sigprocmask(SIG_BLOCK, &set, NULL);
388 sigwait(&set, &sig);
389 }
390
391 METHOD(dispatcher_t, destroy, void,
392 private_dispatcher_t *this)
393 {
394 char *sid;
395 session_entry_t *entry;
396 enumerator_t *enumerator;
397
398 FCGX_ShutdownPending();
399 while (this->thread_count--)
400 {
401 thread_t *thread = this->threads[this->thread_count];
402 thread->cancel(thread);
403 thread->join(thread);
404 }
405 enumerator = this->sessions->create_enumerator(this->sessions);
406 while (enumerator->enumerate(enumerator, &sid, &entry))
407 {
408 session_entry_destroy(entry);
409 }
410 enumerator->destroy(enumerator);
411 this->sessions->destroy(this->sessions);
412 this->controllers->destroy_function(this->controllers, free);
413 this->filters->destroy_function(this->filters, free);
414 this->mutex->destroy(this->mutex);
415 free(this->threads);
416 free(this);
417 }
418
419 /*
420 * see header file
421 */
422 dispatcher_t *dispatcher_create(char *socket, bool debug, int timeout,
423 context_constructor_t constructor, void *param)
424 {
425 private_dispatcher_t *this;
426
427 INIT(this,
428 .public = {
429 .add_controller = _add_controller,
430 .add_filter = _add_filter,
431 .run = _run,
432 .waitsignal = _waitsignal,
433 .destroy = _destroy,
434 },
435 .sessions = hashtable_create((void*)session_hash,
436 (void*)session_equals, 4096),
437 .controllers = linked_list_create(),
438 .filters = linked_list_create(),
439 .context_constructor = constructor,
440 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
441 .param = param,
442 .timeout = timeout,
443 .last_cleanup = time_monotonic(NULL),
444 .debug = debug,
445 );
446
447 FCGX_Init();
448
449 if (socket)
450 {
451 unlink(socket);
452 this->fd = FCGX_OpenSocket(socket, 10);
453 }
454 return &this->public;
455 }
456