2 * wpa_supplicant/hostapd / common helper functions, etc.
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 static int hex2num(char c)
16 if (c >= '0' && c <= '9')
18 if (c >= 'a' && c <= 'f')
20 if (c >= 'A' && c <= 'F')
26 int hex2byte(const char *hex)
40 * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
41 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
42 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
43 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
45 int hwaddr_aton(const char *txt, u8 *addr)
49 for (i = 0; i < 6; i++) {
58 *addr++ = (a << 4) | b;
59 if (i < 5 && *txt++ != ':')
67 * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format)
68 * @txt: MAC address as a string (e.g., "001122334455")
69 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
70 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
72 int hwaddr_compact_aton(const char *txt, u8 *addr)
76 for (i = 0; i < 6; i++) {
85 *addr++ = (a << 4) | b;
92 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
93 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
94 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
95 * Returns: Characters used (> 0) on success, -1 on failure
97 int hwaddr_aton2(const char *txt, u8 *addr)
100 const char *pos = txt;
102 for (i = 0; i < 6; i++) {
105 while (*pos == ':' || *pos == '.' || *pos == '-')
114 *addr++ = (a << 4) | b;
122 * hexstr2bin - Convert ASCII hex string into binary data
123 * @hex: ASCII hex string (e.g., "01ab")
124 * @buf: Buffer for the binary data
125 * @len: Length of the text to convert in bytes (of buf); hex will be double
127 * Returns: 0 on success, -1 on failure (invalid hex string)
129 int hexstr2bin(const char *hex, u8 *buf, size_t len)
133 const char *ipos = hex;
136 for (i = 0; i < len; i++) {
148 * inc_byte_array - Increment arbitrary length byte array by one
149 * @counter: Pointer to byte array
150 * @len: Length of the counter in bytes
152 * This function increments the last byte of the counter by one and continues
153 * rolling over to more significant bytes if the byte was incremented from
156 void inc_byte_array(u8 *counter, size_t len)
161 if (counter[pos] != 0)
168 void wpa_get_ntp_timestamp(u8 *buf)
174 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
176 sec = now.sec + 2208988800U; /* Epoch to 1900 */
177 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
179 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
180 tmp = host_to_be32(sec);
181 os_memcpy(buf, (u8 *) &tmp, 4);
182 tmp = host_to_be32(usec);
183 os_memcpy(buf + 4, (u8 *) &tmp, 4);
187 * wpa_scnprintf - Simpler-to-use snprintf function
188 * @buf: Output buffer
192 * Simpler snprintf version that doesn't require further error checks - the
193 * return value only indicates how many bytes were actually written, excluding
194 * the NULL byte (i.e., 0 on error, size-1 if buffer is not big enough).
196 int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
205 ret = vsnprintf(buf, size, fmt, ap);
210 if ((size_t) ret >= size)
216 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
217 size_t len, int uppercase)
220 char *pos = buf, *end = buf + buf_size;
224 for (i = 0; i < len; i++) {
225 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
227 if (os_snprintf_error(end - pos, ret)) {
238 * wpa_snprintf_hex - Print data as a hex string into a buffer
239 * @buf: Memory area to use as the output buffer
240 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
241 * @data: Data to be printed
242 * @len: Length of data in bytes
243 * Returns: Number of bytes written
245 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
247 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
252 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
253 * @buf: Memory area to use as the output buffer
254 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
255 * @data: Data to be printed
256 * @len: Length of data in bytes
257 * Returns: Number of bytes written
259 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
262 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
266 #ifdef CONFIG_ANSI_C_EXTRA
269 void perror(const char *s)
271 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
272 s, (int) GetLastError());
274 #endif /* _WIN32_WCE */
281 int getopt(int argc, char *const argv[], const char *optstring)
283 static int optchr = 1;
287 if (optind >= argc) {
288 /* all arguments processed */
292 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
293 /* no option characters */
298 if (os_strcmp(argv[optind], "--") == 0) {
299 /* no more options */
304 optopt = argv[optind][optchr];
305 cp = os_strchr(optstring, optopt);
306 if (cp == NULL || optopt == ':') {
307 if (argv[optind][++optchr] == '\0') {
315 /* Argument required */
317 if (argv[optind][optchr + 1]) {
318 /* No space between option and argument */
319 optarg = &argv[optind++][optchr + 1];
320 } else if (++optind >= argc) {
321 /* option requires an argument */
324 /* Argument in the next argv */
325 optarg = argv[optind++];
329 if (argv[optind][++optchr] == '\0') {
337 #endif /* CONFIG_ANSI_C_EXTRA */
340 #ifdef CONFIG_NATIVE_WINDOWS
342 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
343 * @str: Pointer to string to convert
345 * This function converts a unicode string to ASCII using the same
346 * buffer for output. If UNICODE is not set, the buffer is not
349 void wpa_unicode2ascii_inplace(TCHAR *str)
352 char *dst = (char *) str;
354 *dst++ = (char) *str++;
360 TCHAR * wpa_strdup_tchar(const char *str)
364 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
367 wsprintf(buf, L"%S", str);
370 return os_strdup(str);
373 #endif /* CONFIG_NATIVE_WINDOWS */
376 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
378 char *end = txt + maxlen;
381 for (i = 0; i < len; i++) {
411 if (data[i] >= 32 && data[i] <= 127) {
414 txt += os_snprintf(txt, end - txt, "\\x%02x",
425 size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
427 const char *pos = str;
432 if (len + 1 >= maxlen)
485 if (*pos >= '0' && *pos <= '7')
486 val = val * 8 + (*pos++ - '0');
487 if (*pos >= '0' && *pos <= '7')
488 val = val * 8 + (*pos++ - '0');
508 * wpa_ssid_txt - Convert SSID to a printable string
509 * @ssid: SSID (32-octet string)
510 * @ssid_len: Length of ssid in octets
511 * Returns: Pointer to a printable string
513 * This function can be used to convert SSIDs into printable form. In most
514 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
515 * does not limit the used character set, so anything could be used in an SSID.
517 * This function uses a static buffer, so only one call can be used at the
518 * time, i.e., this is not re-entrant and the returned buffer must be used
519 * before calling this again.
521 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
523 static char ssid_txt[32 * 4 + 1];
530 printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
535 void * __hide_aliasing_typecast(void *foo)
541 char * wpa_config_parse_string(const char *value, size_t *len)
547 pos = os_strrchr(value, '"');
548 if (pos == NULL || pos[1] != '\0')
551 str = dup_binstr(value, *len);
555 } else if (*value == 'P' && value[1] == '"') {
560 pos = os_strrchr(value, '"');
561 if (pos == NULL || pos[1] != '\0')
564 tstr = dup_binstr(value, tlen);
568 str = os_malloc(tlen + 1);
574 *len = printf_decode((u8 *) str, tlen + 1, tstr);
580 size_t tlen, hlen = os_strlen(value);
584 str = os_malloc(tlen + 1);
587 if (hexstr2bin(value, str, tlen)) {
598 int is_hex(const u8 *data, size_t len)
602 for (i = 0; i < len; i++) {
603 if (data[i] < 32 || data[i] >= 127)
610 size_t merge_byte_arrays(u8 *res, size_t res_len,
611 const u8 *src1, size_t src1_len,
612 const u8 *src2, size_t src2_len)
616 os_memset(res, 0, res_len);
619 if (src1_len >= res_len) {
620 os_memcpy(res, src1, res_len);
624 os_memcpy(res, src1, src1_len);
629 if (len + src2_len >= res_len) {
630 os_memcpy(res + len, src2, res_len - len);
634 os_memcpy(res + len, src2, src2_len);
642 char * dup_binstr(const void *src, size_t len)
648 res = os_malloc(len + 1);
651 os_memcpy(res, src, len);
658 int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
660 struct wpa_freq_range *freq = NULL, *n;
661 unsigned int count = 0;
662 const char *pos, *pos2, *pos3;
665 * Comma separated list of frequency ranges.
666 * For example: 2412-2432,2462,5000-6000
669 while (pos && pos[0]) {
670 n = os_realloc_array(freq, count + 1,
671 sizeof(struct wpa_freq_range));
677 freq[count].min = atoi(pos);
678 pos2 = os_strchr(pos, '-');
679 pos3 = os_strchr(pos, ',');
680 if (pos2 && (!pos3 || pos2 < pos3)) {
682 freq[count].max = atoi(pos2);
684 freq[count].max = freq[count].min;
699 int freq_range_list_includes(const struct wpa_freq_range_list *list,
707 for (i = 0; i < list->num; i++) {
708 if (freq >= list->range[i].min && freq <= list->range[i].max)
716 char * freq_range_list_str(const struct wpa_freq_range_list *list)
718 char *buf, *pos, *end;
726 maxlen = list->num * 30;
727 buf = os_malloc(maxlen);
733 for (i = 0; i < list->num; i++) {
734 struct wpa_freq_range *range = &list->range[i];
736 if (range->min == range->max)
737 res = os_snprintf(pos, end - pos, "%s%u",
738 i == 0 ? "" : ",", range->min);
740 res = os_snprintf(pos, end - pos, "%s%u-%u",
742 range->min, range->max);
743 if (os_snprintf_error(end - pos, res)) {
754 int int_array_len(const int *a)
757 for (i = 0; a && a[i]; i++)
763 void int_array_concat(int **res, const int *a)
768 reslen = int_array_len(*res);
769 alen = int_array_len(a);
771 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
777 for (i = 0; i <= alen; i++)
778 n[reslen + i] = a[i];
783 static int freq_cmp(const void *a, const void *b)
796 void int_array_sort_unique(int *a)
804 alen = int_array_len(a);
805 qsort(a, alen, sizeof(int), freq_cmp);
809 while (a[i] && a[j]) {
822 void int_array_add_unique(int **res, int a)
827 for (reslen = 0; *res && (*res)[reslen]; reslen++) {
828 if ((*res)[reslen] == a)
829 return; /* already in the list */
832 n = os_realloc_array(*res, reslen + 2, sizeof(int));
846 void str_clear_free(char *str)
849 size_t len = os_strlen(str);
850 os_memset(str, 0, len);
856 void bin_clear_free(void *bin, size_t len)
859 os_memset(bin, 0, len);
865 int random_mac_addr(u8 *addr)
867 if (os_get_random(addr, ETH_ALEN) < 0)
869 addr[0] &= 0xfe; /* unicast */
870 addr[0] |= 0x02; /* locally administered */
875 int random_mac_addr_keep_oui(u8 *addr)
877 if (os_get_random(addr + 3, 3) < 0)
879 addr[0] &= 0xfe; /* unicast */
880 addr[0] |= 0x02; /* locally administered */
886 * str_token - Get next token from a string
887 * @buf: String to tokenize. Note that the string might be modified.
888 * @delim: String of delimiters
889 * @context: Pointer to save our context. Should be initialized with
890 * NULL on the first call, and passed for any further call.
891 * Returns: The next token, NULL if there are no more valid tokens.
893 char * str_token(char *str, const char *delim, char **context)
895 char *end, *pos = str;
900 while (*pos && os_strchr(delim, *pos))
906 while (*end && !os_strchr(delim, *end))