Move debug groups from charon's bus.h to libstrongswan's debug.h.
[strongswan.git] / src / libcharon / 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 libcharon
19 */
20
21 #ifndef BUS_H_
22 #define BUS_H_
23
24 typedef enum alert_t alert_t;
25 typedef struct bus_t bus_t;
26
27 #include <stdarg.h>
28
29 #include <debug.h>
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 /* undefine the definitions from libstrongswan */
36 #undef DBG0
37 #undef DBG1
38 #undef DBG2
39 #undef DBG3
40 #undef DBG4
41
42 #ifndef DEBUG_LEVEL
43 # define DEBUG_LEVEL 4
44 #endif /* DEBUG_LEVEL */
45
46 #if DEBUG_LEVEL >= 0
47 #define DBG0(group, format, ...) charon->bus->log(charon->bus, group, 0, format, ##__VA_ARGS__)
48 #endif /* DEBUG_LEVEL >= 0 */
49 #if DEBUG_LEVEL >= 1
50 #define DBG1(group, format, ...) charon->bus->log(charon->bus, group, 1, format, ##__VA_ARGS__)
51 #endif /* DEBUG_LEVEL >= 1 */
52 #if DEBUG_LEVEL >= 2
53 #define DBG2(group, format, ...) charon->bus->log(charon->bus, group, 2, format, ##__VA_ARGS__)
54 #endif /* DEBUG_LEVEL >= 2 */
55 #if DEBUG_LEVEL >= 3
56 #define DBG3(group, format, ...) charon->bus->log(charon->bus, group, 3, format, ##__VA_ARGS__)
57 #endif /* DEBUG_LEVEL >= 3 */
58 #if DEBUG_LEVEL >= 4
59 #define DBG4(group, format, ...) charon->bus->log(charon->bus, group, 4, format, ##__VA_ARGS__)
60 #endif /* DEBUG_LEVEL >= 4 */
61
62 #ifndef DBG0
63 # define DBG0(...) {}
64 #endif /* DBG0 */
65 #ifndef DBG1
66 # define DBG1(...) {}
67 #endif /* DBG1 */
68 #ifndef DBG2
69 # define DBG2(...) {}
70 #endif /* DBG2 */
71 #ifndef DBG3
72 # define DBG3(...) {}
73 #endif /* DBG3 */
74 #ifndef DBG4
75 # define DBG4(...) {}
76 #endif /* DBG4 */
77
78 /**
79 * Kind of alerts to raise.
80 */
81 enum alert_t {
82 /* a RADIUS server did not respond, no additional arguments */
83 ALERT_RADIUS_NOT_RESPONDING,
84 /* a shutdown signal has been received, argument is a int with the signal */
85 ALERT_SHUTDOWN_SIGNAL,
86 };
87
88 /**
89 * The bus receives events and sends them to all registered listeners.
90 *
91 * Any events sent to are delivered to all registered listeners. Threads
92 * may wait actively to events using the blocking listen() call.
93 */
94 struct bus_t {
95
96 /**
97 * Register a listener to the bus.
98 *
99 * A registered listener receives all events which are sent to the bus.
100 * The listener is passive; the thread which emitted the event
101 * processes the listener routine.
102 *
103 * @param listener listener to register.
104 */
105 void (*add_listener) (bus_t *this, listener_t *listener);
106
107 /**
108 * Unregister a listener from the bus.
109 *
110 * @param listener listener to unregister.
111 */
112 void (*remove_listener) (bus_t *this, listener_t *listener);
113
114 /**
115 * Register a listener and block the calling thread.
116 *
117 * This call registers a listener and blocks the calling thread until
118 * its listeners function returns FALSE. This allows to wait for certain
119 * events. The associated job is executed after the listener has been
120 * registered: This allows to listen on events we initiate with the job,
121 * without missing any events to job may fire.
122 *
123 * @param listener listener to register
124 * @param job job to execute asynchronously when registered, or NULL
125 */
126 void (*listen)(bus_t *this, listener_t *listener, job_t *job);
127
128 /**
129 * Set the IKE_SA the calling thread is using.
130 *
131 * To associate an received log message to an IKE_SA without passing it as
132 * parameter each time, the thread registers the currenlty used IKE_SA
133 * during check-out. Before check-in, the thread unregisters the IKE_SA.
134 * This IKE_SA is stored per-thread, so each thread has its own IKE_SA
135 * registered.
136 *
137 * @param ike_sa ike_sa to register, or NULL to unregister
138 */
139 void (*set_sa) (bus_t *this, ike_sa_t *ike_sa);
140
141 /**
142 * Get the IKE_SA the calling thread is currently using.
143 *
144 * If a thread currently does not know what IKE_SA it is processing,
145 * it can call get_sa() to look up the SA set during checkout via set_sa().
146 *
147 * @return registered ike_sa, NULL if none registered
148 */
149 ike_sa_t* (*get_sa)(bus_t *this);
150
151 /**
152 * Send a log message to the bus.
153 *
154 * The signal specifies the type of the event occured. The format string
155 * specifies an additional informational or error message with a
156 * printf() like variable argument list.
157 * Use the DBG() macros.
158 *
159 * @param group debugging group
160 * @param level verbosity level of the signal
161 * @param format printf() style format string
162 * @param ... printf() style argument list
163 */
164 void (*log)(bus_t *this, debug_t group, level_t level, char* format, ...);
165
166 /**
167 * Send a log message to the bus using va_list arguments.
168 *
169 * Same as bus_t.signal(), but uses va_list argument list.
170 *
171 * @param group kind of the signal (up, down, rekeyed, ...)
172 * @param level verbosity level of the signal
173 * @param format printf() style format string
174 * @param args va_list arguments
175 */
176 void (*vlog)(bus_t *this, debug_t group, level_t level,
177 char* format, va_list args);
178
179 /**
180 * Raise an alert over the bus.
181 *
182 * @param alert kind of alert
183 * @param ... alert specific attributes
184 */
185 void (*alert)(bus_t *this, alert_t alert, ...);
186
187 /**
188 * Send a IKE_SA state change event to the bus.
189 *
190 * @param ike_sa IKE_SA which changes its state
191 * @param state new state IKE_SA changes to
192 */
193 void (*ike_state_change)(bus_t *this, ike_sa_t *ike_sa,
194 ike_sa_state_t state);
195 /**
196 * Send a CHILD_SA state change event to the bus.
197 *
198 * @param child_sa CHILD_SA which changes its state
199 * @param state new state CHILD_SA changes to
200 */
201 void (*child_state_change)(bus_t *this, child_sa_t *child_sa,
202 child_sa_state_t state);
203 /**
204 * Message send/receive hook.
205 *
206 * @param message message to send/receive
207 * @param incoming TRUE for incoming messages, FALSE for outgoing
208 */
209 void (*message)(bus_t *this, message_t *message, bool incoming);
210
211 /**
212 * IKE_SA authorization hook.
213 *
214 * @param final TRUE if this is the final invocation
215 * @return TRUE to establish IKE_SA, FALSE to send AUTH_FAILED
216 */
217 bool (*authorize)(bus_t *this, bool final);
218
219 /**
220 * IKE_SA keymat hook.
221 *
222 * @param ike_sa IKE_SA this keymat belongs to
223 * @param dh diffie hellman shared secret
224 * @param nonce_i initiators nonce
225 * @param nonce_r responders nonce
226 * @param rekey IKE_SA we are rekeying, if any
227 */
228 void (*ike_keys)(bus_t *this, ike_sa_t *ike_sa, diffie_hellman_t *dh,
229 chunk_t nonce_i, chunk_t nonce_r, ike_sa_t *rekey);
230 /**
231 * CHILD_SA keymat hook.
232 *
233 * @param child_sa CHILD_SA this keymat is used for
234 * @param dh diffie hellman shared secret
235 * @param nonce_i initiators nonce
236 * @param nonce_r responders nonce
237 */
238 void (*child_keys)(bus_t *this, child_sa_t *child_sa, diffie_hellman_t *dh,
239 chunk_t nonce_i, chunk_t nonce_r);
240
241 /**
242 * IKE_SA up/down hook.
243 *
244 * @param ike_sa IKE_SA coming up/going down
245 * @param up TRUE for an up event, FALSE for a down event
246 */
247 void (*ike_updown)(bus_t *this, ike_sa_t *ike_sa, bool up);
248
249 /**
250 * IKE_SA rekeying hook.
251 *
252 * @param old rekeyed and obsolete IKE_SA
253 * @param new new IKE_SA replacing old
254 */
255 void (*ike_rekey)(bus_t *this, ike_sa_t *old, ike_sa_t *new);
256
257 /**
258 * CHILD_SA up/down hook.
259 *
260 * @param child_sa CHILD_SA coming up/going down
261 * @param up TRUE for an up event, FALSE for a down event
262 */
263 void (*child_updown)(bus_t *this, child_sa_t *child_sa, bool up);
264
265 /**
266 * CHILD_SA rekeying hook.
267 *
268 * @param old rekeyed and obsolete CHILD_SA
269 * @param new new CHILD_SA replacing old
270 */
271 void (*child_rekey)(bus_t *this, child_sa_t *old, child_sa_t *new);
272
273 /**
274 * Destroy the event bus.
275 */
276 void (*destroy) (bus_t *this);
277 };
278
279 /**
280 * Create the event bus which forwards events to its listeners.
281 *
282 * @return event bus instance
283 */
284 bus_t *bus_create();
285
286 #endif /** BUS_H_ @}*/