refactored bus and interface to resolve threading issues (WIP)
[strongswan.git] / src / charon / bus / bus.h
1 /**
2 * @file bus.h
3 *
4 * @brief Interface of bus_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
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 * @brief 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 * @ingroup bus
57 */
58 enum signal_t {
59 /** pseudo signal, representing any other signal */
60 SIG_ANY,
61
62 /** debugging message from daemon main loop */
63 DBG_DMN,
64 /** debugging message from IKE_SA_MANAGER */
65 DBG_MGR,
66 /** debugging message from an IKE_SA */
67 DBG_IKE,
68 /** debugging message from a CHILD_SA */
69 DBG_CHD,
70 /** debugging message from job processing */
71 DBG_JOB,
72 /** debugging message from configuration backends */
73 DBG_CFG,
74 /** debugging message from kernel interface */
75 DBG_KNL,
76 /** debugging message from networking */
77 DBG_NET,
78 /** debugging message from message encoding/decoding */
79 DBG_ENC,
80 /** debugging message from libstrongswan via logging hook */
81 DBG_LIB,
82
83 /** number of debug signals */
84 DBG_MAX,
85
86 /** signals for IKE_SA establishment */
87 IKE_UP_START,
88 IKE_UP_SUCCESS,
89 IKE_UP_FAILED,
90
91 /** signals for IKE_SA delete */
92 IKE_DOWN_START,
93 IKE_DOWN_SUCCESS,
94 IKE_DOWN_FAILED,
95
96 /** signals for IKE_SA rekeying */
97 IKE_REKEY_START,
98 IKE_REKEY_SUCCESS,
99 IKE_REKEY_FAILED,
100
101 /** signals for CHILD_SA establishment */
102 CHILD_UP_START,
103 CHILD_UP_SUCCESS,
104 CHILD_UP_FAILED,
105
106 /** signals for CHILD_SA delete */
107 CHILD_DOWN_START,
108 CHILD_DOWN_SUCCESS,
109 CHILD_DOWN_FAILED,
110
111 /** signals for CHILD_SA rekeying */
112 CHILD_REKEY_START,
113 CHILD_REKEY_SUCCESS,
114 CHILD_REKEY_FAILED,
115
116 /** signals for CHILD_SA routing */
117 CHILD_ROUTE_START,
118 CHILD_ROUTE_SUCCESS,
119 CHILD_ROUTE_FAILED,
120
121 /** signals for CHILD_SA routing */
122 CHILD_UNROUTE_START,
123 CHILD_UNROUTE_SUCCESS,
124 CHILD_UNROUTE_FAILED,
125
126 SIG_MAX
127 };
128
129 /**
130 * short names of signals using 3 chars
131 */
132 extern enum_name_t *signal_names;
133
134 /**
135 * Signal levels used to control output verbosity.
136 */
137 enum level_t {
138 /** numerical levels from 0 to 4 */
139 LEVEL_0 = 0,
140 LEVEL_1 = 1,
141 LEVEL_2 = 2,
142 LEVEL_3 = 3,
143 LEVEL_4 = 4,
144 /** absolutely silent, no signal is emitted with this level */
145 LEVEL_SILENT = -1,
146 /** alias for numberical levels */
147 LEVEL_AUDIT = LEVEL_0,
148 LEVEL_CTRL = LEVEL_1,
149 LEVEL_CTRLMORE = LEVEL_2,
150 LEVEL_RAW = LEVEL_3,
151 LEVEL_PRIVATE = LEVEL_4,
152 };
153
154 #ifndef DEBUG_LEVEL
155 # define DEBUG_LEVEL 4
156 #endif /* DEBUG_LEVEL */
157
158 #if DEBUG_LEVEL >= 1
159 /**
160 * @brief Log a debug message via the signal bus.
161 *
162 * @param signal signal_t signal description
163 * @param format printf() style format string
164 * @param ... printf() style agument list
165 */
166 # define DBG1(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_1, format, ##__VA_ARGS__)
167 #endif /* DEBUG_LEVEL */
168 #if DEBUG_LEVEL >= 2
169 #define DBG2(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_2, format, ##__VA_ARGS__)
170 #endif /* DEBUG_LEVEL */
171 #if DEBUG_LEVEL >= 3
172 #define DBG3(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_3, format, ##__VA_ARGS__)
173 #endif /* DEBUG_LEVEL */
174 #if DEBUG_LEVEL >= 4
175 #define DBG4(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_4, format, ##__VA_ARGS__)
176 #endif /* DEBUG_LEVEL */
177
178 #ifndef DBG1
179 # define DBG1(...) {}
180 #endif /* DBG1 */
181 #ifndef DBG2
182 # define DBG2(...) {}
183 #endif /* DBG2 */
184 #ifndef DBG3
185 # define DBG3(...) {}
186 #endif /* DBG3 */
187 #ifndef DBG4
188 # define DBG4(...) {}
189 #endif /* DBG4 */
190
191 /**
192 * @brief Raise a signal for an occured event.
193 *
194 * @param sig signal_t signal description
195 * @param format printf() style format string
196 * @param ... printf() style agument list
197 */
198 #define SIG(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_0, format, ##__VA_ARGS__)
199
200 /**
201 * @brief Get the type of a signal.
202 *
203 * A signal may be a debugging signal with a specific context. They have
204 * a level specific for their context > 0. All audit signals use the
205 * type 0. This allows filtering of singals by their type.
206 *
207 * @param signal signal to get the type from
208 * @return type of the signal, between 0..(DBG_MAX-1)
209 */
210 #define SIG_TYPE(sig) (sig > DBG_MAX ? SIG_ANY : sig)
211
212
213 /**
214 * @brief Interface for registering at the signal bus.
215 *
216 * To receive signals from the bus, the client implementing the
217 * bus_listener_t interface registers itself at the signal bus.
218 *
219 * @ingroup bus
220 */
221 struct bus_listener_t {
222
223 /**
224 * @brief Send a signal to a bus listener.
225 *
226 * A numerical identification for the thread is included, as the
227 * associated IKE_SA, if any. Signal specifies the type of
228 * the event occured. The format string specifies
229 * an additional informational or error message with a printf() like
230 * variable argument list. This is in the va_list form, as forwarding
231 * a "..." parameters to functions is not (cleanly) possible.
232 * The implementing signal function returns TRUE to stay registered
233 * to the bus, or FALSE to unregister itself.
234 *
235 * @param this listener
236 * @param singal kind of the signal (up, down, rekeyed, ...)
237 * @param level verbosity level of the signal
238 * @param thread ID of the thread raised this signal
239 * @param ike_sa IKE_SA associated to the event
240 * @param format printf() style format string
241 * @param args vprintf() style va_list argument list
242 " @return TRUE to stay registered, FALSE to unregister
243 */
244 bool (*signal) (bus_listener_t *this, signal_t signal, level_t level,
245 int thread, ike_sa_t *ike_sa, char* format, va_list args);
246 };
247
248 /**
249 * @brief Signal bus which sends signals to registered listeners.
250 *
251 * The signal bus is not much more than a multiplexer. A listener interested
252 * in receiving event signals registers at the bus. Any signals sent to
253 * are delivered to all registered listeners.
254 * To deliver signals to threads, the blocking listen() call may be used
255 * to wait for a signal.
256 *
257 * @ingroup bus
258 */
259 struct bus_t {
260
261 /**
262 * @brief Register a listener to the bus.
263 *
264 * A registered listener receives all signals which are sent to the bus.
265 * The listener is passive; the thread which emitted the signal
266 * processes the listener routine.
267 *
268 * @param this bus
269 * @param listener listener to register.
270 */
271 void (*add_listener) (bus_t *this, bus_listener_t *listener);
272
273 /**
274 * @brief Unregister a listener from the bus.
275 *
276 * @param this bus
277 * @param listener listener to unregister.
278 */
279 void (*remove_listener) (bus_t *this, bus_listener_t *listener);
280
281 /**
282 * @brief Register a listener and block the calling thread.
283 *
284 * This call registers a listener and blocks the calling thread until
285 * its listeners function returns FALSE. This allows to wait for certain
286 * events. The associated job is executed after the listener has been
287 * registered, this allows to listen on events we initiate with the job
288 * without missing any signals.
289 *
290 * @param this bus
291 * @param listener listener to register
292 * @param job job to execute asynchronously when registered, or NULL
293 */
294 void (*listen)(bus_t *this, bus_listener_t *listener, job_t *job);
295
296 /**
297 * @brief Set the IKE_SA the calling thread is using.
298 *
299 * To associate an received signal to an IKE_SA without passing it as
300 * parameter each time, the thread registers it's used IKE_SA each
301 * time it checked it out. Before checking it in, the thread unregisters
302 * the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
303 * thread has one IKE_SA registered (or not).
304 *
305 * @param this bus
306 * @param ike_sa ike_sa to register, or NULL to unregister
307 */
308 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
309
310 /**
311 * @brief Send a signal to the bus.
312 *
313 * The signal specifies the type of the event occured. The format string
314 * specifies an additional informational or error message with a
315 * printf() like variable argument list.
316 * Some useful macros are available to shorten this call.
317 * @see SIG(), DBG1()
318 *
319 * @param this bus
320 * @param singal kind of the signal (up, down, rekeyed, ...)
321 * @param level verbosity level of the signal
322 * @param format printf() style format string
323 * @param ... printf() style argument list
324 */
325 void (*signal) (bus_t *this, signal_t signal, level_t level, char* format, ...);
326
327 /**
328 * @brief Send a signal to the bus using va_list arguments.
329 *
330 * Same as bus_t.signal(), but uses va_list argument list.
331 *
332 * @todo Improve performace of vsignal implementation. This method is
333 * called extensively and therefore shouldn't allocate heap memory or
334 * do other expensive tasks!
335 *
336 * @param this bus
337 * @param singal kind of the signal (up, down, rekeyed, ...)
338 * @param level verbosity level of the signal
339 * @param format printf() style format string
340 * @param args va_list arguments
341 */
342 void (*vsignal) (bus_t *this, signal_t signal, level_t level, char* format, va_list args);
343
344 /**
345 * @brief Destroy the signal bus.
346 *
347 * @param this bus to destroy
348 */
349 void (*destroy) (bus_t *this);
350 };
351
352 /**
353 * @brief Create the signal bus which multiplexes signals to its listeners.
354 *
355 * @return signal bus instance
356 *
357 * @ingroup bus
358 */
359 bus_t *bus_create();
360
361 #endif /* BUS_H_ */