2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2006, Jouni Malinen <j@w1.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.
23 #endif /* __linux__ */
25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
26 #include <sys/types.h>
27 #include <sys/endian.h>
28 #define __BYTE_ORDER _BYTE_ORDER
29 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
30 #define __BIG_ENDIAN _BIG_ENDIAN
31 #define bswap_16 bswap16
32 #define bswap_32 bswap32
33 #define bswap_64 bswap64
34 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
35 * defined(__DragonFly__) */
37 #ifdef CONFIG_TI_COMPILER
38 #define __BIG_ENDIAN 4321
39 #define __LITTLE_ENDIAN 1234
41 #define __BYTE_ORDER __BIG_ENDIAN
43 #define __BYTE_ORDER __LITTLE_ENDIAN
45 #endif /* CONFIG_TI_COMPILER */
47 #ifdef CONFIG_NATIVE_WINDOWS
50 typedef int socklen_t;
53 #define MSG_DONTWAIT 0 /* not supported */
56 #endif /* CONFIG_NATIVE_WINDOWS */
58 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
61 #define inline __inline
64 static inline unsigned short wpa_swap_16(unsigned short v)
66 return ((v & 0xff) << 8) | (v >> 8);
69 static inline unsigned int wpa_swap_32(unsigned int v)
71 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
72 ((v & 0xff0000) >> 8) | (v >> 24);
75 #define le_to_host16(n) (n)
76 #define host_to_le16(n) (n)
77 #define be_to_host16(n) wpa_swap_16(n)
78 #define host_to_be16(n) wpa_swap_16(n)
79 #define le_to_host32(n) (n)
80 #define be_to_host32(n) wpa_swap_32(n)
81 #define host_to_be32(n) wpa_swap_32(n)
83 #else /* __CYGWIN__ */
86 #ifndef __LITTLE_ENDIAN
88 #define __LITTLE_ENDIAN 1234
89 #define __BIG_ENDIAN 4321
91 #define __BYTE_ORDER __BIG_ENDIAN
93 #endif /* __BIG_ENDIAN */
94 #endif /* __LITTLE_ENDIAN */
95 #endif /* __BYTE_ORDER */
97 #if __BYTE_ORDER == __LITTLE_ENDIAN
98 #define le_to_host16(n) (n)
99 #define host_to_le16(n) (n)
100 #define be_to_host16(n) bswap_16(n)
101 #define host_to_be16(n) bswap_16(n)
102 #define le_to_host32(n) (n)
103 #define be_to_host32(n) bswap_32(n)
104 #define host_to_be32(n) bswap_32(n)
105 #define le_to_host64(n) (n)
106 #define host_to_le64(n) (n)
107 #define be_to_host64(n) bswap_64(n)
108 #define host_to_be64(n) bswap_64(n)
109 #elif __BYTE_ORDER == __BIG_ENDIAN
110 #define le_to_host16(n) bswap_16(n)
111 #define host_to_le16(n) bswap_16(n)
112 #define be_to_host16(n) (n)
113 #define host_to_be16(n) (n)
114 #define le_to_host32(n) bswap_32(n)
115 #define be_to_host32(n) (n)
116 #define host_to_be32(n) (n)
117 #define le_to_host64(n) bswap_64(n)
118 #define host_to_le64(n) bswap_64(n)
119 #define be_to_host64(n) (n)
120 #define host_to_be64(n) (n)
121 #ifndef WORDS_BIGENDIAN
122 #define WORDS_BIGENDIAN
125 #error Could not determine CPU byte order
128 #endif /* __CYGWIN__ */
130 /* Macros for handling unaligned 16-bit variables */
131 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
132 #define WPA_PUT_BE16(a, val) \
134 (a)[0] = ((u16) (val)) >> 8; \
135 (a)[1] = ((u16) (val)) & 0xff; \
138 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
139 #define WPA_PUT_LE16(a, val) \
141 (a)[1] = ((u16) (val)) >> 8; \
142 (a)[0] = ((u16) (val)) & 0xff; \
145 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
147 #define WPA_PUT_BE24(a, val) \
149 (a)[0] = (u8) (((u32) (val)) >> 16); \
150 (a)[1] = (u8) (((u32) (val)) >> 8); \
151 (a)[2] = (u8) (((u32) (val)) & 0xff); \
154 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
155 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
156 #define WPA_PUT_BE32(a, val) \
158 (a)[0] = (u8) (((u32) (val)) >> 24); \
159 (a)[1] = (u8) (((u32) (val)) >> 16); \
160 (a)[2] = (u8) (((u32) (val)) >> 8); \
161 (a)[3] = (u8) (((u32) (val)) & 0xff); \
164 #define WPA_PUT_BE64(a, val) \
166 (a)[0] = (u8) (((u64) (val)) >> 56); \
167 (a)[1] = (u8) (((u64) (val)) >> 48); \
168 (a)[2] = (u8) (((u64) (val)) >> 40); \
169 (a)[3] = (u8) (((u64) (val)) >> 32); \
170 (a)[4] = (u8) (((u64) (val)) >> 24); \
171 (a)[5] = (u8) (((u64) (val)) >> 16); \
172 (a)[6] = (u8) (((u64) (val)) >> 8); \
173 (a)[7] = (u8) (((u64) (val)) & 0xff); \
190 #define WPA_TYPES_DEFINED
191 #endif /* _MSC_VER */
194 typedef unsigned long long u64;
198 typedef long long s64;
202 #define WPA_TYPES_DEFINED
203 #endif /* __vxworks */
205 #ifdef CONFIG_TI_COMPILER
206 #ifdef _LLONG_AVAILABLE
207 typedef unsigned long long u64;
210 * TODO: 64-bit variable not available. Using long as a workaround to test the
211 * build, but this will likely not work for all operations.
213 typedef unsigned long u64;
215 typedef unsigned int u32;
216 typedef unsigned short u16;
217 typedef unsigned char u8;
218 #define WPA_TYPES_DEFINED
219 #endif /* CONFIG_TI_COMPILER */
221 #ifndef WPA_TYPES_DEFINED
222 #ifdef CONFIG_USE_INTTYPES_H
223 #include <inttypes.h>
227 typedef uint64_t u64;
228 typedef uint32_t u32;
229 typedef uint16_t u16;
235 #define WPA_TYPES_DEFINED
236 #endif /* !WPA_TYPES_DEFINED */
238 #define hostapd_get_rand os_get_random
239 int hwaddr_aton(const char *txt, u8 *addr);
240 int hexstr2bin(const char *hex, u8 *buf, size_t len);
241 void inc_byte_array(u8 *counter, size_t len);
242 void wpa_get_ntp_timestamp(u8 *buf);
246 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
247 #define STRUCT_PACKED __attribute__ ((packed))
249 #define PRINTF_FORMAT(a,b)
250 #define STRUCT_PACKED
254 /* Debugging function - conditional printf and hex dump. Driver wrappers can
255 * use these for debugging purposes. */
257 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
261 #define wpa_debug_print_timestamp() do {} while (0)
262 #define wpa_hexdump(...) do {} while (0)
263 #define wpa_hexdump_key(...) do {} while (0)
264 #define wpa_hexdump_ascii(...) do {} while (0)
265 #define wpa_hexdump_ascii_key(...) do {} while (0)
266 #define wpa_debug_open_file(...) do {} while (0)
267 #define wpa_debug_close_file() do {} while (0)
269 void android_printf(int level, char *format, ...);
271 #define wpa_printf(level, ...) \
273 if ((level) >= MSG_INFO) { \
274 android_printf((level), __VA_ARGS__); \
280 #ifdef CONFIG_NO_STDOUT_DEBUG
282 #define wpa_debug_print_timestamp() do { } while (0)
283 #define wpa_printf(args...) do { } while (0)
284 #define wpa_hexdump(l,t,b,le) do { } while (0)
285 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
286 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
287 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
288 #define wpa_debug_open_file(p) do { } while (0)
289 #define wpa_debug_close_file() do { } while (0)
291 #else /* CONFIG_NO_STDOUT_DEBUG */
293 int wpa_debug_open_file(const char *path);
294 void wpa_debug_close_file(void);
297 * wpa_debug_printf_timestamp - Print timestamp for debug output
299 * This function prints a timestamp in <seconds from 1970>.<microsoconds>
300 * format if debug output has been configured to include timestamps in debug
303 void wpa_debug_print_timestamp(void);
306 * wpa_printf - conditional printf
307 * @level: priority level (MSG_*) of the message
308 * @fmt: printf format string, followed by optional arguments
310 * This function is used to print conditional debugging and error messages. The
311 * output may be directed to stdout, stderr, and/or syslog based on
314 * Note: New line '\n' is added to the end of the text when printing to stdout.
316 void wpa_printf(int level, char *fmt, ...)
320 * wpa_hexdump - conditional hex dump
321 * @level: priority level (MSG_*) of the message
322 * @title: title of for the message
323 * @buf: data buffer to be dumped
324 * @len: length of the buf
326 * This function is used to print conditional debugging and error messages. The
327 * output may be directed to stdout, stderr, and/or syslog based on
328 * configuration. The contents of buf is printed out has hex dump.
330 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
333 * wpa_hexdump_key - conditional hex dump, hide keys
334 * @level: priority level (MSG_*) of the message
335 * @title: title of for the message
336 * @buf: data buffer to be dumped
337 * @len: length of the buf
339 * This function is used to print conditional debugging and error messages. The
340 * output may be directed to stdout, stderr, and/or syslog based on
341 * configuration. The contents of buf is printed out has hex dump. This works
342 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
343 * etc.) in debug output.
345 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
348 * wpa_hexdump_ascii - conditional hex dump
349 * @level: priority level (MSG_*) of the message
350 * @title: title of for the message
351 * @buf: data buffer to be dumped
352 * @len: length of the buf
354 * This function is used to print conditional debugging and error messages. The
355 * output may be directed to stdout, stderr, and/or syslog based on
356 * configuration. The contents of buf is printed out has hex dump with both
357 * the hex numbers and ASCII characters (for printable range) are shown. 16
358 * bytes per line will be shown.
360 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
364 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
365 * @level: priority level (MSG_*) of the message
366 * @title: title of for the message
367 * @buf: data buffer to be dumped
368 * @len: length of the buf
370 * This function is used to print conditional debugging and error messages. The
371 * output may be directed to stdout, stderr, and/or syslog based on
372 * configuration. The contents of buf is printed out has hex dump with both
373 * the hex numbers and ASCII characters (for printable range) are shown. 16
374 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
375 * default, does not include secret keys (passwords, etc.) in debug output.
377 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
380 #endif /* CONFIG_NO_STDOUT_DEBUG */
384 #ifdef CONFIG_NO_WPA_MSG
385 #define wpa_msg(args...) do { } while (0)
386 #define wpa_msg_register_cb(f) do { } while (0)
387 #else /* CONFIG_NO_WPA_MSG */
389 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
390 * @ctx: Pointer to context data; this is the ctx variable registered
391 * with struct wpa_driver_ops::init()
392 * @level: priority level (MSG_*) of the message
393 * @fmt: printf format string, followed by optional arguments
395 * This function is used to print conditional debugging and error messages. The
396 * output may be directed to stdout, stderr, and/or syslog based on
397 * configuration. This function is like wpa_printf(), but it also sends the
398 * same message to all attached ctrl_iface monitors.
400 * Note: New line '\n' is added to the end of the text when printing to stdout.
402 void wpa_msg(void *ctx, int level, char *fmt, ...) PRINTF_FORMAT(3, 4);
404 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
408 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
409 * @func: Callback function (%NULL to unregister)
411 void wpa_msg_register_cb(wpa_msg_cb_func func);
412 #endif /* CONFIG_NO_WPA_MSG */
415 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
416 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
421 #define WPA_ASSERT(a) \
424 printf("WPA_ASSERT FAILED '" #a "' " \
426 __FUNCTION__, __FILE__, __LINE__); \
431 #define WPA_ASSERT(a) do { } while (0)
437 #define vsnprintf _vsnprintf
439 #define close closesocket
440 #endif /* _MSC_VER */
443 #ifdef CONFIG_ANSI_C_EXTRA
445 #if !defined(_MSC_VER) || _MSC_VER < 1400
446 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
447 * due to possible buffer overflow; see, e.g.,
448 * http://www.ijs.si/software/snprintf/ for portable implementation of
450 int snprintf(char *str, size_t size, const char *format, ...);
452 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
453 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
454 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
456 /* getopt - only used in main.c */
457 int getopt(int argc, char *const argv[], const char *optstring);
461 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
462 #ifndef __socklen_t_defined
463 typedef int socklen_t;
467 /* inline - define as __inline or just define it to be empty, if needed */
468 #ifdef CONFIG_NO_INLINE
471 #define inline __inline
475 #define __func__ "__func__ not defined"
479 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
483 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
484 (((u32) (a) << 8) & 0xff0000) | \
485 (((u32) (a) >> 8) & 0xff00) | \
486 (((u32) (a) >> 24) & 0xff))
490 #define MSG_DONTWAIT 0
494 void perror(const char *s);
495 #endif /* _WIN32_WCE */
497 #endif /* CONFIG_ANSI_C_EXTRA */
499 #define wpa_zalloc(s) os_zalloc((s))
501 #ifdef CONFIG_NATIVE_WINDOWS
502 void wpa_unicode2ascii_inplace(TCHAR *str);
503 TCHAR * wpa_strdup_tchar(const char *str);
504 #else /* CONFIG_NATIVE_WINDOWS */
505 #define wpa_unicode2ascii_inplace(s) do { } while (0)
506 #define wpa_strdup_tchar(s) strdup((s))
507 #endif /* CONFIG_NATIVE_WINDOWS */
509 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len);
516 #endif /* COMMON_H */