2 * Copyright (C) 2006-2012 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 #include <sys/types.h>
27 #include <plugins/plugin_feature.h>
28 #include <config/proposal.h>
29 #include <kernel/kernel_handler.h>
30 #include <processing/jobs/start_action_job.h>
32 typedef struct private_daemon_t private_daemon_t
;
35 * Private additions to daemon_t, contains threads and internal functions.
37 struct private_daemon_t
{
39 * Public members of daemon_t.
44 * Handler for kernel events
46 kernel_handler_t
*kernel_handler
;
50 * One and only instance of the daemon.
55 * hook in library for debugging messages
57 extern void (*dbg
) (debug_t group
, level_t level
, char *fmt
, ...);
60 * we store the previous debug function so we can reset it
62 static void (*dbg_old
) (debug_t group
, level_t level
, char *fmt
, ...);
65 * Logging hook for library logs, spreads debug message over bus
67 static void dbg_bus(debug_t group
, level_t level
, char *fmt
, ...)
72 charon
->bus
->vlog(charon
->bus
, group
, level
, fmt
, args
);
77 * Clean up all daemon resources
79 static void destroy(private_daemon_t
*this)
81 /* terminate all idle threads */
82 lib
->processor
->set_threads(lib
->processor
, 0);
84 /* close all IKE_SAs */
85 if (this->public.ike_sa_manager
)
87 this->public.ike_sa_manager
->flush(this->public.ike_sa_manager
);
89 if (this->public.traps
)
91 this->public.traps
->flush(this->public.traps
);
93 if (this->public.sender
)
95 this->public.sender
->flush(this->public.sender
);
98 /* cancel all threads and wait for their termination */
99 lib
->processor
->cancel(lib
->processor
);
101 DESTROY_IF(this->public.receiver
);
103 DESTROY_IF(this->public.connect_manager
);
104 DESTROY_IF(this->public.mediation_manager
);
106 /* make sure the cache is clear before unloading plugins */
107 lib
->credmgr
->flush_cache(lib
->credmgr
, CERT_ANY
);
108 lib
->plugins
->unload(lib
->plugins
);
109 DESTROY_IF(this->kernel_handler
);
110 DESTROY_IF(this->public.traps
);
111 DESTROY_IF(this->public.shunts
);
112 DESTROY_IF(this->public.ike_sa_manager
);
113 DESTROY_IF(this->public.controller
);
114 DESTROY_IF(this->public.eap
);
115 DESTROY_IF(this->public.xauth
);
116 DESTROY_IF(this->public.backends
);
117 DESTROY_IF(this->public.sender
);
118 DESTROY_IF(this->public.socket
);
119 DESTROY_IF(this->public.caps
);
121 /* rehook library logging, shutdown logging */
123 DESTROY_IF(this->public.bus
);
124 this->public.file_loggers
->destroy_offset(this->public.file_loggers
,
125 offsetof(file_logger_t
, destroy
));
126 this->public.sys_loggers
->destroy_offset(this->public.sys_loggers
,
127 offsetof(sys_logger_t
, destroy
));
128 free((void*)this->public.name
);
132 METHOD(daemon_t
, start
, void,
133 private_daemon_t
*this)
135 /* start the engine, go multithreaded */
136 lib
->processor
->set_threads(lib
->processor
,
137 lib
->settings
->get_int(lib
->settings
, "%s.threads",
138 DEFAULT_THREADS
, charon
->name
));
141 METHOD(daemon_t
, initialize
, bool,
142 private_daemon_t
*this, char *plugins
)
144 static plugin_feature_t features
[] = {
145 PLUGIN_PROVIDE(CUSTOM
, "libcharon"),
146 PLUGIN_DEPENDS(HASHER
, HASH_SHA1
),
147 PLUGIN_DEPENDS(RNG
, RNG_STRONG
),
148 PLUGIN_DEPENDS(NONCE_GEN
),
149 PLUGIN_DEPENDS(CUSTOM
, "kernel-ipsec"),
150 PLUGIN_DEPENDS(CUSTOM
, "kernel-net"),
151 PLUGIN_DEPENDS(CUSTOM
, "socket"),
153 lib
->plugins
->add_static_features(lib
->plugins
, charon
->name
, features
,
154 countof(features
), TRUE
);
156 /* load plugins, further infrastructure may need it */
157 if (!lib
->plugins
->load(lib
->plugins
, NULL
, plugins
))
161 DBG1(DBG_DMN
, "loaded plugins: %s",
162 lib
->plugins
->loaded_plugins(lib
->plugins
));
164 this->public.ike_sa_manager
= ike_sa_manager_create();
165 if (this->public.ike_sa_manager
== NULL
)
169 this->public.sender
= sender_create();
170 this->public.receiver
= receiver_create();
171 if (this->public.receiver
== NULL
)
176 /* Queue start_action job */
177 lib
->processor
->queue_job(lib
->processor
, (job_t
*)start_action_job_create());
180 this->public.connect_manager
= connect_manager_create();
181 if (this->public.connect_manager
== NULL
)
185 this->public.mediation_manager
= mediation_manager_create();
194 private_daemon_t
*daemon_create(const char *name
)
196 private_daemon_t
*this;
200 .initialize
= _initialize
,
203 .file_loggers
= linked_list_create(),
204 .sys_loggers
= linked_list_create(),
205 .name
= strdup(name ?
: "libcharon"),
208 charon
= &this->public;
209 this->public.caps
= capabilities_create();
210 this->public.controller
= controller_create();
211 this->public.eap
= eap_manager_create();
212 this->public.xauth
= xauth_manager_create();
213 this->public.backends
= backend_manager_create();
214 this->public.socket
= socket_manager_create();
215 this->public.traps
= trap_manager_create();
216 this->public.shunts
= shunt_manager_create();
217 this->kernel_handler
= kernel_handler_create();
219 this->public.caps
->keep(this->public.caps
, CAP_NET_ADMIN
);
225 * Described in header.
227 void libcharon_deinit()
229 destroy((private_daemon_t
*)charon
);
234 * Described in header.
236 bool libcharon_init(const char *name
)
240 /* for uncritical pseudo random numbers */
241 srandom(time(NULL
) + getpid());
243 /* set up hook to log dbg message in library via charons message bus */
247 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'P',
248 proposal_printf_hook
,
249 PRINTF_HOOK_ARGTYPE_POINTER
,
250 PRINTF_HOOK_ARGTYPE_END
);
252 if (lib
->integrity
&&
253 !lib
->integrity
->check(lib
->integrity
, "libcharon", libcharon_init
))
255 dbg(DBG_DMN
, 1, "integrity check of libcharon failed");