common.h
Go to the documentation of this file.00001
00016 #ifndef COMMON_H
00017 #define COMMON_H
00018
00019 #include "os.h"
00020
00021 #ifdef __linux__
00022 #include <endian.h>
00023 #include <byteswap.h>
00024 #endif
00025
00026 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
00027 defined(__OpenBSD__)
00028 #include <sys/types.h>
00029 #include <sys/endian.h>
00030 #define __BYTE_ORDER _BYTE_ORDER
00031 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
00032 #define __BIG_ENDIAN _BIG_ENDIAN
00033 #ifdef __OpenBSD__
00034 #define bswap_16 swap16
00035 #define bswap_32 swap32
00036 #define bswap_64 swap64
00037 #else
00038 #define bswap_16 bswap16
00039 #define bswap_32 bswap32
00040 #define bswap_64 bswap64
00041 #endif
00042 #endif
00043
00044
00045 #ifdef __APPLE__
00046 #include <sys/types.h>
00047 #include <machine/endian.h>
00048 #define __BYTE_ORDER _BYTE_ORDER
00049 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
00050 #define __BIG_ENDIAN _BIG_ENDIAN
00051 static inline unsigned short bswap_16(unsigned short v)
00052 {
00053 return ((v & 0xff) << 8) | (v >> 8);
00054 }
00055
00056 static inline unsigned int bswap_32(unsigned int v)
00057 {
00058 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00059 ((v & 0xff0000) >> 8) | (v >> 24);
00060 }
00061 #endif
00062
00063 #ifdef CONFIG_TI_COMPILER
00064 #define __BIG_ENDIAN 4321
00065 #define __LITTLE_ENDIAN 1234
00066 #ifdef __big_endian__
00067 #define __BYTE_ORDER __BIG_ENDIAN
00068 #else
00069 #define __BYTE_ORDER __LITTLE_ENDIAN
00070 #endif
00071 #endif
00072
00073 #ifdef __SYMBIAN32__
00074 #define __BIG_ENDIAN 4321
00075 #define __LITTLE_ENDIAN 1234
00076 #define __BYTE_ORDER __LITTLE_ENDIAN
00077 #endif
00078
00079 #ifdef CONFIG_NATIVE_WINDOWS
00080 #include <winsock.h>
00081
00082 typedef int socklen_t;
00083
00084 #ifndef MSG_DONTWAIT
00085 #define MSG_DONTWAIT 0
00086 #endif
00087
00088 #endif
00089
00090 #ifdef _MSC_VER
00091 #define inline __inline
00092
00093 #undef vsnprintf
00094 #define vsnprintf _vsnprintf
00095 #undef close
00096 #define close closesocket
00097 #endif
00098
00099
00100
00101
00102 #ifdef _MSC_VER
00103 typedef UINT64 u64;
00104 typedef UINT32 u32;
00105 typedef UINT16 u16;
00106 typedef UINT8 u8;
00107 typedef INT64 s64;
00108 typedef INT32 s32;
00109 typedef INT16 s16;
00110 typedef INT8 s8;
00111 #define WPA_TYPES_DEFINED
00112 #endif
00113
00114 #ifdef __vxworks
00115 typedef unsigned long long u64;
00116 typedef UINT32 u32;
00117 typedef UINT16 u16;
00118 typedef UINT8 u8;
00119 typedef long long s64;
00120 typedef INT32 s32;
00121 typedef INT16 s16;
00122 typedef INT8 s8;
00123 #define WPA_TYPES_DEFINED
00124 #endif
00125
00126 #ifdef CONFIG_TI_COMPILER
00127 #ifdef _LLONG_AVAILABLE
00128 typedef unsigned long long u64;
00129 #else
00130
00131
00132
00133
00134 typedef unsigned long u64;
00135 #endif
00136 typedef unsigned int u32;
00137 typedef unsigned short u16;
00138 typedef unsigned char u8;
00139 #define WPA_TYPES_DEFINED
00140 #endif
00141
00142 #ifdef __SYMBIAN32__
00143 #define __REMOVE_PLATSEC_DIAGNOSTICS__
00144 #include <e32def.h>
00145 typedef TUint64 u64;
00146 typedef TUint32 u32;
00147 typedef TUint16 u16;
00148 typedef TUint8 u8;
00149 #define WPA_TYPES_DEFINED
00150 #endif
00151
00152 #ifndef WPA_TYPES_DEFINED
00153 #ifdef CONFIG_USE_INTTYPES_H
00154 #include <inttypes.h>
00155 #else
00156 #include <stdint.h>
00157 #endif
00158 typedef uint64_t u64;
00159 typedef uint32_t u32;
00160 typedef uint16_t u16;
00161 typedef uint8_t u8;
00162 typedef int64_t s64;
00163 typedef int32_t s32;
00164 typedef int16_t s16;
00165 typedef int8_t s8;
00166 #define WPA_TYPES_DEFINED
00167 #endif
00168
00169
00170
00171
00172 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
00173
00174 static inline unsigned short wpa_swap_16(unsigned short v)
00175 {
00176 return ((v & 0xff) << 8) | (v >> 8);
00177 }
00178
00179 static inline unsigned int wpa_swap_32(unsigned int v)
00180 {
00181 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00182 ((v & 0xff0000) >> 8) | (v >> 24);
00183 }
00184
00185 #define le_to_host16(n) (n)
00186 #define host_to_le16(n) (n)
00187 #define be_to_host16(n) wpa_swap_16(n)
00188 #define host_to_be16(n) wpa_swap_16(n)
00189 #define le_to_host32(n) (n)
00190 #define be_to_host32(n) wpa_swap_32(n)
00191 #define host_to_be32(n) wpa_swap_32(n)
00192
00193 #define WPA_BYTE_SWAP_DEFINED
00194
00195 #endif
00196
00197
00198 #ifndef WPA_BYTE_SWAP_DEFINED
00199
00200 #ifndef __BYTE_ORDER
00201 #ifndef __LITTLE_ENDIAN
00202 #ifndef __BIG_ENDIAN
00203 #define __LITTLE_ENDIAN 1234
00204 #define __BIG_ENDIAN 4321
00205 #if defined(sparc)
00206 #define __BYTE_ORDER __BIG_ENDIAN
00207 #endif
00208 #endif
00209 #endif
00210 #endif
00211
00212 #if __BYTE_ORDER == __LITTLE_ENDIAN
00213 #define le_to_host16(n) ((__force u16) (le16) (n))
00214 #define host_to_le16(n) ((__force le16) (u16) (n))
00215 #define be_to_host16(n) bswap_16((__force u16) (be16) (n))
00216 #define host_to_be16(n) ((__force be16) bswap_16((n)))
00217 #define le_to_host32(n) ((__force u32) (le32) (n))
00218 #define host_to_le32(n) ((__force le32) (u32) (n))
00219 #define be_to_host32(n) bswap_32((__force u32) (be32) (n))
00220 #define host_to_be32(n) ((__force be32) bswap_32((n)))
00221 #define le_to_host64(n) ((__force u64) (le64) (n))
00222 #define host_to_le64(n) ((__force le64) (u64) (n))
00223 #define be_to_host64(n) bswap_64((__force u64) (be64) (n))
00224 #define host_to_be64(n) ((__force be64) bswap_64((n)))
00225 #elif __BYTE_ORDER == __BIG_ENDIAN
00226 #define le_to_host16(n) bswap_16(n)
00227 #define host_to_le16(n) bswap_16(n)
00228 #define be_to_host16(n) (n)
00229 #define host_to_be16(n) (n)
00230 #define le_to_host32(n) bswap_32(n)
00231 #define be_to_host32(n) (n)
00232 #define host_to_be32(n) (n)
00233 #define le_to_host64(n) bswap_64(n)
00234 #define host_to_le64(n) bswap_64(n)
00235 #define be_to_host64(n) (n)
00236 #define host_to_be64(n) (n)
00237 #ifndef WORDS_BIGENDIAN
00238 #define WORDS_BIGENDIAN
00239 #endif
00240 #else
00241 #error Could not determine CPU byte order
00242 #endif
00243
00244 #define WPA_BYTE_SWAP_DEFINED
00245 #endif
00246
00247
00248
00249
00250 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
00251 #define WPA_PUT_BE16(a, val) \
00252 do { \
00253 (a)[0] = ((u16) (val)) >> 8; \
00254 (a)[1] = ((u16) (val)) & 0xff; \
00255 } while (0)
00256
00257 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
00258 #define WPA_PUT_LE16(a, val) \
00259 do { \
00260 (a)[1] = ((u16) (val)) >> 8; \
00261 (a)[0] = ((u16) (val)) & 0xff; \
00262 } while (0)
00263
00264 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
00265 ((u32) (a)[2]))
00266 #define WPA_PUT_BE24(a, val) \
00267 do { \
00268 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \
00269 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
00270 (a)[2] = (u8) (((u32) (val)) & 0xff); \
00271 } while (0)
00272
00273 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
00274 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
00275 #define WPA_PUT_BE32(a, val) \
00276 do { \
00277 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \
00278 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \
00279 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \
00280 (a)[3] = (u8) (((u32) (val)) & 0xff); \
00281 } while (0)
00282
00283 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \
00284 (((u32) (a)[1]) << 8) | ((u32) (a)[0]))
00285 #define WPA_PUT_LE32(a, val) \
00286 do { \
00287 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \
00288 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \
00289 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \
00290 (a)[0] = (u8) (((u32) (val)) & 0xff); \
00291 } while (0)
00292
00293 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \
00294 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \
00295 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \
00296 (((u64) (a)[6]) << 8) | ((u64) (a)[7]))
00297 #define WPA_PUT_BE64(a, val) \
00298 do { \
00299 (a)[0] = (u8) (((u64) (val)) >> 56); \
00300 (a)[1] = (u8) (((u64) (val)) >> 48); \
00301 (a)[2] = (u8) (((u64) (val)) >> 40); \
00302 (a)[3] = (u8) (((u64) (val)) >> 32); \
00303 (a)[4] = (u8) (((u64) (val)) >> 24); \
00304 (a)[5] = (u8) (((u64) (val)) >> 16); \
00305 (a)[6] = (u8) (((u64) (val)) >> 8); \
00306 (a)[7] = (u8) (((u64) (val)) & 0xff); \
00307 } while (0)
00308
00309 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \
00310 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \
00311 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \
00312 (((u64) (a)[1]) << 8) | ((u64) (a)[0]))
00313
00314
00315 #ifndef ETH_ALEN
00316 #define ETH_ALEN 6
00317 #endif
00318
00319
00320 #ifdef __GNUC__
00321 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
00322 #define STRUCT_PACKED __attribute__ ((packed))
00323 #else
00324 #define PRINTF_FORMAT(a,b)
00325 #define STRUCT_PACKED
00326 #endif
00327
00328
00329 #ifdef CONFIG_ANSI_C_EXTRA
00330
00331 #if !defined(_MSC_VER) || _MSC_VER < 1400
00332
00333
00334
00335
00336 int snprintf(char *str, size_t size, const char *format, ...);
00337
00338
00339 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
00340 #endif
00341
00342
00343 int getopt(int argc, char *const argv[], const char *optstring);
00344 extern char *optarg;
00345 extern int optind;
00346
00347 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
00348 #ifndef __socklen_t_defined
00349 typedef int socklen_t;
00350 #endif
00351 #endif
00352
00353
00354 #ifdef CONFIG_NO_INLINE
00355 #define inline
00356 #else
00357 #define inline __inline
00358 #endif
00359
00360 #ifndef __func__
00361 #define __func__ "__func__ not defined"
00362 #endif
00363
00364 #ifndef bswap_16
00365 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
00366 #endif
00367
00368 #ifndef bswap_32
00369 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
00370 (((u32) (a) << 8) & 0xff0000) | \
00371 (((u32) (a) >> 8) & 0xff00) | \
00372 (((u32) (a) >> 24) & 0xff))
00373 #endif
00374
00375 #ifndef MSG_DONTWAIT
00376 #define MSG_DONTWAIT 0
00377 #endif
00378
00379 #ifdef _WIN32_WCE
00380 void perror(const char *s);
00381 #endif
00382
00383 #endif
00384
00385 #ifndef MAC2STR
00386 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
00387 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
00388 #endif
00389
00390 #ifndef BIT
00391 #define BIT(x) (1 << (x))
00392 #endif
00393
00394
00395
00396
00397
00398 #ifdef __CHECKER__
00399 #define __force __attribute__((force))
00400 #define __bitwise __attribute__((bitwise))
00401 #else
00402 #define __force
00403 #define __bitwise
00404 #endif
00405
00406 typedef u16 __bitwise be16;
00407 typedef u16 __bitwise le16;
00408 typedef u32 __bitwise be32;
00409 typedef u32 __bitwise le32;
00410 typedef u64 __bitwise be64;
00411 typedef u64 __bitwise le64;
00412
00413 #ifndef __must_check
00414 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00415 #define __must_check __attribute__((__warn_unused_result__))
00416 #else
00417 #define __must_check
00418 #endif
00419 #endif
00420
00421 int hwaddr_aton(const char *txt, u8 *addr);
00422 int hexstr2bin(const char *hex, u8 *buf, size_t len);
00423 void inc_byte_array(u8 *counter, size_t len);
00424 void wpa_get_ntp_timestamp(u8 *buf);
00425 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
00426 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
00427 size_t len);
00428
00429 #ifdef CONFIG_NATIVE_WINDOWS
00430 void wpa_unicode2ascii_inplace(TCHAR *str);
00431 TCHAR * wpa_strdup_tchar(const char *str);
00432 #else
00433 #define wpa_unicode2ascii_inplace(s) do { } while (0)
00434 #define wpa_strdup_tchar(s) strdup((s))
00435 #endif
00436
00437 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
00438
00439 static inline int is_zero_ether_addr(const u8 *a)
00440 {
00441 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
00442 }
00443
00444 #include "wpa_debug.h"
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456 void * __hide_aliasing_typecast(void *foo);
00457 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
00458
00459 #endif
00460