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 #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 #define bswap_16 bswap16
00033 #define bswap_32 bswap32
00034 #define bswap_64 bswap64
00035 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
00036         * defined(__DragonFly__) */
00037 
00038 #ifdef CONFIG_TI_COMPILER
00039 #define __BIG_ENDIAN 4321
00040 #define __LITTLE_ENDIAN 1234
00041 #ifdef __big_endian__
00042 #define __BYTE_ORDER __BIG_ENDIAN
00043 #else
00044 #define __BYTE_ORDER __LITTLE_ENDIAN
00045 #endif
00046 #endif /* CONFIG_TI_COMPILER */
00047 
00048 #ifdef CONFIG_NATIVE_WINDOWS
00049 #include <winsock.h>
00050 
00051 typedef int socklen_t;
00052 
00053 #ifndef MSG_DONTWAIT
00054 #define MSG_DONTWAIT 0 /* not supported */
00055 #endif
00056 
00057 #endif /* CONFIG_NATIVE_WINDOWS */
00058 
00059 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
00060 
00061 #ifdef _MSC_VER
00062 #define inline __inline
00063 #endif /* _MSC_VER */
00064 
00065 static inline unsigned short wpa_swap_16(unsigned short v)
00066 {
00067         return ((v & 0xff) << 8) | (v >> 8);
00068 }
00069 
00070 static inline unsigned int wpa_swap_32(unsigned int v)
00071 {
00072         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00073                 ((v & 0xff0000) >> 8) | (v >> 24);
00074 }
00075 
00076 #define le_to_host16(n) (n)
00077 #define host_to_le16(n) (n)
00078 #define be_to_host16(n) wpa_swap_16(n)
00079 #define host_to_be16(n) wpa_swap_16(n)
00080 #define le_to_host32(n) (n)
00081 #define be_to_host32(n) wpa_swap_32(n)
00082 #define host_to_be32(n) wpa_swap_32(n)
00083 
00084 #else /* __CYGWIN__ */
00085 
00086 #ifndef __BYTE_ORDER
00087 #ifndef __LITTLE_ENDIAN
00088 #ifndef __BIG_ENDIAN
00089 #define __LITTLE_ENDIAN 1234
00090 #define __BIG_ENDIAN 4321
00091 #if defined(sparc)
00092 #define __BYTE_ORDER __BIG_ENDIAN
00093 #endif
00094 #endif /* __BIG_ENDIAN */
00095 #endif /* __LITTLE_ENDIAN */
00096 #endif /* __BYTE_ORDER */
00097 
00098 #if __BYTE_ORDER == __LITTLE_ENDIAN
00099 #define le_to_host16(n) (n)
00100 #define host_to_le16(n) (n)
00101 #define be_to_host16(n) bswap_16(n)
00102 #define host_to_be16(n) bswap_16(n)
00103 #define le_to_host32(n) (n)
00104 #define be_to_host32(n) bswap_32(n)
00105 #define host_to_be32(n) bswap_32(n)
00106 #define le_to_host64(n) (n)
00107 #define host_to_le64(n) (n)
00108 #define be_to_host64(n) bswap_64(n)
00109 #define host_to_be64(n) bswap_64(n)
00110 #elif __BYTE_ORDER == __BIG_ENDIAN
00111 #define le_to_host16(n) bswap_16(n)
00112 #define host_to_le16(n) bswap_16(n)
00113 #define be_to_host16(n) (n)
00114 #define host_to_be16(n) (n)
00115 #define le_to_host32(n) bswap_32(n)
00116 #define be_to_host32(n) (n)
00117 #define host_to_be32(n) (n)
00118 #define le_to_host64(n) bswap_64(n)
00119 #define host_to_le64(n) bswap_64(n)
00120 #define be_to_host64(n) (n)
00121 #define host_to_be64(n) (n)
00122 #ifndef WORDS_BIGENDIAN
00123 #define WORDS_BIGENDIAN
00124 #endif
00125 #else
00126 #error Could not determine CPU byte order
00127 #endif
00128 
00129 #endif /* __CYGWIN__ */
00130 
00131 /* Macros for handling unaligned 16-bit variables */
00132 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
00133 #define WPA_PUT_BE16(a, val)                    \
00134         do {                                    \
00135                 (a)[0] = ((u16) (val)) >> 8;    \
00136                 (a)[1] = ((u16) (val)) & 0xff;  \
00137         } while (0)
00138 
00139 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
00140 #define WPA_PUT_LE16(a, val)                    \
00141         do {                                    \
00142                 (a)[1] = ((u16) (val)) >> 8;    \
00143                 (a)[0] = ((u16) (val)) & 0xff;  \
00144         } while (0)
00145 
00146 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
00147                          ((u32) (a)[2]))
00148 #define WPA_PUT_BE24(a, val)                            \
00149         do {                                            \
00150                 (a)[0] = (u8) (((u32) (val)) >> 16);    \
00151                 (a)[1] = (u8) (((u32) (val)) >> 8);     \
00152                 (a)[2] = (u8) (((u32) (val)) & 0xff);   \
00153         } while (0)
00154 
00155 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
00156                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
00157 #define WPA_PUT_BE32(a, val)                            \
00158         do {                                            \
00159                 (a)[0] = (u8) (((u32) (val)) >> 24);    \
00160                 (a)[1] = (u8) (((u32) (val)) >> 16);    \
00161                 (a)[2] = (u8) (((u32) (val)) >> 8);     \
00162                 (a)[3] = (u8) (((u32) (val)) & 0xff);   \
00163         } while (0)
00164 
00165 #define WPA_PUT_BE64(a, val)                            \
00166         do {                                            \
00167                 (a)[0] = (u8) (((u64) (val)) >> 56);    \
00168                 (a)[1] = (u8) (((u64) (val)) >> 48);    \
00169                 (a)[2] = (u8) (((u64) (val)) >> 40);    \
00170                 (a)[3] = (u8) (((u64) (val)) >> 32);    \
00171                 (a)[4] = (u8) (((u64) (val)) >> 24);    \
00172                 (a)[5] = (u8) (((u64) (val)) >> 16);    \
00173                 (a)[6] = (u8) (((u64) (val)) >> 8);     \
00174                 (a)[7] = (u8) (((u64) (val)) & 0xff);   \
00175         } while (0)
00176 
00177 
00178 #ifndef ETH_ALEN
00179 #define ETH_ALEN 6
00180 #endif
00181 
00182 #ifdef _MSC_VER
00183 typedef UINT64 u64;
00184 typedef UINT32 u32;
00185 typedef UINT16 u16;
00186 typedef UINT8 u8;
00187 typedef INT64 s64;
00188 typedef INT32 s32;
00189 typedef INT16 s16;
00190 typedef INT8 s8;
00191 #define WPA_TYPES_DEFINED
00192 #endif /* _MSC_VER */
00193 
00194 #ifdef __vxworks
00195 typedef unsigned long long u64;
00196 typedef UINT32 u32;
00197 typedef UINT16 u16;
00198 typedef UINT8 u8;
00199 typedef long long s64;
00200 typedef INT32 s32;
00201 typedef INT16 s16;
00202 typedef INT8 s8;
00203 #define WPA_TYPES_DEFINED
00204 #endif /* __vxworks */
00205 
00206 #ifdef CONFIG_TI_COMPILER
00207 #ifdef _LLONG_AVAILABLE
00208 typedef unsigned long long u64;
00209 #else
00210 /*
00211  * TODO: 64-bit variable not available. Using long as a workaround to test the
00212  * build, but this will likely not work for all operations.
00213  */
00214 typedef unsigned long u64;
00215 #endif
00216 typedef unsigned int u32;
00217 typedef unsigned short u16;
00218 typedef unsigned char u8;
00219 #define WPA_TYPES_DEFINED
00220 #endif /* CONFIG_TI_COMPILER */
00221 
00222 #ifndef WPA_TYPES_DEFINED
00223 #ifdef CONFIG_USE_INTTYPES_H
00224 #include <inttypes.h>
00225 #else
00226 #include <stdint.h>
00227 #endif
00228 typedef uint64_t u64;
00229 typedef uint32_t u32;
00230 typedef uint16_t u16;
00231 typedef uint8_t u8;
00232 typedef int64_t s64;
00233 typedef int32_t s32;
00234 typedef int16_t s16;
00235 typedef int8_t s8;
00236 #define WPA_TYPES_DEFINED
00237 #endif /* !WPA_TYPES_DEFINED */
00238 
00239 #define hostapd_get_rand os_get_random
00240 int hwaddr_aton(const char *txt, u8 *addr);
00241 int hexstr2bin(const char *hex, u8 *buf, size_t len);
00242 void inc_byte_array(u8 *counter, size_t len);
00243 void wpa_get_ntp_timestamp(u8 *buf);
00244 
00245 
00246 #ifdef __GNUC__
00247 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
00248 #define STRUCT_PACKED __attribute__ ((packed))
00249 #else
00250 #define PRINTF_FORMAT(a,b)
00251 #define STRUCT_PACKED
00252 #endif
00253 
00254 
00255 /* Debugging function - conditional printf and hex dump. Driver wrappers can
00256  * use these for debugging purposes. */
00257 
00258 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
00259 
00260 #ifdef CONFIG_NO_STDOUT_DEBUG
00261 
00262 #define wpa_debug_print_timestamp() do { } while (0)
00263 #define wpa_printf(args...) do { } while (0)
00264 #define wpa_hexdump(l,t,b,le) do { } while (0)
00265 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
00266 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
00267 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
00268 #define wpa_debug_open_file(void) do { } while (0)
00269 #define wpa_debug_close_file(void) do { } while (0)
00270 
00271 #else /* CONFIG_NO_STDOUT_DEBUG */
00272 
00273 int wpa_debug_open_file(void);
00274 void wpa_debug_close_file(void);
00275 
00284 void wpa_debug_print_timestamp(void);
00285 
00298 void wpa_printf(int level, char *fmt, ...)
00299 PRINTF_FORMAT(2, 3);
00300 
00313 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
00314 
00329 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
00330 
00345 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
00346                        size_t len);
00347 
00363 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
00364                            size_t len);
00365 
00366 #endif /* CONFIG_NO_STDOUT_DEBUG */
00367 
00368 
00369 #ifdef CONFIG_NO_WPA_MSG
00370 #define wpa_msg(args...) do { } while (0)
00371 #define wpa_msg_register_cb(f) do { } while (0)
00372 #else /* CONFIG_NO_WPA_MSG */
00373 
00388 void wpa_msg(void *ctx, int level, char *fmt, ...) PRINTF_FORMAT(3, 4);
00389 
00390 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
00391                                 size_t len);
00392 
00398 void wpa_msg_register_cb(wpa_msg_cb_func func);
00399 #endif /* CONFIG_NO_WPA_MSG */
00400 
00401 
00402 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
00403 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
00404                                size_t len);
00405 
00406 
00407 #ifdef EAPOL_TEST
00408 #define WPA_ASSERT(a)                                                  \
00409         do {                                                           \
00410                 if (!(a)) {                                            \
00411                         printf("WPA_ASSERT FAILED '" #a "' "           \
00412                                "%s %s:%d\n",                           \
00413                                __FUNCTION__, __FILE__, __LINE__);      \
00414                         exit(1);                                       \
00415                 }                                                      \
00416         } while (0)
00417 #else
00418 #define WPA_ASSERT(a) do { } while (0)
00419 #endif
00420 
00421 
00422 #ifdef _MSC_VER
00423 #undef vsnprintf
00424 #define vsnprintf _vsnprintf
00425 #undef close
00426 #define close closesocket
00427 #endif /* _MSC_VER */
00428 
00429 
00430 #ifdef CONFIG_ANSI_C_EXTRA
00431 
00432 #if !defined(_MSC_VER) || _MSC_VER < 1400
00433 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
00434  * due to possible buffer overflow; see, e.g.,
00435  * http://www.ijs.si/software/snprintf/ for portable implementation of
00436  * snprintf. */
00437 int snprintf(char *str, size_t size, const char *format, ...);
00438 
00439 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
00440 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
00441 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
00442 
00443 /* getopt - only used in main.c */
00444 int getopt(int argc, char *const argv[], const char *optstring);
00445 extern char *optarg;
00446 extern int optind;
00447 
00448 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
00449 #ifndef __socklen_t_defined
00450 typedef int socklen_t;
00451 #endif
00452 #endif
00453 
00454 /* inline - define as __inline or just define it to be empty, if needed */
00455 #ifdef CONFIG_NO_INLINE
00456 #define inline
00457 #else
00458 #define inline __inline
00459 #endif
00460 
00461 #ifndef __func__
00462 #define __func__ "__func__ not defined"
00463 #endif
00464 
00465 #ifndef bswap_16
00466 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
00467 #endif
00468 
00469 #ifndef bswap_32
00470 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
00471                      (((u32) (a) << 8) & 0xff0000) | \
00472                      (((u32) (a) >> 8) & 0xff00) | \
00473                      (((u32) (a) >> 24) & 0xff))
00474 #endif
00475 
00476 #ifndef MSG_DONTWAIT
00477 #define MSG_DONTWAIT 0
00478 #endif
00479 
00480 #ifdef _WIN32_WCE
00481 void perror(const char *s);
00482 #endif /* _WIN32_WCE */
00483 
00484 #endif /* CONFIG_ANSI_C_EXTRA */
00485 
00486 #define wpa_zalloc(s) os_zalloc((s))
00487 
00488 #ifdef CONFIG_NATIVE_WINDOWS
00489 void wpa_unicode2ascii_inplace(TCHAR *str);
00490 TCHAR * wpa_strdup_tchar(const char *str);
00491 #else /* CONFIG_NATIVE_WINDOWS */
00492 #define wpa_unicode2ascii_inplace(s) do { } while (0)
00493 #define wpa_strdup_tchar(s) strdup((s))
00494 #endif /* CONFIG_NATIVE_WINDOWS */
00495 
00496 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len);
00497 
00498 typedef u32 __be32;
00499 typedef u64 __be64;
00500 
00501 #endif /* COMMON_H */
00502 

Generated on Sun Dec 31 13:48:51 2006 for wpa_supplicant by  doxygen 1.4.2