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)
39 static const char * hwaddr_parse(const char *txt, u8 *addr)
43 for (i = 0; i < ETH_ALEN; i++) {
51 if (i < ETH_ALEN - 1 && *txt++ != ':')
59 * hwaddr_aton - Convert ASCII string to MAC address (colon-delimited format)
60 * @txt: MAC address as a string (e.g., "00:11:22:33:44:55")
61 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
62 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
64 int hwaddr_aton(const char *txt, u8 *addr)
66 return hwaddr_parse(txt, addr) ? 0 : -1;
71 * hwaddr_masked_aton - Convert ASCII string with optional mask to MAC address (colon-delimited format)
72 * @txt: MAC address with optional mask as a string (e.g., "00:11:22:33:44:55/ff:ff:ff:ff:00:00")
73 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
74 * @mask: Buffer for the MAC address mask (ETH_ALEN = 6 bytes)
75 * @maskable: Flag to indicate whether a mask is allowed
76 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
78 int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable)
82 /* parse address part */
83 r = hwaddr_parse(txt, addr);
87 /* check for optional mask */
88 if (*r == '\0' || isspace(*r)) {
89 /* no mask specified, assume default */
90 os_memset(mask, 0xff, ETH_ALEN);
91 } else if (maskable && *r == '/') {
92 /* mask specified and allowed */
93 r = hwaddr_parse(r + 1, mask);
98 /* mask specified but not allowed or trailing garbage */
107 * hwaddr_compact_aton - Convert ASCII string to MAC address (no colon delimitors format)
108 * @txt: MAC address as a string (e.g., "001122334455")
109 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
110 * Returns: 0 on success, -1 on failure (e.g., string not a MAC address)
112 int hwaddr_compact_aton(const char *txt, u8 *addr)
116 for (i = 0; i < 6; i++) {
125 *addr++ = (a << 4) | b;
132 * hwaddr_aton2 - Convert ASCII string to MAC address (in any known format)
133 * @txt: MAC address as a string (e.g., 00:11:22:33:44:55 or 0011.2233.4455)
134 * @addr: Buffer for the MAC address (ETH_ALEN = 6 bytes)
135 * Returns: Characters used (> 0) on success, -1 on failure
137 int hwaddr_aton2(const char *txt, u8 *addr)
140 const char *pos = txt;
142 for (i = 0; i < 6; i++) {
145 while (*pos == ':' || *pos == '.' || *pos == '-')
154 *addr++ = (a << 4) | b;
162 * hexstr2bin - Convert ASCII hex string into binary data
163 * @hex: ASCII hex string (e.g., "01ab")
164 * @buf: Buffer for the binary data
165 * @len: Length of the text to convert in bytes (of buf); hex will be double
167 * Returns: 0 on success, -1 on failure (invalid hex string)
169 int hexstr2bin(const char *hex, u8 *buf, size_t len)
173 const char *ipos = hex;
176 for (i = 0; i < len; i++) {
187 int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask)
193 for (i = 0; i < ETH_ALEN; i++) {
194 if (mask[i] != 0xff) {
201 res = os_snprintf(buf, len, MACSTR "/" MACSTR,
202 MAC2STR(addr), MAC2STR(mask));
204 res = os_snprintf(buf, len, MACSTR, MAC2STR(addr));
205 if (os_snprintf_error(len, res))
212 * inc_byte_array - Increment arbitrary length byte array by one
213 * @counter: Pointer to byte array
214 * @len: Length of the counter in bytes
216 * This function increments the last byte of the counter by one and continues
217 * rolling over to more significant bytes if the byte was incremented from
220 void inc_byte_array(u8 *counter, size_t len)
225 if (counter[pos] != 0)
232 void wpa_get_ntp_timestamp(u8 *buf)
238 /* 64-bit NTP timestamp (time from 1900-01-01 00:00:00) */
240 sec = now.sec + 2208988800U; /* Epoch to 1900 */
241 /* Estimate 2^32/10^6 = 4295 - 1/32 - 1/512 */
243 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
244 tmp = host_to_be32(sec);
245 os_memcpy(buf, (u8 *) &tmp, 4);
246 tmp = host_to_be32(usec);
247 os_memcpy(buf + 4, (u8 *) &tmp, 4);
251 * wpa_scnprintf - Simpler-to-use snprintf function
252 * @buf: Output buffer
256 * Simpler snprintf version that doesn't require further error checks - the
257 * return value only indicates how many bytes were actually written, excluding
258 * the NULL byte (i.e., 0 on error, size-1 if buffer is not big enough).
260 int wpa_scnprintf(char *buf, size_t size, const char *fmt, ...)
269 ret = vsnprintf(buf, size, fmt, ap);
274 if ((size_t) ret >= size)
280 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
281 size_t len, int uppercase)
284 char *pos = buf, *end = buf + buf_size;
288 for (i = 0; i < len; i++) {
289 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
291 if (os_snprintf_error(end - pos, ret)) {
302 * wpa_snprintf_hex - Print data as a hex string into a buffer
303 * @buf: Memory area to use as the output buffer
304 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
305 * @data: Data to be printed
306 * @len: Length of data in bytes
307 * Returns: Number of bytes written
309 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
311 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
316 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
317 * @buf: Memory area to use as the output buffer
318 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
319 * @data: Data to be printed
320 * @len: Length of data in bytes
321 * Returns: Number of bytes written
323 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
326 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
330 #ifdef CONFIG_ANSI_C_EXTRA
333 void perror(const char *s)
335 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
336 s, (int) GetLastError());
338 #endif /* _WIN32_WCE */
345 int getopt(int argc, char *const argv[], const char *optstring)
347 static int optchr = 1;
351 if (optind >= argc) {
352 /* all arguments processed */
356 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
357 /* no option characters */
362 if (os_strcmp(argv[optind], "--") == 0) {
363 /* no more options */
368 optopt = argv[optind][optchr];
369 cp = os_strchr(optstring, optopt);
370 if (cp == NULL || optopt == ':') {
371 if (argv[optind][++optchr] == '\0') {
379 /* Argument required */
381 if (argv[optind][optchr + 1]) {
382 /* No space between option and argument */
383 optarg = &argv[optind++][optchr + 1];
384 } else if (++optind >= argc) {
385 /* option requires an argument */
388 /* Argument in the next argv */
389 optarg = argv[optind++];
393 if (argv[optind][++optchr] == '\0') {
401 #endif /* CONFIG_ANSI_C_EXTRA */
404 #ifdef CONFIG_NATIVE_WINDOWS
406 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
407 * @str: Pointer to string to convert
409 * This function converts a unicode string to ASCII using the same
410 * buffer for output. If UNICODE is not set, the buffer is not
413 void wpa_unicode2ascii_inplace(TCHAR *str)
416 char *dst = (char *) str;
418 *dst++ = (char) *str++;
424 TCHAR * wpa_strdup_tchar(const char *str)
428 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
431 wsprintf(buf, L"%S", str);
434 return os_strdup(str);
437 #endif /* CONFIG_NATIVE_WINDOWS */
440 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
442 char *end = txt + maxlen;
445 for (i = 0; i < len; i++) {
475 if (data[i] >= 32 && data[i] <= 127) {
478 txt += os_snprintf(txt, end - txt, "\\x%02x",
489 size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
491 const char *pos = str;
496 if (len + 1 >= maxlen)
549 if (*pos >= '0' && *pos <= '7')
550 val = val * 8 + (*pos++ - '0');
551 if (*pos >= '0' && *pos <= '7')
552 val = val * 8 + (*pos++ - '0');
572 * wpa_ssid_txt - Convert SSID to a printable string
573 * @ssid: SSID (32-octet string)
574 * @ssid_len: Length of ssid in octets
575 * Returns: Pointer to a printable string
577 * This function can be used to convert SSIDs into printable form. In most
578 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
579 * does not limit the used character set, so anything could be used in an SSID.
581 * This function uses a static buffer, so only one call can be used at the
582 * time, i.e., this is not re-entrant and the returned buffer must be used
583 * before calling this again.
585 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
587 static char ssid_txt[32 * 4 + 1];
594 printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
599 void * __hide_aliasing_typecast(void *foo)
605 char * wpa_config_parse_string(const char *value, size_t *len)
611 pos = os_strrchr(value, '"');
612 if (pos == NULL || pos[1] != '\0')
615 str = dup_binstr(value, *len);
619 } else if (*value == 'P' && value[1] == '"') {
624 pos = os_strrchr(value, '"');
625 if (pos == NULL || pos[1] != '\0')
628 tstr = dup_binstr(value, tlen);
632 str = os_malloc(tlen + 1);
638 *len = printf_decode((u8 *) str, tlen + 1, tstr);
644 size_t tlen, hlen = os_strlen(value);
648 str = os_malloc(tlen + 1);
651 if (hexstr2bin(value, str, tlen)) {
662 int is_hex(const u8 *data, size_t len)
666 for (i = 0; i < len; i++) {
667 if (data[i] < 32 || data[i] >= 127)
674 size_t merge_byte_arrays(u8 *res, size_t res_len,
675 const u8 *src1, size_t src1_len,
676 const u8 *src2, size_t src2_len)
680 os_memset(res, 0, res_len);
683 if (src1_len >= res_len) {
684 os_memcpy(res, src1, res_len);
688 os_memcpy(res, src1, src1_len);
693 if (len + src2_len >= res_len) {
694 os_memcpy(res + len, src2, res_len - len);
698 os_memcpy(res + len, src2, src2_len);
706 char * dup_binstr(const void *src, size_t len)
712 res = os_malloc(len + 1);
715 os_memcpy(res, src, len);
722 int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
724 struct wpa_freq_range *freq = NULL, *n;
725 unsigned int count = 0;
726 const char *pos, *pos2, *pos3;
729 * Comma separated list of frequency ranges.
730 * For example: 2412-2432,2462,5000-6000
733 while (pos && pos[0]) {
734 n = os_realloc_array(freq, count + 1,
735 sizeof(struct wpa_freq_range));
741 freq[count].min = atoi(pos);
742 pos2 = os_strchr(pos, '-');
743 pos3 = os_strchr(pos, ',');
744 if (pos2 && (!pos3 || pos2 < pos3)) {
746 freq[count].max = atoi(pos2);
748 freq[count].max = freq[count].min;
763 int freq_range_list_includes(const struct wpa_freq_range_list *list,
771 for (i = 0; i < list->num; i++) {
772 if (freq >= list->range[i].min && freq <= list->range[i].max)
780 char * freq_range_list_str(const struct wpa_freq_range_list *list)
782 char *buf, *pos, *end;
790 maxlen = list->num * 30;
791 buf = os_malloc(maxlen);
797 for (i = 0; i < list->num; i++) {
798 struct wpa_freq_range *range = &list->range[i];
800 if (range->min == range->max)
801 res = os_snprintf(pos, end - pos, "%s%u",
802 i == 0 ? "" : ",", range->min);
804 res = os_snprintf(pos, end - pos, "%s%u-%u",
806 range->min, range->max);
807 if (os_snprintf_error(end - pos, res)) {
818 int int_array_len(const int *a)
821 for (i = 0; a && a[i]; i++)
827 void int_array_concat(int **res, const int *a)
832 reslen = int_array_len(*res);
833 alen = int_array_len(a);
835 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
841 for (i = 0; i <= alen; i++)
842 n[reslen + i] = a[i];
847 static int freq_cmp(const void *a, const void *b)
860 void int_array_sort_unique(int *a)
868 alen = int_array_len(a);
869 qsort(a, alen, sizeof(int), freq_cmp);
873 while (a[i] && a[j]) {
886 void int_array_add_unique(int **res, int a)
891 for (reslen = 0; *res && (*res)[reslen]; reslen++) {
892 if ((*res)[reslen] == a)
893 return; /* already in the list */
896 n = os_realloc_array(*res, reslen + 2, sizeof(int));
910 void str_clear_free(char *str)
913 size_t len = os_strlen(str);
914 os_memset(str, 0, len);
920 void bin_clear_free(void *bin, size_t len)
923 os_memset(bin, 0, len);
929 int random_mac_addr(u8 *addr)
931 if (os_get_random(addr, ETH_ALEN) < 0)
933 addr[0] &= 0xfe; /* unicast */
934 addr[0] |= 0x02; /* locally administered */
939 int random_mac_addr_keep_oui(u8 *addr)
941 if (os_get_random(addr + 3, 3) < 0)
943 addr[0] &= 0xfe; /* unicast */
944 addr[0] |= 0x02; /* locally administered */
950 * str_token - Get next token from a string
951 * @buf: String to tokenize. Note that the string might be modified.
952 * @delim: String of delimiters
953 * @context: Pointer to save our context. Should be initialized with
954 * NULL on the first call, and passed for any further call.
955 * Returns: The next token, NULL if there are no more valid tokens.
957 char * str_token(char *str, const char *delim, char **context)
959 char *end, *pos = str;
964 while (*pos && os_strchr(delim, *pos))
970 while (*end && !os_strchr(delim, *end))
981 size_t utf8_unescape(const char *inp, size_t in_size,
982 char *outp, size_t out_size)
990 in_size = os_strlen(inp);
992 /* Advance past leading single quote */
993 if (*inp == '\'' && in_size) {
999 if (res_size >= out_size)
1004 /* Terminate on bare single quote */
1020 /* NUL terminate if space allows */
1021 if (res_size < out_size)
1028 size_t utf8_escape(const char *inp, size_t in_size,
1029 char *outp, size_t out_size)
1031 size_t res_size = 0;
1036 /* inp may or may not be NUL terminated, but must be if 0 size
1039 in_size = os_strlen(inp);
1042 if (res_size++ >= out_size)
1048 if (res_size++ >= out_size)
1059 /* NUL terminate if space allows */
1060 if (res_size < out_size)