~ubuntu-branches/ubuntu/hardy/wpasupplicant/hardy

« back to all changes in this revision

Viewing changes to src/utils/wpa_debug.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
#include "includes.h"
16
 
 
17
 
#include "common.h"
18
 
 
19
 
 
20
 
#ifdef CONFIG_DEBUG_FILE
21
 
static FILE *out_file = NULL;
22
 
#endif /* CONFIG_DEBUG_FILE */
23
 
int wpa_debug_use_file = 0;
24
 
int wpa_debug_level = MSG_INFO;
25
 
int wpa_debug_show_keys = 0;
26
 
int wpa_debug_timestamp = 0;
27
 
 
28
 
 
29
 
#ifndef CONFIG_NO_STDOUT_DEBUG
30
 
 
31
 
void wpa_debug_print_timestamp(void)
32
 
{
33
 
        struct os_time tv;
34
 
 
35
 
        if (!wpa_debug_timestamp)
36
 
                return;
37
 
 
38
 
        os_get_time(&tv);
39
 
#ifdef CONFIG_DEBUG_FILE
40
 
        if (out_file) {
41
 
                fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
42
 
                        (unsigned int) tv.usec);
43
 
        } else
44
 
#endif /* CONFIG_DEBUG_FILE */
45
 
        printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
46
 
}
47
 
 
48
 
 
49
 
/**
50
 
 * wpa_printf - conditional printf
51
 
 * @level: priority level (MSG_*) of the message
52
 
 * @fmt: printf format string, followed by optional arguments
53
 
 *
54
 
 * This function is used to print conditional debugging and error messages. The
55
 
 * output may be directed to stdout, stderr, and/or syslog based on
56
 
 * configuration.
57
 
 *
58
 
 * Note: New line '\n' is added to the end of the text when printing to stdout.
59
 
 */
60
 
void wpa_printf(int level, char *fmt, ...)
61
 
{
62
 
        va_list ap;
63
 
 
64
 
        va_start(ap, fmt);
65
 
        if (level >= wpa_debug_level) {
66
 
                wpa_debug_print_timestamp();
67
 
#ifdef CONFIG_DEBUG_FILE
68
 
                if (out_file) {
69
 
                        vfprintf(out_file, fmt, ap);
70
 
                        fprintf(out_file, "\n");
71
 
                } else {
72
 
#endif /* CONFIG_DEBUG_FILE */
73
 
                vprintf(fmt, ap);
74
 
                printf("\n");
75
 
#ifdef CONFIG_DEBUG_FILE
76
 
                }
77
 
#endif /* CONFIG_DEBUG_FILE */
78
 
        }
79
 
        va_end(ap);
80
 
}
81
 
 
82
 
 
83
 
static void _wpa_hexdump(int level, const char *title, const u8 *buf,
84
 
                         size_t len, int show)
85
 
{
86
 
        size_t i;
87
 
        if (level < wpa_debug_level)
88
 
                return;
89
 
        wpa_debug_print_timestamp();
90
 
#ifdef CONFIG_DEBUG_FILE
91
 
        if (out_file) {
92
 
                fprintf(out_file, "%s - hexdump(len=%lu):",
93
 
                        title, (unsigned long) len);
94
 
                if (buf == NULL) {
95
 
                        fprintf(out_file, " [NULL]");
96
 
                } else if (show) {
97
 
                        for (i = 0; i < len; i++)
98
 
                                fprintf(out_file, " %02x", buf[i]);
99
 
                } else {
100
 
                        fprintf(out_file, " [REMOVED]");
101
 
                }
102
 
                fprintf(out_file, "\n");
103
 
        } else {
104
 
#endif /* CONFIG_DEBUG_FILE */
105
 
        printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
106
 
        if (buf == NULL) {
107
 
                printf(" [NULL]");
108
 
        } else if (show) {
109
 
                for (i = 0; i < len; i++)
110
 
                        printf(" %02x", buf[i]);
111
 
        } else {
112
 
                printf(" [REMOVED]");
113
 
        }
114
 
        printf("\n");
115
 
#ifdef CONFIG_DEBUG_FILE
116
 
        }
117
 
#endif /* CONFIG_DEBUG_FILE */
118
 
}
119
 
 
120
 
