Updated to hostap_2_6
[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 software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #ifndef COMMON_H
10 #define COMMON_H
11
12 #include "os.h"
13
14 #if defined(__linux__) || defined(__GLIBC__)
15 #include <endian.h>
16 #include <byteswap.h>
17 #endif /* __linux__ */
18
19 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
20     defined(__OpenBSD__)
21 #include <sys/types.h>
22 #include <sys/endian.h>
23 #define __BYTE_ORDER    _BYTE_ORDER
24 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
25 #define __BIG_ENDIAN    _BIG_ENDIAN
26 #ifdef __OpenBSD__
27 #define bswap_16 swap16
28 #define bswap_32 swap32
29 #define bswap_64 swap64
30 #else /* __OpenBSD__ */
31 #define bswap_16 bswap16
32 #define bswap_32 bswap32
33 #define bswap_64 bswap64
34 #endif /* __OpenBSD__ */
35 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
36         * defined(__DragonFly__) || defined(__OpenBSD__) */
37
38 #ifdef __APPLE__
39 #include <sys/types.h>
40 #include <machine/endian.h>
41 #define __BYTE_ORDER    _BYTE_ORDER
42 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
43 #define __BIG_ENDIAN    _BIG_ENDIAN
44 static inline unsigned short bswap_16(unsigned short v)
45 {
46         return ((v & 0xff) << 8) | (v >> 8);
47 }
48
49 static inline unsigned int bswap_32(unsigned int v)
50 {
51         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
52                 ((v & 0xff0000) >> 8) | (v >> 24);
53 }
54 #endif /* __APPLE__ */
55
56 #ifdef CONFIG_NATIVE_WINDOWS
57 #ifdef CONFIG_IPV6
58 #include <winsock2.h>
59 #include <ws2tcpip.h>
60 #else
61 #include <winsock.h>
62 #endif
63
64 typedef int socklen_t;
65
66 #ifndef MSG_DONTWAIT
67 #define MSG_DONTWAIT 0 /* not supported */
68 #endif
69
70 #endif /* CONFIG_NATIVE_WINDOWS */
71
72 #ifdef _MSC_VER
73 #ifndef __cplusplus
74 #define inline __inline
75 #endif
76
77 #undef vsnprintf
78 #define vsnprintf _vsnprintf
79 #undef close
80 #define close closesocket
81 #endif /* _MSC_VER */
82
83
84 /* Define platform specific integer types */
85
86 #ifdef _MSC_VER
87 typedef UINT64 u64;
88 typedef UINT32 u32;
89 typedef UINT16 u16;
90 typedef UINT8 u8;
91 typedef INT64 s64;
92 typedef INT32 s32;
93 typedef INT16 s16;
94 typedef INT8 s8;
95 #define WPA_TYPES_DEFINED
96 #endif /* _MSC_VER */
97
98 #ifdef __vxworks
99 typedef unsigned long long u64;
100 typedef UINT32 u32;
101 typedef UINT16 u16;
102 typedef UINT8 u8;
103 typedef long long s64;
104 typedef INT32 s32;
105 typedef INT16 s16;
106 typedef INT8 s8;
107 #define WPA_TYPES_DEFINED
108 #endif /* __vxworks */
109
110 #ifndef WPA_TYPES_DEFINED
111 #ifdef CONFIG_USE_INTTYPES_H
112 #include <inttypes.h>
113 #else
114 #include <stdint.h>
115 #endif
116 typedef uint64_t u64;
117 typedef uint32_t u32;
118 typedef uint16_t u16;
119 typedef uint8_t u8;
120 typedef int64_t s64;
121 typedef int32_t s32;
122 typedef int16_t s16;
123 typedef int8_t s8;
124 #define WPA_TYPES_DEFINED
125 #endif /* !WPA_TYPES_DEFINED */
126
127
128 /* Define platform specific byte swapping macros */
129
130 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
131
132 static inline unsigned short wpa_swap_16(unsigned short v)
133 {
134         return ((v & 0xff) << 8) | (v >> 8);
135 }
136
137 static inline unsigned int wpa_swap_32(unsigned int v)
138 {
139         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
140                 ((v & 0xff0000) >> 8) | (v >> 24);
141 }
142
143 #define le_to_host16(n) (n)
144 #define host_to_le16(n) (n)
145 #define be_to_host16(n) wpa_swap_16(n)
146 #define host_to_be16(n) wpa_swap_16(n)
147 #define le_to_host32(n) (n)
148 #define host_to_le32(n) (n)
149 #define be_to_host32(n) wpa_swap_32(n)
150 #define host_to_be32(n) wpa_swap_32(n)
151
152 #define WPA_BYTE_SWAP_DEFINED
153
154 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
155
156
157 #ifndef WPA_BYTE_SWAP_DEFINED
158
159 #ifndef __BYTE_ORDER
160 #ifndef __LITTLE_ENDIAN
161 #ifndef __BIG_ENDIAN
162 #define __LITTLE_ENDIAN 1234
163 #define __BIG_ENDIAN 4321
164 #if defined(sparc)
165 #define __BYTE_ORDER __BIG_ENDIAN
166 #endif
167 #endif /* __BIG_ENDIAN */
168 #endif /* __LITTLE_ENDIAN */
169 #endif /* __BYTE_ORDER */
170
171 #if __BYTE_ORDER == __LITTLE_ENDIAN
172 #define le_to_host16(n) ((__force u16) (le16) (n))
173 #define host_to_le16(n) ((__force le16) (u16) (n))
174 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
175 #define host_to_be16(n) ((__force be16) bswap_16((n)))
176 #define le_to_host32(n) ((__force u32) (le32) (n))
177 #define host_to_le32(n) ((__force le32) (u32) (n))
178 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
179 #define host_to_be32(n) ((__force be32) bswap_32((n)))
180 #define le_to_host64(n) ((__force u64) (le64) (n))
181 #define host_to_le64(n) ((__force le64) (u64) (n))
182 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
183 #define host_to_be64(n) ((__force be64) bswap_64((n)))
184 #elif __BYTE_ORDER == __BIG_ENDIAN
185 #define le_to_host16(n) bswap_16(n)
186 #define host_to_le16(n) bswap_16(n)
187 #define be_to_host16(n) (n)
188 #define host_to_be16(n) (n)
189 #define le_to_host32(n) bswap_32(n)
190 #define host_to_le32(n) bswap_32(n)
191 #define be_to_host32(n) (n)
192 #define host_to_be32(n) (n)
193 #define le_to_host64(n) bswap_64(n)
194 #define host_to_le64(n) bswap_64(n)
195 #define be_to_host64(n) (n)
196 #define host_to_be64(n) (n)
197 #ifndef WORDS_BIGENDIAN
198 #define WORDS_BIGENDIAN
199 #endif
200 #else
201 #error Could not determine CPU byte order
202 #endif
203
204 #define WPA_BYTE_SWAP_DEFINED
205 #endif /* !WPA_BYTE_SWAP_DEFINED */
206
207
208 /* Macros for handling unaligned memory accesses */
209
210 static inline u16 WPA_GET_BE16(const u8 *a)
211 {
212         return (a[0] << 8) | a[1];
213 }
214
215 static inline void WPA_PUT_BE16(u8 *a, u16 val)
216 {
217         a[0] = val >> 8;
218         a[1] = val & 0xff;
219 }
220
221 static inline u16 WPA_GET_LE16(const u8 *a)
222 {
223         return (a[1] << 8) | a[0];
224 }
225
226 static inline void WPA_PUT_LE16(u8 *a, u16 val)
227 {
228         a[1] = val >> 8;
229         a[0] = val & 0xff;
230 }
231
232 static inline u32 WPA_GET_BE24(const u8 *a)
233 {
234         return (a[0] << 16) | (a[1] << 8) | a[2];
235 }
236
237 static inline void WPA_PUT_BE24(u8 *a, u32 val)
238 {
239         a[0] = (val >> 16) & 0xff;
240         a[1] = (val >> 8) & 0xff;
241         a[2] = val & 0xff;
242 }
243
244 static inline u32 WPA_GET_BE32(const u8 *a)
245 {
246         return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
247 }
248
249 static inline void WPA_PUT_BE32(u8 *a, u32 val)
250 {
251         a[0] = (val >> 24) & 0xff;
252         a[1] = (val >> 16) & 0xff;
253         a[2] = (val >> 8) & 0xff;
254         a[3] = val & 0xff;
255 }
256
257 static inline u32 WPA_GET_LE32(const u8 *a)
258 {
259         return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
260 }
261
262 static inline void WPA_PUT_LE32(u8 *a, u32 val)
263 {
264         a[3] = (val >> 24) & 0xff;
265         a[2] = (val >> 16) & 0xff;
266         a[1] = (val >> 8) & 0xff;
267         a[0] = val & 0xff;
268 }
269
270 static inline u64 WPA_GET_BE64(const u8 *a)
271 {
272         return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
273                 (((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
274                 (((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
275                 (((u64) a[6]) << 8) | ((u64) a[7]);
276 }
277
278 static inline void WPA_PUT_BE64(u8 *a, u64 val)
279 {
280         a[0] = val >> 56;
281         a[1] = val >> 48;
282         a[2] = val >> 40;
283         a[3] = val >> 32;
284         a[4] = val >> 24;
285         a[5] = val >> 16;
286         a[6] = val >> 8;
287         a[7] = val & 0xff;
288 }
289
290 static inline u64 WPA_GET_LE64(const u8 *a)
291 {
292         return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
293                 (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
294                 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
295                 (((u64) a[1]) << 8) | ((u64) a[0]);
296 }
297
298 static inline void WPA_PUT_LE64(u8 *a, u64 val)
299 {
300         a[7] = val >> 56;
301         a[6] = val >> 48;
302         a[5] = val >> 40;
303         a[4] = val >> 32;
304         a[3] = val >> 24;
305         a[2] = val >> 16;
306         a[1] = val >> 8;
307         a[0] = val & 0xff;
308 }
309
310
311 #ifndef ETH_ALEN
312 #define ETH_ALEN 6
313 #endif
314 #ifndef ETH_HLEN
315 #define ETH_HLEN 14
316 #endif
317 #ifndef IFNAMSIZ
318 #define IFNAMSIZ 16
319 #endif
320 #ifndef ETH_P_ALL
321 #define ETH_P_ALL 0x0003
322 #endif
323 #ifndef ETH_P_IP
324 #define ETH_P_IP 0x0800
325 #endif
326 #ifndef ETH_P_80211_ENCAP
327 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
328 #endif
329 #ifndef ETH_P_PAE
330 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
331 #endif /* ETH_P_PAE */
332 #ifndef ETH_P_EAPOL
333 #define ETH_P_EAPOL ETH_P_PAE
334 #endif /* ETH_P_EAPOL */
335 #ifndef ETH_P_RSN_PREAUTH
336 #define ETH_P_RSN_PREAUTH 0x88c7
337 #endif /* ETH_P_RSN_PREAUTH */
338 #ifndef ETH_P_RRB
339 #define ETH_P_RRB 0x890D
340 #endif /* ETH_P_RRB */
341
342
343 #ifdef __GNUC__
344 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
345 #define STRUCT_PACKED __attribute__ ((packed))
346 #define UNUSED __attribute__ ((unused))
347 #else
348 #define PRINTF_FORMAT(a,b)
349 #define STRUCT_PACKED
350 #define UNUSED
351 #endif
352
353
354 #ifdef CONFIG_ANSI_C_EXTRA
355
356 #if !defined(_MSC_VER) || _MSC_VER < 1400
357 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
358  * due to possible buffer overflow; see, e.g.,
359  * http://www.ijs.si/software/snprintf/ for portable implementation of
360  * snprintf. */
361 int snprintf(char *str, size_t size, const char *format, ...);
362
363 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
364 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
365 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
366
367 /* getopt - only used in main.c */
368 int getopt(int argc, char *const argv[], const char *optstring);
369 extern char *optarg;
370 extern int optind;
371
372 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
373 #ifndef __socklen_t_defined
374 typedef int socklen_t;
375 #endif
376 #endif
377
378 /* inline - define as __inline or just define it to be empty, if needed */
379 #ifdef CONFIG_NO_INLINE
380 #define inline
381 #else
382 #define inline __inline
383 #endif
384
385 #ifndef __func__
386 #define __func__ "__func__ not defined"
387 #endif
388
389 #ifndef bswap_16
390 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
391 #endif
392
393 #ifndef bswap_32
394 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
395                      (((u32) (a) << 8) & 0xff0000) | \
396                      (((u32) (a) >> 8) & 0xff00) | \
397                      (((u32) (a) >> 24) & 0xff))
398 #endif
399
400 #ifndef MSG_DONTWAIT
401 #define MSG_DONTWAIT 0
402 #endif
403
404 #ifdef _WIN32_WCE
405 void perror(const char *s);
406 #endif /* _WIN32_WCE */
407
408 #endif /* CONFIG_ANSI_C_EXTRA */
409
410 #ifndef MAC2STR
411 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
412 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
413
414 /*
415  * Compact form for string representation of MAC address
416  * To be used, e.g., for constructing dbus paths for P2P Devices
417  */
418 #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
419 #endif
420
421 #ifndef BIT
422 #define BIT(x) (1U << (x))
423 #endif
424
425 /*
426  * Definitions for sparse validation
427  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
428  */
429 #ifdef __CHECKER__
430 #define __force __attribute__((force))
431 #undef __bitwise
432 #define __bitwise __attribute__((bitwise))
433 #else
434 #define __force
435 #define __bitwise
436 #endif
437
438 typedef u16 __bitwise be16;
439 typedef u16 __bitwise le16;
440 typedef u32 __bitwise be32;
441 typedef u32 __bitwise le32;
442 typedef u64 __bitwise be64;
443 typedef u64 __bitwise le64;
444
445 #ifndef __must_check
446 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
447 #define __must_check __attribute__((__warn_unused_result__))
448 #else
449 #define __must_check
450 #endif /* __GNUC__ */
451 #endif /* __must_check */
452
453 #ifndef __maybe_unused
454 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
455 #define __maybe_unused __attribute__((unused))
456 #else
457 #define __maybe_unused
458 #endif /* __GNUC__ */
459 #endif /* __must_check */
460
461 #define SSID_MAX_LEN 32
462
463 struct wpa_ssid_value {
464         u8 ssid[SSID_MAX_LEN];
465         size_t ssid_len;
466 };
467
468 int hwaddr_aton(const char *txt, u8 *addr);
469 int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
470 int hwaddr_compact_aton(const char *txt, u8 *addr);
471 int hwaddr_aton2(const char *txt, u8 *addr);
472 int hex2byte(const char *hex);
473 int hexstr2bin(const char *hex, u8 *buf, size_t len);
474 void inc_byte_array(u8 *counter, size_t len);
475 void wpa_get_ntp_timestamp(u8 *buf);
476 int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...);
477 int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
478                          char sep);
479 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
480 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
481                                size_t len);
482
483 int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
484 int ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
485
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 */
493
494 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
495 size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
496
497 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
498
499 char * wpa_config_parse_string(const char *value, size_t *len);
500 int is_hex(const u8 *data, size_t len);
501 int has_ctrl_char(const u8 *data, size_t len);
502 int has_newline(const char *str);
503 size_t merge_byte_arrays(u8 *res, size_t res_len,
504                          const u8 *src1, size_t src1_len,
505                          const u8 *src2, size_t src2_len);
506 char * dup_binstr(const void *src, size_t len);
507
508 static inline int is_zero_ether_addr(const u8 *a)
509 {
510         return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
511 }
512
513 static inline int is_broadcast_ether_addr(const u8 *a)
514 {
515         return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
516 }
517
518 static inline int is_multicast_ether_addr(const u8 *a)
519 {
520         return a[0] & 0x01;
521 }
522
523 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
524
525 #include "wpa_debug.h"
526
527
528 struct wpa_freq_range_list {
529         struct wpa_freq_range {
530                 unsigned int min;
531                 unsigned int max;
532         } *range;
533         unsigned int num;
534 };
535
536 int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
537 int freq_range_list_includes(const struct wpa_freq_range_list *list,
538                              unsigned int freq);
539 char * freq_range_list_str(const struct wpa_freq_range_list *list);
540
541 int int_array_len(const int *a);
542 void int_array_concat(int **res, const int *a);
543 void int_array_sort_unique(int *a);
544 void int_array_add_unique(int **res, int a);
545
546 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
547
548 void str_clear_free(char *str);
549 void bin_clear_free(void *bin, size_t len);
550
551 int random_mac_addr(u8 *addr);
552 int random_mac_addr_keep_oui(u8 *addr);
553
554 const char * cstr_token(const char *str, const char *delim, const char **last);
555 char * str_token(char *str, const char *delim, char **context);
556 size_t utf8_escape(const char *inp, size_t in_size,
557                    char *outp, size_t out_size);
558 size_t utf8_unescape(const char *inp, size_t in_size,
559                      char *outp, size_t out_size);
560 int is_ctrl_char(char c);
561
562 int str_starts(const char *str, const char *start);
563
564
565 /*
566  * gcc 4.4 ends up generating strict-aliasing warnings about some very common
567  * networking socket uses that do not really result in a real problem and
568  * cannot be easily avoided with union-based type-punning due to struct
569  * definitions including another struct in system header files. To avoid having
570  * to fully disable strict-aliasing warnings, provide a mechanism to hide the
571  * typecast from aliasing for now. A cleaner solution will hopefully be found
572  * in the future to handle these cases.
573  */
574 void * __hide_aliasing_typecast(void *foo);
575 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
576
577 #ifdef CONFIG_VALGRIND
578 #include <valgrind/memcheck.h>
579 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
580 #else /* CONFIG_VALGRIND */
581 #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
582 #endif /* CONFIG_VALGRIND */
583
584 #endif /* COMMON_H */