4 * @brief Interface of bus_t.
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
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
;
33 #include <sa/ike_sa.h>
34 #include <sa/child_sa.h>
38 * @brief signals emitted by the daemon.
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.
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.
58 /** pseudo signal, representing any other signal */
61 /** debugging message from daemon main loop */
63 /** debugging message from IKE_SA_MANAGER */
65 /** debugging message from an IKE_SA */
67 /** debugging message from a CHILD_SA */
69 /** debugging message from job processing */
71 /** debugging message from configuration backends */
73 /** debugging message from kernel interface */
75 /** debugging message from networking */
77 /** debugging message from message encoding/decoding */
79 /** debugging message from libstrongswan via logging hook */
82 /** number of debug signals */
85 /** signals for IKE_SA establishment */
90 /** signals for IKE_SA delete */
95 /** signals for IKE_SA rekeying */
100 /** signals for CHILD_SA establishment */
105 /** signals for CHILD_SA delete */
110 /** signals for CHILD_SA rekeying */
115 /** signals for CHILD_SA routing */
120 /** signals for CHILD_SA routing */
122 CHILD_UNROUTE_SUCCESS
,
123 CHILD_UNROUTE_FAILED
,
129 * short names of signals using 3 chars
131 extern enum_name_t
*signal_names
;
134 * Signal levels used to control output verbosity.
137 /** numerical levels from 0 to 4 */
143 /** absolutely silent, no signal is emitted with this level */
145 /** alias for numberical levels */
146 LEVEL_AUDIT
= LEVEL_0
,
147 LEVEL_CTRL
= LEVEL_1
,
148 LEVEL_CTRLMORE
= LEVEL_2
,
150 LEVEL_PRIVATE
= LEVEL_4
,
154 # define DEBUG_LEVEL 4
155 #endif /* DEBUG_LEVEL */
159 * @brief Log a debug message via the signal bus.
161 * @param signal signal_t signal description
162 * @param format printf() style format string
163 * @param ... printf() style agument list
165 # define DBG1(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_1, format, ##__VA_ARGS__)
166 #endif /* DEBUG_LEVEL */
168 #define DBG2(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_2, format, ##__VA_ARGS__)
169 #endif /* DEBUG_LEVEL */
171 #define DBG3(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_3, format, ##__VA_ARGS__)
172 #endif /* DEBUG_LEVEL */
174 #define DBG4(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_4, format, ##__VA_ARGS__)
175 #endif /* DEBUG_LEVEL */
178 # define DBG1(...) {}
181 # define DBG2(...) {}
184 # define DBG3(...) {}
187 # define DBG4(...) {}
191 * @brief Raise a signal for an occured event.
193 * @param sig signal_t signal description
194 * @param format printf() style format string
195 * @param ... printf() style agument list
197 #define SIG(sig, format, ...) charon->bus->signal(charon->bus, sig, LEVEL_0, format, ##__VA_ARGS__)
200 * @brief Get the type of a signal.
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.
206 * @param signal signal to get the type from
207 * @return type of the signal, between 0..(DBG_MAX-1)
209 #define SIG_TYPE(sig) (sig > DBG_MAX ? SIG_ANY : sig)
213 * @brief Interface for registering at the signal bus.
215 * To receive signals from the bus, the client implementing the
216 * bus_listener_t interface registers itself at the signal bus.
220 struct bus_listener_t
{
223 * @brief Send a signal to a bus listener.
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.
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
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
);
248 * @brief Signal bus which sends signals to registered listeners.
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.
263 * @brief Register a listener to the bus.
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.
270 * @param listener listener to register.
272 void (*add_listener
) (bus_t
*this, bus_listener_t
*listener
);
275 * @brief Unregister a listener from the bus.
278 * @param listener listener to unregister.
280 void (*remove_listener
) (bus_t
*this, bus_listener_t
*listener
);
283 * @brief Listen actively on the bus.
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.
292 * The listen() function is (has) a thread cancellation point, so you might
293 * want to register cleanup handlers.
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
303 signal_t (*listen
) (bus_t
*this, level_t
* level
, int *thread
,
304 ike_sa_t
**ike_sa
, char** format
, va_list* args
);
307 * @brief Set the listening state of the calling thread.
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 liste_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.
318 * @param active TRUE to set to listening
320 void (*set_listen_state
) (bus_t
*this, bool active
);
323 * @brief Set the IKE_SA the calling thread is using.
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).
332 * @param ike_sa ike_sa to register, or NULL to unregister
334 void (*set_sa
) (bus_t
*this, ike_sa_t
*ike_sa
);
337 * @brief Send a signal to the bus.
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.
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
351 void (*signal
) (bus_t
*this, signal_t signal
, level_t level
, char* format
, ...);
354 * @brief Send a signal to the bus using va_list arguments.
356 * Same as bus_t.signal(), but uses va_list argument list.
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
364 void (*vsignal
) (bus_t
*this, signal_t signal
, level_t level
, char* format
, va_list args
);
367 * @brief Destroy the signal bus.
369 * @param this bus to destroy
371 void (*destroy
) (bus_t
*this);
375 * @brief Create the signal bus which multiplexes signals to its listeners.
377 * @return signal bus instance