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