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