]>
git.ipfire.org Git - thirdparty/hostap.git/blob - src/utils/common.h
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #if defined(__linux__) || defined(__GLIBC__)
17 #endif /* __linux__ */
19 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
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
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__) */
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
)
46 return ((v
& 0xff) << 8) | (v
>> 8);
49 static inline unsigned int bswap_32(unsigned int v
)
51 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
52 ((v
& 0xff0000) >> 8) | (v
>> 24);
54 #endif /* __APPLE__ */
56 #ifdef CONFIG_TI_COMPILER
57 #define __BIG_ENDIAN 4321
58 #define __LITTLE_ENDIAN 1234
60 #define __BYTE_ORDER __BIG_ENDIAN
62 #define __BYTE_ORDER __LITTLE_ENDIAN
64 #endif /* CONFIG_TI_COMPILER */
66 #ifdef CONFIG_NATIVE_WINDOWS
69 typedef int socklen_t
;
72 #define MSG_DONTWAIT 0 /* not supported */
75 #endif /* CONFIG_NATIVE_WINDOWS */
78 #define inline __inline
81 #define vsnprintf _vsnprintf
83 #define close closesocket
87 /* Define platform specific integer types */
98 #define WPA_TYPES_DEFINED
102 typedef unsigned long long u64
;
106 typedef long long s64
;
110 #define WPA_TYPES_DEFINED
111 #endif /* __vxworks */
113 #ifdef CONFIG_TI_COMPILER
114 #ifdef _LLONG_AVAILABLE
115 typedef unsigned long long u64
;
118 * TODO: 64-bit variable not available. Using long as a workaround to test the
119 * build, but this will likely not work for all operations.
121 typedef unsigned long u64
;
123 typedef unsigned int u32
;
124 typedef unsigned short u16
;
125 typedef unsigned char u8
;
126 #define WPA_TYPES_DEFINED
127 #endif /* CONFIG_TI_COMPILER */
129 #ifndef WPA_TYPES_DEFINED
130 #ifdef CONFIG_USE_INTTYPES_H
131 #include <inttypes.h>
135 typedef uint64_t u64
;
136 typedef uint32_t u32
;
137 typedef uint16_t u16
;
143 #define WPA_TYPES_DEFINED
144 #endif /* !WPA_TYPES_DEFINED */
147 /* Define platform specific byte swapping macros */
149 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
151 static inline unsigned short wpa_swap_16(unsigned short v
)
153 return ((v
& 0xff) << 8) | (v
>> 8);
156 static inline unsigned int wpa_swap_32(unsigned int v
)
158 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
159 ((v
& 0xff0000) >> 8) | (v
>> 24);
162 #define le_to_host16(n) (n)
163 #define host_to_le16(n) (n)
164 #define be_to_host16(n) wpa_swap_16(n)
165 #define host_to_be16(n) wpa_swap_16(n)
166 #define le_to_host32(n) (n)
167 #define be_to_host32(n) wpa_swap_32(n)
168 #define host_to_be32(n) wpa_swap_32(n)
170 #define WPA_BYTE_SWAP_DEFINED
172 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
175 #ifndef WPA_BYTE_SWAP_DEFINED
178 #ifndef __LITTLE_ENDIAN
180 #define __LITTLE_ENDIAN 1234
181 #define __BIG_ENDIAN 4321
183 #define __BYTE_ORDER __BIG_ENDIAN
185 #endif /* __BIG_ENDIAN */
186 #endif /* __LITTLE_ENDIAN */
187 #endif /* __BYTE_ORDER */
189 #if __BYTE_ORDER == __LITTLE_ENDIAN
190 #define le_to_host16(n) ((__force u16) (le16) (n))
191 #define host_to_le16(n) ((__force le16) (u16) (n))
192 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
193 #define host_to_be16(n) ((__force be16) bswap_16((n)))
194 #define le_to_host32(n) ((__force u32) (le32) (n))
195 #define host_to_le32(n) ((__force le32) (u32) (n))
196 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
197 #define host_to_be32(n) ((__force be32) bswap_32((n)))
198 #define le_to_host64(n) ((__force u64) (le64) (n))
199 #define host_to_le64(n) ((__force le64) (u64) (n))
200 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
201 #define host_to_be64(n) ((__force be64) bswap_64((n)))
202 #elif __BYTE_ORDER == __BIG_ENDIAN
203 #define le_to_host16(n) bswap_16(n)
204 #define host_to_le16(n) bswap_16(n)
205 #define be_to_host16(n) (n)
206 #define host_to_be16(n) (n)
207 #define le_to_host32(n) bswap_32(n)
208 #define host_to_le32(n) bswap_32(n)
209 #define be_to_host32(n) (n)
210 #define host_to_be32(n) (n)
211 #define le_to_host64(n) bswap_64(n)
212 #define host_to_le64(n) bswap_64(n)
213 #define be_to_host64(n) (n)
214 #define host_to_be64(n) (n)
215 #ifndef WORDS_BIGENDIAN
216 #define WORDS_BIGENDIAN
219 #error Could not determine CPU byte order
222 #define WPA_BYTE_SWAP_DEFINED
223 #endif /* !WPA_BYTE_SWAP_DEFINED */
226 /* Macros for handling unaligned memory accesses */
228 static inline u16
WPA_GET_BE16(const u8
*a
)
230 return (a
[0] << 8) | a
[1];
233 static inline void WPA_PUT_BE16(u8
*a
, u16 val
)
239 static inline u16
WPA_GET_LE16(const u8
*a
)
241 return (a
[1] << 8) | a
[0];
244 static inline void WPA_PUT_LE16(u8
*a
, u16 val
)
250 static inline u32
WPA_GET_BE24(const u8
*a
)
252 return (a
[0] << 16) | (a
[1] << 8) | a
[2];
255 static inline void WPA_PUT_BE24(u8
*a
, u32 val
)
257 a
[0] = (val
>> 16) & 0xff;
258 a
[1] = (val
>> 8) & 0xff;
262 static inline u32
WPA_GET_BE32(const u8
*a
)
264 return (a
[0] << 24) | (a
[1] << 16) | (a
[2] << 8) | a
[3];
267 static inline void WPA_PUT_BE32(u8
*a
, u32 val
)
269 a
[0] = (val
>> 24) & 0xff;
270 a
[1] = (val
>> 16) & 0xff;
271 a
[2] = (val
>> 8) & 0xff;
275 static inline u32
WPA_GET_LE32(const u8
*a
)
277 return (a
[3] << 24) | (a
[2] << 16) | (a
[1] << 8) | a
[0];
280 static inline void WPA_PUT_LE32(u8
*a
, u32 val
)
282 a
[3] = (val
>> 24) & 0xff;
283 a
[2] = (val
>> 16) & 0xff;
284 a
[1] = (val
>> 8) & 0xff;
288 static inline u64
WPA_GET_BE64(const u8
*a
)
290 return (((u64
) a
[0]) << 56) | (((u64
) a
[1]) << 48) |
291 (((u64
) a
[2]) << 40) | (((u64
) a
[3]) << 32) |
292 (((u64
) a
[4]) << 24) | (((u64
) a
[5]) << 16) |
293 (((u64
) a
[6]) << 8) | ((u64
) a
[7]);
296 static inline void WPA_PUT_BE64(u8
*a
, u64 val
)
308 static inline u64
WPA_GET_LE64(const u8
*a
)
310 return (((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]);
316 static inline void WPA_PUT_LE64(u8
*a
, u64 val
)
336 #define ETH_P_ALL 0x0003
338 #ifndef ETH_P_80211_ENCAP
339 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
342 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
343 #endif /* ETH_P_PAE */
345 #define ETH_P_EAPOL ETH_P_PAE
346 #endif /* ETH_P_EAPOL */
347 #ifndef ETH_P_RSN_PREAUTH
348 #define ETH_P_RSN_PREAUTH 0x88c7
349 #endif /* ETH_P_RSN_PREAUTH */
351 #define ETH_P_RRB 0x890D
352 #endif /* ETH_P_RRB */
356 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
357 #define STRUCT_PACKED __attribute__ ((packed))
359 #define PRINTF_FORMAT(a,b)
360 #define STRUCT_PACKED
364 #ifdef CONFIG_ANSI_C_EXTRA
366 #if !defined(_MSC_VER) || _MSC_VER < 1400
367 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
368 * due to possible buffer overflow; see, e.g.,
369 * http://www.ijs.si/software/snprintf/ for portable implementation of
371 int snprintf(char *str
, size_t size
, const char *format
, ...);
373 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
374 int vsnprintf(char *str
, size_t size
, const char *format
, va_list ap
);
375 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
377 /* getopt - only used in main.c */
378 int getopt(int argc
, char *const argv
[], const char *optstring
);
382 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
383 #ifndef __socklen_t_defined
384 typedef int socklen_t
;
388 /* inline - define as __inline or just define it to be empty, if needed */
389 #ifdef CONFIG_NO_INLINE
392 #define inline __inline
396 #define __func__ "__func__ not defined"
400 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
404 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
405 (((u32) (a) << 8) & 0xff0000) | \
406 (((u32) (a) >> 8) & 0xff00) | \
407 (((u32) (a) >> 24) & 0xff))
411 #define MSG_DONTWAIT 0
415 void perror(const char *s
);
416 #endif /* _WIN32_WCE */
418 #endif /* CONFIG_ANSI_C_EXTRA */
421 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
422 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
425 * Compact form for string representation of MAC address
426 * To be used, e.g., for constructing dbus paths for P2P Devices
428 #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
432 #define BIT(x) (1 << (x))
436 * Definitions for sparse validation
437 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
440 #define __force __attribute__((force))
441 #define __bitwise __attribute__((bitwise))
447 typedef u16 __bitwise be16
;
448 typedef u16 __bitwise le16
;
449 typedef u32 __bitwise be32
;
450 typedef u32 __bitwise le32
;
451 typedef u64 __bitwise be64
;
452 typedef u64 __bitwise le64
;
455 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
456 #define __must_check __attribute__((__warn_unused_result__))
459 #endif /* __GNUC__ */
460 #endif /* __must_check */
462 int hwaddr_aton(const char *txt
, u8
*addr
);
463 int hwaddr_compact_aton(const char *txt
, u8
*addr
);
464 int hwaddr_aton2(const char *txt
, u8
*addr
);
465 int hex2byte(const char *hex
);
466 int hexstr2bin(const char *hex
, u8
*buf
, size_t len
);
467 void inc_byte_array(u8
*counter
, size_t len
);
468 void wpa_get_ntp_timestamp(u8
*buf
);
469 int wpa_snprintf_hex(char *buf
, size_t buf_size
, const u8
*data
, size_t len
);
470 int wpa_snprintf_hex_uppercase(char *buf
, size_t buf_size
, const u8
*data
,
473 #ifdef CONFIG_NATIVE_WINDOWS
474 void wpa_unicode2ascii_inplace(TCHAR
*str
);
475 TCHAR
* wpa_strdup_tchar(const char *str
);
476 #else /* CONFIG_NATIVE_WINDOWS */
477 #define wpa_unicode2ascii_inplace(s) do { } while (0)
478 #define wpa_strdup_tchar(s) strdup((s))
479 #endif /* CONFIG_NATIVE_WINDOWS */
481 void printf_encode(char *txt
, size_t maxlen
, const u8
*data
, size_t len
);
482 size_t printf_decode(u8
*buf
, size_t maxlen
, const char *str
);
484 const char * wpa_ssid_txt(const u8
*ssid
, size_t ssid_len
);
486 char * wpa_config_parse_string(const char *value
, size_t *len
);
487 int is_hex(const u8
*data
, size_t len
);
488 size_t merge_byte_arrays(u8
*res
, size_t res_len
,
489 const u8
*src1
, size_t src1_len
,
490 const u8
*src2
, size_t src2_len
);
491 char * dup_binstr(const void *src
, size_t len
);
493 static inline int is_zero_ether_addr(const u8
*a
)
495 return !(a
[0] | a
[1] | a
[2] | a
[3] | a
[4] | a
[5]);
498 static inline int is_broadcast_ether_addr(const u8
*a
)
500 return (a
[0] & a
[1] & a
[2] & a
[3] & a
[4] & a
[5]) == 0xff;
503 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
505 #include "wpa_debug.h"
508 struct wpa_freq_range_list
{
509 struct wpa_freq_range
{
516 int freq_range_list_parse(struct wpa_freq_range_list
*res
, const char *value
);
517 int freq_range_list_includes(const struct wpa_freq_range_list
*list
,
519 char * freq_range_list_str(const struct wpa_freq_range_list
*list
);
521 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
525 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
526 * networking socket uses that do not really result in a real problem and
527 * cannot be easily avoided with union-based type-punning due to struct
528 * definitions including another struct in system header files. To avoid having
529 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
530 * typecast from aliasing for now. A cleaner solution will hopefully be found
531 * in the future to handle these cases.
533 void * __hide_aliasing_typecast(void *foo
);
534 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
536 #ifdef CONFIG_VALGRIND
537 #include <valgrind/memcheck.h>
538 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
539 #else /* CONFIG_VALGRIND */
540 #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
541 #endif /* CONFIG_VALGRIND */
543 #endif /* COMMON_H */