Skip to content

Commit 4341297

Browse files
authored
Fix for crash in WiFiClientSecure when WiFi is disconnected (esp8266#2139)
* WiFiClient: implement stopAll() via stop() * WiFiClientSecure: clean up ClientContext used by axTLS when stop is called (esp8266#2097)
1 parent 6f3785b commit 4341297

File tree

2 files changed

+21
-24
lines changed

2 files changed

+21
-24
lines changed

libraries/ESP8266WiFi/src/WiFiClient.cpp

+5-13
Original file line numberDiff line numberDiff line change
@@ -339,24 +339,16 @@ void WiFiClient::_s_err(void* arg, int8_t err)
339339
void WiFiClient::stopAll()
340340
{
341341
for (WiFiClient* it = _s_first; it; it = it->_next) {
342-
ClientContext* c = it->_client;
343-
if (c) {
344-
c->abort();
345-
c->unref();
346-
it->_client = 0;
347-
}
342+
it->stop();
348343
}
349344
}
350345

351346

352-
void WiFiClient::stopAllExcept(WiFiClient * exC) {
347+
void WiFiClient::stopAllExcept(WiFiClient* except)
348+
{
353349
for (WiFiClient* it = _s_first; it; it = it->_next) {
354-
ClientContext* c = it->_client;
355-
356-
if (c && c != exC->_client) {
357-
c->abort();
358-
c->unref();
359-
it->_client = 0;
350+
if (it != except) {
351+
it->stop();
360352
}
361353
}
362354
}

libraries/ESP8266WiFi/src/WiFiClientSecure.cpp

+16-11
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ static int s_pk_refcnt = 0;
5757
uint8_t* default_certificate = 0;
5858
uint32_t default_certificate_len = 0;
5959
static bool default_certificate_dynamic = false;
60+
static ClientContext* s_io_ctx = nullptr;
6061

6162
static void clear_private_key();
6263
static void clear_certificate();
@@ -94,7 +95,7 @@ class SSLContext {
9495
}
9596

9697
void connect(ClientContext* ctx, const char* hostName, uint32_t timeout_ms) {
97-
_ssl = ssl_client_new(_ssl_ctx, reinterpret_cast<int>(ctx), nullptr, 0, hostName);
98+
_ssl = ssl_client_new(_ssl_ctx, 0, nullptr, 0, hostName);
9899
uint32_t t = millis();
99100

100101
while (millis() - t < timeout_ms && ssl_handshake_status(_ssl) != SSL_OK) {
@@ -211,6 +212,7 @@ WiFiClientSecure::WiFiClientSecure() {
211212
}
212213

213214
WiFiClientSecure::~WiFiClientSecure() {
215+
s_io_ctx = nullptr;
214216
if (_ssl) {
215217
_ssl->unref();
216218
}
@@ -262,6 +264,8 @@ int WiFiClientSecure::_connectSSL(const char* hostName) {
262264
_ssl = nullptr;
263265
}
264266

267+
s_io_ctx = _client;
268+
265269
_ssl = new SSLContext;
266270
_ssl->ref();
267271
_ssl->connect(_client, hostName, 5000);
@@ -325,6 +329,10 @@ size_t WiFiClientSecure::peekBytes(uint8_t *buffer, size_t length) {
325329
yield();
326330
}
327331

332+
if(!_ssl) {
333+
return 0;
334+
}
335+
328336
if(available() < (int) length) {
329337
count = available();
330338
} else {
@@ -363,11 +371,8 @@ uint8_t WiFiClientSecure::connected() {
363371
}
364372

365373
void WiFiClientSecure::stop() {
366-
if (_ssl) {
367-
_ssl->unref();
368-
_ssl = nullptr;
369-
}
370-
return WiFiClient::stop();
374+
s_io_ctx = nullptr;
375+
WiFiClient::stop();
371376
}
372377

373378
static bool parseHexNibble(char pb, uint8_t* res) {
@@ -520,10 +525,10 @@ static void clear_certificate() {
520525
}
521526

522527
extern "C" int ax_port_read(int fd, uint8_t* buffer, size_t count) {
523-
ClientContext* _client = reinterpret_cast<ClientContext*>(fd);
524-
if (_client->state() != ESTABLISHED && !_client->getSize()) {
525-
return -1;
528+
ClientContext* _client = s_io_ctx;
529+
if (!_client || _client->state() != ESTABLISHED && !_client->getSize()) {
526530
errno = EIO;
531+
return -1;
527532
}
528533
size_t cb = _client->read((char*) buffer, count);
529534
if (cb != count) {
@@ -537,8 +542,8 @@ extern "C" int ax_port_read(int fd, uint8_t* buffer, size_t count) {
537542
}
538543

539544
extern "C" int ax_port_write(int fd, uint8_t* buffer, size_t count) {
540-
ClientContext* _client = reinterpret_cast<ClientContext*>(fd);
541-
if (_client->state() != ESTABLISHED) {
545+
ClientContext* _client = s_io_ctx;
546+
if (!_client || _client->state() != ESTABLISHED) {
542547
errno = EIO;
543548
return -1;
544549
}

0 commit comments

Comments
 (0)