wpa_debug.c

Go to the documentation of this file.
00001 
00016 #include "includes.h"
00017 
00018 #include "common.h"
00019 
00020 #ifdef CONFIG_DEBUG_SYSLOG
00021 #include <syslog.h>
00022 
00023 static int wpa_debug_syslog = 0;
00024 #endif /* CONFIG_DEBUG_SYSLOG */
00025 
00026 
00027 #ifdef CONFIG_DEBUG_FILE
00028 static FILE *out_file = NULL;
00029 #endif /* CONFIG_DEBUG_FILE */
00030 int wpa_debug_level = MSG_INFO;
00031 int wpa_debug_show_keys = 0;
00032 int wpa_debug_timestamp = 0;
00033 
00034 
00035 #ifndef CONFIG_NO_STDOUT_DEBUG
00036 
00037 void wpa_debug_print_timestamp(void)
00038 {
00039         struct os_time tv;
00040 
00041         if (!wpa_debug_timestamp)
00042                 return;
00043 
00044         os_get_time(&tv);
00045 #ifdef CONFIG_DEBUG_FILE
00046         if (out_file) {
00047                 fprintf(out_file, "%ld.%06u: ", (long) tv.sec,
00048                         (unsigned int) tv.usec);
00049         } else
00050 #endif /* CONFIG_DEBUG_FILE */
00051         printf("%ld.%06u: ", (long) tv.sec, (unsigned int) tv.usec);
00052 }
00053 
00054 
00055 #ifdef CONFIG_DEBUG_SYSLOG
00056 void wpa_debug_open_syslog(void)
00057 {
00058         openlog("wpa_supplicant", LOG_PID | LOG_NDELAY, LOG_DAEMON);
00059         wpa_debug_syslog++;
00060 }
00061 
00062 
00063 void wpa_debug_close_syslog(void)
00064 {
00065         if (wpa_debug_syslog)
00066                 closelog();
00067 }
00068 
00069 
00070 static int syslog_priority(int level)
00071 {
00072         switch (level) {
00073         case MSG_MSGDUMP:
00074         case MSG_DEBUG:
00075                 return LOG_DEBUG;
00076         case MSG_INFO:
00077                 return LOG_NOTICE;
00078         case MSG_WARNING:
00079                 return LOG_WARNING;
00080         case MSG_ERROR:
00081                 return LOG_ERR;
00082         }
00083         return LOG_INFO;
00084 }
00085 #endif /* CONFIG_DEBUG_SYSLOG */
00086 
00087 
00100 void wpa_printf(int level, char *fmt, ...)
00101 {
00102         va_list ap;
00103 
00104         va_start(ap, fmt);
00105         if (level >= wpa_debug_level) {
00106 #ifdef CONFIG_DEBUG_SYSLOG
00107                 if (wpa_debug_syslog) {
00108                         vsyslog(syslog_priority(level), fmt, ap);
00109                 } else {
00110 #endif /* CONFIG_DEBUG_SYSLOG */
00111                 wpa_debug_print_timestamp();
00112 #ifdef CONFIG_DEBUG_FILE
00113                 if (out_file) {
00114                         vfprintf(out_file, fmt, ap);
00115                         fprintf(out_file, "\n");
00116                 } else {
00117 #endif /* CONFIG_DEBUG_FILE */
00118                 vprintf(fmt, ap);
00119                 printf("\n");
00120 #ifdef CONFIG_DEBUG_FILE
00121                 }
00122 #endif /* CONFIG_DEBUG_FILE */
00123 #ifdef CONFIG_DEBUG_SYSLOG
00124                 }
00125 #endif /* CONFIG_DEBUG_SYSLOG */
00126         }
00127         va_end(ap);
00128 }
00129 
00130 
00131 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
00132                          size_t len, int show)
00133 {
00134         size_t i;
00135         if (level < wpa_debug_level)
00136                 return;
00137         wpa_debug_print_timestamp();
00138 #ifdef CONFIG_DEBUG_FILE
00139         if (out_file) {
00140                 fprintf(out_file, "%s - hexdump(len=%lu):",
00141                         title, (unsigned long) len);
00142                 if (buf == NULL) {
00143                         fprintf(out_file, " [NULL]");
00144                 } else if (show) {
00145                         for (i = 0; i < len; i++)
00146                                 fprintf(out_file, " %02x", buf[i]);
00147                 } else {
00148                         fprintf(out_file, " [REMOVED]");
00149                 }
00150                 fprintf(out_file, "\n");
00151         } else {
00152 #endif /* CONFIG_DEBUG_FILE */
00153         printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
00154         if (buf == NULL) {
00155                 printf(" [NULL]");
00156         } else if (show) {
00157                 for (i = 0; i < len; i++)
00158                         printf(" %02x", buf[i]);
00159         } else {
00160                 printf(" [REMOVED]");
00161         }
00162         printf("\n");
00163 #ifdef CONFIG_DEBUG_FILE
00164         }
00165 #endif /* CONFIG_DEBUG_FILE */
00166 }
00167 
00168 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
00169 {
00170         _wpa_hexdump(level, title, buf, len, 1);
00171 }
00172 
00173 
00174 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
00175 {
00176         _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
00177 }
00178 
00179 
00180 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
00181                                size_t len, int show)
00182 {
00183         size_t i, llen;
00184         const u8 *pos = buf;
00185         const size_t line_len = 16;
00186 
00187         if (level < wpa_debug_level)
00188                 return;
00189         wpa_debug_print_timestamp();
00190 #ifdef CONFIG_DEBUG_FILE
00191         if (out_file) {
00192                 if (!show) {
00193                         fprintf(out_file,
00194                                 "%s - hexdump_ascii(len=%lu): [REMOVED]\n",
00195                                 title, (unsigned long) len);
00196                         return;
00197                 }
00198                 if (buf == NULL) {
00199                         fprintf(out_file,
00200                                 "%s - hexdump_ascii(len=%lu): [NULL]\n",
00201                                 title, (unsigned long) len);
00202                         return;
00203                 }
00204                 fprintf(out_file, "%s - hexdump_ascii(len=%lu):\n",
00205                         title, (unsigned long) len);
00206                 while (len) {
00207                         llen = len > line_len ? line_len : len;
00208                         fprintf(out_file, "    ");
00209                         for (i = 0; i < llen; i++)
00210                                 fprintf(out_file, " %02x", pos[i]);
00211                         for (i = llen; i < line_len; i++)
00212                                 fprintf(out_file, "   ");
00213                         fprintf(out_file, "   ");
00214                         for (i = 0; i < llen; i++) {
00215                                 if (isprint(pos[i]))
00216                                         fprintf(out_file, "%c", pos[i]);
00217                                 else
00218                                         fprintf(out_file, "_");
00219                         }
00220                         for (i = llen; i < line_len; i++)
00221                                 fprintf(out_file, " ");
00222                         fprintf(out_file, "\n");
00223                         pos += llen;
00224                         len -= llen;
00225                 }
00226         } else {
00227 #endif /* CONFIG_DEBUG_FILE */
00228         if (!show) {
00229                 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
00230                        title, (unsigned long) len);
00231                 return;
00232         }
00233         if (buf == NULL) {
00234                 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
00235                        title, (unsigned long) len);
00236                 return;
00237         }
00238         printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
00239         while (len) {
00240                 llen = len > line_len ? line_len : len;
00241                 printf("    ");
00242                 for (i = 0; i < llen; i++)
00243                         printf(" %02x", pos[i]);
00244                 for (i = llen; i < line_len; i++)
00245                         printf("   ");
00246                 printf("   ");
00247                 for (i = 0; i < llen; i++) {
00248                         if (isprint(pos[i]))
00249                                 printf("%c", pos[i]);
00250                         else
00251                                 printf("_");
00252                 }
00253                 for (i = llen; i < line_len; i++)
00254                         printf(" ");
00255                 printf("\n");
00256                 pos += llen;
00257                 len -= llen;
00258         }
00259 #ifdef CONFIG_DEBUG_FILE
00260         }
00261 #endif /* CONFIG_DEBUG_FILE */
00262 }
00263 
00264 
00265 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
00266 {
00267         _wpa_hexdump_ascii(level, title, buf, len, 1);
00268 }
00269 
00270 
00271 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
00272                            size_t len)
00273 {
00274         _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
00275 }
00276 
00277 
00278 int wpa_debug_open_file(const char *path)
00279 {
00280 #ifdef CONFIG_DEBUG_FILE
00281         if (!path)
00282                 return 0;
00283         out_file = fopen(path, "a");
00284         if (out_file == NULL) {
00285                 wpa_printf(MSG_ERROR, "wpa_debug_open_file: Failed to open "
00286                            "output file, using standard output");
00287                 return -1;
00288         }
00289 #ifndef _WIN32
00290         setvbuf(out_file, NULL, _IOLBF, 0);
00291 #endif /* _WIN32 */
00292 #endif /* CONFIG_DEBUG_FILE */
00293         return 0;
00294 }
00295 
00296 
00297 void wpa_debug_close_file(void)
00298 {
00299 #ifdef CONFIG_DEBUG_FILE
00300         if (!out_file)
00301                 return;
00302         fclose(out_file);
00303         out_file = NULL;
00304 #endif /* CONFIG_DEBUG_FILE */
00305 }
00306 
00307 #endif /* CONFIG_NO_STDOUT_DEBUG */
00308 
00309 
00310 #ifndef CONFIG_NO_WPA_MSG
00311 static wpa_msg_cb_func wpa_msg_cb = NULL;
00312 
00313 void wpa_msg_register_cb(wpa_msg_cb_func func)
00314 {
00315         wpa_msg_cb = func;
00316 }
00317 
00318 
00319 void wpa_msg(void *ctx, int level, char *fmt, ...)
00320 {
00321         va_list ap;
00322         char *buf;
00323         const int buflen = 2048;
00324         int len;
00325 
00326         buf = os_malloc(buflen);
00327         if (buf == NULL) {
00328                 wpa_printf(MSG_ERROR, "wpa_msg: Failed to allocate message "
00329                            "buffer");
00330                 return;
00331         }
00332         va_start(ap, fmt);
00333         len = vsnprintf(buf, buflen, fmt, ap);
00334         va_end(ap);
00335         wpa_printf(level, "%s", buf);
00336         if (wpa_msg_cb)
00337                 wpa_msg_cb(ctx, level, buf, len);
00338         os_free(buf);
00339 }
00340 
00341 
00342 void wpa_msg_ctrl(void *ctx, int level, char *fmt, ...)
00343 {
00344         va_list ap;
00345         char *buf;
00346         const int buflen = 2048;
00347         int len;
00348 
00349         if (!wpa_msg_cb)
00350                 return;
00351 
00352         buf = os_malloc(buflen);
00353         if (buf == NULL) {
00354                 wpa_printf(MSG_ERROR, "wpa_msg_ctrl: Failed to allocate "
00355                            "message buffer");
00356                 return;
00357         }
00358         va_start(ap, fmt);
00359         len = vsnprintf(buf, buflen, fmt, ap);
00360         va_end(ap);
00361         wpa_msg_cb(ctx, level, buf, len);
00362         os_free(buf);
00363 }
00364 #endif /* CONFIG_NO_WPA_MSG */
00365 
00366 
00367 #ifndef CONFIG_NO_HOSTAPD_LOGGER
00368 static hostapd_logger_cb_func hostapd_logger_cb = NULL;
00369 
00370 void hostapd_logger_register_cb(hostapd_logger_cb_func func)
00371 {
00372         hostapd_logger_cb = func;
00373 }
00374 
00375 
00376 void hostapd_logger(void *ctx, const u8 *addr, unsigned int module, int level,
00377                     const char *fmt, ...)
00378 {
00379         va_list ap;
00380         char *buf;
00381         const int buflen = 2048;
00382         int len;
00383 
00384         buf = os_malloc(buflen);
00385         if (buf == NULL) {
00386                 wpa_printf(MSG_ERROR, "hostapd_logger: Failed to allocate "
00387                            "message buffer");
00388                 return;
00389         }
00390         va_start(ap, fmt);
00391         len = vsnprintf(buf, buflen, fmt, ap);
00392         va_end(ap);
00393         if (hostapd_logger_cb)
00394                 hostapd_logger_cb(ctx, addr, module, level, buf, len);
00395         else
00396                 wpa_printf(MSG_DEBUG, "hostapd_logger: %s", buf);
00397         os_free(buf);
00398 }
00399 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
00400 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 21 23:16:54 2009 for hostapd by  doxygen 1.6.1