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 /* __linux__ */
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 /* __OpenBSD__ */
00038 #define bswap_16 bswap16
00039 #define bswap_32 bswap32
00040 #define bswap_64 bswap64
00041 #endif /* __OpenBSD__ */
00042 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
00043         * defined(__DragonFly__) || defined(__OpenBSD__) */
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 /* __APPLE__ */
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 /* CONFIG_TI_COMPILER */
00072 
00073 #ifdef __SYMBIAN32__
00074 #define __BIG_ENDIAN 4321
00075 #define __LITTLE_ENDIAN 1234
00076 #define __BYTE_ORDER __LITTLE_ENDIAN
00077 #endif /* __SYMBIAN32__ */
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 /* not supported */
00086 #endif
00087 
00088 #endif /* CONFIG_NATIVE_WINDOWS */
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 /* _MSC_VER */
00098 
00099 
00100 /* Define platform specific integer types */
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 /* _MSC_VER */
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 /* __vxworks */
00125 
00126 #ifdef CONFIG_TI_COMPILER
00127 #ifdef _LLONG_AVAILABLE
00128 typedef unsigned long long u64;
00129 #else
00130 /*
00131  * TODO: 64-bit variable not available. Using long as a workaround to test the
00132  * build, but this will likely not work for all operations.
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 /* CONFIG_TI_COMPILER */
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 /* __SYMBIAN32__ */
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 /* !WPA_TYPES_DEFINED */
00168 
00169 
00170 /* Define platform specific byte swapping macros */
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 /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
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 /* __BIG_ENDIAN */
00209 #endif /* __LITTLE_ENDIAN */
00210 #endif /* __BYTE_ORDER */
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 /* !WPA_BYTE_SWAP_DEFINED */
00246 
00247 
00248 /* Macros for handling unaligned memory accesses */
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 #ifndef IFNAMSIZ
00319 #define IFNAMSIZ 16
00320 #endif
00321 #ifndef ETH_P_ALL
00322 #define ETH_P_ALL 0x0003
00323 #endif
00324 #ifndef ETH_P_PAE
00325 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
00326 #endif /* ETH_P_PAE */
00327 #ifndef ETH_P_EAPOL
00328 #define ETH_P_EAPOL ETH_P_PAE
00329 #endif /* ETH_P_EAPOL */
00330 #ifndef ETH_P_RSN_PREAUTH
00331 #define ETH_P_RSN_PREAUTH 0x88c7
00332 #endif /* ETH_P_RSN_PREAUTH */
00333 #ifndef ETH_P_RRB
00334 #define ETH_P_RRB 0x890D
00335 #endif /* ETH_P_RRB */
00336 
00337 
00338 #ifdef __GNUC__
00339 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
00340 #define STRUCT_PACKED __attribute__ ((packed))
00341 #else
00342 #define PRINTF_FORMAT(a,b)
00343 #define STRUCT_PACKED
00344 #endif
00345 
00346 
00347 #ifdef CONFIG_ANSI_C_EXTRA
00348 
00349 #if !defined(_MSC_VER) || _MSC_VER < 1400
00350 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
00351  * due to possible buffer overflow; see, e.g.,
00352  * http://www.ijs.si/software/snprintf/ for portable implementation of
00353  * snprintf. */
00354 int snprintf(char *str, size_t size, const char *format, ...);
00355 
00356 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
00357 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
00358 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
00359 
00360 /* getopt - only used in main.c */
00361 int getopt(int argc, char *const argv[], const char *optstring);
00362 extern char *optarg;
00363 extern int optind;
00364 
00365 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
00366 #ifndef __socklen_t_defined
00367 typedef int socklen_t;
00368 #endif
00369 #endif
00370 
00371 /* inline - define as __inline or just define it to be empty, if needed */
00372 #ifdef CONFIG_NO_INLINE
00373 #define inline
00374 #else
00375 #define inline __inline
00376 #endif
00377 
00378 #ifndef __func__
00379 #define __func__ "__func__ not defined"
00380 #endif
00381 
00382 #ifndef bswap_16
00383 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
00384 #endif
00385 
00386 #ifndef bswap_32
00387 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
00388                      (((u32) (a) << 8) & 0xff0000) | \
00389                      (((u32) (a) >> 8) & 0xff00) | \
00390                      (((u32) (a) >> 24) & 0xff))
00391 #endif
00392 
00393 #ifndef MSG_DONTWAIT
00394 #define MSG_DONTWAIT 0
00395 #endif
00396 
00397 #ifdef _WIN32_WCE
00398 void perror(const char *s);
00399 #endif /* _WIN32_WCE */
00400 
00401 #endif /* CONFIG_ANSI_C_EXTRA */
00402 
00403 #ifndef MAC2STR
00404 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
00405 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
00406 #endif
00407 
00408 #ifndef BIT
00409 #define BIT(x) (1 << (x))
00410 #endif
00411 
00412 /*
00413  * Definitions for sparse validation
00414  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
00415  */
00416 #ifdef __CHECKER__
00417 #define __force __attribute__((force))
00418 #define __bitwise __attribute__((bitwise))
00419 #else
00420 #define __force
00421 #define __bitwise
00422 #endif
00423 
00424 typedef u16 __bitwise be16;
00425 typedef u16 __bitwise le16;
00426 typedef u32 __bitwise be32;
00427 typedef u32 __bitwise le32;
00428 typedef u64 __bitwise be64;
00429 typedef u64 __bitwise le64;
00430 
00431 #ifndef __must_check
00432 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
00433 #define __must_check __attribute__((__warn_unused_result__))
00434 #else
00435 #define __must_check
00436 #endif /* __GNUC__ */
00437 #endif /* __must_check */
00438 
00439 int hwaddr_aton(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 */
00478 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on Sat Nov 28 23:07:48 2009 for wpa_supplicant/hostapd by  doxygen 1.6.1