Skip to content

Commit 7cd7ed7

Browse files
committed
WiFiClientSecure: clean up ClientContext used by axTLS when stop is called (#2097)
1 parent d143a64 commit 7cd7ed7

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

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)