aboutsummaryrefslogtreecommitdiffstats
path: root/Source/charon/daemon.c
blob: 21ed44ef8f587085c31a9b82f52836e10caf0fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/**
 * @file daemon.c
 * 
 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
 * 
 */

/*
 * Copyright (C) 2005 Jan Hutter, Martin Willi
 * Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */
 
#include <stdio.h>
#include <signal.h>
#include <pthread.h>

#include "daemon.h" 

#include <types.h>
#include <utils/allocator.h>
#include <queues/jobs/initiate_ike_sa_job.h>


typedef struct private_daemon_t private_daemon_t;

/**
 * Private additions to daemon_t, contains threads and internal functions.
 */
struct private_daemon_t {
	/**
	 * Public members of daemon_t.
	 */
	daemon_t public;
	
	/**
	 * A logger_t object assigned for daemon things.
	 */
	logger_t *logger;

	/**
	 * Signal set used for signal handling.
	 */
	sigset_t signal_set;
	
	/** 
	 * The thread_id of main-thread.
	 */
	pthread_t main_thread_id;
	
	/**
	 * Main loop function.
	 * 
	 * @param this 	calling object
	 */
	void (*run) (private_daemon_t *this);
	
	/**
	 * A routine to add job for testing.
	 * 
	 * @param this 					calling object
	 * @param configuration_name 	name of configuration to use for initialization
	 */
	void (*build_test_job) (private_daemon_t *this,char *configuration_name);
	
	/**
	 * Initialize the daemon.
	 * 
	 * @param this 	calling object
	 */
	void (*initialize) (private_daemon_t *this);
	
	/**
	 * Destroy the daemon.
	 * 
	 * @param this 	calling object
	 */
	void (*destroy) (private_daemon_t *this);
};

/** 
 * One and only instance of the daemon.
 */
daemon_t *charon;

/**
 * Implementation of private_daemon_t.run.
 */
static void run(private_daemon_t *this)
{	
	while(TRUE)
	{
		int signal_number;
		int error;
		
		error = sigwait(&(this->signal_set), &signal_number);
		if(error)
		{
			this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
			return;
		}
		switch (signal_number)
		{
			case SIGHUP:
			{
				this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
				break;
			}
			case SIGINT:
			{
				this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
				return;
			}
			case SIGTERM:
				this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
				return;
			default:
			{
				this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
				break;
			}
		}
	}
}

/**
 * Implementation of daemon_t.kill.
 */
static void kill_daemon(private_daemon_t *this, char *reason)
{
	/* we send SIGTERM, so the daemon can cleanly shut down */
	this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
	if (this->main_thread_id == pthread_self())
	{
		/* initialization failed, terminate daemon */
		this->destroy(this);
		exit(-1);
	}
	else
	{
		this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
		kill(0, SIGTERM);
		/* thread must die, since he produced a ciritcal failure and can't continue */
		pthread_exit(NULL);
	}
}

/**
 * Implementation of private_daemon_t.build_test_job.
 */
static void build_test_job(private_daemon_t *this, char *configuration_name)
{	
	initiate_ike_sa_job_t *initiate_job;
	
	/* configuration_name = "localhost-rsa"; */
	/* configuration_name = "localhost-shared"; */
	/* configuration_name = "localhost-bad_dh_group"; */
	
		
	initiate_job = initiate_ike_sa_job_create(configuration_name);
	
	this->public.event_queue->add_relative(this->public.event_queue, (job_t*)initiate_job, 2000);

}

/**
 * Implementation of private_daemon_t.initialize.
 */
static void initialize(private_daemon_t *this)
{
	this->public.socket = socket_create(IKEV2_UDP_PORT);
	this->public.ike_sa_manager = ike_sa_manager_create();
	this->public.job_queue = job_queue_create();
	this->public.event_queue = event_queue_create();
	this->public.send_queue = send_queue_create();
	this->public.configuration_manager = configuration_manager_create(RETRANSMIT_TIMEOUT,MAX_RETRANSMIT_COUNT, HALF_OPEN_IKE_SA_TIMEOUT);
	
	this->public.sender = sender_create();
	this->public.receiver = receiver_create();
	this->public.scheduler = scheduler_create();
	this->public.kernel_interface = kernel_interface_create();
	this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
}

/**
 * Destory all initiated objects
 */
static void destroy(private_daemon_t *this)
{
	if (this->public.ike_sa_manager != NULL)
	{
		this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
	}
	if (this->public.kernel_interface != NULL)
	{
		this->public.kernel_interface->destroy(this->public.kernel_interface);
	}
	if (this->public.receiver != NULL)
	{
		this->public.receiver->destroy(this->public.receiver);
	}
	if (this->public.scheduler != NULL)
	{
		this->public.scheduler->destroy(this->public.scheduler);	
	}
	if (this->public.sender != NULL)
	{
		this->public.sender->destroy(this->public.sender);
	}
	if (this->public.thread_pool != NULL)
	{
		this->public.thread_pool->destroy(this->public.thread_pool);	
	}
	if (this->public.job_queue != NULL)
	{
		this->public.job_queue->destroy(this->public.job_queue);
	}
	if (this->public.event_queue != NULL)
	{
		this->public.event_queue->destroy(this->public.event_queue);	
	}
	if (this->public.send_queue != NULL)
	{
		this->public.send_queue->destroy(this->public.send_queue);	
	}
	if (this->public.socket != NULL)
	{
		this->public.socket->destroy(this->public.socket);
	}
	if (this->public.configuration_manager != NULL)
	{
		this->public.configuration_manager->destroy(this->public.configuration_manager);
	}
	
	this->public.logger_manager->destroy(this->public.logger_manager);
	allocator_free(this);
}



/**
 * @brief Create the daemon.
 * 
 * @return 	created daemon_t
 */
private_daemon_t *daemon_create()
{	
	private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
		
	/* assign methods */
	this->run = run;
	this->destroy = destroy;
	this->build_test_job = build_test_job;
	this->initialize = initialize;
	this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
	
	/* first build a logger */
	this->public.logger_manager = logger_manager_create(DEFAULT_LOGLEVEL);
	this->logger = (this->public.logger_manager)->create_logger(this->public.logger_manager, DAEMON, NULL);
	
	/* NULL members for clean destruction */
	this->public.socket = NULL;
	this->public.ike_sa_manager = NULL;
	this->public.job_queue = NULL;
	this->public.event_queue = NULL;
	this->public.send_queue = NULL;
	this->public.configuration_manager = NULL;
	this->public.sender= NULL;
	this->public.receiver = NULL;
	this->public.scheduler = NULL;
	this->public.kernel_interface = NULL;
	this->public.thread_pool = NULL;
	
	this->main_thread_id = pthread_self();
	
	/* setup signal handling */
	sigemptyset(&(this->signal_set));
	sigaddset(&(this->signal_set), SIGINT); 
	sigaddset(&(this->signal_set), SIGHUP); 
	sigaddset(&(this->signal_set), SIGTERM); 
	pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
	
	return this;
}

/**
 * Main function, manages the daemon.
 */
int main(int argc, char *argv[])
{
	private_daemon_t *private_charon;
	
	/* allocation needs initialization, before any allocs are done */
	allocator_init();
	
	private_charon = daemon_create();
	charon = (daemon_t*)private_charon;
	
	private_charon->initialize(private_charon);
	
	if (argc == 2)
	{
		private_charon->build_test_job(private_charon,argv[1]);
	}

	
	private_charon->run(private_charon);
	
	private_charon->destroy(private_charon);
	
#ifdef LEAK_DETECTIVE
	report_memory_leaks(void);
#endif

	exit(0);
}