67603bf21ac8df6dbba888a53245fa7dba512245
[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 bus_t bus_t;
27
28 #include <stdarg.h>
29
30 #include <sa/ike_sa.h>
31 #include <sa/child_sa.h>
32 #include <processing/jobs/job.h>
33 #include <bus/listeners/listener.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 * The bus receives events and sends them to all registered listeners.
131 *
132 * Any events sent to are delivered to all registered listeners. Threads
133 * may wait actively to events using the blocking listen() call.
134 */
135 struct bus_t {
136
137 /**
138 * Register a listener to the bus.
139 *
140 * A registered listener receives all events which are sent to the bus.
141 * The listener is passive; the thread which emitted the event
142 * processes the listener routine.
143 *
144 * @param listener listener to register.
145 */
146 void (*add_listener) (bus_t *this, listener_t *listener);
147
148 /**
149 * Unregister a listener from the bus.
150 *
151 * @param listener listener to unregister.
152 */
153 void (*remove_listener) (bus_t *this, listener_t *listener);
154
155 /**
156 * Register a listener and block the calling thread.
157 *
158 * This call registers a listener and blocks the calling thread until
159 * its listeners function returns FALSE. This allows to wait for certain
160 * events. The associated job is executed after the listener has been
161 * registered: This allows to listen on events we initiate with the job,
162 * without missing any events to job may fire.
163 *
164 * @param listener listener to register
165 * @param job job to execute asynchronously when registered, or NULL
166 */
167 void (*listen)(bus_t *this, listener_t *listener, job_t *job);
168
169 /**
170 * Set the IKE_SA the calling thread is using.
171 *
172 * To associate an received log message to an IKE_SA without passing it as
173 * parameter each time, the thread registers the currenlty used IKE_SA
174 * during check-out. Before check-in, the thread unregisters the IKE_SA.
175 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
176 * registered.
177 *
178 * @param ike_sa ike_sa to register, or NULL to unregister
179 */
180 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
181
182 /**
183 * Send a log message to the bus.
184 *
185 * The signal specifies the type of the event occured. The format string
186 * specifies an additional informational or error message with a
187 * printf() like variable argument list.
188 * Use the DBG() macros.
189 *
190 * @param group debugging group
191 * @param level verbosity level of the signal
192 * @param format printf() style format string
193 * @param ... printf() style argument list
194 */
195 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
196
197 /**
198 * Send a log message to the bus using va_list arguments.
199 *
200 * Same as bus_t.signal(), but uses va_list argument list.
201 *
202 * @param group kind of the signal (up, down, rekeyed, ...)
203 * @param level verbosity level of the signal
204 * @param format printf() style format string
205 * @param args va_list arguments
206 */
207 void (*vlog)(bus_t *this, debug_t group, level_t level,
208 char* format, va_list args);
209 /**
210 * Send a IKE_SA state change event to the bus.
211 *
212 * @param ike_sa IKE_SA which changes its state
213 * @param state new state IKE_SA changes to
214 */
215 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
216 ike_sa_state_t state);
217 /**
218 * Send a CHILD_SA state change event to the bus.
219 *
220 * @param child_sa CHILD_SA which changes its state
221 * @param state new state CHILD_SA changes to
222 */
223 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
224 child_sa_state_t state);
225 /**
226 * Message send/receive hook.
227 *
228 * @param message message to send/receive
229 * @param incoming TRUE for incoming messages, FALSE for outgoing
230 */
231 void (*message)(bus_t *this, message_t *message, bool incoming);
232
233 /**
234 * IKE_SA authorization hook.
235 *
236 * @param auth list of auth_cfg_t, containing peers authentication info
237 * @param final TRUE if this is the final invocation
238 * @return TRUE to establish IKE_SA, FALSE to send AUTH_FAILED
239 */
240 bool (*authorize)(bus_t *this, linked_list_t *auth, bool final);
241
242 /**
243 * IKE_SA keymat hook.
244 *
245 * @param ike_sa IKE_SA this keymat belongs to
246 * @param dh diffie hellman shared secret
247 * @param nonce_i initiators nonce
248 * @param nonce_r responders nonce
249 * @param rekey IKE_SA we are rekeying, if any
250 */
251 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
252 chunk_t nonce_i, chunk_t nonce_r, ike_sa_t *rekey);
253 /**
254 * CHILD_SA keymat hook.
255 *
256 * @param child_sa CHILD_SA this keymat is used for
257 * @param dh diffie hellman shared secret
258 * @param nonce_i initiators nonce
259 * @param nonce_r responders nonce
260 */
261 void (*child_keys)(bus_t *this, child_sa_t *child_sa, diffie_hellman_t *dh,
262 chunk_t nonce_i, chunk_t nonce_r);
263
264 /**
265 * IKE_SA up/down hook.
266 *
267 * @param ike_sa IKE_SA coming up/going down
268 * @param up TRUE for an up event, FALSE for a down event
269 */
270 void (*ike_updown)(bus_t *this, ike_sa_t *ike_sa, bool up);
271
272 /**
273 * IKE_SA rekeying hook.
274 *
275 * @param old rekeyed and obsolete IKE_SA
276 * @param new new IKE_SA replacing old
277 */
278 void (*ike_rekey)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
279
280 /**
281 * CHILD_SA up/down hook.
282 *
283 * @param child_sa CHILD_SA coming up/going down
284 * @param up TRUE for an up event, FALSE for a down event
285 */
286 void (*child_updown)(bus_t *this, child_sa_t *child_sa, bool up);
287
288 /**
289 * CHILD_SA rekeying hook.
290 *
291 * @param old rekeyed and obsolete CHILD_SA
292 * @param new new CHILD_SA replacing old
293 */
294 void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
295
296 /**
297 * Destroy the event bus.
298 */
299 void (*destroy) (bus_t *this);
300 };
301
302 /**
303 * Create the event bus which forwards events to its listeners.
304 *
305 * @return event bus instance
306 */
307 bus_t *bus_create();
308
309 #endif /** BUS_H_ @}*/