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