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