added an alert() bus hook to raise critical system errors and notifications
[strongswan.git] / src / charon / bus / bus.h
1 /*
2 * Copyright (C) 2006-2009 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
16 /**
17 * @defgroup bus bus
18 * @{ @ingroup charon
19 */
20
21 #ifndef BUS_H_
22 #define BUS_H_
23
24 typedef enum debug_t debug_t;
25 typedef enum level_t level_t;
26 typedef enum alert_t alert_t;
27 typedef struct bus_t bus_t;
28
29 #include <stdarg.h>
30
31 #include <sa/ike_sa.h>
32 #include <sa/child_sa.h>
33 #include <processing/jobs/job.h>
34 #include <bus/listeners/listener.h>
35
36 /**
37 * Debug message group.
38 */
39 enum debug_t {
40 /** daemon main loop */
41 DBG_DMN,
42 /** IKE_SA_MANAGER */
43 DBG_MGR,
44 /** IKE_SA */
45 DBG_IKE,
46 /** CHILD_SA */
47 DBG_CHD,
48 /** job processing */
49 DBG_JOB,
50 /** configuration backends */
51 DBG_CFG,
52 /** kernel interface */
53 DBG_KNL,
54 /** networking/sockets */
55 DBG_NET,
56 /** message encoding/decoding */
57 DBG_ENC,
58 /** libstrongswan via logging hook */
59 DBG_LIB,
60 /** number of groups */
61 DBG_MAX,
62 /** pseudo group with all groups */
63 DBG_ANY = DBG_MAX,
64 };
65
66 /**
67 * short names of debug message group.
68 */
69 extern enum_name_t *debug_names;
70
71 /**
72 * short names of debug message group, lower case.
73 */
74 extern enum_name_t *debug_lower_names;
75
76 /**
77 * Debug levels used to control output verbosity.
78 */
79 enum level_t {
80 /** absolutely silent */
81 LEVEL_SILENT = -1,
82 /** most important auditing logs */
83 LEVEL_AUDIT = 0,
84 /** control flow */
85 LEVEL_CTRL = 1,
86 /** diagnose problems */
87 LEVEL_DIAG = 2,
88 /** raw binary blobs */
89 LEVEL_RAW = 3,
90 /** including sensitive data (private keys) */
91 LEVEL_PRIVATE = 4,
92 };
93
94 #ifndef DEBUG_LEVEL
95 # define DEBUG_LEVEL 4
96 #endif /* DEBUG_LEVEL */
97
98 #if DEBUG_LEVEL >= 0
99 #define DBG0(group, format, ...) charon->bus->log(charon->bus, group, 0, format, ##__VA_ARGS__)
100 #endif /* DEBUG_LEVEL >= 0 */
101 #if DEBUG_LEVEL >= 1
102 #define DBG1(group, format, ...) charon->bus->log(charon->bus, group, 1, format, ##__VA_ARGS__)
103 #endif /* DEBUG_LEVEL >= 1 */
104 #if DEBUG_LEVEL >= 2
105 #define DBG2(group, format, ...) charon->bus->log(charon->bus, group, 2, format, ##__VA_ARGS__)
106 #endif /* DEBUG_LEVEL >= 2 */
107 #if DEBUG_LEVEL >= 3
108 #define DBG3(group, format, ...) charon->bus->log(charon->bus, group, 3, format, ##__VA_ARGS__)
109 #endif /* DEBUG_LEVEL >= 3 */
110 #if DEBUG_LEVEL >= 4
111 #define DBG4(group, format, ...) charon->bus->log(charon->bus, group, 4, format, ##__VA_ARGS__)
112 #endif /* DEBUG_LEVEL >= 4 */
113
114 #ifndef DBG0
115 # define DBG0(...) {}
116 #endif /* DBG0 */
117 #ifndef DBG1
118 # define DBG1(...) {}
119 #endif /* DBG1 */
120 #ifndef DBG2
121 # define DBG2(...) {}
122 #endif /* DBG2 */
123 #ifndef DBG3
124 # define DBG3(...) {}
125 #endif /* DBG3 */
126 #ifndef DBG4
127 # define DBG4(...) {}
128 #endif /* DBG4 */
129
130 /**
131 * Kind of alerts to raise.
132 */
133 enum alert_t {
134 };
135
136 /**
137 * The bus receives events and sends them to all registered listeners.
138 *
139 * Any events sent to are delivered to all registered listeners. Threads
140 * may wait actively to events using the blocking listen() call.
141 */
142 struct bus_t {
143
144 /**
145 * Register a listener to the bus.
146 *
147 * A registered listener receives all events which are sent to the bus.
148 * The listener is passive; the thread which emitted the event
149 * processes the listener routine.
150 *
151 * @param listener listener to register.
152 */
153 void (*add_listener) (bus_t *this, listener_t *listener);
154
155 /**
156 * Unregister a listener from the bus.
157 *
158 * @param listener listener to unregister.
159 */
160 void (*remove_listener) (bus_t *this, listener_t *listener);
161
162 /**
163 * Register a listener and block the calling thread.
164 *
165 * This call registers a listener and blocks the calling thread until
166 * its listeners function returns FALSE. This allows to wait for certain
167 * events. The associated job is executed after the listener has been
168 * registered: This allows to listen on events we initiate with the job,
169 * without missing any events to job may fire.
170 *
171 * @param listener listener to register
172 * @param job job to execute asynchronously when registered, or NULL
173 */
174 void (*listen)(bus_t *this, listener_t *listener, job_t *job);
175
176 /**
177 * Set the IKE_SA the calling thread is using.
178 *
179 * To associate an received log message to an IKE_SA without passing it as
180 * parameter each time, the thread registers the currenlty used IKE_SA
181 * during check-out. Before check-in, the thread unregisters the IKE_SA.
182 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
183 * registered.
184 *
185 * @param ike_sa ike_sa to register, or NULL to unregister
186 */
187 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
188
189 /**
190 * Send a log message to the bus.
191 *
192 * The signal specifies the type of the event occured. The format string
193 * specifies an additional informational or error message with a
194 * printf() like variable argument list.
195 * Use the DBG() macros.
196 *
197 * @param group debugging group
198 * @param level verbosity level of the signal
199 * @param format printf() style format string
200 * @param ... printf() style argument list
201 */
202 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
203
204 /**
205 * Send a log message to the bus using va_list arguments.
206 *
207 * Same as bus_t.signal(), but uses va_list argument list.
208 *
209 * @param group kind of the signal (up, down, rekeyed, ...)
210 * @param level verbosity level of the signal
211 * @param format printf() style format string
212 * @param args va_list arguments
213 */
214 void (*vlog)(bus_t *this, debug_t group, level_t level,
215 char* format, va_list args);
216
217 /**
218 * Raise an alert over the bus.
219 *
220 * @param alert kind of alert
221 * @param ... alert specific attributes
222 */
223 void (*alert)(bus_t *this, alert_t alert, ...);
224
225 /**
226 * Send a IKE_SA state change event to the bus.
227 *
228 * @param ike_sa IKE_SA which changes its state
229 * @param state new state IKE_SA changes to
230 */
231 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
232 ike_sa_state_t state);
233 /**
234 * Send a CHILD_SA state change event to the bus.
235 *
236 * @param child_sa CHILD_SA which changes its state
237 * @param state new state CHILD_SA changes to
238 */
239 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
240 child_sa_state_t state);
241 /**
242 * Message send/receive hook.
243 *
244 * @param message message to send/receive
245 * @param incoming TRUE for incoming messages, FALSE for outgoing
246 */
247 void (*message)(bus_t *this, message_t *message, bool incoming);
248
249 /**
250 * IKE_SA authorization hook.
251 *
252 * @param auth list of auth_cfg_t, containing peers authentication info
253 * @param final TRUE if this is the final invocation
254 * @return TRUE to establish IKE_SA, FALSE to send AUTH_FAILED
255 */
256 bool (*authorize)(bus_t *this, linked_list_t *auth, bool final);
257
258 /**
259 * IKE_SA keymat hook.
260 *
261 * @param ike_sa IKE_SA this keymat belongs to
262 * @param dh diffie hellman shared secret
263 * @param nonce_i initiators nonce
264 * @param nonce_r responders nonce
265 * @param rekey IKE_SA we are rekeying, if any
266 */
267 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
268 chunk_t nonce_i, chunk_t nonce_r, ike_sa_t *rekey);
269 /**
270 * CHILD_SA keymat hook.
271 *
272 * @param child_sa CHILD_SA this keymat is used for
273 * @param dh diffie hellman shared secret
274 * @param nonce_i initiators nonce
275 * @param nonce_r responders nonce
276 */
277 void (*child_keys)(bus_t *this, child_sa_t *child_sa, diffie_hellman_t *dh,
278 chunk_t nonce_i, chunk_t nonce_r);
279
280 /**
281 * IKE_SA up/down hook.
282 *
283 * @param ike_sa IKE_SA coming up/going down
284 * @param up TRUE for an up event, FALSE for a down event
285 */
286 void (*ike_updown)(bus_t *this, ike_sa_t *ike_sa, bool up);
287
288 /**
289 * IKE_SA rekeying hook.
290 *
291 * @param old rekeyed and obsolete IKE_SA
292 * @param new new IKE_SA replacing old
293 */
294 void (*ike_rekey)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
295
296 /**
297 * CHILD_SA up/down hook.
298 *
299 * @param child_sa CHILD_SA coming up/going down
300 * @param up TRUE for an up event, FALSE for a down event
301 */
302 void (*child_updown)(bus_t *this, child_sa_t *child_sa, bool up);
303
304 /**
305 * CHILD_SA rekeying hook.
306 *
307 * @param old rekeyed and obsolete CHILD_SA
308 * @param new new CHILD_SA replacing old
309 */
310 void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
311
312 /**
313 * Destroy the event bus.
314 */
315 void (*destroy) (bus_t *this);
316 };
317
318 /**
319 * Create the event bus which forwards events to its listeners.
320 *
321 * @return event bus instance
322 */
323 bus_t *bus_create();
324
325 #endif /** BUS_H_ @}*/