]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/wpa_supplicant/common.h
This commit was generated by cvs2svn to compensate for changes in r165071,
[FreeBSD/FreeBSD.git] / contrib / wpa_supplicant / common.h
1 /*
2  * wpa_supplicant/hostapd / common helper functions, etc.
3  * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.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 #ifdef __linux__
21 #include <endian.h>
22 #include <byteswap.h>
23 #endif /* __linux__ */
24
25 #if defined(__FreeBSD__) || defined(__NetBSD__)
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
36 #ifdef CONFIG_NATIVE_WINDOWS
37 #include <winsock2.h>
38
39 static inline int daemon(int nochdir, int noclose)
40 {
41         printf("Windows - daemon() not supported yet\n");
42         return -1;
43 }
44
45 static inline void sleep(int seconds)
46 {
47         Sleep(seconds * 1000);
48 }
49
50 static inline void usleep(unsigned long usec)
51 {
52         Sleep(usec / 1000);
53 }
54
55 #ifndef timersub
56 #define timersub(a, b, res) do { \
57         (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
58         (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
59         if ((res)->tv_usec < 0) { \
60                 (res)->tv_sec--; \
61                 (res)->tv_usec += 1000000; \
62         } \
63 } while (0)
64 #endif
65
66 struct timezone {
67         int  tz_minuteswest;
68         int  tz_dsttime;
69 };
70
71 int gettimeofday(struct timeval *tv, struct timezone *tz);
72
73 static inline long int random(void)
74 {
75         return rand();
76 }
77
78 typedef int gid_t;
79 typedef int socklen_t;
80
81 #ifndef MSG_DONTWAIT
82 #define MSG_DONTWAIT 0 /* not supported */
83 #endif
84
85 #endif /* CONFIG_NATIVE_WINDOWS */
86
87 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
88
89 static inline unsigned short wpa_swap_16(unsigned short v)
90 {
91         return ((v & 0xff) << 8) | (v >> 8);
92 }
93
94 static inline unsigned int wpa_swap_32(unsigned int v)
95 {
96         return ((v & 0xff) << 24) | ((v & 0xff00) << 8) |
97                 ((v & 0xff0000) >> 8) | (v >> 24);
98 }
99
100 #define le_to_host16(n) (n)
101 #define host_to_le16(n) (n)
102 #define be_to_host16(n) wpa_swap_16(n)
103 #define host_to_be16(n) wpa_swap_16(n)
104 #define le_to_host32(n) (n)
105 #define be_to_host32(n) wpa_swap_32(n)
106 #define host_to_be32(n) wpa_swap_32(n)
107
108 #else /* __CYGWIN__ */
109
110 #if __BYTE_ORDER == __LITTLE_ENDIAN
111 #define le_to_host16(n) (n)
112 #define host_to_le16(n) (n)
113 #define be_to_host16(n) bswap_16(n)
114 #define host_to_be16(n) bswap_16(n)
115 #define le_to_host32(n) (n)
116 #define be_to_host32(n) bswap_32(n)
117 #define host_to_be32(n) bswap_32(n)
118 #elif __BYTE_ORDER == __BIG_ENDIAN
119 #define le_to_host16(n) bswap_16(n)
120 #define host_to_le16(n) bswap_16(n)
121 #define be_to_host16(n) (n)
122 #define host_to_be16(n) (n)
123 #define le_to_host32(n) bswap_32(n)
124 #define be_to_host32(n) (n)
125 #define host_to_be32(n) (n)
126 #ifndef WORDS_BIGENDIAN
127 #define WORDS_BIGENDIAN
128 #endif
129 #else
130 #error Could not determine CPU byte order
131 #endif
132
133 #endif /* __CYGWIN__ */
134
135 /* Macros for handling unaligned 16-bit variables */
136 #define WPA_GET_BE16(a) ((u16) (((a)[0] << 8) | (a)[1]))
137 #define WPA_PUT_BE16(a, val)                    \
138         do {                                    \
139                 (a)[0] = ((u16) (val)) >> 8;    \
140                 (a)[1] = ((u16) (val)) & 0xff;  \
141         } while (0)
142
143 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
144 #define WPA_PUT_LE16(a, val)                    \
145         do {                                    \
146                 (a)[1] = ((u16) (val)) >> 8;    \
147                 (a)[0] = ((u16) (val)) & 0xff;  \
148         } while (0)
149
150
151 #ifndef ETH_ALEN
152 #define ETH_ALEN 6
153 #endif
154
155 #include <stdint.h>
156 typedef uint64_t u64;
157 typedef uint32_t u32;
158 typedef uint16_t u16;
159 typedef uint8_t u8;
160 typedef int64_t s64;
161 typedef int32_t s32;
162 typedef int16_t s16;
163 typedef int8_t s8;
164
165 int hostapd_get_rand(u8 *buf, size_t len);
166 void hostapd_hexdump(const char *title, const u8 *buf, size_t len);
167 int hwaddr_aton(const char *txt, u8 *addr);
168 int hexstr2bin(const char *hex, u8 *buf, size_t len);
169 char * rel2abs_path(const char *rel_path);
170 void inc_byte_array(u8 *counter, size_t len);
171 void print_char(char c);
172 void fprint_char(FILE *f, char c);
173
174
175 /* Debugging function - conditional printf and hex dump. Driver wrappers can
176  *  use these for debugging purposes. */
177
178 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
179
180 #ifdef CONFIG_NO_STDOUT_DEBUG
181
182 #define wpa_debug_print_timestamp() do { } while (0)
183 #define wpa_printf(args...) do { } while (0)
184 #define wpa_hexdump(args...) do { } while (0)
185 #define wpa_hexdump_key(args...) do { } while (0)
186 #define wpa_hexdump_ascii(args...) do { } while (0)
187 #define wpa_hexdump_ascii_key(args...) do { } while (0)
188
189 #else /* CONFIG_NO_STDOUT_DEBUG */
190
191 /**
192  * wpa_debug_printf_timestamp - Print timestamp for debug output
193  *
194  * This function prints a timestamp in <seconds from 1970>.<microsoconds>
195  * format if debug output has been configured to include timestamps in debug
196  * messages.
197  */
198 void wpa_debug_print_timestamp(void);
199
200 /**
201  * wpa_printf - conditional printf
202  * @level: priority level (MSG_*) of the message
203  * @fmt: printf format string, followed by optional arguments
204  *
205  * This function is used to print conditional debugging and error messages. The
206  * output may be directed to stdout, stderr, and/or syslog based on
207  * configuration.
208  *
209  * Note: New line '\n' is added to the end of the text when printing to stdout.
210  */
211 void wpa_printf(int level, char *fmt, ...)
212 __attribute__ ((format (printf, 2, 3)));
213
214 /**
215  * wpa_hexdump - conditional hex dump
216  * @level: priority level (MSG_*) of the message
217  * @title: title of for the message
218  * @buf: data buffer to be dumped
219  * @len: length of the buf
220  *
221  * This function is used to print conditional debugging and error messages. The
222  * output may be directed to stdout, stderr, and/or syslog based on
223  * configuration. The contents of buf is printed out has hex dump.
224  */
225 void wpa_hexdump(int level, const char *title, const u8 *buf, size_t len);
226
227 /**
228  * wpa_hexdump_key - conditional hex dump, hide keys
229  * @level: priority level (MSG_*) of the message
230  * @title: title of for the message
231  * @buf: data buffer to be dumped
232  * @len: length of the buf
233  *
234  * This function is used to print conditional debugging and error messages. The
235  * output may be directed to stdout, stderr, and/or syslog based on
236  * configuration. The contents of buf is printed out has hex dump. This works
237  * like wpa_hexdump(), but by default, does not include secret keys (passwords,
238  * etc.) in debug output.
239  */
240 void wpa_hexdump_key(int level, const char *title, const u8 *buf, size_t len);
241
242 /**
243  * wpa_hexdump_ascii - conditional hex dump
244  * @level: priority level (MSG_*) of the message
245  * @title: title of for the message
246  * @buf: data buffer to be dumped
247  * @len: length of the buf
248  *
249  * This function is used to print conditional debugging and error messages. The
250  * output may be directed to stdout, stderr, and/or syslog based on
251  * configuration. The contents of buf is printed out has hex dump with both
252  * the hex numbers and ASCII characters (for printable range) are shown. 16
253  * bytes per line will be shown.
254  */
255 void wpa_hexdump_ascii(int level, const char *title, const u8 *buf,
256                        size_t len);
257
258 /**
259  * wpa_hexdump_ascii_key - conditional hex dump, hide keys
260  * @level: priority level (MSG_*) of the message
261  * @title: title of for the message
262  * @buf: data buffer to be dumped
263  * @len: length of the buf
264  *
265  * This function is used to print conditional debugging and error messages. The
266  * output may be directed to stdout, stderr, and/or syslog based on
267  * configuration. The contents of buf is printed out has hex dump with both
268  * the hex numbers and ASCII characters (for printable range) are shown. 16
269  * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
270  * default, does not include secret keys (passwords, etc.) in debug output.
271  */
272 void wpa_hexdump_ascii_key(int level, const char *title, const u8 *buf,
273                            size_t len);
274
275 #endif /* CONFIG_NO_STDOUT_DEBUG */
276
277
278 #ifdef EAPOL_TEST
279 #define WPA_ASSERT(a)                                                  \
280         do {                                                           \
281                 if (!(a)) {                                            \
282                         printf("WPA_ASSERT FAILED '" #a "' "           \
283                                "%s %s:%d\n",                           \
284                                __FUNCTION__, __FILE__, __LINE__);      \
285                         exit(1);                                       \
286                 }                                                      \
287         } while (0)
288 #else
289 #define WPA_ASSERT(a) do { } while (0)
290 #endif
291
292 #endif /* COMMON_H */