common.h revision fb79edc9df1f20461e90e478363d207348213d35
137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines/* 22c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * wpa_supplicant/hostapd / common helper functions, etc. 32c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> 42c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * 52c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * This software may be distributed under the terms of the BSD license. 62c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * See README for more details. 72c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha */ 82c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 92c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifndef COMMON_H 10de2d8694e25a814696358e95141f4b1aa4d8847ePirama Arumuga Nainar#define COMMON_H 112c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 122c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include "os.h" 132c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 142c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#if defined(__linux__) || defined(__GLIBC__) 152c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <endian.h> 162c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <byteswap.h> 172c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* __linux__ */ 182c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 19f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \ 20f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar defined(__OpenBSD__) 21f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include <sys/types.h> 22f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#include <sys/endian.h> 232c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __BYTE_ORDER _BYTE_ORDER 242c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __LITTLE_ENDIAN _LITTLE_ENDIAN 252c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __BIG_ENDIAN _BIG_ENDIAN 262c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef __OpenBSD__ 272c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_16 swap16 282c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_32 swap32 292c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_64 swap64 302c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#else /* __OpenBSD__ */ 312c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_16 bswap16 322c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_32 bswap32 332c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define bswap_64 bswap64 342c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* __OpenBSD__ */ 352c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* defined(__FreeBSD__) || defined(__NetBSD__) || 362c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * defined(__DragonFly__) || defined(__OpenBSD__) */ 372c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 382c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef __APPLE__ 392c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <sys/types.h> 402c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <machine/endian.h> 412c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __BYTE_ORDER _BYTE_ORDER 422c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __LITTLE_ENDIAN _LITTLE_ENDIAN 432c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define __BIG_ENDIAN _BIG_ENDIAN 442c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachastatic inline unsigned short bswap_16(unsigned short v) 452c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha{ 462c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha return ((v & 0xff) << 8) | (v >> 8); 472c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha} 482c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 492c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachastatic inline unsigned int bswap_32(unsigned int v) 5036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines{ 5136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | 5236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines ((v & 0xff0000) >> 8) | (v >> 24); 5336b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines} 5436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#endif /* __APPLE__ */ 5536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 5636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#ifdef CONFIG_TI_COMPILER 5736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define __BIG_ENDIAN 4321 5836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define __LITTLE_ENDIAN 1234 5936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#ifdef __big_endian__ 6036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define __BYTE_ORDER __BIG_ENDIAN 612c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#else 6236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define __BYTE_ORDER __LITTLE_ENDIAN 632c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif 642c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* CONFIG_TI_COMPILER */ 652c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 662c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef CONFIG_NATIVE_WINDOWS 672c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <winsock.h> 682c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 692c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef int socklen_t; 702c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 712c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifndef MSG_DONTWAIT 722c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define MSG_DONTWAIT 0 /* not supported */ 732c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif 7436b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#endif /* CONFIG_NATIVE_WINDOWS */ 7636b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 772c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef _MSC_VER 782c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define inline __inline 792c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 802c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#undef vsnprintf 812c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define vsnprintf _vsnprintf 822c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#undef close 832c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define close closesocket 846f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#endif /* _MSC_VER */ 856f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby 862c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 872c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha/* Define platform specific integer types */ 882c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 892c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef _MSC_VER 90dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestypedef UINT64 u64; 912c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef UINT32 u32; 922c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef UINT16 u16; 932c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef UINT8 u8; 946948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef INT64 s64; 956948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef INT32 s32; 962c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef INT16 s16; 976948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef INT8 s8; 982c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define WPA_TYPES_DEFINED 992c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* _MSC_VER */ 1002c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 101dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#ifdef __vxworks 1022c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef unsigned long long u64; 1032c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef UINT32 u32; 1042c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef UINT16 u16; 1056948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef UINT8 u8; 1066948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef long long s64; 1072c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef INT32 s32; 1086948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef INT16 s16; 1092c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef INT8 s8; 1102c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define WPA_TYPES_DEFINED 1112c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* __vxworks */ 112dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 1132c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef CONFIG_TI_COMPILER 1146948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#ifdef _LLONG_AVAILABLE 1152c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef unsigned long long u64; 1162c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#else 1172c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha/* 1182c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * TODO: 64-bit variable not available. Using long as a workaround to test the 1192c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha * build, but this will likely not work for all operations. 1206948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar */ 1212c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef unsigned long u64; 1226948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#endif 123dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hinestypedef unsigned int u32; 1246948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef unsigned short u16; 1252c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef unsigned char u8; 1262c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define WPA_TYPES_DEFINED 1272c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* CONFIG_TI_COMPILER */ 128dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines 1296948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#ifndef WPA_TYPES_DEFINED 1302c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#ifdef CONFIG_USE_INTTYPES_H 1312c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#include <inttypes.h> 1322c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#else 133dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#include <stdint.h> 1342c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif 1352c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef uint64_t u64; 1366948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainartypedef uint32_t u32; 1372c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef uint16_t u16; 1382c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef uint8_t u8; 1392c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef int64_t s64; 1402c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef int32_t s32; 1412c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef int16_t s16; 1422c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachatypedef int8_t s8; 1436948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar#define WPA_TYPES_DEFINED 1442c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* !WPA_TYPES_DEFINED */ 1452c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 1462c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 1472c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha/* Define platform specific byte swapping macros */ 1482c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 1492c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS) 1502c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 1512c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougachastatic inline unsigned short wpa_swap_16(unsigned short v) 1522c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha{ 1532c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha return ((v & 0xff) << 8) | (v >> 8); 1542c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha} 1556f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby 1566f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderbystatic inline unsigned int wpa_swap_32(unsigned int v) 1572c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha{ 1582c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha return ((v & 0xff) << 24) | ((v & 0xff00) << 8) | 1592c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha ((v & 0xff0000) >> 8) | (v >> 24); 1602c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha} 1612c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 1622c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define le_to_host16(n) (n) 1632c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#define host_to_le16(n) (n) 1646f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define be_to_host16(n) wpa_swap_16(n) 1656f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define host_to_be16(n) wpa_swap_16(n) 1666f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define le_to_host32(n) (n) 16736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define be_to_host32(n) wpa_swap_32(n) 16836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define host_to_be32(n) wpa_swap_32(n) 16936b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 17036b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#define WPA_BYTE_SWAP_DEFINED 17136b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines 1726f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */ 1736f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby 1746f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby 1756f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#ifndef WPA_BYTE_SWAP_DEFINED 1766f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby 1776f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#ifndef __BYTE_ORDER 1786f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#ifndef __LITTLE_ENDIAN 1796f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#ifndef __BIG_ENDIAN 1806f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define __LITTLE_ENDIAN 1234 1816f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define __BIG_ENDIAN 4321 1826f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#if defined(sparc) 1836f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#define __BYTE_ORDER __BIG_ENDIAN 1846f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#endif 1856f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#endif /* __BIG_ENDIAN */ 1866f45b1f0d689d2cafd3a64be9b548bb8bb0927b0Kevin Enderby#endif /* __LITTLE_ENDIAN */ 1872c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha#endif /* __BYTE_ORDER */ 1882c94d0faa0e1c268893d5e04dc77e8a35889db00Ahmed Bougacha 189de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#if __BYTE_ORDER == __LITTLE_ENDIAN 190de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#define le_to_host16(n) ((__force u16) (le16) (n)) 191f3ef5332fa3f4d5ec72c178a2b19dac363a19383Pirama Arumuga Nainar#define host_to_le16(n) ((__force le16) (u16) (n)) 192de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#define be_to_host16(n) bswap_16((__force u16) (be16) (n)) 193ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define host_to_be16(n) ((__force be16) bswap_16((n))) 194ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define le_to_host32(n) ((__force u32) (le32) (n)) 195dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define host_to_le32(n) ((__force le32) (u32) (n)) 196de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#define be_to_host32(n) bswap_32((__force u32) (be32) (n)) 197ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define host_to_be32(n) ((__force be32) bswap_32((n))) 198ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#define le_to_host64(n) ((__force u64) (le64) (n)) 199de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#define host_to_le64(n) ((__force le64) (u64) (n)) 200de7cbbfcce5c068f0699bdcb6dac093c0c91ba6fQuentin Colombet#define be_to_host64(n) bswap_64((__force u64) (be64) (n)) 201#define host_to_be64(n) ((__force be64) bswap_64((n))) 202#elif __BYTE_ORDER == __BIG_ENDIAN 203#define le_to_host16(n) bswap_16(n) 204#define host_to_le16(n) bswap_16(n) 205#define be_to_host16(n) (n) 206#define host_to_be16(n) (n) 207#define le_to_host32(n) bswap_32(n) 208#define host_to_le32(n) bswap_32(n) 209#define be_to_host32(n) (n) 210#define host_to_be32(n) (n) 211#define le_to_host64(n) bswap_64(n) 212#define host_to_le64(n) bswap_64(n) 213#define be_to_host64(n) (n) 214#define host_to_be64(n) (n) 215#ifndef WORDS_BIGENDIAN 216#define WORDS_BIGENDIAN 217#endif 218#else 219#error Could not determine CPU byte order 220#endif 221 222#define WPA_BYTE_SWAP_DEFINED 223#endif /* !WPA_BYTE_SWAP_DEFINED */ 224 225 226/* Macros for handling unaligned memory accesses */ 227 228static inline u16 WPA_GET_BE16(const u8 *a) 229{ 230 return (a[0] << 8) | a[1]; 231} 232 233static inline void WPA_PUT_BE16(u8 *a, u16 val) 234{ 235 a[0] = val >> 8; 236 a[1] = val & 0xff; 237} 238 239static inline u16 WPA_GET_LE16(const u8 *a) 240{ 241 return (a[1] << 8) | a[0]; 242} 243 244static inline void WPA_PUT_LE16(u8 *a, u16 val) 245{ 246 a[1] = val >> 8; 247 a[0] = val & 0xff; 248} 249 250static inline u32 WPA_GET_BE24(const u8 *a) 251{ 252 return (a[0] << 16) | (a[1] << 8) | a[2]; 253} 254 255static inline void WPA_PUT_BE24(u8 *a, u32 val) 256{ 257 a[0] = (val >> 16) & 0xff; 258 a[1] = (val >> 8) & 0xff; 259 a[2] = val & 0xff; 260} 261 262static inline u32 WPA_GET_BE32(const u8 *a) 263{ 264 return (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3]; 265} 266 267static inline void WPA_PUT_BE32(u8 *a, u32 val) 268{ 269 a[0] = (val >> 24) & 0xff; 270 a[1] = (val >> 16) & 0xff; 271 a[2] = (val >> 8) & 0xff; 272 a[3] = val & 0xff; 273} 274 275static inline u32 WPA_GET_LE32(const u8 *a) 276{ 277 return (a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0]; 278} 279 280static inline void WPA_PUT_LE32(u8 *a, u32 val) 281{ 282 a[3] = (val >> 24) & 0xff; 283 a[2] = (val >> 16) & 0xff; 284 a[1] = (val >> 8) & 0xff; 285 a[0] = val & 0xff; 286} 287 288static inline u64 WPA_GET_BE64(const u8 *a) 289{ 290 return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) | 291 (((u64) a[2]) << 40) | (((u64) a[3]) << 32) | 292 (((u64) a[4]) << 24) | (((u64) a[5]) << 16) | 293 (((u64) a[6]) << 8) | ((u64) a[7]); 294} 295 296static inline void WPA_PUT_BE64(u8 *a, u64 val) 297{ 298 a[0] = val >> 56; 299 a[1] = val >> 48; 300 a[2] = val >> 40; 301 a[3] = val >> 32; 302 a[4] = val >> 24; 303 a[5] = val >> 16; 304 a[6] = val >> 8; 305 a[7] = val & 0xff; 306} 307 308static inline u64 WPA_GET_LE64(const u8 *a) 309{ 310 return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) | 311 (((u64) a[5]) << 40) | (((u64) a[4]) << 32) | 312 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) | 313 (((u64) a[1]) << 8) | ((u64) a[0]); 314} 315 316static inline void WPA_PUT_LE64(u8 *a, u64 val) 317{ 318 a[7] = val >> 56; 319 a[6] = val >> 48; 320 a[5] = val >> 40; 321 a[4] = val >> 32; 322 a[3] = val >> 24; 323 a[2] = val >> 16; 324 a[1] = val >> 8; 325 a[0] = val & 0xff; 326} 327 328 329#ifndef ETH_ALEN 330#define ETH_ALEN 6 331#endif 332#ifndef IFNAMSIZ 333#define IFNAMSIZ 16 334#endif 335#ifndef ETH_P_ALL 336#define ETH_P_ALL 0x0003 337#endif 338#ifndef ETH_P_80211_ENCAP 339#define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */ 340#endif 341#ifndef ETH_P_PAE 342#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */ 343#endif /* ETH_P_PAE */ 344#ifndef ETH_P_EAPOL 345#define ETH_P_EAPOL ETH_P_PAE 346#endif /* ETH_P_EAPOL */ 347#ifndef ETH_P_RSN_PREAUTH 348#define ETH_P_RSN_PREAUTH 0x88c7 349#endif /* ETH_P_RSN_PREAUTH */ 350#ifndef ETH_P_RRB 351#define ETH_P_RRB 0x890D 352#endif /* ETH_P_RRB */ 353 354 355#ifdef __GNUC__ 356#define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b)))) 357#define STRUCT_PACKED __attribute__ ((packed)) 358#else 359#define PRINTF_FORMAT(a,b) 360#define STRUCT_PACKED 361#endif 362 363 364#ifdef CONFIG_ANSI_C_EXTRA 365 366#if !defined(_MSC_VER) || _MSC_VER < 1400 367/* snprintf - used in number of places; sprintf() is _not_ a good replacement 368 * due to possible buffer overflow; see, e.g., 369 * http://www.ijs.si/software/snprintf/ for portable implementation of 370 * snprintf. */ 371int snprintf(char *str, size_t size, const char *format, ...); 372 373/* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */ 374int vsnprintf(char *str, size_t size, const char *format, va_list ap); 375#endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */ 376 377/* getopt - only used in main.c */ 378int getopt(int argc, char *const argv[], const char *optstring); 379extern char *optarg; 380extern int optind; 381 382#ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF 383#ifndef __socklen_t_defined 384typedef int socklen_t; 385#endif 386#endif 387 388/* inline - define as __inline or just define it to be empty, if needed */ 389#ifdef CONFIG_NO_INLINE 390#define inline 391#else 392#define inline __inline 393#endif 394 395#ifndef __func__ 396#define __func__ "__func__ not defined" 397#endif 398 399#ifndef bswap_16 400#define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff)) 401#endif 402 403#ifndef bswap_32 404#define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \ 405 (((u32) (a) << 8) & 0xff0000) | \ 406 (((u32) (a) >> 8) & 0xff00) | \ 407 (((u32) (a) >> 24) & 0xff)) 408#endif 409 410#ifndef MSG_DONTWAIT 411#define MSG_DONTWAIT 0 412#endif 413 414#ifdef _WIN32_WCE 415void perror(const char *s); 416#endif /* _WIN32_WCE */ 417 418#endif /* CONFIG_ANSI_C_EXTRA */ 419 420#ifndef MAC2STR 421#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5] 422#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x" 423 424/* 425 * Compact form for string representation of MAC address 426 * To be used, e.g., for constructing dbus paths for P2P Devices 427 */ 428#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x" 429#endif 430 431#ifndef BIT 432#define BIT(x) (1 << (x)) 433#endif 434 435/* 436 * Definitions for sparse validation 437 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/) 438 */ 439#ifdef __CHECKER__ 440#define __force __attribute__((force)) 441#define __bitwise __attribute__((bitwise)) 442#else 443#define __force 444#define __bitwise 445#endif 446 447typedef u16 __bitwise be16; 448typedef u16 __bitwise le16; 449typedef u32 __bitwise be32; 450typedef u32 __bitwise le32; 451typedef u64 __bitwise be64; 452typedef u64 __bitwise le64; 453 454#ifndef __must_check 455#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 456#define __must_check __attribute__((__warn_unused_result__)) 457#else 458#define __must_check 459#endif /* __GNUC__ */ 460#endif /* __must_check */ 461 462#ifndef __maybe_unused 463#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) 464#define __maybe_unused __attribute__((unused)) 465#else 466#define __maybe_unused 467#endif /* __GNUC__ */ 468#endif /* __must_check */ 469 470int hwaddr_aton(const char *txt, u8 *addr); 471int hwaddr_compact_aton(const char *txt, u8 *addr); 472int hwaddr_aton2(const char *txt, u8 *addr); 473int hex2byte(const char *hex); 474int hexstr2bin(const char *hex, u8 *buf, size_t len); 475void inc_byte_array(u8 *counter, size_t len); 476void wpa_get_ntp_timestamp(u8 *buf); 477int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len); 478int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, 479 size_t len); 480 481#ifdef CONFIG_NATIVE_WINDOWS 482void wpa_unicode2ascii_inplace(TCHAR *str); 483TCHAR * wpa_strdup_tchar(const char *str); 484#else /* CONFIG_NATIVE_WINDOWS */ 485#define wpa_unicode2ascii_inplace(s) do { } while (0) 486#define wpa_strdup_tchar(s) strdup((s)) 487#endif /* CONFIG_NATIVE_WINDOWS */ 488 489void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len); 490size_t printf_decode(u8 *buf, size_t maxlen, const char *str); 491 492const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len); 493 494char * wpa_config_parse_string(const char *value, size_t *len); 495int is_hex(const u8 *data, size_t len); 496int find_first_bit(u32 value); 497size_t merge_byte_arrays(u8 *res, size_t res_len, 498 const u8 *src1, size_t src1_len, 499 const u8 *src2, size_t src2_len); 500char * dup_binstr(const void *src, size_t len); 501 502static inline int is_zero_ether_addr(const u8 *a) 503{ 504 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]); 505} 506 507static inline int is_broadcast_ether_addr(const u8 *a) 508{ 509 return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff; 510} 511 512#define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff" 513 514#include "wpa_debug.h" 515 516 517struct wpa_freq_range_list { 518 struct wpa_freq_range { 519 unsigned int min; 520 unsigned int max; 521 } *range; 522 unsigned int num; 523}; 524 525int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value); 526int freq_range_list_includes(const struct wpa_freq_range_list *list, 527 unsigned int freq); 528char * freq_range_list_str(const struct wpa_freq_range_list *list); 529 530int int_array_len(const int *a); 531void int_array_concat(int **res, const int *a); 532void int_array_sort_unique(int *a); 533void int_array_add_unique(int **res, int a); 534 535#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 536 537 538/* 539 * gcc 4.4 ends up generating strict-aliasing warnings about some very common 540 * networking socket uses that do not really result in a real problem and 541 * cannot be easily avoided with union-based type-punning due to struct 542 * definitions including another struct in system header files. To avoid having 543 * to fully disable strict-aliasing warnings, provide a mechanism to hide the 544 * typecast from aliasing for now. A cleaner solution will hopefully be found 545 * in the future to handle these cases. 546 */ 547void * __hide_aliasing_typecast(void *foo); 548#define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a)) 549 550#ifdef CONFIG_VALGRIND 551#include <valgrind/memcheck.h> 552#define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len)) 553#else /* CONFIG_VALGRIND */ 554#define WPA_MEM_DEFINED(ptr, len) do { } while (0) 555#endif /* CONFIG_VALGRIND */ 556 557#endif /* COMMON_H */ 558