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.
25 #endif /* __linux__ */
27 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
28 #include <sys/types.h>
29 #include <sys/endian.h>
30 #define __BYTE_ORDER _BYTE_ORDER
31 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
32 #define __BIG_ENDIAN _BIG_ENDIAN
33 #define bswap_16 bswap16
34 #define bswap_32 bswap32
35 #define bswap_64 bswap64
36 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
37 * defined(__DragonFly__) */
39 #ifdef CONFIG_TI_COMPILER
40 #define __BIG_ENDIAN 4321
41 #define __LITTLE_ENDIAN 1234
43 #define __BYTE_ORDER __BIG_ENDIAN
45 #define __BYTE_ORDER __LITTLE_ENDIAN
47 #endif /* CONFIG_TI_COMPILER */
49 #ifdef CONFIG_NATIVE_WINDOWS
52 typedef int socklen_t;
55 #define MSG_DONTWAIT 0 /* not supported */
58 #endif /* CONFIG_NATIVE_WINDOWS */
60 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
63 #define inline __inline
66 static inline unsigned short wpa_swap_16(unsigned short v)
68 return ((v & 0xff) << 8) | (v >> 8);
71 static inline unsigned int wpa_swap_32(unsigned int v)
73 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
74 ((v & 0xff0000) >> 8) | (v >> 24);
77 #define le_to_host16(n) (n)
78 #define host_to_le16(n) (n)
79 #define be_to_host16(n) wpa_swap_16(n)
80 #define host_to_be16(n) wpa_swap_16(n)
81 #define le_to_host32(n) (n)
82 #define be_to_host32(n) wpa_swap_32(n)
83 #define host_to_be32(n) wpa_swap_32(n)
85 #else /* __CYGWIN__ */
88 #ifndef __LITTLE_ENDIAN
90 #define __LITTLE_ENDIAN 1234
91 #define __BIG_ENDIAN 4321
93 #define __BYTE_ORDER __BIG_ENDIAN
95 #endif /* __BIG_ENDIAN */
96 #endif /* __LITTLE_ENDIAN */
97 #endif /* __BYTE_ORDER */
99 #if __BYTE_ORDER == __LITTLE_ENDIAN
100 #define le_to_host16(n) (n)
101 #define host_to_le16(n) (n)
102 #define be_to_host16(n) bswap_16(n)
103 #define host_to_be16(n) bswap_16(n)
104 #define le_to_host32(n) (n)
105 #define be_to_host32(n) bswap_32(n)
106 #define host_to_be32(n) bswap_32(n)
107 #define le_to_host64(n) (n)
108 #define host_to_le64(n) (n)
109 #define be_to_host64(n) bswap_64(n)
110 #define host_to_be64(n) bswap_64(n)
111 #elif __BYTE_ORDER == __BIG_ENDIAN
112 #define le_to_host16(n) bswap_16(n)
113 #define host_to_le16(n) bswap_16(n)
114 #define be_to_host16(n) (n)
115 #define host_to_be16(n) (n)
116 #define le_to_host32(n) bswap_32(n)
117 #define be_to_host32(n) (n)
118 #define host_to_be32(n) (n)
119 #define le_to_host64(n) bswap_64(n)
120 #define host_to_le64(n) bswap_64(n)
121 #define be_to_host64(n) (n)
122 #define host_to_be64(n) (n)
123 #ifndef WORDS_BIGENDIAN
124 #define WORDS_BIGENDIAN
127 #error Could not determine CPU byte order
130 #endif /* __CYGWIN__ */
132 /* Macros for handling unaligned 16-bit variables */
133 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
134 #define WPA_PUT_BE16(a, val) \
136 (a)[0] = ((u16) (val)) >> 8; \
137 (a)[1] = ((u16) (val)) & 0xff; \
140 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
141 #define WPA_PUT_LE16(a, val) \
143 (a)[1] = ((u16) (val)) >> 8; \
144 (a)[0] = ((u16) (val)) & 0xff; \
147 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
149 #define WPA_PUT_BE24(a, val) \
151 (a)[0] = (u8) (((u32) (val)) >> 16); \
152 (a)[1] = (u8) (((u32) (val)) >> 8); \
153 (a)[2] = (u8) (((u32) (val)) & 0xff); \
156 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
157 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
158 #define WPA_PUT_BE32(a, val) \
160 (a)[0] = (u8) (((u32) (val)) >> 24); \
161 (a)[1] = (u8) (((u32) (val)) >> 16); \
162 (a)[2] = (u8) (((u32) (val)) >> 8); \
163 (a)[3] = (u8) (((u32) (val)) & 0xff); \
166 #define WPA_PUT_BE64(a, val) \
168 (a)[0] = (u8) (((u64) (val)) >> 56); \
169 (a)[1] = (u8) (((u64) (val)) >> 48); \
170 (a)[2] = (u8) (((u64) (val)) >> 40); \
171 (a)[3] = (u8) (((u64) (val)) >> 32); \
172 (a)[4] = (u8) (((u64) (val)) >> 24); \
173 (a)[5] = (u8) (((u64) (val)) >> 16); \
174 (a)[6] = (u8) (((u64) (val)) >> 8); \
175 (a)[7] = (u8) (((u64) (val)) & 0xff); \
192 #define WPA_TYPES_DEFINED
193 #endif /* _MSC_VER */
196 typedef unsigned long long u64;
200 typedef long long s64;
204 #define WPA_TYPES_DEFINED
205 #endif /* __vxworks */
207 #ifdef CONFIG_TI_COMPILER
208 #ifdef _LLONG_AVAILABLE
209 typedef unsigned long long u64;
212 * TODO: 64-bit variable not available. Using long as a workaround to test the
213 * build, but this will likely not work for all operations.
215 typedef unsigned long u64;
217 typedef unsigned int u32;
218 typedef unsigned short u16;
219 typedef unsigned char u8;
220 #define WPA_TYPES_DEFINED
221 #endif /* CONFIG_TI_COMPILER */
223 #ifndef WPA_TYPES_DEFINED
224 #ifdef CONFIG_USE_INTTYPES_H
225 #include <inttypes.h>
229 typedef uint64_t u64;
230 typedef uint32_t u32;
231 typedef uint16_t u16;
237 #define WPA_TYPES_DEFINED
238 #endif /* !WPA_TYPES_DEFINED */
240 #define hostapd_get_rand os_get_random
241 int hwaddr_aton(const char *txt, u8 *addr);
242 int hexstr2bin(const char *hex, u8 *buf, size_t len);
243 void inc_byte_array(u8 *counter, size_t len);
244 void wpa_get_ntp_timestamp(u8 *buf);
248 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
249 #define STRUCT_PACKED __attribute__ ((packed))
251 #define PRINTF_FORMAT(a,b)
252 #define STRUCT_PACKED
256 /* Debugging function - conditional printf and hex dump. Driver wrappers can
257 * use these for debugging purposes. */
259 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
261 #ifdef CONFIG_NO_STDOUT_DEBUG
263 #define wpa_debug_print_timestamp() do { } while (0)
264 #define wpa_printf(args...) do { } while (0)
265 #define wpa_hexdump(l,t,b,le) do { } while (0)
266 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
267 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
268 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
269 #define wpa_debug_open_file(p) do { } while (0)
270 #define wpa_debug_close_file() do { } while (0)
272 #else /* CONFIG_NO_STDOUT_DEBUG */
274 int wpa_debug_open_file(const char *path);
275 void wpa_debug_close_file(void);
278 * wpa_debug_printf_timestamp - Print timestamp for debug output
280 * This function prints a timestamp in <seconds from 1970>.<microsoconds>
281 * format if debug output has been configured to include timestamps in debug
284 void wpa_debug_print_timestamp(void);
287 * wpa_printf - conditional printf
288 * @level: priority level (MSG_*) of the message
289 * @fmt: printf format string, followed by optional arguments
291 * This function is used to print conditional debugging and error messages. The
292 * output may be directed to stdout, stderr, and/or syslog based on
295 * Note: New line '\n' is added to the end of the text when printing to stdout.
297 void wpa_printf(int level, char *fmt, ...)
301 * wpa_hexdump - conditional hex dump
302 * @level: priority level (MSG_*) of the message
303 * @title: title of for the message
304 * @buf: data buffer to be dumped
305 * @len: length of the buf
307 * This function is used to print conditional debugging and error messages. The
308 * output may be directed to stdout, stderr, and/or syslog based on
309 * configuration. The contents of buf is printed out has hex dump.
311 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
314 * wpa_hexdump_key - conditional hex dump, hide keys
315 * @level: priority level (MSG_*) of the message
316 * @title: title of for the message
317 * @buf: data buffer to be dumped
318 * @len: length of the buf
320 * This function is used to print conditional debugging and error messages. The
321 * output may be directed to stdout, stderr, and/or syslog based on
322 * configuration. The contents of buf is printed out has hex dump. This works
323 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
324 * etc.) in debug output.
326 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
329 * wpa_hexdump_ascii - conditional hex dump
330 * @level: priority level (MSG_*) of the message
331 * @title: title of for the message
332 * @buf: data buffer to be dumped
333 * @len: length of the buf
335 * This function is used to print conditional debugging and error messages. The
336 * output may be directed to stdout, stderr, and/or syslog based on
337 * configuration. The contents of buf is printed out has hex dump with both
338 * the hex numbers and ASCII characters (for printable range) are shown. 16
339 * bytes per line will be shown.
341 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
345 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
346 * @level: priority level (MSG_*) of the message
347 * @title: title of for the message
348 * @buf: data buffer to be dumped
349 * @len: length of the buf
351 * This function is used to print conditional debugging and error messages. The
352 * output may be directed to stdout, stderr, and/or syslog based on
353 * configuration. The contents of buf is printed out has hex dump with both
354 * the hex numbers and ASCII characters (for printable range) are shown. 16
355 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
356 * default, does not include secret keys (passwords, etc.) in debug output.
358 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
361 #endif /* CONFIG_NO_STDOUT_DEBUG */
364 #ifdef CONFIG_NO_WPA_MSG
365 #define wpa_msg(args...) do { } while (0)
366 #define wpa_msg_register_cb(f) do { } while (0)
367 #else /* CONFIG_NO_WPA_MSG */
369 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
370 * @ctx: Pointer to context data; this is the ctx variable registered
371 * with struct wpa_driver_ops::init()
372 * @level: priority level (MSG_*) of the message
373 * @fmt: printf format string, followed by optional arguments
375 * This function is used to print conditional debugging and error messages. The
376 * output may be directed to stdout, stderr, and/or syslog based on
377 * configuration. This function is like wpa_printf(), but it also sends the
378 * same message to all attached ctrl_iface monitors.
380 * Note: New line '\n' is added to the end of the text when printing to stdout.
382 void wpa_msg(void *ctx, int level, char *fmt, ...) PRINTF_FORMAT(3, 4);
384 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
388 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
389 * @func: Callback function (%NULL to unregister)
391 void wpa_msg_register_cb(wpa_msg_cb_func func);
392 #endif /* CONFIG_NO_WPA_MSG */
395 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
396 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
401 #define WPA_ASSERT(a) \
404 printf("WPA_ASSERT FAILED '" #a "' " \
406 __FUNCTION__, __FILE__, __LINE__); \
411 #define WPA_ASSERT(a) do { } while (0)
417 #define vsnprintf _vsnprintf
419 #define close closesocket
420 #endif /* _MSC_VER */
423 #ifdef CONFIG_ANSI_C_EXTRA
425 #if !defined(_MSC_VER) || _MSC_VER < 1400
426 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
427 * due to possible buffer overflow; see, e.g.,
428 * http://www.ijs.si/software/snprintf/ for portable implementation of
430 int snprintf(char *str, size_t size, const char *format, ...);
432 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
433 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
434 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
436 /* getopt - only used in main.c */
437 int getopt(int argc, char *const argv[], const char *optstring);
441 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
442 #ifndef __socklen_t_defined
443 typedef int socklen_t;
447 /* inline - define as __inline or just define it to be empty, if needed */
448 #ifdef CONFIG_NO_INLINE
451 #define inline __inline
455 #define __func__ "__func__ not defined"
459 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
463 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
464 (((u32) (a) << 8) & 0xff0000) | \
465 (((u32) (a) >> 8) & 0xff00) | \
466 (((u32) (a) >> 24) & 0xff))
470 #define MSG_DONTWAIT 0
474 void perror(const char *s);
475 #endif /* _WIN32_WCE */
477 #endif /* CONFIG_ANSI_C_EXTRA */
479 #define wpa_zalloc(s) os_zalloc((s))
481 #ifdef CONFIG_NATIVE_WINDOWS
482 void wpa_unicode2ascii_inplace(TCHAR *str);
483 TCHAR * wpa_strdup_tchar(const char *str);
484 #else /* CONFIG_NATIVE_WINDOWS */
485 #define wpa_unicode2ascii_inplace(s) do { } while (0)
486 #define wpa_strdup_tchar(s) strdup((s))
487 #endif /* CONFIG_NATIVE_WINDOWS */
489 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len);
494 #endif /* COMMON_H */