b4c6ce3a6d0d94e36116c1a31bbdc7876f00029a
[strongswan.git] / src / libfast / fast_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 "fast_dispatcher.h"
17
18 #include "fast_request.h"
19 #include "fast_session.h"
20
21 #include <fcgiapp.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 #include <utils/debug.h>
26 #include <threading/thread.h>
27 #include <threading/condvar.h>
28 #include <threading/mutex.h>
29 #include <collections/linked_list.h>
30 #include <collections/hashtable.h>
31
32 /** Intervall to check for expired sessions, in seconds */
33 #define CLEANUP_INTERVAL 30
34
35 typedef struct private_fast_dispatcher_t private_fast_dispatcher_t;
36
37 /**
38 * private data of the task manager
39 */
40 struct private_fast_dispatcher_t {
41
42 /**
43 * public functions
44 */
45 fast_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 fast_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 fast_controller_constructor_t constructor;
111 /** parameter to constructor */
112 void *param;
113 } controller_entry_t;
114
115 typedef struct {
116 /** constructor function */
117 fast_filter_constructor_t constructor;
118 /** parameter to constructor */
119 void *param;
120 } filter_entry_t;
121
122 typedef struct {
123 /** session instance */
124 fast_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 fast_session_t* load_session(private_fast_dispatcher_t *this)
141 {
142 enumerator_t *enumerator;
143 controller_entry_t *centry;
144 filter_entry_t *fentry;
145 fast_session_t *session;
146 fast_context_t *context = NULL;
147 fast_controller_t *controller;
148 fast_filter_t *filter;
149
150 if (this->context_constructor)
151 {
152 context = this->context_constructor(this->param);
153 }
154 session = fast_session_create(context);
155 if (!session)
156 {
157 return NULL;
158 }
159
160 enumerator = this->controllers->create_enumerator(this->controllers);
161 while (enumerator->enumerate(enumerator, &centry))
162 {
163 controller = centry->constructor(context, centry->param);
164 session->add_controller(session, controller);
165 }
166 enumerator->destroy(enumerator);
167
168 enumerator = this->filters->create_enumerator(this->filters);
169 while (enumerator->enumerate(enumerator, &fentry))
170 {
171 filter = fentry->constructor(context, fentry->param);
172 session->add_filter(session, filter);
173 }
174 enumerator->destroy(enumerator);
175
176 return session;
177 }
178
179 /**
180 * create a new session entry
181 */
182 static session_entry_t *session_entry_create(private_fast_dispatcher_t *this,
183 char *host)
184 {
185 session_entry_t *entry;
186 fast_session_t *session;
187
188 session = load_session(this);
189 if (!session)
190 {
191 return NULL;
192 }
193 INIT(entry,
194 .cond = condvar_create(CONDVAR_TYPE_DEFAULT),
195 .session = session,
196 .host = strdup(host),
197 .used = time_monotonic(NULL),
198 );
199 return entry;
200 }
201
202 /**
203 * destroy a session
204 */
205 static void session_entry_destroy(session_entry_t *entry)
206 {
207 entry->session->destroy(entry->session);
208 entry->cond->destroy(entry->cond);
209 free(entry->host);
210 free(entry);
211 }
212
213 METHOD(fast_dispatcher_t, add_controller, void,
214 private_fast_dispatcher_t *this, fast_controller_constructor_t constructor,
215 void *param)
216 {
217 controller_entry_t *entry;
218
219 INIT(entry,
220 .constructor = constructor,
221 .param = param,
222 );
223 this->controllers->insert_last(this->controllers, entry);
224 }
225
226 METHOD(fast_dispatcher_t, add_filter, void,
227 private_fast_dispatcher_t *this, fast_filter_constructor_t constructor,
228 void *param)
229 {
230 filter_entry_t *entry;
231
232 INIT(entry,
233 .constructor = constructor,
234 .param = param,
235 );
236 this->filters->insert_last(this->filters, entry);
237 }
238
239 /**
240 * Hashtable hash function
241 */
242 static u_int session_hash(char *sid)
243 {
244 return chunk_hash(chunk_create(sid, strlen(sid)));
245 }
246
247 /**
248 * Hashtable equals function
249 */
250 static bool session_equals(char *sid1, char *sid2)
251 {
252 return streq(sid1, sid2);
253 }
254
255 /**
256 * Cleanup unused sessions
257 */
258 static void cleanup_sessions(private_fast_dispatcher_t *this, time_t now)
259 {
260 if (this->last_cleanup < now - CLEANUP_INTERVAL)
261 {
262 char *sid;
263 session_entry_t *entry;
264 enumerator_t *enumerator;
265 linked_list_t *remove;
266
267 this->last_cleanup = now;
268 remove = linked_list_create();
269 enumerator = this->sessions->create_enumerator(this->sessions);
270 while (enumerator->enumerate(enumerator, &sid, &entry))
271 {
272 /* check all sessions for timeout or close flag */
273 if (!entry->in_use &&
274 (entry->used < now - this->timeout || entry->closed))
275 {
276 remove->insert_last(remove, sid);
277 }
278 }
279 enumerator->destroy(enumerator);
280
281 while (remove->remove_last(remove, (void**)&sid) == SUCCESS)
282 {
283 entry = this->sessions->remove(this->sessions, sid);
284 if (entry)
285 {
286 session_entry_destroy(entry);
287 }
288 }
289 remove->destroy(remove);
290 }
291 }
292
293 /**
294 * Actual dispatching code
295 */
296 static void dispatch(private_fast_dispatcher_t *this)
297 {
298 thread_cancelability(FALSE);
299
300 while (TRUE)
301 {
302 fast_request_t *request;
303 session_entry_t *found = NULL;
304 time_t now;
305 char *sid;
306
307 thread_cancelability(TRUE);
308 request = fast_request_create(this->fd, this->debug);
309 thread_cancelability(FALSE);
310
311 if (request == NULL)
312 {
313 break;
314 }
315 now = time_monotonic(NULL);
316 sid = request->get_cookie(request, "SID");
317
318 this->mutex->lock(this->mutex);
319 if (sid)
320 {
321 found = this->sessions->get(this->sessions, sid);
322 }
323 if (found && !streq(found->host, request->get_host(request)))
324 {
325 found = NULL;
326 }
327 if (found)
328 {
329 /* wait until session is unused */
330 while (found->in_use)
331 {
332 found->cond->wait(found->cond, this->mutex);
333 }
334 }
335 else
336 { /* create a new session if not found */
337 found = session_entry_create(this, request->get_host(request));
338 if (!found)
339 {
340 request->destroy(request);
341 this->mutex->unlock(this->mutex);
342 continue;
343 }
344 sid = found->session->get_sid(found->session);
345 this->sessions->put(this->sessions, sid, found);
346 }
347 found->in_use = TRUE;
348 this->mutex->unlock(this->mutex);
349
350 /* start processing */
351 found->session->process(found->session, request);
352 found->used = time_monotonic(NULL);
353
354 /* release session */
355 this->mutex->lock(this->mutex);
356 found->in_use = FALSE;
357 found->closed = request->session_closed(request);
358 found->cond->signal(found->cond);
359 cleanup_sessions(this, now);
360 this->mutex->unlock(this->mutex);
361
362 request->destroy(request);
363 }
364 }
365
366 METHOD(fast_dispatcher_t, run, void,
367 private_fast_dispatcher_t *this, int threads)
368 {
369 this->thread_count = threads;
370 this->threads = malloc(sizeof(thread_t*) * threads);
371 while (threads)
372 {
373 this->threads[threads - 1] = thread_create((thread_main_t)dispatch,
374 this);
375 if (this->threads[threads - 1])
376 {
377 threads--;
378 }
379 }
380 }
381
382 METHOD(fast_dispatcher_t, waitsignal, void,
383 private_fast_dispatcher_t *this)
384 {
385 sigset_t set;
386
387 sigemptyset(&set);
388 sigaddset(&set, SIGINT);
389 sigaddset(&set, SIGTERM);
390 sigaddset(&set, SIGHUP);
391 sigprocmask(SIG_BLOCK, &set, NULL);
392 sigwaitinfo(&set, NULL);
393 }
394
395 METHOD(fast_dispatcher_t, destroy, void,
396 private_fast_dispatcher_t *this)
397 {
398 char *sid;
399 session_entry_t *entry;
400 enumerator_t *enumerator;
401
402 FCGX_ShutdownPending();
403 while (this->thread_count--)
404 {
405 thread_t *thread = this->threads[this->thread_count];
406 thread->cancel(thread);
407 thread->join(thread);
408 }
409 enumerator = this->sessions->create_enumerator(this->sessions);
410 while (enumerator->enumerate(enumerator, &sid, &entry))
411 {
412 session_entry_destroy(entry);
413 }
414 enumerator->destroy(enumerator);
415 this->sessions->destroy(this->sessions);
416 this->controllers->destroy_function(this->controllers, free);
417 this->filters->destroy_function(this->filters, free);
418 this->mutex->destroy(this->mutex);
419 free(this->threads);
420 free(this);
421 }
422
423 /*
424 * see header file
425 */
426 fast_dispatcher_t *fast_dispatcher_create(char *socket, bool debug, int timeout,
427 fast_context_constructor_t constructor, void *param)
428 {
429 private_fast_dispatcher_t *this;
430
431 INIT(this,
432 .public = {
433 .add_controller = _add_controller,
434 .add_filter = _add_filter,
435 .run = _run,
436 .waitsignal = _waitsignal,
437 .destroy = _destroy,
438 },
439 .sessions = hashtable_create((void*)session_hash,
440 (void*)session_equals, 4096),
441 .controllers = linked_list_create(),
442 .filters = linked_list_create(),
443 .context_constructor = constructor,
444 .mutex = mutex_create(MUTEX_TYPE_DEFAULT),
445 .param = param,
446 .timeout = timeout,
447 .last_cleanup = time_monotonic(NULL),
448 .debug = debug,
449 );
450
451 FCGX_Init();
452
453 if (socket)
454 {
455 unlink(socket);
456 this->fd = FCGX_OpenSocket(socket, 10);
457 }
458 return &this->public;
459 }