Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / src / utils / wpa_debug.h
1 /*
2  * wpa_supplicant/hostapd / Debug prints
3  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #ifndef WPA_DEBUG_H
10 #define WPA_DEBUG_H
11
12 #include "wpabuf.h"
13
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17
18 extern int wpa_debug_level;
19 extern int wpa_debug_show_keys;
20 extern int wpa_debug_timestamp;
21
22
23 /* Debugging function - conditional printf and hex dump. Driver wrappers can
24  * use these for debugging purposes. */
25
26 enum {
27         MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
28 };
29
30 #ifdef CONFIG_NO_STDOUT_DEBUG
31
32 #define wpa_debug_print_timestamp() do { } while (0)
33 #define wpa_printf(args...) do { } while (0)
34 #define wpa_hexdump(l,t,b,le) do { } while (0)
35 #define wpa_hexdump_buf(l,t,b) do { } while (0)
36 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
37 #define wpa_hexdump_buf_key(l,t,b) do { } while (0)
38 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
39 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
40 #define wpa_debug_open_file(p) do { } while (0)
41 #define wpa_debug_close_file() do { } while (0)
42 #define wpa_debug_setup_stdout() do { } while (0)
43 #define wpa_dbg(args...) do { } while (0)
44
45 static inline int wpa_debug_reopen_file(void)
46 {
47         return 0;
48 }
49
50 #else /* CONFIG_NO_STDOUT_DEBUG */
51
52 int wpa_debug_open_file(const char *path);
53 int wpa_debug_reopen_file(void);
54 void wpa_debug_close_file(void);
55 void wpa_debug_setup_stdout(void);
56
57 /**
58  * wpa_debug_printf_timestamp - Print timestamp for debug output
59  *
60  * This function prints a timestamp in seconds_from_1970.microsoconds
61  * format if debug output has been configured to include timestamps in debug
62  * messages.
63  */
64 void wpa_debug_print_timestamp(void);
65
66 /**
67  * wpa_printf - conditional printf
68  * @level: priority level (MSG_*) of the message
69  * @fmt: printf format string, followed by optional arguments
70  *
71  * This function is used to print conditional debugging and error messages. The
72  * output may be directed to stdout, stderr, and/or syslog based on
73  * configuration.
74  *
75  * Note: New line '\n' is added to the end of the text when printing to stdout.
76  */
77 void wpa_printf(int level, const char *fmt, ...)
78 PRINTF_FORMAT(2, 3);
79
80 /**
81  * wpa_hexdump - conditional hex dump
82  * @level: priority level (MSG_*) of the message
83  * @title: title of for the message
84  * @buf: data buffer to be dumped
85  * @len: length of the buf
86  *
87  * This function is used to print conditional debugging and error messages. The
88  * output may be directed to stdout, stderr, and/or syslog based on
89  * configuration. The contents of buf is printed out has hex dump.
90  */
91 void wpa_hexdump(int level, const char *title, const void *buf, size_t len);
92
93 static inline void wpa_hexdump_buf(int level, const char *title,
94                                    const struct wpabuf *buf)
95 {
96 //!!LOCAL       wpa_hexdump(level, title, (const u8 *)wpabuf_head(buf), wpabuf_len(buf));
97
98         wpa_hexdump(level, title, buf ? wpabuf_head(buf) : NULL,
99                     buf ? wpabuf_len(buf) : 0);
100 }
101
102 /**
103  * wpa_hexdump_key - conditional hex dump, hide keys
104  * @level: priority level (MSG_*) of the message
105  * @title: title of for the message
106  * @buf: data buffer to be dumped
107  * @len: length of the buf
108  *
109  * This function is used to print conditional debugging and error messages. The
110  * output may be directed to stdout, stderr, and/or syslog based on
111  * configuration. The contents of buf is printed out has hex dump. This works
112  * like wpa_hexdump(), but by default, does not include secret keys (passwords,
113  * etc.) in debug output.
114  */
115 void wpa_hexdump_key(int level, const char *title, const void *buf, size_t len);
116
117 static inline void wpa_hexdump_buf_key(int level, const char *title,
118                                        const struct wpabuf *buf)
119 {
120 //!! LOCAL      wpa_hexdump_key(level, title, (const u8 *)wpabuf_head(buf), wpabuf_len(buf));
121
122         wpa_hexdump_key(level, title, buf ? wpabuf_head(buf) : NULL,
123                         buf ? wpabuf_len(buf) : 0);
124 }
125
126 /**
127  * wpa_hexdump_ascii - conditional hex dump
128  * @level: priority level (MSG_*) of the message
129  * @title: title of for the message
130  * @buf: data buffer to be dumped
131  * @len: length of the buf
132  *
133  * This function is used to print conditional debugging and error messages. The
134  * output may be directed to stdout, stderr, and/or syslog based on
135  * configuration. The contents of buf is printed out has hex dump with both
136  * the hex numbers and ASCII characters (for printable range) are shown. 16
137  * bytes per line will be shown.
138  */
139 void wpa_hexdump_ascii(int level, const char *title, const void *buf,
140                        size_t len);
141
142 /**
143  * wpa_hexdump_ascii_key - conditional hex dump, hide keys
144  * @level: priority level (MSG_*) of the message
145  * @title: title of for the message
146  * @buf: data buffer to be dumped
147  * @len: length of the buf
148  *
149  * This function is used to print conditional debugging and error messages. The
150  * output may be directed to stdout, stderr, and/or syslog based on
151  * configuration. The contents of buf is printed out has hex dump with both
152  * the hex numbers and ASCII characters (for printable range) are shown. 16
153  * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
154  * default, does not include secret keys (passwords, etc.) in debug output.
155  */
156 void wpa_hexdump_ascii_key(int level, const char *title, const void *buf,
157                            size_t len);
158
159 /*
160  * wpa_dbg() behaves like wpa_msg(), but it can be removed from build to reduce
161  * binary size. As such, it should be used with debugging messages that are not
162  * needed in the control interface while wpa_msg() has to be used for anything
163  * that needs to shown to control interface monitors.
164  */
165 #define wpa_dbg(args...) wpa_msg(args)
166
167 #endif /* CONFIG_NO_STDOUT_DEBUG */
168
169
170 #ifdef CONFIG_NO_WPA_MSG
171 #define wpa_msg(args...) do { } while (0)
172 #define wpa_msg_ctrl(args...) do { } while (0)
173 #define wpa_msg_global(args...) do { } while (0)
174 #define wpa_msg_global_ctrl(args...) do { } while (0)
175 #define wpa_msg_no_global(args...) do { } while (0)
176 #define wpa_msg_global_only(args...) do { } while (0)
177 #define wpa_msg_register_cb(f) do { } while (0)
178 #define wpa_msg_register_ifname_cb(f) do { } while (0)
179 #else /* CONFIG_NO_WPA_MSG */
180 /**
181  * wpa_msg - Conditional printf for default target and ctrl_iface monitors
182  * @ctx: Pointer to context data; this is the ctx variable registered
183  *      with struct wpa_driver_ops::init()
184  * @level: priority level (MSG_*) of the message
185  * @fmt: printf format string, followed by optional arguments
186  *
187  * This function is used to print conditional debugging and error messages. The
188  * output may be directed to stdout, stderr, and/or syslog based on
189  * configuration. This function is like wpa_printf(), but it also sends the
190  * same message to all attached ctrl_iface monitors.
191  *
192  * Note: New line '\n' is added to the end of the text when printing to stdout.
193  */
194 void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
195
196 /**
197  * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors
198  * @ctx: Pointer to context data; this is the ctx variable registered
199  *      with struct wpa_driver_ops::init()
200  * @level: priority level (MSG_*) of the message
201  * @fmt: printf format string, followed by optional arguments
202  *
203  * This function is used to print conditional debugging and error messages.
204  * This function is like wpa_msg(), but it sends the output only to the
205  * attached ctrl_iface monitors. In other words, it can be used for frequent
206  * events that do not need to be sent to syslog.
207  */
208 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
209 PRINTF_FORMAT(3, 4);
210
211 /**
212  * wpa_msg_global - Global printf for ctrl_iface monitors
213  * @ctx: Pointer to context data; this is the ctx variable registered
214  *      with struct wpa_driver_ops::init()
215  * @level: priority level (MSG_*) of the message
216  * @fmt: printf format string, followed by optional arguments
217  *
218  * This function is used to print conditional debugging and error messages.
219  * This function is like wpa_msg(), but it sends the output as a global event,
220  * i.e., without being specific to an interface. For backwards compatibility,
221  * an old style event is also delivered on one of the interfaces (the one
222  * specified by the context data).
223  */
224 void wpa_msg_global(void *ctx, int level, const char *fmt, ...)
225 PRINTF_FORMAT(3, 4);
226
227 /**
228  * wpa_msg_global_ctrl - Conditional global printf for ctrl_iface monitors
229  * @ctx: Pointer to context data; this is the ctx variable registered
230  *      with struct wpa_driver_ops::init()
231  * @level: priority level (MSG_*) of the message
232  * @fmt: printf format string, followed by optional arguments
233  *
234  * This function is used to print conditional debugging and error messages.
235  * This function is like wpa_msg_global(), but it sends the output only to the
236  * attached global ctrl_iface monitors. In other words, it can be used for
237  * frequent events that do not need to be sent to syslog.
238  */
239 void wpa_msg_global_ctrl(void *ctx, int level, const char *fmt, ...)
240 PRINTF_FORMAT(3, 4);
241
242 /**
243  * wpa_msg_no_global - Conditional printf for ctrl_iface monitors
244  * @ctx: Pointer to context data; this is the ctx variable registered
245  *      with struct wpa_driver_ops::init()
246  * @level: priority level (MSG_*) of the message
247  * @fmt: printf format string, followed by optional arguments
248  *
249  * This function is used to print conditional debugging and error messages.
250  * This function is like wpa_msg(), but it does not send the output as a global
251  * event.
252  */
253 void wpa_msg_no_global(void *ctx, int level, const char *fmt, ...)
254 PRINTF_FORMAT(3, 4);
255
256 /**
257  * wpa_msg_global_only - Conditional printf for ctrl_iface monitors
258  * @ctx: Pointer to context data; this is the ctx variable registered
259  *      with struct wpa_driver_ops::init()
260  * @level: priority level (MSG_*) of the message
261  * @fmt: printf format string, followed by optional arguments
262  *
263  * This function is used to print conditional debugging and error messages.
264  * This function is like wpa_msg_global(), but it sends the output only as a
265  * global event.
266  */
267 void wpa_msg_global_only(void *ctx, int level, const char *fmt, ...)
268 PRINTF_FORMAT(3, 4);
269
270 enum wpa_msg_type {
271         WPA_MSG_PER_INTERFACE,
272         WPA_MSG_GLOBAL,
273         WPA_MSG_NO_GLOBAL,
274         WPA_MSG_ONLY_GLOBAL,
275 };
276
277 typedef void (*wpa_msg_cb_func)(void *ctx, int level, enum wpa_msg_type type,
278                                 const char *txt, size_t len);
279
280 /**
281  * wpa_msg_register_cb - Register callback function for wpa_msg() messages
282  * @func: Callback function (%NULL to unregister)
283  */
284 void wpa_msg_register_cb(wpa_msg_cb_func func);
285
286 typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
287 void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
288
289 #endif /* CONFIG_NO_WPA_MSG */
290
291 #ifdef CONFIG_NO_HOSTAPD_LOGGER
292 #define hostapd_logger(args...) do { } while (0)
293 #define hostapd_logger_register_cb(f) do { } while (0)
294 #else /* CONFIG_NO_HOSTAPD_LOGGER */
295 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
296                     const char *fmt, ...) PRINTF_FORMAT(5, 6);
297
298 typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
299                                        unsigned int module, int level,
300                                        const char *txt, size_t len);
301
302 /**
303  * hostapd_logger_register_cb - Register callback function for hostapd_logger()
304  * @func: Callback function (%NULL to unregister)
305  */
306 void hostapd_logger_register_cb(hostapd_logger_cb_func func);
307 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
308
309 #define HOSTAPD_MODULE_IEEE80211        0x00000001
310 #define HOSTAPD_MODULE_IEEE8021X        0x00000002
311 #define HOSTAPD_MODULE_RADIUS           0x00000004
312 #define HOSTAPD_MODULE_WPA              0x00000008
313 #define HOSTAPD_MODULE_DRIVER           0x00000010
314 #define HOSTAPD_MODULE_IAPP             0x00000020
315 #define HOSTAPD_MODULE_MLME             0x00000040
316
317 enum hostapd_logger_level {
318         HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
319         HOSTAPD_LEVEL_DEBUG = 1,
320         HOSTAPD_LEVEL_INFO = 2,
321         HOSTAPD_LEVEL_NOTICE = 3,
322         HOSTAPD_LEVEL_WARNING = 4
323 };
324
325
326 #ifdef CONFIG_DEBUG_SYSLOG
327
328 void wpa_debug_open_syslog(void);
329 void wpa_debug_close_syslog(void);
330
331 #else /* CONFIG_DEBUG_SYSLOG */
332
333 static inline void wpa_debug_open_syslog(void)
334 {
335 }
336
337 static inline void wpa_debug_close_syslog(void)
338 {
339 }
340
341 #endif /* CONFIG_DEBUG_SYSLOG */
342
343 #ifdef CONFIG_DEBUG_LINUX_TRACING
344
345 int wpa_debug_open_linux_tracing(void);
346 void wpa_debug_close_linux_tracing(void);
347
348 #else /* CONFIG_DEBUG_LINUX_TRACING */
349
350 static inline int wpa_debug_open_linux_tracing(void)
351 {
352         return 0;
353 }
354
355 static inline void wpa_debug_close_linux_tracing(void)
356 {
357 }
358
359 #endif /* CONFIG_DEBUG_LINUX_TRACING */
360
361
362 #ifdef EAPOL_TEST
363 #define WPA_ASSERT(a)                                                  \
364         do {                                                           \
365                 if (!(a)) {                                            \
366                         printf("WPA_ASSERT FAILED '" #a "' "           \
367                                "%s %s:%d\n",                           \
368                                __FUNCTION__, __FILE__, __LINE__);      \
369                         exit(1);                                       \
370                 }                                                      \
371         } while (0)
372 #else
373 #define WPA_ASSERT(a) do { } while (0)
374 #endif
375
376 const char * debug_level_str(int level);
377 int str_to_debug_level(const char *s);
378
379 #ifdef __cplusplus
380 }
381 #endif
382
383 #endif /* WPA_DEBUG_H */