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__) || \
26 defined(__OpenBSD__) || defined(__AROS__)
27 #include <sys/types.h>
29 #include <sys/endian.h>
31 #define __BYTE_ORDER _BYTE_ORDER
32 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
33 #define __BIG_ENDIAN _BIG_ENDIAN
35 #define bswap_16 swap16
36 #define bswap_32 swap32
37 #define bswap_64 swap64
38 #else /* __OpenBSD__ */
39 #define bswap_16 bswap16
40 #define bswap_32 bswap32
41 #define bswap_64 bswap64
42 #endif /* __OpenBSD__ */
43 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
44 * defined(__DragonFly__) || defined(__OpenBSD__) ||
45 * defined(__AROS__) */
48 #include <sys/types.h>
49 #include <machine/endian.h>
50 #define __BYTE_ORDER _BYTE_ORDER
51 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
52 #define __BIG_ENDIAN _BIG_ENDIAN
53 static inline unsigned short bswap_16(unsigned short v
)
55 return ((v
& 0xff) << 8) | (v
>> 8);
58 static inline unsigned int bswap_32(unsigned int v
)
60 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
61 ((v
& 0xff0000) >> 8) | (v
>> 24);
63 #endif /* __APPLE__ */
66 #define __BIG_ENDIAN 4321
67 #define __LITTLE_ENDIAN 1234
68 #define __BYTE_ORDER __BIG_ENDIAN
69 #endif /* __amigaos__ */
71 #ifdef CONFIG_TI_COMPILER
72 #define __BIG_ENDIAN 4321
73 #define __LITTLE_ENDIAN 1234
75 #define __BYTE_ORDER __BIG_ENDIAN
77 #define __BYTE_ORDER __LITTLE_ENDIAN
79 #endif /* CONFIG_TI_COMPILER */
82 #define __BIG_ENDIAN 4321
83 #define __LITTLE_ENDIAN 1234
84 #define __BYTE_ORDER __LITTLE_ENDIAN
85 #endif /* __SYMBIAN32__ */
87 #ifdef CONFIG_NATIVE_WINDOWS
90 typedef int socklen_t
;
93 #define MSG_DONTWAIT 0 /* not supported */
96 #endif /* CONFIG_NATIVE_WINDOWS */
99 #define inline __inline
102 #define vsnprintf _vsnprintf
104 #define close closesocket
105 #endif /* _MSC_VER */
108 /* Define platform specific integer types */
119 #define WPA_TYPES_DEFINED
120 #endif /* _MSC_VER */
123 typedef unsigned long long u64
;
127 typedef long long s64
;
131 #define WPA_TYPES_DEFINED
132 #endif /* __vxworks */
134 #ifdef CONFIG_TI_COMPILER
135 #ifdef _LLONG_AVAILABLE
136 typedef unsigned long long u64
;
139 * TODO: 64-bit variable not available. Using long as a workaround to test the
140 * build, but this will likely not work for all operations.
142 typedef unsigned long u64
;
144 typedef unsigned int u32
;
145 typedef unsigned short u16
;
146 typedef unsigned char u8
;
147 #define WPA_TYPES_DEFINED
148 #endif /* CONFIG_TI_COMPILER */
151 #define __REMOVE_PLATSEC_DIAGNOSTICS__
157 #define WPA_TYPES_DEFINED
158 #endif /* __SYMBIAN32__ */
160 #if defined(__mc68000__) || defined(__MORPHOS__)
161 #include <exec/types.h>
162 typedef unsigned long long u64
;
166 typedef long long s64
;
170 #define WPA_TYPES_DEFINED
171 #endif /* defined(__mc68000__) || defined(__MORPHOS__) */
173 #ifndef WPA_TYPES_DEFINED
174 #ifdef CONFIG_USE_INTTYPES_H
175 #include <inttypes.h>
179 typedef uint64_t u64
;
180 typedef uint32_t u32
;
181 typedef uint16_t u16
;
187 #define WPA_TYPES_DEFINED
188 #endif /* !WPA_TYPES_DEFINED */
191 /* Define platform specific byte swapping macros */
193 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
195 static inline unsigned short wpa_swap_16(unsigned short v
)
197 return ((v
& 0xff) << 8) | (v
>> 8);
200 static inline unsigned int wpa_swap_32(unsigned int v
)
202 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
203 ((v
& 0xff0000) >> 8) | (v
>> 24);
206 #define le_to_host16(n) (n)
207 #define host_to_le16(n) (n)
208 #define be_to_host16(n) wpa_swap_16(n)
209 #define host_to_be16(n) wpa_swap_16(n)
210 #define le_to_host32(n) (n)
211 #define be_to_host32(n) wpa_swap_32(n)
212 #define host_to_be32(n) wpa_swap_32(n)
214 #define WPA_BYTE_SWAP_DEFINED
216 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
219 #if defined(CONFIG_DRIVER_SANA2)
221 static inline unsigned short bswap_16(unsigned short v
)
223 return ((v
& 0xff) << 8) | (v
>> 8);
226 static inline unsigned int bswap_32(unsigned int v
)
228 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
229 ((v
& 0xff0000) >> 8) | (v
>> 24);
232 #endif /* CONFIG_DRIVER_SANA2 */
235 #ifndef WPA_BYTE_SWAP_DEFINED
238 #ifndef __LITTLE_ENDIAN
240 #define __LITTLE_ENDIAN 1234
241 #define __BIG_ENDIAN 4321
243 #define __BYTE_ORDER __BIG_ENDIAN
245 #endif /* __BIG_ENDIAN */
246 #endif /* __LITTLE_ENDIAN */
247 #endif /* __BYTE_ORDER */
249 #if __BYTE_ORDER == __LITTLE_ENDIAN
250 #define le_to_host16(n) ((__force u16) (le16) (n))
251 #define host_to_le16(n) ((__force le16) (u16) (n))
252 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
253 #define host_to_be16(n) ((__force be16) bswap_16((n)))
254 #define le_to_host32(n) ((__force u32) (le32) (n))
255 #define host_to_le32(n) ((__force le32) (u32) (n))
256 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
257 #define host_to_be32(n) ((__force be32) bswap_32((n)))
258 #define le_to_host64(n) ((__force u64) (le64) (n))
259 #define host_to_le64(n) ((__force le64) (u64) (n))
260 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
261 #define host_to_be64(n) ((__force be64) bswap_64((n)))
262 #elif __BYTE_ORDER == __BIG_ENDIAN
263 #define le_to_host16(n) bswap_16(n)
264 #define host_to_le16(n) bswap_16(n)
265 #define be_to_host16(n) (n)
266 #define host_to_be16(n) (n)
267 #define le_to_host32(n) bswap_32(n)
268 #define host_to_le32(n) bswap_32(n)
269 #define be_to_host32(n) (n)
270 #define host_to_be32(n) (n)
271 #define le_to_host64(n) bswap_64(n)
272 #define host_to_le64(n) bswap_64(n)
273 #define be_to_host64(n) (n)
274 #define host_to_be64(n) (n)
275 #ifndef WORDS_BIGENDIAN
276 #define WORDS_BIGENDIAN
279 #error Could not determine CPU byte order
282 #define WPA_BYTE_SWAP_DEFINED
283 #endif /* !WPA_BYTE_SWAP_DEFINED */
286 /* Macros for handling unaligned memory accesses */
288 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
289 #define WPA_PUT_BE16(a, val) \
291 (a)[0] = ((u16) (val)) >> 8; \
292 (a)[1] = ((u16) (val)) & 0xff; \
295 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
296 #define WPA_PUT_LE16(a, val) \
298 (a)[1] = ((u16) (val)) >> 8; \
299 (a)[0] = ((u16) (val)) & 0xff; \
302 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
304 #define WPA_PUT_BE24(a, val) \
306 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
307 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
308 (a)[2] = (u8) (((u32) (val)) & 0xff); \
311 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
312 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
313 #define WPA_PUT_BE32(a, val) \
315 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
316 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
317 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
318 (a)[3] = (u8) (((u32) (val)) & 0xff); \
321 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
322 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
323 #define WPA_PUT_LE32(a, val) \
325 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
326 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
327 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
328 (a)[0] = (u8) (((u32) (val)) & 0xff); \
331 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
332 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
333 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
334 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
335 #define WPA_PUT_BE64(a, val) \
337 (a)[0] = (u8) (((u64) (val)) >> 56); \
338 (a)[1] = (u8) (((u64) (val)) >> 48); \
339 (a)[2] = (u8) (((u64) (val)) >> 40); \
340 (a)[3] = (u8) (((u64) (val)) >> 32); \
341 (a)[4] = (u8) (((u64) (val)) >> 24); \
342 (a)[5] = (u8) (((u64) (val)) >> 16); \
343 (a)[6] = (u8) (((u64) (val)) >> 8); \
344 (a)[7] = (u8) (((u64) (val)) & 0xff); \
347 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
348 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
349 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
350 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
360 #define ETH_P_ALL 0x0003
363 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
364 #endif /* ETH_P_PAE */
366 #define ETH_P_EAPOL ETH_P_PAE
367 #endif /* ETH_P_EAPOL */
368 #ifndef ETH_P_RSN_PREAUTH
369 #define ETH_P_RSN_PREAUTH 0x88c7
370 #endif /* ETH_P_RSN_PREAUTH */
372 #define ETH_P_RRB 0x890D
373 #endif /* ETH_P_RRB */
377 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
378 #define STRUCT_PACKED __attribute__ ((packed))
380 #define PRINTF_FORMAT(a,b)
381 #define STRUCT_PACKED
385 #ifdef CONFIG_ANSI_C_EXTRA
387 #if !defined(_MSC_VER) || _MSC_VER < 1400
388 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
389 * due to possible buffer overflow; see, e.g.,
390 * http://www.ijs.si/software/snprintf/ for portable implementation of
392 int snprintf(char *str
, size_t size
, const char *format
, ...);
394 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
395 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
396 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
398 /* getopt - only used in main.c */
399 int getopt(int argc
, char *const argv
[], const char *optstring
);
403 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
404 #ifndef __socklen_t_defined
405 typedef int socklen_t
;
409 /* inline - define as __inline or just define it to be empty, if needed */
410 #ifdef CONFIG_NO_INLINE
413 #define inline __inline
417 #define __func__ "__func__ not defined"
421 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
425 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
426 (((u32) (a) << 8) & 0xff0000) | \
427 (((u32) (a) >> 8) & 0xff00) | \
428 (((u32) (a) >> 24) & 0xff))
432 #define MSG_DONTWAIT 0
436 void perror(const char *s
);
437 #endif /* _WIN32_WCE */
439 #endif /* CONFIG_ANSI_C_EXTRA */
442 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
443 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
447 #define BIT(x) (1 << (x))
451 * Definitions for sparse validation
452 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
455 #define __force __attribute__((force))
456 #define __bitwise __attribute__((bitwise))
462 typedef u16 __bitwise be16
;
463 typedef u16 __bitwise le16
;
464 typedef u32 __bitwise be32
;
465 typedef u32 __bitwise le32
;
466 typedef u64 __bitwise be64
;
467 typedef u64 __bitwise le64
;
470 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
471 #define __must_check __attribute__((__warn_unused_result__))
474 #endif /* __GNUC__ */
475 #endif /* __must_check */
477 int hwaddr_aton(const char *txt
, u8
*addr
);
478 int hwaddr_aton2(const char *txt
, u8
*addr
);
479 int hexstr2bin(const char *hex
, u8
*buf
, size_t len
);
480 void inc_byte_array(u8
*counter
, size_t len
);
481 void wpa_get_ntp_timestamp(u8
*buf
);
482 int wpa_snprintf_hex(char *buf
, size_t buf_size
, const u8
*data
, size_t len
);
483 int wpa_snprintf_hex_uppercase(char *buf
, size_t buf_size
, const u8
*data
,
486 #ifdef CONFIG_NATIVE_WINDOWS
487 void wpa_unicode2ascii_inplace(TCHAR
*str
);
488 TCHAR
* wpa_strdup_tchar(const char *str
);
489 #else /* CONFIG_NATIVE_WINDOWS */
490 #define wpa_unicode2ascii_inplace(s) do { } while (0)
491 #define wpa_strdup_tchar(s) strdup((s))
492 #endif /* CONFIG_NATIVE_WINDOWS */
494 const char * wpa_ssid_txt(const u8
*ssid
, size_t ssid_len
);
496 static inline int is_zero_ether_addr(const u8
*a
)
498 return !(a
[0] | a
[1] | a
[2] | a
[3] | a
[4] | a
[5]);
501 #include "wpa_debug.h"
505 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
506 * networking socket uses that do not really result in a real problem and
507 * cannot be easily avoided with union-based type-punning due to struct
508 * definitions including another struct in system header files. To avoid having
509 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
510 * typecast from aliasing for now. A cleaner solution will hopefully be found
511 * in the future to handle these cases.
513 void * __hide_aliasing_typecast(void *foo
);
514 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
516 #endif /* COMMON_H */