utils: Add ssid_parse() function
authorDavid Spinadel <david.spinadel@intel.com>
Wed, 6 Apr 2016 16:42:04 +0000 (19:42 +0300)
committerJouni Malinen <j@w1.fi>
Sat, 16 Apr 2016 18:05:37 +0000 (21:05 +0300)
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>
src/common/ieee802_11_defs.h
src/utils/common.c
src/utils/common.h
wpa_supplicant/wpa_supplicant_i.h

index 4061fa1..b72f350 100644 (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 */
index 9c7d0d4..450e2c6 100644 (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;
+}
index 6f0de69..701dbb2 100644 (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);
index c485891..e6fc457 100644 (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)