utils: Add ssid_parse() function

Add a function that parses SSID in text or hex format. In case of the
text format, the SSID is enclosed in double quotes. In case of the hex
format, the SSID must include only hex digits and not be enclosed in
double quotes. The input string may include other arguments after the
SSID.

Signed-off-by: David Spinadel <david.spinadel@intel.com>
This commit is contained in:
David Spinadel 2016-04-06 19:42:04 +03:00 committed by Jouni Malinen
parent e4fbc8d423
commit 624b8a061f
4 changed files with 56 additions and 7 deletions

View File

@ -1523,8 +1523,6 @@ struct rrm_link_measurement_report {
u8 variable[0];
} STRUCT_PACKED;
#define SSID_MAX_LEN 32
/* IEEE Std 802.11ad-2012 - Multi-band element */
struct multi_band_ie {
u8 eid; /* WLAN_EID_MULTI_BAND */

View File

@ -1123,3 +1123,51 @@ int is_ctrl_char(char c)
{
return c > 0 && c < 32;
}
/**
* ssid_parse - Parse a string that contains SSID in hex or text format
* @buf: Input NULL terminated string that contains the SSID
* @ssid: Output SSID
* Returns: 0 on success, -1 otherwise
*
* The SSID has to be enclosed in double quotes for the text format or space
* or NULL terminated string of hex digits for the hex format. buf can include
* additional arguments after the SSID.
*/
int ssid_parse(const char *buf, struct wpa_ssid_value *ssid)
{
char *tmp, *res, *end;
size_t len;
ssid->ssid_len = 0;
tmp = os_strdup(buf);
if (!tmp)
return -1;
if (*tmp != '"') {
end = os_strchr(tmp, ' ');
if (end)
*end = '\0';
} else {
end = os_strchr(tmp + 1, '"');
if (!end) {
os_free(tmp);
return -1;
}
end[1] = '\0';
}
res = wpa_config_parse_string(tmp, &len);
if (res && len <= SSID_MAX_LEN) {
ssid->ssid_len = len;
os_memcpy(ssid->ssid, res, len);
}
os_free(tmp);
os_free(res);
return ssid->ssid_len ? 0 : -1;
}

View File

@ -448,6 +448,13 @@ typedef u64 __bitwise le64;
#endif /* __GNUC__ */
#endif /* __must_check */
#define SSID_MAX_LEN 32
struct wpa_ssid_value {
u8 ssid[SSID_MAX_LEN];
size_t ssid_len;
};
int hwaddr_aton(const char *txt, u8 *addr);
int hwaddr_masked_aton(const char *txt, u8 *addr, u8 *mask, u8 maskable);
int hwaddr_compact_aton(const char *txt, u8 *addr);
@ -464,6 +471,7 @@ int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
size_t len);
int hwaddr_mask_txt(char *buf, size_t len, const u8 *addr, const u8 *mask);
int ssid_parse(const char *buf, struct wpa_ssid_value *ssid);
#ifdef CONFIG_NATIVE_WINDOWS
void wpa_unicode2ascii_inplace(TCHAR *str);

View File

@ -393,11 +393,6 @@ struct wps_ap_info {
u8 uuid[WPS_UUID_LEN];
};
struct wpa_ssid_value {
u8 ssid[SSID_MAX_LEN];
size_t ssid_len;
};
#define WPA_FREQ_USED_BY_INFRA_STATION BIT(0)
#define WPA_FREQ_USED_BY_P2P_CLIENT BIT(1)