1 /* 2 * wpa_supplicant/hostapd / common helper functions, etc. 3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #ifndef COMMON_H 16 #define COMMON_H 17 18 #include "os.h" 19 20 #ifdef __linux__ 21 #include <endian.h> 22 #include <byteswap.h> 23 #endif /* __linux__ */ 24 25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) 26 #include <sys/types.h> 27 #include <sys/endian.h> 28 #define __BYTE_ORDER _BYTE_ORDER 29 #define __LITTLE_ENDIAN _LITTLE_ENDIAN 30 #define __BIG_ENDIAN _BIG_ENDIAN 31 #define bswap_16 bswap16 32 #define bswap_32 bswap32 33 #define bswap_64 bswap64 34 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) || 35 * defined(__DragonFly__) */ 36 37 #ifdef __APPLE__ 38 #include <sys/types.h> 39 #include <machine/endian.h> 40 #define __BYTE_ORDER _BYTE_ORDER 41 #define __LITTLE_ENDIAN _LITTLE_ENDIAN 42 #define __BIG_ENDIAN _BIG_ENDIAN 43 static inline unsigned short bswap_16(unsigned short v) 44 { 45 return ((v & 0xff) << 8) | (v >> 8); 46 } 47 48 static inline unsigned int bswap_32(unsigned int v) 49 { 50 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | 51 ((v & 0xff0000) >> 8) | (v >> 24); 52 } 53 #endif /* __APPLE__ */ 54 55 #ifdef CONFIG_TI_COMPILER 56 #define __BIG_ENDIAN 4321 57 #define __LITTLE_ENDIAN 1234 58 #ifdef __big_endian__ 59 #define __BYTE_ORDER __BIG_ENDIAN 60 #else 61 #define __BYTE_ORDER __LITTLE_ENDIAN 62 #endif 63 #endif /* CONFIG_TI_COMPILER */ 64 65 #ifdef __SYMBIAN32__ 66 #define __BIG_ENDIAN 4321 67 #define __LITTLE_ENDIAN 1234 68 #define __BYTE_ORDER __LITTLE_ENDIAN 69 #endif /* __SYMBIAN32__ */ 70 71 #ifdef CONFIG_NATIVE_WINDOWS 72 #include <winsock.h> 73 74 typedef int socklen_t; 75 76 #ifndef MSG_DONTWAIT 77 #define MSG_DONTWAIT 0 /* not supported */ 78 #endif 79 80 #endif /* CONFIG_NATIVE_WINDOWS */ 81 82 #ifdef _MSC_VER 83 #define inline __inline 84 85 #undef vsnprintf 86 #define vsnprintf _vsnprintf 87 #undef close 88 #define close closesocket 89 #endif /* _MSC_VER */ 90 91 92 /* Define platform specific integer types */ 93 94 #ifdef _MSC_VER 95 typedef UINT64 u64; 96 typedef UINT32 u32; 97 typedef UINT16 u16; 98 typedef UINT8 u8; 99 typedef INT64 s64; 100 typedef INT32 s32; 101 typedef INT16 s16; 102 typedef INT8 s8; 103 #define WPA_TYPES_DEFINED 104 #endif /* _MSC_VER */ 105 106 #ifdef __vxworks 107 typedef unsigned long long u64; 108 typedef UINT32 u32; 109 typedef UINT16 u16; 110 typedef UINT8 u8; 111 typedef long long s64; 112 typedef INT32 s32; 113 typedef INT16 s16; 114 typedef INT8 s8; 115 #define WPA_TYPES_DEFINED 116 #endif /* __vxworks */ 117 118 #ifdef CONFIG_TI_COMPILER 119 #ifdef _LLONG_AVAILABLE 120 typedef unsigned long long u64; 121 #else 122 /* 123 * TODO: 64-bit variable not available. Using long as a workaround to test the 124 * build, but this will likely not work for all operations. 125 */ 126 typedef unsigned long u64; 127 #endif 128 typedef unsigned int u32; 129 typedef unsigned short u16; 130 typedef unsigned char u8; 131 #define WPA_TYPES_DEFINED 132 #endif /* CONFIG_TI_COMPILER */ 133 134 #ifdef __SYMBIAN32__ 135 #define __REMOVE_PLATSEC_DIAGNOSTICS__ 136 #include <e32def.h> 137 typedef TUint64 u64; 138 typedef TUint32 u32; 139 typedef TUint16 u16; 140 typedef TUint8 u8; 141 #define WPA_TYPES_DEFINED 142 #endif /* __SYMBIAN32__ */ 143 144 #ifndef WPA_TYPES_DEFINED 145 #ifdef CONFIG_USE_INTTYPES_H 146 #include <inttypes.h> 147 #else 148 #include <stdint.h> 149 #endif 150 typedef uint64_t u64; 151 typedef uint32_t u32; 152 typedef uint16_t u16; 153 typedef uint8_t u8; 154 typedef int64_t s64; 155 typedef int32_t s32; 156 typedef int16_t s16; 157 typedef int8_t s8; 158 #define WPA_TYPES_DEFINED 159 #endif /* !WPA_TYPES_DEFINED */ 160 161 162 /* Define platform specific byte swapping macros */ 163 164 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS) 165 166 static inline unsigned short wpa_swap_16(unsigned short v) 167 { 168 return ((v & 0xff) << 8) | (v >> 8); 169 } 170 171 static inline unsigned int wpa_swap_32(unsigned int v) 172 { 173 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | 174 ((v & 0xff0000) >> 8) | (v >> 24); 175 } 176 177 #define le_to_host16(n) (n) 178 #define host_to_le16(n) (n) 179 #define be_to_host16(n) wpa_swap_16(n) 180 #define host_to_be16(n) wpa_swap_16(n) 181 #define le_to_host32(n) (n) 182 #define be_to_host32(n) wpa_swap_32(n) 183 #define host_to_be32(n) wpa_swap_32(n) 184 185 #define WPA_BYTE_SWAP_DEFINED 186 187 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */ 188 189 190 #ifndef WPA_BYTE_SWAP_DEFINED 191 192 #ifndef __BYTE_ORDER 193 #ifndef __LITTLE_ENDIAN 194 #ifndef __BIG_ENDIAN 195 #define __LITTLE_ENDIAN 1234 196 #define __BIG_ENDIAN 4321 197 #if defined(sparc) 198 #define __BYTE_ORDER __BIG_ENDIAN 199 #endif 200 #endif /* __BIG_ENDIAN */ 201 #endif /* __LITTLE_ENDIAN */ 202 #endif /* __BYTE_ORDER */ 203 204 #if __BYTE_ORDER == __LITTLE_ENDIAN 205 #define le_to_host16(n) ((__force u16) (le16) (n)) 206 #define host_to_le16(n) ((__force le16) (u16) (n)) 207 #define be_to_host16(n) bswap_16((__force u16) (be16) (n)) 208 #define host_to_be16(n) ((__force be16) bswap_16((n))) 209 #define le_to_host32(n) ((__force u32) (le32) (n)) 210 #define host_to_le32(n) ((__force le32) (u32) (n)) 211 #define be_to_host32(n) bswap_32((__force u32) (be32) (n)) 212 #define host_to_be32(n) ((__force be32) bswap_32((n))) 213 #define le_to_host64(n) ((__force u64) (le64) (n)) 214 #define host_to_le64(n) ((__force le64) (u64) (n)) 215 #define be_to_host64(n) bswap_64((__force u64) (be64) (n)) 216 #define host_to_be64(n) ((__force be64) bswap_64((n))) 217 #elif __BYTE_ORDER == __BIG_ENDIAN 218 #define le_to_host16(n) bswap_16(n) 219 #define host_to_le16(n) bswap_16(n) 220 #define be_to_host16(n) (n) 221 #define host_to_be16(n) (n) 222 #define le_to_host32(n) bswap_32(n) 223 #define be_to_host32(n) (n) 224 #define host_to_be32(n) (n) 225 #define le_to_host64(n) bswap_64(n) 226 #define host_to_le64(n) bswap_64(n) 227 #define be_to_host64(n) (n) 228 #define host_to_be64(n) (n) 229 #ifndef WORDS_BIGENDIAN 230 #define WORDS_BIGENDIAN 231 #endif 232 #else 233 #error Could not determine CPU byte order 234 #endif 235 236 #define WPA_BYTE_SWAP_DEFINED 237 #endif /* !WPA_BYTE_SWAP_DEFINED */ 238 239 240 /* Macros for handling unaligned memory accesses */ 241 242 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1])) 243 #define WPA_PUT_BE16(a, val) \ 244 do { \ 245 (a)[0] = ((u16) (val)) >> 8; \ 246 (a)[1] = ((u16) (val)) & 0xff; \ 247 } while (0) 248 249 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0])) 250 #define WPA_PUT_LE16(a, val) \ 251 do { \ 252 (a)[1] = ((u16) (val)) >> 8; \ 253 (a)[0] = ((u16) (val)) & 0xff; \ 254 } while (0) 255 256 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \ 257 ((u32) (a)[2])) 258 #define WPA_PUT_BE24(a, val) \ 259 do { \ 260 (a)[0] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 261 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 262 (a)[2] = (u8) (((u32) (val)) & 0xff); \ 263 } while (0) 264 265 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \ 266 (((u32) (a)[2]) << 8) | ((u32) (a)[3])) 267 #define WPA_PUT_BE32(a, val) \ 268 do { \ 269 (a)[0] = (u8) ((((u32) (val)) >> 24) & 0xff); \ 270 (a)[1] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 271 (a)[2] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 272 (a)[3] = (u8) (((u32) (val)) & 0xff); \ 273 } while (0) 274 275 #define WPA_GET_LE32(a) ((((u32) (a)[3]) << 24) | (((u32) (a)[2]) << 16) | \ 276 (((u32) (a)[1]) << 8) | ((u32) (a)[0])) 277 #define WPA_PUT_LE32(a, val) \ 278 do { \ 279 (a)[3] = (u8) ((((u32) (val)) >> 24) & 0xff); \ 280 (a)[2] = (u8) ((((u32) (val)) >> 16) & 0xff); \ 281 (a)[1] = (u8) ((((u32) (val)) >> 8) & 0xff); \ 282 (a)[0] = (u8) (((u32) (val)) & 0xff); \ 283 } while (0) 284 285 #define WPA_GET_BE64(a) ((((u64) (a)[0]) << 56) | (((u64) (a)[1]) << 48) | \ 286 (((u64) (a)[2]) << 40) | (((u64) (a)[3]) << 32) | \ 287 (((u64) (a)[4]) << 24) | (((u64) (a)[5]) << 16) | \ 288 (((u64) (a)[6]) << 8) | ((u64) (a)[7])) 289 #define WPA_PUT_BE64(a, val) \ 290 do { \ 291 (a)[0] = (u8) (((u64) (val)) >> 56); \ 292 (a)[1] = (u8) (((u64) (val)) >> 48); \ 293 (a)[2] = (u8) (((u64) (val)) >> 40); \ 294 (a)[3] = (u8) (((u64) (val)) >> 32); \ 295 (a)[4] = (u8) (((u64) (val)) >> 24); \ 296 (a)[5] = (u8) (((u64) (val)) >> 16); \ 297 (a)[6] = (u8) (((u64) (val)) >> 8); \ 298 (a)[7] = (u8) (((u64) (val)) & 0xff); \ 299 } while (0) 300 301 #define WPA_GET_LE64(a) ((((u64) (a)[7]) << 56) | (((u64) (a)[6]) << 48) | \ 302 (((u64) (a)[5]) << 40) | (((u64) (a)[4]) << 32) | \ 303 (((u64) (a)[3]) << 24) | (((u64) (a)[2]) << 16) | \ 304 (((u64) (a)[1]) << 8) | ((u64) (a)[0])) 305 306 307 #ifndef ETH_ALEN 308 #define ETH_ALEN 6 309 #endif 310 311 312 #ifdef __GNUC__ 313 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b)))) 314 #define STRUCT_PACKED __attribute__ ((packed)) 315 #else 316 #define PRINTF_FORMAT(a,b) 317 #define STRUCT_PACKED 318 #endif 319 320 321 #ifdef CONFIG_ANSI_C_EXTRA 322 323 #if !defined(_MSC_VER) || _MSC_VER < 1400 324 /* snprintf - used in number of places; sprintf() is _not_ a good replacement 325 * due to possible buffer overflow; see, e.g., 326 * http://www.ijs.si/software/snprintf/ for portable implementation of 327 * snprintf. */ 328 int snprintf(char *str, size_t size, const char *format, ...); 329 330 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */ 331 int vsnprintf(char *str, size_t size, const char *format, va_list ap); 332 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */ 333 334 /* getopt - only used in main.c */ 335 int getopt(int argc, char *const argv[], const char *optstring); 336 extern char *optarg; 337 extern int optind; 338 339 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF 340 #ifndef __socklen_t_defined 341 typedef int socklen_t; 342 #endif 343 #endif 344 345 /* inline - define as __inline or just define it to be empty, if needed */ 346 #ifdef CONFIG_NO_INLINE 347 #define inline 348 #else 349 #define inline __inline 350 #endif 351 352 #ifndef __func__ 353 #define __func__ "__func__ not defined" 354 #endif 355 356 #ifndef bswap_16 357 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff)) 358 #endif 359 360 #ifndef bswap_32 361 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \ 362 (((u32) (a) << 8) & 0xff0000) | \ 363 (((u32) (a) >> 8) & 0xff00) | \ 364 (((u32) (a) >> 24) & 0xff)) 365 #endif 366 367 #ifndef MSG_DONTWAIT 368 #define MSG_DONTWAIT 0 369 #endif 370 371 #ifdef _WIN32_WCE 372 void perror(const char *s); 373 #endif /* _WIN32_WCE */ 374 375 #endif /* CONFIG_ANSI_C_EXTRA */ 376 377 #ifndef MAC2STR 378 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] 379 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" 380 #endif 381 382 #ifndef BIT 383 #define BIT(x) (1 << (x)) 384 #endif 385 386 /* 387 * Definitions for sparse validation 388 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/) 389 */ 390 #ifdef __CHECKER__ 391 #define __force __attribute__((force)) 392 #define __bitwise __attribute__((bitwise)) 393 #else 394 #define __force 395 #define __bitwise 396 #endif 397 398 typedef u16 __bitwise be16; 399 typedef u16 __bitwise le16; 400 typedef u32 __bitwise be32; 401 typedef u32 __bitwise le32; 402 typedef u64 __bitwise be64; 403 typedef u64 __bitwise le64; 404 405 #ifndef __must_check 406 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 407 #define __must_check __attribute__((__warn_unused_result__)) 408 #else 409 #define __must_check 410 #endif /* __GNUC__ */ 411 #endif /* __must_check */ 412 413 int hwaddr_aton(const char *txt, u8 *addr); 414 int hexstr2bin(const char *hex, u8 *buf, size_t len); 415 void inc_byte_array(u8 *counter, size_t len); 416 void wpa_get_ntp_timestamp(u8 *buf); 417 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len); 418 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, 419 size_t len); 420 421 #ifdef CONFIG_NATIVE_WINDOWS 422 void wpa_unicode2ascii_inplace(TCHAR *str); 423 TCHAR * wpa_strdup_tchar(const char *str); 424 #else /* CONFIG_NATIVE_WINDOWS */ 425 #define wpa_unicode2ascii_inplace(s) do { } while (0) 426 #define wpa_strdup_tchar(s) strdup((s)) 427 #endif /* CONFIG_NATIVE_WINDOWS */ 428 429 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len); 430 431 static inline int is_zero_ether_addr(const u8 *a) 432 { 433 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]); 434 } 435 436 #include "wpa_debug.h" 437 438 #endif /* COMMON_H */ 439