wpa_debug.h revision 8d520ff1dc2da35cdca849e982051b86468016d8
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
23enum {
24	MSG_EXCESSIVE, MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR
25};
26
27#ifdef CONFIG_ANDROID_LOG
28
29#define wpa_debug_print_timestamp() do {} while (0)
30#define wpa_hexdump(...)            do {} while (0)
31#define wpa_hexdump_key(...)        do {} while (0)
32#define wpa_hexdump_buf(l,t,b)      do {} while (0)
33#define wpa_hexdump_buf_key(l,t,b)  do {} while (0)
34#define wpa_hexdump_ascii(...)      do {} while (0)
35#define wpa_hexdump_ascii_key(...)  do {} while (0)
36#define wpa_debug_open_file(...)    do {} while (0)
37#define wpa_debug_close_file()      do {} while (0)
38#define wpa_dbg(...)                do {} while (0)
39
40static inline int wpa_debug_reopen_file(void)
41{
42	return 0;
43}
44
45
46void android_printf(int level, char *format, ...)
47PRINTF_FORMAT(2, 3);
48
49#define wpa_printf android_printf
50
51#else /* CONFIG_ANDROID_LOG */
52
53#ifdef CONFIG_NO_STDOUT_DEBUG
54
55#define wpa_debug_print_timestamp() do { } while (0)
56#define wpa_printf(args...) do { } while (0)
57#define wpa_hexdump(l,t,b,le) do { } while (0)
58#define wpa_hexdump_buf(l,t,b) do { } while (0)
59#define wpa_hexdump_key(l,t,b,le) do { } while (0)
60#define wpa_hexdump_buf_key(l,t,b) do { } while (0)
61#define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
62#define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
63#define wpa_debug_open_file(p) do { } while (0)
64#define wpa_debug_close_file() do { } while (0)
65#define wpa_dbg(args...) do { } while (0)
66
67static inline int wpa_debug_reopen_file(void)
68{
69	return 0;
70}
71
72#else /* CONFIG_NO_STDOUT_DEBUG */
73
74int wpa_debug_open_file(const char *path);
75int wpa_debug_reopen_file(void);
76void wpa_debug_close_file(void);
77
78/**
79 * wpa_debug_printf_timestamp - Print timestamp for debug output
80 *
81 * This function prints a timestamp in seconds_from_1970.microsoconds
82 * format if debug output has been configured to include timestamps in debug
83 * messages.
84 */
85void wpa_debug_print_timestamp(void);
86
87/**
88 * wpa_printf - conditional printf
89 * @level: priority level (MSG_*) of the message
90 * @fmt: printf format string, followed by optional arguments
91 *
92 * This function is used to print conditional debugging and error messages. The
93 * output may be directed to stdout, stderr, and/or syslog based on
94 * configuration.
95 *
96 * Note: New line '\n' is added to the end of the text when printing to stdout.
97 */
98void wpa_printf(int level, const char *fmt, ...)
99PRINTF_FORMAT(2, 3);
100
101/**
102 * wpa_hexdump - conditional hex dump
103 * @level: priority level (MSG_*) of the message
104 * @title: title of for the message
105 * @buf: data buffer to be dumped
106 * @len: length of the buf
107 *
108 * This function is used to print conditional debugging and error messages. The
109 * output may be directed to stdout, stderr, and/or syslog based on
110 * configuration. The contents of buf is printed out has hex dump.
111 */
112void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
113
114static inline void wpa_hexdump_buf(int level, const char *title,
115				   const struct wpabuf *buf)
116{
117	wpa_hexdump(level, title, buf ? wpabuf_head(buf) : NULL,
118		    buf ? wpabuf_len(buf) : 0);
119}
120
121/**
122 * wpa_hexdump_key - conditional hex dump, hide keys
123 * @level: priority level (MSG_*) of the message
124 * @title: title of for the message
125 * @buf: data buffer to be dumped
126 * @len: length of the buf
127 *
128 * This function is used to print conditional debugging and error messages. The
129 * output may be directed to stdout, stderr, and/or syslog based on
130 * configuration. The contents of buf is printed out has hex dump. This works
131 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
132 * etc.) in debug output.
133 */
134void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
135
136static inline void wpa_hexdump_buf_key(int level, const char *title,
137				       const struct wpabuf *buf)
138{
139	wpa_hexdump_key(level, title, buf ? wpabuf_head(buf) : 0,
140			buf ? wpabuf_len(buf) : 0);
141}
142
143/**
144 * wpa_hexdump_ascii - conditional hex dump
145 * @level: priority level (MSG_*) of the message
146 * @title: title of for the message
147 * @buf: data buffer to be dumped
148 * @len: length of the buf
149 *
150 * This function is used to print conditional debugging and error messages. The
151 * output may be directed to stdout, stderr, and/or syslog based on
152 * configuration. The contents of buf is printed out has hex dump with both
153 * the hex numbers and ASCII characters (for printable range) are shown. 16
154 * bytes per line will be shown.
155 */
156void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
157		       size_t len);
158
159/**
160 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
161 * @level: priority level (MSG_*) of the message
162 * @title: title of for the message
163 * @buf: data buffer to be dumped
164 * @len: length of the buf
165 *
166 * This function is used to print conditional debugging and error messages. The
167 * output may be directed to stdout, stderr, and/or syslog based on
168 * configuration. The contents of buf is printed out has hex dump with both
169 * the hex numbers and ASCII characters (for printable range) are shown. 16
170 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
171 * default, does not include secret keys (passwords, etc.) in debug output.
172 */
173void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
174			   size_t len);
175
176/*
177 * wpa_dbg() behaves like wpa_msg(), but it can be removed from build to reduce
178 * binary size. As such, it should be used with debugging messages that are not
179 * needed in the control interface while wpa_msg() has to be used for anything
180 * that needs to shown to control interface monitors.
181 */
182#define wpa_dbg(args...) wpa_msg(args)
183
184#endif /* CONFIG_NO_STDOUT_DEBUG */
185
186#endif /* CONFIG_ANDROID_LOG */
187
188
189#ifdef CONFIG_NO_WPA_MSG
190#define wpa_msg(args...) do { } while (0)
191#define wpa_msg_ctrl(args...) do { } while (0)
192#define wpa_msg_register_cb(f) do { } while (0)
193#define wpa_msg_register_ifname_cb(f) do { } while (0)
194#else /* CONFIG_NO_WPA_MSG */
195/**
196 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
197 * @ctx: Pointer to context data; this is the ctx variable registered
198 *	with struct wpa_driver_ops::init()
199 * @level: priority level (MSG_*) of the message
200 * @fmt: printf format string, followed by optional arguments
201 *
202 * This function is used to print conditional debugging and error messages. The
203 * output may be directed to stdout, stderr, and/or syslog based on
204 * configuration. This function is like wpa_printf(), but it also sends the
205 * same message to all attached ctrl_iface monitors.
206 *
207 * Note: New line '\n' is added to the end of the text when printing to stdout.
208 */
209void wpa_msg(void *ctx, int level, const char *fmt, ...) PRINTF_FORMAT(3, 4);
210
211/**
212 * wpa_msg_ctrl - Conditional 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 only to the
220 * attached ctrl_iface monitors. In other words, it can be used for frequent
221 * events that do not need to be sent to syslog.
222 */
223void wpa_msg_ctrl(void *ctx, int level, const char *fmt, ...)
224PRINTF_FORMAT(3, 4);
225
226typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
227				size_t len);
228
229/**
230 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
231 * @func: Callback function (%NULL to unregister)
232 */
233void wpa_msg_register_cb(wpa_msg_cb_func func);
234
235typedef const char * (*wpa_msg_get_ifname_func)(void *ctx);
236void wpa_msg_register_ifname_cb(wpa_msg_get_ifname_func func);
237
238#endif /* CONFIG_NO_WPA_MSG */
239
240#ifdef CONFIG_NO_HOSTAPD_LOGGER
241#define hostapd_logger(args...) do { } while (0)
242#define hostapd_logger_register_cb(f) do { } while (0)
243#else /* CONFIG_NO_HOSTAPD_LOGGER */
244void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
245		    const char *fmt, ...) PRINTF_FORMAT(5, 6);
246
247typedef void (*hostapd_logger_cb_func)(void *ctx, const u8 *addr,
248				       unsigned int module, int level,
249				       const char *txt, size_t len);
250
251/**
252 * hostapd_logger_register_cb - Register callback function for hostapd_logger()
253 * @func: Callback function (%NULL to unregister)
254 */
255void hostapd_logger_register_cb(hostapd_logger_cb_func func);
256#endif /* CONFIG_NO_HOSTAPD_LOGGER */
257
258#define HOSTAPD_MODULE_IEEE80211	0x00000001
259#define HOSTAPD_MODULE_IEEE8021X	0x00000002
260#define HOSTAPD_MODULE_RADIUS		0x00000004
261#define HOSTAPD_MODULE_WPA		0x00000008
262#define HOSTAPD_MODULE_DRIVER		0x00000010
263#define HOSTAPD_MODULE_IAPP		0x00000020
264#define HOSTAPD_MODULE_MLME		0x00000040
265
266enum hostapd_logger_level {
267	HOSTAPD_LEVEL_DEBUG_VERBOSE = 0,
268	HOSTAPD_LEVEL_DEBUG = 1,
269	HOSTAPD_LEVEL_INFO = 2,
270	HOSTAPD_LEVEL_NOTICE = 3,
271	HOSTAPD_LEVEL_WARNING = 4
272};
273
274
275#ifdef CONFIG_DEBUG_SYSLOG
276
277void wpa_debug_open_syslog(void);
278void wpa_debug_close_syslog(void);
279
280#else /* CONFIG_DEBUG_SYSLOG */
281
282static inline void wpa_debug_open_syslog(void)
283{
284}
285
286static inline void wpa_debug_close_syslog(void)
287{
288}
289
290#endif /* CONFIG_DEBUG_SYSLOG */
291
292
293#ifdef EAPOL_TEST
294#define WPA_ASSERT(a)						       \
295	do {							       \
296		if (!(a)) {					       \
297			printf("WPA_ASSERT FAILED '" #a "' "	       \
298			       "%s %s:%d\n",			       \
299			       __FUNCTION__, __FILE__, __LINE__);      \
300			exit(1);				       \
301		}						       \
302	} while (0)
303#else
304#define WPA_ASSERT(a) do { } while (0)
305#endif
306
307#endif /* WPA_DEBUG_H */
308