1189251Ssam/*
2189251Ssam * wpa_supplicant/hostapd / common helper functions, etc.
3189251Ssam * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
4189251Ssam *
5252726Srpaulo * This software may be distributed under the terms of the BSD license.
6252726Srpaulo * See README for more details.
7189251Ssam */
8189251Ssam
9189251Ssam#ifndef COMMON_H
10189251Ssam#define COMMON_H
11189251Ssam
12189251Ssam#include "os.h"
13189251Ssam
14214734Srpaulo#if defined(__linux__) || defined(__GLIBC__)
15189251Ssam#include <endian.h>
16189251Ssam#include <byteswap.h>
17189251Ssam#endif /* __linux__ */
18189251Ssam
19209158Srpaulo#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__) || \
20209158Srpaulo    defined(__OpenBSD__)
21189251Ssam#include <sys/types.h>
22189251Ssam#include <sys/endian.h>
23189251Ssam#define __BYTE_ORDER	_BYTE_ORDER
24189251Ssam#define	__LITTLE_ENDIAN	_LITTLE_ENDIAN
25189251Ssam#define	__BIG_ENDIAN	_BIG_ENDIAN
26209158Srpaulo#ifdef __OpenBSD__
27209158Srpaulo#define bswap_16 swap16
28209158Srpaulo#define bswap_32 swap32
29209158Srpaulo#define bswap_64 swap64
30209158Srpaulo#else /* __OpenBSD__ */
31189251Ssam#define bswap_16 bswap16
32189251Ssam#define bswap_32 bswap32
33189251Ssam#define bswap_64 bswap64
34209158Srpaulo#endif /* __OpenBSD__ */
35189251Ssam#endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
36209158Srpaulo	* defined(__DragonFly__) || defined(__OpenBSD__) */
37189251Ssam
38189251Ssam#ifdef __APPLE__
39189251Ssam#include <sys/types.h>
40189251Ssam#include <machine/endian.h>
41189251Ssam#define __BYTE_ORDER	_BYTE_ORDER
42189251Ssam#define __LITTLE_ENDIAN	_LITTLE_ENDIAN
43189251Ssam#define __BIG_ENDIAN	_BIG_ENDIAN
44189251Ssamstatic inline unsigned short bswap_16(unsigned short v)
45189251Ssam{
46189251Ssam	return ((v & 0xff) << 8) | (v >> 8);
47189251Ssam}
48189251Ssam
49189251Ssamstatic inline unsigned int bswap_32(unsigned int v)
50189251Ssam{
51189251Ssam	return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
52189251Ssam		((v & 0xff0000) >> 8) | (v >> 24);
53189251Ssam}
54189251Ssam#endif /* __APPLE__ */
55189251Ssam
56346981Scy#ifdef __rtems__
57346981Scy#include <rtems/endian.h>
58346981Scy#define __BYTE_ORDER BYTE_ORDER
59346981Scy#define __LITTLE_ENDIAN LITTLE_ENDIAN
60346981Scy#define __BIG_ENDIAN BIG_ENDIAN
61346981Scy#define bswap_16 CPU_swap_u16
62346981Scy#define bswap_32 CPU_swap_u32
63346981Scy#endif /* __rtems__ */
64346981Scy
65189251Ssam#ifdef CONFIG_NATIVE_WINDOWS
66189251Ssam#include <winsock.h>
67189251Ssam
68189251Ssamtypedef int socklen_t;
69189251Ssam
70189251Ssam#ifndef MSG_DONTWAIT
71189251Ssam#define MSG_DONTWAIT 0 /* not supported */
72189251Ssam#endif
73189251Ssam
74189251Ssam#endif /* CONFIG_NATIVE_WINDOWS */
75189251Ssam
76189251Ssam#ifdef _MSC_VER
77189251Ssam#define inline __inline
78189251Ssam
79189251Ssam#undef vsnprintf
80189251Ssam#define vsnprintf _vsnprintf
81189251Ssam#undef close
82189251Ssam#define close closesocket
83189251Ssam#endif /* _MSC_VER */
84189251Ssam
85189251Ssam
86189251Ssam/* Define platform specific integer types */
87189251Ssam
88189251Ssam#ifdef _MSC_VER
89189251Ssamtypedef UINT64 u64;
90189251Ssamtypedef UINT32 u32;
91189251Ssamtypedef UINT16 u16;
92189251Ssamtypedef UINT8 u8;
93189251Ssamtypedef INT64 s64;
94189251Ssamtypedef INT32 s32;
95189251Ssamtypedef INT16 s16;
96189251Ssamtypedef INT8 s8;
97189251Ssam#define WPA_TYPES_DEFINED
98189251Ssam#endif /* _MSC_VER */
99189251Ssam
100189251Ssam#ifdef __vxworks
101189251Ssamtypedef unsigned long long u64;
102189251Ssamtypedef UINT32 u32;
103189251Ssamtypedef UINT16 u16;
104189251Ssamtypedef UINT8 u8;
105189251Ssamtypedef long long s64;
106189251Ssamtypedef INT32 s32;
107189251Ssamtypedef INT16 s16;
108189251Ssamtypedef INT8 s8;
109189251Ssam#define WPA_TYPES_DEFINED
110189251Ssam#endif /* __vxworks */
111189251Ssam
112189251Ssam#ifndef WPA_TYPES_DEFINED
113189251Ssam#ifdef CONFIG_USE_INTTYPES_H
114189251Ssam#include <inttypes.h>
115189251Ssam#else
116189251Ssam#include <stdint.h>
117189251Ssam#endif
118189251Ssamtypedef uint64_t u64;
119189251Ssamtypedef uint32_t u32;
120189251Ssamtypedef uint16_t u16;
121189251Ssamtypedef uint8_t u8;
122189251Ssamtypedef int64_t s64;
123189251Ssamtypedef int32_t s32;
124189251Ssamtypedef int16_t s16;
125189251Ssamtypedef int8_t s8;
126189251Ssam#define WPA_TYPES_DEFINED
127189251Ssam#endif /* !WPA_TYPES_DEFINED */
128189251Ssam
129189251Ssam
130189251Ssam/* Define platform specific byte swapping macros */
131189251Ssam
132189251Ssam#if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
133189251Ssam
134189251Ssamstatic inline unsigned short wpa_swap_16(unsigned short v)
135189251Ssam{
136189251Ssam	return ((v & 0xff) << 8) | (v >> 8);
137189251Ssam}
138189251Ssam
139189251Ssamstatic inline unsigned int wpa_swap_32(unsigned int v)
140189251Ssam{
141189251Ssam	return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
142189251Ssam		((v & 0xff0000) >> 8) | (v >> 24);
143189251Ssam}
144189251Ssam
145189251Ssam#define le_to_host16(n) (n)
146189251Ssam#define host_to_le16(n) (n)
147189251Ssam#define be_to_host16(n) wpa_swap_16(n)
148189251Ssam#define host_to_be16(n) wpa_swap_16(n)
149189251Ssam#define le_to_host32(n) (n)
150281806Srpaulo#define host_to_le32(n) (n)
151189251Ssam#define be_to_host32(n) wpa_swap_32(n)
152189251Ssam#define host_to_be32(n) wpa_swap_32(n)
153346981Scy#define host_to_le64(n) (n)
154189251Ssam
155189251Ssam#define WPA_BYTE_SWAP_DEFINED
156189251Ssam
157189251Ssam#endif /* __CYGWIN__ || CONFIG_NATIVE_WINDOWS */
158189251Ssam
159189251Ssam
160189251Ssam#ifndef WPA_BYTE_SWAP_DEFINED
161189251Ssam
162189251Ssam#ifndef __BYTE_ORDER
163189251Ssam#ifndef __LITTLE_ENDIAN
164189251Ssam#ifndef __BIG_ENDIAN
165189251Ssam#define __LITTLE_ENDIAN 1234
166189251Ssam#define __BIG_ENDIAN 4321
167189251Ssam#if defined(sparc)
168189251Ssam#define __BYTE_ORDER __BIG_ENDIAN
169189251Ssam#endif
170189251Ssam#endif /* __BIG_ENDIAN */
171189251Ssam#endif /* __LITTLE_ENDIAN */
172189251Ssam#endif /* __BYTE_ORDER */
173189251Ssam
174189251Ssam#if __BYTE_ORDER == __LITTLE_ENDIAN
175189251Ssam#define le_to_host16(n) ((__force u16) (le16) (n))
176189251Ssam#define host_to_le16(n) ((__force le16) (u16) (n))
177189251Ssam#define be_to_host16(n) bswap_16((__force u16) (be16) (n))
178189251Ssam#define host_to_be16(n) ((__force be16) bswap_16((n)))
179189251Ssam#define le_to_host32(n) ((__force u32) (le32) (n))
180189251Ssam#define host_to_le32(n) ((__force le32) (u32) (n))
181189251Ssam#define be_to_host32(n) bswap_32((__force u32) (be32) (n))
182189251Ssam#define host_to_be32(n) ((__force be32) bswap_32((n)))
183189251Ssam#define le_to_host64(n) ((__force u64) (le64) (n))
184189251Ssam#define host_to_le64(n) ((__force le64) (u64) (n))
185189251Ssam#define be_to_host64(n) bswap_64((__force u64) (be64) (n))
186189251Ssam#define host_to_be64(n) ((__force be64) bswap_64((n)))
187189251Ssam#elif __BYTE_ORDER == __BIG_ENDIAN
188189251Ssam#define le_to_host16(n) bswap_16(n)
189189251Ssam#define host_to_le16(n) bswap_16(n)
190189251Ssam#define be_to_host16(n) (n)
191189251Ssam#define host_to_be16(n) (n)
192189251Ssam#define le_to_host32(n) bswap_32(n)
193281806Srpaulo#define host_to_le32(n) bswap_32(n)
194189251Ssam#define be_to_host32(n) (n)
195189251Ssam#define host_to_be32(n) (n)
196189251Ssam#define le_to_host64(n) bswap_64(n)
197189251Ssam#define host_to_le64(n) bswap_64(n)
198189251Ssam#define be_to_host64(n) (n)
199189251Ssam#define host_to_be64(n) (n)
200189251Ssam#ifndef WORDS_BIGENDIAN
201189251Ssam#define WORDS_BIGENDIAN
202189251Ssam#endif
203189251Ssam#else
204189251Ssam#error Could not determine CPU byte order
205189251Ssam#endif
206189251Ssam
207189251Ssam#define WPA_BYTE_SWAP_DEFINED
208189251Ssam#endif /* !WPA_BYTE_SWAP_DEFINED */
209189251Ssam
210189251Ssam
211189251Ssam/* Macros for handling unaligned memory accesses */
212189251Ssam
213281806Srpaulostatic inline u16 WPA_GET_BE16(const u8 *a)
214281806Srpaulo{
215281806Srpaulo	return (a[0] << 8) | a[1];
216281806Srpaulo}
217189251Ssam
218281806Srpaulostatic inline void WPA_PUT_BE16(u8 *a, u16 val)
219281806Srpaulo{
220281806Srpaulo	a[0] = val >> 8;
221281806Srpaulo	a[1] = val & 0xff;
222281806Srpaulo}
223189251Ssam
224281806Srpaulostatic inline u16 WPA_GET_LE16(const u8 *a)
225281806Srpaulo{
226281806Srpaulo	return (a[1] << 8) | a[0];
227281806Srpaulo}
228189251Ssam
229281806Srpaulostatic inline void WPA_PUT_LE16(u8 *a, u16 val)
230281806Srpaulo{
231281806Srpaulo	a[1] = val >> 8;
232281806Srpaulo	a[0] = val & 0xff;
233281806Srpaulo}
234189251Ssam
235281806Srpaulostatic inline u32 WPA_GET_BE24(const u8 *a)
236281806Srpaulo{
237281806Srpaulo	return (a[0] << 16) | (a[1] << 8) | a[2];
238281806Srpaulo}
239189251Ssam
240281806Srpaulostatic inline void WPA_PUT_BE24(u8 *a, u32 val)
241281806Srpaulo{
242281806Srpaulo	a[0] = (val >> 16) & 0xff;
243281806Srpaulo	a[1] = (val >> 8) & 0xff;
244281806Srpaulo	a[2] = val & 0xff;
245281806Srpaulo}
246189251Ssam
247281806Srpaulostatic inline u32 WPA_GET_BE32(const u8 *a)
248281806Srpaulo{
249289549Srpaulo	return ((u32) a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
250281806Srpaulo}
251189251Ssam
252281806Srpaulostatic inline void WPA_PUT_BE32(u8 *a, u32 val)
253281806Srpaulo{
254281806Srpaulo	a[0] = (val >> 24) & 0xff;
255281806Srpaulo	a[1] = (val >> 16) & 0xff;
256281806Srpaulo	a[2] = (val >> 8) & 0xff;
257281806Srpaulo	a[3] = val & 0xff;
258281806Srpaulo}
259189251Ssam
260281806Srpaulostatic inline u32 WPA_GET_LE32(const u8 *a)
261281806Srpaulo{
262289549Srpaulo	return ((u32) a[3] << 24) | (a[2] << 16) | (a[1] << 8) | a[0];
263281806Srpaulo}
264281806Srpaulo
265281806Srpaulostatic inline void WPA_PUT_LE32(u8 *a, u32 val)
266281806Srpaulo{
267281806Srpaulo	a[3] = (val >> 24) & 0xff;
268281806Srpaulo	a[2] = (val >> 16) & 0xff;
269281806Srpaulo	a[1] = (val >> 8) & 0xff;
270281806Srpaulo	a[0] = val & 0xff;
271281806Srpaulo}
272281806Srpaulo
273281806Srpaulostatic inline u64 WPA_GET_BE64(const u8 *a)
274281806Srpaulo{
275281806Srpaulo	return (((u64) a[0]) << 56) | (((u64) a[1]) << 48) |
276281806Srpaulo		(((u64) a[2]) << 40) | (((u64) a[3]) << 32) |
277281806Srpaulo		(((u64) a[4]) << 24) | (((u64) a[5]) << 16) |
278281806Srpaulo		(((u64) a[6]) << 8) | ((u64) a[7]);
279281806Srpaulo}
280281806Srpaulo
281281806Srpaulostatic inline void WPA_PUT_BE64(u8 *a, u64 val)
282281806Srpaulo{
283281806Srpaulo	a[0] = val >> 56;
284281806Srpaulo	a[1] = val >> 48;
285281806Srpaulo	a[2] = val >> 40;
286281806Srpaulo	a[3] = val >> 32;
287281806Srpaulo	a[4] = val >> 24;
288281806Srpaulo	a[5] = val >> 16;
289281806Srpaulo	a[6] = val >> 8;
290281806Srpaulo	a[7] = val & 0xff;
291281806Srpaulo}
292281806Srpaulo
293281806Srpaulostatic inline u64 WPA_GET_LE64(const u8 *a)
294281806Srpaulo{
295281806Srpaulo	return (((u64) a[7]) << 56) | (((u64) a[6]) << 48) |
296281806Srpaulo		(((u64) a[5]) << 40) | (((u64) a[4]) << 32) |
297281806Srpaulo		(((u64) a[3]) << 24) | (((u64) a[2]) << 16) |
298281806Srpaulo		(((u64) a[1]) << 8) | ((u64) a[0]);
299281806Srpaulo}
300281806Srpaulo
301281806Srpaulostatic inline void WPA_PUT_LE64(u8 *a, u64 val)
302281806Srpaulo{
303281806Srpaulo	a[7] = val >> 56;
304281806Srpaulo	a[6] = val >> 48;
305281806Srpaulo	a[5] = val >> 40;
306281806Srpaulo	a[4] = val >> 32;
307281806Srpaulo	a[3] = val >> 24;
308281806Srpaulo	a[2] = val >> 16;
309281806Srpaulo	a[1] = val >> 8;
310281806Srpaulo	a[0] = val & 0xff;
311281806Srpaulo}
312281806Srpaulo
313281806Srpaulo
314189251Ssam#ifndef ETH_ALEN
315189251Ssam#define ETH_ALEN 6
316189251Ssam#endif
317281806Srpaulo#ifndef ETH_HLEN
318281806Srpaulo#define ETH_HLEN 14
319281806Srpaulo#endif
320214734Srpaulo#ifndef IFNAMSIZ
321214734Srpaulo#define IFNAMSIZ 16
322214734Srpaulo#endif
323214734Srpaulo#ifndef ETH_P_ALL
324214734Srpaulo#define ETH_P_ALL 0x0003
325214734Srpaulo#endif
326337817Scy#ifndef ETH_P_IP
327337817Scy#define ETH_P_IP 0x0800
328337817Scy#endif
329252726Srpaulo#ifndef ETH_P_80211_ENCAP
330252726Srpaulo#define ETH_P_80211_ENCAP 0x890d /* TDLS comes under this category */
331252726Srpaulo#endif
332214734Srpaulo#ifndef ETH_P_PAE
333214734Srpaulo#define ETH_P_PAE 0x888E /* Port Access Entity (IEEE 802.1X) */
334214734Srpaulo#endif /* ETH_P_PAE */
335214734Srpaulo#ifndef ETH_P_EAPOL
336214734Srpaulo#define ETH_P_EAPOL ETH_P_PAE
337214734Srpaulo#endif /* ETH_P_EAPOL */
338214734Srpaulo#ifndef ETH_P_RSN_PREAUTH
339214734Srpaulo#define ETH_P_RSN_PREAUTH 0x88c7
340214734Srpaulo#endif /* ETH_P_RSN_PREAUTH */
341214734Srpaulo#ifndef ETH_P_RRB
342214734Srpaulo#define ETH_P_RRB 0x890D
343214734Srpaulo#endif /* ETH_P_RRB */
344346981Scy#ifndef ETH_P_OUI
345346981Scy#define ETH_P_OUI 0x88B7
346346981Scy#endif /* ETH_P_OUI */
347189251Ssam
348189251Ssam
349189251Ssam#ifdef __GNUC__
350189251Ssam#define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
351189251Ssam#define STRUCT_PACKED __attribute__ ((packed))
352189251Ssam#else
353189251Ssam#define PRINTF_FORMAT(a,b)
354189251Ssam#define STRUCT_PACKED
355189251Ssam#endif
356189251Ssam
357189251Ssam
358189251Ssam#ifdef CONFIG_ANSI_C_EXTRA
359189251Ssam
360189251Ssam#if !defined(_MSC_VER) || _MSC_VER < 1400
361189251Ssam/* snprintf - used in number of places; sprintf() is _not_ a good replacement
362189251Ssam * due to possible buffer overflow; see, e.g.,
363189251Ssam * http://www.ijs.si/software/snprintf/ for portable implementation of
364189251Ssam * snprintf. */
365189251Ssamint snprintf(char *str, size_t size, const char *format, ...);
366189251Ssam
367189251Ssam/* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
368189251Ssamint vsnprintf(char *str, size_t size, const char *format, va_list ap);
369189251Ssam#endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
370189251Ssam
371189251Ssam/* getopt - only used in main.c */
372189251Ssamint getopt(int argc, char *const argv[], const char *optstring);
373189251Ssamextern char *optarg;
374189251Ssamextern int optind;
375189251Ssam
376189251Ssam#ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
377189251Ssam#ifndef __socklen_t_defined
378189251Ssamtypedef int socklen_t;
379189251Ssam#endif
380189251Ssam#endif
381189251Ssam
382189251Ssam/* inline - define as __inline or just define it to be empty, if needed */
383189251Ssam#ifdef CONFIG_NO_INLINE
384189251Ssam#define inline
385189251Ssam#else
386189251Ssam#define inline __inline
387189251Ssam#endif
388189251Ssam
389189251Ssam#ifndef __func__
390189251Ssam#define __func__ "__func__ not defined"
391189251Ssam#endif
392189251Ssam
393189251Ssam#ifndef bswap_16
394189251Ssam#define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
395189251Ssam#endif
396189251Ssam
397189251Ssam#ifndef bswap_32
398189251Ssam#define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
399189251Ssam		     (((u32) (a) << 8) & 0xff0000) | \
400189251Ssam     		     (((u32) (a) >> 8) & 0xff00) | \
401189251Ssam     		     (((u32) (a) >> 24) & 0xff))
402189251Ssam#endif
403189251Ssam
404189251Ssam#ifndef MSG_DONTWAIT
405189251Ssam#define MSG_DONTWAIT 0
406189251Ssam#endif
407189251Ssam
408189251Ssam#ifdef _WIN32_WCE
409189251Ssamvoid perror(const char *s);
410189251Ssam#endif /* _WIN32_WCE */
411189251Ssam
412189251Ssam#endif /* CONFIG_ANSI_C_EXTRA */
413189251Ssam
414189251Ssam#ifndef MAC2STR
415189251Ssam#define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
416189251Ssam#define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
417252726Srpaulo
418252726Srpaulo/*
419252726Srpaulo * Compact form for string representation of MAC address
420252726Srpaulo * To be used, e.g., for constructing dbus paths for P2P Devices
421252726Srpaulo */
422252726Srpaulo#define COMPACT_MACSTR "%02x%02x%02x%02x%02x%02x"
423189251Ssam#endif
424189251Ssam
425189251Ssam#ifndef BIT
426289549Srpaulo#define BIT(x) (1U << (x))
427189251Ssam#endif
428189251Ssam
429189251Ssam/*
430189251Ssam * Definitions for sparse validation
431189251Ssam * (http://kernel.org/pub/linux/kernel/people/josh/sparse/)
432189251Ssam */
433189251Ssam#ifdef __CHECKER__
434189251Ssam#define __force __attribute__((force))
435337817Scy#undef __bitwise
436189251Ssam#define __bitwise __attribute__((bitwise))
437189251Ssam#else
438189251Ssam#define __force
439346981Scy#undef __bitwise
440189251Ssam#define __bitwise
441189251Ssam#endif
442189251Ssam
443189251Ssamtypedef u16 __bitwise be16;
444189251Ssamtypedef u16 __bitwise le16;
445189251Ssamtypedef u32 __bitwise be32;
446189251Ssamtypedef u32 __bitwise le32;
447189251Ssamtypedef u64 __bitwise be64;
448189251Ssamtypedef u64 __bitwise le64;
449189251Ssam
450189251Ssam#ifndef __must_check
451189251Ssam#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
452189251Ssam#define __must_check __attribute__((__warn_unused_result__))
453189251Ssam#else
454189251Ssam#define __must_check
455189251Ssam#endif /* __GNUC__ */
456189251Ssam#endif /* __must_check */
457189251Ssam
458281806Srpaulo#ifndef __maybe_unused
459281806Srpaulo#if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
460281806Srpaulo#define __maybe_unused __attribute__((unused))
461281806Srpaulo#else
462281806Srpaulo#define __maybe_unused
463281806Srpaulo#endif /* __GNUC__ */
464281806Srpaulo#endif /* __must_check */
465281806Srpaulo
466337817Scy#define SSID_MAX_LEN 32
467337817Scy
468337817Scystruct wpa_ssid_value {
469337817Scy	u8 ssid[SSID_MAX_LEN];
470337817Scy	size_t ssid_len;
471337817Scy};
472337817Scy
473189251Ssamint hwaddr_aton(const char *txt, u8 *addr);
474281806Srpauloint hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
475252726Srpauloint hwaddr_compact_aton(const char *txt, u8 *addr);
476214734Srpauloint hwaddr_aton2(const char *txt, u8 *addr);
477252726Srpauloint hex2byte(const char *hex);
478189251Ssamint hexstr2bin(const char *hex, u8 *buf, size_t len);
479189251Ssamvoid inc_byte_array(u8 *counter, size_t len);
480351611Scyvoid buf_shift_right(u8 *buf, size_t len, size_t bits);
481189251Ssamvoid wpa_get_ntp_timestamp(u8 *buf);
482281806Srpauloint wpa_scnprintf(char *buf, size_t size, const char *fmt, ...);
483289549Srpauloint wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
484289549Srpaulo			 char sep);
485189251Ssamint wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
486189251Ssamint wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
487189251Ssam			       size_t len);
488189251Ssam
489281806Srpauloint hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
490337817Scyint ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
491281806Srpaulo
492189251Ssam#ifdef CONFIG_NATIVE_WINDOWS
493189251Ssamvoid wpa_unicode2ascii_inplace(TCHAR *str);
494189251SsamTCHAR * wpa_strdup_tchar(const char *str);
495189251Ssam#else /* CONFIG_NATIVE_WINDOWS */
496189251Ssam#define wpa_unicode2ascii_inplace(s) do { } while (0)
497189251Ssam#define wpa_strdup_tchar(s) strdup((s))
498189251Ssam#endif /* CONFIG_NATIVE_WINDOWS */
499189251Ssam
500252726Srpaulovoid printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len);
501252726Srpaulosize_t printf_decode(u8 *buf, size_t maxlen, const char *str);
502252726Srpaulo
503189251Ssamconst char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len);
504189251Ssam
505252726Srpaulochar * wpa_config_parse_string(const char *value, size_t *len);
506252726Srpauloint is_hex(const u8 *data, size_t len);
507337817Scyint has_ctrl_char(const u8 *data, size_t len);
508337817Scyint has_newline(const char *str);
509252726Srpaulosize_t merge_byte_arrays(u8 *res, size_t res_len,
510252726Srpaulo			 const u8 *src1, size_t src1_len,
511252726Srpaulo			 const u8 *src2, size_t src2_len);
512281806Srpaulochar * dup_binstr(const void *src, size_t len);
513252726Srpaulo
514189251Ssamstatic inline int is_zero_ether_addr(const u8 *a)
515189251Ssam{
516189251Ssam	return !(a[0] | a[1] | a[2] | a[3] | a[4] | a[5]);
517189251Ssam}
518189251Ssam
519252726Srpaulostatic inline int is_broadcast_ether_addr(const u8 *a)
520252726Srpaulo{
521252726Srpaulo	return (a[0] & a[1] & a[2] & a[3] & a[4] & a[5]) == 0xff;
522252726Srpaulo}
523252726Srpaulo
524289549Srpaulostatic inline int is_multicast_ether_addr(const u8 *a)
525289549Srpaulo{
526289549Srpaulo	return a[0] & 0x01;
527289549Srpaulo}
528289549Srpaulo
529252726Srpaulo#define broadcast_ether_addr (const u8 *) "\xff\xff\xff\xff\xff\xff"
530252726Srpaulo
531189251Ssam#include "wpa_debug.h"
532189251Ssam
533209158Srpaulo
534281806Srpaulostruct wpa_freq_range_list {
535281806Srpaulo	struct wpa_freq_range {
536281806Srpaulo		unsigned int min;
537281806Srpaulo		unsigned int max;
538281806Srpaulo	} *range;
539281806Srpaulo	unsigned int num;
540281806Srpaulo};
541281806Srpaulo
542281806Srpauloint freq_range_list_parse(struct wpa_freq_range_list *res, const char *value);
543281806Srpauloint freq_range_list_includes(const struct wpa_freq_range_list *list,
544281806Srpaulo			     unsigned int freq);
545281806Srpaulochar * freq_range_list_str(const struct wpa_freq_range_list *list);
546281806Srpaulo
547281806Srpauloint int_array_len(const int *a);
548281806Srpaulovoid int_array_concat(int **res, const int *a);
549281806Srpaulovoid int_array_sort_unique(int *a);
550281806Srpaulovoid int_array_add_unique(int **res, int a);
551281806Srpaulo
552281806Srpaulo#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
553281806Srpaulo
554281806Srpaulovoid str_clear_free(char *str);
555281806Srpaulovoid bin_clear_free(void *bin, size_t len);
556281806Srpaulo
557281806Srpauloint random_mac_addr(u8 *addr);
558281806Srpauloint random_mac_addr_keep_oui(u8 *addr);
559281806Srpaulo
560289549Srpauloconst char * cstr_token(const char *str, const char *delim, const char **last);
561281806Srpaulochar * str_token(char *str, const char *delim, char **context);
562281806Srpaulosize_t utf8_escape(const char *inp, size_t in_size,
563281806Srpaulo		   char *outp, size_t out_size);
564281806Srpaulosize_t utf8_unescape(const char *inp, size_t in_size,
565281806Srpaulo		     char *outp, size_t out_size);
566289549Srpauloint is_ctrl_char(char c);
567281806Srpaulo
568337817Scyint str_starts(const char *str, const char *start);
569281806Srpaulo
570346981Scyu8 rssi_to_rcpi(int rssi);
571346981Scychar * get_param(const char *cmd, const char *param);
572337817Scy
573351611Scyvoid forced_memzero(void *ptr, size_t len);
574351611Scy
575209158Srpaulo/*
576209158Srpaulo * gcc 4.4 ends up generating strict-aliasing warnings about some very common
577209158Srpaulo * networking socket uses that do not really result in a real problem and
578209158Srpaulo * cannot be easily avoided with union-based type-punning due to struct
579209158Srpaulo * definitions including another struct in system header files. To avoid having
580209158Srpaulo * to fully disable strict-aliasing warnings, provide a mechanism to hide the
581209158Srpaulo * typecast from aliasing for now. A cleaner solution will hopefully be found
582209158Srpaulo * in the future to handle these cases.
583209158Srpaulo */
584209158Srpaulovoid * __hide_aliasing_typecast(void *foo);
585209158Srpaulo#define aliasing_hide_typecast(a,t) (t *) __hide_aliasing_typecast((a))
586209158Srpaulo
587252726Srpaulo#ifdef CONFIG_VALGRIND
588252726Srpaulo#include <valgrind/memcheck.h>
589252726Srpaulo#define WPA_MEM_DEFINED(ptr, len) VALGRIND_MAKE_MEM_DEFINED((ptr), (len))
590252726Srpaulo#else /* CONFIG_VALGRIND */
591252726Srpaulo#define WPA_MEM_DEFINED(ptr, len) do { } while (0)
592252726Srpaulo#endif /* CONFIG_VALGRIND */
593252726Srpaulo
594189251Ssam#endif /* COMMON_H */
595