common.h

Go to the documentation of this file.
00001 
00016 #ifndef COMMON_H
00017 #define COMMON_H
00018 
00019 #ifdef __linux__
00020 #include <endian.h>
00021 #include <byteswap.h>
00022 #endif /* __linux__ */
00023 
00024 #if defined(__FreeBSD__) || defined(__NetBSD__)
00025 #include <sys/types.h>
00026 #include <sys/endian.h>
00027 #define __BYTE_ORDER    _BYTE_ORDER
00028 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
00029 #define __BIG_ENDIAN    _BIG_ENDIAN
00030 #define bswap_16 bswap16
00031 #define bswap_32 bswap32
00032 #define bswap_64 bswap64
00033 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) */
00034 
00035 #ifdef CONFIG_NATIVE_WINDOWS
00036 #include <winsock2.h>
00037 
00038 static inline int daemon(int nochdir, int noclose)
00039 {
00040         printf("Windows - daemon() not supported yet\n");
00041         return -1;
00042 }
00043 
00044 static inline void sleep(int seconds)
00045 {
00046         Sleep(seconds * 1000);
00047 }
00048 
00049 static inline void usleep(unsigned long usec)
00050 {
00051         Sleep(usec / 1000);
00052 }
00053 
00054 #ifndef timersub
00055 #define timersub(a, b, res) do { \
00056         (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
00057         (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
00058         if ((res)->tv_usec < 0) { \
00059                 (res)->tv_sec--; \
00060                 (res)->tv_usec += 1000000; \
00061         } \
00062 } while (0)
00063 #endif
00064 
00065 struct timezone {
00066         int  tz_minuteswest;
00067         int  tz_dsttime;
00068 };
00069 
00070 int gettimeofday(struct timeval *tv, struct timezone *tz);
00071 
00072 static inline long int random(void)
00073 {
00074         return rand();
00075 }
00076 
00077 typedef int gid_t;
00078 typedef int socklen_t;
00079 
00080 #ifndef MSG_DONTWAIT
00081 #define MSG_DONTWAIT 0 /* not supported */
00082 #endif
00083 
00084 #endif /* CONFIG_NATIVE_WINDOWS */
00085 
00086 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
00087 
00088 static inline unsigned short wpa_swap_16(unsigned short v)
00089 {
00090         return ((v & 0xff) << 8) | (v >> 8);
00091 }
00092 
00093 static inline unsigned int wpa_swap_32(unsigned int v)
00094 {
00095         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
00096                 ((v & 0xff0000) >> 8) | (v >> 24);
00097 }
00098 
00099 #define le_to_host16(n) (n)
00100 #define host_to_le16(n) (n)
00101 #define be_to_host16(n) wpa_swap_16(n)
00102 #define host_to_be16(n) wpa_swap_16(n)
00103 #define le_to_host32(n) (n)
00104 #define be_to_host32(n) wpa_swap_32(n)
00105 #define host_to_be32(n) wpa_swap_32(n)
00106 
00107 #else /* __CYGWIN__ */
00108 
00109 #if __BYTE_ORDER == __LITTLE_ENDIAN
00110 #define le_to_host16(n) (n)
00111 #define host_to_le16(n) (n)
00112 #define be_to_host16(n) bswap_16(n)
00113 #define host_to_be16(n) bswap_16(n)
00114 #define le_to_host32(n) (n)
00115 #define be_to_host32(n) bswap_32(n)
00116 #define host_to_be32(n) bswap_32(n)
00117 #elif __BYTE_ORDER == __BIG_ENDIAN
00118 #define le_to_host16(n) bswap_16(n)
00119 #define host_to_le16(n) bswap_16(n)
00120 #define be_to_host16(n) (n)
00121 #define host_to_be16(n) (n)
00122 #define le_to_host32(n) bswap_32(n)
00123 #define be_to_host32(n) (n)
00124 #define host_to_be32(n) (n)
00125 #ifndef WORDS_BIGENDIAN
00126 #define WORDS_BIGENDIAN
00127 #endif
00128 #else
00129 #error Could not determine CPU byte order
00130 #endif
00131 
00132 #endif /* __CYGWIN__ */
00133 
00134 /* Macros for handling unaligned 16-bit variables */
00135 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
00136 #define WPA_PUT_BE16(a, val)                    \
00137         do {                                    \
00138                 (a)[0] = ((u16) (val)) >> 8;    \
00139                 (a)[1] = ((u16) (val)) & 0xff;  \
00140         } while (0)
00141 
00142 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
00143 #define WPA_PUT_LE16(a, val)                    \
00144         do {                                    \
00145                 (a)[1] = ((u16) (val)) >> 8;    \
00146                 (a)[0] = ((u16) (val)) & 0xff;  \
00147         } while (0)
00148 
00149 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
00150                          (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
00151 
00152 
00153 #ifndef ETH_ALEN
00154 #define ETH_ALEN 6
00155 #endif
00156 
00157 #include <stdint.h>
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 
00167 int hostapd_get_rand(u8 *buf, size_t len);
00168 void hostapd_hexdump(const char *title, const u8 *buf, size_t len);
00169 int hwaddr_aton(const char *txt, u8 *addr);
00170 int hexstr2bin(const char *hex, u8 *buf, size_t len);
00171 char * rel2abs_path(const char *rel_path);
00172 void inc_byte_array(u8 *counter, size_t len);
00173 void print_char(char c);
00174 void fprint_char(FILE *f, char c);
00175 
00176 
00177 /* Debugging function - conditional printf and hex dump. Driver wrappers can
00178  *  use these for debugging purposes. */
00179 
00180 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
00181 
00182 #ifdef CONFIG_NO_STDOUT_DEBUG
00183 
00184 #define wpa_debug_print_timestamp() do { } while (0)
00185 #define wpa_printf(args...) do { } while (0)
00186 #define wpa_hexdump(args...) do { } while (0)
00187 #define wpa_hexdump_key(args...) do { } while (0)
00188 #define wpa_hexdump_ascii(args...) do { } while (0)
00189 #define wpa_hexdump_ascii_key(args...) do { } while (0)
00190 
00191 #else /* CONFIG_NO_STDOUT_DEBUG */
00192 
00201 void wpa_debug_print_timestamp(void);
00202 
00215 void wpa_printf(int level, char *fmt, ...)
00216 __attribute__ ((format (printf, 2, 3)));
00217 
00230 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
00231 
00246 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
00247 
00262 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
00263                        size_t len);
00264 
00280 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
00281                            size_t len);
00282 
00283 #endif /* CONFIG_NO_STDOUT_DEBUG */
00284 
00285 
00286 #ifdef EAPOL_TEST
00287 #define WPA_ASSERT(a)                                                  \
00288         do {                                                           \
00289                 if (!(a)) {                                            \
00290                         printf("WPA_ASSERT FAILED '" #a "' "           \
00291                                "%s %s:%d\n",                           \
00292                                __FUNCTION__, __FILE__, __LINE__);      \
00293                         exit(1);                                       \
00294                 }                                                      \
00295         } while (0)
00296 #else
00297 #define WPA_ASSERT(a) do { } while (0)
00298 #endif
00299 
00300 #endif /* COMMON_H */
00301 

Generated on Sat May 6 21:13:30 2006 for wpa_supplicant by  doxygen 1.4.2