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