void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
121
 
{
122
 
        _wpa_hexdump(level, title, buf, len, 1);
123
 
}
124
 
 
125
 
 
126
 
void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
127
 
{
128
 
        _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
129
 
}
130
 
 
131
 
 
132
 
static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
133
 
                               size_t len, int show)
134
 
{
135
 
        size_t i, llen;
136
 
        const u8 *pos = buf;
137
 
        const size_t line_len = 16;
138
 
 
139
 
        if (level < wpa_debug_level)
140
 
                return;
141
 
        wpa_debug_print_timestamp();
142
 
#ifdef CONFIG_DEBUG_FILE
143
 
        if (out_file) {
144
 
                if (!show) {
145
 
                        fprintf(out_file,
146
 
                                "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
147
 
                                title, (unsigned long) len);
148
 
                        return;
149
 
                }
150
 
                if (buf == NULL) {
151
 
                        fprintf(out_file,
152
 
                                "%s - hexdump_ascii(len=%lu): [NULL]\n",
153
 
                                title, (unsigned long) len);
154
 
                        return;
155
 
                }
156
 
                fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
157
 
                        title, (unsigned long) len);
158
 
                while (len) {
159
 
                        llen = len > line_len ? line_len : len;
160
 
                        fprintf(out_file, "    ");
161
 
                        for (i = 0; i < llen; i++)
162
 
                                fprintf(out_file, " %02x", pos[i]);
163
 
                        for (i = llen; i < line_len; i++)
164
 
                                fprintf(out_file, "   ");
165
 
                        fprintf(out_file, "   ");
166
 
                        for (i = 0; i < llen; i++) {
167
 
                                if (isprint(pos[i]))
168
 
                                        fprintf(out_file, "%c", pos[i]);
169
 
                                else
170
 
                                        fprintf(out_file, "_");
171
 
                        }
172
 
                        for (i = llen; i < line_len; i++)
173
 
                                fprintf(out_file, " ");
174
 
                        fprintf(out_file, "\n");
175
 
                        pos += llen;
176
 
                        len -= llen;
177
 
                }
178
 
        } else {
179
 
#endif /* CONFIG_DEBUG_FILE */
180
 
        if (!show) {
181
 
                printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
182
 
                       title, (unsigned long) len);
183
 
                return;
184
 
        }
185
 
        if (buf == NULL) {
186
 
                printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
187
 
                       title, (unsigned long) len);
188
 
                return;
189
 
        }
190
 
        printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
191
 
        while (len) {
192
 
                llen = len > line_len ? line_len : len;
193
 
                printf("    ");
194
 
                for (i = 0; i < llen; i++)
195
 
                        printf(" %02x", pos[i]);
196
 
                for (i = llen; i < line_len; i++)
197
 
                        printf("   ");
198
 
                printf("   ");
199
 
                for (i = 0; i < llen; i++) {
200
 
                        if (isprint(pos[i]))
201
 
                                printf("%c", pos[i]);
202
 
                        else
203
 
                                printf("_");
204
 
                }
205
 
                for (i = llen; i < line_len; i++)
206
 
                        printf(" ");
207
 
                printf("\n");
208
 
                pos += llen;
209
 
                len -= llen;
210
 
        }
211
 
#ifdef CONFIG_DEBUG_FILE
212
 
        }
213
 
#endif /* CONFIG_DEBUG_FILE */
214
 
}
215
 
 
216
 
 
217
 
void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
218
 
{
219
 
        _wpa_hexdump_ascii(level, title, buf, len, 1);
220
 
}
221
 
 
222
 
 
223
 
void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
224
 
                           size_t len)
225
 
