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