@@ -57,6 +57,7 @@ static int s_pk_refcnt = 0;
57
57
uint8_t * default_certificate = 0 ;
58
58
uint32_t default_certificate_len = 0 ;
59
59
static bool default_certificate_dynamic = false ;
60
+ static ClientContext* s_io_ctx = nullptr ;
60
61
61
62
static void clear_private_key ();
62
63
static void clear_certificate ();
@@ -94,7 +95,7 @@ class SSLContext {
94
95
}
95
96
96
97
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);
98
99
uint32_t t = millis ();
99
100
100
101
while (millis () - t < timeout_ms && ssl_handshake_status (_ssl) != SSL_OK) {
@@ -211,6 +212,7 @@ WiFiClientSecure::WiFiClientSecure() {
211
212
}
212
213
213
214
WiFiClientSecure::~WiFiClientSecure () {
215
+ s_io_ctx = nullptr ;
214
216
if (_ssl) {
215
217
_ssl->unref ();
216
218
}
@@ -262,6 +264,8 @@ int WiFiClientSecure::_connectSSL(const char* hostName) {
262
264
_ssl = nullptr ;
263
265
}
264
266
267
+ s_io_ctx = _client;
268
+
265
269
_ssl = new SSLContext;
266
270
_ssl->ref ();
267
271
_ssl->connect (_client, hostName, 5000 );
@@ -325,6 +329,10 @@ size_t WiFiClientSecure::peekBytes(uint8_t *buffer, size_t length) {
325
329
yield ();
326
330
}
327
331
332
+ if (!_ssl) {
333
+ return 0 ;
334
+ }
335
+
328
336
if (available () < (int ) length) {
329
337
count = available ();
330
338
} else {
@@ -363,11 +371,8 @@ uint8_t WiFiClientSecure::connected() {
363
371
}
364
372
365
373
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 ();
371
376
}
372
377
373
378
static bool parseHexNibble (char pb, uint8_t * res) {
@@ -520,10 +525,10 @@ static void clear_certificate() {
520
525
}
521
526
522
527
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 ()) {
526
530
errno = EIO;
531
+ return -1 ;
527
532
}
528
533
size_t cb = _client->read ((char *) buffer, count);
529
534
if (cb != count) {
@@ -537,8 +542,8 @@ extern "C" int ax_port_read(int fd, uint8_t* buffer, size_t count) {
537
542
}
538
543
539
544
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) {
542
547
errno = EIO;
543
548
return -1 ;
544
549
}
0 commit comments