List.mui: Update entries count prior to range change
[AROS.git] / workbench / network / WirelessManager / src / utils / common.h
bloba8ea5d2a965bad4f4aa325bde4a0b887bffccbc7
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #ifndef COMMON_H
16 #define COMMON_H
18 #include "os.h"
20 #if defined(__linux__) || defined(__GLIBC__)
21 #include <endian.h>
22 #include <byteswap.h>
23 #endif /* __linux__ */
25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
26 defined(__OpenBSD__) || defined(__AROS__)
27 #include <sys/types.h>
28 #ifndef __AROS__
29 #include <sys/endian.h>
30 #endif
31 #define __BYTE_ORDER _BYTE_ORDER
32 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
33 #define __BIG_ENDIAN _BIG_ENDIAN
34 #ifdef __OpenBSD__
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__) */
47 #ifdef __APPLE__
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__ */
65 #ifdef __amigaos__
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
74 #ifdef __big_endian__
75 #define __BYTE_ORDER __BIG_ENDIAN
76 #else
77 #define __BYTE_ORDER __LITTLE_ENDIAN
78 #endif
79 #endif /* CONFIG_TI_COMPILER */
81 #ifdef __SYMBIAN32__
82 #define __BIG_ENDIAN 4321
83 #define __LITTLE_ENDIAN 1234
84 #define __BYTE_ORDER __LITTLE_ENDIAN
85 #endif /* __SYMBIAN32__ */
87 #ifdef CONFIG_NATIVE_WINDOWS
88 #include <winsock.h>
90 typedef int socklen_t;
92 #ifndef MSG_DONTWAIT
93 #define MSG_DONTWAIT 0 /* not supported */
94 #endif
96 #endif /* CONFIG_NATIVE_WINDOWS */
98 #ifdef _MSC_VER
99 #define inline __inline
101 #undef vsnprintf
102 #define vsnprintf _vsnprintf
103 #undef close
104 #define close closesocket
105 #endif /* _MSC_VER */
108 /* Define platform specific integer types */
110 #ifdef _MSC_VER
111 typedef UINT64 u64;
112 typedef UINT32 u32;
113 typedef UINT16 u16;
114 typedef UINT8 u8;
115 typedef INT64 s64;
116 typedef INT32 s32;
117 typedef INT16 s16;
118 typedef INT8 s8;
119 #define WPA_TYPES_DEFINED
120 #endif /* _MSC_VER */
122 #ifdef __vxworks
123 typedef unsigned long long u64;
124 typedef UINT32 u32;
125 typedef UINT16 u16;
126 typedef UINT8 u8;
127 typedef long long s64;
128 typedef INT32 s32;
129 typedef INT16 s16;
130 typedef INT8 s8;
131 #define WPA_TYPES_DEFINED
132 #endif /* __vxworks */
134 #ifdef CONFIG_TI_COMPILER
135 #ifdef _LLONG_AVAILABLE
136 typedef unsigned long long u64;
137 #else
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;
143 #endif
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 */
150 #ifdef __SYMBIAN32__
151 #define __REMOVE_PLATSEC_DIAGNOSTICS__
152 #include <e32def.h>
153 typedef TUint64 u64;
154 typedef TUint32 u32;
155 typedef TUint16 u16;
156 typedef TUint8 u8;
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;
163 typedef ULONG u32;
164 typedef UWORD u16;
165 typedef UBYTE u8;
166 typedef long long s64;
167 typedef LONG s32;
168 typedef WORD s16;
169 typedef BYTE s8;
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>
176 #else
177 #include <stdint.h>
178 #endif
179 typedef uint64_t u64;
180 typedef uint32_t u32;
181 typedef uint16_t u16;
182 typedef uint8_t u8;
183 typedef int64_t s64;
184 typedef int32_t s32;
185 typedef int16_t s16;
186 typedef int8_t s8;
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
237 #ifndef __BYTE_ORDER
238 #ifndef __LITTLE_ENDIAN
239 #ifndef __BIG_ENDIAN
240 #define __LITTLE_ENDIAN 1234
241 #define __BIG_ENDIAN 4321
242 #if defined(sparc)
243 #define __BYTE_ORDER __BIG_ENDIAN
244 #endif
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
277 #endif
278 #else
279 #error Could not determine CPU byte order
280 #endif
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) \
290 do { \
291 (a)[0] = ((u16) (val)) >> 8; \
292 (a)[1] = ((u16) (val)) & 0xff; \
293 } while (0)
295 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
296 #define WPA_PUT_LE16(a, val) \
297 do { \
298 (a)[1] = ((u16) (val)) >> 8; \
299 (a)[0] = ((u16) (val)) & 0xff; \
300 } while (0)
302 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
303 ((u32) (a)[2]))
304 #define WPA_PUT_BE24(a, val) \
305 do { \
306 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
307 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
308 (a)[2] = (u8) (((u32) (val)) & 0xff); \
309 } while (0)
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) \
314 do { \
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); \
319 } while (0)
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) \
324 do { \
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); \
329 } while (0)
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) \
336 do { \
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); \
345 } while (0)
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]))
353 #ifndef ETH_ALEN
354 #define ETH_ALEN 6
355 #endif
356 #ifndef IFNAMSIZ
357 #define IFNAMSIZ 16
358 #endif
359 #ifndef ETH_P_ALL
360 #define ETH_P_ALL 0x0003
361 #endif
362 #ifndef ETH_P_PAE
363 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
364 #endif /* ETH_P_PAE */
365 #ifndef ETH_P_EAPOL
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 */
371 #ifndef ETH_P_RRB
372 #define ETH_P_RRB 0x890D
373 #endif /* ETH_P_RRB */
376 #ifdef __GNUC__
377 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
378 #define STRUCT_PACKED __attribute__ ((packed))
379 #else
380 #define PRINTF_FORMAT(a,b)
381 #define STRUCT_PACKED
382 #endif
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
391 * snprintf. */
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);
400 extern char *optarg;
401 extern int optind;
403 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
404 #ifndef __socklen_t_defined
405 typedef int socklen_t;
406 #endif
407 #endif
409 /* inline - define as __inline or just define it to be empty, if needed */
410 #ifdef CONFIG_NO_INLINE
411 #define inline
412 #else
413 #define inline __inline
414 #endif
416 #ifndef __func__
417 #define __func__ "__func__ not defined"
418 #endif
420 #ifndef bswap_16
421 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
422 #endif
424 #ifndef bswap_32
425 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
426 (((u32) (a) << 8) & 0xff0000) | \
427 (((u32) (a) >> 8) & 0xff00) | \
428 (((u32) (a) >> 24) & 0xff))
429 #endif
431 #ifndef MSG_DONTWAIT
432 #define MSG_DONTWAIT 0
433 #endif
435 #ifdef _WIN32_WCE
436 void perror(const char *s);
437 #endif /* _WIN32_WCE */
439 #endif /* CONFIG_ANSI_C_EXTRA */
441 #ifndef MAC2STR
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"
444 #endif
446 #ifndef BIT
447 #define BIT(x) (1 << (x))
448 #endif
451 * Definitions for sparse validation
452 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
454 #ifdef __CHECKER__
455 #define __force __attribute__((force))
456 #define __bitwise __attribute__((bitwise))
457 #else
458 #define __force
459 #define __bitwise
460 #endif
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;
469 #ifndef __must_check
470 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
471 #define __must_check __attribute__((__warn_unused_result__))
472 #else
473 #define __must_check
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,
484 size_t len);
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 */