{
226
 
        _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
227
 
}
228
 
 
229
 
 
230
 
int wpa_debug_open_file(void)
231
 
{
232
 
#ifdef CONFIG_DEBUG_FILE
233
 
        static int count = 0;
234
 
        char fname[64];
235
 
        if (!wpa_debug_use_file)
236
 
                return 0;
237
 
#ifdef _WIN32
238
 
        os_snprintf(fname, sizeof(fname), "\\Temp\\wpa_supplicant-log-%d.txt",
239
 
                    count++);
240
 
#else /* _WIN32 */
241
 
        os_snprintf(fname, sizeof(fname), "/tmp/wpa_supplicant-log-%d.txt",
242
 
                    count++);
243
 
#endif /* _WIN32 */
244
 
        out_file = fopen(fname, "w");
245
 
        return out_file == NULL ? -1 : 0;
246
 
#else /* CONFIG_DEBUG_FILE */
247
 
        return 0;
248
 
#endif /* CONFIG_DEBUG_FILE */
249
 
}
250
 
 
251
 
 
252
 
void wpa_debug_close_file(void)
253
 
{
254
 
#ifdef CONFIG_DEBUG_FILE
255
 
        if (!wpa_debug_use_file)
256
 
                return;
257
 
        fclose(out_file);
258
 
        out_file = NULL;
259
 
#endif /* CONFIG_DEBUG_FILE */
260
 
}
261
 
 
262
 
#endif /* CONFIG_NO_STDOUT_DEBUG */
263
 
 
264
 
 
265
 
#ifndef CONFIG_NO_WPA_MSG
266
 
static wpa_msg_cb_func wpa_msg_cb = NULL;
267
 
 
268
 
void wpa_msg_register_cb(wpa_msg_cb_func func)
269
 
{
270
 
        wpa_msg_cb = func;
271
 
}
272
 
 
273
 
 
274
 
void wpa_msg(void *ctx, int level, char *fmt, ...)
275
 
{
276
 
        va_list ap;
277
 
        char *buf;
278
 
        const int buflen = 2048;
279
 
        int len;
280
 
 
281
 
        buf = os_malloc(buflen);
282
 
        if (buf == NULL) {
283
 
                wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
284
 
                           "buffer");
285
 
                return;
286
 
        }
287
 
        va_start(ap, fmt);
288
 
        len = vsnprintf(buf, buflen, fmt, ap);
289
 
        va_end(ap);
290
 
        wpa_printf(level, "%s", buf);
291
 
        if (wpa_msg_cb)
292
 
                wpa_msg_cb(ctx, level, buf, len);
293
 
        os_free(buf);
294
 
}
295
 
#endif /* CONFIG_NO_WPA_MSG */
296
 
 
297
 
 
298
 
#ifndef CONFIG_NO_HOSTAPD_LOGGER
299
 
static hostapd_logger_cb_func hostapd_logger_cb = NULL;
300
 
 
301
 
void hostapd_logger_register_cb(hostapd_logger_cb_func func)
302
 
{
303
 
        hostapd_logger_cb = func;
304
 
}
305
 
 
306
 
 
307
 
void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
308
 
                    const char *fmt, ...)
309
 
{
310
 
        va_list ap;
311
 
        char *buf;
312
 
        const int buflen = 2048;
313
 
        int len;
314
 
 
315
 
        buf = os_malloc(buflen);
316
 
        if (buf == NULL) {
317
 
                wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
318
 
                           "message buffer");
319
 
                return;
320
 
        }
321
 
        va_start(ap, fmt);
322
 
        len = vsnprintf(buf, buflen, fmt, ap);
323
 
        va_end(ap);
324
 
        if (hostapd_logger_cb)
325
 
                hostapd_logger_cb(ctx, addr, module, level, buf, len);
326
 
        else
327
 
                wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
328
 
        os_free(buf);
329
 
}
330
 
#endif /* CONFIG_NO_HOSTAPD_LOGGER */