Merge remote-tracking branch 'origin/rfc3961-mic'
[mech_eap.git] / libeap / src / utils / common.h
1 /*
2  * wpa_supplicant/hostapd / common helper functions, etc.
3  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #ifndef COMMON_H
16 #define COMMON_H
17
18 #include "os.h"
19
20 #if defined(__linux__) || defined(__GLIBC__)
21 #include <endian.h>
22 #include <byteswap.h>
23 #endif /* __linux__ */
24
25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
26     defined(__OpenBSD__)
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
32 #ifdef __OpenBSD__
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__) */
43
44 #ifdef __APPLE__
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)
51 {
52         return ((v & 0xff) << 8) | (v >> 8);
53 }
54
55 static inline unsigned int bswap_32(unsigned int v)
56 {
57         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
58                 ((v & 0xff0000) >> 8) | (v >> 24);
59 }
60 #endif /* __APPLE__ */
61
62 #ifdef CONFIG_TI_COMPILER
63 #define __BIG_ENDIAN 4321
64 #define __LITTLE_ENDIAN 1234
65 #ifdef __big_endian__
66 #define __BYTE_ORDER __BIG_ENDIAN
67 #else
68 #define __BYTE_ORDER __LITTLE_ENDIAN
69 #endif
70 #endif /* CONFIG_TI_COMPILER */
71
72 #ifdef __SYMBIAN32__
73 #define __BIG_ENDIAN 4321
74 #define __LITTLE_ENDIAN 1234
75 #define __BYTE_ORDER __LITTLE_ENDIAN
76 #endif /* __SYMBIAN32__ */
77
78 #ifdef CONFIG_NATIVE_WINDOWS
79 #include <winsock.h>
80
81 typedef int socklen_t;
82
83 #ifndef MSG_DONTWAIT
84 #define MSG_DONTWAIT 0 /* not supported */
85 #endif
86
87 #endif /* CONFIG_NATIVE_WINDOWS */
88
89 #ifdef _MSC_VER
90 #ifndef __cplusplus
91 #define inline __inline
92 #endif
93
94 #undef vsnprintf
95 #define vsnprintf _vsnprintf
96 #undef close
97 #define close closesocket
98 #endif /* _MSC_VER */
99
100
101 /* Define platform specific integer types */
102
103 #ifdef _MSC_VER
104 typedef UINT64 u64;
105 typedef UINT32 u32;
106 typedef UINT16 u16;
107 typedef UINT8 u8;
108 typedef INT64 s64;
109 typedef INT32 s32;
110 typedef INT16 s16;
111 typedef INT8 s8;
112 #define WPA_TYPES_DEFINED
113 #endif /* _MSC_VER */
114
115 #ifdef __vxworks
116 typedef unsigned long long u64;
117 typedef UINT32 u32;
118 typedef UINT16 u16;
119 typedef UINT8 u8;
120 typedef long long s64;
121 typedef INT32 s32;
122 typedef INT16 s16;
123 typedef INT8 s8;
124 #define WPA_TYPES_DEFINED
125 #endif /* __vxworks */
126
127 #ifdef CONFIG_TI_COMPILER
128 #ifdef _LLONG_AVAILABLE
129 typedef unsigned long long u64;
130 #else
131 /*
132  * TODO: 64-bit variable not available. Using long as a workaround to test the
133  * build, but this will likely not work for all operations.
134  */
135 typedef unsigned long u64;
136 #endif
137 typedef unsigned int u32;
138 typedef unsigned short u16;
139 typedef unsigned char u8;
140 #define WPA_TYPES_DEFINED
141 #endif /* CONFIG_TI_COMPILER */
142
143 #ifdef __SYMBIAN32__
144 #define __REMOVE_PLATSEC_DIAGNOSTICS__
145 #include <e32def.h>
146 typedef TUint64 u64;
147 typedef TUint32 u32;
148 typedef TUint16 u16;
149 typedef TUint8 u8;
150 #define WPA_TYPES_DEFINED
151 #endif /* __SYMBIAN32__ */
152
153 #ifndef WPA_TYPES_DEFINED
154 #ifdef CONFIG_USE_INTTYPES_H
155 #include <inttypes.h>
156 #else
157 #include <stdint.h>
158 #endif
159 typedef uint64_t u64;
160 typedef uint32_t u32;
161 typedef uint16_t u16;
162 typedef uint8_t u8;
163 typedef int64_t s64;
164 typedef int32_t s32;
165 typedef int16_t s16;
166 typedef int8_t s8;
167 #define WPA_TYPES_DEFINED
168 #endif /* !WPA_TYPES_DEFINED */
169
170
171 /* Define platform specific byte swapping macros */
172
173 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
174
175 static inline unsigned short wpa_swap_16(unsigned short v)
176 {
177         return ((v & 0xff) << 8) | (v >> 8);
178 }
179
180 static inline unsigned int wpa_swap_32(unsigned int v)
181 {
182         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
183                 ((v & 0xff0000) >> 8) | (v >> 24);
184 }
185
186 #define le_to_host16(n) (n)
187 #define host_to_le16(n) (n)
188 #define be_to_host16(n) wpa_swap_16(n)
189 #define host_to_be16(n) wpa_swap_16(n)
190 #define le_to_host32(n) (n)
191 #define be_to_host32(n) wpa_swap_32(n)
192 #define host_to_be32(n) wpa_swap_32(n)
193
194 #define WPA_BYTE_SWAP_DEFINED
195
196 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
197
198
199 #ifndef WPA_BYTE_SWAP_DEFINED
200
201 #ifndef __BYTE_ORDER
202 #ifndef __LITTLE_ENDIAN
203 #ifndef __BIG_ENDIAN
204 #define __LITTLE_ENDIAN 1234
205 #define __BIG_ENDIAN 4321
206 #if defined(sparc)
207 #define __BYTE_ORDER __BIG_ENDIAN
208 #endif
209 #endif /* __BIG_ENDIAN */
210 #endif /* __LITTLE_ENDIAN */
211 #endif /* __BYTE_ORDER */
212
213 #if __BYTE_ORDER == __LITTLE_ENDIAN
214 #define le_to_host16(n) ((__force u16) (le16) (n))
215 #define host_to_le16(n) ((__force le16) (u16) (n))
216 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
217 #define host_to_be16(n) ((__force be16) bswap_16((n)))
218 #define le_to_host32(n) ((__force u32) (le32) (n))
219 #define host_to_le32(n) ((__force le32) (u32) (n))
220 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
221 #define host_to_be32(n) ((__force be32) bswap_32((n)))
222 #define le_to_host64(n) ((__force u64) (le64) (n))
223 #define host_to_le64(n) ((__force le64) (u64) (n))
224 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
225 #define host_to_be64(n) ((__force be64) bswap_64((n)))
226 #elif __BYTE_ORDER == __BIG_ENDIAN
227 #define le_to_host16(n) bswap_16(n)
228 #define host_to_le16(n) bswap_16(n)
229 #define be_to_host16(n) (n)
230 #define host_to_be16(n) (n)
231 #define le_to_host32(n) bswap_32(n)
232 #define be_to_host32(n) (n)
233 #define host_to_be32(n) (n)
234 #define le_to_host64(n) bswap_64(n)
235 #define host_to_le64(n) bswap_64(n)
236 #define be_to_host64(n) (n)
237 #define host_to_be64(n) (n)
238 #ifndef WORDS_BIGENDIAN
239 #define WORDS_BIGENDIAN
240 #endif
241 #else
242 #error Could not determine CPU byte order
243 #endif
244
245 #define WPA_BYTE_SWAP_DEFINED
246 #endif /* !WPA_BYTE_SWAP_DEFINED */
247
248
249 /* Macros for handling unaligned memory accesses */
250
251 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
252 #define WPA_PUT_BE16(a, val)                    \
253         do {                                    \
254                 (a)[0] = ((u16) (val)) >> 8;    \
255                 (a)[1] = ((u16) (val)) & 0xff;  \
256         } while (0)
257
258 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
259 #define WPA_PUT_LE16(a, val)                    \
260         do {                                    \
261                 (a)[1] = ((u16) (val)) >> 8;    \
262                 (a)[0] = ((u16) (val)) & 0xff;  \
263         } while (0)
264
265 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
266                          ((u32) (a)[2]))
267 #define WPA_PUT_BE24(a, val)                                    \
268         do {                                                    \
269                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
270                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
271                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
272         } while (0)
273
274 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
275                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
276 #define WPA_PUT_BE32(a, val)                                    \
277         do {                                                    \
278                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
279                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
280                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
281                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
282         } while (0)
283
284 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
285                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
286 #define WPA_PUT_LE32(a, val)                                    \
287         do {                                                    \
288                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
289                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
290                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
291                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
292         } while (0)
293
294 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
295                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
296                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
297                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
298 #define WPA_PUT_BE64(a, val)                            \
299         do {                                            \
300                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
301                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
302                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
303                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
304                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
305                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
306                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
307                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
308         } while (0)
309
310 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
311                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
312                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
313                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
314
315
316 #ifndef ETH_ALEN
317 #define ETH_ALEN 6
318 #endif
319 #ifndef IFNAMSIZ
320 #define IFNAMSIZ 16
321 #endif
322 #ifndef ETH_P_ALL
323 #define ETH_P_ALL 0x0003
324 #endif
325 #ifndef ETH_P_PAE
326 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
327 #endif /* ETH_P_PAE */
328 #ifndef ETH_P_EAPOL
329 #define ETH_P_EAPOL ETH_P_PAE
330 #endif /* ETH_P_EAPOL */
331 #ifndef ETH_P_RSN_PREAUTH
332 #define ETH_P_RSN_PREAUTH 0x88c7
333 #endif /* ETH_P_RSN_PREAUTH */
334 #ifndef ETH_P_RRB
335 #define ETH_P_RRB 0x890D
336 #endif /* ETH_P_RRB */
337
338
339 #ifdef __GNUC__
340 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
341 #define STRUCT_PACKED __attribute__ ((packed))
342 #define UNUSED __attribute__ ((unused))
343 #else
344 #define PRINTF_FORMAT(a,b)
345 #define STRUCT_PACKED
346 #define UNUSED
347 #endif
348
349
350 #ifdef CONFIG_ANSI_C_EXTRA
351
352 #if !defined(_MSC_VER) || _MSC_VER < 1400
353 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
354  * due to possible buffer overflow; see, e.g.,
355  * http://www.ijs.si/software/snprintf/ for portable implementation of
356  * snprintf. */
357 int snprintf(char *str, size_t size, const char *format, ...);
358
359 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
360 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
361 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
362
363 /* getopt - only used in main.c */
364 int getopt(int argc, char *const argv[], const char *optstring);
365 extern char *optarg;
366 extern int optind;
367
368 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
369 #ifndef __socklen_t_defined
370 typedef int socklen_t;
371 #endif
372 #endif
373
374 /* inline - define as __inline or just define it to be empty, if needed */
375 #ifdef CONFIG_NO_INLINE
376 #define inline
377 #else
378 #define inline __inline
379 #endif
380
381 #ifndef __func__
382 #define __func__ "__func__ not defined"
383 #endif
384
385 #ifndef bswap_16
386 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
387 #endif
388
389 #ifndef bswap_32
390 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
391                      (((u32) (a) << 8) & 0xff0000) | \
392                      (((u32) (a) >> 8) & 0xff00) | \
393                      (((u32) (a) >> 24) & 0xff))
394 #endif
395
396 #ifndef MSG_DONTWAIT
397 #define MSG_DONTWAIT 0
398 #endif
399
400 #ifdef _WIN32_WCE
401 void perror(const char *s);
402 #endif /* _WIN32_WCE */
403
404 #endif /* CONFIG_ANSI_C_EXTRA */
405
406 #ifndef MAC2STR
407 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
408 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
409 #endif
410
411 #ifndef BIT
412 #define BIT(x) (1 << (x))
413 #endif
414
415 /*
416  * Definitions for sparse validation
417  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
418  */
419 #ifdef __CHECKER__
420 #define __force __attribute__((force))
421 #define __bitwise __attribute__((bitwise))
422 #else
423 #define __force
424 #define __bitwise
425 #endif
426
427 typedef u16 __bitwise be16;
428 typedef u16 __bitwise le16;
429 typedef u32 __bitwise be32;
430 typedef u32 __bitwise le32;
431 typedef u64 __bitwise be64;
432 typedef u64 __bitwise le64;
433
434 #ifndef __must_check
435 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
436 #define __must_check __attribute__((__warn_unused_result__))
437 #else
438 #define __must_check
439 #endif /* __GNUC__ */
440 #endif /* __must_check */
441
442 int hwaddr_aton(const char *txt, u8 *addr);
443 int hwaddr_aton2(const char *txt, u8 *addr);
444 int hexstr2bin(const char *hex, u8 *buf, size_t len);
445 void inc_byte_array(u8 *counter, size_t len);
446 void wpa_get_ntp_timestamp(u8 *buf);
447 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
448 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
449                                size_t len);
450
451 #ifdef CONFIG_NATIVE_WINDOWS
452 void wpa_unicode2ascii_inplace(TCHAR *str);
453 TCHAR * wpa_strdup_tchar(const char *str);
454 #else /* CONFIG_NATIVE_WINDOWS */
455 #define wpa_unicode2ascii_inplace(s) do { } while (0)
456 #define wpa_strdup_tchar(s) strdup((s))
457 #endif /* CONFIG_NATIVE_WINDOWS */
458
459 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
460
461 static inline int is_zero_ether_addr(const u8 *a)
462 {
463         return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
464 }
465
466 #include "wpa_debug.h"
467
468
469 /*
470  * gcc 4.4 ends up generating strict-aliasing warnings about some very common
471  * networking socket uses that do not really result in a real problem and
472  * cannot be easily avoided with union-based type-punning due to struct
473  * definitions including another struct in system header files. To avoid having
474  * to fully disable strict-aliasing warnings, provide a mechanism to hide the
475  * typecast from aliasing for now. A cleaner solution will hopefully be found
476  * in the future to handle these cases.
477  */
478 void * __hide_aliasing_typecast(void *foo);
479 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
480
481 #endif /* COMMON_H */