2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
24 #ifdef CONFIG_NATIVE_WINDOWS
27 #endif /* CONFIG_NATIVE_WINDOWS */
32 int wpa_debug_level
= MSG_INFO
;
33 int wpa_debug_show_keys
= 0;
34 int wpa_debug_timestamp
= 0;
37 int hostapd_get_rand(u8
*buf
, size_t len
)
39 #ifdef CONFIG_NATIVE_WINDOWS
43 if (!CryptAcquireContext(&prov
, NULL
, NULL
, PROV_RSA_FULL
,
47 ret
= CryptGenRandom(prov
, len
, buf
);
48 CryptReleaseContext(prov
, 0);
51 #else /* CONFIG_NATIVE_WINDOWS */
55 f
= fopen("/dev/urandom", "r");
57 printf("Could not open /dev/urandom.\n");
61 rc
= fread(buf
, 1, len
, f
);
64 return rc
!= len
? -1 : 0;
65 #endif /* CONFIG_NATIVE_WINDOWS */
69 void hostapd_hexdump(const char *title
, const u8
*buf
, size_t len
)
72 printf("%s - hexdump(len=%lu):", title
, (unsigned long) len
);
73 for (i
= 0; i
< len
; i
++)
74 printf(" %02x", buf
[i
]);
79 static int hex2num(char c
)
81 if (c
>= '0' && c
<= '9')
83 if (c
>= 'a' && c
<= 'f')
85 if (c
>= 'A' && c
<= 'F')
91 static int hex2byte(const char *hex
)
105 * hwaddr_aton - Convert ASCII string to MAC address
106 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
107 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
108 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
110 int hwaddr_aton(const char *txt
, u8
*addr
)
114 for (i
= 0; i
< 6; i
++) {
123 *addr
++ = (a
<< 4) | b
;
124 if (i
< 5 && *txt
++ != ':')
133 * hexstr2bin - Convert ASCII hex string into binary data
134 * @hex: ASCII hex string (e.g., "01ab")
135 * @buf: Buffer for the binary data
136 * @len: Length of the text to convert in bytes (of buf); hex will be double
138 * Returns: 0 on success, -1 on failure (invalid hex string)
140 int hexstr2bin(const char *hex
, u8
*buf
, size_t len
)
143 const char *ipos
= hex
;
146 for (i
= 0; i
< len
; i
++) {
157 char * rel2abs_path(const char *rel_path
)
159 char *buf
= NULL
, *cwd
, *ret
;
160 size_t len
= 128, cwd_len
, rel_len
, ret_len
;
162 if (rel_path
[0] == '/')
163 return strdup(rel_path
);
169 cwd
= getcwd(buf
, len
);
172 if (errno
!= ERANGE
) {
181 cwd_len
= strlen(cwd
);
182 rel_len
= strlen(rel_path
);
183 ret_len
= cwd_len
+ 1 + rel_len
+ 1;
184 ret
= malloc(ret_len
);
186 memcpy(ret
, cwd
, cwd_len
);
188 memcpy(ret
+ cwd_len
+ 1, rel_path
, rel_len
);
189 ret
[ret_len
- 1] = '\0';
197 * inc_byte_array - Increment arbitrary length byte array by one
198 * @counter: Pointer to byte array
199 * @len: Length of the counter in bytes
201 * This function increments the last byte of the counter by one and continues
202 * rolling over to more significant bytes if the byte was incremented from
205 void inc_byte_array(u8
*counter
, size_t len
)
210 if (counter
[pos
] != 0)
217 void print_char(char c
)
219 if (c
>= 32 && c
< 127)
226 void fprint_char(FILE *f
, char c
)
228 if (c
>= 32 && c
< 127)
231 fprintf(f
, "<%02x>", c
);
235 #ifndef CONFIG_NO_STDOUT_DEBUG
237 void wpa_debug_print_timestamp(void)
242 if (!wpa_debug_timestamp
)
245 gettimeofday(&tv
, NULL
);
246 if (strftime(buf
, sizeof(buf
), "%b %d %H:%M:%S",
247 localtime((const time_t *) &tv
.tv_sec
)) <= 0) {
248 snprintf(buf
, sizeof(buf
), "%u", (int) tv
.tv_sec
);
250 printf("%s.%06u: ", buf
, (unsigned int) tv
.tv_usec
);
255 * wpa_printf - conditional printf
256 * @level: priority level (MSG_*) of the message
257 * @fmt: printf format string, followed by optional arguments
259 * This function is used to print conditional debugging and error messages. The
260 * output may be directed to stdout, stderr, and/or syslog based on
263 * Note: New line '\n' is added to the end of the text when printing to stdout.
265 void wpa_printf(int level
, char *fmt
, ...)
270 if (level
>= wpa_debug_level
) {
271 wpa_debug_print_timestamp();
279 static void _wpa_hexdump(int level
, const char *title
, const u8
*buf
,
280 size_t len
, int show
)
283 if (level
< wpa_debug_level
)
285 wpa_debug_print_timestamp();
286 printf("%s - hexdump(len=%lu):", title
, (unsigned long) len
);
290 for (i
= 0; i
< len
; i
++)
291 printf(" %02x", buf
[i
]);
293 printf(" [REMOVED]");
298 void wpa_hexdump(int level
, const char *title
, const u8
*buf
, size_t len
)
300 _wpa_hexdump(level
, title
, buf
, len
, 1);
304 void wpa_hexdump_key(int level
, const char *title
, const u8
*buf
, size_t len
)
306 _wpa_hexdump(level
, title
, buf
, len
, wpa_debug_show_keys
);
310 static void _wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
,
311 size_t len
, int show
)
315 const int line_len
= 16;
317 if (level
< wpa_debug_level
)
319 wpa_debug_print_timestamp();
321 printf("%s - hexdump_ascii(len=%lu): [REMOVED]\n",
322 title
, (unsigned long) len
);
326 printf("%s - hexdump_ascii(len=%lu): [NULL]\n",
327 title
, (unsigned long) len
);
330 printf("%s - hexdump_ascii(len=%lu):\n", title
, (unsigned long) len
);
332 llen
= len
> line_len
? line_len
: len
;
334 for (i
= 0; i
< llen
; i
++)
335 printf(" %02x", pos
[i
]);
336 for (i
= llen
; i
< line_len
; i
++)
339 for (i
= 0; i
< llen
; i
++) {
341 printf("%c", pos
[i
]);
345 for (i
= llen
; i
< line_len
; i
++)
354 void wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
, size_t len
)
356 _wpa_hexdump_ascii(level
, title
, buf
, len
, 1);
360 void wpa_hexdump_ascii_key(int level
, const char *title
, const u8
*buf
,
363 _wpa_hexdump_ascii(level
, title
, buf
, len
, wpa_debug_show_keys
);
366 #endif /* CONFIG_NO_STDOUT_DEBUG */
369 #ifdef CONFIG_NATIVE_WINDOWS
371 #define EPOCHFILETIME (116444736000000000ULL)
373 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
379 GetSystemTimeAsFileTime(&ft
);
380 li
.LowPart
= ft
.dwLowDateTime
;
381 li
.HighPart
= ft
.dwHighDateTime
;
382 t
= (li
.QuadPart
- EPOCHFILETIME
) / 10;
383 tv
->tv_sec
= (long) (t
/ 1000000);
384 tv
->tv_usec
= (long) (t
% 1000000);
388 #endif /* CONFIG_NATIVE_WINDOWS */