2 * Host AP (software wireless LAN access point) user space daemon for
3 * Host AP kernel driver / common helper functions, etc.
4 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
29 int wpa_debug_level = MSG_INFO;
30 int wpa_debug_show_keys = 0;
31 int wpa_debug_timestamp = 0;
34 int hostapd_get_rand(u8 *buf, size_t len)
36 #ifdef CONFIG_NATIVE_WINDOWS
38 /* FIX: use more secure pseudo random number generator */
39 for (i = 0; i < len; i++) {
43 #else /* CONFIG_NATIVE_WINDOWS */
47 f = fopen("/dev/urandom", "r");
49 printf("Could not open /dev/urandom.\n");
53 rc = fread(buf, 1, len, f);
56 return rc != len ? -1 : 0;
57 #endif /* CONFIG_NATIVE_WINDOWS */
61 void hostapd_hexdump(const char *title, const u8 *buf, size_t len)
64 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
65 for (i = 0; i < len; i++)
66 printf(" %02x", buf[i]);
71 static int hex2num(char c)
73 if (c >= '0' && c <= '9')
75 if (c >= 'a' && c <= 'f')
77 if (c >= 'A' && c <= 'F')
83 static int hex2byte(const char *hex)
96 int hwaddr_aton(const char *txt, u8 *addr)
100 for (i = 0; i < 6; i++) {
109 *addr++ = (a << 4) | b;
110 if (i < 5 && *txt++ != ':')
118 int hexstr2bin(const char *hex, u8 *buf, size_t len)
121 const char *ipos = hex;
124 for (i = 0; i < len; i++) {
135 char * rel2abs_path(const char *rel_path)
137 char *buf = NULL, *cwd, *ret;
138 size_t len = 128, cwd_len, rel_len, ret_len;
140 if (rel_path[0] == '/')
141 return strdup(rel_path);
147 cwd = getcwd(buf, len);
150 if (errno != ERANGE) {
159 cwd_len = strlen(cwd);
160 rel_len = strlen(rel_path);
161 ret_len = cwd_len + 1 + rel_len + 1;
162 ret = malloc(ret_len);
164 memcpy(ret, cwd, cwd_len);
166 memcpy(ret + cwd_len + 1, rel_path, rel_len);
167 ret[ret_len - 1] = '\0';
174 void inc_byte_array(u8 *counter, size_t len)
179 if (counter[pos] != 0)
186 void print_char(char c)
188 if (c >= 32 && c < 127)
195 void fprint_char(FILE *f, char c)
197 if (c >= 32 && c < 127)
200 fprintf(f, "<%02x>", c);
204 static void wpa_debug_print_timestamp(void)
209 if (!wpa_debug_timestamp)
212 gettimeofday(&tv, NULL);
213 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S",
214 localtime((const time_t *) &tv.tv_sec)) <= 0) {
215 snprintf(buf, sizeof(buf), "%u", (int) tv.tv_sec);
217 printf("%s.%06u: ", buf, (unsigned int) tv.tv_usec);
221 void wpa_printf(int level, char *fmt, ...)
226 if (level >= wpa_debug_level) {
227 wpa_debug_print_timestamp();
235 static void _wpa_hexdump(int level, const char *title, const u8 *buf,
236 size_t len, int show)
239 if (level < wpa_debug_level)
241 wpa_debug_print_timestamp();
242 printf("%s - hexdump(len=%lu):", title, (unsigned long) len);
244 for (i = 0; i < len; i++)
245 printf(" %02x", buf[i]);
247 printf(" [REMOVED]");
252 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len)
254 _wpa_hexdump(level, title, buf, len, 1);
258 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len)
260 _wpa_hexdump(level, title, buf, len, wpa_debug_show_keys);
264 static void _wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
265 size_t len, int show)
269 const int line_len = 16;
271 if (level < wpa_debug_level)
273 wpa_debug_print_timestamp();
275 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
276 title, (unsigned long) len);
279 printf("%s - hexdump_ascii(len=%lu):\n", title, (unsigned long) len);
281 llen = len > line_len ? line_len : len;
283 for (i = 0; i < llen; i++)
284 printf(" %02x", pos[i]);
285 for (i = llen; i < line_len; i++)
288 for (i = 0; i < llen; i++) {
290 printf("%c", pos[i]);
294 for (i = llen; i < line_len; i++)
303 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf, size_t len)
305 _wpa_hexdump_ascii(level, title, buf, len, 1);
309 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
312 _wpa_hexdump_ascii(level, title, buf, len, wpa_debug_show_keys);
316 #ifdef CONFIG_NATIVE_WINDOWS
318 #define EPOCHFILETIME (116444736000000000ULL)
320 int gettimeofday(struct timeval *tv, struct timezone *tz)
326 GetSystemTimeAsFileTime(&ft);
327 li.LowPart = ft.dwLowDateTime;
328 li.HighPart = ft.dwHighDateTime;
329 t = (li.QuadPart - EPOCHFILETIME) / 10;
330 tv->tv_sec = (long) (t / 1000000);
331 tv->tv_usec = (long) (t % 1000000);
335 #endif /* CONFIG_NATIVE_WINDOWS */