From 467584f6ca9fda822b4967a52bb7dc4d95f40c18 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Thu, 25 Mar 2021 03:06:24 +0300 Subject: [PATCH 1/6] WiFi: clean up AP SSID setter / getters, support 32 chars --- doc/esp8266wifi/soft-access-point-class.rst | 2 +- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 53 +++++++++++---------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/doc/esp8266wifi/soft-access-point-class.rst b/doc/esp8266wifi/soft-access-point-class.rst index 473960fd63..5dba40ccfa 100644 --- a/doc/esp8266wifi/soft-access-point-class.rst +++ b/doc/esp8266wifi/soft-access-point-class.rst @@ -50,7 +50,7 @@ The first parameter of this function is required, remaining four are optional. Meaning of all parameters is as follows: -- ``ssid`` - character string containing network SSID (max. 31 characters) +- ``ssid`` - character string containing network SSID (max. 32 characters) - ``password`` - optional character string with a password. For WPA2-PSK network it should be at least 8 character long. If not specified, the access point will be open for anybody to connect, (max. 63 characters). - ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1. - ``hidden`` - optional parameter, if set to ``true`` will hide SSID. diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 042f809807..392e142e96 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -54,10 +54,13 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r * @return equal */ static bool softap_config_equal(const softap_config& lhs, const softap_config& rhs) { - if(strcmp(reinterpret_cast(lhs.ssid), reinterpret_cast(rhs.ssid)) != 0) { + if(lhs.ssid_len != rhs.ssid_len) { return false; } - if(strcmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password)) != 0) { + if(memcmp(lhs.ssid, rhs.ssid, lhs.ssid_len) != 0) { + return false; + } + if(strncmp(reinterpret_cast(lhs.password), reinterpret_cast(rhs.password), sizeof(softap_config::password)) != 0) { return false; } if(lhs.channel != rhs.channel) { @@ -85,7 +88,7 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r /** * Set up an access point - * @param ssid Pointer to the SSID (max 31 char). + * @param ssid Pointer to the SSID (max 32 char). * @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char). * @param channel WiFi channel number, 1 - 13. * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) @@ -99,13 +102,15 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch return false; } - if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) { + size_t ssid_len = strlen(ssid); + if(!ssid || ssid_len == 0 || ssid_len > 32) { // fail SSID too long or missing! DEBUG_WIFI("[AP] SSID too long or missing!\n"); return false; } - if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) { + size_t pass_len = strlen(passphrase); + if(passphrase && pass_len > 0 && (pass_len > 63 || pass_len < 8)) { // fail passphrase to long or short! DEBUG_WIFI("[AP] fail passphrase too long or short!\n"); return false; @@ -114,14 +119,13 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch bool ret = true; struct softap_config conf; - strcpy(reinterpret_cast(conf.ssid), ssid); - conf.channel = channel; - conf.ssid_len = strlen(ssid); - conf.ssid_hidden = ssid_hidden; - conf.max_connection = max_connection; - conf.beacon_interval = 100; + memcpy(reinterpret_cast(conf.ssid), ssid, ssid_len); + if (ssid_len < 32) { + conf.ssid[ssid_len] = '\0'; + } + conf.ssid_len = ssid_len; - if(!passphrase || strlen(passphrase) == 0) { + if(!passphrase || pass_len == 0) { conf.authmode = AUTH_OPEN; *conf.password = 0; } else { @@ -129,6 +133,11 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch strcpy(reinterpret_cast(conf.password), passphrase); } + conf.channel = channel; + conf.ssid_hidden = ssid_hidden; + conf.max_connection = max_connection; + conf.beacon_interval = 100; + struct softap_config conf_compare; if(WiFi._persistent){ wifi_softap_get_config_default(&conf_compare); @@ -358,25 +367,19 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) { String ESP8266WiFiAPClass::softAPSSID() const { struct softap_config config; wifi_softap_get_config(&config); - char* name = reinterpret_cast(config.ssid); - char ssid[sizeof(config.ssid) + 1]; - memcpy(ssid, name, sizeof(config.ssid)); - ssid[sizeof(config.ssid)] = '\0'; - - return String(ssid); + String ssid; + ssid.concat(reinterpret_cast(config.ssid), config.ssid_len); + return ssid; } /** - * Get the configured(Not-In-Flash) softAP PSK or PASSWORD. - * @return String psk. + * Get the configured(Not-In-Flash) softAP PASSWORD. + * @return String password. */ String ESP8266WiFiAPClass::softAPPSK() const { struct softap_config config; wifi_softap_get_config(&config); char* pass = reinterpret_cast(config.password); - char psk[sizeof(config.password) + 1]; - memcpy(psk, pass, sizeof(config.password)); - psk[sizeof(config.password)] = '\0'; - - return String(psk); + pass[sizeof(config.password) - 1] = '\0'; // it is impossible to set PSK through the Arduino API because we only support WPA2 + return pass; } From 8bba2fec085dc08cddbba8628ad196d200747a3c Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 2 Apr 2021 01:39:22 +0300 Subject: [PATCH 2/6] upd remove comment about passphrase & wpa2, retrieve 64byte psk from the sdk update softAP to accept 64byte psk rename password -> psk update docs --- doc/esp8266wifi/soft-access-point-class.rst | 8 ++-- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 50 ++++++++++++--------- libraries/ESP8266WiFi/src/ESP8266WiFiAP.h | 4 +- 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/doc/esp8266wifi/soft-access-point-class.rst b/doc/esp8266wifi/soft-access-point-class.rst index 5dba40ccfa..193e22e4bc 100644 --- a/doc/esp8266wifi/soft-access-point-class.rst +++ b/doc/esp8266wifi/soft-access-point-class.rst @@ -40,18 +40,18 @@ terms `__) of this function WiFi.softAP(ssid) -To set up password protected network, or to configure additional network parameters, use the following overload: +To set up pre-shared key protected network, or to configure additional network parameters, use the following overload: .. code:: cpp - WiFi.softAP(ssid, password, channel, hidden, max_connection) + WiFi.softAP(ssid, psk, channel, hidden, max_connection) The first parameter of this function is required, remaining four are optional. Meaning of all parameters is as follows: - ``ssid`` - character string containing network SSID (max. 32 characters) -- ``password`` - optional character string with a password. For WPA2-PSK network it should be at least 8 character long. If not specified, the access point will be open for anybody to connect, (max. 63 characters). +- ``psk`` - optional character string with a pre-shared key. For WPA2-PSK network it hould be minimum 8 characters long and not longer than 64 characters. If not specified, the access point will be open for anybody to connect. - ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1. - ``hidden`` - optional parameter, if set to ``true`` will hide SSID. - ``max_connection`` - optional parameter to set max simultaneous connected stations, `from 0 to 8 `__. Defaults to 4. Once the max number has been reached, any other station that wants to connect will be forced to wait until an already connected station disconnects. @@ -152,7 +152,7 @@ Disconnect stations from the network established by the soft-AP. WiFi.softAPdisconnect(wifioff) -Function will set currently configured SSID and password of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off. +Function will set currently configured SSID and psk of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off. Function will return ``true`` if operation was successful or ``false`` if otherwise. diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 392e142e96..e6effc0554 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -89,12 +89,12 @@ static bool softap_config_equal(const softap_config& lhs, const softap_config& r /** * Set up an access point * @param ssid Pointer to the SSID (max 32 char). - * @param passphrase For WPA2 min 8 char, for open use NULL (max 63 char). + * @param psk For WPA2 min 8 char max 64 char, for open use "" or NULL. * @param channel WiFi channel number, 1 - 13. * @param ssid_hidden Network cloaking (0 = broadcast SSID, 1 = hide SSID) * @param max_connection Max simultaneous connected clients, 0 - 8. https://bbs.espressif.com/viewtopic.php?f=46&t=481&p=1832&hilit=max_connection#p1832 */ -bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection) { +bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, int ssid_hidden, int max_connection) { if(!WiFi.enableAP(true)) { // enable AP failed @@ -103,16 +103,14 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch } size_t ssid_len = strlen(ssid); - if(!ssid || ssid_len == 0 || ssid_len > 32) { - // fail SSID too long or missing! - DEBUG_WIFI("[AP] SSID too long or missing!\n"); + if(ssid_len == 0 || ssid_len > 32) { + DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len); return false; } - size_t pass_len = strlen(passphrase); - if(passphrase && pass_len > 0 && (pass_len > 63 || pass_len < 8)) { - // fail passphrase to long or short! - DEBUG_WIFI("[AP] fail passphrase too long or short!\n"); + size_t psk_len = strlen(psk); + if(psk_len > 0 && (psk_len > 64 || psk_len < 8)) { + DEBUG_WIFI("[AP] fail psk length %u, too long or short!\n", psk_len); return false; } @@ -121,16 +119,19 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch struct softap_config conf; memcpy(reinterpret_cast(conf.ssid), ssid, ssid_len); if (ssid_len < 32) { - conf.ssid[ssid_len] = '\0'; + conf.ssid[ssid_len] = 0; } conf.ssid_len = ssid_len; - if(!passphrase || pass_len == 0) { - conf.authmode = AUTH_OPEN; - *conf.password = 0; - } else { + if(psk_len) { conf.authmode = AUTH_WPA2_PSK; - strcpy(reinterpret_cast(conf.password), passphrase); + memcpy(reinterpret_cast(conf.password), psk, psk_len); + if (psk_len < 63) { + conf.password[psk_len] = 0; + } + } else { + conf.authmode = AUTH_OPEN; + conf.password[0] = 0; } conf.channel = channel; @@ -190,8 +191,8 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int ch return ret; } -bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& passphrase, int channel, int ssid_hidden, int max_connection) { - return softAP(ssid.c_str(), passphrase.c_str(), channel, ssid_hidden, max_connection); +bool ESP8266WiFiAPClass::softAP(const String& ssid, const String& psk, int channel, int ssid_hidden, int max_connection) { + return softAP(ssid.c_str(), psk.c_str(), channel, ssid_hidden, max_connection); } /** @@ -367,19 +368,24 @@ String ESP8266WiFiAPClass::softAPmacAddress(void) { String ESP8266WiFiAPClass::softAPSSID() const { struct softap_config config; wifi_softap_get_config(&config); + String ssid; ssid.concat(reinterpret_cast(config.ssid), config.ssid_len); + return ssid; } /** - * Get the configured(Not-In-Flash) softAP PASSWORD. - * @return String password. + * Get the configured(Not-In-Flash) softAP PSK. + * @return String psk. */ String ESP8266WiFiAPClass::softAPPSK() const { struct softap_config config; wifi_softap_get_config(&config); - char* pass = reinterpret_cast(config.password); - pass[sizeof(config.password) - 1] = '\0'; // it is impossible to set PSK through the Arduino API because we only support WPA2 - return pass; + + char* ptr = reinterpret_cast(config.password); + String psk; + psk.concat(ptr, strnlen(ptr, sizeof(config.password)); + + return psk; } diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h index 599c8e0e11..b8b3b8fb12 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.h @@ -36,8 +36,8 @@ class ESP8266WiFiAPClass { public: - bool softAP(const char* ssid, const char* passphrase = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); - bool softAP(const String& ssid,const String& passphrase = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); + bool softAP(const char* ssid, const char* psk = NULL, int channel = 1, int ssid_hidden = 0, int max_connection = 4); + bool softAP(const String& ssid,const String& psk = emptyString,int channel = 1,int ssid_hidden = 0,int max_connection = 4); bool softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet); bool softAPdisconnect(bool wifioff = false); From 635182564af66a589ca00aa18cd00f2d08b78bf4 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 2 Apr 2021 01:44:17 +0300 Subject: [PATCH 3/6] typo --- doc/esp8266wifi/soft-access-point-class.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/esp8266wifi/soft-access-point-class.rst b/doc/esp8266wifi/soft-access-point-class.rst index 193e22e4bc..06e84b08a4 100644 --- a/doc/esp8266wifi/soft-access-point-class.rst +++ b/doc/esp8266wifi/soft-access-point-class.rst @@ -51,7 +51,7 @@ The first parameter of this function is required, remaining four are optional. Meaning of all parameters is as follows: - ``ssid`` - character string containing network SSID (max. 32 characters) -- ``psk`` - optional character string with a pre-shared key. For WPA2-PSK network it hould be minimum 8 characters long and not longer than 64 characters. If not specified, the access point will be open for anybody to connect. +- ``psk`` - optional character string with a pre-shared key. For WPA2-PSK network it should be minimum 8 characters long and not longer than 64 characters. If not specified, the access point will be open for anybody to connect. - ``channel`` - optional parameter to set Wi-Fi channel, from 1 to 13. Default channel = 1. - ``hidden`` - optional parameter, if set to ``true`` will hide SSID. - ``max_connection`` - optional parameter to set max simultaneous connected stations, `from 0 to 8 `__. Defaults to 4. Once the max number has been reached, any other station that wants to connect will be forced to wait until an already connected station disconnects. @@ -152,7 +152,7 @@ Disconnect stations from the network established by the soft-AP. WiFi.softAPdisconnect(wifioff) -Function will set currently configured SSID and psk of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off. +Function will set currently configured SSID and pre-shared key of the soft-AP to null values. The parameter ``wifioff`` is optional. If set to ``true`` it will switch the soft-AP mode off. Function will return ``true`` if operation was successful or ``false`` if otherwise. From 4e9b19ee7991c36f765bc1536be80f37d66ee02e Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Fri, 2 Apr 2021 01:53:06 +0300 Subject: [PATCH 4/6] fixup! typo --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index e6effc0554..cf02a75ea3 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -385,7 +385,7 @@ String ESP8266WiFiAPClass::softAPPSK() const { char* ptr = reinterpret_cast(config.password); String psk; - psk.concat(ptr, strnlen(ptr, sizeof(config.password)); + psk.concat(ptr, strnlen(ptr, sizeof(config.password))); return psk; } From 3b54881b115abc0caeacf767b8878598171202ca Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 3 Apr 2021 08:07:19 +0300 Subject: [PATCH 5/6] strlen cant be expected to check for null --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index cf02a75ea3..390b3236db 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -102,13 +102,13 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, return false; } - size_t ssid_len = strlen(ssid); + size_t ssid_len = ssid ? strlen(ssid) : 0; if(ssid_len == 0 || ssid_len > 32) { DEBUG_WIFI("[AP] SSID length %u, too long or missing!\n", ssid_len); return false; } - size_t psk_len = strlen(psk); + size_t psk_len = psk ? strlen(psk) : 0; if(psk_len > 0 && (psk_len > 64 || psk_len < 8)) { DEBUG_WIFI("[AP] fail psk length %u, too long or short!\n", psk_len); return false; From e7318cbd94da4bbe6196c8bb4ac4c324138f563e Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sun, 4 Apr 2021 04:16:25 +0300 Subject: [PATCH 6/6] check for sizeof instead --- libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp index 390b3236db..d046a33841 100644 --- a/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp +++ b/libraries/ESP8266WiFi/src/ESP8266WiFiAP.cpp @@ -118,7 +118,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, struct softap_config conf; memcpy(reinterpret_cast(conf.ssid), ssid, ssid_len); - if (ssid_len < 32) { + if (ssid_len < sizeof(conf.ssid)) { conf.ssid[ssid_len] = 0; } conf.ssid_len = ssid_len; @@ -126,7 +126,7 @@ bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* psk, int channel, if(psk_len) { conf.authmode = AUTH_WPA2_PSK; memcpy(reinterpret_cast(conf.password), psk, psk_len); - if (psk_len < 63) { + if (psk_len < sizeof(conf.password)) { conf.password[psk_len] = 0; } } else {