1 /*
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2006, Jouni Malinen <j@w1.fi>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #ifndef COMMON_H
16 #define COMMON_H
17
18 #include "os.h"
19
20 #ifdef __linux__
21 #include <endian.h>
22 #include <byteswap.h>
23 #endif /* __linux__ */
24
25 #if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
26 #include <sys/types.h>
27 #include <sys/endian.h>
28 #define __BYTE_ORDER _BYTE_ORDER
29 #define __LITTLE_ENDIAN _LITTLE_ENDIAN
30 #define __BIG_ENDIAN _BIG_ENDIAN
31 #define bswap_16 bswap16
32 #define bswap_32 bswap32
33 #define bswap_64 bswap64
34 #endif /* defined(__FreeBSD__) || defined(__NetBSD__) ||
35 * defined(__DragonFly__) */
36
37 #ifdef CONFIG_TI_COMPILER
38 #define __BIG_ENDIAN 4321
39 #define __LITTLE_ENDIAN 1234
40 #ifdef __big_endian__
41 #define __BYTE_ORDER __BIG_ENDIAN
42 #else
43 #define __BYTE_ORDER __LITTLE_ENDIAN
44 #endif
45 #endif /* CONFIG_TI_COMPILER */
46
47 #ifdef CONFIG_NATIVE_WINDOWS
48 #include <winsock.h>
49
50 typedef int socklen_t;
51
52 #ifndef MSG_DONTWAIT
53 #define MSG_DONTWAIT 0 /* not supported */
54 #endif
55
56 #endif /* CONFIG_NATIVE_WINDOWS */
57
58 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
59
60 #ifdef _MSC_VER
61 #define inline __inline
62 #endif /* _MSC_VER */
63
wpa_swap_16(unsigned short v)64 static inline unsigned short wpa_swap_16(unsigned short v)
65 {
66 return ((v & 0xff) << 8) | (v >> 8);
67 }
68
wpa_swap_32(unsigned int v)69 static inline unsigned int wpa_swap_32(unsigned int v)
70 {
71 return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
72 ((v & 0xff0000) >> 8) | (v >> 24);
73 }
74
75 #define le_to_host16(n) (n)
76 #define host_to_le16(n) (n)
77 #define be_to_host16(n) wpa_swap_16(n)
78 #define host_to_be16(n) wpa_swap_16(n)
79 #define le_to_host32(n) (n)
80 #define be_to_host32(n) wpa_swap_32(n)
81 #define host_to_be32(n) wpa_swap_32(n)
82
83 #else /* __CYGWIN__ */
84
85 #ifndef __BYTE_ORDER
86 #ifndef __LITTLE_ENDIAN
87 #ifndef __BIG_ENDIAN
88 #define __LITTLE_ENDIAN 1234
89 #define __BIG_ENDIAN 4321
90 #if defined(sparc)
91 #define __BYTE_ORDER __BIG_ENDIAN
92 #endif
93 #endif /* __BIG_ENDIAN */
94 #endif /* __LITTLE_ENDIAN */
95 #endif /* __BYTE_ORDER */
96
97 #if __BYTE_ORDER == __LITTLE_ENDIAN
98 #define le_to_host16(n) (n)
99 #define host_to_le16(n) (n)
100 #define be_to_host16(n) bswap_16(n)
101 #define host_to_be16(n) bswap_16(n)
102 #define le_to_host32(n) (n)
103 #define be_to_host32(n) bswap_32(n)
104 #define host_to_be32(n) bswap_32(n)
105 #define le_to_host64(n) (n)
106 #define host_to_le64(n) (n)
107 #define be_to_host64(n) bswap_64(n)
108 #define host_to_be64(n) bswap_64(n)
109 #elif __BYTE_ORDER == __BIG_ENDIAN
110 #define le_to_host16(n) bswap_16(n)
111 #define host_to_le16(n) bswap_16(n)
112 #define be_to_host16(n) (n)
113 #define host_to_be16(n) (n)
114 #define le_to_host32(n) bswap_32(n)
115 #define be_to_host32(n) (n)
116 #define host_to_be32(n) (n)
117 #define le_to_host64(n) bswap_64(n)
118 #define host_to_le64(n) bswap_64(n)
119 #define be_to_host64(n) (n)
120 #define host_to_be64(n) (n)
121 #ifndef WORDS_BIGENDIAN
122 #define WORDS_BIGENDIAN
123 #endif
124 #else
125 #error Could not determine CPU byte order
126 #endif
127
128 #endif /* __CYGWIN__ */
129
130 /* Macros for handling unaligned 16-bit variables */
131 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
132 #define WPA_PUT_BE16(a, val) \
133 do { \
134 (a)[0] = ((u16) (val)) >> 8; \
135 (a)[1] = ((u16) (val)) & 0xff; \
136 } while (0)
137
138 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
139 #define WPA_PUT_LE16(a, val) \
140 do { \
141 (a)[1] = ((u16) (val)) >> 8; \
142 (a)[0] = ((u16) (val)) & 0xff; \
143 } while (0)
144
145 #define WPA_GET_BE24(a) ((((u32) (a)[0]) << 16) | (((u32) (a)[1]) << 8) | \
146 ((u32) (a)[2]))
147 #define WPA_PUT_BE24(a, val) \
148 do { \
149 (a)[0] = (u8) (((u32) (val)) >> 16); \
150 (a)[1] = (u8) (((u32) (val)) >> 8); \
151 (a)[2] = (u8) (((u32) (val)) & 0xff); \
152 } while (0)
153
154 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
155 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
156 #define WPA_PUT_BE32(a, val) \
157 do { \
158 (a)[0] = (u8) (((u32) (val)) >> 24); \
159 (a)[1] = (u8) (((u32) (val)) >> 16); \
160 (a)[2] = (u8) (((u32) (val)) >> 8); \
161 (a)[3] = (u8) (((u32) (val)) & 0xff); \
162 } while (0)
163
164 #define WPA_PUT_BE64(a, val) \
165 do { \
166 (a)[0] = (u8) (((u64) (val)) >> 56); \
167 (a)[1] = (u8) (((u64) (val)) >> 48); \
168 (a)[2] = (u8) (((u64) (val)) >> 40); \
169 (a)[3] = (u8) (((u64) (val)) >> 32); \
170 (a)[4] = (u8) (((u64) (val)) >> 24); \
171 (a)[5] = (u8) (((u64) (val)) >> 16); \
172 (a)[6] = (u8) (((u64) (val)) >> 8); \
173 (a)[7] = (u8) (((u64) (val)) & 0xff); \
174 } while (0)
175
176
177 #ifndef ETH_ALEN
178 #define ETH_ALEN 6
179 #endif
180
181 #ifdef _MSC_VER
182 typedef UINT64 u64;
183 typedef UINT32 u32;
184 typedef UINT16 u16;
185 typedef UINT8 u8;
186 typedef INT64 s64;
187 typedef INT32 s32;
188 typedef INT16 s16;
189 typedef INT8 s8;
190 #define WPA_TYPES_DEFINED
191 #endif /* _MSC_VER */
192
193 #ifdef __vxworks
194 typedef unsigned long long u64;
195 typedef UINT32 u32;
196 typedef UINT16 u16;
197 typedef UINT8 u8;
198 typedef long long s64;
199 typedef INT32 s32;
200 typedef INT16 s16;
201 typedef INT8 s8;
202 #define WPA_TYPES_DEFINED
203 #endif /* __vxworks */
204
205 #ifdef CONFIG_TI_COMPILER
206 #ifdef _LLONG_AVAILABLE
207 typedef unsigned long long u64;
208 #else
209 /*
210 * TODO: 64-bit variable not available. Using long as a workaround to test the
211 * build, but this will likely not work for all operations.
212 */
213 typedef unsigned long u64;
214 #endif
215 typedef unsigned int u32;
216 typedef unsigned short u16;
217 typedef unsigned char u8;
218 #define WPA_TYPES_DEFINED
219 #endif /* CONFIG_TI_COMPILER */
220
221 #ifndef WPA_TYPES_DEFINED
222 #ifdef CONFIG_USE_INTTYPES_H
223 #include <inttypes.h>
224 #else
225 #include <stdint.h>
226 #endif
227 typedef uint64_t u64;
228 typedef uint32_t u32;
229 typedef uint16_t u16;
230 typedef uint8_t u8;
231 typedef int64_t s64;
232 typedef int32_t s32;
233 typedef int16_t s16;
234 typedef int8_t s8;
235 #define WPA_TYPES_DEFINED
236 #endif /* !WPA_TYPES_DEFINED */
237
238 #define hostapd_get_rand os_get_random
239 int hwaddr_aton(const char *txt, u8 *addr);
240 int hexstr2bin(const char *hex, u8 *buf, size_t len);
241 void inc_byte_array(u8 *counter, size_t len);
242 void wpa_get_ntp_timestamp(u8 *buf);
243
244
245 #ifdef __GNUC__
246 #define PRINTF_FORMAT(a,b) __attribute__ ((format (printf, (a), (b))))
247 #define STRUCT_PACKED __attribute__ ((packed))
248 #else
249 #define PRINTF_FORMAT(a,b)
250 #define STRUCT_PACKED
251 #endif
252
253
254 /* Debugging function - conditional printf and hex dump. Driver wrappers can
255 * use these for debugging purposes. */
256
257 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
258
259 #ifdef ANDROID
260
261 #define wpa_debug_print_timestamp() do {} while (0)
262 #define wpa_hexdump(...) do {} while (0)
263 #define wpa_hexdump_key(...) do {} while (0)
264 #define wpa_hexdump_ascii(...) do {} while (0)
265 #define wpa_hexdump_ascii_key(...) do {} while (0)
266 #define wpa_debug_open_file(...) do {} while (0)
267 #define wpa_debug_close_file() do {} while (0)
268
269 void android_printf(int level, char *format, ...);
270
271 #define wpa_printf(level, ...) \
272 do { \
273 if ((level) >= MSG_INFO) { \
274 android_printf((level), __VA_ARGS__); \
275 } \
276 } while (0)
277
278 #else /* ANDROID */
279
280 #ifdef CONFIG_NO_STDOUT_DEBUG
281
282 #define wpa_debug_print_timestamp() do { } while (0)
283 #define wpa_printf(args...) do { } while (0)
284 #define wpa_hexdump(l,t,b,le) do { } while (0)
285 #define wpa_hexdump_key(l,t,b,le) do { } while (0)
286 #define wpa_hexdump_ascii(l,t,b,le) do { } while (0)
287 #define wpa_hexdump_ascii_key(l,t,b,le) do { } while (0)
288 #define wpa_debug_open_file(p) do { } while (0)
289 #define wpa_debug_close_file() do { } while (0)
290
291 #else /* CONFIG_NO_STDOUT_DEBUG */
292
293 int wpa_debug_open_file(const char *path);
294 void wpa_debug_close_file(void);
295
296 /**
297 * wpa_debug_printf_timestamp - Print timestamp for debug output
298 *
299 * This function prints a timestamp in <seconds from 1970>.<microsoconds>
300 * format if debug output has been configured to include timestamps in debug
301 * messages.
302 */
303 void wpa_debug_print_timestamp(void);
304
305 /**
306 * wpa_printf - conditional printf
307 * @level: priority level (MSG_*) of the message
308 * @fmt: printf format string, followed by optional arguments
309 *
310 * This function is used to print conditional debugging and error messages. The
311 * output may be directed to stdout, stderr, and/or syslog based on
312 * configuration.
313 *
314 * Note: New line '\n' is added to the end of the text when printing to stdout.
315 */
316 void wpa_printf(int level, char *fmt, ...)
317 PRINTF_FORMAT(2, 3);
318
319 /**
320 * wpa_hexdump - conditional hex dump
321 * @level: priority level (MSG_*) of the message
322 * @title: title of for the message
323 * @buf: data buffer to be dumped
324 * @len: length of the buf
325 *
326 * This function is used to print conditional debugging and error messages. The
327 * output may be directed to stdout, stderr, and/or syslog based on
328 * configuration. The contents of buf is printed out has hex dump.
329 */
330 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
331
332 /**
333 * wpa_hexdump_key - conditional hex dump, hide keys
334 * @level: priority level (MSG_*) of the message
335 * @title: title of for the message
336 * @buf: data buffer to be dumped
337 * @len: length of the buf
338 *
339 * This function is used to print conditional debugging and error messages. The
340 * output may be directed to stdout, stderr, and/or syslog based on
341 * configuration. The contents of buf is printed out has hex dump. This works
342 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
343 * etc.) in debug output.
344 */
345 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
346
347 /**
348 * wpa_hexdump_ascii - conditional hex dump
349 * @level: priority level (MSG_*) of the message
350 * @title: title of for the message
351 * @buf: data buffer to be dumped
352 * @len: length of the buf
353 *
354 * This function is used to print conditional debugging and error messages. The
355 * output may be directed to stdout, stderr, and/or syslog based on
356 * configuration. The contents of buf is printed out has hex dump with both
357 * the hex numbers and ASCII characters (for printable range) are shown. 16
358 * bytes per line will be shown.
359 */
360 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
361 size_t len);
362
363 /**
364 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
365 * @level: priority level (MSG_*) of the message
366 * @title: title of for the message
367 * @buf: data buffer to be dumped
368 * @len: length of the buf
369 *
370 * This function is used to print conditional debugging and error messages. The
371 * output may be directed to stdout, stderr, and/or syslog based on
372 * configuration. The contents of buf is printed out has hex dump with both
373 * the hex numbers and ASCII characters (for printable range) are shown. 16
374 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
375 * default, does not include secret keys (passwords, etc.) in debug output.
376 */
377 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
378 size_t len);
379
380 #endif /* CONFIG_NO_STDOUT_DEBUG */
381
382 #endif /* ANDROID */
383
384 #ifdef CONFIG_NO_WPA_MSG
385 #define wpa_msg(args...) do { } while (0)
386 #define wpa_msg_register_cb(f) do { } while (0)
387 #else /* CONFIG_NO_WPA_MSG */
388 /**
389 * wpa_msg - Conditional printf for default target and ctrl_iface monitors
390 * @ctx: Pointer to context data; this is the ctx variable registered
391 * with struct wpa_driver_ops::init()
392 * @level: priority level (MSG_*) of the message
393 * @fmt: printf format string, followed by optional arguments
394 *
395 * This function is used to print conditional debugging and error messages. The
396 * output may be directed to stdout, stderr, and/or syslog based on
397 * configuration. This function is like wpa_printf(), but it also sends the
398 * same message to all attached ctrl_iface monitors.
399 *
400 * Note: New line '\n' is added to the end of the text when printing to stdout.
401 */
402 void wpa_msg(void *ctx, int level, char *fmt, ...) PRINTF_FORMAT(3, 4);
403
404 typedef void (*wpa_msg_cb_func)(void *ctx, int level, const char *txt,
405 size_t len);
406
407 /**
408 * wpa_msg_register_cb - Register callback function for wpa_msg() messages
409 * @func: Callback function (%NULL to unregister)
410 */
411 void wpa_msg_register_cb(wpa_msg_cb_func func);
412 #endif /* CONFIG_NO_WPA_MSG */
413
414
415 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len);
416 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
417 size_t len);
418
419
420 #ifdef EAPOL_TEST
421 #define WPA_ASSERT(a) \
422 do { \
423 if (!(a)) { \
424 printf("WPA_ASSERT FAILED '" #a "' " \
425 "%s %s:%d\n", \
426 __FUNCTION__, __FILE__, __LINE__); \
427 exit(1); \
428 } \
429 } while (0)
430 #else
431 #define WPA_ASSERT(a) do { } while (0)
432 #endif
433
434
435 #ifdef _MSC_VER
436 #undef vsnprintf
437 #define vsnprintf _vsnprintf
438 #undef close
439 #define close closesocket
440 #endif /* _MSC_VER */
441
442
443 #ifdef CONFIG_ANSI_C_EXTRA
444
445 #if !defined(_MSC_VER) || _MSC_VER < 1400
446 /* snprintf - used in number of places; sprintf() is _not_ a good replacement
447 * due to possible buffer overflow; see, e.g.,
448 * http://www.ijs.si/software/snprintf/ for portable implementation of
449 * snprintf. */
450 int snprintf(char *str, size_t size, const char *format, ...);
451
452 /* vsnprintf - only used for wpa_msg() in wpa_supplicant.c */
453 int vsnprintf(char *str, size_t size, const char *format, va_list ap);
454 #endif /* !defined(_MSC_VER) || _MSC_VER < 1400 */
455
456 /* getopt - only used in main.c */
457 int getopt(int argc, char *const argv[], const char *optstring);
458 extern char *optarg;
459 extern int optind;
460
461 #ifndef CONFIG_NO_SOCKLEN_T_TYPEDEF
462 #ifndef __socklen_t_defined
463 typedef int socklen_t;
464 #endif
465 #endif
466
467 /* inline - define as __inline or just define it to be empty, if needed */
468 #ifdef CONFIG_NO_INLINE
469 #define inline
470 #else
471 #define inline __inline
472 #endif
473
474 #ifndef __func__
475 #define __func__ "__func__ not defined"
476 #endif
477
478 #ifndef bswap_16
479 #define bswap_16(a) ((((u16) (a) << 8) & 0xff00) | (((u16) (a) >> 8) & 0xff))
480 #endif
481
482 #ifndef bswap_32
483 #define bswap_32(a) ((((u32) (a) << 24) & 0xff000000) | \
484 (((u32) (a) << 8) & 0xff0000) | \
485 (((u32) (a) >> 8) & 0xff00) | \
486 (((u32) (a) >> 24) & 0xff))
487 #endif
488
489 #ifndef MSG_DONTWAIT
490 #define MSG_DONTWAIT 0
491 #endif
492
493 #ifdef _WIN32_WCE
494 void perror(const char *s);
495 #endif /* _WIN32_WCE */
496
497 #endif /* CONFIG_ANSI_C_EXTRA */
498
499 #define wpa_zalloc(s) os_zalloc((s))
500
501 #ifdef CONFIG_NATIVE_WINDOWS
502 void wpa_unicode2ascii_inplace(TCHAR *str);
503 TCHAR * wpa_strdup_tchar(const char *str);
504 #else /* CONFIG_NATIVE_WINDOWS */
505 #define wpa_unicode2ascii_inplace(s) do { } while (0)
506 #define wpa_strdup_tchar(s) strdup((s))
507 #endif /* CONFIG_NATIVE_WINDOWS */
508
509 const char * wpa_ssid_txt(u8 *ssid, size_t ssid_len);
510
511 #ifndef ANDROID
512 typedef u32 __be32;
513 typedef u64 __be64;
514 #endif /* Dm: */
515
516 #endif /* COMMON_H */
517