Home | History | Annotate | Line # | Download | only in utils
      1 /*
      2  * wpa_supplicant/hostapd / common helper functions, etc.
      3  * Copyright (c) 2002-2007, Jouni Malinen <j (at) w1.fi>
      4  *
      5  * This software may be distributed under the terms of the BSD license.
      6  * See README for more details.
      7  */
      8 
      9 #ifndef COMMON_H
     10 #define COMMON_H
     11 
     12 #include "os.h"
     13 
     14 #if defined(__linux__) || defined(__GLIBC__)
     15 #include <endian.h>
     16 #include <byteswap.h>
     17 #endif /* __linux__ */
     18 
     19 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
     20     defined(__OpenBSD__)
     21 #include <sys/types.h>
     22 #include <sys/endian.h>
     23 #define __BYTE_ORDER	_BYTE_ORDER
     24 #define	__LITTLE_ENDIAN	_LITTLE_ENDIAN
     25 #define	__BIG_ENDIAN	_BIG_ENDIAN
     26 #ifdef __OpenBSD__
     27 #define bswap_16 swap16
     28 #define bswap_32 swap32
     29 #define bswap_64 swap64
     30 #else /* __OpenBSD__ */
     31 #define bswap_16 bswap16
     32 #define bswap_32 bswap32
     33 #define bswap_64 bswap64
     34 #endif /* __OpenBSD__ */
     35 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
     36 	* defined(__DragonFly__) || defined(__OpenBSD__) */
     37 
     38 #ifdef __APPLE__
     39 #include <sys/types.h>
     40 #include <machine/endian.h>
     41 #define __BYTE_ORDER	_BYTE_ORDER
     42 #define __LITTLE_ENDIAN	_LITTLE_ENDIAN
     43 #define __BIG_ENDIAN	_BIG_ENDIAN
     44 static inline unsigned short bswap_16(unsigned short v)
     45 {
     46 	return ((v & 0xff) << 8) | (v >> 8);
     47 }
     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 
     56 #ifdef __NetBSD__
     57 #include <net/if.h>
     58 #endif
     59 
     60 #ifdef __rtems__
     61 #include <rtems/endian.h>
     62 #define __BYTE_ORDER BYTE_ORDER
     63 #define __LITTLE_ENDIAN LITTLE_ENDIAN
     64 #define __BIG_ENDIAN BIG_ENDIAN
     65 #define bswap_16 CPU_swap_u16
     66 #define bswap_32 CPU_swap_u32
     67 #endif /* __rtems__ */
     68 
     69 #ifdef CONFIG_NATIVE_WINDOWS
     70 #include <winsock.h>
     71 
     72 typedef int socklen_t;
     73 
     74 #ifndef MSG_DONTWAIT
     75 #define MSG_DONTWAIT 0 /* not supported */
     76 #endif
     77 
     78 #endif /* CONFIG_NATIVE_WINDOWS */
     79 
     80 #ifdef _MSC_VER
     81 #define inline __inline
     82 
     83 #undef vsnprintf
     84 #define vsnprintf _vsnprintf
     85 #undef close
     86 #define close closesocket
     87 #endif /* _MSC_VER */
     88 
     89 
     90 /* Define platform specific integer types */
     91 
     92 #ifdef _MSC_VER
     93 typedef UINT64 u64;
     94 typedef UINT32 u32;
     95 typedef UINT16 u16;
     96 typedef UINT8 u8;
     97 typedef INT64 s64;
     98 typedef INT32 s32;
     99 typedef INT16 s16;
    100 typedef INT8 s8;
    101 #define WPA_TYPES_DEFINED
    102 #endif /* _MSC_VER */
    103 
    104 #ifdef __vxworks
    105 typedef unsigned long long u64;
    106 typedef UINT32 u32;
    107 typedef UINT16 u16;
    108 typedef UINT8 u8;
    109 typedef long long s64;
    110 typedef INT32 s32;
    111 typedef INT16 s16;
    112 typedef INT8 s8;
    113 #define WPA_TYPES_DEFINED
    114 #endif /* __vxworks */
    115 
    116 #ifndef WPA_TYPES_DEFINED
    117 #ifdef CONFIG_USE_INTTYPES_H
    118 #include <inttypes.h>
    119 #else
    120 #include <stdint.h>
    121 #endif
    122 typedef uint64_t u64;
    123 typedef uint32_t u32;
    124 typedef uint16_t u16;
    125 typedef uint8_t u8;
    126 typedef int64_t s64;
    127 typedef int32_t s32;
    128 typedef int16_t s16;
    129 typedef int8_t s8;
    130 #define WPA_TYPES_DEFINED
    131 #endif /* !WPA_TYPES_DEFINED */
    132 
    133 
    134 /* Define platform specific byte swapping macros */
    135 
    136 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
    137 
    138 static inline unsigned short wpa_swap_16(unsigned short v)
    139 {
    140 	return ((v & 0xff) << 8) | (v >> 8);
    141 }
    142 
    143 static inline unsigned int wpa_swap_32(unsigned int v)
    144 {
    145 	return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
    146 		((v & 0xff0000) >> 8) | (v >> 24);
    147 }
    148 
    149 #define le_to_host16(n) (n)
    150 #define host_to_le16(n) (n)
    151 #define be_to_host16(n) wpa_swap_16(n)
    152 #define host_to_be16(n) wpa_swap_16(n)
    153 #define le_to_host32(n) (n)
    154 #define host_to_le32(n) (n)
    155 #define be_to_host32(n) wpa_swap_32(n)
    156 #define host_to_be32(n) wpa_swap_32(n)
    157 #define host_to_le64(n) (n)
    158 
    159 #define WPA_BYTE_SWAP_DEFINED
    160 
    161 #endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
    162 
    163 
    164 #ifndef WPA_BYTE_SWAP_DEFINED
    165 
    166 #ifndef __BYTE_ORDER
    167 #ifndef __LITTLE_ENDIAN
    168 #ifndef __BIG_ENDIAN
    169 #define __LITTLE_ENDIAN 1234
    170 #define __BIG_ENDIAN 4321
    171 #if defined(sparc)
    172 #define __BYTE_ORDER __BIG_ENDIAN
    173 #endif
    174 #endif /* __BIG_ENDIAN */
    175 #endif /* __LITTLE_ENDIAN */
    176 #endif /* __BYTE_ORDER */
    177 
    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 host_to_le32(n) bswap_32(n)
    198 #define be_to_host32(n) (n)
    199 #define host_to_be32(n) (n)
    200 #define le_to_host64(n) bswap_64(n)
    201 #define host_to_le64(n) bswap_64(n)
    202 #define be_to_host64(n) (n)
    203 #define host_to_be64(n) (n)
    204 #ifndef WORDS_BIGENDIAN
    205 #define WORDS_BIGENDIAN
    206 #endif
    207 #else
    208 #error Could not determine CPU byte order
    209 #endif
    210 
    211 #define WPA_BYTE_SWAP_DEFINED
    212 #endif /* !WPA_BYTE_SWAP_DEFINED */
    213 
    214 
    215 /* Macros for handling unaligned memory accesses */
    216 
    217 static inline u16 WPA_GET_BE16(const u8 *a)
    218 {
    219 	return (a[0] << 8) | a[1];
    220 }
    221 
    222 static inline void WPA_PUT_BE16(u8 *a, u16 val)
    223 {
    224 	a[0] = val >> 8;
    225 	a[1] = val & 0xff;
    226 }
    227 
    228 static inline u16 WPA_GET_LE16(const u8 *a)
    229 {
    230 	return (a[1] << 8) | a[0];
    231 }
    232 
    233 static inline void WPA_PUT_LE16(u8 *a, u16 val)
    234 {
    235 	a[1] = val >> 8;
    236 	a[0] = val & 0xff;
    237 }
    238 
    239 static inline u32 WPA_GET_BE24(const u8 *a)
    240 {
    241 	return (a[0] << 16) | (a[1] << 8) | a[2];
    242 }
    243 
    244 static inline void WPA_PUT_BE24(u8 *a, u32 val)
    245 {
    246 	a[0] = (val >> 16) & 0xff;
    247 	a[1] = (val >> 8) & 0xff;
    248 	a[2] = val & 0xff;
    249 }
    250 
    251 static inline u32 WPA_GET_LE24(const u8 *a)
    252 {
    253 	return (a[2] << 16) | (a[1] << 8) | a[0];
    254 }
    255 
    256 static inline void WPA_PUT_LE24(u8 *a, u32 val)
    257 {
    258 	a[2] = (val >> 16) & 0xff;
    259 	a[1] = (val >> 8) & 0xff;
    260 	a[0] = val & 0xff;
    261 }
    262 
    263 static inline u32 WPA_GET_BE32(const u8 *a)
    264 {
    265 	return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
    266 }
    267 
    268 static inline void WPA_PUT_BE32(u8 *a, u32 val)
    269 {
    270 	a[0] = (val >> 24) & 0xff;
    271 	a[1] = (val >> 16) & 0xff;
    272 	a[2] = (val >> 8) & 0xff;
    273 	a[3] = val & 0xff;
    274 }
    275 
    276 static inline u32 WPA_GET_LE32(const u8 *a)
    277 {
    278 	return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
    279 }
    280 
    281 static inline void WPA_PUT_LE32(u8 *a, u32 val)
    282 {
    283 	a[3] = (val >> 24) & 0xff;
    284 	a[2] = (val >> 16) & 0xff;
    285 	a[1] = (val >> 8) & 0xff;
    286 	a[0] = val & 0xff;
    287 }
    288 
    289 static inline u64 WPA_GET_BE64(const u8 *a)
    290 {
    291 	return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
    292 		(((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
    293 		(((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
    294 		(((u64) a[6]) << 8) | ((u64) a[7]);
    295 }
    296 
    297 static inline void WPA_PUT_BE64(u8 *a, u64 val)
    298 {
    299 	a[0] = val >> 56;
    300 	a[1] = val >> 48;
    301 	a[2] = val >> 40;
    302 	a[3] = val >> 32;
    303 	a[4] = val >> 24;
    304 	a[5] = val >> 16;
    305 	a[6] = val >> 8;
    306 	a[7] = val & 0xff;
    307 }
    308 
    309 static inline u64 WPA_GET_LE64(const u8 *a)
    310 {
    311 	return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
    312 		(((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
    313 		(((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
    314 		(((u64) a[1]) << 8) | ((u64) a[0]);
    315 }
    316 
    317 static inline void WPA_PUT_LE64(u8 *a, u64 val)
    318 {
    319 	a[7] = val >> 56;
    320 	a[6] = val >> 48;
    321 	a[5] = val >> 40;
    322 	a[4] = val >> 32;
    323 	a[3] = val >> 24;
    324 	a[2] = val >> 16;
    325 	a[1] = val >> 8;
    326 	a[0] = val & 0xff;
    327 }
    328 
    329 
    330 #ifndef ETH_ALEN
    331 #define ETH_ALEN 6
    332 #endif
    333 #ifndef ETH_HLEN
    334 #define ETH_HLEN 14
    335 #endif
    336 #ifndef IFNAMSIZ
    337 #define IFNAMSIZ 16
    338 #endif
    339 #ifndef ETH_P_ALL
    340 #define ETH_P_ALL 0x0003
    341 #endif
    342 #ifndef ETH_P_IP
    343 #define ETH_P_IP 0x0800
    344 #endif
    345 #ifndef ETH_P_80211_ENCAP
    346 #define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
    347 #endif
    348 #ifndef ETH_P_PAE
    349 #define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
    350 #endif /* ETH_P_PAE */
    351 #ifndef ETH_P_EAPOL
    352 #define ETH_P_EAPOL ETH_P_PAE
    353 #endif /* ETH_P_EAPOL */
    354 #ifndef ETH_P_RSN_PREAUTH
    355 #define ETH_P_RSN_PREAUTH 0x88c7
    356 #endif /* ETH_P_RSN_PREAUTH */
    357 #ifndef ETH_P_RRB
    358 #define ETH_P_RRB 0x890D
    359 #endif /* ETH_P_RRB */
    360 #ifndef ETH_P_OUI
    361 #define ETH_P_OUI 0x88B7
    362 #endif /* ETH_P_OUI */
    363 #ifndef ETH_P_8021Q
    364 #define ETH_P_8021Q 0x8100
    365 #endif /* ETH_P_8021Q */
    366 
    367 
    368 #ifdef __GNUC__
    369 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
    370 #define STRUCT_PACKED __attribute__ ((packed))
    371 #else
    372 #define PRINTF_FORMAT(a,b)
    373 #define STRUCT_PACKED
    374 #endif
    375 
    376 
    377 #ifdef CONFIG_ANSI_C_EXTRA
    378 
    379 #if !defined(_MSC_VER) || _MSC_VER < 1400
    380 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
    381  * due to possible buffer overflow; see, e.g.,
    382  * http://www.ijs.si/software/snprintf/ for portable implementation of
    383  * snprintf. */
    384 int snprintf(char *str, size_t size, const char *format, ...);
    385 
    386 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
    387 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
    388 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
    389 
    390 /* getopt - only used in main.c */
    391 int getopt(int argc, char *const argv[], const char *optstring);
    392 extern char *optarg;
    393 extern int optind;
    394 
    395 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
    396 #ifndef __socklen_t_defined
    397 typedef int socklen_t;
    398 #endif
    399 #endif
    400 
    401 /* inline - define as __inline or just define it to be empty, if needed */
    402 #ifdef CONFIG_NO_INLINE
    403 #define inline
    404 #else
    405 #define inline __inline
    406 #endif
    407 
    408 #ifndef __func__
    409 #define __func__ "__func__ not defined"
    410 #endif
    411 
    412 #ifndef bswap_16
    413 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
    414 #endif
    415 
    416 #ifndef bswap_32
    417 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
    418 		     (((u32) (a) << 8) & 0xff0000) | \
    419      		     (((u32) (a) >> 8) & 0xff00) | \
    420      		     (((u32) (a) >> 24) & 0xff))
    421 #endif
    422 
    423 #ifndef MSG_DONTWAIT
    424 #define MSG_DONTWAIT 0
    425 #endif
    426 
    427 #ifdef _WIN32_WCE
    428 void perror(const char *s);
    429 #endif /* _WIN32_WCE */
    430 
    431 #endif /* CONFIG_ANSI_C_EXTRA */
    432 
    433 #ifndef MAC2STR
    434 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
    435 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
    436 
    437 /*
    438  * Compact form for string representation of MAC address
    439  * To be used, e.g., for constructing dbus paths for P2P Devices
    440  */
    441 #define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
    442 #endif
    443 
    444 #ifndef BIT
    445 #define BIT(x) (1U << (x))
    446 #endif
    447 
    448 #ifndef MIN
    449 #define MIN(a, b) ((a) < (b) ? (a) : (b))
    450 #endif
    451 #ifndef MAX
    452 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
    453 #endif
    454 
    455 /*
    456  * Definitions for sparse validation
    457  * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
    458  */
    459 #ifdef __CHECKER__
    460 #define __force __attribute__((force))
    461 #undef __bitwise
    462 #define __bitwise __attribute__((bitwise))
    463 #else
    464 #define __force
    465 #undef __bitwise
    466 #define __bitwise
    467 #endif
    468 
    469 typedef u16 __bitwise be16;
    470 typedef u16 __bitwise le16;
    471 typedef u32 __bitwise be32;
    472 typedef u32 __bitwise le32;
    473 typedef u64 __bitwise be64;
    474 typedef u64 __bitwise le64;
    475 
    476 #ifndef __must_check
    477 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
    478 #define __must_check __attribute__((__warn_unused_result__))
    479 #else
    480 #define __must_check
    481 #endif /* __GNUC__ */
    482 #endif /* __must_check */
    483 
    484 #ifndef __maybe_unused
    485 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
    486 #define __maybe_unused __attribute__((unused))
    487 #else
    488 #define __maybe_unused
    489 #endif /* __GNUC__ */
    490 #endif /* __must_check */
    491 
    492 #define SSID_MAX_LEN 32
    493 
    494 struct wpa_ssid_value {
    495 	u8 ssid[SSID_MAX_LEN];
    496 	size_t ssid_len;
    497 };
    498 
    499 int hwaddr_aton(const char *txt, u8 *addr);
    500 int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
    501 int hwaddr_compact_aton(const char *txt, u8 *addr);
    502 int hwaddr_aton2(const char *txt, u8 *addr);
    503 int hex2num(char c);
    504 int hex2byte(const char *hex);
    505 int hexstr2bin(const char *hex, u8 *buf, size_t len);
    506 void inc_byte_array(u8 *counter, size_t len);
    507 void buf_shift_right(u8 *buf, size_t len, size_t bits);
    508 void wpa_get_ntp_timestamp(u8 *buf);
    509 int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
    510 	PRINTF_FORMAT(3, 4);
    511 int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
    512 			 char sep);
    513 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
    514 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
    515 			       size_t len);
    516 
    517 int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
    518 int ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
    519 
    520 #ifdef CONFIG_NATIVE_WINDOWS
    521 void wpa_unicode2ascii_inplace(TCHAR *str);
    522 TCHAR * wpa_strdup_tchar(const char *str);
    523 #else /* CONFIG_NATIVE_WINDOWS */
    524 #define wpa_unicode2ascii_inplace(s) do { } while (0)
    525 #define wpa_strdup_tchar(s) strdup((s))
    526 #endif /* CONFIG_NATIVE_WINDOWS */
    527 
    528 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
    529 size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
    530 
    531 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
    532 
    533 char * wpa_config_parse_string(const char *value, size_t *len);
    534 int is_hex(const u8 *data, size_t len);
    535 int has_ctrl_char(const u8 *data, size_t len);
    536 int has_newline(const char *str);
    537 size_t merge_byte_arrays(u8 *res, size_t res_len,
    538 			 const u8 *src1, size_t src1_len,
    539 			 const u8 *src2, size_t src2_len);
    540 char * dup_binstr(const void *src, size_t len);
    541 
    542 static inline int is_zero_ether_addr(const u8 *a)
    543 {
    544 	return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
    545 }
    546 
    547 static inline int is_broadcast_ether_addr(const u8 *a)
    548 {
    549 	return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
    550 }
    551 
    552 static inline int is_multicast_ether_addr(const u8 *a)
    553 {
    554 	return a[0] & 0x01;
    555 }
    556 
    557 static inline bool ether_addr_equal(const u8 *a, const u8 *b)
    558 {
    559 	return os_memcmp(a, b, ETH_ALEN) == 0;
    560 }
    561 
    562 #define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
    563 
    564 #include "wpa_debug.h"
    565 
    566 
    567 struct wpa_freq_range_list {
    568 	struct wpa_freq_range {
    569 		unsigned int min;
    570 		unsigned int max;
    571 	} *range;
    572 	unsigned int num;
    573 };
    574 
    575 int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
    576 int freq_range_list_includes(const struct wpa_freq_range_list *list,
    577 			     unsigned int freq);
    578 char * freq_range_list_str(const struct wpa_freq_range_list *list);
    579 
    580 size_t int_array_len(const int *a);
    581 void int_array_concat(int **res, const int *a);
    582 void int_array_sort_unique(int *a);
    583 void int_array_add_unique(int **res, int a);
    584 bool int_array_includes(int *arr, int val);
    585 
    586 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
    587 
    588 void str_clear_free(char *str);
    589 void bin_clear_free(void *bin, size_t len);
    590 
    591 int random_mac_addr(u8 *addr);
    592 int random_mac_addr_keep_oui(u8 *addr);
    593 
    594 const char * cstr_token(const char *str, const char *delim, const char **last);
    595 char * str_token(char *str, const char *delim, char **context);
    596 size_t utf8_escape(const char *inp, size_t in_size,
    597 		   char *outp, size_t out_size);
    598 size_t utf8_unescape(const char *inp, size_t in_size,
    599 		     char *outp, size_t out_size);
    600 int is_ctrl_char(char c);
    601 
    602 int str_starts(const char *str, const char *start);
    603 
    604 u8 rssi_to_rcpi(int rssi);
    605 char * get_param(const char *cmd, const char *param);
    606 
    607 #define for_each_link(__links, __i)                            \
    608 	for ((__i) = 0; (__i) < MAX_NUM_MLD_LINKS; (__i)++)    \
    609 		if ((__links) & BIT(__i))
    610 
    611 /* Iterate all links, or, if no link is defined, iterate given index */
    612 #define for_each_link_default(_links, _i, _def_idx)	\
    613 	for ((_i) = (_links) ? 0 : (_def_idx);		\
    614 	     (_i) < MAX_NUM_MLD_LINKS ||		\
    615 		     (!(_links) && (_i) == (_def_idx));	\
    616 	     (_i)++)					\
    617 		if (!(_links) || (_links) & BIT(_i))
    618 
    619 void forced_memzero(void *ptr, size_t len);
    620 
    621 /*
    622  * gcc 4.4 ends up generating strict-aliasing warnings about some very common
    623  * networking socket uses that do not really result in a real problem and
    624  * cannot be easily avoided with union-based type-punning due to struct
    625  * definitions including another struct in system header files. To avoid having
    626  * to fully disable strict-aliasing warnings, provide a mechanism to hide the
    627  * typecast from aliasing for now. A cleaner solution will hopefully be found
    628  * in the future to handle these cases.
    629  */
    630 void * __hide_aliasing_typecast(void *foo);
    631 #define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
    632 
    633 #ifdef CONFIG_VALGRIND
    634 #include <valgrind/memcheck.h>
    635 #define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
    636 #else /* CONFIG_VALGRIND */
    637 #define WPA_MEM_DEFINED(ptr, len) do { } while (0)
    638 #endif /* CONFIG_VALGRIND */
    639 
    640 #endif /* COMMON_H */
    641