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