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