]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Use helper function for writing cipher suite names
authorJouni Malinen <j@w1.fi>
Sun, 13 Jan 2013 15:31:36 +0000 (17:31 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 13 Jan 2013 15:31:36 +0000 (17:31 +0200)
Signed-hostap: Jouni Malinen <j@w1.fi>

hostapd/ctrl_iface.c
src/common/wpa_common.c
src/common/wpa_common.h
wpa_supplicant/config.c
wpa_supplicant/ctrl_iface.c

index 3c9071caa3aff2d2027d59fa53f06fc376068ce1..93b740ebafb5af89a6cc63d3191142eef050c883 100644 (file)
@@ -635,20 +635,9 @@ static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
                pos += ret;
        }
 
-       if (hapd->conf->wpa && hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
-               ret = os_snprintf(pos, end - pos, "group_cipher=CCMP\n");
-               if (ret < 0 || ret >= end - pos)
-                       return pos - buf;
-               pos += ret;
-       } else if (hapd->conf->wpa &&
-                  hapd->conf->wpa_group == WPA_CIPHER_GCMP) {
-               ret = os_snprintf(pos, end - pos, "group_cipher=GCMP\n");
-               if (ret < 0 || ret >= end - pos)
-                       return pos - buf;
-               pos += ret;
-       } else if (hapd->conf->wpa &&
-                  hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
-               ret = os_snprintf(pos, end - pos, "group_cipher=TKIP\n");
+       if (hapd->conf->wpa) {
+               ret = os_snprintf(pos, end - pos, "group_cipher=%s\n",
+                                 wpa_cipher_txt(hapd->conf->wpa_group));
                if (ret < 0 || ret >= end - pos)
                        return pos - buf;
                pos += ret;
@@ -660,24 +649,11 @@ static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
                        return pos - buf;
                pos += ret;
 
-               if (hapd->conf->rsn_pairwise & WPA_CIPHER_CCMP) {
-                       ret = os_snprintf(pos, end - pos, "CCMP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
-               if (hapd->conf->rsn_pairwise & WPA_CIPHER_GCMP) {
-                       ret = os_snprintf(pos, end - pos, "GCMP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
-               if (hapd->conf->rsn_pairwise & WPA_CIPHER_TKIP) {
-                       ret = os_snprintf(pos, end - pos, "TKIP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
+               ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
+                                       " ");
+               if (ret < 0)
+                       return pos - buf;
+               pos += ret;
 
                ret = os_snprintf(pos, end - pos, "\n");
                if (ret < 0 || ret >= end - pos)
@@ -691,24 +667,11 @@ static int hostapd_ctrl_iface_get_config(struct hostapd_data *hapd,
                        return pos - buf;
                pos += ret;
 
-               if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
-                       ret = os_snprintf(pos, end - pos, "CCMP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
-               if (hapd->conf->wpa_pairwise & WPA_CIPHER_GCMP) {
-                       ret = os_snprintf(pos, end - pos, "GCMP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
-               if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
-                       ret = os_snprintf(pos, end - pos, "TKIP ");
-                       if (ret < 0 || ret >= end - pos)
-                               return pos - buf;
-                       pos += ret;
-               }
+               ret = wpa_write_ciphers(pos, end, hapd->conf->rsn_pairwise,
+                                       " ");
+               if (ret < 0)
+                       return pos - buf;
+               pos += ret;
 
                ret = os_snprintf(pos, end - pos, "\n");
                if (ret < 0 || ret >= end - pos)
index 6c99b49335f9e7251aff008917ec01911723eb4b..fdf418f449bd2a10027ad62d94a1f6c7dd62cc07 100644 (file)
@@ -1291,3 +1291,55 @@ int wpa_parse_cipher(const char *value)
 
        return val;
 }
+
+
+int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim)
+{
+       char *pos = start;
+       int ret;
+
+       if (ciphers & WPA_CIPHER_CCMP) {
+               ret = os_snprintf(pos, end - pos, "%sCCMP",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+       if (ciphers & WPA_CIPHER_GCMP) {
+               ret = os_snprintf(pos, end - pos, "%sGCMP",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+       if (ciphers & WPA_CIPHER_TKIP) {
+               ret = os_snprintf(pos, end - pos, "%sTKIP",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+       if (ciphers & WPA_CIPHER_WEP104) {
+               ret = os_snprintf(pos, end - pos, "%sWEP104",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+       if (ciphers & WPA_CIPHER_WEP40) {
+               ret = os_snprintf(pos, end - pos, "%sWEP40",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+       if (ciphers & WPA_CIPHER_NONE) {
+               ret = os_snprintf(pos, end - pos, "%sNONE",
+                                 pos == start ? "" : delim);
+               if (ret < 0 || ret >= end - pos)
+                       return -1;
+               pos += ret;
+       }
+
+       return pos - start;
+}
index 2423cc1db77f1594c7f327a5045ca919f0f464fa..a23038a055ac35a54134d0298616b953fd2669d7 100644 (file)
@@ -399,5 +399,6 @@ int wpa_cipher_put_suites(u8 *pos, int ciphers);
 int wpa_pick_pairwise_cipher(int ciphers, int none_allowed);
 int wpa_pick_group_cipher(int ciphers);
 int wpa_parse_cipher(const char *value);
+int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim);
 
 #endif /* WPA_COMMON_H */
index f90dc88528fe729259b00267fd050e29a6cc8b10..2c52c682e5f1af5dd7868d5d12ab99c7ae903818 100644 (file)
@@ -647,72 +647,13 @@ static int wpa_config_parse_cipher(int line, const char *value)
 #ifndef NO_CONFIG_WRITE
 static char * wpa_config_write_cipher(int cipher)
 {
-       char *buf, *pos, *end;
-       int ret;
-
-       pos = buf = os_zalloc(50);
+       char *buf = os_zalloc(50);
        if (buf == NULL)
                return NULL;
-       end = buf + 50;
-
-       if (cipher & WPA_CIPHER_CCMP) {
-               ret = os_snprintf(pos, end - pos, "%sCCMP",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
-       }
-
-       if (cipher & WPA_CIPHER_GCMP) {
-               ret = os_snprintf(pos, end - pos, "%sGCMP",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
-       }
-
-       if (cipher & WPA_CIPHER_TKIP) {
-               ret = os_snprintf(pos, end - pos, "%sTKIP",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
-       }
-
-       if (cipher & WPA_CIPHER_WEP104) {
-               ret = os_snprintf(pos, end - pos, "%sWEP104",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
-       }
 
-       if (cipher & WPA_CIPHER_WEP40) {
-               ret = os_snprintf(pos, end - pos, "%sWEP40",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
-       }
-
-       if (cipher & WPA_CIPHER_NONE) {
-               ret = os_snprintf(pos, end - pos, "%sNONE",
-                                 pos == buf ? "" : " ");
-               if (ret < 0 || ret >= end - pos) {
-                       end[-1] = '\0';
-                       return buf;
-               }
-               pos += ret;
+       if (wpa_write_ciphers(buf, buf + 50, cipher, " ") < 0) {
+               os_free(buf);
+               return NULL;
        }
 
        return buf;
index 0dad5c7bf07e80a7a673e36484d9fa9a71987081..f10817927fd8c6e0bbe99420fee322b39d459f37 100644 (file)
@@ -1717,54 +1717,15 @@ static int wpa_supplicant_ctrl_iface_list_networks(
 
 static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
 {
-       int first = 1, ret;
+       int ret;
        ret = os_snprintf(pos, end - pos, "-");
        if (ret < 0 || ret >= end - pos)
                return pos;
        pos += ret;
-       if (cipher & WPA_CIPHER_NONE) {
-               ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
-       if (cipher & WPA_CIPHER_WEP40) {
-               ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
-       if (cipher & WPA_CIPHER_WEP104) {
-               ret = os_snprintf(pos, end - pos, "%sWEP104",
-                                 first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
-       if (cipher & WPA_CIPHER_TKIP) {
-               ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
-       if (cipher & WPA_CIPHER_CCMP) {
-               ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
-       if (cipher & WPA_CIPHER_GCMP) {
-               ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : "+");
-               if (ret < 0 || ret >= end - pos)
-                       return pos;
-               pos += ret;
-               first = 0;
-       }
+       ret = wpa_write_ciphers(pos, end, cipher, "+");
+       if (ret < 0)
+               return pos;
+       pos += ret;
        return pos;
 }