backports from the p2p-nat-t branch:
[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
36
37 /**
38 * @brief signals emitted by the daemon.
39 *
40 * Signaling is for different purporses. First, it allows debugging via
41 * "debugging signal messages", sencondly, it allows to follow certain
42 * mechanisms currently going on in the daemon. As we are multithreaded,
43 * and multiple transactions are involved, it's not possible to follow
44 * one connection setup without further infrastructure. These infrastructure
45 * is provided by the bus and the signals the daemon emits to the bus.
46 *
47 * There are different scenarios to follow these signals, but all have
48 * the same scheme. First, a START signal is emitted to indicate the daemon
49 * has started to do something. After a start signal, a SUCCESS or a FAILED
50 * signal of the same type follows. This allows to track the operation. Any
51 * Debug signal betwee a START and a SUCCESS/FAILED belongs to that operation
52 * if the IKE_SA is the same. The thread may change, as multiple threads
53 * may be involved in a complex scenario.
54 *
55 * @ingroup bus
56 */
57 enum signal_t {
58 /** pseudo signal, representing any other signal */
59 SIG_ANY,
60
61 /** debugging message from daemon main loop */
62 DBG_DMN,
63 /** debugging message from IKE_SA_MANAGER */
64 DBG_MGR,
65 /** debugging message from an IKE_SA */
66 DBG_IKE,
67 /** debugging message from a CHILD_SA */
68 DBG_CHD,
69 /** debugging message from job processing */
70 DBG_JOB,
71 /** debugging message from configuration backends */
72 DBG_CFG,
73 /** debugging message from kernel interface */
74 DBG_KNL,
75 /** debugging message from networking */
76 DBG_NET,
77 /** debugging message from message encoding/decoding */
78 DBG_ENC,
79 /** debugging message from libstrongswan via logging hook */
80 DBG_LIB,
81
82 /** number of debug signals */
83 DBG_MAX,
84
85 /** signals for IKE_SA establishment */
86 IKE_UP_START,
87 IKE_UP_SUCCESS,
88 IKE_UP_FAILED,
89
90 /** signals for IKE_SA delete */
91 IKE_DOWN_START,
92 IKE_DOWN_SUCCESS,
93 IKE_DOWN_FAILED,
94
95 /** signals for IKE_SA rekeying */
96 IKE_REKEY_START,
97 IKE_REKEY_SUCCESS,
98 IKE_REKEY_FAILED,
99
100 /** signals for CHILD_SA establishment */
101 CHILD_UP_START,
102 CHILD_UP_SUCCESS,
103 CHILD_UP_FAILED,
104
105 /** signals for CHILD_SA delete */
106 CHILD_DOWN_START,
107 CHILD_DOWN_SUCCESS,
108 CHILD_DOWN_FAILED,
109
110 /** signals for CHILD_SA rekeying */
111 CHILD_REKEY_START,
112 CHILD_REKEY_SUCCESS,
113 CHILD_REKEY_FAILED,
114
115 /** signals for CHILD_SA routing */
116 CHILD_ROUTE_START,
117 CHILD_ROUTE_SUCCESS,
118 CHILD_ROUTE_FAILED,
119
120 /** signals for CHILD_SA routing */
121 CHILD_UNROUTE_START,
122 CHILD_UNROUTE_SUCCESS,
123 CHILD_UNROUTE_FAILED,
124
125 SIG_MAX
126 };
127
128 /**
129 * short names of signals using 3 chars
130 */
131 extern enum_name_t *signal_names;
132
133 /**
134 * Signal levels used to control output verbosity.
135 */
136 enum level_t {
137 /** numerical levels from 0 to 4 */
138 LEVEL_0 = 0,
139 LEVEL_1 = 1,
140 LEVEL_2 = 2,
141 LEVEL_3 = 3,
142 LEVEL_4 = 4,
143 /** absolutely silent, no signal is emitted with this level */
144 LEVEL_SILENT = -1,
145 /** alias for numberical levels */
146 LEVEL_AUDIT = LEVEL_0,
147 LEVEL_CTRL = LEVEL_1,
148 LEVEL_CTRLMORE = LEVEL_2,
149 LEVEL_RAW = LEVEL_3,
150 LEVEL_PRIVATE = LEVEL_4,
151 };
152
153 #ifndef DEBUG_LEVEL
154 # define DEBUG_LEVEL 4
155 #endif /* DEBUG_LEVEL */
156
157 #if DEBUG_LEVEL >= 1
158 /**
159 * @brief Log a debug message via the signal bus.
160 *
161 * @param signal signal_t signal description
162 * @param format printf() style format string
163 * @param ... printf() style agument list
164 */
165 # define DBG1(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_1, format, ##__VA_ARGS__)
166 #endif /* DEBUG_LEVEL */
167 #if DEBUG_LEVEL >= 2
168 #define DBG2(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_2, format, ##__VA_ARGS__)
169 #endif /* DEBUG_LEVEL */
170 #if DEBUG_LEVEL >= 3
171 #define DBG3(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_3, format, ##__VA_ARGS__)
172 #endif /* DEBUG_LEVEL */
173 #if DEBUG_LEVEL >= 4
174 #define DBG4(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_4, format, ##__VA_ARGS__)
175 #endif /* DEBUG_LEVEL */
176
177 #ifndef DBG1
178 # define DBG1(...) {}
179 #endif /* DBG1 */
180 #ifndef DBG2
181 # define DBG2(...) {}
182 #endif /* DBG2 */
183 #ifndef DBG3
184 # define DBG3(...) {}
185 #endif /* DBG3 */
186 #ifndef DBG4
187 # define DBG4(...) {}
188 #endif /* DBG4 */
189
190 /**
191 * @brief Raise a signal for an occured event.
192 *
193 * @param sig signal_t signal description
194 * @param format printf() style format string
195 * @param ... printf() style agument list
196 */
197 #define SIG(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_0, format, ##__VA_ARGS__)
198
199 /**
200 * @brief Get the type of a signal.
201 *
202 * A signal may be a debugging signal with a specific context. They have
203 * a level specific for their context > 0. All audit signals use the
204 * type 0. This allows filtering of singals by their type.
205 *
206 * @param signal signal to get the type from
207 * @return type of the signal, between 0..(DBG_MAX-1)
208 */
209 #define SIG_TYPE(sig) (sig > DBG_MAX ? SIG_ANY : sig)
210
211
212 /**
213 * @brief Interface for registering at the signal bus.
214 *
215 * To receive signals from the bus, the client implementing the
216 * bus_listener_t interface registers itself at the signal bus.
217 *
218 * @ingroup bus
219 */
220 struct bus_listener_t {
221
222 /**
223 * @brief Send a signal to a bus listener.
224 *
225 * A numerical identification for the thread is included, as the
226 * associated IKE_SA, if any. Signal specifies the type of
227 * the event occured. The format string specifies
228 * an additional informational or error message with a printf() like
229 * variable argument list. This is in the va_list form, as forwarding
230 * a "..." parameters to functions is not (cleanly) possible.
231 * The implementing signal function returns TRUE to stay registered
232 * to the bus, or FALSE to unregister itself.
233 *
234 * @param this listener
235 * @param singal kind of the signal (up, down, rekeyed, ...)
236 * @param level verbosity level of the signal
237 * @param thread ID of the thread raised this signal
238 * @param ike_sa IKE_SA associated to the event
239 * @param format printf() style format string
240 * @param args vprintf() style va_list argument list
241 " @return TRUE to stay registered, FALSE to unregister
242 */
243 bool (*signal) (bus_listener_t *this, signal_t signal, level_t level,
244 int thread, ike_sa_t *ike_sa, char* format, va_list args);
245 };
246
247 /**
248 * @brief Signal bus which sends signals to registered listeners.
249 *
250 * The signal bus is not much more than a multiplexer. A listener interested
251 * in receiving event signals registers at the bus. Any signals sent to
252 * are delivered to all registered listeners.
253 * To deliver signals to threads, the blocking listen() call may be used
254 * to wait for a signal. However, passive listeners should be preferred,
255 * as listening actively requires some synchronization overhead as data
256 * must be passed from the raising thread to the listening thread.
257 *
258 * @ingroup bus
259 */
260 struct bus_t {
261
262 /**
263 * @brief Register a listener to the bus.
264 *
265 * A registered listener receives all signals which are sent to the bus.
266 * The listener is passive; the thread which emitted the signal
267 * processes the listener routine.
268 *
269 * @param this bus
270 * @param listener listener to register.
271 */
272 void (*add_listener) (bus_t *this, bus_listener_t *listener);
273
274 /**
275 * @brief Unregister a listener from the bus.
276 *
277 * @param this bus
278 * @param listener listener to unregister.
279 */
280 void (*remove_listener) (bus_t *this, bus_listener_t *listener);
281
282 /**
283 * @brief Listen actively on the bus.
284 *
285 * As we are fully multithreaded, we must provide a mechanism
286 * for active threads to listen to the bus. With the listen() method,
287 * a thread waits until a signal occurs, and then processes it.
288 * To prevent the listen() calling thread to miss signals emitted while
289 * it processes a signal, registration is required. This is done through
290 * the set_listen_state() method, see below.
291 *
292 * The listen() function is (has) a thread cancellation point, so you might
293 * want to register cleanup handlers.
294 *
295 * @param this bus
296 * @param level verbosity level of the signal
297 * @param thread receives thread number emitted the signal
298 * @param ike_sa receives the IKE_SA involved in the signal, or NULL
299 * @param format receives the format string supplied with the signal
300 * @param va_list receives the variable argument list for format
301 * @return the emitted signal type
302 */
303 signal_t (*listen) (bus_t *this, level_t* level, int *thread,
304 ike_sa_t **ike_sa, char** format, va_list* args);
305
306 /**
307 * @brief Set the listening state of the calling thread.
308 *
309 * To prevent message loss for active listeners using listen(), threads
310 * must register themself to the bus before starting to listen(). When
311 * a signal occurs, the emitter waits until all threads with listen_state
312 * TRUE are waiting in the listen() method to process the signal.
313 * It is important that a thread with listen_state TRUE calls listen()
314 * periodically, or sets it's listening state to FALSE; otherwise
315 * all signal emitting threads get blocked on the bus.
316 *
317 * @param this bus
318 * @param active TRUE to set to listening
319 */
320 void (*set_listen_state) (bus_t *this, bool active);
321
322 /**
323 * @brief Set the IKE_SA the calling thread is using.
324 *
325 * To associate an received signal to an IKE_SA without passing it as
326 * parameter each time, the thread registers it's used IKE_SA each
327 * time it checked it out. Before checking it in, the thread unregisters
328 * the IKE_SA (by passing NULL). This IKE_SA is stored per-thread, so each
329 * thread has one IKE_SA registered (or not).
330 *
331 * @param this bus
332 * @param ike_sa ike_sa to register, or NULL to unregister
333 */
334 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
335
336 /**
337 * @brief Send a signal to the bus.
338 *
339 * The signal specifies the type of the event occured. The format string
340 * specifies an additional informational or error message with a
341 * printf() like variable argument list.
342 * Some useful macros are available to shorten this call.
343 * @see SIG(), DBG1()
344 *
345 * @param this bus
346 * @param singal kind of the signal (up, down, rekeyed, ...)
347 * @param level verbosity level of the signal
348 * @param format printf() style format string
349 * @param ... printf() style argument list
350 */
351 void (*signal) (bus_t *this, signal_t signal, level_t level, char* format, ...);
352
353 /**
354 * @brief Send a signal to the bus using va_list arguments.
355 *
356 * Same as bus_t.signal(), but uses va_list argument list.
357 *
358 * @param this bus
359 * @param singal kind of the signal (up, down, rekeyed, ...)
360 * @param level verbosity level of the signal
361 * @param format printf() style format string
362 * @param args va_list arguments
363 */
364 void (*vsignal) (bus_t *this, signal_t signal, level_t level, char* format, va_list args);
365
366 /**
367 * @brief Destroy the signal bus.
368 *
369 * @param this bus to destroy
370 */
371 void (*destroy) (bus_t *this);
372 };
373
374 /**
375 * @brief Create the signal bus which multiplexes signals to its listeners.
376 *
377 * @return signal bus instance
378 *
379 * @ingroup bus
380 */
381 bus_t *bus_create();
382
383 #endif /* BUS_H_ */