common.h
Go to the documentation of this file.
00001 /*
00002  * wpa_supplicant/hostapd / common helper functions, etc.
00003  * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License version 2 as
00007  * published by the Free Software Foundation.
00008  *
00009  * Alternatively, this software may be distributed under the terms of BSD
00010  * license.
00011  *
00012  * See README and COPYING for more details.
00013  */
00014 
00015 #ifndef COMMON_H
00016 #define COMMON_H
00017 
00018 #include "os.h"
00019 
00020 #if defined(__linux__) || defined(__GLIBC__)
00021 #include <endian.h>
00022 #include <byteswap.h>
00023 #endif /* __linux__ */
00024 
00025 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
00026     defined(__OpenBSD__)
00027 #include <sys/types.h>
00028 #include <sys/endian.h>
00029 #define __BYTE_ORDER    _BYTE_ORDER
00030 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
00031 #define __BIG_ENDIAN    _BIG_ENDIAN
00032 #ifdef __OpenBSD__
00033 #define bswap_16 swap16
00034 #define bswap_32 swap32
00035 #define bswap_64 swap64
00036 #else /* __OpenBSD__ */
00037 #define bswap_16 bswap16
00038 #define bswap_32 bswap32
00039 #define bswap_64 bswap64
00040 #endif /* __OpenBSD__ */
00041 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
00042         * defined(__DragonFly__) || defined(__OpenBSD__) */
00043 
00044 #ifdef __APPLE__
00045 #include <sys/types.h>
00046 #include <machine/endian.h>
00047 #define __BYTE_ORDER    _BYTE_ORDER
00048 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
00049 #define __BIG_ENDIAN    _BIG_ENDIAN
00050 static inline unsigned short bswap_16(unsigned short v)
00051 {
00052         return ((v & 0xff) << 8) | (v >> 8);
00053 }
00054 
00055 static inline unsigned int bswap_32(unsigned int v)
00056 {
00057         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00058                 ((v & 0xff0000) >> 8) | (v >> 24);
00059 }
00060 #endif /* __APPLE__ */
00061 
00062 #ifdef CONFIG_TI_COMPILER
00063 #define __BIG_ENDIAN 4321
00064 #define __LITTLE_ENDIAN 1234
00065 #ifdef __big_endian__
00066 #define __BYTE_ORDER __BIG_ENDIAN
00067 #else
00068 #define __BYTE_ORDER __LITTLE_ENDIAN
00069 #endif
00070 #endif /* CONFIG_TI_COMPILER */
00071 
00072 #ifdef __SYMBIAN32__
00073 #define __BIG_ENDIAN 4321
00074 #define __LITTLE_ENDIAN 1234
00075 #define __BYTE_ORDER __LITTLE_ENDIAN
00076 #endif /* __SYMBIAN32__ */
00077 
00078 #ifdef CONFIG_NATIVE_WINDOWS
00079 #include <winsock.h>
00080 
00081 typedef int socklen_t;
00082 
00083 #ifndef MSG_DONTWAIT
00084 #define MSG_DONTWAIT 0 /* not supported */
00085 #endif
00086 
00087 #endif /* CONFIG_NATIVE_WINDOWS */
00088 
00089 #ifdef _MSC_VER
00090 #define inline __inline
00091 
00092 #undef vsnprintf
00093 #define vsnprintf _vsnprintf
00094 #undef close
00095 #define close closesocket
00096 #endif /* _MSC_VER */
00097 
00098 
00099 /* Define platform specific integer types */
00100 
00101 #ifdef _MSC_VER
00102 typedef UINT64 u64;
00103 typedef UINT32 u32;
00104 typedef UINT16 u16;
00105 typedef UINT8 u8;
00106 typedef INT64 s64;
00107 typedef INT32 s32;
00108 typedef INT16 s16;
00109 typedef INT8 s8;
00110 #define WPA_TYPES_DEFINED
00111 #endif /* _MSC_VER */
00112 
00113 #ifdef __vxworks
00114 typedef unsigned long long u64;
00115 typedef UINT32 u32;
00116 typedef UINT16 u16;
00117 typedef UINT8 u8;
00118 typedef long long s64;
00119 typedef INT32 s32;
00120 typedef INT16 s16;
00121 typedef INT8 s8;
00122 #define WPA_TYPES_DEFINED
00123 #endif /* __vxworks */
00124 
00125 #ifdef CONFIG_TI_COMPILER
00126 #ifdef _LLONG_AVAILABLE
00127 typedef unsigned long long u64;
00128 #else
00129 /*
00130  * TODO: 64-bit variable not available. Using long as a workaround to test the
00131  * build, but this will likely not work for all operations.
00132  */
00133 typedef unsigned long u64;
00134 #endif
00135 typedef unsigned int u32;
00136 typedef unsigned short u16;
00137 typedef unsigned char u8;
00138 #define WPA_TYPES_DEFINED
00139 #endif /* CONFIG_TI_COMPILER */
00140 
00141 #ifdef __SYMBIAN32__
00142 #define __REMOVE_PLATSEC_DIAGNOSTICS__
00143 #include <e32def.h>
00144 typedef TUint64 u64;
00145 typedef TUint32 u32;
00146 typedef TUint16 u16;
00147 typedef TUint8 u8;
00148 #define WPA_TYPES_DEFINED
00149 #endif /* __SYMBIAN32__ */
00150 
00151 #ifndef WPA_TYPES_DEFINED
00152 #ifdef CONFIG_USE_INTTYPES_H
00153 #include <inttypes.h>
00154 #else
00155 #include <stdint.h>
00156 #endif
00157 typedef uint64_t u64;
00158 typedef uint32_t u32;
00159 typedef uint16_t u16;
00160 typedef uint8_t u8;
00161 typedef int64_t s64;
00162 typedef int32_t s32;
00163 typedef int16_t s16;
00164 typedef int8_t s8;
00165 #define WPA_TYPES_DEFINED
00166 #endif /* !WPA_TYPES_DEFINED */
00167 
00168 
00169 /* Define platform specific byte swapping macros */
00170 
00171 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
00172 
00173 static inline unsigned short wpa_swap_16(unsigned short v)
00174 {
00175         return ((v & 0xff) << 8) | (v >> 8);
00176 }
00177 
00178 static inline unsigned int wpa_swap_32(unsigned int v)
00179 {
00180         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00181                 ((v & 0xff0000) >> 8) | (v >> 24);
00182 }
00183 
00184 #define le_to_host16(n) (n)
00185 #define host_to_le16(n) (n)
00186 #define be_to_host16(n) wpa_swap_16(n)
00187 #define host_to_be16(n) wpa_swap_16(n)
00188 #define le_to_host32(n) (n)
00189 #define be_to_host32(n) wpa_swap_32(n)
00190 #define host_to_be32(n) wpa_swap_32(n)
00191 
00192 #define WPA_BYTE_SWAP_DEFINED
00193 
00194 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
00195 
00196 
00197 #ifndef WPA_BYTE_SWAP_DEFINED
00198 
00199 #ifndef __BYTE_ORDER
00200 #ifndef __LITTLE_ENDIAN
00201 #ifndef __BIG_ENDIAN
00202 #define __LITTLE_ENDIAN 1234
00203 #define __BIG_ENDIAN 4321
00204 #if defined(sparc)
00205 #define __BYTE_ORDER __BIG_ENDIAN
00206 #endif
00207 #endif /* __BIG_ENDIAN */
00208 #endif /* __LITTLE_ENDIAN */
00209 #endif /* __BYTE_ORDER */
00210 
00211 #if __BYTE_ORDER == __LITTLE_ENDIAN
00212 #define le_to_host16(n) ((__force u16) (le16) (n))
00213 #define host_to_le16(n) ((__force le16) (u16) (n))
00214 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
00215 #define host_to_be16(n) ((__force be16) bswap_16((n)))
00216 #define le_to_host32(n) ((__force u32) (le32) (n))
00217 #define host_to_le32(n) ((__force le32) (u32) (n))
00218 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
00219 #define host_to_be32(n) ((__force be32) bswap_32((n)))
00220 #define le_to_host64(n) ((__force u64) (le64) (n))
00221 #define host_to_le64(n) ((__force le64) (u64) (n))
00222 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
00223 #define host_to_be64(n) ((__force be64) bswap_64((n)))
00224 #elif __BYTE_ORDER == __BIG_ENDIAN
00225 #define le_to_host16(n) bswap_16(n)
00226 #define host_to_le16(n) bswap_16(n)
00227 #define be_to_host16(n) (n)
00228 #define host_to_be16(n) (n)
00229 #define le_to_host32(n) bswap_32(n)
00230 #define be_to_host32(n) (n)
00231 #define host_to_be32(n) (n)
00232 #define le_to_host64(n) bswap_64(n)
00233 #define host_to_le64(n) bswap_64(n)
00234 #define be_to_host64(n) (n)
00235 #define host_to_be64(n) (n)
00236 #ifndef WORDS_BIGENDIAN
00237 #define WORDS_BIGENDIAN
00238 #endif
00239 #else
00240 #error Could not determine CPU byte order
00241 #endif
00242 
00243 #define WPA_BYTE_SWAP_DEFINED
00244 #endif /* !WPA_BYTE_SWAP_DEFINED */
00245 
00246 
00247 /* Macros for handling unaligned memory accesses */
00248 
00249 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
00250 #define WPA_PUT_BE16(a, val)                    \
00251         do {                                    \
00252                 (a)[0] = ((u16) (val)) >> 8;    \
00253                 (a)[1] = ((u16) (val)) & 0xff;  \
00254         } while (0)
00255 
00256 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
00257 #define WPA_PUT_LE16(a, val)                    \
00258         do {                                    \
00259                 (a)[1] = ((u16) (val)) >> 8;    \
00260                 (a)[0] = ((u16) (val)) & 0xff;  \
00261         } while (0)
00262 
00263 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
00264                          ((u32) (a)[2]))
00265 #define WPA_PUT_BE24(a, val)                                    \
00266         do {                                                    \
00267                 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
00268                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
00269                 (a)[2] = (u8) (((u32) (val)) & 0xff);           \
00270         } while (0)
00271 
00272 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
00273                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
00274 #define WPA_PUT_BE32(a, val)                                    \
00275         do {                                                    \
00276                 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
00277                 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
00278                 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
00279                 (a)[3] = (u8) (((u32) (val)) & 0xff);           \
00280         } while (0)
00281 
00282 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
00283                          (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
00284 #define WPA_PUT_LE32(a, val)                                    \
00285         do {                                                    \
00286                 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff);   \
00287                 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff);   \
00288                 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff);    \
00289                 (a)[0] = (u8) (((u32) (val)) & 0xff);           \
00290         } while (0)
00291 
00292 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
00293                          (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
00294                          (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
00295                          (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
00296 #define WPA_PUT_BE64(a, val)                            \
00297         do {                                            \
00298                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
00299                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
00300                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
00301                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
00302                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
00303                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
00304                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
00305                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
00306         } while (0)
00307 
00308 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
00309                          (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
00310                          (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
00311                          (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
00312 
00313 
00314 #ifndef ETH_ALEN
00315 #define ETH_ALEN 6
00316 #endif
00317 #ifndef IFNAMSIZ
00318 #define IFNAMSIZ 16
00319 #endif
00320 #ifndef ETH_P_ALL
00321 #define ETH_P_ALL 0x0003
00322 #endif
00323 #ifndef ETH_P_PAE
00324 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
00325 #endif /* ETH_P_PAE */
00326 #ifndef ETH_P_EAPOL
00327 #define ETH_P_EAPOL ETH_P_PAE
00328 #endif /* ETH_P_EAPOL */
00329 #ifndef ETH_P_RSN_PREAUTH
00330 #define ETH_P_RSN_PREAUTH 0x88c7
00331 #endif /* ETH_P_RSN_PREAUTH */
00332 #ifndef ETH_P_RRB
00333 #define ETH_P_RRB 0x890D
00334 #endif /* ETH_P_RRB */
00335 
00336 
00337 #ifdef __GNUC__
00338 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
00339 #define STRUCT_PACKED __attribute__ ((packed))
00340 #else
00341 #define PRINTF_FORMAT(a,b)
00342 #define STRUCT_PACKED
00343 #endif
00344 
00345 
00346 #ifdef CONFIG_ANSI_C_EXTRA
00347 
00348 #if !defined(_MSC_VER) || _MSC_VER < 1400
00349 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
00350  * due to possible buffer overflow; see, e.g.,
00351  * http://www.ijs.si/software/snprintf/ for portable implementation of
00352  * snprintf. */
00353 int snprintf(char *str, size_t size, const char *format, ...);
00354 
00355 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
00356 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
00357 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
00358 
00359 /* getopt - only used in main.c */
00360 int getopt(int argc, char *const argv[], const char *optstring);
00361 extern char *optarg;
00362 extern int optind;
00363 
00364 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
00365 #ifndef __socklen_t_defined
00366 typedef int socklen_t;
00367 #endif
00368 #endif
00369 
00370 /* inline - define as __inline or just define it to be empty, if needed */
00371 #ifdef CONFIG_NO_INLINE
00372 #define inline
00373 #else
00374 #define inline __inline
00375 #endif
00376 
00377 #ifndef __func__
00378 #define __func__ "__func__ not defined"
00379 #endif
00380 
00381 #ifndef bswap_16
00382 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
00383 #endif
00384 
00385 #ifndef bswap_32
00386 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
00387                      (((u32) (a) << 8) & 0xff0000) | \
00388                      (((u32) (a) >> 8) & 0xff00) | \
00389                      (((u32) (a) >> 24) & 0xff))
00390 #endif
00391 
00392 #ifndef MSG_DONTWAIT
00393 #define MSG_DONTWAIT 0
00394 #endif
00395 
00396 #ifdef _WIN32_WCE
00397 void perror(const char *s);
00398 #endif /* _WIN32_WCE */
00399 
00400 #endif /* CONFIG_ANSI_C_EXTRA */
00401 
00402 #ifndef MAC2STR
00403 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
00404 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
00405 #endif
00406 
00407 #ifndef BIT
00408 #define BIT(x) (1 << (x))
00409 #endif
00410 
00411 /*
00412  * Definitions for sparse validation
00413  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
00414  */
00415 #ifdef __CHECKER__
00416 #define __force __attribute__((force))
00417 #define __bitwise __attribute__((bitwise))
00418 #else
00419 #define __force
00420 #define __bitwise
00421 #endif
00422 
00423 typedef u16 __bitwise be16;
00424 typedef u16 __bitwise le16;
00425 typedef u32 __bitwise be32;
00426 typedef u32 __bitwise le32;
00427 typedef u64 __bitwise be64;
00428 typedef u64 __bitwise le64;
00429 
00430 #ifndef __must_check
00431 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00432 #define __must_check __attribute__((__warn_unused_result__))
00433 #else
00434 #define __must_check
00435 #endif /* __GNUC__ */
00436 #endif /* __must_check */
00437 
00438 int hwaddr_aton(const char *txt, u8 *addr);
00439 int hwaddr_aton2(const char *txt, u8 *addr);
00440 int hexstr2bin(const char *hex, u8 *buf, size_t len);
00441 void inc_byte_array(u8 *counter, size_t len);
00442 void wpa_get_ntp_timestamp(u8 *buf);
00443 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
00444 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
00445                                size_t len);
00446 
00447 #ifdef CONFIG_NATIVE_WINDOWS
00448 void wpa_unicode2ascii_inplace(TCHAR *str);
00449 TCHAR * wpa_strdup_tchar(const char *str);
00450 #else /* CONFIG_NATIVE_WINDOWS */
00451 #define wpa_unicode2ascii_inplace(s) do { } while (0)
00452 #define wpa_strdup_tchar(s) strdup((s))
00453 #endif /* CONFIG_NATIVE_WINDOWS */
00454 
00455 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
00456 
00457 static inline int is_zero_ether_addr(const u8 *a)
00458 {
00459         return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
00460 }
00461 
00462 #include "wpa_debug.h"
00463 
00464 
00465 /*
00466  * gcc 4.4 ends up generating strict-aliasing warnings about some very common
00467  * networking socket uses that do not really result in a real problem and
00468  * cannot be easily avoided with union-based type-punning due to struct
00469  * definitions including another struct in system header files. To avoid having
00470  * to fully disable strict-aliasing warnings, provide a mechanism to hide the
00471  * typecast from aliasing for now. A cleaner solution will hopefully be found
00472  * in the future to handle these cases.
00473  */
00474 void * __hide_aliasing_typecast(void *foo);
00475 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
00476 
00477 #endif /* COMMON_H */


wpa_supplicant
Author(s): Package maintained by Blaise Gassend
autogenerated on Thu Jan 2 2014 11:26:36