Fix some mistakes and add some missing entries.
[dragonfly.git] / contrib / wpa_supplicant-0.5.8 / common.h
blobb200b580d5df3de971338843789857b58c5b5354
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #ifndef COMMON_H
16 #define COMMON_H
18 #include "os.h"
20 #ifdef __linux__
21 #include <endian.h>
22 #include <byteswap.h>
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
40 #ifdef __big_endian__
41 #define __BYTE_ORDER __BIG_ENDIAN
42 #else
43 #define __BYTE_ORDER __LITTLE_ENDIAN
44 #endif
45 #endif /* CONFIG_TI_COMPILER */
47 #ifdef CONFIG_NATIVE_WINDOWS
48 #include <winsock.h>
50 typedef int socklen_t;
52 #ifndef MSG_DONTWAIT
53 #define MSG_DONTWAIT 0 /* not supported */
54 #endif
56 #endif /* CONFIG_NATIVE_WINDOWS */
58 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
60 #ifdef _MSC_VER
61 #define inline __inline
62 #endif /* _MSC_VER */
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__ */
85 #ifndef __BYTE_ORDER
86 #ifndef __LITTLE_ENDIAN
87 #ifndef __BIG_ENDIAN
88 #define __LITTLE_ENDIAN 1234
89 #define __BIG_ENDIAN 4321
90 #if defined(sparc)
91 #define __BYTE_ORDER __BIG_ENDIAN
92 #endif
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
123 #endif
124 #else
125 #error Could not determine CPU byte order
126 #endif
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) \
133 do { \
134 (a)[0] = ((u16) (val)) >> 8; \
135 (a)[1] = ((u16) (val)) & 0xff; \
136 } while (0)
138 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
139 #define WPA_PUT_LE16(a, val) \
140 do { \
141 (a)[1] = ((u16) (val)) >> 8; \
142 (a)[0] = ((u16) (val)) & 0xff; \
143 } while (0)
145 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
146 ((u32) (a)[2]))
147 #define WPA_PUT_BE24(a, val) \
148 do { \
149 (a)[0] = (u8) (((u32) (val)) >> 16); \
150 (a)[1] = (u8) (((u32) (val)) >> 8); \
151 (a)[2] = (u8) (((u32) (val)) & 0xff); \
152 } while (0)
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) \
157 do { \
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); \
162 } while (0)
164 #define WPA_PUT_BE64(a, val) \
165 do { \
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); \
174 } while (0)
177 #ifndef ETH_ALEN
178 #define ETH_ALEN 6
179 #endif
181 #ifdef _MSC_VER
182 typedef UINT64 u64;
183 typedef UINT32 u32;
184 typedef UINT16 u16;
185 typedef UINT8 u8;
186 typedef INT64 s64;
187 typedef INT32 s32;
188 typedef INT16 s16;
189 typedef INT8 s8;
190 #define WPA_TYPES_DEFINED
191 #endif /* _MSC_VER */
193 #ifdef __vxworks
194 typedef unsigned long long u64;
195 typedef UINT32 u32;
196 typedef UINT16 u16;
197 typedef UINT8 u8;
198 typedef long long s64;
199 typedef INT32 s32;
200 typedef INT16 s16;
201 typedef INT8 s8;
202 #define WPA_TYPES_DEFINED
203 #endif /* __vxworks */
205 #ifdef CONFIG_TI_COMPILER
206 #ifdef _LLONG_AVAILABLE
207 typedef unsigned long long u64;
208 #else
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;
214 #endif
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>
224 #else
225 #include <stdint.h>
226 #endif
227 typedef uint64_t u64;
228 typedef uint32_t u32;
229 typedef uint16_t u16;
230 typedef uint8_t u8;
231 typedef int64_t s64;
232 typedef int32_t s32;
233 typedef int16_t s16;
234 typedef int8_t s8;
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);
245 #ifdef __GNUC__
246 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
247 #define STRUCT_PACKED __attribute__ ((packed))
248 #else
249 #define PRINTF_FORMAT(a,b)
250 #define STRUCT_PACKED
251 #endif
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 };
259 #ifdef CONFIG_NO_STDOUT_DEBUG
261 #define wpa_debug_print_timestamp() do { } while (0)
262 #define wpa_printf(args...) do { } while (0)
263 #define wpa_hexdump(l,t,b,le) do { } while (0)
264 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
265 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
266 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
267 #define wpa_debug_open_file() do { } while (0)
268 #define wpa_debug_close_file() do { } while (0)
270 #else /* CONFIG_NO_STDOUT_DEBUG */
272 int wpa_debug_open_file(void);
273 void wpa_debug_close_file(void);
276 * wpa_debug_printf_timestamp - Print timestamp for debug output
278 * This function prints a timestamp in <seconds from 1970>.<microsoconds>
279 * format if debug output has been configured to include timestamps in debug
280 * messages.
282 void wpa_debug_print_timestamp(void);
285 * wpa_printf - conditional printf
286 * @level: priority level (MSG_*) of the message
287 * @fmt: printf format string, followed by optional arguments
289 * This function is used to print conditional debugging and error messages. The
290 * output may be directed to stdout, stderr, and/or syslog based on
291 * configuration.
293 * Note: New line '\n' is added to the end of the text when printing to stdout.
295 void wpa_printf(int level, char *fmt, ...)
296 PRINTF_FORMAT(2, 3);
299 * wpa_hexdump - conditional hex dump
300 * @level: priority level (MSG_*) of the message
301 * @title: title of for the message
302 * @buf: data buffer to be dumped
303 * @len: length of the buf
305 * This function is used to print conditional debugging and error messages. The
306 * output may be directed to stdout, stderr, and/or syslog based on
307 * configuration. The contents of buf is printed out has hex dump.
309 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
312 * wpa_hexdump_key - conditional hex dump, hide keys
313 * @level: priority level (MSG_*) of the message
314 * @title: title of for the message
315 * @buf: data buffer to be dumped
316 * @len: length of the buf
318 * This function is used to print conditional debugging and error messages. The
319 * output may be directed to stdout, stderr, and/or syslog based on
320 * configuration. The contents of buf is printed out has hex dump. This works
321 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
322 * etc.) in debug output.
324 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
327 * wpa_hexdump_ascii - conditional hex dump
328 * @level: priority level (MSG_*) of the message
329 * @title: title of for the message
330 * @buf: data buffer to be dumped
331 * @len: length of the buf
333 * This function is used to print conditional debugging and error messages. The
334 * output may be directed to stdout, stderr, and/or syslog based on
335 * configuration. The contents of buf is printed out has hex dump with both
336 * the hex numbers and ASCII characters (for printable range) are shown. 16
337 * bytes per line will be shown.
339 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
340 size_t len);
343 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
344 * @level: priority level (MSG_*) of the message
345 * @title: title of for the message
346 * @buf: data buffer to be dumped
347 * @len: length of the buf
349 * This function is used to print conditional debugging and error messages. The
350 * output may be directed to stdout, stderr, and/or syslog based on
351 * configuration. The contents of buf is printed out has hex dump with both
352 * the hex numbers and ASCII characters (for printable range) are shown. 16
353 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
354 * default, does not include secret keys (passwords, etc.) in debug output.
356 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
357 size_t len);
359 #endif /* CONFIG_NO_STDOUT_DEBUG */
362 #ifdef CONFIG_NO_WPA_MSG
363 #define wpa_msg(args...) do { } while (0)
364 #define wpa_msg_register_cb(f) do { } while (0)
365 #else /* CONFIG_NO_WPA_MSG */
367 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
368 * @ctx: Pointer to context data; this is the ctx variable registered
369 * with struct wpa_driver_ops::init()
370 * @level: priority level (MSG_*) of the message
371 * @fmt: printf format string, followed by optional arguments
373 * This function is used to print conditional debugging and error messages. The
374 * output may be directed to stdout, stderr, and/or syslog based on
375 * configuration. This function is like wpa_printf(), but it also sends the
376 * same message to all attached ctrl_iface monitors.
378 * Note: New line '\n' is added to the end of the text when printing to stdout.
380 void wpa_msg(void *ctx, int level, char *fmt, ...) PRINTF_FORMAT(3, 4);
382 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
383 size_t len);
386 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
387 * @func: Callback function (%NULL to unregister)
389 void wpa_msg_register_cb(wpa_msg_cb_func func);
390 #endif /* CONFIG_NO_WPA_MSG */
393 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
394 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
395 size_t len);
398 #ifdef EAPOL_TEST
399 #define WPA_ASSERT(a) \
400 do { \
401 if (!(a)) { \
402 printf("WPA_ASSERT FAILED '" #a "' " \
403 "%s %s:%d\n", \
404 __FUNCTION__, __FILE__, __LINE__); \
405 exit(1); \
407 } while (0)
408 #else
409 #define WPA_ASSERT(a) do { } while (0)
410 #endif
413 #ifdef _MSC_VER
414 #undef vsnprintf
415 #define vsnprintf _vsnprintf
416 #undef close
417 #define close closesocket
418 #endif /* _MSC_VER */
421 #ifdef CONFIG_ANSI_C_EXTRA
423 #if !defined(_MSC_VER) || _MSC_VER < 1400
424 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
425 * due to possible buffer overflow; see, e.g.,
426 * http://www.ijs.si/software/snprintf/ for portable implementation of
427 * snprintf. */
428 int snprintf(char *str, size_t size, const char *format, ...);
430 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
431 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
432 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
434 /* getopt - only used in main.c */
435 int getopt(int argc, char *const argv[], const char *optstring);
436 extern char *optarg;
437 extern int optind;
439 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
440 #ifndef __socklen_t_defined
441 typedef int socklen_t;
442 #endif
443 #endif
445 /* inline - define as __inline or just define it to be empty, if needed */
446 #ifdef CONFIG_NO_INLINE
447 #define inline
448 #else
449 #define inline __inline
450 #endif
452 #ifndef __func__
453 #define __func__ "__func__ not defined"
454 #endif
456 #ifndef bswap_16
457 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
458 #endif
460 #ifndef bswap_32
461 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
462 (((u32) (a) << 8) & 0xff0000) | \
463 (((u32) (a) >> 8) & 0xff00) | \
464 (((u32) (a) >> 24) & 0xff))
465 #endif
467 #ifndef MSG_DONTWAIT
468 #define MSG_DONTWAIT 0
469 #endif
471 #ifdef _WIN32_WCE
472 void perror(const char *s);
473 #endif /* _WIN32_WCE */
475 #endif /* CONFIG_ANSI_C_EXTRA */
477 #define wpa_zalloc(s) os_zalloc((s))
479 #ifdef CONFIG_NATIVE_WINDOWS
480 void wpa_unicode2ascii_inplace(TCHAR *str);
481 TCHAR * wpa_strdup_tchar(const char *str);
482 #else /* CONFIG_NATIVE_WINDOWS */
483 #define wpa_unicode2ascii_inplace(s) do { } while (0)
484 #define wpa_strdup_tchar(s) strdup((s))
485 #endif /* CONFIG_NATIVE_WINDOWS */
487 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len);
489 typedef u32 __be32;
490 typedef u64 __be64;
492 #endif /* COMMON_H */