reintegrated bus-refactoring branch
[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 debug_t debug_t;
27 typedef enum level_t level_t;
28 typedef struct listener_t 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 * Debug message group.
39 */
40 enum debug_t {
41 /** daemon main loop */
42 DBG_DMN,
43 /** IKE_SA_MANAGER */
44 DBG_MGR,
45 /** IKE_SA */
46 DBG_IKE,
47 /** CHILD_SA */
48 DBG_CHD,
49 /** job processing */
50 DBG_JOB,
51 /** configuration backends */
52 DBG_CFG,
53 /** kernel interface */
54 DBG_KNL,
55 /** networking/sockets */
56 DBG_NET,
57 /** message encoding/decoding */
58 DBG_ENC,
59 /** libstrongswan via logging hook */
60 DBG_LIB,
61 /** number of groups */
62 DBG_MAX,
63 /** pseudo group with all groups */
64 DBG_ANY = DBG_MAX,
65 };
66
67 /**
68 * short names of debug message group.
69 */
70 extern enum_name_t *debug_names;
71
72 /**
73 * Debug levels used to control output verbosity.
74 */
75 enum level_t {
76 /** absolutely silent */
77 LEVEL_SILENT = -1,
78 /** most important auditing logs */
79 LEVEL_AUDIT = 0,
80 /** control flow */
81 LEVEL_CTRL = 1,
82 /** diagnose problems */
83 LEVEL_DIAG = 2,
84 /** raw binary blobs */
85 LEVEL_RAW = 3,
86 /** including sensitive data (private keys) */
87 LEVEL_PRIVATE = 4,
88 };
89
90 #ifndef DEBUG_LEVEL
91 # define DEBUG_LEVEL 4
92 #endif /* DEBUG_LEVEL */
93
94 #if DEBUG_LEVEL >= 0
95 #define DBG0(group, format, ...) charon->bus->log(charon->bus, group, 0, format, ##__VA_ARGS__)
96 #endif /* DEBUG_LEVEL >= 0 */
97 #if DEBUG_LEVEL >= 1
98 #define DBG1(group, format, ...) charon->bus->log(charon->bus, group, 1, format, ##__VA_ARGS__)
99 #endif /* DEBUG_LEVEL >= 1 */
100 #if DEBUG_LEVEL >= 2
101 #define DBG2(group, format, ...) charon->bus->log(charon->bus, group, 2, format, ##__VA_ARGS__)
102 #endif /* DEBUG_LEVEL >= 2 */
103 #if DEBUG_LEVEL >= 3
104 #define DBG3(group, format, ...) charon->bus->log(charon->bus, group, 3, format, ##__VA_ARGS__)
105 #endif /* DEBUG_LEVEL >= 3 */
106 #if DEBUG_LEVEL >= 4
107 #define DBG4(group, format, ...) charon->bus->log(charon->bus, group, 4, format, ##__VA_ARGS__)
108 #endif /* DEBUG_LEVEL >= 4 */
109
110 #ifndef DBG0
111 # define DBG0(...) {}
112 #endif /* DBG0 */
113 #ifndef DBG1
114 # define DBG1(...) {}
115 #endif /* DBG1 */
116 #ifndef DBG2
117 # define DBG2(...) {}
118 #endif /* DBG2 */
119 #ifndef DBG3
120 # define DBG3(...) {}
121 #endif /* DBG3 */
122 #ifndef DBG4
123 # define DBG4(...) {}
124 #endif /* DBG4 */
125
126
127 /**
128 * Listener interface, listens to events if registered to the bus.
129 */
130 struct listener_t {
131
132 /**
133 * Log a debugging message.
134 *
135 * The implementing signal function returns TRUE to stay registered
136 * to the bus, or FALSE to unregister itself.
137 * Calling bus_t.log() inside of a registered listener is possible,
138 * but the bus does not invoke listeners recursively.
139 *
140 * @param singal kind of the signal (up, down, rekeyed, ...)
141 * @param level verbosity level of the signal
142 * @param thread ID of the thread raised this signal
143 * @param ike_sa IKE_SA associated to the event
144 * @param format printf() style format string
145 * @param args vprintf() style va_list argument list
146 " @return TRUE to stay registered, FALSE to unregister
147 */
148 bool (*log) (listener_t *this, debug_t group, level_t level, int thread,
149 ike_sa_t *ike_sa, char* format, va_list args);
150
151 /**
152 * Handle state changes in an IKE_SA.
153 *
154 * @param ike_sa IKE_SA which changes its state
155 * @param state new IKE_SA state this IKE_SA changes to
156 * @return TRUE to stay registered, FALSE to unregister
157 */
158 bool (*ike_state_change)(listener_t *this, ike_sa_t *ike_sa,
159 ike_sa_state_t state);
160
161 /**
162 * Handle state changes in a CHILD_SA.
163 *
164 * @param ike_sa IKE_SA containing the affected CHILD_SA
165 * @param child_sa CHILD_SA which changes its state
166 * @param state new CHILD_SA state this CHILD_SA changes to
167 * @return TRUE to stay registered, FALSE to unregister
168 */
169 bool (*child_state_change)(listener_t *this, ike_sa_t *ike_sa,
170 child_sa_t *child_sa, child_sa_state_t state);
171
172 /**
173 * Hook called for received/sent messages of an IKE_SA.
174 *
175 * @param ike_sa IKE_SA sending/receving a message
176 * @param message message object
177 * @param incoming TRUE for incoming messages, FALSE for outgoing
178 * @return TRUE to stay registered, FALSE to unregister
179 */
180 bool (*message)(listener_t *this, ike_sa_t *ike_sa, message_t *message,
181 bool incoming);
182
183 };
184
185 /**
186 * The bus receives events and sends them to all registered listeners.
187 *
188 * Any events sent to are delivered to all registered listeners. Threads
189 * may wait actively to events using the blocking listen() call.
190 */
191 struct bus_t {
192
193 /**
194 * Register a listener to the bus.
195 *
196 * A registered listener receives all events which are sent to the bus.
197 * The listener is passive; the thread which emitted the event
198 * processes the listener routine.
199 *
200 * @param listener listener to register.
201 */
202 void (*add_listener) (bus_t *this, listener_t *listener);
203
204 /**
205 * Unregister a listener from the bus.
206 *
207 * @param listener listener to unregister.
208 */
209 void (*remove_listener) (bus_t *this, listener_t *listener);
210
211 /**
212 * Register a listener and block the calling thread.
213 *
214 * This call registers a listener and blocks the calling thread until
215 * its listeners function returns FALSE. This allows to wait for certain
216 * events. The associated job is executed after the listener has been
217 * registered: This allows to listen on events we initiate with the job,
218 * without missing any events to job may fire.
219 *
220 * @param listener listener to register
221 * @param job job to execute asynchronously when registered, or NULL
222 */
223 void (*listen)(bus_t *this, listener_t *listener, job_t *job);
224
225 /**
226 * Set the IKE_SA the calling thread is using.
227 *
228 * To associate an received log message to an IKE_SA without passing it as
229 * parameter each time, the thread registers the currenlty used IKE_SA
230 * during check-out. Before check-in, the thread unregisters the IKE_SA.
231 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
232 * registered.
233 *
234 * @param ike_sa ike_sa to register, or NULL to unregister
235 */
236 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
237
238 /**
239 * Send a log message to the bus.
240 *
241 * The signal specifies the type of the event occured. The format string
242 * specifies an additional informational or error message with a
243 * printf() like variable argument list.
244 * Use the DBG() macros.
245 *
246 * @param group debugging group
247 * @param level verbosity level of the signal
248 * @param format printf() style format string
249 * @param ... printf() style argument list
250 */
251 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
252
253 /**
254 * Send a log message to the bus using va_list arguments.
255 *
256 * Same as bus_t.signal(), but uses va_list argument list.
257 *
258 * @param group kind of the signal (up, down, rekeyed, ...)
259 * @param level verbosity level of the signal
260 * @param format printf() style format string
261 * @param args va_list arguments
262 */
263 void (*vlog)(bus_t *this, debug_t group, level_t level,
264 char* format, va_list args);
265
266 /**
267 * Send a IKE_SA state change event to the bus.
268 *
269 * @param ike_sa IKE_SA which changes its state
270 * @param state new state IKE_SA changes to
271 */
272 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
273 ike_sa_state_t state);
274
275 /**
276 * Send a CHILD_SA state change event to the bus.
277 *
278 * @param child_sa CHILD_SA which changes its state
279 * @param state new state CHILD_SA changes to
280 */
281 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
282 child_sa_state_t state);
283
284 /**
285 * Message send/receive hook.
286 *
287 * @param message message to send/receive
288 * @param incoming TRUE for incoming messages, FALSE for outgoing
289 */
290 void (*message)(bus_t *this, message_t *message, bool incoming);
291
292 /**
293 * Destroy the event bus.
294 */
295 void (*destroy) (bus_t *this);
296 };
297
298 /**
299 * Create the event bus which forwards events to its listeners.
300 *
301 * @return event bus instance
302 */
303 bus_t *bus_create();
304
305 #endif /* BUS_H_ @} */