introduced an additional bus->signal parameter for signal specific data
[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 CHD_UP_START,
101 CHD_UP_SUCCESS,
102 CHD_UP_FAILED,
103
104 /** signals for CHILD_SA delete */
105 CHD_DOWN_START,
106 CHD_DOWN_SUCCESS,
107 CHD_DOWN_FAILED,
108
109 /** signals for CHILD_SA rekeying */
110 CHD_REKEY_START,
111 CHD_REKEY_SUCCESS,
112 CHD_REKEY_FAILED,
113
114 /** signals for CHILD_SA routing */
115 CHD_ROUTE_START,
116 CHD_ROUTE_SUCCESS,
117 CHD_ROUTE_FAILED,
118
119 /** signals for CHILD_SA routing */
120 CHD_UNROUTE_START,
121 CHD_UNROUTE_SUCCESS,
122 CHD_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, NULL, 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, NULL, 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, NULL, 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, NULL, 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 IKE_SA 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_IKE(sig, format, ...) charon->bus->signal(charon->bus, IKE_##sig, LEVEL_0, NULL, format, ##__VA_ARGS__)
197
198 /**
199 * Raise a signal for an IKE event.
200 *
201 * @param sig signal_t signal description
202 * @param format printf() style format string
203 * @param ... printf() style agument list
204 */
205 #define SIG_CHD(sig, chd, format, ...) charon->bus->signal(charon->bus, CHD_##sig, LEVEL_0, chd, format, ##__VA_ARGS__)
206
207 /**
208 * Get the type of a signal.
209 *
210 * A signal may be a debugging signal with a specific context. They have
211 * a level specific for their context > 0. All audit signals use the
212 * type 0. This allows filtering of singals by their type.
213 *
214 * @param signal signal to get the type from
215 * @return type of the signal, between 0..(DBG_MAX-1)
216 */
217 #define SIG_TYPE(sig) (sig > DBG_MAX ? SIG_ANY : sig)
218
219
220 /**
221 * Interface for registering at the signal bus.
222 *
223 * To receive signals from the bus, the client implementing the
224 * bus_listener_t interface registers itself at the signal bus.
225 */
226 struct bus_listener_t {
227
228 /**
229 * Send a signal to a bus listener.
230 *
231 * A numerical identification for the thread is included, as the
232 * associated IKE_SA, if any. Signal specifies the type of
233 * the event occured. The format string specifies
234 * an additional informational or error message with a printf() like
235 * variable argument list. This is in the va_list form, as forwarding
236 * a "..." parameters to functions is not (cleanly) possible.
237 * The implementing signal function returns TRUE to stay registered
238 * to the bus, or FALSE to unregister itself.
239 * Calling bus_t.signal() inside of a registered listener is possible,
240 * but the bus does not invoke listeners recursively.
241 *
242 * @param singal kind of the signal (up, down, rekeyed, ...)
243 * @param level verbosity level of the signal
244 * @param thread ID of the thread raised this signal
245 * @param ike_sa IKE_SA associated to the event
246 * @param data additional signal specific user data
247 * @param format printf() style format string
248 * @param args vprintf() style va_list argument list
249 " @return TRUE to stay registered, FALSE to unregister
250 */
251 bool (*signal) (bus_listener_t *this, signal_t signal, level_t level,
252 int thread, ike_sa_t *ike_sa, void *data,
253 char* format, va_list args);
254 };
255
256 /**
257 * Signal bus which sends signals to registered listeners.
258 *
259 * The signal bus is not much more than a multiplexer. A listener interested
260 * in receiving event signals registers at the bus. Any signals sent to
261 * are delivered to all registered listeners.
262 * To deliver signals to threads, the blocking listen() call may be used
263 * to wait for a signal.
264 */
265 struct bus_t {
266
267 /**
268 * Register a listener to the bus.
269 *
270 * A registered listener receives all signals which are sent to the bus.
271 * The listener is passive; the thread which emitted the signal
272 * processes the listener routine.
273 *
274 * @param listener listener to register.
275 */
276 void (*add_listener) (bus_t *this, bus_listener_t *listener);
277
278 /**
279 * Unregister a listener from the bus.
280 *
281 * @param listener listener to unregister.
282 */
283 void (*remove_listener) (bus_t *this, bus_listener_t *listener);
284
285 /**
286 * Register a listener and block the calling thread.
287 *
288 * This call registers a listener and blocks the calling thread until
289 * its listeners function returns FALSE. This allows to wait for certain
290 * events. The associated job is executed after the listener has been
291 * registered, this allows to listen on events we initiate with the job
292 * without missing any signals.
293 *
294 * @param listener listener to register
295 * @param job job to execute asynchronously when registered, or NULL
296 */
297 void (*listen)(bus_t *this, bus_listener_t *listener, job_t *job);
298
299 /**
300 * Set the IKE_SA the calling thread is using.
301 *
302 * To associate an received signal to an IKE_SA without passing it as
303 * parameter each time, the thread registers it's used IKE_SA each
304 * time it checked it out. Before checking it in, the thread unregisters
305 * the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
306 * thread has one IKE_SA registered (or not).
307 *
308 * @param ike_sa ike_sa to register, or NULL to unregister
309 */
310 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
311
312 /**
313 * Send a signal to the bus.
314 *
315 * The signal specifies the type of the event occured. The format string
316 * specifies an additional informational or error message with a
317 * printf() like variable argument list.
318 * Some useful macros are available to shorten this call.
319 * @see SIG(), DBG1()
320 *
321 * @param singal kind of the signal (up, down, rekeyed, ...)
322 * @param level verbosity level of the signal
323 * @param data additional signal specific user data
324 * @param format printf() style format string
325 * @param ... printf() style argument list
326 */
327 void (*signal) (bus_t *this, signal_t signal, level_t level,
328 void *data, char* format, ...);
329
330 /**
331 * Send a signal to the bus using va_list arguments.
332 *
333 * Same as bus_t.signal(), but uses va_list argument list.
334 *
335 * @param singal kind of the signal (up, down, rekeyed, ...)
336 * @param level verbosity level of the signal
337 * @param data additional signal specific user data
338 * @param format printf() style format string
339 * @param args va_list arguments
340 */
341 void (*vsignal) (bus_t *this, signal_t signal, level_t level,
342 void *data, char* format, va_list args);
343
344 /**
345 * Destroy the signal bus.
346 */
347 void (*destroy) (bus_t *this);
348 };
349
350 /**
351 * Create the signal bus which multiplexes signals to its listeners.
352 *
353 * @return signal bus instance
354 */
355 bus_t *bus_create();
356
357 #endif /* BUS_H_ @} */