2005-11-03 07:18:16 +00:00
|
|
|
/**
|
|
|
|
* @file daemon.c
|
|
|
|
*
|
2005-12-06 12:54:34 +00:00
|
|
|
* @brief Implementation of daemon_t and main of IKEv2-Daemon.
|
2005-11-03 07:18:16 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2005-11-03 08:56:04 +00:00
|
|
|
#include <stdio.h>
|
2005-11-16 12:06:34 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
2005-11-23 09:43:14 +00:00
|
|
|
#include <types.h>
|
|
|
|
#include <utils/allocator.h>
|
2005-11-24 16:57:16 +00:00
|
|
|
#include <queues/jobs/initiate_ike_sa_job.h>
|
2006-03-07 09:42:15 +00:00
|
|
|
#include <config/static_configuration.h>
|
|
|
|
#include <config/starter_configuration.h>
|
2005-11-03 17:26:49 +00:00
|
|
|
|
2005-11-03 08:56:04 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
typedef struct private_daemon_t private_daemon_t;
|
2005-11-16 12:06:34 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Private additions to daemon_t, contains threads and internal functions.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
struct private_daemon_t {
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Public members of daemon_t.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
daemon_t public;
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* A logger_t object assigned for daemon things.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
logger_t *logger;
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Signal set used for signal handling.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
sigset_t signal_set;
|
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* The thread_id of main-thread.
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-12-04 11:46:58 +00:00
|
|
|
pthread_t main_thread_id;
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Main loop function.
|
|
|
|
*
|
|
|
|
* @param this calling object
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
void (*run) (private_daemon_t *this);
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-12 18:59:09 +00:00
|
|
|
* A routine to add job for testing.
|
2005-12-06 12:54:34 +00:00
|
|
|
*
|
2005-12-12 18:59:09 +00:00
|
|
|
* @param this calling object
|
|
|
|
* @param configuration_name name of configuration to use for initialization
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-12-12 18:59:09 +00:00
|
|
|
void (*build_test_job) (private_daemon_t *this,char *configuration_name);
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Initialize the daemon.
|
|
|
|
*
|
|
|
|
* @param this calling object
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
void (*initialize) (private_daemon_t *this);
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Destroy the daemon.
|
|
|
|
*
|
|
|
|
* @param this calling object
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
|
|
|
void (*destroy) (private_daemon_t *this);
|
2005-11-29 10:25:07 +00:00
|
|
|
};
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* One and only instance of the daemon.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
daemon_t *charon;
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of private_daemon_t.run.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
static void run(private_daemon_t *this)
|
|
|
|
{
|
|
|
|
while(TRUE)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
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)
|
|
|
|
{
|
2005-11-16 12:06:34 +00:00
|
|
|
case SIGHUP:
|
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SIGINT:
|
|
|
|
{
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case SIGTERM:
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of daemon_t.kill.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
static void kill_daemon(private_daemon_t *this, char *reason)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
/* we send SIGTERM, so the daemon can cleanly shut down */
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
|
2005-12-04 11:46:58 +00:00
|
|
|
if (this->main_thread_id == pthread_self())
|
2005-11-29 11:27:25 +00:00
|
|
|
{
|
|
|
|
/* 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);
|
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
}
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
/**
|
2005-12-12 18:59:09 +00:00
|
|
|
* Implementation of private_daemon_t.build_test_job.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
2005-12-12 18:59:09 +00:00
|
|
|
static void build_test_job(private_daemon_t *this, char *configuration_name)
|
2005-12-12 12:52:59 +00:00
|
|
|
{
|
|
|
|
initiate_ike_sa_job_t *initiate_job;
|
|
|
|
|
2005-12-12 18:59:09 +00:00
|
|
|
/* configuration_name = "localhost-rsa"; */
|
|
|
|
/* configuration_name = "localhost-shared"; */
|
|
|
|
/* configuration_name = "localhost-bad_dh_group"; */
|
2005-12-12 12:52:59 +00:00
|
|
|
|
|
|
|
|
2005-12-12 18:59:09 +00:00
|
|
|
initiate_job = initiate_ike_sa_job_create(configuration_name);
|
2005-12-12 12:52:59 +00:00
|
|
|
|
|
|
|
this->public.event_queue->add_relative(this->public.event_queue, (job_t*)initiate_job, 2000);
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of private_daemon_t.initialize.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
static void initialize(private_daemon_t *this)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
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();
|
2006-03-07 09:42:15 +00:00
|
|
|
this->public.configuration = (configuration_t*)static_configuration_create();
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.sender = sender_create();
|
|
|
|
this->public.receiver = receiver_create();
|
|
|
|
this->public.scheduler = scheduler_create();
|
2006-02-09 16:25:02 +00:00
|
|
|
this->public.kernel_interface = kernel_interface_create();
|
|
|
|
this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-11-29 10:25:07 +00:00
|
|
|
* Destory all initiated objects
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
static void destroy(private_daemon_t *this)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-03-01 15:56:51 +00:00
|
|
|
if (this->public.ike_sa_manager != NULL)
|
|
|
|
{
|
|
|
|
this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
|
|
|
|
}
|
2006-02-09 16:25:02 +00:00
|
|
|
if (this->public.kernel_interface != NULL)
|
|
|
|
{
|
|
|
|
this->public.kernel_interface->destroy(this->public.kernel_interface);
|
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.receiver != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.receiver->destroy(this->public.receiver);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.scheduler != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.scheduler->destroy(this->public.scheduler);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.sender != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.sender->destroy(this->public.sender);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.thread_pool != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.thread_pool->destroy(this->public.thread_pool);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.job_queue != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.job_queue->destroy(this->public.job_queue);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.event_queue != NULL)
|
2005-11-17 11:24:03 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.event_queue->destroy(this->public.event_queue);
|
2005-11-17 11:24:03 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.send_queue != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.send_queue->destroy(this->public.send_queue);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.socket != NULL)
|
2005-11-26 15:48:45 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.socket->destroy(this->public.socket);
|
2005-11-29 10:25:07 +00:00
|
|
|
}
|
2006-03-07 09:42:15 +00:00
|
|
|
if (this->public.configuration != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-03-07 09:42:15 +00:00
|
|
|
this->public.configuration->destroy(this->public.configuration);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.logger_manager->destroy(this->public.logger_manager);
|
2005-11-29 10:25:07 +00:00
|
|
|
allocator_free(this);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2005-11-29 10:25:07 +00:00
|
|
|
* @brief Create the daemon.
|
2005-11-16 12:06:34 +00:00
|
|
|
*
|
2005-11-29 10:25:07 +00:00
|
|
|
* @return created daemon_t
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
private_daemon_t *daemon_create()
|
2005-12-04 01:30:35 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
private_daemon_t *this = allocator_alloc_thing(private_daemon_t);
|
|
|
|
|
|
|
|
/* assign methods */
|
|
|
|
this->run = run;
|
2005-11-29 11:27:25 +00:00
|
|
|
this->destroy = destroy;
|
2005-12-12 18:59:09 +00:00
|
|
|
this->build_test_job = build_test_job;
|
2005-11-29 11:27:25 +00:00
|
|
|
this->initialize = initialize;
|
|
|
|
this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
|
2005-11-29 10:25:07 +00:00
|
|
|
|
|
|
|
/* 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;
|
2006-03-07 09:42:15 +00:00
|
|
|
this->public.configuration = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
this->public.sender= NULL;
|
|
|
|
this->public.receiver = NULL;
|
|
|
|
this->public.scheduler = NULL;
|
2006-02-09 16:25:02 +00:00
|
|
|
this->public.kernel_interface = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
this->public.thread_pool = NULL;
|
|
|
|
|
2005-12-04 11:46:58 +00:00
|
|
|
this->main_thread_id = pthread_self();
|
2005-11-29 11:27:25 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
/* 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;
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Main function, manages the daemon.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
int main(int argc, char *argv[])
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
private_daemon_t *private_charon;
|
2005-11-03 17:55:27 +00:00
|
|
|
|
2005-12-04 01:30:35 +00:00
|
|
|
/* allocation needs initialization, before any allocs are done */
|
|
|
|
allocator_init();
|
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
private_charon = daemon_create();
|
|
|
|
charon = (daemon_t*)private_charon;
|
|
|
|
|
|
|
|
private_charon->initialize(private_charon);
|
|
|
|
|
2005-12-12 18:59:09 +00:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
private_charon->build_test_job(private_charon,argv[1]);
|
|
|
|
}
|
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
|
|
|
|
private_charon->run(private_charon);
|
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
private_charon->destroy(private_charon);
|
2005-11-16 12:22:57 +00:00
|
|
|
|
2005-11-03 14:50:02 +00:00
|
|
|
#ifdef LEAK_DETECTIVE
|
2005-11-16 12:06:34 +00:00
|
|
|
report_memory_leaks(void);
|
2005-11-03 14:50:02 +00:00
|
|
|
#endif
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
exit(0);
|
2005-11-03 08:22:10 +00:00
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
|