1 /*
2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, 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 #include "includes.h"
16
17 #include "common.h"
18
19
hex2num(char c)20 static int hex2num(char c)
21 {
22 if (c >= '0' && c <= '9')
23 return c - '0';
24 if (c >= 'a' && c <= 'f')
25 return c - 'a' + 10;
26 if (c >= 'A' && c <= 'F')
27 return c - 'A' + 10;
28 return -1;
29 }
30
31
hex2byte(const char * hex)32 static int hex2byte(const char *hex)
33 {
34 int a, b;
35 a = hex2num(*hex++);
36 if (a < 0)
37 return -1;
38 b = hex2num(*hex++);
39 if (b < 0)
40 return -1;
41 return (a << 4) | b;
42 }
43
44
45 /**
46 * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
47 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
48 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
49 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
50 */
hwaddr_aton(const char * txt,u8 * addr)51 int hwaddr_aton(const char *txt, u8 *addr)
52 {
53 int i;
54
55 for (i = 0; i < 6; i++) {
56 int a, b;
57
58 a = hex2num(*txt++);
59 if (a < 0)
60 return -1;
61 b = hex2num(*txt++);
62 if (b < 0)
63 return -1;
64 *addr++ = (a << 4) | b;
65 if (i < 5 && *txt++ != ':')
66 return -1;
67 }
68
69 return 0;
70 }
71
72
73 /**
74 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
75 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
76 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
77 * Returns: Characters used (> 0) on success, -1 on failure
78 */
hwaddr_aton2(const char * txt,u8 * addr)79 int hwaddr_aton2(const char *txt, u8 *addr)
80 {
81 int i;
82 const char *pos = txt;
83
84 for (i = 0; i < 6; i++) {
85 int a, b;
86
87 while (*pos == ':' || *pos == '.' || *pos == '-')
88 pos++;
89
90 a = hex2num(*pos++);
91 if (a < 0)
92 return -1;
93 b = hex2num(*pos++);
94 if (b < 0)
95 return -1;
96 *addr++ = (a << 4) | b;
97 }
98
99 return pos - txt;
100 }
101
102
103 /**
104 * hexstr2bin - Convert ASCII hex string into binary data
105 * @hex: ASCII hex string (e.g., "01ab")
106 * @buf: Buffer for the binary data
107 * @len: Length of the text to convert in bytes (of buf); hex will be double
108 * this size
109 * Returns: 0 on success, -1 on failure (invalid hex string)
110 */
hexstr2bin(const char * hex,u8 * buf,size_t len)111 int hexstr2bin(const char *hex, u8 *buf, size_t len)
112 {
113 size_t i;
114 int a;
115 const char *ipos = hex;
116 u8 *opos = buf;
117
118 for (i = 0; i < len; i++) {
119 a = hex2byte(ipos);
120 if (a < 0)
121 return -1;
122 *opos++ = a;
123 ipos += 2;
124 }
125 return 0;
126 }
127
128
129 /**
130 * inc_byte_array - Increment arbitrary length byte array by one
131 * @counter: Pointer to byte array
132 * @len: Length of the counter in bytes
133 *
134 * This function increments the last byte of the counter by one and continues
135 * rolling over to more significant bytes if the byte was incremented from
136 * 0xff to 0x00.
137 */
inc_byte_array(u8 * counter,size_t len)138 void inc_byte_array(u8 *counter, size_t len)
139 {
140 int pos = len - 1;
141 while (pos >= 0) {
142 counter[pos]++;
143 if (counter[pos] != 0)
144 break;
145 pos--;
146 }
147 }
148
149
wpa_get_ntp_timestamp(u8 * buf)150 void wpa_get_ntp_timestamp(u8 *buf)
151 {
152 struct os_time now;
153 u32 sec, usec;
154 be32 tmp;
155
156 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
157 os_get_time(&now);
158 sec = now.sec + 2208988800U; /* Epoch to 1900 */
159 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
160 usec = now.usec;
161 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
162 tmp = host_to_be32(sec);
163 os_memcpy(buf, (u8 *) &tmp, 4);
164 tmp = host_to_be32(usec);
165 os_memcpy(buf + 4, (u8 *) &tmp, 4);
166 }
167
168
_wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len,int uppercase)169 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
170 size_t len, int uppercase)
171 {
172 size_t i;
173 char *pos = buf, *end = buf + buf_size;
174 int ret;
175 if (buf_size == 0)
176 return 0;
177 for (i = 0; i < len; i++) {
178 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
179 data[i]);
180 if (ret < 0 || ret >= end - pos) {
181 end[-1] = '\0';
182 return pos - buf;
183 }
184 pos += ret;
185 }
186 end[-1] = '\0';
187 return pos - buf;
188 }
189
190 /**
191 * wpa_snprintf_hex - Print data as a hex string into a buffer
192 * @buf: Memory area to use as the output buffer
193 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
194 * @data: Data to be printed
195 * @len: Length of data in bytes
196 * Returns: Number of bytes written
197 */
wpa_snprintf_hex(char * buf,size_t buf_size,const u8 * data,size_t len)198 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
199 {
200 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
201 }
202
203
204 /**
205 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
206 * @buf: Memory area to use as the output buffer
207 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
208 * @data: Data to be printed
209 * @len: Length of data in bytes
210 * Returns: Number of bytes written
211 */
wpa_snprintf_hex_uppercase(char * buf,size_t buf_size,const u8 * data,size_t len)212 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
213 size_t len)
214 {
215 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
216 }
217
218
219 #ifdef CONFIG_ANSI_C_EXTRA
220
221 #ifdef _WIN32_WCE
perror(const char * s)222 void perror(const char *s)
223 {
224 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
225 s, (int) GetLastError());
226 }
227 #endif /* _WIN32_WCE */
228
229
230 int optind = 1;
231 int optopt;
232 char *optarg;
233
getopt(int argc,char * const argv[],const char * optstring)234 int getopt(int argc, char *const argv[], const char *optstring)
235 {
236 static int optchr = 1;
237 char *cp;
238
239 if (optchr == 1) {
240 if (optind >= argc) {
241 /* all arguments processed */
242 return EOF;
243 }
244
245 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
246 /* no option characters */
247 return EOF;
248 }
249 }
250
251 if (os_strcmp(argv[optind], "--") == 0) {
252 /* no more options */
253 optind++;
254 return EOF;
255 }
256
257 optopt = argv[optind][optchr];
258 cp = os_strchr(optstring, optopt);
259 if (cp == NULL || optopt == ':') {
260 if (argv[optind][++optchr] == '\0') {
261 optchr = 1;
262 optind++;
263 }
264 return '?';
265 }
266
267 if (cp[1] == ':') {
268 /* Argument required */
269 optchr = 1;
270 if (argv[optind][optchr + 1]) {
271 /* No space between option and argument */
272 optarg = &argv[optind++][optchr + 1];
273 } else if (++optind >= argc) {
274 /* option requires an argument */
275 return '?';
276 } else {
277 /* Argument in the next argv */
278 optarg = argv[optind++];
279 }
280 } else {
281 /* No argument */
282 if (argv[optind][++optchr] == '\0') {
283 optchr = 1;
284 optind++;
285 }
286 optarg = NULL;
287 }
288 return *cp;
289 }
290 #endif /* CONFIG_ANSI_C_EXTRA */
291
292
293 #ifdef CONFIG_NATIVE_WINDOWS
294 /**
295 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
296 * @str: Pointer to string to convert
297 *
298 * This function converts a unicode string to ASCII using the same
299 * buffer for output. If UNICODE is not set, the buffer is not
300 * modified.
301 */
wpa_unicode2ascii_inplace(TCHAR * str)302 void wpa_unicode2ascii_inplace(TCHAR *str)
303 {
304 #ifdef UNICODE
305 char *dst = (char *) str;
306 while (*str)
307 *dst++ = (char) *str++;
308 *dst = '\0';
309 #endif /* UNICODE */
310 }
311
312
wpa_strdup_tchar(const char * str)313 TCHAR * wpa_strdup_tchar(const char *str)
314 {
315 #ifdef UNICODE
316 TCHAR *buf;
317 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
318 if (buf == NULL)
319 return NULL;
320 wsprintf(buf, L"%S", str);
321 return buf;
322 #else /* UNICODE */
323 return os_strdup(str);
324 #endif /* UNICODE */
325 }
326 #endif /* CONFIG_NATIVE_WINDOWS */
327
328
329 /**
330 * wpa_ssid_txt - Convert SSID to a printable string
331 * @ssid: SSID (32-octet string)
332 * @ssid_len: Length of ssid in octets
333 * Returns: Pointer to a printable string
334 *
335 * This function can be used to convert SSIDs into printable form. In most
336 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
337 * does not limit the used character set, so anything could be used in an SSID.
338 *
339 * This function uses a static buffer, so only one call can be used at the
340 * time, i.e., this is not re-entrant and the returned buffer must be used
341 * before calling this again.
342 */
wpa_ssid_txt(const u8 * ssid,size_t ssid_len)343 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
344 {
345 static char ssid_txt[33];
346 char *pos;
347
348 if (ssid_len > 32)
349 ssid_len = 32;
350 os_memcpy(ssid_txt, ssid, ssid_len);
351 ssid_txt[ssid_len] = '\0';
352 for (pos = ssid_txt; *pos != '\0'; pos++) {
353 if ((u8) *pos < 32 || (u8) *pos >= 127)
354 *pos = '_';
355 }
356 return ssid_txt;
357 }
358
359
__hide_aliasing_typecast(void * foo)360 void * __hide_aliasing_typecast(void *foo)
361 {
362 return foo;
363 }
364