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)
281 int wpa_snprintf_hex_sep(char *buf, size_t buf_size, const u8 *data, size_t len,
285 char *pos = buf, *end = buf + buf_size;
291 for (i = 0; i < len; i++) {
292 ret = os_snprintf(pos, end - pos, "%02x%c",
294 if (os_snprintf_error(end - pos, ret)) {
305 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
306 size_t len, int uppercase)
309 char *pos = buf, *end = buf + buf_size;
313 for (i = 0; i < len; i++) {
314 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
316 if (os_snprintf_error(end - pos, ret)) {
327 * wpa_snprintf_hex - Print data as a hex string into a buffer
328 * @buf: Memory area to use as the output buffer
329 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
330 * @data: Data to be printed
331 * @len: Length of data in bytes
332 * Returns: Number of bytes written
334 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
336 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
341 * wpa_snprintf_hex_uppercase - Print data as a upper case hex string into buf
342 * @buf: Memory area to use as the output buffer
343 * @buf_size: Maximum buffer size in bytes (should be at least 2 * len + 1)
344 * @data: Data to be printed
345 * @len: Length of data in bytes
346 * Returns: Number of bytes written
348 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
351 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
355 #ifdef CONFIG_ANSI_C_EXTRA
358 void perror(const char *s)
360 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
361 s, (int) GetLastError());
363 #endif /* _WIN32_WCE */
370 int getopt(int argc, char *const argv[], const char *optstring)
372 static int optchr = 1;
376 if (optind >= argc) {
377 /* all arguments processed */
381 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
382 /* no option characters */
387 if (os_strcmp(argv[optind], "--") == 0) {
388 /* no more options */
393 optopt = argv[optind][optchr];
394 cp = os_strchr(optstring, optopt);
395 if (cp == NULL || optopt == ':') {
396 if (argv[optind][++optchr] == '\0') {
404 /* Argument required */
406 if (argv[optind][optchr + 1]) {
407 /* No space between option and argument */
408 optarg = &argv[optind++][optchr + 1];
409 } else if (++optind >= argc) {
410 /* option requires an argument */
413 /* Argument in the next argv */
414 optarg = argv[optind++];
418 if (argv[optind][++optchr] == '\0') {
426 #endif /* CONFIG_ANSI_C_EXTRA */
429 #ifdef CONFIG_NATIVE_WINDOWS
431 * wpa_unicode2ascii_inplace - Convert unicode string into ASCII
432 * @str: Pointer to string to convert
434 * This function converts a unicode string to ASCII using the same
435 * buffer for output. If UNICODE is not set, the buffer is not
438 void wpa_unicode2ascii_inplace(TCHAR *str)
441 char *dst = (char *) str;
443 *dst++ = (char) *str++;
449 TCHAR * wpa_strdup_tchar(const char *str)
453 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
456 wsprintf(buf, L"%S", str);
459 return os_strdup(str);
462 #endif /* CONFIG_NATIVE_WINDOWS */
465 void printf_encode(char *txt, size_t maxlen, const u8 *data, size_t len)
467 char *end = txt + maxlen;
470 for (i = 0; i < len; i++) {
500 if (data[i] >= 32 && data[i] <= 127) {
503 txt += os_snprintf(txt, end - txt, "\\x%02x",
514 size_t printf_decode(u8 *buf, size_t maxlen, const char *str)
516 const char *pos = str;
521 if (len + 1 >= maxlen)
574 if (*pos >= '0' && *pos <= '7')
575 val = val * 8 + (*pos++ - '0');
576 if (*pos >= '0' && *pos <= '7')
577 val = val * 8 + (*pos++ - '0');
597 * wpa_ssid_txt - Convert SSID to a printable string
598 * @ssid: SSID (32-octet string)
599 * @ssid_len: Length of ssid in octets
600 * Returns: Pointer to a printable string
602 * This function can be used to convert SSIDs into printable form. In most
603 * cases, SSIDs do not use unprintable characters, but IEEE 802.11 standard
604 * does not limit the used character set, so anything could be used in an SSID.
606 * This function uses a static buffer, so only one call can be used at the
607 * time, i.e., this is not re-entrant and the returned buffer must be used
608 * before calling this again.
610 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
612 static char ssid_txt[32 * 4 + 1];
619 printf_encode(ssid_txt, sizeof(ssid_txt), ssid, ssid_len);
624 void * __hide_aliasing_typecast(void *foo)
630 char * wpa_config_parse_string(const char *value, size_t *len)
636 pos = os_strrchr(value, '"');
637 if (pos == NULL || pos[1] != '\0')
640 str = dup_binstr(value, *len);
644 } else if (*value == 'P' && value[1] == '"') {
649 pos = os_strrchr(value, '"');
650 if (pos == NULL || pos[1] != '\0')
653 tstr = dup_binstr(value, tlen);
657 str = os_malloc(tlen + 1);
663 *len = printf_decode((u8 *) str, tlen + 1, tstr);
669 size_t tlen, hlen = os_strlen(value);
673 str = os_malloc(tlen + 1);
676 if (hexstr2bin(value, str, tlen)) {
687 int is_hex(const u8 *data, size_t len)
691 for (i = 0; i < len; i++) {
692 if (data[i] < 32 || data[i] >= 127)
699 size_t merge_byte_arrays(u8 *res, size_t res_len,
700 const u8 *src1, size_t src1_len,
701 const u8 *src2, size_t src2_len)
705 os_memset(res, 0, res_len);
708 if (src1_len >= res_len) {
709 os_memcpy(res, src1, res_len);
713 os_memcpy(res, src1, src1_len);
718 if (len + src2_len >= res_len) {
719 os_memcpy(res + len, src2, res_len - len);
723 os_memcpy(res + len, src2, src2_len);
731 char * dup_binstr(const void *src, size_t len)
737 res = os_malloc(len + 1);
740 os_memcpy(res, src, len);
747 int freq_range_list_parse(struct wpa_freq_range_list *res, const char *value)
749 struct wpa_freq_range *freq = NULL, *n;
750 unsigned int count = 0;
751 const char *pos, *pos2, *pos3;
754 * Comma separated list of frequency ranges.
755 * For example: 2412-2432,2462,5000-6000
758 while (pos && pos[0]) {
759 n = os_realloc_array(freq, count + 1,
760 sizeof(struct wpa_freq_range));
766 freq[count].min = atoi(pos);
767 pos2 = os_strchr(pos, '-');
768 pos3 = os_strchr(pos, ',');
769 if (pos2 && (!pos3 || pos2 < pos3)) {
771 freq[count].max = atoi(pos2);
773 freq[count].max = freq[count].min;
788 int freq_range_list_includes(const struct wpa_freq_range_list *list,
796 for (i = 0; i < list->num; i++) {
797 if (freq >= list->range[i].min && freq <= list->range[i].max)
805 char * freq_range_list_str(const struct wpa_freq_range_list *list)
807 char *buf, *pos, *end;
815 maxlen = list->num * 30;
816 buf = os_malloc(maxlen);
822 for (i = 0; i < list->num; i++) {
823 struct wpa_freq_range *range = &list->range[i];
825 if (range->min == range->max)
826 res = os_snprintf(pos, end - pos, "%s%u",
827 i == 0 ? "" : ",", range->min);
829 res = os_snprintf(pos, end - pos, "%s%u-%u",
831 range->min, range->max);
832 if (os_snprintf_error(end - pos, res)) {
843 int int_array_len(const int *a)
846 for (i = 0; a && a[i]; i++)
852 void int_array_concat(int **res, const int *a)
857 reslen = int_array_len(*res);
858 alen = int_array_len(a);
860 n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
866 for (i = 0; i <= alen; i++)
867 n[reslen + i] = a[i];
872 static int freq_cmp(const void *a, const void *b)
885 void int_array_sort_unique(int *a)
893 alen = int_array_len(a);
894 qsort(a, alen, sizeof(int), freq_cmp);
898 while (a[i] && a[j]) {
911 void int_array_add_unique(int **res, int a)
916 for (reslen = 0; *res && (*res)[reslen]; reslen++) {
917 if ((*res)[reslen] == a)
918 return; /* already in the list */
921 n = os_realloc_array(*res, reslen + 2, sizeof(int));
935 void str_clear_free(char *str)
938 size_t len = os_strlen(str);
939 os_memset(str, 0, len);
945 void bin_clear_free(void *bin, size_t len)
948 os_memset(bin, 0, len);
954 int random_mac_addr(u8 *addr)
956 if (os_get_random(addr, ETH_ALEN) < 0)
958 addr[0] &= 0xfe; /* unicast */
959 addr[0] |= 0x02; /* locally administered */
964 int random_mac_addr_keep_oui(u8 *addr)
966 if (os_get_random(addr + 3, 3) < 0)
968 addr[0] &= 0xfe; /* unicast */
969 addr[0] |= 0x02; /* locally administered */
975 * str_token - Get next token from a string
976 * @buf: String to tokenize. Note that the string might be modified.
977 * @delim: String of delimiters
978 * @context: Pointer to save our context. Should be initialized with
979 * NULL on the first call, and passed for any further call.
980 * Returns: The next token, NULL if there are no more valid tokens.
982 char * str_token(char *str, const char *delim, char **context)
984 char *end, *pos = str;
989 while (*pos && os_strchr(delim, *pos))
995 while (*end && !os_strchr(delim, *end))
1006 size_t utf8_unescape(const char *inp, size_t in_size,
1007 char *outp, size_t out_size)
1009 size_t res_size = 0;
1015 in_size = os_strlen(inp);
1017 /* Advance past leading single quote */
1018 if (*inp == '\'' && in_size) {
1024 if (res_size >= out_size)
1029 /* Terminate on bare single quote */
1045 /* NUL terminate if space allows */
1046 if (res_size < out_size)
1053 size_t utf8_escape(const char *inp, size_t in_size,
1054 char *outp, size_t out_size)
1056 size_t res_size = 0;
1061 /* inp may or may not be NUL terminated, but must be if 0 size
1064 in_size = os_strlen(inp);
1067 if (res_size++ >= out_size)
1073 if (res_size++ >= out_size)
1084 /* NUL terminate if space allows */
1085 if (res_size < out_size)