Use helper function for writing cipher suite names

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-01-13 17:31:36 +02:00
parent 031453265f
commit 0282a8c46a
5 changed files with 75 additions and 157 deletions

View 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)

View 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;
}

View 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 */

View 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;

View 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;
}