Skip to content

Commit

Permalink
Implement for ssid a similar approach as for passphrase (#5411)
Browse files Browse the repository at this point in the history
* Implement for ssid a similar approach as for passphrase

* Additional fixes for 32-char ssid
  • Loading branch information
devyte authored and earlephilhower committed Dec 3, 2018
1 parent d8acfff commit 4941711
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 10 deletions.
10 changes: 8 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,19 @@ void ESP8266WiFiClass::printDiag(Print& p) {
struct station_config conf;
wifi_station_get_config(&conf);

const char* ssid = reinterpret_cast<const char*>(conf.ssid);
char ssid[33]; //ssid can be up to 32chars, => plus null term
memcpy(ssid, conf.ssid, sizeof(conf.ssid));
ssid[32] = 0; //nullterm in case of 32 char ssid

p.print("SSID (");
p.print(strlen(ssid));
p.print("): ");
p.println(ssid);

const char* passphrase = reinterpret_cast<const char*>(conf.password);
char passphrase[65];
memcpy(passphrase, conf.password, sizeof(conf.password));
passphrase[64] = 0;

p.print("Passphrase (");
p.print(strlen(passphrase));
p.print("): ");
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266WiFi/src/ESP8266WiFiMulti.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {

WifiAPEntry newAP;

if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
// fail SSID too long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistAdd] no ssid or ssid too long\n");
return false;
Expand Down Expand Up @@ -230,7 +230,7 @@ bool ESP8266WiFiMulti::APlistAdd(const char* ssid, const char *passphrase) {
}

bool ESP8266WiFiMulti::APlistExists(const char* ssid, const char *passphrase) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
// fail SSID too long or missing!
DEBUG_WIFI_MULTI("[WIFI][APlistExists] no ssid or ssid too long\n");
return false;
Expand Down
15 changes: 10 additions & 5 deletions libraries/ESP8266WiFi/src/ESP8266WiFiSTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static bool sta_config_equal(const station_config& lhs, const station_config& rh
* @return equal
*/
static bool sta_config_equal(const station_config& lhs, const station_config& rhs) {
if(strcmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid)) != 0) {
if(strncmp(reinterpret_cast<const char*>(lhs.ssid), reinterpret_cast<const char*>(rhs.ssid), sizeof(lhs.ssid)) != 0) {
return false;
}

Expand Down Expand Up @@ -108,7 +108,7 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
return WL_CONNECT_FAILED;
}

if(!ssid || *ssid == 0x00 || strlen(ssid) > 31) {
if(!ssid || *ssid == 0x00 || strlen(ssid) > 32) {
// fail SSID too long or missing!
return WL_CONNECT_FAILED;
}
Expand All @@ -119,10 +119,12 @@ wl_status_t ESP8266WiFiSTAClass::begin(const char* ssid, const char *passphrase,
}

struct station_config conf;
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
if(strlen(ssid) == 32)
memcpy(reinterpret_cast<char*>(conf.ssid), ssid, 32); //copied in without null term
else
strcpy(reinterpret_cast<char*>(conf.ssid), ssid);

conf.threshold.authmode = AUTH_OPEN;

if(passphrase) {
conf.threshold.authmode = _useInsecureWEP ? AUTH_WEP : AUTH_WPA_PSK;
if (strlen(passphrase) == 64) // it's not a passphrase, is the PSK, which is copied into conf.password without null term
Expand Down Expand Up @@ -524,7 +526,10 @@ wl_status_t ESP8266WiFiSTAClass::status() {
String ESP8266WiFiSTAClass::SSID() const {
struct station_config conf;
wifi_station_get_config(&conf);
return String(reinterpret_cast<char*>(conf.ssid));
char tmp[33]; //ssid can be up to 32chars, => plus null term
memcpy(tmp, conf.ssid, sizeof(conf.ssid));
tmp[32] = 0; //nullterm in case of 32 char ssid
return String(reinterpret_cast<char*>(tmp));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion libraries/ESP8266WiFi/src/ESP8266WiFiScan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,11 @@ String ESP8266WiFiScanClass::SSID(uint8_t i) {
if(!it) {
return "";
}
char tmp[33]; //ssid can be up to 32chars, => plus null term
memcpy(tmp, it->ssid, sizeof(it->ssid));
tmp[32] = 0; //nullterm in case of 32 char ssid

return String(reinterpret_cast<const char*>(it->ssid));
return String(reinterpret_cast<const char*>(tmp));
}


Expand Down

0 comments on commit 4941711

Please sign in to comment.