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