blob: 3deb2046ef4c4d5db00efea2add068ddb80e620c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#ifndef COMMON_H
10#define COMMON_H
11
Sunil Ravib0ac25f2024-07-12 01:42:03 +000012#include "includes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#include "os.h"
14
15#if defined(__linux__) || defined(__GLIBC__)
16#include <endian.h>
17#include <byteswap.h>
18#endif /* __linux__ */
19
20#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
21 defined(__OpenBSD__)
22#include <sys/types.h>
23#include <sys/endian.h>
24#define __BYTE_ORDER _BYTE_ORDER
25#define __LITTLE_ENDIAN _LITTLE_ENDIAN
26#define __BIG_ENDIAN _BIG_ENDIAN
27#ifdef __OpenBSD__
28#define bswap_16 swap16
29#define bswap_32 swap32
30#define bswap_64 swap64
31#else /* __OpenBSD__ */
32#define bswap_16 bswap16
33#define bswap_32 bswap32
34#define bswap_64 bswap64
35#endif /* __OpenBSD__ */
36#endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
37 * defined(__DragonFly__) || defined(__OpenBSD__) */
38
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
45static inline unsigned short bswap_16(unsigned short v)
46{
47 return ((v & 0xff) << 8) | (v >> 8);
48}
49
50static inline unsigned int bswap_32(unsigned int v)
51{
52 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
53 ((v & 0xff0000) >> 8) | (v >> 24);
54}
55#endif /* __APPLE__ */
56
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070057#ifdef __rtems__
58#include <rtems/endian.h>
59#define __BYTE_ORDER BYTE_ORDER
60#define __LITTLE_ENDIAN LITTLE_ENDIAN
61#define __BIG_ENDIAN BIG_ENDIAN
62#define bswap_16 CPU_swap_u16
63#define bswap_32 CPU_swap_u32
64#endif /* __rtems__ */
65
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070066#ifdef CONFIG_NATIVE_WINDOWS
67#include <winsock.h>
68
69typedef int socklen_t;
70
71#ifndef MSG_DONTWAIT
72#define MSG_DONTWAIT 0 /* not supported */
73#endif
74
75#endif /* CONFIG_NATIVE_WINDOWS */
76
77#ifdef _MSC_VER
78#define inline __inline
79
80#undef vsnprintf
81#define vsnprintf _vsnprintf
82#undef close
83#define close closesocket
84#endif /* _MSC_VER */
85
86
87/* Define platform specific integer types */
88
89#ifdef _MSC_VER
90typedef UINT64 u64;
91typedef UINT32 u32;
92typedef UINT16 u16;
93typedef UINT8 u8;
94typedef INT64 s64;
95typedef INT32 s32;
96typedef INT16 s16;
97typedef INT8 s8;
98#define WPA_TYPES_DEFINED
99#endif /* _MSC_VER */
100
101#ifdef __vxworks
102typedef unsigned long long u64;
103typedef UINT32 u32;
104typedef UINT16 u16;
105typedef UINT8 u8;
106typedef long long s64;
107typedef INT32 s32;
108typedef INT16 s16;
109typedef INT8 s8;
110#define WPA_TYPES_DEFINED
111#endif /* __vxworks */
112
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113#ifndef WPA_TYPES_DEFINED
114#ifdef CONFIG_USE_INTTYPES_H
115#include <inttypes.h>
116#else
117#include <stdint.h>
118#endif
119typedef uint64_t u64;
120typedef uint32_t u32;
121typedef uint16_t u16;
122typedef uint8_t u8;
123typedef int64_t s64;
124typedef int32_t s32;
125typedef int16_t s16;
126typedef int8_t s8;
127#define WPA_TYPES_DEFINED
128#endif /* !WPA_TYPES_DEFINED */
129
130
131/* Define platform specific byte swapping macros */
132
133#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
134
135static inline unsigned short wpa_swap_16(unsigned short v)
136{
137 return ((v & 0xff) << 8) | (v >> 8);
138}
139
140static inline unsigned int wpa_swap_32(unsigned int v)
141{
142 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
143 ((v & 0xff0000) >> 8) | (v >> 24);
144}
145
146#define le_to_host16(n) (n)
147#define host_to_le16(n) (n)
148#define be_to_host16(n) wpa_swap_16(n)
149#define host_to_be16(n) wpa_swap_16(n)
150#define le_to_host32(n) (n)
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800151#define host_to_le32(n) (n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152#define be_to_host32(n) wpa_swap_32(n)
153#define host_to_be32(n) wpa_swap_32(n)
Roshan Pius3a1667e2018-07-03 15:17:14 -0700154#define host_to_le64(n) (n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155
156#define WPA_BYTE_SWAP_DEFINED
157
158#endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
159
160
161#ifndef WPA_BYTE_SWAP_DEFINED
162
163#ifndef __BYTE_ORDER
164#ifndef __LITTLE_ENDIAN
165#ifndef __BIG_ENDIAN
166#define __LITTLE_ENDIAN 1234
167#define __BIG_ENDIAN 4321
168#if defined(sparc)
169#define __BYTE_ORDER __BIG_ENDIAN
170#endif
171#endif /* __BIG_ENDIAN */
172#endif /* __LITTLE_ENDIAN */
173#endif /* __BYTE_ORDER */
174
175#if __BYTE_ORDER == __LITTLE_ENDIAN
176#define le_to_host16(n) ((__force u16) (le16) (n))
177#define host_to_le16(n) ((__force le16) (u16) (n))
178#define be_to_host16(n) bswap_16((__force u16) (be16) (n))
179#define host_to_be16(n) ((__force be16) bswap_16((n)))
180#define le_to_host32(n) ((__force u32) (le32) (n))
181#define host_to_le32(n) ((__force le32) (u32) (n))
182#define be_to_host32(n) bswap_32((__force u32) (be32) (n))
183#define host_to_be32(n) ((__force be32) bswap_32((n)))
184#define le_to_host64(n) ((__force u64) (le64) (n))
185#define host_to_le64(n) ((__force le64) (u64) (n))
186#define be_to_host64(n) bswap_64((__force u64) (be64) (n))
187#define host_to_be64(n) ((__force be64) bswap_64((n)))
188#elif __BYTE_ORDER == __BIG_ENDIAN
189#define le_to_host16(n) bswap_16(n)
190#define host_to_le16(n) bswap_16(n)
191#define be_to_host16(n) (n)
192#define host_to_be16(n) (n)
193#define le_to_host32(n) bswap_32(n)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700194#define host_to_le32(n) bswap_32(n)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195#define be_to_host32(n) (n)
196#define host_to_be32(n) (n)
197#define le_to_host64(n) bswap_64(n)
198#define host_to_le64(n) bswap_64(n)
199#define be_to_host64(n) (n)
200#define host_to_be64(n) (n)
201#ifndef WORDS_BIGENDIAN
202#define WORDS_BIGENDIAN
203#endif
204#else
205#error Could not determine CPU byte order
206#endif
207
208#define WPA_BYTE_SWAP_DEFINED
209#endif /* !WPA_BYTE_SWAP_DEFINED */
210
211
212/* Macros for handling unaligned memory accesses */
213
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800214static inline u16 WPA_GET_BE16(const u8 *a)
215{
216 return (a[0] << 8) | a[1];
217}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800219static inline void WPA_PUT_BE16(u8 *a, u16 val)
220{
221 a[0] = val >> 8;
222 a[1] = val & 0xff;
223}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700224
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800225static inline u16 WPA_GET_LE16(const u8 *a)
226{
227 return (a[1] << 8) | a[0];
228}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700229
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800230static inline void WPA_PUT_LE16(u8 *a, u16 val)
231{
232 a[1] = val >> 8;
233 a[0] = val & 0xff;
234}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800236static inline u32 WPA_GET_BE24(const u8 *a)
237{
238 return (a[0] << 16) | (a[1] << 8) | a[2];
239}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800241static inline void WPA_PUT_BE24(u8 *a, u32 val)
242{
243 a[0] = (val >> 16) & 0xff;
244 a[1] = (val >> 8) & 0xff;
245 a[2] = val & 0xff;
246}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000248static inline u32 WPA_GET_LE24(const u8 *a)
249{
250 return (a[2] << 16) | (a[1] << 8) | a[0];
251}
252
253static inline void WPA_PUT_LE24(u8 *a, u32 val)
254{
255 a[2] = (val >> 16) & 0xff;
256 a[1] = (val >> 8) & 0xff;
257 a[0] = val & 0xff;
258}
259
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800260static inline u32 WPA_GET_BE32(const u8 *a)
261{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800262 return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800263}
264
265static inline void WPA_PUT_BE32(u8 *a, u32 val)
266{
267 a[0] = (val >> 24) & 0xff;
268 a[1] = (val >> 16) & 0xff;
269 a[2] = (val >> 8) & 0xff;
270 a[3] = val & 0xff;
271}
272
273static inline u32 WPA_GET_LE32(const u8 *a)
274{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800275 return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800276}
277
278static inline void WPA_PUT_LE32(u8 *a, u32 val)
279{
280 a[3] = (val >> 24) & 0xff;
281 a[2] = (val >> 16) & 0xff;
282 a[1] = (val >> 8) & 0xff;
283 a[0] = val & 0xff;
284}
285
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000286static inline u64 WPA_GET_LE48(const u8 *a)
287{
288 return (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
289 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
290 (((u64) a[1]) << 8) | ((u64) a[0]);
291}
292
293static inline void WPA_PUT_LE48(u8 *a, u64 val)
294{
295 a[5] = val >> 40;
296 a[4] = val >> 32;
297 a[3] = val >> 24;
298 a[2] = val >> 16;
299 a[1] = val >> 8;
300 a[0] = val & 0xff;
301}
302
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800303static inline u64 WPA_GET_BE64(const u8 *a)
304{
305 return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
306 (((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
307 (((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
308 (((u64) a[6]) << 8) | ((u64) a[7]);
309}
310
311static inline void WPA_PUT_BE64(u8 *a, u64 val)
312{
313 a[0] = val >> 56;
314 a[1] = val >> 48;
315 a[2] = val >> 40;
316 a[3] = val >> 32;
317 a[4] = val >> 24;
318 a[5] = val >> 16;
319 a[6] = val >> 8;
320 a[7] = val & 0xff;
321}
322
323static inline u64 WPA_GET_LE64(const u8 *a)
324{
325 return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
326 (((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
327 (((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
328 (((u64) a[1]) << 8) | ((u64) a[0]);
329}
330
331static inline void WPA_PUT_LE64(u8 *a, u64 val)
332{
333 a[7] = val >> 56;
334 a[6] = val >> 48;
335 a[5] = val >> 40;
336 a[4] = val >> 32;
337 a[3] = val >> 24;
338 a[2] = val >> 16;
339 a[1] = val >> 8;
340 a[0] = val & 0xff;
341}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342
343
344#ifndef ETH_ALEN
345#define ETH_ALEN 6
346#endif
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800347#ifndef ETH_HLEN
348#define ETH_HLEN 14
349#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350#ifndef IFNAMSIZ
351#define IFNAMSIZ 16
352#endif
353#ifndef ETH_P_ALL
354#define ETH_P_ALL 0x0003
355#endif
Dmitry Shmidte4663042016-04-04 10:07:49 -0700356#ifndef ETH_P_IP
357#define ETH_P_IP 0x0800
358#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359#ifndef ETH_P_80211_ENCAP
360#define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
361#endif
362#ifndef ETH_P_PAE
363#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
364#endif /* ETH_P_PAE */
365#ifndef ETH_P_EAPOL
366#define ETH_P_EAPOL ETH_P_PAE
367#endif /* ETH_P_EAPOL */
368#ifndef ETH_P_RSN_PREAUTH
369#define ETH_P_RSN_PREAUTH 0x88c7
370#endif /* ETH_P_RSN_PREAUTH */
371#ifndef ETH_P_RRB
372#define ETH_P_RRB 0x890D
373#endif /* ETH_P_RRB */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700374#ifndef ETH_P_OUI
375#define ETH_P_OUI 0x88B7
376#endif /* ETH_P_OUI */
Hai Shalomc3565922019-10-28 11:58:20 -0700377#ifndef ETH_P_8021Q
378#define ETH_P_8021Q 0x8100
379#endif /* ETH_P_8021Q */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700380
381
382#ifdef __GNUC__
383#define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
384#define STRUCT_PACKED __attribute__ ((packed))
385#else
386#define PRINTF_FORMAT(a,b)
387#define STRUCT_PACKED
388#endif
389
390
391#ifdef CONFIG_ANSI_C_EXTRA
392
393#if !defined(_MSC_VER) || _MSC_VER < 1400
394/* snprintf - used in number of places; sprintf() is _not_ a good replacement
395 * due to possible buffer overflow; see, e.g.,
396 * http://www.ijs.si/software/snprintf/ for portable implementation of
397 * snprintf. */
398int snprintf(char *str, size_t size, const char *format, ...);
399
400/* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
401int vsnprintf(char *str, size_t size, const char *format, va_list ap);
402#endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
403
404/* getopt - only used in main.c */
405int getopt(int argc, char *const argv[], const char *optstring);
406extern char *optarg;
407extern int optind;
408
409#ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
410#ifndef __socklen_t_defined
411typedef int socklen_t;
412#endif
413#endif
414
415/* inline - define as __inline or just define it to be empty, if needed */
416#ifdef CONFIG_NO_INLINE
417#define inline
418#else
419#define inline __inline
420#endif
421
422#ifndef __func__
423#define __func__ "__func__ not defined"
424#endif
425
426#ifndef bswap_16
427#define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
428#endif
429
430#ifndef bswap_32
431#define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
432 (((u32) (a) << 8) & 0xff0000) | \
433 (((u32) (a) >> 8) & 0xff00) | \
434 (((u32) (a) >> 24) & 0xff))
435#endif
436
437#ifndef MSG_DONTWAIT
438#define MSG_DONTWAIT 0
439#endif
440
441#ifdef _WIN32_WCE
442void perror(const char *s);
443#endif /* _WIN32_WCE */
444
445#endif /* CONFIG_ANSI_C_EXTRA */
446
447#ifndef MAC2STR
448#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
449#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
450
451/*
452 * Compact form for string representation of MAC address
453 * To be used, e.g., for constructing dbus paths for P2P Devices
454 */
455#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
456#endif
457
458#ifndef BIT
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800459#define BIT(x) (1U << (x))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460#endif
461
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000462#ifndef MIN
463#define MIN(a, b) ((a) < (b) ? (a) : (b))
464#endif
465#ifndef MAX
466#define MAX(a, b) (((a) > (b)) ? (a) : (b))
467#endif
468
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469/*
470 * Definitions for sparse validation
471 * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
472 */
473#ifdef __CHECKER__
474#define __force __attribute__((force))
Dmitry Shmidt4ae50e62016-06-27 13:48:39 -0700475#undef __bitwise
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476#define __bitwise __attribute__((bitwise))
477#else
478#define __force
Roshan Pius3a1667e2018-07-03 15:17:14 -0700479#undef __bitwise
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480#define __bitwise
481#endif
482
483typedef u16 __bitwise be16;
484typedef u16 __bitwise le16;
485typedef u32 __bitwise be32;
486typedef u32 __bitwise le32;
487typedef u64 __bitwise be64;
488typedef u64 __bitwise le64;
489
490#ifndef __must_check
491#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
492#define __must_check __attribute__((__warn_unused_result__))
493#else
494#define __must_check
495#endif /* __GNUC__ */
496#endif /* __must_check */
497
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800498#ifndef __maybe_unused
499#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
500#define __maybe_unused __attribute__((unused))
501#else
502#define __maybe_unused
503#endif /* __GNUC__ */
504#endif /* __must_check */
505
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700506#define SSID_MAX_LEN 32
507
508struct wpa_ssid_value {
509 u8 ssid[SSID_MAX_LEN];
510 size_t ssid_len;
511};
512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513int hwaddr_aton(const char *txt, u8 *addr);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800514int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515int hwaddr_compact_aton(const char *txt, u8 *addr);
516int hwaddr_aton2(const char *txt, u8 *addr);
Sunil Ravia04bd252022-05-02 22:54:18 -0700517int hex2num(char c);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518int hex2byte(const char *hex);
519int hexstr2bin(const char *hex, u8 *buf, size_t len);
520void inc_byte_array(u8 *counter, size_t len);
Hai Shalom81f62d82019-07-22 12:10:00 -0700521void buf_shift_right(u8 *buf, size_t len, size_t bits);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522void wpa_get_ntp_timestamp(u8 *buf);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700523int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
524 PRINTF_FORMAT(3, 4);
Dmitry Shmidtaf9da312015-04-03 10:03:11 -0700525int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
526 char sep);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
528int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
529 size_t len);
530
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800531int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700532int ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534#ifdef CONFIG_NATIVE_WINDOWS
535void wpa_unicode2ascii_inplace(TCHAR *str);
536TCHAR * wpa_strdup_tchar(const char *str);
537#else /* CONFIG_NATIVE_WINDOWS */
538#define wpa_unicode2ascii_inplace(s) do { } while (0)
539#define wpa_strdup_tchar(s) strdup((s))
540#endif /* CONFIG_NATIVE_WINDOWS */
541
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700542void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
543size_t printf_decode(u8 *buf, size_t maxlen, const char *str);
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
546
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700547char * wpa_config_parse_string(const char *value, size_t *len);
548int is_hex(const u8 *data, size_t len);
Dmitry Shmidt849734c2016-05-27 09:59:01 -0700549int has_ctrl_char(const u8 *data, size_t len);
550int has_newline(const char *str);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700551size_t merge_byte_arrays(u8 *res, size_t res_len,
552 const u8 *src1, size_t src1_len,
553 const u8 *src2, size_t src2_len);
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700554char * dup_binstr(const void *src, size_t len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556static inline int is_zero_ether_addr(const u8 *a)
557{
558 return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
559}
560
561static inline int is_broadcast_ether_addr(const u8 *a)
562{
563 return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
564}
565
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800566static inline int is_multicast_ether_addr(const u8 *a)
567{
568 return a[0] & 0x01;
569}
570
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000571static inline bool ether_addr_equal(const u8 *a, const u8 *b)
572{
573 return os_memcmp(a, b, ETH_ALEN) == 0;
574}
575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576#define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
577
578#include "wpa_debug.h"
579
580
Dmitry Shmidt4ce9c872013-10-24 11:08:13 -0700581struct wpa_freq_range_list {
582 struct wpa_freq_range {
583 unsigned int min;
584 unsigned int max;
585 } *range;
586 unsigned int num;
587};
588
589int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
590int freq_range_list_includes(const struct wpa_freq_range_list *list,
591 unsigned int freq);
592char * freq_range_list_str(const struct wpa_freq_range_list *list);
593
Hai Shalomfdcde762020-04-02 11:19:20 -0700594size_t int_array_len(const int *a);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800595void int_array_concat(int **res, const int *a);
596void int_array_sort_unique(int *a);
597void int_array_add_unique(int **res, int a);
Sunil Ravi7f769292024-07-23 22:21:32 +0000598bool int_array_includes(int *arr, int val);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800599
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -0700600#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
601
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700602void str_clear_free(char *str);
603void bin_clear_free(void *bin, size_t len);
604
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700605int random_mac_addr(u8 *addr);
606int random_mac_addr_keep_oui(u8 *addr);
607
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800608const char * cstr_token(const char *str, const char *delim, const char **last);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800609char * str_token(char *str, const char *delim, char **context);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800610size_t utf8_escape(const char *inp, size_t in_size,
611 char *outp, size_t out_size);
612size_t utf8_unescape(const char *inp, size_t in_size,
613 char *outp, size_t out_size);
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700614int is_ctrl_char(char c);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800615
Dmitry Shmidt7f2c7532016-08-15 09:48:12 -0700616int str_starts(const char *str, const char *start);
617
Dmitry Shmidt29333592017-01-09 12:27:11 -0800618u8 rssi_to_rcpi(int rssi);
Hai Shalom021b0b52019-04-10 11:17:58 -0700619char * get_param(const char *cmd, const char *param);
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700620
Sunil Ravi99c035e2024-07-12 01:42:03 +0000621#define for_each_link(__links, __i) \
622 for ((__i) = 0; (__i) < MAX_NUM_MLD_LINKS; (__i)++) \
623 if ((__links) & BIT(__i))
624
625/* Iterate all links, or, if no link is defined, iterate given index */
626#define for_each_link_default(_links, _i, _def_idx) \
627 for ((_i) = (_links) ? 0 : (_def_idx); \
628 (_i) < MAX_NUM_MLD_LINKS || \
629 (!(_links) && (_i) == (_def_idx)); \
630 (_i)++) \
631 if (!(_links) || (_links) & BIT(_i))
632
Hai Shalom81f62d82019-07-22 12:10:00 -0700633void forced_memzero(void *ptr, size_t len);
634
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635/*
636 * gcc 4.4 ends up generating strict-aliasing warnings about some very common
637 * networking socket uses that do not really result in a real problem and
638 * cannot be easily avoided with union-based type-punning due to struct
639 * definitions including another struct in system header files. To avoid having
640 * to fully disable strict-aliasing warnings, provide a mechanism to hide the
641 * typecast from aliasing for now. A cleaner solution will hopefully be found
642 * in the future to handle these cases.
643 */
644void * __hide_aliasing_typecast(void *foo);
645#define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
646
647#ifdef CONFIG_VALGRIND
648#include <valgrind/memcheck.h>
649#define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
650#else /* CONFIG_VALGRIND */
651#define WPA_MEM_DEFINED(ptr, len) do { } while (0)
652#endif /* CONFIG_VALGRIND */
653
654#endif /* COMMON_H */