2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, 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.
20 #if defined(__linux__) || defined(__GLIBC__)
23 #endif /* __linux__ */
25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
27 #include <sys/types.h>
28 #include <sys/endian.h>
29 #define __BYTE_ORDER _BYTE_ORDER
30 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
31 #define __BIG_ENDIAN _BIG_ENDIAN
33 #define bswap_16 swap16
34 #define bswap_32 swap32
35 #define bswap_64 swap64
36 #else /* __OpenBSD__ */
37 #define bswap_16 bswap16
38 #define bswap_32 bswap32
39 #define bswap_64 bswap64
40 #endif /* __OpenBSD__ */
41 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
42 * defined(__DragonFly__) || defined(__OpenBSD__) */
45 #include <sys/types.h>
46 #include <machine/endian.h>
47 #define __BYTE_ORDER _BYTE_ORDER
48 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
49 #define __BIG_ENDIAN _BIG_ENDIAN
50 static inline unsigned short bswap_16(unsigned short v)
52 return ((v & 0xff) << 8) | (v >> 8);
55 static inline unsigned int bswap_32(unsigned int v)
57 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
58 ((v & 0xff0000) >> 8) | (v >> 24);
60 #endif /* __APPLE__ */
62 #ifdef CONFIG_TI_COMPILER
63 #define __BIG_ENDIAN 4321
64 #define __LITTLE_ENDIAN 1234
66 #define __BYTE_ORDER __BIG_ENDIAN
68 #define __BYTE_ORDER __LITTLE_ENDIAN
70 #endif /* CONFIG_TI_COMPILER */
73 #define __BIG_ENDIAN 4321
74 #define __LITTLE_ENDIAN 1234
75 #define __BYTE_ORDER __LITTLE_ENDIAN
76 #endif /* __SYMBIAN32__ */
78 #ifdef CONFIG_NATIVE_WINDOWS
86 typedef int socklen_t;
89 #define MSG_DONTWAIT 0 /* not supported */
92 #endif /* CONFIG_NATIVE_WINDOWS */
95 #define inline __inline
98 #define vsnprintf _vsnprintf
100 #define close closesocket
101 #endif /* _MSC_VER */
104 /* Define platform specific integer types */
115 #define WPA_TYPES_DEFINED
116 #endif /* _MSC_VER */
119 typedef unsigned long long u64;
123 typedef long long s64;
127 #define WPA_TYPES_DEFINED
128 #endif /* __vxworks */
130 #ifdef CONFIG_TI_COMPILER
131 #ifdef _LLONG_AVAILABLE
132 typedef unsigned long long u64;
135 * TODO: 64-bit variable not available. Using long as a workaround to test the
136 * build, but this will likely not work for all operations.
138 typedef unsigned long u64;
140 typedef unsigned int u32;
141 typedef unsigned short u16;
142 typedef unsigned char u8;
143 #define WPA_TYPES_DEFINED
144 #endif /* CONFIG_TI_COMPILER */
147 #define __REMOVE_PLATSEC_DIAGNOSTICS__
153 #define WPA_TYPES_DEFINED
154 #endif /* __SYMBIAN32__ */
156 #ifndef WPA_TYPES_DEFINED
157 #ifdef CONFIG_USE_INTTYPES_H
158 #include <inttypes.h>
162 typedef uint64_t u64;
163 typedef uint32_t u32;
164 typedef uint16_t u16;
170 #define WPA_TYPES_DEFINED
171 #endif /* !WPA_TYPES_DEFINED */
174 /* Define platform specific byte swapping macros */
176 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
178 static inline unsigned short wpa_swap_16(unsigned short v)
180 return ((v & 0xff) << 8) | (v >> 8);
183 static inline unsigned int wpa_swap_32(unsigned int v)
185 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
186 ((v & 0xff0000) >> 8) | (v >> 24);
189 #define le_to_host16(n) (n)
190 #define host_to_le16(n) (n)
191 #define be_to_host16(n) wpa_swap_16(n)
192 #define host_to_be16(n) wpa_swap_16(n)
193 #define le_to_host32(n) (n)
194 #define be_to_host32(n) wpa_swap_32(n)
195 #define host_to_be32(n) wpa_swap_32(n)
197 #define WPA_BYTE_SWAP_DEFINED
199 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
202 #ifndef WPA_BYTE_SWAP_DEFINED
205 #ifndef __LITTLE_ENDIAN
207 #define __LITTLE_ENDIAN 1234
208 #define __BIG_ENDIAN 4321
210 #define __BYTE_ORDER __BIG_ENDIAN
212 #endif /* __BIG_ENDIAN */
213 #endif /* __LITTLE_ENDIAN */
214 #endif /* __BYTE_ORDER */
216 #if __BYTE_ORDER == __LITTLE_ENDIAN
217 #define le_to_host16(n) ((__force u16) (le16) (n))
218 #define host_to_le16(n) ((__force le16) (u16) (n))
219 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
220 #define host_to_be16(n) ((__force be16) bswap_16((n)))
221 #define le_to_host32(n) ((__force u32) (le32) (n))
222 #define host_to_le32(n) ((__force le32) (u32) (n))
223 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
224 #define host_to_be32(n) ((__force be32) bswap_32((n)))
225 #define le_to_host64(n) ((__force u64) (le64) (n))
226 #define host_to_le64(n) ((__force le64) (u64) (n))
227 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
228 #define host_to_be64(n) ((__force be64) bswap_64((n)))
229 #elif __BYTE_ORDER == __BIG_ENDIAN
230 #define le_to_host16(n) bswap_16(n)
231 #define host_to_le16(n) bswap_16(n)
232 #define be_to_host16(n) (n)
233 #define host_to_be16(n) (n)
234 #define le_to_host32(n) bswap_32(n)
235 #define be_to_host32(n) (n)
236 #define host_to_be32(n) (n)
237 #define le_to_host64(n) bswap_64(n)
238 #define host_to_le64(n) bswap_64(n)
239 #define be_to_host64(n) (n)
240 #define host_to_be64(n) (n)
241 #ifndef WORDS_BIGENDIAN
242 #define WORDS_BIGENDIAN
245 #error Could not determine CPU byte order
248 #define WPA_BYTE_SWAP_DEFINED
249 #endif /* !WPA_BYTE_SWAP_DEFINED */
252 /* Macros for handling unaligned memory accesses */
254 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
255 #define WPA_PUT_BE16(a, val) \
257 (a)[0] = ((u16) (val)) >> 8; \
258 (a)[1] = ((u16) (val)) & 0xff; \
261 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
262 #define WPA_PUT_LE16(a, val) \
264 (a)[1] = ((u16) (val)) >> 8; \
265 (a)[0] = ((u16) (val)) & 0xff; \
268 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
270 #define WPA_PUT_BE24(a, val) \
272 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
273 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
274 (a)[2] = (u8) (((u32) (val)) & 0xff); \
277 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
278 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
279 #define WPA_PUT_BE32(a, val) \
281 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
282 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
283 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
284 (a)[3] = (u8) (((u32) (val)) & 0xff); \
287 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
288 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
289 #define WPA_PUT_LE32(a, val) \
291 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
292 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
293 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
294 (a)[0] = (u8) (((u32) (val)) & 0xff); \
297 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
298 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
299 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
300 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
301 #define WPA_PUT_BE64(a, val) \
303 (a)[0] = (u8) (((u64) (val)) >> 56); \
304 (a)[1] = (u8) (((u64) (val)) >> 48); \
305 (a)[2] = (u8) (((u64) (val)) >> 40); \
306 (a)[3] = (u8) (((u64) (val)) >> 32); \
307 (a)[4] = (u8) (((u64) (val)) >> 24); \
308 (a)[5] = (u8) (((u64) (val)) >> 16); \
309 (a)[6] = (u8) (((u64) (val)) >> 8); \
310 (a)[7] = (u8) (((u64) (val)) & 0xff); \
313 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
314 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
315 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
316 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
326 #define ETH_P_ALL 0x0003
329 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
330 #endif /* ETH_P_PAE */
332 #define ETH_P_EAPOL ETH_P_PAE
333 #endif /* ETH_P_EAPOL */
334 #ifndef ETH_P_RSN_PREAUTH
335 #define ETH_P_RSN_PREAUTH 0x88c7
336 #endif /* ETH_P_RSN_PREAUTH */
338 #define ETH_P_RRB 0x890D
339 #endif /* ETH_P_RRB */
343 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
344 #define STRUCT_PACKED __attribute__ ((packed))
345 #define UNUSED __attribute__ ((unused))
347 #define PRINTF_FORMAT(a,b)
348 #define STRUCT_PACKED
353 #ifdef CONFIG_ANSI_C_EXTRA
355 #if !defined(_MSC_VER) || _MSC_VER < 1400
356 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
357 * due to possible buffer overflow; see, e.g.,
358 * http://www.ijs.si/software/snprintf/ for portable implementation of
360 int snprintf(char *str, size_t size, const char *format, ...);
362 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
363 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
364 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
366 /* getopt - only used in main.c */
367 int getopt(int argc, char *const argv[], const char *optstring);
371 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
372 #ifndef __socklen_t_defined
373 typedef int socklen_t;
377 /* inline - define as __inline or just define it to be empty, if needed */
378 #ifdef CONFIG_NO_INLINE
381 #define inline __inline
385 #define __func__ "__func__ not defined"
389 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
393 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
394 (((u32) (a) << 8) & 0xff0000) | \
395 (((u32) (a) >> 8) & 0xff00) | \
396 (((u32) (a) >> 24) & 0xff))
400 #define MSG_DONTWAIT 0
404 void perror(const char *s);
405 #endif /* _WIN32_WCE */
407 #endif /* CONFIG_ANSI_C_EXTRA */
410 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
411 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
415 #define BIT(x) (1 << (x))
419 * Definitions for sparse validation
420 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
423 #define __force __attribute__((force))
424 #define __bitwise __attribute__((bitwise))
430 typedef u16 __bitwise be16;
431 typedef u16 __bitwise le16;
432 typedef u32 __bitwise be32;
433 typedef u32 __bitwise le32;
434 typedef u64 __bitwise be64;
435 typedef u64 __bitwise le64;
438 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
439 #define __must_check __attribute__((__warn_unused_result__))
442 #endif /* __GNUC__ */
443 #endif /* __must_check */
445 int hwaddr_aton(const char *txt, u8 *addr);
446 int hwaddr_aton2(const char *txt, u8 *addr);
447 int hexstr2bin(const char *hex, u8 *buf, size_t len);
448 void inc_byte_array(u8 *counter, size_t len);
449 void wpa_get_ntp_timestamp(u8 *buf);
450 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
451 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
454 #ifdef CONFIG_NATIVE_WINDOWS
455 void wpa_unicode2ascii_inplace(TCHAR *str);
456 TCHAR * wpa_strdup_tchar(const char *str);
457 #else /* CONFIG_NATIVE_WINDOWS */
458 #define wpa_unicode2ascii_inplace(s) do { } while (0)
459 #define wpa_strdup_tchar(s) strdup((s))
460 #endif /* CONFIG_NATIVE_WINDOWS */
462 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
464 static inline int is_zero_ether_addr(const u8 *a)
466 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
469 #include "wpa_debug.h"
473 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
474 * networking socket uses that do not really result in a real problem and
475 * cannot be easily avoided with union-based type-punning due to struct
476 * definitions including another struct in system header files. To avoid having
477 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
478 * typecast from aliasing for now. A cleaner solution will hopefully be found
479 * in the future to handle these cases.
481 void * __hide_aliasing_typecast(void *foo);
482 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
484 #endif /* COMMON_H */