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