merged the modularization branch (credentials) back to trunk
[strongswan.git] / src / charon / bus / bus.h
1 /*
2 * Copyright (C) 2006 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 * $Id$
16 */
17
18 /**
19 * @defgroup bus bus
20 * @{ @ingroup charon
21 */
22
23 #ifndef BUS_H_
24 #define BUS_H_
25
26 typedef enum signal_t signal_t;
27 typedef enum level_t level_t;
28 typedef struct bus_listener_t bus_listener_t;
29 typedef struct bus_t bus_t;
30
31 #include <stdarg.h>
32
33 #include <sa/ike_sa.h>
34 #include <sa/child_sa.h>
35 #include <processing/jobs/job.h>
36
37
38 /**
39 * signals emitted by the daemon.
40 *
41 * Signaling is for different purporses. First, it allows debugging via
42 * "debugging signal messages", sencondly, it allows to follow certain
43 * mechanisms currently going on in the daemon. As we are multithreaded,
44 * and multiple transactions are involved, it's not possible to follow
45 * one connection setup without further infrastructure. These infrastructure
46 * is provided by the bus and the signals the daemon emits to the bus.
47 *
48 * There are different scenarios to follow these signals, but all have
49 * the same scheme. First, a START signal is emitted to indicate the daemon
50 * has started to do something. After a start signal, a SUCCESS or a FAILED
51 * signal of the same type follows. This allows to track the operation. Any
52 * Debug signal betwee a START and a SUCCESS/FAILED belongs to that operation
53 * if the IKE_SA is the same. The thread may change, as multiple threads
54 * may be involved in a complex scenario.
55 */
56 enum signal_t {
57 /** pseudo signal, representing any other signal */
58 SIG_ANY,
59
60 /** debugging message from daemon main loop */
61 DBG_DMN,
62 /** debugging message from IKE_SA_MANAGER */
63 DBG_MGR,
64 /** debugging message from an IKE_SA */
65 DBG_IKE,
66 /** debugging message from a CHILD_SA */
67 DBG_CHD,
68 /** debugging message from job processing */
69 DBG_JOB,
70 /** debugging message from configuration backends */
71 DBG_CFG,
72 /** debugging message from kernel interface */
73 DBG_KNL,
74 /** debugging message from networking */
75 DBG_NET,
76 /** debugging message from message encoding/decoding */
77 DBG_ENC,
78 /** debugging message from libstrongswan via logging hook */
79 DBG_LIB,
80
81 /** number of debug signals */
82 DBG_MAX,
83
84 /** signals for IKE_SA establishment */
85 IKE_UP_START,
86 IKE_UP_SUCCESS,
87 IKE_UP_FAILED,
88
89 /** signals for IKE_SA delete */
90 IKE_DOWN_START,
91 IKE_DOWN_SUCCESS,
92 IKE_DOWN_FAILED,
93
94 /** signals for IKE_SA rekeying */
95 IKE_REKEY_START,
96 IKE_REKEY_SUCCESS,
97 IKE_REKEY_FAILED,
98
99 /** signals for CHILD_SA establishment */
100 CHILD_UP_START,
101 CHILD_UP_SUCCESS,
102 CHILD_UP_FAILED,
103
104 /** signals for CHILD_SA delete */
105 CHILD_DOWN_START,
106 CHILD_DOWN_SUCCESS,
107 CHILD_DOWN_FAILED,
108
109 /** signals for CHILD_SA rekeying */
110 CHILD_REKEY_START,
111 CHILD_REKEY_SUCCESS,
112 CHILD_REKEY_FAILED,
113
114 /** signals for CHILD_SA routing */
115 CHILD_ROUTE_START,
116 CHILD_ROUTE_SUCCESS,
117 CHILD_ROUTE_FAILED,
118
119 /** signals for CHILD_SA routing */
120 CHILD_UNROUTE_START,
121 CHILD_UNROUTE_SUCCESS,
122 CHILD_UNROUTE_FAILED,
123
124 SIG_MAX
125 };
126
127 /**
128 * short names of signals using 3 chars
129 */
130 extern enum_name_t *signal_names;
131
132 /**
133 * Signal levels used to control output verbosity.
134 */
135 enum level_t {
136 /** numerical levels from 0 to 4 */
137 LEVEL_0 = 0,
138 LEVEL_1 = 1,
139 LEVEL_2 = 2,
140 LEVEL_3 = 3,
141 LEVEL_4 = 4,
142 /** absolutely silent, no signal is emitted with this level */
143 LEVEL_SILENT = -1,
144 /** alias for numberical levels */
145 LEVEL_AUDIT = LEVEL_0,
146 LEVEL_CTRL = LEVEL_1,
147 LEVEL_CTRLMORE = LEVEL_2,
148 LEVEL_RAW = LEVEL_3,
149 LEVEL_PRIVATE = LEVEL_4,
150 };
151
152 #ifndef DEBUG_LEVEL
153 # define DEBUG_LEVEL 4
154 #endif /* DEBUG_LEVEL */
155
156 #if DEBUG_LEVEL >= 1
157 /**
158 * Log a debug message via the signal bus.
159 *
160 * @param signal signal_t signal description
161 * @param format printf() style format string
162 * @param ... printf() style agument list
163 */
164 # define DBG1(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_1, format, ##__VA_ARGS__)
165 #endif /* DEBUG_LEVEL */
166 #if DEBUG_LEVEL >= 2
167 #define DBG2(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_2, format, ##__VA_ARGS__)
168 #endif /* DEBUG_LEVEL */
169 #if DEBUG_LEVEL >= 3
170 #define DBG3(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_3, format, ##__VA_ARGS__)
171 #endif /* DEBUG_LEVEL */
172 #if DEBUG_LEVEL >= 4
173 #define DBG4(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_4, format, ##__VA_ARGS__)
174 #endif /* DEBUG_LEVEL */
175
176 #ifndef DBG1
177 # define DBG1(...) {}
178 #endif /* DBG1 */
179 #ifndef DBG2
180 # define DBG2(...) {}
181 #endif /* DBG2 */
182 #ifndef DBG3
183 # define DBG3(...) {}
184 #endif /* DBG3 */
185 #ifndef DBG4
186 # define DBG4(...) {}
187 #endif /* DBG4 */
188
189 /**
190 * Raise a signal for an occured event.
191 *
192 * @param sig signal_t signal description
193 * @param format printf() style format string
194 * @param ... printf() style agument list
195 */
196 #define SIG(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_0, format, ##__VA_ARGS__)
197
198 /**
199 * Get the type of a signal.
200 *
201 * A signal may be a debugging signal with a specific context. They have
202 * a level specific for their context > 0. All audit signals use the
203 * type 0. This allows filtering of singals by their type.
204 *
205 * @param signal signal to get the type from
206 * @return type of the signal, between 0..(DBG_MAX-1)
207 */
208 #define SIG_TYPE(sig) (sig > DBG_MAX ? SIG_ANY : sig)
209
210
211 /**
212 * Interface for registering at the signal bus.
213 *
214 * To receive signals from the bus, the client implementing the
215 * bus_listener_t interface registers itself at the signal bus.
216 */
217 struct bus_listener_t {
218
219 /**
220 * Send a signal to a bus listener.
221 *
222 * A numerical identification for the thread is included, as the
223 * associated IKE_SA, if any. Signal specifies the type of
224 * the event occured. The format string specifies
225 * an additional informational or error message with a printf() like
226 * variable argument list. This is in the va_list form, as forwarding
227 * a "..." parameters to functions is not (cleanly) possible.
228 * The implementing signal function returns TRUE to stay registered
229 * to the bus, or FALSE to unregister itself.
230 * You should not call bus_t.signal() inside of a registered listener,
231 * as it WILL call itself recursively. If you do so, make shure to
232 * avoid infinite recursion. Watch your stack!
233 *
234 * @param singal kind of the signal (up, down, rekeyed, ...)
235 * @param level verbosity level of the signal
236 * @param thread ID of the thread raised this signal
237 * @param ike_sa IKE_SA associated to the event
238 * @param format printf() style format string
239 * @param args vprintf() style va_list argument list
240 " @return TRUE to stay registered, FALSE to unregister
241 */
242 bool (*signal) (bus_listener_t *this, signal_t signal, level_t level,
243 int thread, ike_sa_t *ike_sa, char* format, va_list args);
244 };
245
246 /**
247 * Signal bus which sends signals to registered listeners.
248 *
249 * The signal bus is not much more than a multiplexer. A listener interested
250 * in receiving event signals registers at the bus. Any signals sent to
251 * are delivered to all registered listeners.
252 * To deliver signals to threads, the blocking listen() call may be used
253 * to wait for a signal.
254 */
255 struct bus_t {
256
257 /**
258 * Register a listener to the bus.
259 *
260 * A registered listener receives all signals which are sent to the bus.
261 * The listener is passive; the thread which emitted the signal
262 * processes the listener routine.
263 *
264 * @param listener listener to register.
265 */
266 void (*add_listener) (bus_t *this, bus_listener_t *listener);
267
268 /**
269 * Unregister a listener from the bus.
270 *
271 * @param listener listener to unregister.
272 */
273 void (*remove_listener) (bus_t *this, bus_listener_t *listener);
274
275 /**
276 * Register a listener and block the calling thread.
277 *
278 * This call registers a listener and blocks the calling thread until
279 * its listeners function returns FALSE. This allows to wait for certain
280 * events. The associated job is executed after the listener has been
281 * registered, this allows to listen on events we initiate with the job
282 * without missing any signals.
283 *
284 * @param listener listener to register
285 * @param job job to execute asynchronously when registered, or NULL
286 */
287 void (*listen)(bus_t *this, bus_listener_t *listener, job_t *job);
288
289 /**
290 * Set the IKE_SA the calling thread is using.
291 *
292 * To associate an received signal to an IKE_SA without passing it as
293 * parameter each time, the thread registers it's used IKE_SA each
294 * time it checked it out. Before checking it in, the thread unregisters
295 * the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
296 * thread has one IKE_SA registered (or not).
297 *
298 * @param ike_sa ike_sa to register, or NULL to unregister
299 */
300 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
301
302 /**
303 * Send a signal to the bus.
304 *
305 * The signal specifies the type of the event occured. The format string
306 * specifies an additional informational or error message with a
307 * printf() like variable argument list.
308 * Some useful macros are available to shorten this call.
309 * @see SIG(), DBG1()
310 *
311 * @param singal kind of the signal (up, down, rekeyed, ...)
312 * @param level verbosity level of the signal
313 * @param format printf() style format string
314 * @param ... printf() style argument list
315 */
316 void (*signal) (bus_t *this, signal_t signal, level_t level, char* format, ...);
317
318 /**
319 * Send a signal to the bus using va_list arguments.
320 *
321 * Same as bus_t.signal(), but uses va_list argument list.
322 *
323 * @todo Improve performace of vsignal implementation. This method is
324 * called extensively and therefore shouldn't allocate heap memory or
325 * do other expensive tasks!
326 *
327 * @param singal kind of the signal (up, down, rekeyed, ...)
328 * @param level verbosity level of the signal
329 * @param format printf() style format string
330 * @param args va_list arguments
331 */
332 void (*vsignal) (bus_t *this, signal_t signal, level_t level, char* format, va_list args);
333
334 /**
335 * Destroy the signal bus.
336 */
337 void (*destroy) (bus_t *this);
338 };
339
340 /**
341 * Create the signal bus which multiplexes signals to its listeners.
342 *
343 * @return signal bus instance
344 */
345 bus_t *bus_create();
346
347 #endif /* BUS_H_ @} */