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.
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__) */
38 #include <sys/types.h>
39 #include <machine/endian.h>
40 #define __BYTE_ORDER _BYTE_ORDER
41 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
42 #define __BIG_ENDIAN _BIG_ENDIAN
43 static inline unsigned short bswap_16(unsigned short v
)
45 return ((v
& 0xff) << 8) | (v
>> 8);
48 static inline unsigned int bswap_32(unsigned int v
)
50 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
51 ((v
& 0xff0000) >> 8) | (v
>> 24);
53 #endif /* __APPLE__ */
55 #ifdef CONFIG_TI_COMPILER
56 #define __BIG_ENDIAN 4321
57 #define __LITTLE_ENDIAN 1234
59 #define __BYTE_ORDER __BIG_ENDIAN
61 #define __BYTE_ORDER __LITTLE_ENDIAN
63 #endif /* CONFIG_TI_COMPILER */
66 #define __BIG_ENDIAN 4321
67 #define __LITTLE_ENDIAN 1234
68 #define __BYTE_ORDER __LITTLE_ENDIAN
69 #endif /* __SYMBIAN32__ */
71 #ifdef CONFIG_NATIVE_WINDOWS
74 typedef int socklen_t
;
77 #define MSG_DONTWAIT 0 /* not supported */
80 #endif /* CONFIG_NATIVE_WINDOWS */
83 #define inline __inline
86 #define vsnprintf _vsnprintf
88 #define close closesocket
92 /* Define platform specific integer types */
103 #define WPA_TYPES_DEFINED
104 #endif /* _MSC_VER */
107 typedef unsigned long long u64
;
111 typedef long long s64
;
115 #define WPA_TYPES_DEFINED
116 #endif /* __vxworks */
118 #ifdef CONFIG_TI_COMPILER
119 #ifdef _LLONG_AVAILABLE
120 typedef unsigned long long u64
;
123 * TODO: 64-bit variable not available. Using long as a workaround to test the
124 * build, but this will likely not work for all operations.
126 typedef unsigned long u64
;
128 typedef unsigned int u32
;
129 typedef unsigned short u16
;
130 typedef unsigned char u8
;
131 #define WPA_TYPES_DEFINED
132 #endif /* CONFIG_TI_COMPILER */
135 #define __REMOVE_PLATSEC_DIAGNOSTICS__
141 #define WPA_TYPES_DEFINED
142 #endif /* __SYMBIAN32__ */
144 #ifndef WPA_TYPES_DEFINED
145 #ifdef CONFIG_USE_INTTYPES_H
146 #include <inttypes.h>
150 typedef uint64_t u64
;
151 typedef uint32_t u32
;
152 typedef uint16_t u16
;
158 #define WPA_TYPES_DEFINED
159 #endif /* !WPA_TYPES_DEFINED */
162 /* Define platform specific byte swapping macros */
164 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
166 static inline unsigned short wpa_swap_16(unsigned short v
)
168 return ((v
& 0xff) << 8) | (v
>> 8);
171 static inline unsigned int wpa_swap_32(unsigned int v
)
173 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
174 ((v
& 0xff0000) >> 8) | (v
>> 24);
177 #define le_to_host16(n) (n)
178 #define host_to_le16(n) (n)
179 #define be_to_host16(n) wpa_swap_16(n)
180 #define host_to_be16(n) wpa_swap_16(n)
181 #define le_to_host32(n) (n)
182 #define be_to_host32(n) wpa_swap_32(n)
183 #define host_to_be32(n) wpa_swap_32(n)
185 #define WPA_BYTE_SWAP_DEFINED
187 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
190 #ifndef WPA_BYTE_SWAP_DEFINED
193 #ifndef __LITTLE_ENDIAN
195 #define __LITTLE_ENDIAN 1234
196 #define __BIG_ENDIAN 4321
198 #define __BYTE_ORDER __BIG_ENDIAN
200 #endif /* __BIG_ENDIAN */
201 #endif /* __LITTLE_ENDIAN */
202 #endif /* __BYTE_ORDER */
204 #if __BYTE_ORDER == __LITTLE_ENDIAN
205 #define le_to_host16(n) ((__force u16) (le16) (n))
206 #define host_to_le16(n) ((__force le16) (u16) (n))
207 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
208 #define host_to_be16(n) ((__force be16) bswap_16((n)))
209 #define le_to_host32(n) ((__force u32) (le32) (n))
210 #define host_to_le32(n) ((__force le32) (u32) (n))
211 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
212 #define host_to_be32(n) ((__force be32) bswap_32((n)))
213 #define le_to_host64(n) ((__force u64) (le64) (n))
214 #define host_to_le64(n) ((__force le64) (u64) (n))
215 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
216 #define host_to_be64(n) ((__force be64) bswap_64((n)))
217 #elif __BYTE_ORDER == __BIG_ENDIAN
218 #define le_to_host16(n) bswap_16(n)
219 #define host_to_le16(n) bswap_16(n)
220 #define be_to_host16(n) (n)
221 #define host_to_be16(n) (n)
222 #define le_to_host32(n) bswap_32(n)
223 #define be_to_host32(n) (n)
224 #define host_to_be32(n) (n)
225 #define le_to_host64(n) bswap_64(n)
226 #define host_to_le64(n) bswap_64(n)
227 #define be_to_host64(n) (n)
228 #define host_to_be64(n) (n)
229 #ifndef WORDS_BIGENDIAN
230 #define WORDS_BIGENDIAN
233 #error Could not determine CPU byte order
236 #define WPA_BYTE_SWAP_DEFINED
237 #endif /* !WPA_BYTE_SWAP_DEFINED */
240 /* Macros for handling unaligned memory accesses */
242 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
243 #define WPA_PUT_BE16(a, val) \
245 (a)[0] = ((u16) (val)) >> 8; \
246 (a)[1] = ((u16) (val)) & 0xff; \
249 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
250 #define WPA_PUT_LE16(a, val) \
252 (a)[1] = ((u16) (val)) >> 8; \
253 (a)[0] = ((u16) (val)) & 0xff; \
256 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
258 #define WPA_PUT_BE24(a, val) \
260 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
261 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
262 (a)[2] = (u8) (((u32) (val)) & 0xff); \
265 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
266 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
267 #define WPA_PUT_BE32(a, val) \
269 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
270 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
271 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
272 (a)[3] = (u8) (((u32) (val)) & 0xff); \
275 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
276 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
277 #define WPA_PUT_LE32(a, val) \
279 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
280 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
281 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
282 (a)[0] = (u8) (((u32) (val)) & 0xff); \
285 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
286 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
287 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
288 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
289 #define WPA_PUT_BE64(a, val) \
291 (a)[0] = (u8) (((u64) (val)) >> 56); \
292 (a)[1] = (u8) (((u64) (val)) >> 48); \
293 (a)[2] = (u8) (((u64) (val)) >> 40); \
294 (a)[3] = (u8) (((u64) (val)) >> 32); \
295 (a)[4] = (u8) (((u64) (val)) >> 24); \
296 (a)[5] = (u8) (((u64) (val)) >> 16); \
297 (a)[6] = (u8) (((u64) (val)) >> 8); \
298 (a)[7] = (u8) (((u64) (val)) & 0xff); \
301 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
302 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
303 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
304 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
313 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
314 #define STRUCT_PACKED __attribute__ ((packed))
316 #define PRINTF_FORMAT(a,b)
317 #define STRUCT_PACKED
321 #ifdef CONFIG_ANSI_C_EXTRA
323 #if !defined(_MSC_VER) || _MSC_VER < 1400
324 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
325 * due to possible buffer overflow; see, e.g.,
326 * http://www.ijs.si/software/snprintf/ for portable implementation of
328 int snprintf(char *str
, size_t size
, const char *format
, ...);
330 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
331 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
332 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
334 /* getopt - only used in main.c */
335 int getopt(int argc
, char *const argv
[], const char *optstring
);
339 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
340 #ifndef __socklen_t_defined
341 typedef int socklen_t
;
345 /* inline - define as __inline or just define it to be empty, if needed */
346 #ifdef CONFIG_NO_INLINE
349 #define inline __inline
353 #define __func__ "__func__ not defined"
357 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
361 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
362 (((u32) (a) << 8) & 0xff0000) | \
363 (((u32) (a) >> 8) & 0xff00) | \
364 (((u32) (a) >> 24) & 0xff))
368 #define MSG_DONTWAIT 0
372 void perror(const char *s
);
373 #endif /* _WIN32_WCE */
375 #endif /* CONFIG_ANSI_C_EXTRA */
378 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
379 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
383 #define BIT(x) (1 << (x))
387 * Definitions for sparse validation
388 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
391 #define __force __attribute__((force))
392 #define __bitwise __attribute__((bitwise))
398 typedef u16 __bitwise be16
;
399 typedef u16 __bitwise le16
;
400 typedef u32 __bitwise be32
;
401 typedef u32 __bitwise le32
;
402 typedef u64 __bitwise be64
;
403 typedef u64 __bitwise le64
;
406 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
407 #define __must_check __attribute__((__warn_unused_result__))
410 #endif /* __GNUC__ */
411 #endif /* __must_check */
413 int hwaddr_aton(const char *txt
, u8
*addr
);
414 int hexstr2bin(const char *hex
, u8
*buf
, size_t len
);
415 void inc_byte_array(u8
*counter
, size_t len
);
416 void wpa_get_ntp_timestamp(u8
*buf
);
417 int wpa_snprintf_hex(char *buf
, size_t buf_size
, const u8
*data
, size_t len
);
418 int wpa_snprintf_hex_uppercase(char *buf
, size_t buf_size
, const u8
*data
,
421 #ifdef CONFIG_NATIVE_WINDOWS
422 void wpa_unicode2ascii_inplace(TCHAR
*str
);
423 TCHAR
* wpa_strdup_tchar(const char *str
);
424 #else /* CONFIG_NATIVE_WINDOWS */
425 #define wpa_unicode2ascii_inplace(s) do { } while (0)
426 #define wpa_strdup_tchar(s) strdup((s))
427 #endif /* CONFIG_NATIVE_WINDOWS */
429 const char * wpa_ssid_txt(const u8
*ssid
, size_t ssid_len
);
431 static inline int is_zero_ether_addr(const u8
*a
)
433 return !(a
[0] | a
[1] | a
[2] | a
[3] | a
[4] | a
[5]);
436 #include "wpa_debug.h"
438 #endif /* COMMON_H */