Moving charon to libcharon.
[strongswan.git] / src / libcharon / plugins / load_tester / load_tester_listener.c
1 /*
2 * Copyright (C) 2008 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 "load_tester_listener.h"
17
18 #include <signal.h>
19
20 #include <daemon.h>
21 #include <processing/jobs/delete_ike_sa_job.h>
22
23 typedef struct private_load_tester_listener_t private_load_tester_listener_t;
24
25 /**
26 * Private data of an load_tester_listener_t object
27 */
28 struct private_load_tester_listener_t {
29 /**
30 * Public part
31 */
32 load_tester_listener_t public;
33
34 /**
35 * Delete IKE_SA after it has been established
36 */
37 bool delete_after_established;
38
39 /**
40 * Number of established SAs
41 */
42 u_int established;
43
44 /**
45 * Shutdown the daemon if we have established this SA count
46 */
47 u_int shutdown_on;
48 };
49
50 /**
51 * Implementation of listener_t.ike_state_change
52 */
53 static bool ike_state_change(private_load_tester_listener_t *this,
54 ike_sa_t *ike_sa, ike_sa_state_t state)
55 {
56 if (state == IKE_ESTABLISHED)
57 {
58 ike_sa_id_t *id = ike_sa->get_id(ike_sa);
59
60 if (this->delete_after_established)
61 {
62 charon->processor->queue_job(charon->processor,
63 (job_t*)delete_ike_sa_job_create(id, TRUE));
64 }
65
66 if (id->is_initiator(id))
67 {
68 if (this->shutdown_on == ++this->established)
69 {
70 DBG1(DBG_CFG, "load-test complete, raising SIGTERM");
71 kill(0, SIGTERM);
72 }
73 }
74 }
75 return TRUE;
76 }
77
78 /**
79 * Implementation of load_tester_listener_t.destroy
80 */
81 static void destroy(private_load_tester_listener_t *this)
82 {
83 free(this);
84 }
85
86 load_tester_listener_t *load_tester_listener_create(u_int shutdown_on)
87 {
88 private_load_tester_listener_t *this = malloc_thing(private_load_tester_listener_t);
89
90 memset(&this->public.listener, 0, sizeof(listener_t));
91 this->public.listener.ike_state_change = (void*)ike_state_change;
92 this->public.destroy = (void(*) (load_tester_listener_t*))destroy;
93
94 this->delete_after_established = lib->settings->get_bool(lib->settings,
95 "charon.plugins.load-tester.delete_after_established", FALSE);
96
97 this->shutdown_on = shutdown_on;
98 this->established = 0;
99
100 return &this->public;
101 }
102