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