common.h 13 KB

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