added hooks for IKE and CHILD keymat
[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 * Hook called with IKE_SA key material.
185 *
186 * @param ike_sa IKE_SA this keymat belongs to
187 * @param dh diffie hellman shared secret
188 * @param nonce_i initiators nonce
189 * @param nonce_r responders nonce
190 * @param rekey IKE_SA we are rekeying, if any
191 * @return TRUE to stay registered, FALSE to unregister
192 */
193 bool (*ike_keys)(listener_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
194 chunk_t nonce_i, chunk_t nonce_r, ike_sa_t *rekey);
195
196 /**
197 * Hook called with CHILD_SA key material.
198 *
199 * @param ike_sa IKE_SA the child sa belongs to
200 * @param child_sa CHILD_SA this keymat is used for
201 * @param dh diffie hellman shared secret
202 * @param nonce_i initiators nonce
203 * @param nonce_r responders nonce
204 * @return TRUE to stay registered, FALSE to unregister
205 */
206 bool (*child_keys)(listener_t *this, ike_sa_t *ike_sa, child_sa_t *child_sa,
207 diffie_hellman_t *dh, chunk_t nonce_i, chunk_t nonce_r);
208 };
209
210 /**
211 * The bus receives events and sends them to all registered listeners.
212 *
213 * Any events sent to are delivered to all registered listeners. Threads
214 * may wait actively to events using the blocking listen() call.
215 */
216 struct bus_t {
217
218 /**
219 * Register a listener to the bus.
220 *
221 * A registered listener receives all events which are sent to the bus.
222 * The listener is passive; the thread which emitted the event
223 * processes the listener routine.
224 *
225 * @param listener listener to register.
226 */
227 void (*add_listener) (bus_t *this, listener_t *listener);
228
229 /**
230 * Unregister a listener from the bus.
231 *
232 * @param listener listener to unregister.
233 */
234 void (*remove_listener) (bus_t *this, listener_t *listener);
235
236 /**
237 * Register a listener and block the calling thread.
238 *
239 * This call registers a listener and blocks the calling thread until
240 * its listeners function returns FALSE. This allows to wait for certain
241 * events. The associated job is executed after the listener has been
242 * registered: This allows to listen on events we initiate with the job,
243 * without missing any events to job may fire.
244 *
245 * @param listener listener to register
246 * @param job job to execute asynchronously when registered, or NULL
247 */
248 void (*listen)(bus_t *this, listener_t *listener, job_t *job);
249
250 /**
251 * Set the IKE_SA the calling thread is using.
252 *
253 * To associate an received log message to an IKE_SA without passing it as
254 * parameter each time, the thread registers the currenlty used IKE_SA
255 * during check-out. Before check-in, the thread unregisters the IKE_SA.
256 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
257 * registered.
258 *
259 * @param ike_sa ike_sa to register, or NULL to unregister
260 */
261 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
262
263 /**
264 * Send a log message to the bus.
265 *
266 * The signal specifies the type of the event occured. The format string
267 * specifies an additional informational or error message with a
268 * printf() like variable argument list.
269 * Use the DBG() macros.
270 *
271 * @param group debugging group
272 * @param level verbosity level of the signal
273 * @param format printf() style format string
274 * @param ... printf() style argument list
275 */
276 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
277
278 /**
279 * Send a log message to the bus using va_list arguments.
280 *
281 * Same as bus_t.signal(), but uses va_list argument list.
282 *
283 * @param group kind of the signal (up, down, rekeyed, ...)
284 * @param level verbosity level of the signal
285 * @param format printf() style format string
286 * @param args va_list arguments
287 */
288 void (*vlog)(bus_t *this, debug_t group, level_t level,
289 char* format, va_list args);
290 /**
291 * Send a IKE_SA state change event to the bus.
292 *
293 * @param ike_sa IKE_SA which changes its state
294 * @param state new state IKE_SA changes to
295 */
296 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
297 ike_sa_state_t state);
298 /**
299 * Send a CHILD_SA state change event to the bus.
300 *
301 * @param child_sa CHILD_SA which changes its state
302 * @param state new state CHILD_SA changes to
303 */
304 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
305 child_sa_state_t state);
306 /**
307 * Message send/receive hook.
308 *
309 * @param message message to send/receive
310 * @param incoming TRUE for incoming messages, FALSE for outgoing
311 */
312 void (*message)(bus_t *this, message_t *message, bool incoming);
313
314 /**
315 * IKE_SA keymat hook.
316 *
317 * @param ike_sa IKE_SA this keymat belongs to
318 * @param dh diffie hellman shared secret
319 * @param nonce_i initiators nonce
320 * @param nonce_r responders nonce
321 * @param rekey IKE_SA we are rekeying, if any
322 */
323 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
324 chunk_t nonce_i, chunk_t nonce_r, ike_sa_t *rekey);
325 /**
326 * CHILD_SA keymat hook.
327 *
328 * @param child_sa CHILD_SA this keymat is used for
329 * @param dh diffie hellman shared secret
330 * @param nonce_i initiators nonce
331 * @param nonce_r responders nonce
332 */
333 void (*child_keys)(bus_t *this, child_sa_t *child_sa, diffie_hellman_t *dh,
334 chunk_t nonce_i, chunk_t nonce_r);
335 /**
336 * Destroy the event bus.
337 */
338 void (*destroy) (bus_t *this);
339 };
340
341 /**
342 * Create the event bus which forwards events to its listeners.
343 *
344 * @return event bus instance
345 */
346 bus_t *bus_create();
347
348 #endif /* BUS_H_ @} */