From 0282a8c46a0f3792b622211a3ca710e32a86ac0d Mon Sep 17 00:00:00 2001 From: Jouni Malinen Date: Sun, 13 Jan 2013 17:31:36 +0200 Subject: [PATCH] Use helper function for writing cipher suite names Signed-hostap: Jouni Malinen --- hostapd/ctrl_iface.c | 63 +++++++--------------------------- src/common/wpa_common.c | 52 ++++++++++++++++++++++++++++ src/common/wpa_common.h | 1 + wpa_supplicant/config.c | 67 +++---------------------------------- wpa_supplicant/ctrl_iface.c | 49 +++------------------------ 5 files changed, 75 insertions(+), 157 deletions(-) diff --git a/hostapd/ctrl_iface.c b/hostapd/ctrl_iface.c index 3c9071caa..93b740eba 100644 --- a/hostapd/ctrl_iface.c +++ b/hostapd/ctrl_iface.c @@ -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) diff --git a/src/common/wpa_common.c b/src/common/wpa_common.c index 6c99b4933..fdf418f44 100644 --- a/src/common/wpa_common.c +++ b/src/common/wpa_common.c @@ -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; +} diff --git a/src/common/wpa_common.h b/src/common/wpa_common.h index 2423cc1db..a23038a05 100644 --- a/src/common/wpa_common.h +++ b/src/common/wpa_common.h @@ -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 */ diff --git a/wpa_supplicant/config.c b/wpa_supplicant/config.c index f90dc8852..2c52c682e 100644 --- a/wpa_supplicant/config.c +++ b/wpa_supplicant/config.c @@ -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; diff --git a/wpa_supplicant/ctrl_iface.c b/wpa_supplicant/ctrl_iface.c index 0dad5c7bf..f10817927 100644 --- a/wpa_supplicant/ctrl_iface.c +++ b/wpa_supplicant/ctrl_iface.c @@ -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; }