9dace70f629686e8c261eed0e745477f7d201698
[libeap.git] / src / utils / wpa_debug.h
1 /*
2  * wpa_supplicant/hostapd / Debug prints
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #ifndef WPA_DEBUG_H
16 #define WPA_DEBUG_H
17
18 #include "wpabuf.h"
19
20 /* Debugging function - conditional printf and hex dump. Driver wrappers can
21  * use these for debugging purposes. */
22
23 enum {
24         MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
25 };
26
27 #ifdef CONFIG_NO_STDOUT_DEBUG
28
29 #define wpa_debug_print_timestamp() do { } while (0)
30 #define wpa_printf(args...) do { } while (0)
31 #define wpa_hexdump(l,t,b,le) do { } while (0)
32 #define wpa_hexdump_buf(l,t,b) do { } while (0)
33 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
34 #define wpa_hexdump_buf_key(l,t,b) do { } while (0)
35 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
36 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
37 #define wpa_debug_open_file(p) do { } while (0)
38 #define wpa_debug_close_file() do { } while (0)
39
40 #else /* CONFIG_NO_STDOUT_DEBUG */
41
42 int wpa_debug_open_file(const char *path);
43 void wpa_debug_close_file(void);
44
45 /**
46  * wpa_debug_printf_timestamp - Print timestamp for debug output
47  *
48  * This function prints a timestamp in seconds_from_1970.microsoconds
49  * format if debug output has been configured to include timestamps in debug
50  * messages.
51  */
52 void wpa_debug_print_timestamp(void);
53
54 /**
55  * wpa_printf - conditional printf
56  * @level: priority level (MSG_*) of the message
57  * @fmt: printf format string, followed by optional arguments
58  *
59  * This function is used to print conditional debugging and error messages. The
60  * output may be directed to stdout, stderr, and/or syslog based on
61  * configuration.
62  *
63  * Note: New line '\n' is added to the end of the text when printing to stdout.
64  */
65 void wpa_printf(int level, const char *fmt, ...)
66 PRINTF_FORMAT(2, 3);
67
68 /**
69  * wpa_hexdump - conditional hex dump
70  * @level: priority level (MSG_*) of the message
71  * @title: title of for the message
72  * @buf: data buffer to be dumped
73  * @len: length of the buf
74  *
75  * This function is used to print conditional debugging and error messages. The
76  * output may be directed to stdout, stderr, and/or syslog based on
77  * configuration. The contents of buf is printed out has hex dump.
78  */
79 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
80
81 static inline void wpa_hexdump_buf(int level, const char *title,
82                                    const struct wpabuf *buf)
83 {
84         wpa_hexdump(level, title, wpabuf_head(buf), wpabuf_len(buf));
85 }
86
87 /**
88  * wpa_hexdump_key - conditional hex dump, hide keys
89  * @level: priority level (MSG_*) of the message
90  * @title: title of for the message
91  * @buf: data buffer to be dumped
92  * @len: length of the buf
93  *
94  * This function is used to print conditional debugging and error messages. The
95  * output may be directed to stdout, stderr, and/or syslog based on
96  * configuration. The contents of buf is printed out has hex dump. This works
97  * like wpa_hexdump(), but by default, does not include secret keys (passwords,
98  * etc.) in debug output.
99  */
100 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
101
102 static inline void wpa_hexdump_buf_key(int level, const char *title,
103                                        const struct wpabuf *buf)
104 {
105         wpa_hexdump_key(level, title, wpabuf_head(buf), wpabuf_len(buf));
106 }
107
108 /**
109  * wpa_hexdump_ascii - conditional hex dump
110  * @level: priority level (MSG_*) of the message
111  * @title: title of for the message
112  * @buf: data buffer to be dumped
113  * @len: length of the buf
114  *
115  * This function is used to print conditional debugging and error messages. The
116  * output may be directed to stdout, stderr, and/or syslog based on
117  * configuration. The contents of buf is printed out has hex dump with both
118  * the hex numbers and ASCII characters (for printable range) are shown. 16
119  * bytes per line will be shown.
120  */
121 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
122                        size_t len);
123
124 /**
125  * wpa_hexdump_ascii_key - conditional hex dump, hide keys
126  * @level: priority level (MSG_*) of the message
127  * @title: title of for the message
128  * @buf: data buffer to be dumped
129  * @len: length of the buf
130  *
131  * This function is used to print conditional debugging and error messages. The
132  * output may be directed to stdout, stderr, and/or syslog based on
133  * configuration. The contents of buf is printed out has hex dump with both
134  * the hex numbers and ASCII characters (for printable range) are shown. 16
135  * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
136  * default, does not include secret keys (passwords, etc.) in debug output.
137  */
138 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
139                            size_t len);
140
141 #endif /* CONFIG_NO_STDOUT_DEBUG */
142
143
144 #ifdef CONFIG_NO_WPA_MSG
145 #define wpa_msg(args...) do { } while (0)
146 #define wpa_msg_ctrl(args...) do { } while (0)
147 #define wpa_msg_register_cb(f) do { } while (0)
148 #else /* CONFIG_NO_WPA_MSG */
149 /**
150  * wpa_msg - Conditional printf for default target and ctrl_iface monitors
151  * @ctx: Pointer to context data; this is the ctx variable registered
152  *      with struct wpa_driver_ops::init()
153  * @level: priority level (MSG_*) of the message
154  * @fmt: printf format string, followed by optional arguments
155  *
156  * This function is used to print conditional debugging and error messages. The
157  * output may be directed to stdout, stderr, and/or syslog based on
158  * configuration. This function is like wpa_printf(), but it also sends the
159  * same message to all attached ctrl_iface monitors.
160  *
161  * Note: New line '\n' is added to the end of the text when printing to stdout.
162  */
163 void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
164
165 /**
166  * wpa_msg_ctrl - Conditional printf for ctrl_iface monitors
167  * @ctx: Pointer to context data; this is the ctx variable registered
168  *      with struct wpa_driver_ops::init()
169  * @level: priority level (MSG_*) of the message
170  * @fmt: printf format string, followed by optional arguments
171  *
172  * This function is used to print conditional debugging and error messages.
173  * This function is like wpa_msg(), but it sends the output only to the
174  * attached ctrl_iface monitors. In other words, it can be used for frequent
175  * events that do not need to be sent to syslog.
176  */
177 void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
178 PRINTF_FORMAT(3, 4);
179
180 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
181                                 size_t len);
182
183 /**
184  * wpa_msg_register_cb - Register callback function for wpa_msg() messages
185  * @func: Callback function (%NULL to unregister)
186  */
187 void wpa_msg_register_cb(wpa_msg_cb_func func);
188 #endif /* CONFIG_NO_WPA_MSG */
189
190
191 #ifdef CONFIG_NO_HOSTAPD_LOGGER
192 #define hostapd_logger(args...) do { } while (0)
193 #define hostapd_logger_register_cb(f) do { } while (0)
194 #else /* CONFIG_NO_HOSTAPD_LOGGER */
195 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
196                     const char *fmt, ...) PRINTF_FORMAT(5, 6);
197
198 typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
199                                        unsigned int module, int level,
200                                        const char *txt, size_t len);
201
202 /**
203  * hostapd_logger_register_cb - Register callback function for hostapd_logger()
204  * @func: Callback function (%NULL to unregister)
205  */
206 void hostapd_logger_register_cb(hostapd_logger_cb_func func);
207 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
208
209 #define HOSTAPD_MODULE_IEEE80211        0x00000001
210 #define HOSTAPD_MODULE_IEEE8021X        0x00000002
211 #define HOSTAPD_MODULE_RADIUS           0x00000004
212 #define HOSTAPD_MODULE_WPA              0x00000008
213 #define HOSTAPD_MODULE_DRIVER           0x00000010
214 #define HOSTAPD_MODULE_IAPP             0x00000020
215 #define HOSTAPD_MODULE_MLME             0x00000040
216
217 enum hostapd_logger_level {
218         HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
219         HOSTAPD_LEVEL_DEBUG = 1,
220         HOSTAPD_LEVEL_INFO = 2,
221         HOSTAPD_LEVEL_NOTICE = 3,
222         HOSTAPD_LEVEL_WARNING = 4
223 };
224
225
226 #ifdef CONFIG_DEBUG_SYSLOG
227
228 void wpa_debug_open_syslog(void);
229 void wpa_debug_close_syslog(void);
230
231 #else /* CONFIG_DEBUG_SYSLOG */
232
233 static inline void wpa_debug_open_syslog(void)
234 {
235 }
236
237 static inline void wpa_debug_close_syslog(void)
238 {
239 }
240
241 #endif /* CONFIG_DEBUG_SYSLOG */
242
243
244 #ifdef EAPOL_TEST
245 #define WPA_ASSERT(a)                                                  \
246         do {                                                           \
247                 if (!(a)) {                                            \
248                         printf("WPA_ASSERT FAILED '" #a "' "           \
249                                "%s %s:%d\n",                           \
250                                __FUNCTION__, __FILE__, __LINE__);      \
251                         exit(1);                                       \
252                 }                                                      \
253         } while (0)
254 #else
255 #define WPA_ASSERT(a) do { } while (0)
256 #endif
257
258 #endif /* WPA_DEBUG_H */