2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2005, Jouni Malinen <jkmaline@cc.hut.fi>
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
21 #endif /* __linux__ */
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__) */
34 #ifdef CONFIG_NATIVE_WINDOWS
37 static inline int daemon(int nochdir
, int noclose
)
39 printf("Windows - daemon() not supported yet\n");
43 static inline void sleep(int seconds
)
45 Sleep(seconds
* 1000);
48 static inline void usleep(unsigned long usec
)
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) { \
59 (res)->tv_usec += 1000000; \
69 int gettimeofday(struct timeval
*tv
, struct timezone
*tz
);
71 static inline long int random(void)
77 typedef int socklen_t
;
80 #define MSG_DONTWAIT 0 /* not supported */
83 #endif /* CONFIG_NATIVE_WINDOWS */
85 #if defined(__CYGWIN__) || defined(CONFIG_NATIVE_WINDOWS)
87 static inline unsigned short wpa_swap_16(unsigned short v
)
89 return ((v
& 0xff) << 8) | (v
>> 8);
92 static inline unsigned int wpa_swap_32(unsigned int v
)
94 return ((v
& 0xff) << 24) | ((v
& 0xff00) << 8) |
95 ((v
& 0xff0000) >> 8) | (v
>> 24);
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)
106 #else /* __CYGWIN__ */
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
128 #error Could not determine CPU byte order
131 #endif /* __CYGWIN__ */
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) \
137 (a)[0] = ((u16) (val)) >> 8; \
138 (a)[1] = ((u16) (val)) & 0xff; \
141 #define WPA_GET_LE16(a) ((u16) (((a)[1] << 8) | (a)[0]))
142 #define WPA_PUT_LE16(a, val) \
144 (a)[1] = ((u16) (val)) >> 8; \
145 (a)[0] = ((u16) (val)) & 0xff; \
148 #define WPA_GET_BE32(a) ((((u32) (a)[0]) << 24) | (((u32) (a)[1]) << 16) | \
149 (((u32) (a)[2]) << 8) | ((u32) (a)[3]))
157 typedef uint64_t u64
;
158 typedef uint32_t u32
;
159 typedef uint16_t u16
;
166 int hostapd_get_rand(u8
*buf
, size_t len
);
167 void hostapd_hexdump(const char *title
, const u8
*buf
, size_t len
);
168 int hwaddr_aton(const char *txt
, u8
*addr
);
169 int hexstr2bin(const char *hex
, u8
*buf
, size_t len
);
170 char * rel2abs_path(const char *rel_path
);
171 void inc_byte_array(u8
*counter
, size_t len
);
172 void print_char(char c
);
173 void fprint_char(FILE *f
, char c
);
176 /* Debugging function - conditional printf and hex dump. Driver wrappers can
177 * use these for debugging purposes. */
179 enum { MSG_MSGDUMP
, MSG_DEBUG
, MSG_INFO
, MSG_WARNING
, MSG_ERROR
};
181 #ifdef CONFIG_NO_STDOUT_DEBUG
183 #define wpa_debug_print_timestamp() do { } while (0)
184 #define wpa_printf(args...) do { } while (0)
185 #define wpa_hexdump(args...) do { } while (0)
186 #define wpa_hexdump_key(args...) do { } while (0)
187 #define wpa_hexdump_ascii(args...) do { } while (0)
188 #define wpa_hexdump_ascii_key(args...) do { } while (0)
190 #else /* CONFIG_NO_STDOUT_DEBUG */
193 * wpa_debug_printf_timestamp - Print timestamp for debug output
195 * This function prints a timestamp in <seconds from 1970>.<microsoconds>
196 * format if debug output has been configured to include timestamps in debug
199 void wpa_debug_print_timestamp(void);
202 * wpa_printf - conditional printf
203 * @level: priority level (MSG_*) of the message
204 * @fmt: printf format string, followed by optional arguments
206 * This function is used to print conditional debugging and error messages. The
207 * output may be directed to stdout, stderr, and/or syslog based on
210 * Note: New line '\n' is added to the end of the text when printing to stdout.
212 void wpa_printf(int level
, char *fmt
, ...)
213 __attribute__ ((format (printf
, 2, 3)));
216 * wpa_hexdump - conditional hex dump
217 * @level: priority level (MSG_*) of the message
218 * @title: title of for the message
219 * @buf: data buffer to be dumped
220 * @len: length of the buf
222 * This function is used to print conditional debugging and error messages. The
223 * output may be directed to stdout, stderr, and/or syslog based on
224 * configuration. The contents of buf is printed out has hex dump.
226 void wpa_hexdump(int level
, const char *title
, const u8
*buf
, size_t len
);
229 * wpa_hexdump_key - conditional hex dump, hide keys
230 * @level: priority level (MSG_*) of the message
231 * @title: title of for the message
232 * @buf: data buffer to be dumped
233 * @len: length of the buf
235 * This function is used to print conditional debugging and error messages. The
236 * output may be directed to stdout, stderr, and/or syslog based on
237 * configuration. The contents of buf is printed out has hex dump. This works
238 * like wpa_hexdump(), but by default, does not include secret keys (passwords,
239 * etc.) in debug output.
241 void wpa_hexdump_key(int level
, const char *title
, const u8
*buf
, size_t len
);
244 * wpa_hexdump_ascii - conditional hex dump
245 * @level: priority level (MSG_*) of the message
246 * @title: title of for the message
247 * @buf: data buffer to be dumped
248 * @len: length of the buf
250 * This function is used to print conditional debugging and error messages. The
251 * output may be directed to stdout, stderr, and/or syslog based on
252 * configuration. The contents of buf is printed out has hex dump with both
253 * the hex numbers and ASCII characters (for printable range) are shown. 16
254 * bytes per line will be shown.
256 void wpa_hexdump_ascii(int level
, const char *title
, const u8
*buf
,
260 * wpa_hexdump_ascii_key - conditional hex dump, hide keys
261 * @level: priority level (MSG_*) of the message
262 * @title: title of for the message
263 * @buf: data buffer to be dumped
264 * @len: length of the buf
266 * This function is used to print conditional debugging and error messages. The
267 * output may be directed to stdout, stderr, and/or syslog based on
268 * configuration. The contents of buf is printed out has hex dump with both
269 * the hex numbers and ASCII characters (for printable range) are shown. 16
270 * bytes per line will be shown. This works like wpa_hexdump_ascii(), but by
271 * default, does not include secret keys (passwords, etc.) in debug output.
273 void wpa_hexdump_ascii_key(int level
, const char *title
, const u8
*buf
,
276 #endif /* CONFIG_NO_STDOUT_DEBUG */
280 #define WPA_ASSERT(a) \
283 printf("WPA_ASSERT FAILED '" #a "' " \
285 __FUNCTION__, __FILE__, __LINE__); \
290 #define WPA_ASSERT(a) do { } while (0)
293 #endif /* COMMON_H */